Skip to content

Commit fe86349

Browse files
committed
fix: lazy sync for botlist state
1 parent 3f4fd99 commit fe86349

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

src/sdk/GearboxSDK.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ export class GearboxSDK {
173173
// Attaching bot list contract
174174
const botListAddress = this.#addressProvider.getAddress(AP_BOT_LIST, 300);
175175
this.#botListContract = new BotListContract(this, botListAddress);
176-
await this.#botListContract.fetchState(this.currentBlock);
177176

178177
// Attaching gear staking contract
179178
this.#gear = this.#addressProvider.getAddress(AP_GEAR_TOKEN);

src/sdk/constants/address-provider.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,13 @@ export const ADDRESS_PROVIDER: Record<NetworkType, Address> = {
4141
Optimism: "0x3761ca4BFAcFCFFc1B8034e69F19116dD6756726",
4242
Base: NOT_DEPLOYED,
4343
};
44+
45+
/**
46+
* Block number when address provider was deployed
47+
*/
48+
export const ADDRESS_PROVIDER_BLOCK: Record<NetworkType, bigint> = {
49+
Mainnet: 18433056n,
50+
Arbitrum: 184650310n,
51+
Optimism: 118410666n,
52+
Base: 0n,
53+
};

src/sdk/core/BotListV3Contract.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,19 @@ import type {
77

88
import { botListV3Abi } from "../abi";
99
import { BaseContract } from "../base";
10-
import { botPermissionsToString } from "../constants";
10+
import { ADDRESS_PROVIDER_BLOCK, botPermissionsToString } from "../constants";
1111
import type { GearboxSDK } from "../GearboxSDK";
1212
import type { BotListStateHuman } from "../types";
1313

1414
type abi = typeof botListV3Abi;
1515

1616
export class BotListContract extends BaseContract<abi> {
17-
approvedCreditManagers: Set<Address> = new Set();
17+
#approvedCreditManagers?: Set<Address>;
18+
#currentBlock: bigint;
1819

1920
constructor(sdk: GearboxSDK, address: Address) {
2021
super(sdk, { addr: address, name: "BotListV3", abi: botListV3Abi });
22+
this.#currentBlock = ADDRESS_PROVIDER_BLOCK[sdk.provider.networkType];
2123
}
2224

2325
public parseFunctionParams(
@@ -41,15 +43,15 @@ export class BotListContract extends BaseContract<abi> {
4143
}
4244
}
4345

44-
public async fetchState(toBlock: bigint): Promise<void> {
46+
public async syncState(toBlock: bigint): Promise<void> {
4547
const logs = await this.provider.publicClient.getContractEvents({
4648
address: this.address,
4749
abi: this.abi,
48-
fromBlock: 0n,
50+
fromBlock: this.#currentBlock,
4951
toBlock,
5052
});
51-
5253
logs.forEach(e => this.processLog(e));
54+
this.#currentBlock = toBlock;
5355
}
5456

5557
public override processLog(
@@ -85,6 +87,15 @@ export class BotListContract extends BaseContract<abi> {
8587
}
8688
}
8789

90+
public get approvedCreditManagers(): Set<Address> {
91+
if (!this.#approvedCreditManagers) {
92+
throw new Error(
93+
"BotListContract state needs to be synced to load approvedCreditManagers",
94+
);
95+
}
96+
return this.#approvedCreditManagers;
97+
}
98+
8899
public override stateHuman(raw = true): BotListStateHuman {
89100
return super.stateHuman(raw);
90101
}

0 commit comments

Comments
 (0)