@gearbox-protocol/sdk 13.3.3 → 13.4.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/permissionless/utils/price-update/get-price-update-tx.js +3 -15
- package/dist/cjs/sdk/GearboxSDK.js +135 -20
- package/dist/cjs/sdk/accounts/AbstractCreditAccountsService.js +196 -340
- package/dist/cjs/sdk/accounts/CreditAccountsServiceV310.js +30 -35
- package/dist/cjs/sdk/accounts/multicall-utils.js +91 -0
- package/dist/cjs/sdk/base/BaseContract.js +26 -6
- package/dist/cjs/sdk/base/ChainContractsRegister.js +39 -2
- package/dist/cjs/sdk/base/Construct.js +15 -3
- package/dist/cjs/sdk/base/TokensMeta.js +23 -0
- package/dist/cjs/sdk/constants/address-provider.js +0 -22
- package/dist/cjs/sdk/core/AbstractAddressProviderContract.js +15 -0
- package/dist/cjs/sdk/market/MarketRegister.js +74 -3
- package/dist/cjs/sdk/market/credit/CreditFacadeV310Contract.js +6 -0
- package/dist/cjs/sdk/market/oracle/PriceOracleBaseContract.js +31 -50
- package/dist/cjs/sdk/market/pricefeeds/PriceFeedRef.js +16 -0
- package/dist/cjs/sdk/market/pricefeeds/PriceFeedsRegister.js +55 -12
- package/dist/cjs/sdk/options.js +30 -24
- package/dist/cjs/sdk/plugins/BasePlugin.js +24 -0
- package/dist/cjs/sdk/pools/AbstractPoolService.js +6 -0
- package/dist/cjs/sdk/router/AbstractRouterContract.js +4 -1
- package/dist/cjs/sdk/router/RouterV310Contract.js +20 -15
- package/dist/cjs/sdk/utils/AddressMap.js +53 -17
- package/dist/cjs/sdk/utils/AddressSet.js +9 -0
- package/dist/esm/permissionless/utils/price-update/get-price-update-tx.js +6 -20
- package/dist/esm/sdk/GearboxSDK.js +135 -20
- package/dist/esm/sdk/accounts/AbstractCreditAccountsService.js +204 -342
- package/dist/esm/sdk/accounts/CreditAccountsServiceV310.js +30 -35
- package/dist/esm/sdk/accounts/multicall-utils.js +69 -0
- package/dist/esm/sdk/base/BaseContract.js +26 -6
- package/dist/esm/sdk/base/ChainContractsRegister.js +39 -2
- package/dist/esm/sdk/base/Construct.js +15 -3
- package/dist/esm/sdk/base/TokensMeta.js +23 -0
- package/dist/esm/sdk/constants/address-provider.js +0 -21
- package/dist/esm/sdk/core/AbstractAddressProviderContract.js +15 -0
- package/dist/esm/sdk/market/MarketRegister.js +74 -3
- package/dist/esm/sdk/market/credit/CreditFacadeV310Contract.js +6 -0
- package/dist/esm/sdk/market/oracle/PriceOracleBaseContract.js +31 -50
- package/dist/esm/sdk/market/pricefeeds/PriceFeedRef.js +16 -0
- package/dist/esm/sdk/market/pricefeeds/PriceFeedsRegister.js +55 -12
- package/dist/esm/sdk/options.js +30 -24
- package/dist/esm/sdk/plugins/BasePlugin.js +24 -0
- package/dist/esm/sdk/pools/AbstractPoolService.js +6 -0
- package/dist/esm/sdk/router/AbstractRouterContract.js +4 -1
- package/dist/esm/sdk/router/RouterV310Contract.js +20 -15
- package/dist/esm/sdk/utils/AddressMap.js +53 -17
- package/dist/esm/sdk/utils/AddressSet.js +9 -0
- package/dist/types/permissionless/bindings/price-feed-store.d.ts +1 -2
- package/dist/types/permissionless/bindings/types.d.ts +0 -4
- package/dist/types/permissionless/utils/price-update/get-price-update-tx.d.ts +1 -3
- package/dist/types/sdk/GearboxSDK.d.ts +236 -34
- package/dist/types/sdk/accounts/AbstractCreditAccountsService.d.ts +92 -147
- package/dist/types/sdk/accounts/CreditAccountsServiceV310.d.ts +14 -5
- package/dist/types/sdk/accounts/multicall-utils.d.ts +39 -0
- package/dist/types/sdk/accounts/types.d.ts +237 -40
- package/dist/types/sdk/base/BaseContract.d.ts +67 -6
- package/dist/types/sdk/base/ChainContractsRegister.d.ts +51 -2
- package/dist/types/sdk/base/Construct.d.ts +31 -0
- package/dist/types/sdk/base/PlaceholderContract.d.ts +3 -0
- package/dist/types/sdk/base/SDKConstruct.d.ts +10 -0
- package/dist/types/sdk/base/TokensMeta.d.ts +59 -2
- package/dist/types/sdk/base/types.d.ts +185 -25
- package/dist/types/sdk/chain/chains.d.ts +78 -18
- package/dist/types/sdk/chain/detectNetwork.d.ts +7 -0
- package/dist/types/sdk/constants/address-provider.d.ts +4 -3
- package/dist/types/sdk/core/AbstractAddressProviderContract.d.ts +23 -0
- package/dist/types/sdk/core/types.d.ts +46 -0
- package/dist/types/sdk/market/MarketRegister.d.ts +81 -0
- package/dist/types/sdk/market/adapters/PlaceholderAdapterContracts.d.ts +3 -0
- package/dist/types/sdk/market/credit/CreditFacadeV310Contract.d.ts +1 -0
- package/dist/types/sdk/market/oracle/PriceOracleBaseContract.d.ts +40 -50
- package/dist/types/sdk/market/oracle/types.d.ts +78 -59
- package/dist/types/sdk/market/pricefeeds/AbstractLPPriceFeed.d.ts +3 -0
- package/dist/types/sdk/market/pricefeeds/AbstractPriceFeed.d.ts +3 -0
- package/dist/types/sdk/market/pricefeeds/PriceFeedRef.d.ts +22 -2
- package/dist/types/sdk/market/pricefeeds/PriceFeedsRegister.d.ts +80 -16
- package/dist/types/sdk/market/pricefeeds/getRawPriceUpdates.d.ts +2 -2
- package/dist/types/sdk/market/pricefeeds/types.d.ts +75 -11
- package/dist/types/sdk/options.d.ts +13 -4
- package/dist/types/sdk/plugins/BasePlugin.d.ts +39 -0
- package/dist/types/sdk/plugins/types.d.ts +73 -43
- package/dist/types/sdk/pools/AbstractPoolService.d.ts +12 -0
- package/dist/types/sdk/pools/types.d.ts +75 -6
- package/dist/types/sdk/router/AbstractRouterContract.d.ts +21 -2
- package/dist/types/sdk/router/RouterV310Contract.d.ts +27 -15
- package/dist/types/sdk/router/types.d.ts +47 -70
- package/dist/types/sdk/types/state.d.ts +32 -3
- package/dist/types/sdk/utils/AddressMap.d.ts +61 -17
- package/dist/types/sdk/utils/AddressSet.d.ts +15 -0
- package/package.json +4 -2
|
@@ -4,7 +4,7 @@ import { MAX_UINT256 } from "../constants/math.js";
|
|
|
4
4
|
import { AbstractCreditAccountService } from "./AbstractCreditAccountsService.js";
|
|
5
5
|
class CreditAccountServiceV310 extends AbstractCreditAccountService {
|
|
6
6
|
/**
|
|
7
|
-
*
|
|
7
|
+
* {@inheritDoc ICreditAccountsService.setBot}
|
|
8
8
|
*/
|
|
9
9
|
async setBot({
|
|
10
10
|
botAddress,
|
|
@@ -14,10 +14,6 @@ class CreditAccountServiceV310 extends AbstractCreditAccountService {
|
|
|
14
14
|
const cm = this.sdk.marketRegister.findCreditManager(
|
|
15
15
|
targetContract.creditManager
|
|
16
16
|
);
|
|
17
|
-
const priceUpdatesCalls = targetContract.type === "creditAccount" ? await this.getPriceUpdatesForFacade({
|
|
18
|
-
creditManager: targetContract.creditManager,
|
|
19
|
-
creditAccount: targetContract
|
|
20
|
-
}) : [];
|
|
21
17
|
const permissions = defaultPermissions !== null ? defaultPermissions : await getContract({
|
|
22
18
|
address: botAddress,
|
|
23
19
|
client: this.sdk.client,
|
|
@@ -41,12 +37,16 @@ class CreditAccountServiceV310 extends AbstractCreditAccountService {
|
|
|
41
37
|
args: [botAddress, permissions]
|
|
42
38
|
})
|
|
43
39
|
};
|
|
44
|
-
const calls =
|
|
40
|
+
const calls = targetContract.type === "creditAccount" ? await this.prependPriceUpdates(
|
|
41
|
+
targetContract.creditManager,
|
|
42
|
+
[addBotCall],
|
|
43
|
+
targetContract
|
|
44
|
+
) : [addBotCall];
|
|
45
45
|
const tx = targetContract.type === "creditAccount" ? cm.creditFacade.multicall(targetContract.creditAccount, calls) : void 0;
|
|
46
46
|
return { tx, calls, creditFacade: cm.creditFacade };
|
|
47
47
|
}
|
|
48
48
|
/**
|
|
49
|
-
*
|
|
49
|
+
* {@inheritDoc ICreditAccountsService.withdrawCollateral}
|
|
50
50
|
*/
|
|
51
51
|
async withdrawCollateral({
|
|
52
52
|
creditAccount,
|
|
@@ -58,12 +58,7 @@ class CreditAccountServiceV310 extends AbstractCreditAccountService {
|
|
|
58
58
|
const cm = this.sdk.marketRegister.findCreditManager(
|
|
59
59
|
creditAccount.creditManager
|
|
60
60
|
);
|
|
61
|
-
const
|
|
62
|
-
creditManager: creditAccount.creditManager,
|
|
63
|
-
creditAccount
|
|
64
|
-
});
|
|
65
|
-
const calls = [
|
|
66
|
-
...priceUpdatesCalls,
|
|
61
|
+
const operationCalls = [
|
|
67
62
|
...assetsToWithdraw.map(
|
|
68
63
|
(a) => this.prepareWithdrawToken(
|
|
69
64
|
creditAccount.creditFacade,
|
|
@@ -77,11 +72,16 @@ class CreditAccountServiceV310 extends AbstractCreditAccountService {
|
|
|
77
72
|
averageQuota
|
|
78
73
|
})
|
|
79
74
|
];
|
|
75
|
+
const calls = await this.prependPriceUpdates(
|
|
76
|
+
creditAccount.creditManager,
|
|
77
|
+
operationCalls,
|
|
78
|
+
creditAccount
|
|
79
|
+
);
|
|
80
80
|
const tx = cm.creditFacade.multicall(creditAccount.creditAccount, calls);
|
|
81
81
|
return { tx, calls, creditFacade: cm.creditFacade };
|
|
82
82
|
}
|
|
83
83
|
/**
|
|
84
|
-
*
|
|
84
|
+
* {@inheritDoc ICreditAccountsService.repayCreditAccount}
|
|
85
85
|
*/
|
|
86
86
|
async repayCreditAccount({
|
|
87
87
|
operation,
|
|
@@ -99,12 +99,7 @@ class CreditAccountServiceV310 extends AbstractCreditAccountService {
|
|
|
99
99
|
tokensToClaim,
|
|
100
100
|
creditAccount: ca
|
|
101
101
|
});
|
|
102
|
-
const
|
|
103
|
-
creditManager: ca.creditManager,
|
|
104
|
-
creditAccount: ca
|
|
105
|
-
});
|
|
106
|
-
const calls = [
|
|
107
|
-
...operation === "close" ? [] : priceUpdates,
|
|
102
|
+
const operationCalls = [
|
|
108
103
|
...this.prepareAddCollateral(ca.creditFacade, addCollateral, permits),
|
|
109
104
|
...this.prepareDisableQuotas(ca),
|
|
110
105
|
...this.prepareDecreaseDebt(ca),
|
|
@@ -113,11 +108,12 @@ class CreditAccountServiceV310 extends AbstractCreditAccountService {
|
|
|
113
108
|
(t) => this.prepareWithdrawToken(ca.creditFacade, t.token, MAX_UINT256, to)
|
|
114
109
|
)
|
|
115
110
|
];
|
|
111
|
+
const calls = operation === "close" ? operationCalls : await this.prependPriceUpdates(ca.creditManager, operationCalls, ca);
|
|
116
112
|
const tx = operation === "close" ? cm.creditFacade.closeCreditAccount(ca.creditAccount, calls) : cm.creditFacade.multicall(ca.creditAccount, calls);
|
|
117
113
|
return { tx, calls, creditFacade: cm.creditFacade };
|
|
118
114
|
}
|
|
119
115
|
/**
|
|
120
|
-
*
|
|
116
|
+
* {@inheritDoc ICreditAccountsService.repayAndLiquidateCreditAccount}
|
|
121
117
|
*/
|
|
122
118
|
async repayAndLiquidateCreditAccount({
|
|
123
119
|
collateralAssets,
|
|
@@ -133,19 +129,19 @@ class CreditAccountServiceV310 extends AbstractCreditAccountService {
|
|
|
133
129
|
tokensToClaim,
|
|
134
130
|
creditAccount: ca
|
|
135
131
|
});
|
|
136
|
-
const priceUpdates = await this.getPriceUpdatesForFacade({
|
|
137
|
-
creditManager: ca.creditManager,
|
|
138
|
-
creditAccount: ca
|
|
139
|
-
});
|
|
140
132
|
const addCollateral = collateralAssets.filter((a) => a.balance > 0);
|
|
141
|
-
const
|
|
142
|
-
...priceUpdates,
|
|
133
|
+
const operationCalls = [
|
|
143
134
|
...this.prepareAddCollateral(ca.creditFacade, addCollateral, permits),
|
|
144
135
|
...claimPath.calls,
|
|
145
136
|
...assetsToWithdraw.map(
|
|
146
137
|
(t) => this.prepareWithdrawToken(ca.creditFacade, t.token, MAX_UINT256, to)
|
|
147
138
|
)
|
|
148
139
|
];
|
|
140
|
+
const calls = await this.prependPriceUpdates(
|
|
141
|
+
ca.creditManager,
|
|
142
|
+
operationCalls,
|
|
143
|
+
ca
|
|
144
|
+
);
|
|
149
145
|
const tx = cm.creditFacade.liquidateCreditAccount(
|
|
150
146
|
ca.creditAccount,
|
|
151
147
|
to,
|
|
@@ -154,7 +150,7 @@ class CreditAccountServiceV310 extends AbstractCreditAccountService {
|
|
|
154
150
|
return { tx, calls, creditFacade: cm.creditFacade };
|
|
155
151
|
}
|
|
156
152
|
/**
|
|
157
|
-
*
|
|
153
|
+
* {@inheritDoc ICreditAccountsService.claimFarmRewards}
|
|
158
154
|
*/
|
|
159
155
|
async claimFarmRewards({
|
|
160
156
|
calls: externalCalls,
|
|
@@ -175,16 +171,15 @@ class CreditAccountServiceV310 extends AbstractCreditAccountService {
|
|
|
175
171
|
});
|
|
176
172
|
}
|
|
177
173
|
if (claimPath.calls.length === 0) throw new Error("No path to execute");
|
|
178
|
-
const
|
|
179
|
-
creditManager: ca.creditManager,
|
|
180
|
-
creditAccount: ca,
|
|
181
|
-
desiredQuotas: averageQuota
|
|
182
|
-
});
|
|
183
|
-
const calls = [
|
|
184
|
-
...priceUpdatesCalls,
|
|
174
|
+
const operationCalls = [
|
|
185
175
|
...claimPath.calls,
|
|
186
176
|
...this.prepareUpdateQuotas(ca.creditFacade, { minQuota, averageQuota })
|
|
187
177
|
];
|
|
178
|
+
const calls = await this.prependPriceUpdates(
|
|
179
|
+
ca.creditManager,
|
|
180
|
+
operationCalls,
|
|
181
|
+
ca
|
|
182
|
+
);
|
|
188
183
|
const tx = cm.creditFacade.multicall(ca.creditAccount, calls);
|
|
189
184
|
return { tx, calls, creditFacade: cm.creditFacade };
|
|
190
185
|
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import {
|
|
2
|
+
decodeFunctionData,
|
|
3
|
+
getAbiItem,
|
|
4
|
+
toFunctionSelector
|
|
5
|
+
} from "viem";
|
|
6
|
+
import { iCreditFacadeMulticallV310Abi } from "../../abi/310/generated.js";
|
|
7
|
+
import { AddressMap } from "../utils/AddressMap.js";
|
|
8
|
+
import { AddressSet } from "../utils/AddressSet.js";
|
|
9
|
+
const ON_DEMAND_SELECTOR = toFunctionSelector(
|
|
10
|
+
getAbiItem({
|
|
11
|
+
abi: iCreditFacadeMulticallV310Abi,
|
|
12
|
+
name: "onDemandPriceUpdates"
|
|
13
|
+
})
|
|
14
|
+
);
|
|
15
|
+
const UPDATE_QUOTA_SELECTOR = toFunctionSelector(
|
|
16
|
+
getAbiItem({
|
|
17
|
+
abi: iCreditFacadeMulticallV310Abi,
|
|
18
|
+
name: "updateQuota"
|
|
19
|
+
})
|
|
20
|
+
);
|
|
21
|
+
function extractPriceUpdates(calls) {
|
|
22
|
+
const priceUpdates = [];
|
|
23
|
+
const remainingCalls = [];
|
|
24
|
+
for (const call of calls) {
|
|
25
|
+
if (isOnDemandPriceUpdateCall(call)) {
|
|
26
|
+
const decoded = decodeFunctionData({
|
|
27
|
+
abi: iCreditFacadeMulticallV310Abi,
|
|
28
|
+
data: call.callData
|
|
29
|
+
});
|
|
30
|
+
const updates = decoded.args[0];
|
|
31
|
+
priceUpdates.push(...updates);
|
|
32
|
+
} else {
|
|
33
|
+
remainingCalls.push(call);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return { priceUpdates, remainingCalls };
|
|
37
|
+
}
|
|
38
|
+
function extractQuotaTokens(calls) {
|
|
39
|
+
const tokens = new AddressSet();
|
|
40
|
+
for (const { callData } of calls) {
|
|
41
|
+
if (callData.slice(0, 10) !== UPDATE_QUOTA_SELECTOR) {
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
const decoded = decodeFunctionData({
|
|
45
|
+
abi: iCreditFacadeMulticallV310Abi,
|
|
46
|
+
data: callData
|
|
47
|
+
});
|
|
48
|
+
const [token, quotaChange] = decoded.args;
|
|
49
|
+
if (quotaChange > 0n) {
|
|
50
|
+
tokens.add(token);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return tokens;
|
|
54
|
+
}
|
|
55
|
+
function mergePriceUpdates(existing, generated) {
|
|
56
|
+
const seen = new AddressMap();
|
|
57
|
+
for (const u of [...generated, ...existing]) {
|
|
58
|
+
seen.upsert(u.priceFeed, u);
|
|
59
|
+
}
|
|
60
|
+
return seen.values();
|
|
61
|
+
}
|
|
62
|
+
function isOnDemandPriceUpdateCall(call) {
|
|
63
|
+
return call.callData.slice(0, 10) === ON_DEMAND_SELECTOR;
|
|
64
|
+
}
|
|
65
|
+
export {
|
|
66
|
+
extractPriceUpdates,
|
|
67
|
+
extractQuotaTokens,
|
|
68
|
+
mergePriceUpdates
|
|
69
|
+
};
|
|
@@ -35,11 +35,30 @@ class ContractParseError extends BaseError {
|
|
|
35
35
|
}
|
|
36
36
|
}
|
|
37
37
|
class BaseContract extends Construct {
|
|
38
|
+
/**
|
|
39
|
+
* Viem contract instance for direct read calls
|
|
40
|
+
**/
|
|
38
41
|
contract;
|
|
42
|
+
/**
|
|
43
|
+
* Contract ABI
|
|
44
|
+
**/
|
|
39
45
|
abi;
|
|
46
|
+
/**
|
|
47
|
+
* Gearbox contract type identifier (e.g. `"CREDIT_MANAGER"`), or empty string if unknown.
|
|
48
|
+
**/
|
|
40
49
|
contractType;
|
|
50
|
+
/**
|
|
51
|
+
* Contract version number.
|
|
52
|
+
* @default 0
|
|
53
|
+
**/
|
|
41
54
|
version;
|
|
55
|
+
/**
|
|
56
|
+
* On-chain address of the contract.
|
|
57
|
+
**/
|
|
42
58
|
address;
|
|
59
|
+
/**
|
|
60
|
+
* Display name for the contract.
|
|
61
|
+
**/
|
|
43
62
|
name;
|
|
44
63
|
constructor(options, args) {
|
|
45
64
|
super(options);
|
|
@@ -63,6 +82,7 @@ class BaseContract extends Construct {
|
|
|
63
82
|
register.setAddressLabel(this.address, this.name);
|
|
64
83
|
}
|
|
65
84
|
}
|
|
85
|
+
/** {@inheritDoc IBaseContract.stateHuman} */
|
|
66
86
|
stateHuman(_ = true) {
|
|
67
87
|
return {
|
|
68
88
|
address: this.labelAddress(this.address),
|
|
@@ -71,9 +91,10 @@ class BaseContract extends Construct {
|
|
|
71
91
|
};
|
|
72
92
|
}
|
|
73
93
|
/**
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
|
|
94
|
+
* Applies an on-chain event to update this contract's local state.
|
|
95
|
+
*
|
|
96
|
+
* @param _log - Decoded event log emitted by this contract.
|
|
97
|
+
**/
|
|
77
98
|
processLog(_log) {
|
|
78
99
|
}
|
|
79
100
|
/**
|
|
@@ -148,9 +169,8 @@ class BaseContract extends Construct {
|
|
|
148
169
|
}
|
|
149
170
|
}
|
|
150
171
|
/**
|
|
151
|
-
* Same as {@link
|
|
152
|
-
* @param calldata
|
|
153
|
-
* @returns
|
|
172
|
+
* Same as {@link stringifyFunctionData}, but throws if error occurs.
|
|
173
|
+
* @param calldata - Raw ABI-encoded calldata.
|
|
154
174
|
*/
|
|
155
175
|
mustStringifyFunctionData(calldata) {
|
|
156
176
|
const decoded = decodeFunctionData({
|
|
@@ -9,8 +9,8 @@ class ChainContractsRegister {
|
|
|
9
9
|
labels = new AddressMap([], "labels");
|
|
10
10
|
client;
|
|
11
11
|
/**
|
|
12
|
-
*
|
|
13
|
-
|
|
12
|
+
* Shared token metadata (symbol, decimals) for all tokens known on this chain.
|
|
13
|
+
**/
|
|
14
14
|
tokensMeta;
|
|
15
15
|
logger;
|
|
16
16
|
constructor(client, logger) {
|
|
@@ -18,6 +18,9 @@ class ChainContractsRegister {
|
|
|
18
18
|
this.tokensMeta = new TokensMeta(client);
|
|
19
19
|
this.logger = logger;
|
|
20
20
|
}
|
|
21
|
+
/**
|
|
22
|
+
* Clears all registered contracts, address labels, and token metadata.
|
|
23
|
+
**/
|
|
21
24
|
resetContracts() {
|
|
22
25
|
this.logger?.debug(
|
|
23
26
|
`resetting contacts register with ${this.contracts.size} contracts`
|
|
@@ -26,9 +29,19 @@ class ChainContractsRegister {
|
|
|
26
29
|
this.contracts.clear();
|
|
27
30
|
this.tokensMeta.reset();
|
|
28
31
|
}
|
|
32
|
+
/**
|
|
33
|
+
* Looks up a contract by address.
|
|
34
|
+
* @param address - On-chain address.
|
|
35
|
+
* @returns The contract wrapper, or `undefined` if not registered.
|
|
36
|
+
*/
|
|
29
37
|
getContract(address) {
|
|
30
38
|
return this.contracts.get(address);
|
|
31
39
|
}
|
|
40
|
+
/**
|
|
41
|
+
* Looks up a contract by address, throwing if not found.
|
|
42
|
+
* @param address - On-chain address.
|
|
43
|
+
* @throws If no contract is registered at this address.
|
|
44
|
+
*/
|
|
32
45
|
mustGetContract(address) {
|
|
33
46
|
const contract = this.contracts.mustGet(address);
|
|
34
47
|
if (!contract) {
|
|
@@ -36,9 +49,21 @@ class ChainContractsRegister {
|
|
|
36
49
|
}
|
|
37
50
|
return contract;
|
|
38
51
|
}
|
|
52
|
+
/**
|
|
53
|
+
* Registers (or replaces) a contract at the given address.
|
|
54
|
+
* @param address - On-chain address.
|
|
55
|
+
* @param contract - Contract wrapper instance.
|
|
56
|
+
*/
|
|
39
57
|
setContract(address, contract) {
|
|
40
58
|
this.contracts.upsert(address, contract);
|
|
41
59
|
}
|
|
60
|
+
/**
|
|
61
|
+
* Assigns a human-readable label to an address for use in logging and
|
|
62
|
+
* parsed call output.
|
|
63
|
+
* @param address - On-chain address.
|
|
64
|
+
* @param label - Static label string, or a function that receives the
|
|
65
|
+
* current label and returns a new one.
|
|
66
|
+
*/
|
|
42
67
|
setAddressLabel(address, label) {
|
|
43
68
|
if (address === NOT_DEPLOYED) {
|
|
44
69
|
return;
|
|
@@ -49,13 +74,25 @@ class ChainContractsRegister {
|
|
|
49
74
|
this.labels.upsert(address, label(this.labels.get(address)));
|
|
50
75
|
}
|
|
51
76
|
}
|
|
77
|
+
/**
|
|
78
|
+
* Returns a display string for an address, incorporating its label if one exists.
|
|
79
|
+
* @param address - On-chain address.
|
|
80
|
+
* @param omitAddress - When `true`, returns only the label (no address prefix).
|
|
81
|
+
* Falls back to the raw address when no label is set.
|
|
82
|
+
*/
|
|
52
83
|
labelAddress(address, omitAddress) {
|
|
53
84
|
const label = this.labels.get(address);
|
|
54
85
|
return label ? omitAddress ? label : `${address} [${label}]` : address;
|
|
55
86
|
}
|
|
87
|
+
/**
|
|
88
|
+
* The viem {@link Chain} object for this register's connected client.
|
|
89
|
+
**/
|
|
56
90
|
get chain() {
|
|
57
91
|
return this.client.chain;
|
|
58
92
|
}
|
|
93
|
+
/**
|
|
94
|
+
* Numeric chain ID (e.g. `1` for Ethereum mainnet).
|
|
95
|
+
**/
|
|
59
96
|
get chainId() {
|
|
60
97
|
return this.client.chain.id;
|
|
61
98
|
}
|
|
@@ -4,9 +4,6 @@ class Construct {
|
|
|
4
4
|
logger;
|
|
5
5
|
client;
|
|
6
6
|
#register;
|
|
7
|
-
/**
|
|
8
|
-
* Indicates that contract state needs to be updated
|
|
9
|
-
*/
|
|
10
7
|
#dirty = false;
|
|
11
8
|
constructor(options) {
|
|
12
9
|
if (options instanceof ChainContractsRegister) {
|
|
@@ -38,12 +35,22 @@ class Construct {
|
|
|
38
35
|
safeGetRegister() {
|
|
39
36
|
return this.#register;
|
|
40
37
|
}
|
|
38
|
+
/**
|
|
39
|
+
* The viem {@link Chain} object associated with the connected client.
|
|
40
|
+
**/
|
|
41
41
|
get chain() {
|
|
42
42
|
return this.client.chain;
|
|
43
43
|
}
|
|
44
|
+
/**
|
|
45
|
+
* Numeric chain ID (e.g. `1` for Ethereum mainnet).
|
|
46
|
+
**/
|
|
44
47
|
get chainId() {
|
|
45
48
|
return this.client.chain.id;
|
|
46
49
|
}
|
|
50
|
+
/**
|
|
51
|
+
* Gearbox network type for this chain (e.g. `"Mainnet"`, `"Arbitrum"`).
|
|
52
|
+
* @throws If the chain was not created by the Gearbox SDK.
|
|
53
|
+
*/
|
|
47
54
|
get networkType() {
|
|
48
55
|
if ("network" in this.chain) {
|
|
49
56
|
return this.chain.network;
|
|
@@ -51,6 +58,7 @@ class Construct {
|
|
|
51
58
|
throw new Error(`chain ${this.chain.id} is not a Gearbox SDK chain`);
|
|
52
59
|
}
|
|
53
60
|
/**
|
|
61
|
+
* @internal
|
|
54
62
|
* Indicates that contract state diverged from onchain state and needs to be updated
|
|
55
63
|
*/
|
|
56
64
|
get dirty() {
|
|
@@ -59,6 +67,9 @@ class Construct {
|
|
|
59
67
|
set dirty(value) {
|
|
60
68
|
this.#dirty = value;
|
|
61
69
|
}
|
|
70
|
+
/**
|
|
71
|
+
* Information about tokens known on this chain
|
|
72
|
+
*/
|
|
62
73
|
get tokensMeta() {
|
|
63
74
|
return this.register.tokensMeta;
|
|
64
75
|
}
|
|
@@ -66,6 +77,7 @@ class Construct {
|
|
|
66
77
|
return this.#register?.labelAddress(address, omitAddress) ?? address;
|
|
67
78
|
}
|
|
68
79
|
/**
|
|
80
|
+
* @internal
|
|
69
81
|
* Returns list of addresses that should be watched for events to sync state
|
|
70
82
|
*/
|
|
71
83
|
get watchAddresses() {
|
|
@@ -12,13 +12,26 @@ class TokensMeta extends AddressMap {
|
|
|
12
12
|
super(void 0, "tokensMeta");
|
|
13
13
|
this.#client = client;
|
|
14
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* Clears all token metadata
|
|
17
|
+
**/
|
|
15
18
|
reset() {
|
|
16
19
|
this.clear();
|
|
17
20
|
this.#phantomTokensLoaded = void 0;
|
|
18
21
|
}
|
|
22
|
+
/**
|
|
23
|
+
* Returns the symbol string for a token.
|
|
24
|
+
* @param token - Token address.
|
|
25
|
+
* @throws If the token is not in the registry.
|
|
26
|
+
*/
|
|
19
27
|
symbol(token) {
|
|
20
28
|
return this.mustGet(token).symbol;
|
|
21
29
|
}
|
|
30
|
+
/**
|
|
31
|
+
* Returns the decimal count for a token.
|
|
32
|
+
* @param token - Token address.
|
|
33
|
+
* @throws If the token is not in the registry.
|
|
34
|
+
*/
|
|
22
35
|
decimals(token) {
|
|
23
36
|
return this.mustGet(token).decimals;
|
|
24
37
|
}
|
|
@@ -53,9 +66,19 @@ class TokensMeta extends AddressMap {
|
|
|
53
66
|
const asStr = formatBN(amount, this.decimals(token), precision);
|
|
54
67
|
return symbol ? `${asStr} ${this.symbol(token)}` : asStr;
|
|
55
68
|
}
|
|
69
|
+
/**
|
|
70
|
+
* Finds a token by its symbol (e.g. `"USDC"`).
|
|
71
|
+
* @param symbol - Case-sensitive ticker symbol.
|
|
72
|
+
* @returns The matching metadata, or `undefined` if no token has this symbol.
|
|
73
|
+
*/
|
|
56
74
|
findBySymbol(symbol) {
|
|
57
75
|
return this.values().find((v) => v.symbol === symbol);
|
|
58
76
|
}
|
|
77
|
+
/**
|
|
78
|
+
* Finds a token by its symbol, throwing if not found.
|
|
79
|
+
* @param symbol - Case-sensitive ticker symbol.
|
|
80
|
+
* @throws If no token matches the symbol.
|
|
81
|
+
*/
|
|
59
82
|
mustFindBySymbol(symbol) {
|
|
60
83
|
const meta = this.findBySymbol(symbol);
|
|
61
84
|
if (!meta) {
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { NOT_DEPLOYED } from "./addresses.js";
|
|
2
1
|
const NO_VERSION = 0;
|
|
3
2
|
const AP_ACCOUNT_FACTORY = "ACCOUNT_FACTORY";
|
|
4
3
|
const AP_ACL = "ACL";
|
|
@@ -32,28 +31,8 @@ const AP_WETH_GATEWAY = "WETH_GATEWAY";
|
|
|
32
31
|
const AP_WETH_TOKEN = "WETH_TOKEN";
|
|
33
32
|
const AP_ZAPPER_REGISTER = "ZAPPER_REGISTER";
|
|
34
33
|
const AP_ZERO_PRICE_FEED = "ZERO_PRICE_FEED";
|
|
35
|
-
const ADDRESS_PROVIDER = {
|
|
36
|
-
Mainnet: "0x9ea7b04Da02a5373317D745c1571c84aaD03321D",
|
|
37
|
-
Arbitrum: "0x7d04eCdb892Ae074f03B5D0aBA03796F90F3F2af",
|
|
38
|
-
Optimism: "0x3761ca4BFAcFCFFc1B8034e69F19116dD6756726",
|
|
39
|
-
Base: NOT_DEPLOYED,
|
|
40
|
-
Sonic: "0x4b27b296273B72d7c7bfee1ACE93DC081467C41B",
|
|
41
|
-
// New networks
|
|
42
|
-
MegaETH: NOT_DEPLOYED,
|
|
43
|
-
Monad: NOT_DEPLOYED,
|
|
44
|
-
Berachain: NOT_DEPLOYED,
|
|
45
|
-
Avalanche: NOT_DEPLOYED,
|
|
46
|
-
BNB: NOT_DEPLOYED,
|
|
47
|
-
WorldChain: NOT_DEPLOYED,
|
|
48
|
-
Etherlink: NOT_DEPLOYED,
|
|
49
|
-
Hemi: NOT_DEPLOYED,
|
|
50
|
-
Lisk: NOT_DEPLOYED,
|
|
51
|
-
Plasma: NOT_DEPLOYED,
|
|
52
|
-
Somnia: NOT_DEPLOYED
|
|
53
|
-
};
|
|
54
34
|
const ADDRESS_PROVIDER_V310 = "0xF7f0a609BfAb9a0A98786951ef10e5FE26cC1E38";
|
|
55
35
|
export {
|
|
56
|
-
ADDRESS_PROVIDER,
|
|
57
36
|
ADDRESS_PROVIDER_V310,
|
|
58
37
|
AP_ACCOUNT_FACTORY,
|
|
59
38
|
AP_ACL,
|
|
@@ -33,6 +33,9 @@ class AbstractAddressProviderContract extends BaseContract {
|
|
|
33
33
|
this.#addresses[k][version] = overriden;
|
|
34
34
|
}
|
|
35
35
|
}
|
|
36
|
+
/**
|
|
37
|
+
* {@inheritDoc IAddressProviderContract.getAddress}
|
|
38
|
+
**/
|
|
36
39
|
getAddress(contract, version = NO_VERSION) {
|
|
37
40
|
if (!this.#addresses[contract]) {
|
|
38
41
|
throw new Error(`Address ${contract}, version: ${version} not found`);
|
|
@@ -43,6 +46,9 @@ class AbstractAddressProviderContract extends BaseContract {
|
|
|
43
46
|
}
|
|
44
47
|
return result;
|
|
45
48
|
}
|
|
49
|
+
/**
|
|
50
|
+
* {@inheritDoc IAddressProviderContract.getLatest}
|
|
51
|
+
**/
|
|
46
52
|
getLatest(contract, range) {
|
|
47
53
|
const allVersions = this.#addresses[contract];
|
|
48
54
|
if (!allVersions) {
|
|
@@ -62,6 +68,9 @@ class AbstractAddressProviderContract extends BaseContract {
|
|
|
62
68
|
}
|
|
63
69
|
return [address, version];
|
|
64
70
|
}
|
|
71
|
+
/**
|
|
72
|
+
* {@inheritDoc IAddressProviderContract.mustGetLatest}
|
|
73
|
+
**/
|
|
65
74
|
mustGetLatest(contract, range) {
|
|
66
75
|
const result = this.getLatest(contract, range);
|
|
67
76
|
if (!result) {
|
|
@@ -69,6 +78,9 @@ class AbstractAddressProviderContract extends BaseContract {
|
|
|
69
78
|
}
|
|
70
79
|
return result;
|
|
71
80
|
}
|
|
81
|
+
/**
|
|
82
|
+
* {@inheritDoc IAddressProviderContract.state}
|
|
83
|
+
**/
|
|
72
84
|
get state() {
|
|
73
85
|
return {
|
|
74
86
|
baseParams: {
|
|
@@ -80,6 +92,9 @@ class AbstractAddressProviderContract extends BaseContract {
|
|
|
80
92
|
addresses: this.#addresses
|
|
81
93
|
};
|
|
82
94
|
}
|
|
95
|
+
/**
|
|
96
|
+
* {@inheritDoc IAddressProviderContract.stateHuman}
|
|
97
|
+
**/
|
|
83
98
|
stateHuman(raw = true) {
|
|
84
99
|
return {
|
|
85
100
|
...super.stateHuman(raw),
|