Skip to content

Commit 0f604a7

Browse files
committed
fix: update multicall spy
1 parent 4c30646 commit 0f604a7

File tree

5 files changed

+29
-88
lines changed

5 files changed

+29
-88
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"@gearbox-protocol/cli-utils": "^5.45.16",
3030
"@gearbox-protocol/liquidator-contracts": "^1.36.0-experimental.41",
3131
"@gearbox-protocol/liquidator-v2-contracts": "^2.4.0",
32-
"@gearbox-protocol/sdk": "8.28.1",
32+
"@gearbox-protocol/sdk": "8.28.4",
3333
"@gearbox-protocol/types": "^1.14.8",
3434
"@types/node": "^24.3.1",
3535
"@uniswap/sdk-core": "^7.7.2",

src/MulticallSpy.ts

Lines changed: 14 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
11
import { join } from "node:path";
22
import { PutObjectCommand, S3Client } from "@aws-sdk/client-s3";
3-
import type {
4-
EIP1193Parameters,
5-
HttpTransportConfig,
6-
PublicRpcSchema,
7-
} from "viem";
3+
import { type EthCallRequest, EthCallSpy } from "@gearbox-protocol/sdk/dev";
84
import type { Config } from "./config/index.js";
95
import { DI } from "./di.js";
106
import { type ILogger, Logger } from "./log/index.js";
117

12-
type DetectedCall = EIP1193Parameters<PublicRpcSchema> & {
13-
headers?: Record<string, string>;
14-
};
15-
168
/**
179
* This is temporary solution to diagnose bug where compressor occasionally returns many accounts with HF = 0
1810
*/
@@ -24,50 +16,25 @@ export default class MulticallSpy {
2416
@DI.Inject(DI.Config)
2517
config!: Config;
2618

19+
public readonly spy: EthCallSpy;
2720
#client = new S3Client({});
28-
#detectedCalls: DetectedCall[] = [];
29-
#detectedBlock = 0n;
30-
31-
public multicallRequestSpy: Required<HttpTransportConfig>["onFetchRequest"] =
32-
async request => {
33-
if (!this.config.debugScanner || this.config.optimistic) {
34-
return;
35-
}
36-
const data = (await request.json()) as EIP1193Parameters<PublicRpcSchema>;
37-
const blockNumber = isGetCreditAccountsMulticall(data);
38-
if (blockNumber) {
39-
this.#storeCall(blockNumber, data);
40-
this.log.debug(
41-
`stored getCreditAccounts multicall at block ${blockNumber}, total calls: ${this.#detectedCalls.length}`,
42-
);
43-
}
44-
};
4521

46-
public multicallResponseSpy: Required<HttpTransportConfig>["onFetchResponse"] =
47-
async response => {
48-
if (!this.config.debugScanner || this.config.optimistic) {
49-
return;
50-
}
51-
const copy = response.clone();
52-
const resp = await copy.json();
53-
const id = (resp as any).id as number;
54-
const call = this.#detectedCalls.find(c => "id" in c && c.id === id);
55-
if (call) {
56-
call.headers = Object.fromEntries(response.headers.entries());
57-
}
58-
};
22+
constructor() {
23+
this.spy = new EthCallSpy(
24+
isGetCreditAccountsMulticall,
25+
this.log,
26+
this.config.debugScanner && !this.config.optimistic,
27+
);
28+
}
5929

6030
public async dumpCalls(): Promise<void> {
61-
if (!this.config.debugScanner || this.config.optimistic) {
62-
return;
63-
}
6431
if (!this.config.outS3Bucket) {
6532
this.log.error("outS3Bucket is not set");
6633
return;
6734
}
6835
const key = join(
6936
this.config.outS3Prefix,
70-
`getCreditAccounts_${this.#detectedBlock}.json`,
37+
`getCreditAccounts_${this.spy.detectedBlock}.json`,
7138
);
7239
const s3Url = `s3://${this.config.outS3Bucket}/${key}`;
7340
try {
@@ -77,42 +44,27 @@ export default class MulticallSpy {
7744
Bucket: this.config.outS3Bucket,
7845
Key: key,
7946
ContentType: "application/json",
80-
Body: JSON.stringify(this.#detectedCalls),
47+
Body: JSON.stringify(this.spy.detectedCalls),
8148
}),
8249
);
8350
this.log.debug(`uploaded to ${s3Url}`);
8451
} catch (e) {
8552
this.log.error(e, `failed to upload to ${s3Url}`);
8653
}
8754
}
88-
89-
#storeCall(
90-
blockNumber: bigint,
91-
data: EIP1193Parameters<PublicRpcSchema>,
92-
): void {
93-
if (blockNumber !== this.#detectedBlock) {
94-
this.#detectedBlock = blockNumber;
95-
this.#detectedCalls = [];
96-
}
97-
this.#detectedCalls.push(data);
98-
}
9955
}
10056

10157
/**
10258
* Detects multicalls to CreditAccounts.getCreditAccounts
10359
* @param data
10460
* @returns block number if it's a getCreditAccounts multicall, undefined otherwise
10561
*/
106-
function isGetCreditAccountsMulticall(
107-
data: EIP1193Parameters<PublicRpcSchema>,
108-
): bigint | undefined {
62+
function isGetCreditAccountsMulticall(data: EthCallRequest): boolean {
10963
try {
11064
if (
11165
data.method === "eth_call" &&
11266
// detect eth_call to multicall3: 0xca11bde05977b3631167028862be2a173976ca11
11367
data.params[0].to === "0xca11bde05977b3631167028862be2a173976ca11" &&
114-
typeof data.params[1] === "string" &&
115-
data.params[1]?.startsWith("0x") && // non-latest block
11668
// that contain CA compressor: "0x4115708Fc8fe6bB392De2e0C21c2C81dA2222394"
11769
data.params[0].data?.includes(
11870
"4115708fc8fe6bb392de2e0c21c2c81da2222394",
@@ -125,7 +77,8 @@ function isGetCreditAccountsMulticall(
12577
(data.params[0].data?.includes("f43bdb34") ||
12678
data.params[0].data?.includes("8b59b911"))
12779
) {
128-
return BigInt(data.params[1]);
80+
return true;
12981
}
13082
} catch {}
83+
return false;
13184
}

src/attachSDK.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,8 @@ export default async function attachSDK(): Promise<ICreditAccountsService> {
6262

6363
const transport = createTransport(config, {
6464
timeout: 600_000,
65-
onFetchRequest: config.debugScanner
66-
? (r, o) => multicallSpy.multicallRequestSpy(r, o)
67-
: undefined,
68-
onFetchResponse: config.debugScanner
69-
? r => multicallSpy.multicallResponseSpy(r)
70-
: undefined,
65+
onFetchRequest: multicallSpy.spy.onFetchRequest,
66+
onFetchResponse: multicallSpy.spy.onFetchResponse,
7167
});
7268

7369
const sdk = await GearboxSDK.attach({

src/services/Scanner.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -401,12 +401,8 @@ class ScannerDiagnoster {
401401
this.#drpc = new WorkaroundCAS(this.caService.sdk, {
402402
rpcURL,
403403
rpcOptions: {
404-
onFetchRequest: this.config.debugScanner
405-
? (r, o) => this.multicallSpy.multicallRequestSpy(r, o)
406-
: undefined,
407-
onFetchResponse: this.config.debugScanner
408-
? r => this.multicallSpy.multicallResponseSpy(r)
409-
: undefined,
404+
onFetchRequest: this.multicallSpy.spy.onFetchRequest,
405+
onFetchResponse: this.multicallSpy.spy.onFetchResponse,
410406
},
411407
});
412408
}
@@ -421,12 +417,8 @@ class ScannerDiagnoster {
421417
this.#alchemy = new WorkaroundCAS(this.caService.sdk, {
422418
rpcURL,
423419
rpcOptions: {
424-
onFetchRequest: this.config.debugScanner
425-
? (r, o) => this.multicallSpy.multicallRequestSpy(r, o)
426-
: undefined,
427-
onFetchResponse: this.config.debugScanner
428-
? r => this.multicallSpy.multicallResponseSpy(r)
429-
: undefined,
420+
onFetchRequest: this.multicallSpy.spy.onFetchRequest,
421+
onFetchResponse: this.multicallSpy.spy.onFetchResponse,
430422
},
431423
});
432424
}

yarn.lock

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1770,7 +1770,7 @@ __metadata:
17701770
"@gearbox-protocol/cli-utils": "npm:^5.45.16"
17711771
"@gearbox-protocol/liquidator-contracts": "npm:^1.36.0-experimental.41"
17721772
"@gearbox-protocol/liquidator-v2-contracts": "npm:^2.4.0"
1773-
"@gearbox-protocol/sdk": "npm:8.28.1"
1773+
"@gearbox-protocol/sdk": "npm:8.28.4"
17741774
"@gearbox-protocol/types": "npm:^1.14.8"
17751775
"@types/node": "npm:^24.3.1"
17761776
"@uniswap/sdk-core": "npm:^7.7.2"
@@ -1796,9 +1796,9 @@ __metadata:
17961796
languageName: unknown
17971797
linkType: soft
17981798

1799-
"@gearbox-protocol/sdk@npm:8.28.1":
1800-
version: 8.28.1
1801-
resolution: "@gearbox-protocol/sdk@npm:8.28.1"
1799+
"@gearbox-protocol/sdk@npm:8.28.4":
1800+
version: 8.28.4
1801+
resolution: "@gearbox-protocol/sdk@npm:8.28.4"
18021802
dependencies:
18031803
"@redstone-finance/evm-connector": "npm:^0.7.5"
18041804
"@redstone-finance/protocol": "npm:^0.7.5"
@@ -1812,7 +1812,7 @@ __metadata:
18121812
zod: "npm:^4.1.1"
18131813
peerDependencies:
18141814
axios: ^1.0.0
1815-
checksum: 10c0/d82d6861757e9a9dd5cc112b7f607fda40ae48793a7b390da72701aed8f198101e93416311f2a8eda8de0f860f2fc15409c60685c7a7c43a3712eb46205dd774
1815+
checksum: 10c0/f25d12497ef4a2787c4d6a0a9a0090477323887e67f085610db776cd136f70a331c453c6e5195f17f4deab09faa899ef71b6e4d1321fd23758917bfcc5ef88b3
18161816
languageName: node
18171817
linkType: hard
18181818

@@ -4166,9 +4166,9 @@ __metadata:
41664166
linkType: hard
41674167

41684168
"get-east-asian-width@npm:^1.0.0, get-east-asian-width@npm:^1.3.1":
4169-
version: 1.3.1
4170-
resolution: "get-east-asian-width@npm:1.3.1"
4171-
checksum: 10c0/cfe2eba0ae066d9a8b9f2e524922c6ec00ed91427758d701850839315febbbc56b26b06b43c8a9c1373ae769cc188c04c6a6fcaf3c9273e712a1cc8cd438a1f8
4169+
version: 1.4.0
4170+
resolution: "get-east-asian-width@npm:1.4.0"
4171+
checksum: 10c0/4e481d418e5a32061c36fbb90d1b225a254cc5b2df5f0b25da215dcd335a3c111f0c2023ffda43140727a9cafb62dac41d022da82c08f31083ee89f714ee3b83
41724172
languageName: node
41734173
linkType: hard
41744174

0 commit comments

Comments
 (0)