@gearbox-protocol/sdk 8.19.2 → 8.19.4
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.
|
@@ -39,6 +39,8 @@ class OpenTxRevertedError extends import_viem.BaseError {
|
|
|
39
39
|
}
|
|
40
40
|
}
|
|
41
41
|
class AccountOpener extends import_sdk.SDKConstruct {
|
|
42
|
+
borrowerKey;
|
|
43
|
+
depositorKey;
|
|
42
44
|
#service;
|
|
43
45
|
#anvil;
|
|
44
46
|
#logger;
|
|
@@ -47,8 +49,15 @@ class AccountOpener extends import_sdk.SDKConstruct {
|
|
|
47
49
|
#faucet;
|
|
48
50
|
#poolDepositMultiplier;
|
|
49
51
|
#minDebtMultiplier;
|
|
50
|
-
constructor(service,
|
|
52
|
+
constructor(service, options_ = {}) {
|
|
51
53
|
super(service.sdk);
|
|
54
|
+
const {
|
|
55
|
+
borrowerKey,
|
|
56
|
+
depositorKey,
|
|
57
|
+
faucet,
|
|
58
|
+
poolDepositMultiplier = 30000n,
|
|
59
|
+
minDebtMultiplier = 10100n
|
|
60
|
+
} = options_;
|
|
52
61
|
this.#service = service;
|
|
53
62
|
this.#logger = (0, import_sdk.childLogger)("AccountOpener", service.sdk.logger);
|
|
54
63
|
this.#anvil = (0, import_createAnvilClient.createAnvilClient)({
|
|
@@ -56,20 +65,26 @@ class AccountOpener extends import_sdk.SDKConstruct {
|
|
|
56
65
|
transport: service.sdk.provider.transport
|
|
57
66
|
});
|
|
58
67
|
try {
|
|
59
|
-
this.#faucet =
|
|
68
|
+
this.#faucet = faucet ?? service.sdk.addressProvider.getAddress("FAUCET");
|
|
60
69
|
} catch (_e) {
|
|
61
70
|
this.#logger?.warn("faucet not found, will not claim from faucet");
|
|
62
71
|
}
|
|
63
|
-
this
|
|
64
|
-
this
|
|
65
|
-
this.#poolDepositMultiplier =
|
|
66
|
-
this.#minDebtMultiplier =
|
|
72
|
+
this.borrowerKey = borrowerKey ?? (0, import_accounts.generatePrivateKey)();
|
|
73
|
+
this.depositorKey = depositorKey ?? (0, import_accounts.generatePrivateKey)();
|
|
74
|
+
this.#poolDepositMultiplier = BigInt(poolDepositMultiplier);
|
|
75
|
+
this.#minDebtMultiplier = BigInt(minDebtMultiplier);
|
|
67
76
|
}
|
|
68
77
|
get borrower() {
|
|
69
78
|
if (!this.#borrower) {
|
|
70
79
|
throw new Error("borrower can be used only after openCreditAccounts");
|
|
71
80
|
}
|
|
72
|
-
return this.#borrower
|
|
81
|
+
return this.#borrower;
|
|
82
|
+
}
|
|
83
|
+
get depositor() {
|
|
84
|
+
if (!this.#depositor) {
|
|
85
|
+
throw new Error("depositor can be used only after openCreditAccounts");
|
|
86
|
+
}
|
|
87
|
+
return this.#depositor;
|
|
73
88
|
}
|
|
74
89
|
/**
|
|
75
90
|
* Tries to open account with underlying only in each CM
|
|
@@ -280,7 +295,7 @@ class AccountOpener extends import_sdk.SDKConstruct {
|
|
|
280
295
|
}
|
|
281
296
|
async getOpenedAccounts() {
|
|
282
297
|
return await this.#service.getCreditAccounts({
|
|
283
|
-
owner: this.borrower
|
|
298
|
+
owner: this.borrower.address
|
|
284
299
|
});
|
|
285
300
|
}
|
|
286
301
|
async #depositIntoPools(targets) {
|
|
@@ -291,7 +306,8 @@ class AccountOpener extends import_sdk.SDKConstruct {
|
|
|
291
306
|
const leverage = this.#getLeverage(t);
|
|
292
307
|
const cm = this.sdk.marketRegister.findCreditManager(t.creditManager);
|
|
293
308
|
const minDebt = this.#minDebtMultiplier * cm.creditFacade.minDebt / import_sdk.PERCENTAGE_FACTOR;
|
|
294
|
-
|
|
309
|
+
let amount = minDebt * (leverage - import_sdk.PERCENTAGE_FACTOR) / import_sdk.PERCENTAGE_FACTOR * this.#poolDepositMultiplier / import_sdk.PERCENTAGE_FACTOR;
|
|
310
|
+
amount = amount > cm.creditFacade.maxDebt ? cm.creditFacade.maxDebt : amount;
|
|
295
311
|
minAvailableByPool[cm.pool] = (minAvailableByPool[cm.pool] ?? 0n) + amount;
|
|
296
312
|
this.#logger?.debug(
|
|
297
313
|
`target #${i + 1} (${this.labelAddress(t.target)}) needs ${this.sdk.tokensMeta.formatBN(cm.underlying, amount)} ${this.sdk.tokensMeta.symbol(cm.underlying)} in pool (leverage: ${Number(leverage / import_sdk.PERCENTAGE_FACTOR)}%)`
|
|
@@ -321,8 +337,8 @@ class AccountOpener extends import_sdk.SDKConstruct {
|
|
|
321
337
|
this.#logger?.debug(
|
|
322
338
|
`total USD to claim from faucet: ${(0, import_sdk.formatBN)(totalUSD, 8)}`
|
|
323
339
|
);
|
|
324
|
-
const depositor =
|
|
325
|
-
this.#logger?.debug(`
|
|
340
|
+
const depositor = await this.#getDepositor();
|
|
341
|
+
this.#logger?.debug(`depositor: ${depositor.address}`);
|
|
326
342
|
await this.#claimFromFaucet(depositor, "depositor", totalUSD);
|
|
327
343
|
const results = [];
|
|
328
344
|
for (const [pool, amount] of deposits) {
|
|
@@ -537,13 +553,20 @@ class AccountOpener extends import_sdk.SDKConstruct {
|
|
|
537
553
|
}
|
|
538
554
|
async #getBorrower() {
|
|
539
555
|
if (!this.#borrower) {
|
|
540
|
-
this.#borrower = await this.#createAccount();
|
|
556
|
+
this.#borrower = await this.#createAccount(this.borrowerKey);
|
|
541
557
|
this.#logger?.info(`created borrower ${this.#borrower.address}`);
|
|
542
558
|
}
|
|
543
|
-
return this
|
|
559
|
+
return this.borrower;
|
|
560
|
+
}
|
|
561
|
+
async #getDepositor() {
|
|
562
|
+
if (!this.#depositor) {
|
|
563
|
+
this.#depositor = await this.#createAccount(this.depositorKey);
|
|
564
|
+
this.#logger?.info(`created depositor ${this.#depositor.address}`);
|
|
565
|
+
}
|
|
566
|
+
return this.depositor;
|
|
544
567
|
}
|
|
545
|
-
async #createAccount() {
|
|
546
|
-
const acc = (0, import_accounts.privateKeyToAccount)(
|
|
568
|
+
async #createAccount(privateKey) {
|
|
569
|
+
const acc = (0, import_accounts.privateKeyToAccount)(privateKey);
|
|
547
570
|
await this.#anvil.setBalance({
|
|
548
571
|
address: acc.address,
|
|
549
572
|
value: (0, import_viem.parseEther)("100")
|
|
@@ -699,7 +722,8 @@ class AccountOpener extends import_sdk.SDKConstruct {
|
|
|
699
722
|
const cm = this.sdk.marketRegister.findCreditManager(creditManager);
|
|
700
723
|
const lt = BigInt(cm.creditManager.liquidationThresholds.mustGet(target));
|
|
701
724
|
const d = 50n;
|
|
702
|
-
|
|
725
|
+
const result = import_sdk.PERCENTAGE_FACTOR * (1n + (lt - d) / (import_sdk.PERCENTAGE_FACTOR - lt));
|
|
726
|
+
return result > import_sdk.PERCENTAGE_FACTOR * 10n ? import_sdk.PERCENTAGE_FACTOR * 10n : result;
|
|
703
727
|
}
|
|
704
728
|
get faucet() {
|
|
705
729
|
if (!this.#faucet) {
|
|
@@ -31,6 +31,8 @@ class OpenTxRevertedError extends BaseError {
|
|
|
31
31
|
}
|
|
32
32
|
}
|
|
33
33
|
class AccountOpener extends SDKConstruct {
|
|
34
|
+
borrowerKey;
|
|
35
|
+
depositorKey;
|
|
34
36
|
#service;
|
|
35
37
|
#anvil;
|
|
36
38
|
#logger;
|
|
@@ -39,8 +41,15 @@ class AccountOpener extends SDKConstruct {
|
|
|
39
41
|
#faucet;
|
|
40
42
|
#poolDepositMultiplier;
|
|
41
43
|
#minDebtMultiplier;
|
|
42
|
-
constructor(service,
|
|
44
|
+
constructor(service, options_ = {}) {
|
|
43
45
|
super(service.sdk);
|
|
46
|
+
const {
|
|
47
|
+
borrowerKey,
|
|
48
|
+
depositorKey,
|
|
49
|
+
faucet,
|
|
50
|
+
poolDepositMultiplier = 30000n,
|
|
51
|
+
minDebtMultiplier = 10100n
|
|
52
|
+
} = options_;
|
|
44
53
|
this.#service = service;
|
|
45
54
|
this.#logger = childLogger("AccountOpener", service.sdk.logger);
|
|
46
55
|
this.#anvil = createAnvilClient({
|
|
@@ -48,20 +57,26 @@ class AccountOpener extends SDKConstruct {
|
|
|
48
57
|
transport: service.sdk.provider.transport
|
|
49
58
|
});
|
|
50
59
|
try {
|
|
51
|
-
this.#faucet =
|
|
60
|
+
this.#faucet = faucet ?? service.sdk.addressProvider.getAddress("FAUCET");
|
|
52
61
|
} catch (_e) {
|
|
53
62
|
this.#logger?.warn("faucet not found, will not claim from faucet");
|
|
54
63
|
}
|
|
55
|
-
this
|
|
56
|
-
this
|
|
57
|
-
this.#poolDepositMultiplier =
|
|
58
|
-
this.#minDebtMultiplier =
|
|
64
|
+
this.borrowerKey = borrowerKey ?? generatePrivateKey();
|
|
65
|
+
this.depositorKey = depositorKey ?? generatePrivateKey();
|
|
66
|
+
this.#poolDepositMultiplier = BigInt(poolDepositMultiplier);
|
|
67
|
+
this.#minDebtMultiplier = BigInt(minDebtMultiplier);
|
|
59
68
|
}
|
|
60
69
|
get borrower() {
|
|
61
70
|
if (!this.#borrower) {
|
|
62
71
|
throw new Error("borrower can be used only after openCreditAccounts");
|
|
63
72
|
}
|
|
64
|
-
return this.#borrower
|
|
73
|
+
return this.#borrower;
|
|
74
|
+
}
|
|
75
|
+
get depositor() {
|
|
76
|
+
if (!this.#depositor) {
|
|
77
|
+
throw new Error("depositor can be used only after openCreditAccounts");
|
|
78
|
+
}
|
|
79
|
+
return this.#depositor;
|
|
65
80
|
}
|
|
66
81
|
/**
|
|
67
82
|
* Tries to open account with underlying only in each CM
|
|
@@ -272,7 +287,7 @@ class AccountOpener extends SDKConstruct {
|
|
|
272
287
|
}
|
|
273
288
|
async getOpenedAccounts() {
|
|
274
289
|
return await this.#service.getCreditAccounts({
|
|
275
|
-
owner: this.borrower
|
|
290
|
+
owner: this.borrower.address
|
|
276
291
|
});
|
|
277
292
|
}
|
|
278
293
|
async #depositIntoPools(targets) {
|
|
@@ -283,7 +298,8 @@ class AccountOpener extends SDKConstruct {
|
|
|
283
298
|
const leverage = this.#getLeverage(t);
|
|
284
299
|
const cm = this.sdk.marketRegister.findCreditManager(t.creditManager);
|
|
285
300
|
const minDebt = this.#minDebtMultiplier * cm.creditFacade.minDebt / PERCENTAGE_FACTOR;
|
|
286
|
-
|
|
301
|
+
let amount = minDebt * (leverage - PERCENTAGE_FACTOR) / PERCENTAGE_FACTOR * this.#poolDepositMultiplier / PERCENTAGE_FACTOR;
|
|
302
|
+
amount = amount > cm.creditFacade.maxDebt ? cm.creditFacade.maxDebt : amount;
|
|
287
303
|
minAvailableByPool[cm.pool] = (minAvailableByPool[cm.pool] ?? 0n) + amount;
|
|
288
304
|
this.#logger?.debug(
|
|
289
305
|
`target #${i + 1} (${this.labelAddress(t.target)}) needs ${this.sdk.tokensMeta.formatBN(cm.underlying, amount)} ${this.sdk.tokensMeta.symbol(cm.underlying)} in pool (leverage: ${Number(leverage / PERCENTAGE_FACTOR)}%)`
|
|
@@ -313,8 +329,8 @@ class AccountOpener extends SDKConstruct {
|
|
|
313
329
|
this.#logger?.debug(
|
|
314
330
|
`total USD to claim from faucet: ${formatBN(totalUSD, 8)}`
|
|
315
331
|
);
|
|
316
|
-
const depositor =
|
|
317
|
-
this.#logger?.debug(`
|
|
332
|
+
const depositor = await this.#getDepositor();
|
|
333
|
+
this.#logger?.debug(`depositor: ${depositor.address}`);
|
|
318
334
|
await this.#claimFromFaucet(depositor, "depositor", totalUSD);
|
|
319
335
|
const results = [];
|
|
320
336
|
for (const [pool, amount] of deposits) {
|
|
@@ -529,13 +545,20 @@ class AccountOpener extends SDKConstruct {
|
|
|
529
545
|
}
|
|
530
546
|
async #getBorrower() {
|
|
531
547
|
if (!this.#borrower) {
|
|
532
|
-
this.#borrower = await this.#createAccount();
|
|
548
|
+
this.#borrower = await this.#createAccount(this.borrowerKey);
|
|
533
549
|
this.#logger?.info(`created borrower ${this.#borrower.address}`);
|
|
534
550
|
}
|
|
535
|
-
return this
|
|
551
|
+
return this.borrower;
|
|
552
|
+
}
|
|
553
|
+
async #getDepositor() {
|
|
554
|
+
if (!this.#depositor) {
|
|
555
|
+
this.#depositor = await this.#createAccount(this.depositorKey);
|
|
556
|
+
this.#logger?.info(`created depositor ${this.#depositor.address}`);
|
|
557
|
+
}
|
|
558
|
+
return this.depositor;
|
|
536
559
|
}
|
|
537
|
-
async #createAccount() {
|
|
538
|
-
const acc = privateKeyToAccount(
|
|
560
|
+
async #createAccount(privateKey) {
|
|
561
|
+
const acc = privateKeyToAccount(privateKey);
|
|
539
562
|
await this.#anvil.setBalance({
|
|
540
563
|
address: acc.address,
|
|
541
564
|
value: parseEther("100")
|
|
@@ -691,7 +714,8 @@ class AccountOpener extends SDKConstruct {
|
|
|
691
714
|
const cm = this.sdk.marketRegister.findCreditManager(creditManager);
|
|
692
715
|
const lt = BigInt(cm.creditManager.liquidationThresholds.mustGet(target));
|
|
693
716
|
const d = 50n;
|
|
694
|
-
|
|
717
|
+
const result = PERCENTAGE_FACTOR * (1n + (lt - d) / (PERCENTAGE_FACTOR - lt));
|
|
718
|
+
return result > PERCENTAGE_FACTOR * 10n ? PERCENTAGE_FACTOR * 10n : result;
|
|
695
719
|
}
|
|
696
720
|
get faucet() {
|
|
697
721
|
if (!this.#faucet) {
|
|
@@ -8,10 +8,10 @@ export declare class OpenTxRevertedError extends BaseError {
|
|
|
8
8
|
}
|
|
9
9
|
export interface AccountOpenerOptions {
|
|
10
10
|
faucet?: Address;
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
poolDepositMultiplier?: bigint;
|
|
14
|
-
minDebtMultiplier?: bigint;
|
|
11
|
+
borrowerKey?: Hex;
|
|
12
|
+
depositorKey?: Hex;
|
|
13
|
+
poolDepositMultiplier?: bigint | string | number;
|
|
14
|
+
minDebtMultiplier?: bigint | string | number;
|
|
15
15
|
}
|
|
16
16
|
export interface TargetAccount {
|
|
17
17
|
creditManager: Address;
|
|
@@ -59,8 +59,11 @@ export interface OpenAccountsResult {
|
|
|
59
59
|
}
|
|
60
60
|
export declare class AccountOpener extends SDKConstruct {
|
|
61
61
|
#private;
|
|
62
|
-
|
|
63
|
-
|
|
62
|
+
readonly borrowerKey: Hex;
|
|
63
|
+
readonly depositorKey: Hex;
|
|
64
|
+
constructor(service: ICreditAccountsService, options_?: AccountOpenerOptions);
|
|
65
|
+
get borrower(): PrivateKeyAccount;
|
|
66
|
+
get depositor(): PrivateKeyAccount;
|
|
64
67
|
/**
|
|
65
68
|
* Tries to open account with underlying only in each CM
|
|
66
69
|
*/
|