Skip to content

Commit 23f2b95

Browse files
committed
feat: add get zappers
1 parent 0689be9 commit 23f2b95

File tree

6 files changed

+66
-24
lines changed

6 files changed

+66
-24
lines changed

scripts/example.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ async function example(): Promise<void> {
1313

1414
await example.run({
1515
addressProvider: ADDRESS_PROVIDER,
16-
marketConfigurator: MARKET_CONFIGURATOR,
16+
marketConfigurators: [MARKET_CONFIGURATOR],
1717
outFile: "example-state.json",
1818
});
1919
logger.info("done");

src/dev/SDKExample.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ export class SDKExample {
7777
await publicClient.waitForTransactionReceipt({ hash });
7878

7979
await this.#sdk.marketRegister.loadMarkets(marketConfigurators, true);
80+
try {
81+
await this.#sdk.marketRegister.loadZappers();
82+
} catch (e) {
83+
this.#logger?.error(`failed to load zappers: ${e}`);
84+
}
8085

8186
this.#logger?.info("attached sdk");
8287
if (outFile) {

src/sdk/GearboxSDK.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ export class GearboxSDK {
339339
botList: this.botListContract.stateHuman(raw),
340340
gearStakingV3: this.gearStakingContract.stateHuman(raw),
341341
},
342-
markets: this.marketRegister.stateHuman(raw),
342+
...this.marketRegister.stateHuman(raw),
343343
};
344344
}
345345

src/sdk/market/MarketRegister.ts

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
import type { Address } from "viem";
22

3-
import { iMarketCompressorAbi } from "../abi";
4-
import type { MarketData } from "../base";
3+
import { iMarketCompressorAbi, iPeripheryCompressorAbi } from "../abi";
4+
import type { MarketData, ZapperData } from "../base";
55
import { SDKConstruct } from "../base";
6-
import { ADDRESS_0X0, AP_MARKET_COMPRESSOR } from "../constants";
6+
import {
7+
ADDRESS_0X0,
8+
AP_MARKET_COMPRESSOR,
9+
AP_PERIPHERY_COMPRESSOR,
10+
} from "../constants";
711
import type { GearboxSDK } from "../GearboxSDK";
8-
import type { ILogger, MarketStateHuman, RawTx, TVL } from "../types";
12+
import type {
13+
ILogger,
14+
MarketStateHuman,
15+
RawTx,
16+
TVL,
17+
ZapperStateHuman,
18+
} from "../types";
919
import { AddressMap, childLogger } from "../utils";
1020
import { simulateMulticall } from "../utils/viem";
1121
import type { CreditSuite } from "./CreditSuite";
@@ -20,6 +30,7 @@ export class MarketRegister extends SDKConstruct {
2030
* Mapping pool.address -> MarketSuite
2131
*/
2232
#markets = new AddressMap<MarketSuite>();
33+
#zappers: ZapperData[] = [];
2334

2435
constructor(sdk: GearboxSDK, markets?: MarketData[]) {
2536
super(sdk);
@@ -45,6 +56,29 @@ export class MarketRegister extends SDKConstruct {
4556
await this.#loadMarkets(marketConfigurators, [], ignoreUpdateablePrices);
4657
}
4758

59+
public async loadZappers(): Promise<void> {
60+
const pcAddr = this.sdk.addressProvider.getAddress(
61+
AP_PERIPHERY_COMPRESSOR,
62+
3_10,
63+
);
64+
this.#logger?.debug(`loading zappers with periphery compressor ${pcAddr}`);
65+
const resp = await this.provider.publicClient.multicall({
66+
contracts: this.markets.map(m => ({
67+
abi: iPeripheryCompressorAbi,
68+
address: pcAddr,
69+
functionName: "getZappers",
70+
args: [m.configurator, m.pool.pool.address],
71+
})),
72+
allowFailure: false,
73+
});
74+
this.#zappers = resp.flat() as any as ZapperData[];
75+
const zappersTokens = this.#zappers.flatMap(z => [z.tokenIn, z.tokenOut]);
76+
for (const t of zappersTokens) {
77+
this.sdk.tokensMeta.upsert(t.addr, t);
78+
this.sdk.provider.addressLabels.set(t.addr as Address, t.symbol);
79+
}
80+
}
81+
4882
public async syncState(): Promise<void> {
4983
const pools = this.markets
5084
.filter(m => m.dirty)
@@ -144,8 +178,20 @@ export class MarketRegister extends SDKConstruct {
144178
return this.markets.map(market => market.state);
145179
}
146180

147-
public stateHuman(raw = true): MarketStateHuman[] {
148-
return this.markets.map(market => market.stateHuman(raw));
181+
public stateHuman(raw = true): {
182+
markets: MarketStateHuman[];
183+
zappers: ZapperStateHuman[];
184+
} {
185+
return {
186+
markets: this.markets.map(market => market.stateHuman(raw)),
187+
zappers: this.zappers.map(z => ({
188+
address: z.baseParams.addr,
189+
contractType: z.baseParams.contractType,
190+
version: Number(z.baseParams.version),
191+
tokenIn: this.labelAddress(z.tokenIn.addr),
192+
tokenOut: this.labelAddress(z.tokenOut.addr),
193+
})),
194+
};
149195
}
150196

151197
public get pools(): PoolSuite[] {
@@ -213,6 +259,10 @@ export class MarketRegister extends SDKConstruct {
213259
return this.#markets;
214260
}
215261

262+
public get zappers(): ZapperData[] {
263+
return this.#zappers;
264+
}
265+
216266
public get markets(): MarketSuite[] {
217267
return this.#markets.values();
218268
}

src/sdk/market/MarketSuite.ts

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

3-
import type { MarketData, ZapperData } from "../base";
3+
import type { MarketData } from "../base";
44
import { SDKConstruct } from "../base";
55
import type { GearboxSDK } from "../GearboxSDK";
66
import type { MarketStateHuman } from "../types";
@@ -22,7 +22,6 @@ export class MarketSuite extends SDKConstruct {
2222
* Original data received from compressor
2323
*/
2424
public readonly state: MarketData;
25-
public readonly zappers: readonly ZapperData[] = [];
2625

2726
constructor(sdk: GearboxSDK, marketData: MarketData) {
2827
super(sdk);
@@ -41,17 +40,12 @@ export class MarketSuite extends SDKConstruct {
4140

4241
this.acl = marketData.acl;
4342

44-
const allTokens = [
45-
...marketData.tokens,
46-
// ...marketData.zappers.flatMap(z => [z.tokenIn, z.tokenOut]),
47-
];
48-
for (const t of allTokens) {
43+
for (const t of marketData.tokens) {
4944
sdk.tokensMeta.upsert(t.addr, t);
5045
sdk.provider.addressLabels.set(t.addr as Address, t.symbol);
5146
}
5247

5348
this.pool = new PoolSuite(sdk, marketData);
54-
// this.zappers = marketData.zappers;
5549

5650
for (let i = 0; i < marketData.creditManagers.length; i++) {
5751
this.creditManagers.push(new CreditSuite(sdk, marketData, i));
@@ -93,13 +87,6 @@ export class MarketSuite extends SDKConstruct {
9387
emergencyLiquidators: this.state.emergencyLiquidators.map(a =>
9488
this.labelAddress(a),
9589
),
96-
zappers: this.zappers.map(z => ({
97-
address: z.baseParams.addr,
98-
contractType: z.baseParams.contractType,
99-
version: Number(z.baseParams.version),
100-
tokenIn: this.labelAddress(z.tokenIn.addr),
101-
tokenOut: this.labelAddress(z.tokenOut.addr),
102-
})),
10390
};
10491
}
10592
}

src/sdk/types/state-human.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,12 +199,12 @@ export interface MarketStateHuman {
199199
pausableAdmins: string[];
200200
unpausableAdmins: string[];
201201
emergencyLiquidators: string[];
202-
zappers: ZapperStateHuman[];
203202
}
204203

205204
export interface GearboxStateHuman {
206205
block: number;
207206
timestamp: number;
208207
core: CoreStateHuman;
209208
markets: MarketStateHuman[];
209+
zappers: ZapperStateHuman[];
210210
}

0 commit comments

Comments
 (0)