Skip to content

Commit 83a4ad3

Browse files
committed
fix: distributors loading
1 parent b35bcb8 commit 83a4ad3

File tree

2 files changed

+47
-8
lines changed

2 files changed

+47
-8
lines changed

src/plugins/degen-distributors/DegenDistributorsPlugin.ts

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

33
import type { IGearboxSDKPlugin, MarketSuite } from "../../sdk/index.js";
44
import { AddressMap, BasePlugin } from "../../sdk/index.js";
5+
import { MarketConfiguratorContract } from "../../sdk/market/MarketConfiguratorContract.js";
56
import type { DegenDistributorsStateHuman } from "./types.js";
67

78
export interface DegenDistributorsPluginState {
@@ -36,15 +37,32 @@ export class DegenDistributorsPlugin
3637
`loading degen distributors for ${this.sdk.networkType}`,
3738
);
3839

39-
const distributors = await Promise.allSettled(
40-
configurators.map(cfg => cfg.getPeripheryContract("DEGEN_DISTRIBUTOR")),
41-
);
40+
const distributors =
41+
await MarketConfiguratorContract.getPeripheryContractBatch(
42+
Object.values(configurators),
43+
this.sdk.client,
44+
"DEGEN_DISTRIBUTOR",
45+
);
4246

4347
const distributorByConfigurator = configurators.reduce<
44-
Record<Address, PromiseSettledResult<Address>>
48+
Record<
49+
Address,
50+
| {
51+
error?: undefined;
52+
result: readonly `0x${string}`[];
53+
status: "success";
54+
}
55+
| {
56+
error: Error;
57+
result?: undefined;
58+
status: "failure";
59+
}
60+
>
4561
>((acc, cfg, index) => {
4662
const cfgLC = cfg.address.toLowerCase() as Address;
47-
acc[cfgLC] = distributors[index];
63+
const distributor = distributors[index];
64+
65+
acc[cfgLC] = distributor;
4866
return acc;
4967
}, {});
5068

@@ -55,11 +73,13 @@ export class DegenDistributorsPlugin
5573
const cfgLC = cfg.toLowerCase() as Address;
5674
const r = distributorByConfigurator?.[cfgLC];
5775

58-
if (r.status === "fulfilled") {
59-
this.#distributors?.upsert(pool, r.value);
76+
if (r.status === "success" && r.result.length > 0) {
77+
this.#distributors?.upsert(pool, r.result[0]);
6078
} else {
6179
this.sdk.logger?.error(
62-
`failed to load degen distributor for market configurator ${this.labelAddress(cfg)} and pool ${this.labelAddress(pool)}: ${r.reason}`,
80+
`failed to load degen distributor for market configurator ${this.labelAddress(
81+
cfg,
82+
)} and pool ${this.labelAddress(pool)}: ${r.error}`,
6383
);
6484
}
6585
});

src/sdk/market/MarketConfiguratorContract.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,25 @@ export class MarketConfiguratorContract extends BaseContract<abi> {
3838
);
3939
}
4040

41+
public static async getPeripheryContractBatch(
42+
configurators: MarketConfiguratorContract[],
43+
client: MarketConfiguratorContract["client"],
44+
contract: PeripheryContract,
45+
) {
46+
const resp = await client.multicall({
47+
allowFailure: true,
48+
contracts: configurators.map(
49+
cfg =>
50+
({
51+
address: cfg.address,
52+
abi: cfg.abi,
53+
functionName: "getPeripheryContracts",
54+
args: [stringToHex(contract, { size: 32 })],
55+
}) as const,
56+
),
57+
});
58+
return resp;
59+
}
4160
public async getPeripheryContract(
4261
contract: PeripheryContract,
4362
): Promise<Address> {

0 commit comments

Comments
 (0)