@gearbox-protocol/sdk 8.13.0 → 8.14.1
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.
|
@@ -43,6 +43,7 @@ class AccountOpener extends import_sdk.SDKConstruct {
|
|
|
43
43
|
#anvil;
|
|
44
44
|
#logger;
|
|
45
45
|
#borrower;
|
|
46
|
+
#depositor;
|
|
46
47
|
#faucet;
|
|
47
48
|
#poolDepositMultiplier;
|
|
48
49
|
#minDebtMultiplier;
|
|
@@ -60,6 +61,7 @@ class AccountOpener extends import_sdk.SDKConstruct {
|
|
|
60
61
|
this.#logger?.warn("faucet not found, will not claim from faucet");
|
|
61
62
|
}
|
|
62
63
|
this.#borrower = options.borrower;
|
|
64
|
+
this.#depositor = options.depositor;
|
|
63
65
|
this.#poolDepositMultiplier = options.poolDepositMultiplier ?? 11000n;
|
|
64
66
|
this.#minDebtMultiplier = options.minDebtMultiplier ?? 10100n;
|
|
65
67
|
}
|
|
@@ -73,9 +75,10 @@ class AccountOpener extends import_sdk.SDKConstruct {
|
|
|
73
75
|
* Tries to open account with underlying only in each CM
|
|
74
76
|
*/
|
|
75
77
|
async openCreditAccounts(targets, depositIntoPools = true, claimFromFaucet2 = true) {
|
|
78
|
+
let deposits = [];
|
|
76
79
|
if (depositIntoPools) {
|
|
77
80
|
try {
|
|
78
|
-
await this.#depositIntoPools(targets);
|
|
81
|
+
deposits = await this.#depositIntoPools(targets);
|
|
79
82
|
} catch (e) {
|
|
80
83
|
this.#logger?.warn(`failed to deposit into pools: ${e}`);
|
|
81
84
|
}
|
|
@@ -96,13 +99,13 @@ class AccountOpener extends import_sdk.SDKConstruct {
|
|
|
96
99
|
await this.#approve(token, cm);
|
|
97
100
|
}
|
|
98
101
|
}
|
|
99
|
-
const
|
|
102
|
+
const accounts = [];
|
|
100
103
|
let success = 0;
|
|
101
104
|
for (let i = 0; i < targets.length; i++) {
|
|
102
105
|
const target = targets[i];
|
|
103
106
|
try {
|
|
104
107
|
const result = await this.#openAccount(target, i + 1, targets.length);
|
|
105
|
-
|
|
108
|
+
accounts.push(result);
|
|
106
109
|
success += result.account ? 1 : 0;
|
|
107
110
|
if (result.error) {
|
|
108
111
|
this.#logger?.error(
|
|
@@ -113,7 +116,7 @@ class AccountOpener extends import_sdk.SDKConstruct {
|
|
|
113
116
|
this.#logger?.error(
|
|
114
117
|
`failed to open account #${i + 1}/${targets.length}: ${e}`
|
|
115
118
|
);
|
|
116
|
-
|
|
119
|
+
accounts.push({
|
|
117
120
|
input: target,
|
|
118
121
|
error: e
|
|
119
122
|
});
|
|
@@ -121,12 +124,12 @@ class AccountOpener extends import_sdk.SDKConstruct {
|
|
|
121
124
|
}
|
|
122
125
|
this.#logger?.info(`opened ${success}/${targets.length} accounts`);
|
|
123
126
|
try {
|
|
124
|
-
await this.#directlyDistributeTokens(
|
|
127
|
+
await this.#directlyDistributeTokens(accounts);
|
|
125
128
|
this.#logger?.info("distributed direct transfer tokens");
|
|
126
129
|
} catch (e) {
|
|
127
130
|
this.#logger?.error(`failed to distribute tokens: ${e}`);
|
|
128
131
|
}
|
|
129
|
-
return
|
|
132
|
+
return { deposits, accounts };
|
|
130
133
|
}
|
|
131
134
|
async #openAccount(input, index, total) {
|
|
132
135
|
const { creditManager, target: collateral } = input;
|
|
@@ -313,61 +316,85 @@ class AccountOpener extends import_sdk.SDKConstruct {
|
|
|
313
316
|
this.#logger?.debug(
|
|
314
317
|
`total USD to claim from faucet: ${(0, import_sdk.formatBN)(totalUSD, 8)}`
|
|
315
318
|
);
|
|
316
|
-
const depositor = await this.#createAccount();
|
|
319
|
+
const depositor = this.#depositor ?? await this.#createAccount();
|
|
317
320
|
this.#logger?.debug(`created depositor ${depositor.address}`);
|
|
318
321
|
await this.#claimFromFaucet(depositor, "depositor", totalUSD);
|
|
322
|
+
const results = [];
|
|
319
323
|
for (const [pool, amount] of deposits) {
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
} catch (e) {
|
|
323
|
-
this.#logger?.warn(`failed to deposit into ${pool.name}: ${e}`);
|
|
324
|
-
}
|
|
324
|
+
const result = await this.#depositToPool(pool, depositor, amount);
|
|
325
|
+
results.push(result);
|
|
325
326
|
}
|
|
327
|
+
return results;
|
|
326
328
|
}
|
|
327
329
|
async #depositToPool(pool, depositor, amount) {
|
|
328
330
|
const { underlying, address } = pool;
|
|
329
331
|
const poolName = this.sdk.provider.addressLabels.get(address);
|
|
330
|
-
const
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
chain: this.#anvil.chain
|
|
348
|
-
});
|
|
349
|
-
let receipt = await this.#anvil.waitForTransactionReceipt({ hash });
|
|
350
|
-
if (receipt.status === "reverted") {
|
|
351
|
-
throw new Error(
|
|
352
|
-
`tx ${hash} that approves underlying from depositor for pool ${poolName} reverted`
|
|
332
|
+
const poolData = {
|
|
333
|
+
pool: address,
|
|
334
|
+
token: underlying,
|
|
335
|
+
amount
|
|
336
|
+
};
|
|
337
|
+
let txHash;
|
|
338
|
+
try {
|
|
339
|
+
const amnt = this.sdk.tokensMeta.formatBN(pool.underlying, amount) + " " + this.sdk.tokensMeta.symbol(pool.underlying);
|
|
340
|
+
this.#logger?.debug(`depositing ${amnt} into pool ${poolName}`);
|
|
341
|
+
const allowance = await this.#anvil.readContract({
|
|
342
|
+
address: underlying,
|
|
343
|
+
abi: import_iERC20.ierc20Abi,
|
|
344
|
+
functionName: "balanceOf",
|
|
345
|
+
args: [depositor.address]
|
|
346
|
+
});
|
|
347
|
+
this.#logger?.debug(
|
|
348
|
+
`depositor balance in underlying: ${this.sdk.tokensMeta.formatBN(pool.underlying, allowance)}`
|
|
353
349
|
);
|
|
350
|
+
txHash = await this.#anvil.writeContract({
|
|
351
|
+
account: depositor,
|
|
352
|
+
address: underlying,
|
|
353
|
+
abi: import_iERC20.ierc20Abi,
|
|
354
|
+
functionName: "approve",
|
|
355
|
+
args: [address, allowance],
|
|
356
|
+
chain: this.#anvil.chain
|
|
357
|
+
});
|
|
358
|
+
let receipt = await this.#anvil.waitForTransactionReceipt({
|
|
359
|
+
hash: txHash
|
|
360
|
+
});
|
|
361
|
+
if (receipt.status === "reverted") {
|
|
362
|
+
throw new Error(
|
|
363
|
+
`tx ${txHash} that approves underlying from depositor for pool ${poolName} reverted`
|
|
364
|
+
);
|
|
365
|
+
}
|
|
366
|
+
this.#logger?.debug(
|
|
367
|
+
`depositor approved underlying for pool ${poolName}: ${txHash}`
|
|
368
|
+
);
|
|
369
|
+
txHash = await this.#anvil.writeContract({
|
|
370
|
+
account: depositor,
|
|
371
|
+
address,
|
|
372
|
+
abi: import_v300.iPoolV300Abi,
|
|
373
|
+
functionName: "deposit",
|
|
374
|
+
args: [amount, depositor.address],
|
|
375
|
+
chain: this.#anvil.chain
|
|
376
|
+
});
|
|
377
|
+
receipt = await this.#anvil.waitForTransactionReceipt({ hash: txHash });
|
|
378
|
+
if (receipt.status === "reverted") {
|
|
379
|
+
throw new Error(
|
|
380
|
+
`tx ${txHash} that deposits to pool ${poolName} reverted`
|
|
381
|
+
);
|
|
382
|
+
}
|
|
383
|
+
this.#logger?.debug(`deposited ${amnt} into ${poolName}`);
|
|
384
|
+
return {
|
|
385
|
+
...poolData,
|
|
386
|
+
txHash,
|
|
387
|
+
success: true
|
|
388
|
+
};
|
|
389
|
+
} catch (e) {
|
|
390
|
+
this.#logger?.error(`failed to deposit to pool ${poolName}: ${e}`);
|
|
391
|
+
return {
|
|
392
|
+
...poolData,
|
|
393
|
+
txHash,
|
|
394
|
+
success: false,
|
|
395
|
+
error: e
|
|
396
|
+
};
|
|
354
397
|
}
|
|
355
|
-
this.#logger?.debug(
|
|
356
|
-
`depositor approved underlying for pool ${poolName}: ${hash}`
|
|
357
|
-
);
|
|
358
|
-
hash = await this.#anvil.writeContract({
|
|
359
|
-
account: depositor,
|
|
360
|
-
address,
|
|
361
|
-
abi: import_v300.iPoolV300Abi,
|
|
362
|
-
functionName: "deposit",
|
|
363
|
-
args: [amount, depositor.address],
|
|
364
|
-
chain: this.#anvil.chain
|
|
365
|
-
});
|
|
366
|
-
receipt = await this.#anvil.waitForTransactionReceipt({ hash });
|
|
367
|
-
if (receipt.status === "reverted") {
|
|
368
|
-
throw new Error(`tx ${hash} that deposits to pool ${poolName} reverted`);
|
|
369
|
-
}
|
|
370
|
-
this.#logger?.debug(`deposited ${amnt} into ${poolName}`);
|
|
371
398
|
}
|
|
372
399
|
/**
|
|
373
400
|
* Creates borrower wallet,
|
|
@@ -35,6 +35,7 @@ class AccountOpener extends SDKConstruct {
|
|
|
35
35
|
#anvil;
|
|
36
36
|
#logger;
|
|
37
37
|
#borrower;
|
|
38
|
+
#depositor;
|
|
38
39
|
#faucet;
|
|
39
40
|
#poolDepositMultiplier;
|
|
40
41
|
#minDebtMultiplier;
|
|
@@ -52,6 +53,7 @@ class AccountOpener extends SDKConstruct {
|
|
|
52
53
|
this.#logger?.warn("faucet not found, will not claim from faucet");
|
|
53
54
|
}
|
|
54
55
|
this.#borrower = options.borrower;
|
|
56
|
+
this.#depositor = options.depositor;
|
|
55
57
|
this.#poolDepositMultiplier = options.poolDepositMultiplier ?? 11000n;
|
|
56
58
|
this.#minDebtMultiplier = options.minDebtMultiplier ?? 10100n;
|
|
57
59
|
}
|
|
@@ -65,9 +67,10 @@ class AccountOpener extends SDKConstruct {
|
|
|
65
67
|
* Tries to open account with underlying only in each CM
|
|
66
68
|
*/
|
|
67
69
|
async openCreditAccounts(targets, depositIntoPools = true, claimFromFaucet2 = true) {
|
|
70
|
+
let deposits = [];
|
|
68
71
|
if (depositIntoPools) {
|
|
69
72
|
try {
|
|
70
|
-
await this.#depositIntoPools(targets);
|
|
73
|
+
deposits = await this.#depositIntoPools(targets);
|
|
71
74
|
} catch (e) {
|
|
72
75
|
this.#logger?.warn(`failed to deposit into pools: ${e}`);
|
|
73
76
|
}
|
|
@@ -88,13 +91,13 @@ class AccountOpener extends SDKConstruct {
|
|
|
88
91
|
await this.#approve(token, cm);
|
|
89
92
|
}
|
|
90
93
|
}
|
|
91
|
-
const
|
|
94
|
+
const accounts = [];
|
|
92
95
|
let success = 0;
|
|
93
96
|
for (let i = 0; i < targets.length; i++) {
|
|
94
97
|
const target = targets[i];
|
|
95
98
|
try {
|
|
96
99
|
const result = await this.#openAccount(target, i + 1, targets.length);
|
|
97
|
-
|
|
100
|
+
accounts.push(result);
|
|
98
101
|
success += result.account ? 1 : 0;
|
|
99
102
|
if (result.error) {
|
|
100
103
|
this.#logger?.error(
|
|
@@ -105,7 +108,7 @@ class AccountOpener extends SDKConstruct {
|
|
|
105
108
|
this.#logger?.error(
|
|
106
109
|
`failed to open account #${i + 1}/${targets.length}: ${e}`
|
|
107
110
|
);
|
|
108
|
-
|
|
111
|
+
accounts.push({
|
|
109
112
|
input: target,
|
|
110
113
|
error: e
|
|
111
114
|
});
|
|
@@ -113,12 +116,12 @@ class AccountOpener extends SDKConstruct {
|
|
|
113
116
|
}
|
|
114
117
|
this.#logger?.info(`opened ${success}/${targets.length} accounts`);
|
|
115
118
|
try {
|
|
116
|
-
await this.#directlyDistributeTokens(
|
|
119
|
+
await this.#directlyDistributeTokens(accounts);
|
|
117
120
|
this.#logger?.info("distributed direct transfer tokens");
|
|
118
121
|
} catch (e) {
|
|
119
122
|
this.#logger?.error(`failed to distribute tokens: ${e}`);
|
|
120
123
|
}
|
|
121
|
-
return
|
|
124
|
+
return { deposits, accounts };
|
|
122
125
|
}
|
|
123
126
|
async #openAccount(input, index, total) {
|
|
124
127
|
const { creditManager, target: collateral } = input;
|
|
@@ -305,61 +308,85 @@ class AccountOpener extends SDKConstruct {
|
|
|
305
308
|
this.#logger?.debug(
|
|
306
309
|
`total USD to claim from faucet: ${formatBN(totalUSD, 8)}`
|
|
307
310
|
);
|
|
308
|
-
const depositor = await this.#createAccount();
|
|
311
|
+
const depositor = this.#depositor ?? await this.#createAccount();
|
|
309
312
|
this.#logger?.debug(`created depositor ${depositor.address}`);
|
|
310
313
|
await this.#claimFromFaucet(depositor, "depositor", totalUSD);
|
|
314
|
+
const results = [];
|
|
311
315
|
for (const [pool, amount] of deposits) {
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
} catch (e) {
|
|
315
|
-
this.#logger?.warn(`failed to deposit into ${pool.name}: ${e}`);
|
|
316
|
-
}
|
|
316
|
+
const result = await this.#depositToPool(pool, depositor, amount);
|
|
317
|
+
results.push(result);
|
|
317
318
|
}
|
|
319
|
+
return results;
|
|
318
320
|
}
|
|
319
321
|
async #depositToPool(pool, depositor, amount) {
|
|
320
322
|
const { underlying, address } = pool;
|
|
321
323
|
const poolName = this.sdk.provider.addressLabels.get(address);
|
|
322
|
-
const
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
chain: this.#anvil.chain
|
|
340
|
-
});
|
|
341
|
-
let receipt = await this.#anvil.waitForTransactionReceipt({ hash });
|
|
342
|
-
if (receipt.status === "reverted") {
|
|
343
|
-
throw new Error(
|
|
344
|
-
`tx ${hash} that approves underlying from depositor for pool ${poolName} reverted`
|
|
324
|
+
const poolData = {
|
|
325
|
+
pool: address,
|
|
326
|
+
token: underlying,
|
|
327
|
+
amount
|
|
328
|
+
};
|
|
329
|
+
let txHash;
|
|
330
|
+
try {
|
|
331
|
+
const amnt = this.sdk.tokensMeta.formatBN(pool.underlying, amount) + " " + this.sdk.tokensMeta.symbol(pool.underlying);
|
|
332
|
+
this.#logger?.debug(`depositing ${amnt} into pool ${poolName}`);
|
|
333
|
+
const allowance = await this.#anvil.readContract({
|
|
334
|
+
address: underlying,
|
|
335
|
+
abi: ierc20Abi,
|
|
336
|
+
functionName: "balanceOf",
|
|
337
|
+
args: [depositor.address]
|
|
338
|
+
});
|
|
339
|
+
this.#logger?.debug(
|
|
340
|
+
`depositor balance in underlying: ${this.sdk.tokensMeta.formatBN(pool.underlying, allowance)}`
|
|
345
341
|
);
|
|
342
|
+
txHash = await this.#anvil.writeContract({
|
|
343
|
+
account: depositor,
|
|
344
|
+
address: underlying,
|
|
345
|
+
abi: ierc20Abi,
|
|
346
|
+
functionName: "approve",
|
|
347
|
+
args: [address, allowance],
|
|
348
|
+
chain: this.#anvil.chain
|
|
349
|
+
});
|
|
350
|
+
let receipt = await this.#anvil.waitForTransactionReceipt({
|
|
351
|
+
hash: txHash
|
|
352
|
+
});
|
|
353
|
+
if (receipt.status === "reverted") {
|
|
354
|
+
throw new Error(
|
|
355
|
+
`tx ${txHash} that approves underlying from depositor for pool ${poolName} reverted`
|
|
356
|
+
);
|
|
357
|
+
}
|
|
358
|
+
this.#logger?.debug(
|
|
359
|
+
`depositor approved underlying for pool ${poolName}: ${txHash}`
|
|
360
|
+
);
|
|
361
|
+
txHash = await this.#anvil.writeContract({
|
|
362
|
+
account: depositor,
|
|
363
|
+
address,
|
|
364
|
+
abi: iPoolV300Abi,
|
|
365
|
+
functionName: "deposit",
|
|
366
|
+
args: [amount, depositor.address],
|
|
367
|
+
chain: this.#anvil.chain
|
|
368
|
+
});
|
|
369
|
+
receipt = await this.#anvil.waitForTransactionReceipt({ hash: txHash });
|
|
370
|
+
if (receipt.status === "reverted") {
|
|
371
|
+
throw new Error(
|
|
372
|
+
`tx ${txHash} that deposits to pool ${poolName} reverted`
|
|
373
|
+
);
|
|
374
|
+
}
|
|
375
|
+
this.#logger?.debug(`deposited ${amnt} into ${poolName}`);
|
|
376
|
+
return {
|
|
377
|
+
...poolData,
|
|
378
|
+
txHash,
|
|
379
|
+
success: true
|
|
380
|
+
};
|
|
381
|
+
} catch (e) {
|
|
382
|
+
this.#logger?.error(`failed to deposit to pool ${poolName}: ${e}`);
|
|
383
|
+
return {
|
|
384
|
+
...poolData,
|
|
385
|
+
txHash,
|
|
386
|
+
success: false,
|
|
387
|
+
error: e
|
|
388
|
+
};
|
|
346
389
|
}
|
|
347
|
-
this.#logger?.debug(
|
|
348
|
-
`depositor approved underlying for pool ${poolName}: ${hash}`
|
|
349
|
-
);
|
|
350
|
-
hash = await this.#anvil.writeContract({
|
|
351
|
-
account: depositor,
|
|
352
|
-
address,
|
|
353
|
-
abi: iPoolV300Abi,
|
|
354
|
-
functionName: "deposit",
|
|
355
|
-
args: [amount, depositor.address],
|
|
356
|
-
chain: this.#anvil.chain
|
|
357
|
-
});
|
|
358
|
-
receipt = await this.#anvil.waitForTransactionReceipt({ hash });
|
|
359
|
-
if (receipt.status === "reverted") {
|
|
360
|
-
throw new Error(`tx ${hash} that deposits to pool ${poolName} reverted`);
|
|
361
|
-
}
|
|
362
|
-
this.#logger?.debug(`deposited ${amnt} into ${poolName}`);
|
|
363
390
|
}
|
|
364
391
|
/**
|
|
365
392
|
* Creates borrower wallet,
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Address, Hash, PrivateKeyAccount } from "viem";
|
|
1
|
+
import type { Address, Hash, Hex, PrivateKeyAccount } from "viem";
|
|
2
2
|
import { BaseError } from "viem";
|
|
3
3
|
import type { CreditAccountData, ICreditAccountsService, RawTx } from "../sdk/index.js";
|
|
4
4
|
import { SDKConstruct } from "../sdk/index.js";
|
|
@@ -9,6 +9,7 @@ export declare class OpenTxRevertedError extends BaseError {
|
|
|
9
9
|
export interface AccountOpenerOptions {
|
|
10
10
|
faucet?: Address;
|
|
11
11
|
borrower?: PrivateKeyAccount;
|
|
12
|
+
depositor?: PrivateKeyAccount;
|
|
12
13
|
poolDepositMultiplier?: bigint;
|
|
13
14
|
minDebtMultiplier?: bigint;
|
|
14
15
|
}
|
|
@@ -40,6 +41,22 @@ export interface OpenAccountResult {
|
|
|
40
41
|
rawTx?: RawTx;
|
|
41
42
|
account?: CreditAccountData;
|
|
42
43
|
}
|
|
44
|
+
export type PoolDepositResult = {
|
|
45
|
+
pool: Address;
|
|
46
|
+
token: Address;
|
|
47
|
+
amount: bigint;
|
|
48
|
+
} & ({
|
|
49
|
+
success: true;
|
|
50
|
+
txHash: Hex;
|
|
51
|
+
} | {
|
|
52
|
+
success: false;
|
|
53
|
+
error: Error;
|
|
54
|
+
txHash?: Hex;
|
|
55
|
+
});
|
|
56
|
+
export interface OpenAccountsResult {
|
|
57
|
+
deposits: PoolDepositResult[];
|
|
58
|
+
accounts: OpenAccountResult[];
|
|
59
|
+
}
|
|
43
60
|
export declare class AccountOpener extends SDKConstruct {
|
|
44
61
|
#private;
|
|
45
62
|
constructor(service: ICreditAccountsService, options?: AccountOpenerOptions);
|
|
@@ -47,7 +64,7 @@ export declare class AccountOpener extends SDKConstruct {
|
|
|
47
64
|
/**
|
|
48
65
|
* Tries to open account with underlying only in each CM
|
|
49
66
|
*/
|
|
50
|
-
openCreditAccounts(targets: TargetAccount[], depositIntoPools?: boolean, claimFromFaucet?: boolean): Promise<
|
|
67
|
+
openCreditAccounts(targets: TargetAccount[], depositIntoPools?: boolean, claimFromFaucet?: boolean): Promise<OpenAccountsResult>;
|
|
51
68
|
prepareOpen(input: TargetAccount): Promise<RawTx>;
|
|
52
69
|
getOpenedAccounts(): Promise<CreditAccountData[]>;
|
|
53
70
|
get faucet(): Address;
|