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