Skip to content

Commit f838419

Browse files
committed
fix: remove static BaseContract methods
1 parent 266ae2c commit f838419

File tree

9 files changed

+72
-58
lines changed

9 files changed

+72
-58
lines changed

src/GearboxSDK.ts

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import type { NetworkType } from "@gearbox-protocol/sdk-gov";
22
import { formatBN, TIMELOCK } from "@gearbox-protocol/sdk-gov";
33
import { EventEmitter } from "eventemitter3";
4-
import { type Address, http } from "viem";
4+
import type { Address, Hex, Log } from "viem";
5+
import { http } from "viem";
56

6-
import { BaseContract } from "./base";
7+
import type { BaseContract } from "./base";
78
import { Provider } from "./chain";
89
import {
910
ADDRESS_PROVIDER,
@@ -22,7 +23,7 @@ import { PriceFeedRegister } from "./market/pricefeeds";
2223
import { RouterV3Contract } from "./router";
2324
import type { SDKEventsMap } from "./SDKEvents";
2425
import type { GearboxState } from "./state/state";
25-
import type { ILogger } from "./types";
26+
import type { ILogger, MultiCall } from "./types";
2627
import { AddressMap } from "./utils";
2728
import { createAnvilClient } from "./utils/viem";
2829

@@ -88,8 +89,14 @@ export class GearboxSDK extends EventEmitter<SDKEventsMap> {
8889
public readonly interestRateModels = new AddressMap<
8990
BaseContract<readonly unknown[]>
9091
>();
91-
92+
/**
93+
* All price feeds known to sdk, without oracle-related data (stalenessPeriod, main/reserve, etc.)
94+
*/
9295
public readonly priceFeeds: PriceFeedRegister;
96+
/**
97+
* All contracts known to sdk
98+
*/
99+
public readonly contracts = new AddressMap<BaseContract<any>>();
93100

94101
public static async attach(options: SDKAttachOptions): Promise<GearboxSDK> {
95102
const { rpcURL, timeout, logger } = options;
@@ -174,6 +181,36 @@ export class GearboxSDK extends EventEmitter<SDKEventsMap> {
174181
return this;
175182
}
176183

184+
public parseLogs(logs: Array<Log>): void {
185+
logs.forEach(log => {
186+
const contract = this.contracts.get(log.address);
187+
contract?.parseLog(log);
188+
});
189+
}
190+
191+
/**
192+
* Converts contract call into some human-friendly string
193+
* @param address
194+
* @param calldata
195+
* @returns
196+
*/
197+
public parseFunctionData(address: Address, calldata: Hex): string {
198+
const contract = this.contracts.mustGet(address);
199+
return contract.parseFunctionData(calldata);
200+
}
201+
202+
/**
203+
* Converts multicalls into some human-friendly strings
204+
* @param address
205+
* @param calldata
206+
* @returns
207+
*/
208+
public parseMultiCall(calls: MultiCall[]): string[] {
209+
return calls.map(call =>
210+
this.parseFunctionData(call.target, call.callData),
211+
);
212+
}
213+
177214
public async updateBlock(): Promise<void> {
178215
const block = await this.provider.publicClient.getBlock({
179216
blockTag: "latest",
@@ -215,7 +252,7 @@ export class GearboxSDK extends EventEmitter<SDKEventsMap> {
215252
toBlock: BigInt(toBlock),
216253
});
217254

218-
BaseContract.parseLogs(events);
255+
this.parseLogs(events);
219256

220257
for (const pf of this.marketRegister.getPoolFactories()) {
221258
if (pf.poolContract.hasOperation) {

src/base/BaseContract.ts

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { iVersionAbi } from "../abi";
1616
import { ADDRESS_0X0 } from "../constants";
1717
import type { GearboxSDK } from "../GearboxSDK";
1818
import type { BaseContractState } from "../state";
19-
import type { ILogger, MultiCall, RawTx } from "../types";
19+
import type { ILogger, RawTx } from "../types";
2020
import { childLogger, createRawTx, json_stringify } from "../utils";
2121
import type { IAddressLabeller } from "./IAddressLabeller";
2222
import { SDKConstruct } from "./SDKConstruct";
@@ -36,8 +36,6 @@ export abstract class BaseContract<
3636
public readonly abi: abi;
3737
public readonly logger?: ILogger;
3838

39-
protected static register: Record<Address, BaseContract<any>> = {};
40-
4139
version: number;
4240
contractType: string;
4341
#name: string;
@@ -55,12 +53,14 @@ export abstract class BaseContract<
5553
public: sdk.provider.publicClient,
5654
},
5755
});
58-
this.#name = args.name || args.contractType || this.#address;
56+
this.name = this.#name = args.name || args.contractType || this.#address;
5957
this.version = args.version || 0;
6058
this.contractType = args.contractType || "";
61-
this.logger = childLogger(this.#name, sdk.logger);
59+
this.logger = childLogger(this.name, sdk.logger);
6260

63-
BaseContract.register[this.#address.toLowerCase() as Address] = this;
61+
// register contract by address
62+
// TODO: I don't like it, this is side-effect. why do we even need this?
63+
sdk.contracts.upsert(this.address, this);
6464
}
6565

6666
get address(): Address {
@@ -99,19 +99,13 @@ export abstract class BaseContract<
9999
};
100100
}
101101

102-
public static parseLogs(logs: Array<Log>): void {
103-
logs.forEach(log => {
104-
const contract =
105-
BaseContract.register[log.address.toLowerCase() as Address];
106-
if (contract) {
107-
contract.parseLog(log);
108-
}
109-
});
110-
}
102+
/**
103+
* Updates contract's internal state from event
104+
* @param _log
105+
*/
106+
public parseLog(_log: Log): void {}
111107

112-
protected parseLog(_log: Log) {}
113-
114-
protected parseFunctionData(calldata: Hex): string {
108+
public parseFunctionData(calldata: Hex): string {
115109
const decoded = decodeFunctionData({
116110
abi: this.abi,
117111
data: calldata,
@@ -151,19 +145,6 @@ export abstract class BaseContract<
151145
return undefined;
152146
}
153147

154-
public static parseMultiCall(calls: Array<MultiCall>): Array<string> {
155-
return calls.map(call => BaseContract.parse(call.target, call.callData));
156-
}
157-
158-
public static parse(address: Address, calldata: Hex): string {
159-
const contract = BaseContract.register[address.toLowerCase() as Address];
160-
if (contract) {
161-
return contract.parseFunctionData(calldata);
162-
} else {
163-
throw new Error(`Contract not found: ${address}`);
164-
}
165-
}
166-
167148
async getVersion(): Promise<number> {
168149
this.version = Number(
169150
await this.sdk.provider.publicClient.readContract({

src/core/AddressProviderV3_1Contract.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export class AddressProviderContractV3_1 extends BaseContract<abi> {
2222
});
2323
}
2424

25-
parseFunctionParams(
25+
public parseFunctionParams(
2626
params: DecodeFunctionDataReturnType<abi>,
2727
): Array<string> | undefined {
2828
switch (params.functionName) {
@@ -100,7 +100,7 @@ export class AddressProviderContractV3_1 extends BaseContract<abi> {
100100
};
101101
}
102102

103-
protected parseLog(log: Log): void {
103+
public override parseLog(log: Log): void {
104104
const parsedLog = parseEventLogs({
105105
abi: this.abi,
106106
logs: [log],

src/core/BotListV3Contract.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export class BotListContract extends BaseContract<abi> {
1616
super(sdk, { address, name: "BotListV3", abi: botListV3Abi });
1717
}
1818

19-
parseFunctionParams(
19+
public parseFunctionParams(
2020
params: DecodeFunctionDataReturnType<abi>,
2121
): Array<string> | undefined {
2222
switch (params.functionName) {
@@ -37,7 +37,7 @@ export class BotListContract extends BaseContract<abi> {
3737
}
3838
}
3939

40-
async fetchState(toBlock: bigint) {
40+
public async fetchState(toBlock: bigint) {
4141
const logs = await this.provider.publicClient.getContractEvents({
4242
address: this.address,
4343
abi: this.abi,
@@ -48,7 +48,7 @@ export class BotListContract extends BaseContract<abi> {
4848
logs.forEach(e => this.parseLog(e));
4949
}
5050

51-
protected parseLog(log: Log): void {
51+
public override parseLog(log: Log): void {
5252
const parsedLog = parseEventLogs({
5353
abi: this.abi,
5454
logs: [log],

src/market/CreditFacadeContract.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,23 +67,23 @@ export class CreditFacadeContract extends BaseContract<abi> {
6767
const [onBehalfOf, calls, referralCode] = params.args;
6868
return [
6969
this.addressLabels.get(onBehalfOf),
70-
BaseContract.parseMultiCall([...calls]).join(","),
70+
this.sdk.parseMultiCall([...calls]).join(","),
7171
`${referralCode}`,
7272
];
7373
}
7474
case "closeCreditAccount": {
7575
const [creditAccount, calls] = params.args;
7676
return [
7777
this.addressLabels.get(creditAccount),
78-
BaseContract.parseMultiCall([...calls]).join(","),
78+
this.sdk.parseMultiCall([...calls]).join(","),
7979
];
8080
}
8181
case "liquidateCreditAccount": {
8282
const [creditAccount, to, calls] = params.args;
8383
return [
8484
this.addressLabels.get(creditAccount),
8585
this.addressLabels.get(to),
86-
BaseContract.parseMultiCall([...calls]).join(","),
86+
this.sdk.parseMultiCall([...calls]).join(","),
8787
];
8888
}
8989
case "setBotPermissions": {

src/market/CreditManagerContract.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,11 @@ export class CreditManagerContract extends BaseContract<abi> {
3838
collateralTokens.map((t, i) => [t, liquidationThresholds[i]]),
3939
),
4040

41-
feeInterest: Number(creditManager.feeInterest),
42-
feeLiquidation: Number(creditManager.feeLiquidation),
43-
liquidationDiscount: Number(creditManager.liquidationDiscount),
44-
feeLiquidationExpired: Number(creditManager.feeLiquidationExpired),
45-
liquidationDiscountExpired: Number(
46-
creditManager.liquidationDiscountExpired,
47-
),
41+
feeInterest: creditManager.feeInterest,
42+
feeLiquidation: creditManager.feeLiquidation,
43+
liquidationDiscount: creditManager.liquidationDiscount,
44+
feeLiquidationExpired: creditManager.feeLiquidationExpired,
45+
liquidationDiscountExpired: creditManager.liquidationDiscountExpired,
4846
quotedTokensMask: 0n,
4947
contractsToAdapters: {}, // cmd.adapters.reduce(
5048
// (acc, adapter) => {

src/market/PoolContract.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export class PoolContract extends BaseContract<typeof abi> {
1414
state: PoolState;
1515

1616
// Contracts
17-
hasOperation = false;
17+
public hasOperation = false;
1818

1919
constructor(sdk: GearboxSDK, data: PoolData) {
2020
super(sdk, {
@@ -49,9 +49,7 @@ export class PoolContract extends BaseContract<typeof abi> {
4949
};
5050
}
5151

52-
// LOGS
53-
54-
protected parseLog(log: Log): void {
52+
public override parseLog(log: Log): void {
5553
const parsedLog = parseEventLogs({
5654
abi: this.abi,
5755
logs: [log],
@@ -70,7 +68,7 @@ export class PoolContract extends BaseContract<typeof abi> {
7068
}
7169
}
7270

73-
parseFunctionParams(
71+
public parseFunctionParams(
7472
params: DecodeFunctionDataReturnType<typeof abi>,
7573
): Array<string> | undefined {
7674
switch (params.functionName) {

src/market/pricefeeds/PriceFeedsRegister.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ export class PriceFeedRegister extends SDKConstruct {
5252

5353
/**
5454
* Returns RawTxs to update price feeds
55-
* This method is static because price feeds may belong to different markets with different PriceFeedFactory instances
5655
* @param priceFeeds top-level price feeds, actual updatable price feeds will be derived. If not provided will use all price feeds that are attached
5756
* @returns
5857
*/

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"skipLibCheck": true,
99
"lib": ["ESNext"],
1010
"declaration": true,
11-
"removeComments": false
11+
"removeComments": false,
12+
"resolveJsonModule": true
1213
},
1314
"include": ["src", "scripts", "tsup.config.ts"],
1415
"exclude": ["dist", "node_modules"]

0 commit comments

Comments
 (0)