Skip to content

Commit afbc11c

Browse files
committed
feat: add state as json
1 parent 714f637 commit afbc11c

File tree

9 files changed

+112
-32
lines changed

9 files changed

+112
-32
lines changed

src/sdk/GearboxSDK.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@ import {
2525
import { MarketRegister } from "./market/MarketRegister";
2626
import { PriceFeedRegister } from "./market/pricefeeds";
2727
import { RouterV3Contract } from "./router";
28-
import type { GearboxStateHuman, ILogger, MultiCall } from "./types";
28+
import type {
29+
GearboxState,
30+
GearboxStateHuman,
31+
ILogger,
32+
MultiCall,
33+
} from "./types";
2934
import { AddressMap, formatBN } from "./utils";
3035
import { Hooks } from "./utils/internal";
3136
import { detectNetwork } from "./utils/viem";
@@ -326,6 +331,14 @@ export class GearboxSDK {
326331
};
327332
}
328333

334+
public get state(): GearboxState {
335+
return {
336+
currentBlock: this.currentBlock,
337+
addressProvider: this.addressProvider.state,
338+
markets: this.marketRegister.state,
339+
};
340+
}
341+
329342
public async tvl(): Promise<void> {
330343
const { tvl, tvlUSD } = await this.marketRegister.tvl();
331344
this.logger?.info(tvl);

src/sdk/core/address-provider/AbstractAddressProviderContract.ts

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
1-
import type { Abi, Address } from "viem";
1+
import { type Abi, type Address, stringToHex } from "viem";
22

33
import { BaseContract } from "../../base";
4+
import type { BaseContractOptions } from "../../base/BaseContract";
45
import { NO_VERSION } from "../../constants";
6+
import type { GearboxSDK } from "../../GearboxSDK";
57
import type { AddressProviderV3StateHuman } from "../../types";
8+
import type { AddressProviderState } from "./types";
69

710
export default abstract class AbstractAddressProviderContract<
811
abi extends Abi | readonly unknown[],
912
> extends BaseContract<abi> {
1013
#addresses: Record<string, Record<number, Address>> = {};
11-
#versions: Record<string, Set<number>> = {};
1214
#latest: Record<string, number> = {};
1315

16+
constructor(
17+
sdk: GearboxSDK,
18+
args: BaseContractOptions<abi>,
19+
addresses: Record<string, Record<number, Address>> = {},
20+
) {
21+
super(sdk, args);
22+
this.#addresses = addresses;
23+
}
24+
1425
protected setInternalAddress(key: string, address: Address, version: number) {
1526
if (!this.#addresses[key]) {
1627
this.#addresses[key] = {};
@@ -20,12 +31,6 @@ export default abstract class AbstractAddressProviderContract<
2031
if (!this.#latest[key] || version > this.#latest[key]) {
2132
this.#latest[key] = version;
2233
}
23-
24-
if (!this.#versions[key]) {
25-
this.#versions[key] = new Set();
26-
}
27-
28-
this.#versions[key].add(version);
2934
}
3035

3136
public getAddress(contract: string, version = NO_VERSION): Address {
@@ -53,6 +58,18 @@ export default abstract class AbstractAddressProviderContract<
5358
return this.getAddress(contract, this.#latest[contract]);
5459
}
5560

61+
public get state(): AddressProviderState {
62+
return {
63+
baseParams: {
64+
addr: this.address,
65+
version: BigInt(this.version),
66+
contractType: stringToHex(this.contractType, { size: 32 }),
67+
serializedParams: "0x0",
68+
},
69+
addresses: this.#addresses,
70+
};
71+
}
72+
5673
public override stateHuman(raw = true): AddressProviderV3StateHuman {
5774
return {
5875
...super.stateHuman(raw),

src/sdk/core/address-provider/AddressProviderV3Contract.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,21 @@ export class AddressProviderContractV3
1919
extends AbstractAddressProviderContract<abi>
2020
implements IAddressProviderContract
2121
{
22-
constructor(sdk: GearboxSDK, address: Address) {
23-
super(sdk, {
24-
addr: address,
25-
name: "AddressProviderV3",
26-
abi,
27-
version: 3_00,
28-
});
22+
constructor(
23+
sdk: GearboxSDK,
24+
address: Address,
25+
addresses: Record<string, Record<number, Address>> = {},
26+
) {
27+
super(
28+
sdk,
29+
{
30+
addr: address,
31+
name: "AddressProviderV3",
32+
abi,
33+
version: 3_00,
34+
},
35+
addresses,
36+
);
2937
}
3038

3139
public parseFunctionParams(

src/sdk/core/address-provider/AddressProviderV3_1Contract.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,21 @@ export class AddressProviderContractV3_1
1818
extends AbstractAddressProviderContract<abi>
1919
implements IAddressProviderContract
2020
{
21-
constructor(sdk: GearboxSDK, address: Address) {
22-
super(sdk, {
23-
addr: address,
24-
name: "AddressProviderV3_1",
25-
abi,
26-
version: 3_10,
27-
});
21+
constructor(
22+
sdk: GearboxSDK,
23+
address: Address,
24+
addresses: Record<string, Record<number, Address>> = {},
25+
) {
26+
super(
27+
sdk,
28+
{
29+
addr: address,
30+
name: "AddressProviderV3_1",
31+
abi,
32+
version: 3_10,
33+
},
34+
addresses,
35+
);
2836
}
2937

3038
public parseFunctionParams(

src/sdk/core/address-provider/getAddressProvider.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,41 @@ import { iVersionAbi } from "../../abi";
44
import type { GearboxSDK } from "../../GearboxSDK";
55
import { AddressProviderContractV3_1 } from "./AddressProviderV3_1Contract";
66
import { AddressProviderContractV3 } from "./AddressProviderV3Contract";
7-
import type { IAddressProviderContract } from "./types";
7+
import type { AddressProviderState, IAddressProviderContract } from "./types";
8+
9+
export interface GetAddressProviderOptions {
10+
version?: number;
11+
state?: AddressProviderState;
12+
}
813

914
export async function getAddressProvider(
1015
sdk: GearboxSDK,
1116
address: Address,
12-
version?: number,
17+
options?: GetAddressProviderOptions,
1318
): Promise<IAddressProviderContract> {
14-
let v = version;
19+
const addr = options?.state?.baseParams.addr ?? address;
20+
let v = options?.state?.baseParams.version ?? options?.version;
1521
if (!v) {
1622
const vv = await sdk.provider.publicClient.readContract({
17-
address,
23+
address: addr,
1824
abi: iVersionAbi,
1925
functionName: "version",
2026
});
2127
v = Number(vv);
2228
}
23-
2429
switch (v) {
2530
case 3_00:
26-
return new AddressProviderContractV3(sdk, address);
31+
return new AddressProviderContractV3(
32+
sdk,
33+
addr,
34+
options?.state?.addresses,
35+
);
2736
case 3_10:
28-
return new AddressProviderContractV3_1(sdk, address);
37+
return new AddressProviderContractV3_1(
38+
sdk,
39+
addr,
40+
options?.state?.addresses,
41+
);
2942
default:
3043
throw new Error(`Unsupported address provider version: ${v}`);
3144
}

src/sdk/core/address-provider/types.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
import type { Address } from "viem";
22

3-
import type { IBaseContract } from "../../base";
3+
import type { BaseParams, IBaseContract } from "../../base";
44
import type { AddressProviderV3StateHuman } from "../../types";
55

6+
export interface AddressProviderState {
7+
baseParams: BaseParams;
8+
addresses: Record<string, Record<number, Address>>;
9+
}
10+
611
export interface IAddressProviderContract extends IBaseContract {
12+
state: AddressProviderState;
713
getAddress: (contract: string, version?: number) => Address;
814
getLatestVersion: (contract: string) => Address;
915
syncState: (blockNumber: bigint) => Promise<void>;

src/sdk/market/MarketRegister.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,15 @@ export class MarketRegister extends SDKConstruct {
2121
*/
2222
#markets = new AddressMap<MarketFactory>();
2323

24-
constructor(sdk: GearboxSDK) {
24+
constructor(sdk: GearboxSDK, markets?: MarketData[]) {
2525
super(sdk);
2626
this.#logger = childLogger("MarketRegister", sdk.logger);
27+
for (const data of markets ?? []) {
28+
this.#markets.upsert(
29+
data.pool.baseParams.addr,
30+
new MarketFactory(this.sdk, data),
31+
);
32+
}
2733
}
2834

2935
public async loadMarkets(

src/sdk/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export type * from "./logger";
2+
export type * from "./state";
23
export type * from "./state-human";
34
export type * from "./transactions";
45
export type * from "./tvl";

src/sdk/types/state.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import type { MarketData } from "../base";
2+
import type { AddressProviderState } from "../core";
3+
4+
export interface GearboxState {
5+
currentBlock: bigint;
6+
addressProvider: AddressProviderState;
7+
markets: MarketData[];
8+
}

0 commit comments

Comments
 (0)