Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement read-only provider #749

Merged
merged 1 commit into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions projects/sdk/src/lib/BeanstalkSDK.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export type Provider = ethers.providers.JsonRpcProvider;
export type Signer = ethers.Signer;
export type BeanstalkConfig = Partial<{
provider: Provider;
readProvider?: Provider;
signer: Signer;
rpcUrl: string;
subgraphUrl: string;
Expand All @@ -41,6 +42,7 @@ export class BeanstalkSDK {
public DEBUG: boolean;
public signer?: Signer;
public provider: Provider;
public readProvider?: Provider;
public providerOrSigner: Signer | Provider;
public source: DataSource;
public subgraphUrl: string;
Expand Down Expand Up @@ -116,6 +118,7 @@ export class BeanstalkSDK {
} else {
this.provider = (config.signer?.provider as Provider) ?? config.provider!;
}
this.readProvider = config.readProvider;
this.providerOrSigner = config.signer ?? config.provider!;

this.DEBUG = config.DEBUG ?? false;
Expand Down
4 changes: 3 additions & 1 deletion projects/sdk/src/lib/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
Math,
Math__factory,
UsdOracle,
UsdOracle__factory,
UsdOracle__factory
} from "src/constants/generated";
import { BaseContract } from "ethers";

Expand All @@ -53,6 +53,7 @@ export class Contracts {
static sdk: BeanstalkSDK;

public readonly beanstalk: Beanstalk;
public readonly beanstalkRead: Beanstalk;
public readonly beanstalkPrice: BeanstalkPrice;
public readonly fertilizer: BeanstalkFertilizer;

Expand Down Expand Up @@ -90,6 +91,7 @@ export class Contracts {

// Instances
this.beanstalk = Beanstalk__factory.connect(beanstalkAddress, sdk.providerOrSigner);
this.beanstalkRead = Beanstalk__factory.connect(beanstalkAddress, sdk.readProvider ?? sdk.providerOrSigner);
this.beanstalkPrice = BeanstalkPrice__factory.connect(beanstalkPriceAddress, sdk.providerOrSigner);
this.fertilizer = BeanstalkFertilizer__factory.connect(beanstalkFertilizerAddress, sdk.providerOrSigner);

Expand Down
26 changes: 13 additions & 13 deletions projects/sdk/src/lib/events/EventManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,18 @@ export class EventManager {
const toBlock = opts.toBlock ?? "latest";

return Promise.all([
this.sdk.contracts.beanstalk.queryFilter(
this.sdk.contracts.beanstalk.filters.AddDeposit(account, opts.token?.address),
this.sdk.contracts.beanstalkRead.queryFilter(
this.sdk.contracts.beanstalkRead.filters.AddDeposit(account, opts.token?.address),
fromBlock,
toBlock
),
this.sdk.contracts.beanstalk.queryFilter(
this.sdk.contracts.beanstalk.filters.RemoveDeposit(account, opts.token?.address),
this.sdk.contracts.beanstalkRead.queryFilter(
this.sdk.contracts.beanstalkRead.filters.RemoveDeposit(account, opts.token?.address),
fromBlock,
toBlock
),
this.sdk.contracts.beanstalk.queryFilter(
this.sdk.contracts.beanstalk.filters.RemoveDeposits(account, opts.token?.address),
this.sdk.contracts.beanstalkRead.queryFilter(
this.sdk.contracts.beanstalkRead.filters.RemoveDeposits(account, opts.token?.address),
fromBlock,
toBlock
)
Expand All @@ -87,19 +87,19 @@ export class EventManager {
const toBlock = opts.toBlock ?? "latest";

return Promise.all([
this.sdk.contracts.beanstalk.queryFilter(
this.sdk.contracts.beanstalk.filters["Sow(address,uint256,uint256,uint256)"](account),
this.sdk.contracts.beanstalkRead.queryFilter(
this.sdk.contracts.beanstalkRead.filters["Sow(address,uint256,uint256,uint256)"](account),
fromBlock,
toBlock
),
this.sdk.contracts.beanstalk.queryFilter(this.sdk.contracts.beanstalk.filters.Harvest(account), fromBlock, toBlock),
this.sdk.contracts.beanstalk.queryFilter(
this.sdk.contracts.beanstalk.filters.PlotTransfer(account, null), // from
this.sdk.contracts.beanstalkRead.queryFilter(this.sdk.contracts.beanstalkRead.filters.Harvest(account), fromBlock, toBlock),
this.sdk.contracts.beanstalkRead.queryFilter(
this.sdk.contracts.beanstalkRead.filters.PlotTransfer(account, null), // from
fromBlock,
toBlock
),
this.sdk.contracts.beanstalk.queryFilter(
this.sdk.contracts.beanstalk.filters.PlotTransfer(null, account), // to
this.sdk.contracts.beanstalkRead.queryFilter(
this.sdk.contracts.beanstalkRead.filters.PlotTransfer(null, account), // to
fromBlock,
toBlock
)
Expand Down
1 change: 1 addition & 0 deletions projects/ui/src/components/App/SdkProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const useBeanstalkSdkContext = () => {

const _sdk = new BeanstalkSDK({
provider: provider as any,
readProvider: provider as any,
signer: signer ?? undefined,
source: datasource,
DEBUG: IS_DEVELOPMENT_ENV,
Expand Down
25 changes: 0 additions & 25 deletions projects/ui/src/graph/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,31 +65,6 @@ const mergeUsingSeasons: (keyArgs: string[]) => FieldPolicy = (keyArgs) => ({
existing.length - 1 // clamp to last index
);

console.debug('[apollo/client/read@seasons] READ:');
console.debug(
`| left: index = ${left}, season = ${readField(
'season',
existing[left]
)}`
);
console.debug(
`| right: index = ${right}, season = ${readField(
'season',
existing[right]
)}`
);
console.debug(`| existing.length = ${existing.length}`);
console.debug(
`| existing[0] = ${readField('season', existing[0])}`,
existing
);
console.debug(
`| existing[${existing.length - 1}] = ${readField(
'season',
existing[existing.length - 1]
)}`
);

// If one of the endpoints is missing, force refresh
if (!existing[left] || !existing[right]) return;

Expand Down