@gearbox-protocol/sdk 3.0.0-vfour.13 → 3.0.0-vfour.131
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/README.md +0 -1
- package/dist/cjs/dev/index.cjs +452 -27
- package/dist/cjs/dev/index.d.ts +96 -4
- package/dist/cjs/sdk/index.cjs +56640 -10815
- package/dist/cjs/sdk/index.d.ts +84844 -10049
- package/dist/esm/dev/index.d.mts +96 -4
- package/dist/esm/dev/index.mjs +448 -29
- package/dist/esm/sdk/index.d.mts +84844 -10049
- package/dist/esm/sdk/index.mjs +56343 -10702
- package/package.json +21 -16
package/README.md
CHANGED
|
@@ -3,7 +3,6 @@
|
|
|
3
3
|
Gearbox core types SDK
|
|
4
4
|
Contains core datastructures which are used across multiple gearbox services
|
|
5
5
|
|
|
6
|
-
|
|
7
6
|
### Important information for contributors
|
|
8
7
|
|
|
9
8
|
As a contributor to the Gearbox Protocol GitHub repository, your pull requests indicate acceptance of our Gearbox Contribution Agreement. This agreement outlines that you assign the Intellectual Property Rights of your contributions to the Gearbox Foundation. This helps safeguard the Gearbox protocol and ensure the accumulation of its intellectual property. Contributions become part of the repository and may be used for various purposes, including commercial. As recognition for your expertise and work, you receive the opportunity to participate in the protocol's development and the potential to see your work integrated within it. The full Gearbox Contribution Agreement is accessible within the [repository](/ContributionAgreement) for comprehensive understanding. [Let's innovate together!]
|
package/dist/cjs/dev/index.cjs
CHANGED
|
@@ -1,9 +1,326 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var sdkGov = require('@gearbox-protocol/sdk-gov');
|
|
4
4
|
var viem = require('viem');
|
|
5
|
+
var accounts = require('viem/accounts');
|
|
6
|
+
var sdk = require('../sdk');
|
|
7
|
+
|
|
8
|
+
// src/dev/AccountOpener.ts
|
|
9
|
+
function createAnvilClient({
|
|
10
|
+
chain,
|
|
11
|
+
transport
|
|
12
|
+
}) {
|
|
13
|
+
return viem.createTestClient(
|
|
14
|
+
{
|
|
15
|
+
chain,
|
|
16
|
+
mode: "anvil",
|
|
17
|
+
transport,
|
|
18
|
+
cacheTime: 0
|
|
19
|
+
}
|
|
20
|
+
).extend(viem.publicActions).extend(viem.walletActions).extend((client) => ({
|
|
21
|
+
anvilNodeInfo: () => anvilNodeInfo(client),
|
|
22
|
+
isAnvil: () => isAnvil(client),
|
|
23
|
+
evmMineDetailed: (timestamp) => evmMineDetailed(client, timestamp)
|
|
24
|
+
}));
|
|
25
|
+
}
|
|
26
|
+
async function isAnvil(client) {
|
|
27
|
+
try {
|
|
28
|
+
const resp = await client.request({
|
|
29
|
+
method: "anvil_nodeInfo",
|
|
30
|
+
params: []
|
|
31
|
+
});
|
|
32
|
+
return !!resp.currentBlockNumber;
|
|
33
|
+
} catch {
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
async function anvilNodeInfo(client) {
|
|
38
|
+
return client.request({
|
|
39
|
+
method: "anvil_nodeInfo",
|
|
40
|
+
params: []
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
async function evmMineDetailed(client, timestamp) {
|
|
44
|
+
try {
|
|
45
|
+
const [block] = await client.request({
|
|
46
|
+
method: "evm_mine_detailed",
|
|
47
|
+
params: [viem.toHex(timestamp)]
|
|
48
|
+
});
|
|
49
|
+
return block;
|
|
50
|
+
} catch {
|
|
51
|
+
return void 0;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
5
54
|
|
|
6
|
-
// src/dev/
|
|
55
|
+
// src/dev/AccountOpener.ts
|
|
56
|
+
var AccountOpener = class {
|
|
57
|
+
#service;
|
|
58
|
+
#anvil;
|
|
59
|
+
#logger;
|
|
60
|
+
#borrower;
|
|
61
|
+
#faucet;
|
|
62
|
+
constructor(service, options = {}) {
|
|
63
|
+
this.#service = service;
|
|
64
|
+
this.#logger = sdk.childLogger("AccountOpener", service.sdk.logger);
|
|
65
|
+
this.#anvil = createAnvilClient({
|
|
66
|
+
chain: service.sdk.provider.chain,
|
|
67
|
+
transport: service.sdk.provider.transport
|
|
68
|
+
});
|
|
69
|
+
this.#faucet = options.faucet ?? service.sdk.addressProvider.getAddress("FAUCET");
|
|
70
|
+
}
|
|
71
|
+
get borrower() {
|
|
72
|
+
if (!this.#borrower) {
|
|
73
|
+
throw new Error("borrower can be used only after openCreditAccounts");
|
|
74
|
+
}
|
|
75
|
+
return this.#borrower.address;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Tries to open account with underlying only in each CM
|
|
79
|
+
*/
|
|
80
|
+
async openCreditAccounts(targets) {
|
|
81
|
+
const borrower = await this.#prepareBorrower(targets);
|
|
82
|
+
for (const target of targets) {
|
|
83
|
+
try {
|
|
84
|
+
await this.#openAccount(target);
|
|
85
|
+
} catch (e) {
|
|
86
|
+
this.#logger?.error(e);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
const accounts = await this.#service.getCreditAccounts({
|
|
90
|
+
owner: borrower.address
|
|
91
|
+
});
|
|
92
|
+
this.#logger?.info(`opened ${accounts.length} accounts`);
|
|
93
|
+
}
|
|
94
|
+
async #openAccount({
|
|
95
|
+
creditManager,
|
|
96
|
+
collateral,
|
|
97
|
+
leverage = 4,
|
|
98
|
+
slippage = 50
|
|
99
|
+
}) {
|
|
100
|
+
const borrower = await this.#getBorrower();
|
|
101
|
+
const cm = this.sdk.marketRegister.findCreditManager(creditManager);
|
|
102
|
+
const symbol = this.sdk.tokensMeta.symbol(collateral);
|
|
103
|
+
const logger = this.#logger?.child?.({
|
|
104
|
+
creditManager: cm.name,
|
|
105
|
+
collateral: symbol
|
|
106
|
+
});
|
|
107
|
+
const { minDebt, underlying } = cm.creditFacade;
|
|
108
|
+
const expectedBalances = [];
|
|
109
|
+
const leftoverBalances = [];
|
|
110
|
+
for (const t of Object.keys(cm.collateralTokens)) {
|
|
111
|
+
const token = t;
|
|
112
|
+
expectedBalances.push({
|
|
113
|
+
token,
|
|
114
|
+
balance: token === underlying ? BigInt(leverage) * minDebt : 1n
|
|
115
|
+
});
|
|
116
|
+
leftoverBalances.push({
|
|
117
|
+
token,
|
|
118
|
+
balance: 1n
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
logger?.debug("looking for open strategy");
|
|
122
|
+
const strategy = await this.sdk.router.findOpenStrategyPath({
|
|
123
|
+
creditManager: cm.creditManager,
|
|
124
|
+
expectedBalances,
|
|
125
|
+
leftoverBalances,
|
|
126
|
+
slippage,
|
|
127
|
+
target: collateral
|
|
128
|
+
});
|
|
129
|
+
logger?.debug("found open strategy");
|
|
130
|
+
const { tx, calls } = await this.#service.openCA({
|
|
131
|
+
creditManager: cm.creditManager.address,
|
|
132
|
+
averageQuota: [],
|
|
133
|
+
minQuota: [],
|
|
134
|
+
collateral: [{ token: underlying, balance: minDebt }],
|
|
135
|
+
debt: minDebt * BigInt(leverage - 1),
|
|
136
|
+
calls: strategy.calls,
|
|
137
|
+
ethAmount: 0n,
|
|
138
|
+
permits: {},
|
|
139
|
+
to: borrower.address,
|
|
140
|
+
referralCode: 0n
|
|
141
|
+
});
|
|
142
|
+
for (let i = 0; i < calls.length; i++) {
|
|
143
|
+
const call = calls[i];
|
|
144
|
+
logger?.debug(
|
|
145
|
+
`call #${i + 1}: ${this.sdk.parseFunctionData(call.target, call.callData)}`
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
logger?.debug("prepared open account transaction");
|
|
149
|
+
const hash = await sdk.sendRawTx(this.#anvil, {
|
|
150
|
+
tx,
|
|
151
|
+
account: borrower
|
|
152
|
+
});
|
|
153
|
+
logger?.debug(`send transaction ${hash}`);
|
|
154
|
+
const receipt = await this.#anvil.waitForTransactionReceipt({ hash });
|
|
155
|
+
if (receipt.status === "reverted") {
|
|
156
|
+
throw new Error(`open credit account tx ${hash} reverted`);
|
|
157
|
+
}
|
|
158
|
+
logger?.debug(
|
|
159
|
+
`opened credit account in ${cm.name} with collateral ${symbol}`
|
|
160
|
+
);
|
|
161
|
+
return this.getOpenedAccounts();
|
|
162
|
+
}
|
|
163
|
+
async getOpenedAccounts() {
|
|
164
|
+
return await this.#service.getCreditAccounts({
|
|
165
|
+
owner: this.borrower
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Creates borrower wallet,
|
|
170
|
+
* Sets ETH balance,
|
|
171
|
+
* Gets tokens from faucet,
|
|
172
|
+
* Approves collateral tokens to credit manager,
|
|
173
|
+
* Gets DEGEN_NFT,
|
|
174
|
+
*/
|
|
175
|
+
async #prepareBorrower(targets) {
|
|
176
|
+
const borrower = await this.#getBorrower();
|
|
177
|
+
let claimUSD = 0n;
|
|
178
|
+
let degenNFTS = {};
|
|
179
|
+
for (const target of targets) {
|
|
180
|
+
const cm = this.sdk.marketRegister.findCreditManager(
|
|
181
|
+
target.creditManager
|
|
182
|
+
);
|
|
183
|
+
const market = this.sdk.marketRegister.findByCreditManager(
|
|
184
|
+
target.creditManager
|
|
185
|
+
);
|
|
186
|
+
const { minDebt, degenNFT } = cm.creditFacade;
|
|
187
|
+
claimUSD += market.priceOracle.convertToUSD(cm.underlying, minDebt);
|
|
188
|
+
if (viem.isAddress(degenNFT) && degenNFT !== sdkGov.ADDRESS_0X0) {
|
|
189
|
+
degenNFTS[degenNFT] = (degenNFTS[degenNFT] ?? 0) + 1;
|
|
190
|
+
}
|
|
191
|
+
for (const t of Object.keys(cm.collateralTokens)) {
|
|
192
|
+
await this.#approve(t, cm);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
claimUSD = claimUSD * 11n / 10n;
|
|
196
|
+
this.#logger?.debug(`claiming ${sdkGov.formatBN(claimUSD, 8)} USD from faucet`);
|
|
197
|
+
let hash = await this.#anvil.writeContract({
|
|
198
|
+
account: borrower,
|
|
199
|
+
address: this.#faucet,
|
|
200
|
+
abi: [
|
|
201
|
+
{
|
|
202
|
+
type: "function",
|
|
203
|
+
inputs: [
|
|
204
|
+
{ name: "amountUSD", internalType: "uint256", type: "uint256" }
|
|
205
|
+
],
|
|
206
|
+
name: "claim",
|
|
207
|
+
outputs: [],
|
|
208
|
+
stateMutability: "nonpayable"
|
|
209
|
+
}
|
|
210
|
+
],
|
|
211
|
+
functionName: "claim",
|
|
212
|
+
args: [claimUSD],
|
|
213
|
+
chain: this.#anvil.chain
|
|
214
|
+
});
|
|
215
|
+
let receipt = await this.#anvil.waitForTransactionReceipt({
|
|
216
|
+
hash
|
|
217
|
+
});
|
|
218
|
+
if (receipt.status === "reverted") {
|
|
219
|
+
throw new Error(
|
|
220
|
+
`borrower ${borrower.address} failed to claimed equivalent of ${sdkGov.formatBN(claimUSD, 8)} USD from faucet, tx: ${hash}`
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
this.#logger?.debug(
|
|
224
|
+
`borrower ${borrower.address} claimed equivalent of ${sdkGov.formatBN(claimUSD, 8)} USD from faucet, tx: ${hash}`
|
|
225
|
+
);
|
|
226
|
+
for (const [degenNFT, amount] of Object.entries(degenNFTS)) {
|
|
227
|
+
await this.#mintDegenNft(degenNFT, borrower.address, amount);
|
|
228
|
+
}
|
|
229
|
+
this.#logger?.debug("prepared borrower");
|
|
230
|
+
return borrower;
|
|
231
|
+
}
|
|
232
|
+
async #approve(token, cm) {
|
|
233
|
+
const borrower = await this.#getBorrower();
|
|
234
|
+
try {
|
|
235
|
+
const hash = await this.#anvil.writeContract({
|
|
236
|
+
account: borrower,
|
|
237
|
+
address: token,
|
|
238
|
+
abi: sdk.ierc20Abi,
|
|
239
|
+
functionName: "approve",
|
|
240
|
+
args: [cm.creditManager.address, sdk.MAX_UINT256],
|
|
241
|
+
chain: this.#anvil.chain
|
|
242
|
+
});
|
|
243
|
+
const receipt = await this.#anvil.waitForTransactionReceipt({
|
|
244
|
+
hash
|
|
245
|
+
});
|
|
246
|
+
if (receipt.status === "reverted") {
|
|
247
|
+
this.#logger?.error(
|
|
248
|
+
`failed to allowed credit manager ${cm.creditManager.name} to spend ${token}, tx reverted: ${hash}`
|
|
249
|
+
);
|
|
250
|
+
} else {
|
|
251
|
+
this.#logger?.debug(
|
|
252
|
+
`allowed credit manager ${cm.creditManager.name} to spend ${token}, tx: ${hash}`
|
|
253
|
+
);
|
|
254
|
+
}
|
|
255
|
+
} catch (e) {
|
|
256
|
+
this.#logger?.error(
|
|
257
|
+
`failed to allowed credit manager ${cm.creditManager.name} to spend ${token}: ${e}`
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
async #mintDegenNft(degenNFT, to, amount) {
|
|
262
|
+
if (amount <= 0) {
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
let minter;
|
|
266
|
+
try {
|
|
267
|
+
minter = await this.#anvil.readContract({
|
|
268
|
+
address: degenNFT,
|
|
269
|
+
abi: sdk.iDegenNftv2Abi,
|
|
270
|
+
functionName: "minter"
|
|
271
|
+
});
|
|
272
|
+
} catch (e) {
|
|
273
|
+
this.#logger?.error(`failed to get minter of degenNFT ${degenNFT}: ${e}`);
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
try {
|
|
277
|
+
await this.#anvil.impersonateAccount({ address: minter });
|
|
278
|
+
await this.#anvil.setBalance({
|
|
279
|
+
address: minter,
|
|
280
|
+
value: viem.parseEther("100")
|
|
281
|
+
});
|
|
282
|
+
const hash = await this.#anvil.writeContract({
|
|
283
|
+
account: minter,
|
|
284
|
+
address: degenNFT,
|
|
285
|
+
abi: sdk.iDegenNftv2Abi,
|
|
286
|
+
functionName: "mint",
|
|
287
|
+
args: [to, BigInt(amount)],
|
|
288
|
+
chain: this.#anvil.chain
|
|
289
|
+
});
|
|
290
|
+
const receipt = await this.#anvil.waitForTransactionReceipt({
|
|
291
|
+
hash
|
|
292
|
+
});
|
|
293
|
+
if (receipt.status === "reverted") {
|
|
294
|
+
this.#logger?.error(
|
|
295
|
+
`failed to mint ${amount} degenNFT ${degenNFT} to borrower ${to}, tx reverted: ${hash}`
|
|
296
|
+
);
|
|
297
|
+
}
|
|
298
|
+
this.#logger?.debug(
|
|
299
|
+
`minted ${amount} degenNFT ${degenNFT} to borrower ${to}, tx: ${hash}`
|
|
300
|
+
);
|
|
301
|
+
} catch (e) {
|
|
302
|
+
this.#logger?.error(
|
|
303
|
+
`failed to mint ${amount} degenNFT ${degenNFT} to borrower ${to}: ${e}`
|
|
304
|
+
);
|
|
305
|
+
} finally {
|
|
306
|
+
await this.#anvil.stopImpersonatingAccount({ address: minter });
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
async #getBorrower() {
|
|
310
|
+
if (!this.#borrower) {
|
|
311
|
+
this.#borrower = accounts.privateKeyToAccount(accounts.generatePrivateKey());
|
|
312
|
+
await this.#anvil.setBalance({
|
|
313
|
+
address: this.#borrower.address,
|
|
314
|
+
value: viem.parseEther("100")
|
|
315
|
+
});
|
|
316
|
+
this.#logger?.info(`created borrower ${this.#borrower.address}`);
|
|
317
|
+
}
|
|
318
|
+
return this.#borrower;
|
|
319
|
+
}
|
|
320
|
+
get sdk() {
|
|
321
|
+
return this.#service.sdk;
|
|
322
|
+
}
|
|
323
|
+
};
|
|
7
324
|
async function calcLiquidatableLTs(sdk$1, ca, factor = 9990n, logger) {
|
|
8
325
|
const cm = sdk$1.marketRegister.findCreditManager(ca.creditManager);
|
|
9
326
|
const market = sdk$1.marketRegister.findByCreditManager(ca.creditManager);
|
|
@@ -42,7 +359,7 @@ async function calcLiquidatableLTs(sdk$1, ca, factor = 9990n, logger) {
|
|
|
42
359
|
if (token !== ca.underlying) {
|
|
43
360
|
const newLT = oldLT * k / sdk.WAD;
|
|
44
361
|
logger?.debug(
|
|
45
|
-
`proposed ${sdk$1.
|
|
362
|
+
`proposed ${sdk$1.tokensMeta.symbol(token)} LT change: ${oldLT} => ${newLT} `
|
|
46
363
|
);
|
|
47
364
|
result[token] = Number(newLT);
|
|
48
365
|
}
|
|
@@ -150,6 +467,21 @@ var iaclAbi = [
|
|
|
150
467
|
name: "AddressNotUnpausableAdminException"
|
|
151
468
|
}
|
|
152
469
|
];
|
|
470
|
+
var iaclTraitAbi = [
|
|
471
|
+
{
|
|
472
|
+
type: "function",
|
|
473
|
+
name: "acl",
|
|
474
|
+
inputs: [],
|
|
475
|
+
outputs: [
|
|
476
|
+
{
|
|
477
|
+
name: "",
|
|
478
|
+
type: "address",
|
|
479
|
+
internalType: "address"
|
|
480
|
+
}
|
|
481
|
+
],
|
|
482
|
+
stateMutability: "view"
|
|
483
|
+
}
|
|
484
|
+
];
|
|
153
485
|
var iCreditConfiguratorV3Abi = [
|
|
154
486
|
{
|
|
155
487
|
type: "function",
|
|
@@ -1413,17 +1745,17 @@ var iCreditManagerV3Abi = [
|
|
|
1413
1745
|
];
|
|
1414
1746
|
|
|
1415
1747
|
// src/dev/setLTs.ts
|
|
1416
|
-
async function setLTs(
|
|
1417
|
-
const aclAddr =
|
|
1418
|
-
|
|
1748
|
+
async function setLTs(anvil, cm, newLTs, logger) {
|
|
1749
|
+
const aclAddr = await anvil.readContract({
|
|
1750
|
+
address: cm.creditConfigurator,
|
|
1751
|
+
abi: iaclTraitAbi,
|
|
1752
|
+
functionName: "acl"
|
|
1753
|
+
});
|
|
1754
|
+
const configuratorAddr = await anvil.readContract({
|
|
1419
1755
|
address: aclAddr,
|
|
1420
1756
|
abi: iaclAbi,
|
|
1421
1757
|
functionName: "owner"
|
|
1422
1758
|
});
|
|
1423
|
-
const anvil = sdk.createAnvilClient({
|
|
1424
|
-
transport: sdk$1.provider.transport,
|
|
1425
|
-
chain: sdk$1.provider.chain
|
|
1426
|
-
});
|
|
1427
1759
|
await anvil.impersonateAccount({
|
|
1428
1760
|
address: configuratorAddr
|
|
1429
1761
|
});
|
|
@@ -1432,28 +1764,121 @@ async function setLTs(sdk$1, cm, newLTs, logger) {
|
|
|
1432
1764
|
value: viem.parseEther("100")
|
|
1433
1765
|
});
|
|
1434
1766
|
for (const [t, lt] of Object.entries(newLTs)) {
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
`set ${
|
|
1451
|
-
|
|
1767
|
+
try {
|
|
1768
|
+
await anvil.writeContract({
|
|
1769
|
+
chain: anvil.chain,
|
|
1770
|
+
address: cm.creditConfigurator,
|
|
1771
|
+
account: configuratorAddr,
|
|
1772
|
+
abi: iCreditConfiguratorV3Abi,
|
|
1773
|
+
functionName: "setLiquidationThreshold",
|
|
1774
|
+
args: [t, lt]
|
|
1775
|
+
});
|
|
1776
|
+
const newLT = await anvil.readContract({
|
|
1777
|
+
address: cm.baseParams.addr,
|
|
1778
|
+
abi: iCreditManagerV3Abi,
|
|
1779
|
+
functionName: "liquidationThresholds",
|
|
1780
|
+
args: [t]
|
|
1781
|
+
});
|
|
1782
|
+
logger?.debug(`set ${t} LT to ${newLT}`);
|
|
1783
|
+
} catch {
|
|
1784
|
+
}
|
|
1452
1785
|
}
|
|
1453
1786
|
await anvil.stopImpersonatingAccount({
|
|
1454
1787
|
address: configuratorAddr
|
|
1455
1788
|
});
|
|
1456
1789
|
}
|
|
1790
|
+
async function setLTZero(anvil, cm, logger) {
|
|
1791
|
+
const aclAddr = await anvil.readContract({
|
|
1792
|
+
address: cm.creditConfigurator,
|
|
1793
|
+
abi: iaclTraitAbi,
|
|
1794
|
+
functionName: "acl"
|
|
1795
|
+
});
|
|
1796
|
+
const configuratorAddr = await anvil.readContract({
|
|
1797
|
+
address: aclAddr,
|
|
1798
|
+
abi: iaclAbi,
|
|
1799
|
+
functionName: "owner"
|
|
1800
|
+
});
|
|
1801
|
+
await anvil.impersonateAccount({
|
|
1802
|
+
address: configuratorAddr
|
|
1803
|
+
});
|
|
1804
|
+
await anvil.setBalance({
|
|
1805
|
+
address: configuratorAddr,
|
|
1806
|
+
value: viem.parseEther("100")
|
|
1807
|
+
});
|
|
1808
|
+
let hash = await anvil.writeContract({
|
|
1809
|
+
chain: anvil.chain,
|
|
1810
|
+
address: cm.creditConfigurator,
|
|
1811
|
+
account: configuratorAddr,
|
|
1812
|
+
abi: iCreditConfiguratorV3Abi,
|
|
1813
|
+
functionName: "setFees",
|
|
1814
|
+
args: [
|
|
1815
|
+
cm.feeInterest,
|
|
1816
|
+
cm.liquidationDiscount - 1,
|
|
1817
|
+
Number(sdk.PERCENTAGE_FACTOR) - cm.liquidationDiscount,
|
|
1818
|
+
cm.feeLiquidationExpired,
|
|
1819
|
+
cm.liquidationDiscountExpired
|
|
1820
|
+
]
|
|
1821
|
+
});
|
|
1822
|
+
await anvil.waitForTransactionReceipt({ hash });
|
|
1823
|
+
logger?.debug(`[${cm.name}] setFees part 2`);
|
|
1824
|
+
hash = await anvil.writeContract({
|
|
1825
|
+
chain: anvil.chain,
|
|
1826
|
+
address: cm.creditConfigurator,
|
|
1827
|
+
account: configuratorAddr,
|
|
1828
|
+
abi: iCreditConfiguratorV3Abi,
|
|
1829
|
+
functionName: "setFees",
|
|
1830
|
+
args: [
|
|
1831
|
+
cm.feeInterest,
|
|
1832
|
+
cm.feeLiquidation,
|
|
1833
|
+
Number(sdk.PERCENTAGE_FACTOR) - cm.liquidationDiscount,
|
|
1834
|
+
cm.feeLiquidationExpired,
|
|
1835
|
+
cm.liquidationDiscountExpired
|
|
1836
|
+
]
|
|
1837
|
+
});
|
|
1838
|
+
await anvil.waitForTransactionReceipt({ hash });
|
|
1839
|
+
logger?.debug(`[${cm.name}] setFees done`);
|
|
1840
|
+
await anvil.impersonateAccount({
|
|
1841
|
+
address: cm.creditConfigurator
|
|
1842
|
+
});
|
|
1843
|
+
await anvil.setBalance({
|
|
1844
|
+
address: cm.creditConfigurator,
|
|
1845
|
+
value: viem.parseEther("100")
|
|
1846
|
+
});
|
|
1847
|
+
logger?.debug(
|
|
1848
|
+
`[${cm.name}] impresonating creditConfigurator ${cm.creditConfigurator}`
|
|
1849
|
+
);
|
|
1850
|
+
logger?.debug(`[${cm.name}] setting liquidation threshold`);
|
|
1851
|
+
hash = await anvil.writeContract({
|
|
1852
|
+
chain: anvil.chain,
|
|
1853
|
+
address: cm.baseParams.addr,
|
|
1854
|
+
account: cm.creditConfigurator,
|
|
1855
|
+
abi: iCreditManagerV3Abi,
|
|
1856
|
+
functionName: "setCollateralTokenData",
|
|
1857
|
+
args: [cm.underlying, 1, 1, Number(2n ** 40n - 1n), 0]
|
|
1858
|
+
});
|
|
1859
|
+
await anvil.waitForTransactionReceipt({ hash });
|
|
1860
|
+
logger?.debug(`[${cm.name}] setting configurator ${cm.creditConfigurator}`);
|
|
1861
|
+
hash = await anvil.writeContract({
|
|
1862
|
+
chain: anvil.chain,
|
|
1863
|
+
address: cm.baseParams.addr,
|
|
1864
|
+
account: cm.creditConfigurator,
|
|
1865
|
+
abi: iCreditManagerV3Abi,
|
|
1866
|
+
functionName: "setCreditConfigurator",
|
|
1867
|
+
args: [cm.creditConfigurator]
|
|
1868
|
+
});
|
|
1869
|
+
await anvil.waitForTransactionReceipt({ hash });
|
|
1870
|
+
logger?.debug(`[${cm.name}] done`);
|
|
1871
|
+
await anvil.stopImpersonatingAccount({
|
|
1872
|
+
address: cm.creditConfigurator
|
|
1873
|
+
});
|
|
1874
|
+
await anvil.stopImpersonatingAccount({ address: configuratorAddr });
|
|
1875
|
+
}
|
|
1457
1876
|
|
|
1877
|
+
exports.AccountOpener = AccountOpener;
|
|
1878
|
+
exports.anvilNodeInfo = anvilNodeInfo;
|
|
1458
1879
|
exports.calcLiquidatableLTs = calcLiquidatableLTs;
|
|
1880
|
+
exports.createAnvilClient = createAnvilClient;
|
|
1881
|
+
exports.evmMineDetailed = evmMineDetailed;
|
|
1882
|
+
exports.isAnvil = isAnvil;
|
|
1883
|
+
exports.setLTZero = setLTZero;
|
|
1459
1884
|
exports.setLTs = setLTs;
|
package/dist/cjs/dev/index.d.ts
CHANGED
|
@@ -1,5 +1,26 @@
|
|
|
1
|
-
import { Address } from 'viem';
|
|
2
|
-
import {
|
|
1
|
+
import { Address, TestRpcSchema, Hex, Block, Prettify, Client, Transport, Chain, TestActions, PublicClient, WalletClient } from 'viem';
|
|
2
|
+
import { CreditAccountsService, CreditAccountData, GearboxSDK, ILogger, CreditManagerState } from '../sdk';
|
|
3
|
+
|
|
4
|
+
interface AccountOpenerOptions {
|
|
5
|
+
faucet?: Address;
|
|
6
|
+
}
|
|
7
|
+
interface TargetAccount {
|
|
8
|
+
creditManager: Address;
|
|
9
|
+
collateral: Address;
|
|
10
|
+
leverage?: number;
|
|
11
|
+
slippage?: number;
|
|
12
|
+
}
|
|
13
|
+
declare class AccountOpener {
|
|
14
|
+
#private;
|
|
15
|
+
constructor(service: CreditAccountsService, options?: AccountOpenerOptions);
|
|
16
|
+
get borrower(): Address;
|
|
17
|
+
/**
|
|
18
|
+
* Tries to open account with underlying only in each CM
|
|
19
|
+
*/
|
|
20
|
+
openCreditAccounts(targets: TargetAccount[]): Promise<void>;
|
|
21
|
+
getOpenedAccounts(): Promise<CreditAccountData[]>;
|
|
22
|
+
private get sdk();
|
|
23
|
+
}
|
|
3
24
|
|
|
4
25
|
/**
|
|
5
26
|
* Given credit accounts, calculates new liquidation thresholds that needs to be set to drop account health factor a bit to make it eligible for partial liquidation
|
|
@@ -7,12 +28,83 @@ import { GearboxSDK, CreditAccountData, ILogger, CreditFactory } from '../sdk';
|
|
|
7
28
|
*/
|
|
8
29
|
declare function calcLiquidatableLTs(sdk: GearboxSDK, ca: CreditAccountData, factor?: bigint, logger?: ILogger): Promise<Record<Address, number>>;
|
|
9
30
|
|
|
31
|
+
interface AnvilNodeInfo {
|
|
32
|
+
currentBlockNumber: string;
|
|
33
|
+
currentBlockTimestamp: number;
|
|
34
|
+
currentBlockHash: string;
|
|
35
|
+
hardFork: string;
|
|
36
|
+
transactionOrder: string;
|
|
37
|
+
environment: {
|
|
38
|
+
baseFee: string;
|
|
39
|
+
chainId: number;
|
|
40
|
+
gasLimit: string;
|
|
41
|
+
gasPrice: string;
|
|
42
|
+
};
|
|
43
|
+
forkConfig: {
|
|
44
|
+
forkUrl: string;
|
|
45
|
+
forkBlockNumber: string;
|
|
46
|
+
forkRetryBackoff: number;
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
type AnvilRPCSchema = [
|
|
50
|
+
...TestRpcSchema<"anvil">,
|
|
51
|
+
{
|
|
52
|
+
Method: "anvil_nodeInfo";
|
|
53
|
+
Parameters: [];
|
|
54
|
+
ReturnType: AnvilNodeInfo;
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
Method: "evm_mine_detailed";
|
|
58
|
+
Parameters: [Hex];
|
|
59
|
+
ReturnType: Block<Hex>[];
|
|
60
|
+
}
|
|
61
|
+
];
|
|
62
|
+
type AnvilActions = {
|
|
63
|
+
anvilNodeInfo: () => Promise<AnvilNodeInfo>;
|
|
64
|
+
isAnvil: () => Promise<boolean>;
|
|
65
|
+
evmMineDetailed: (
|
|
66
|
+
/**
|
|
67
|
+
* Block timestamp in seconds
|
|
68
|
+
*/
|
|
69
|
+
timestamp: bigint | number) => Promise<Block<Hex> | undefined>;
|
|
70
|
+
};
|
|
71
|
+
type AnvilClient = Prettify<{
|
|
72
|
+
mode: "anvil";
|
|
73
|
+
} & Client<Transport, Chain, undefined, AnvilRPCSchema, AnvilActions & TestActions>> & PublicClient & WalletClient;
|
|
74
|
+
interface AnvilClientConfig<transport extends Transport = Transport, chain extends Chain | undefined = Chain | undefined> {
|
|
75
|
+
transport: transport;
|
|
76
|
+
chain?: chain;
|
|
77
|
+
}
|
|
78
|
+
declare function createAnvilClient({ chain, transport, }: AnvilClientConfig): AnvilClient;
|
|
79
|
+
/**
|
|
80
|
+
* View action to detect if client is an anvil client
|
|
81
|
+
* @param client
|
|
82
|
+
* @returns
|
|
83
|
+
*/
|
|
84
|
+
declare function isAnvil(client: Client<any, any, any, AnvilRPCSchema, any>): Promise<boolean>;
|
|
85
|
+
/**
|
|
86
|
+
* Anvil node info
|
|
87
|
+
* @param client
|
|
88
|
+
* @returns
|
|
89
|
+
*/
|
|
90
|
+
declare function anvilNodeInfo(client: Client<any, any, any, AnvilRPCSchema, any>): Promise<AnvilNodeInfo>;
|
|
91
|
+
/**
|
|
92
|
+
* Safely tries to mine block with given timestamp
|
|
93
|
+
* @param client
|
|
94
|
+
* @param timestamp in seconds
|
|
95
|
+
* @returns
|
|
96
|
+
*/
|
|
97
|
+
declare function evmMineDetailed(client: Client<any, any, any, AnvilRPCSchema, any>, timestamp: bigint | number): Promise<Block<Hex> | undefined>;
|
|
98
|
+
|
|
10
99
|
/**
|
|
11
100
|
* Helper function to set liquidation thresholds on credit manager via anvil impersonation
|
|
12
101
|
* @param sdk
|
|
13
102
|
* @param cm
|
|
14
103
|
* @param newLTs
|
|
15
104
|
*/
|
|
16
|
-
declare function setLTs(
|
|
105
|
+
declare function setLTs(anvil: AnvilClient, cm: CreditManagerState, newLTs: Record<Address, number>, logger?: ILogger): Promise<void>;
|
|
106
|
+
|
|
107
|
+
type ZeroLTCMSlice = Pick<CreditManagerState, "creditConfigurator" | "feeInterest" | "liquidationDiscount" | "feeLiquidationExpired" | "liquidationDiscountExpired" | "feeLiquidation" | "name" | "underlying" | "baseParams">;
|
|
108
|
+
declare function setLTZero(anvil: AnvilClient, cm: ZeroLTCMSlice, logger?: ILogger): Promise<void>;
|
|
17
109
|
|
|
18
|
-
export { calcLiquidatableLTs, setLTs };
|
|
110
|
+
export { AccountOpener, type AccountOpenerOptions, type AnvilActions, type AnvilClient, type AnvilClientConfig, type AnvilNodeInfo, type AnvilRPCSchema, type TargetAccount, anvilNodeInfo, calcLiquidatableLTs, createAnvilClient, evmMineDetailed, isAnvil, setLTZero, setLTs };
|