@gearbox-protocol/sdk 9.9.4 → 9.9.6
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/dev/AccountOpener.js +85 -28
- package/dist/esm/dev/AccountOpener.js +85 -28
- package/package.json +1 -1
|
@@ -257,11 +257,14 @@ class AccountOpener extends import_sdk.SDKConstruct {
|
|
|
257
257
|
}
|
|
258
258
|
logger?.debug(
|
|
259
259
|
{
|
|
260
|
-
minDebt: this.sdk.tokensMeta.formatBN(underlying, minDebt
|
|
260
|
+
minDebt: this.sdk.tokensMeta.formatBN(underlying, minDebt, {
|
|
261
|
+
symbol: true
|
|
262
|
+
}),
|
|
261
263
|
leverage: leverage.toString(),
|
|
262
264
|
expectedUnderlyingBalance: this.sdk.tokensMeta.formatBN(
|
|
263
265
|
underlying,
|
|
264
|
-
expectedUnderlyingBalance
|
|
266
|
+
expectedUnderlyingBalance,
|
|
267
|
+
{ symbol: true }
|
|
265
268
|
)
|
|
266
269
|
},
|
|
267
270
|
"looking for open strategy"
|
|
@@ -274,7 +277,7 @@ class AccountOpener extends import_sdk.SDKConstruct {
|
|
|
274
277
|
});
|
|
275
278
|
if (borrowerBalance < minDebt) {
|
|
276
279
|
this.#logger?.warn(
|
|
277
|
-
`borrower balance in underlying: ${this.sdk.tokensMeta.formatBN(underlying, borrowerBalance)} is less than the minimum debt: ${this.sdk.tokensMeta.formatBN(underlying, minDebt)}`
|
|
280
|
+
`borrower balance in underlying: ${this.sdk.tokensMeta.formatBN(underlying, borrowerBalance, { symbol: true })} is less than the minimum debt: ${this.sdk.tokensMeta.formatBN(underlying, minDebt, { symbol: true })}`
|
|
278
281
|
);
|
|
279
282
|
if (this.#allowMint) {
|
|
280
283
|
borrowerBalance = await this.#tryMint(
|
|
@@ -285,7 +288,7 @@ class AccountOpener extends import_sdk.SDKConstruct {
|
|
|
285
288
|
}
|
|
286
289
|
}
|
|
287
290
|
this.#logger?.debug(
|
|
288
|
-
`borrower balance in underlying: ${this.sdk.tokensMeta.formatBN(underlying, borrowerBalance
|
|
291
|
+
`borrower balance in underlying: ${this.sdk.tokensMeta.formatBN(underlying, borrowerBalance, { symbol: true })}`
|
|
289
292
|
);
|
|
290
293
|
const strategy = await this.sdk.routerFor(cm).findOpenStrategyPath({
|
|
291
294
|
creditManager: cm.creditManager,
|
|
@@ -360,7 +363,7 @@ class AccountOpener extends import_sdk.SDKConstruct {
|
|
|
360
363
|
amount = amount > cm.creditFacade.maxDebt ? cm.creditFacade.maxDebt : amount;
|
|
361
364
|
minAvailableByPool[cm.pool] = (minAvailableByPool[cm.pool] ?? 0n) + amount;
|
|
362
365
|
this.#logger?.debug(
|
|
363
|
-
`target #${i + 1} (${this.labelAddress(t.target)}) needs ${this.sdk.tokensMeta.formatBN(cm.underlying, amount
|
|
366
|
+
`target #${i + 1} (${this.labelAddress(t.target)}) needs ${this.sdk.tokensMeta.formatBN(cm.underlying, amount, { symbol: true })} in pool (leverage: ${Number(leverage / import_sdk.PERCENTAGE_FACTOR)}%)`
|
|
364
367
|
);
|
|
365
368
|
}
|
|
366
369
|
let totalUSD = 0n;
|
|
@@ -374,9 +377,11 @@ class AccountOpener extends import_sdk.SDKConstruct {
|
|
|
374
377
|
minAvailable,
|
|
375
378
|
pool.availableLiquidity,
|
|
376
379
|
diff
|
|
377
|
-
].map(
|
|
380
|
+
].map(
|
|
381
|
+
(v) => this.sdk.tokensMeta.formatBN(pool.underlying, v, { symbol: true })
|
|
382
|
+
);
|
|
378
383
|
this.#logger?.debug(
|
|
379
|
-
`Pool ${this.labelAddress(pool.address)} has ${availableS} liquidity, needs ${diffS} more for the minimum of ${minS}
|
|
384
|
+
`Pool ${this.labelAddress(pool.address)} has ${availableS} liquidity, needs ${diffS} more for the minimum of ${minS}`
|
|
380
385
|
);
|
|
381
386
|
if (diff > 0n) {
|
|
382
387
|
deposits.push([pool, diff]);
|
|
@@ -389,7 +394,15 @@ class AccountOpener extends import_sdk.SDKConstruct {
|
|
|
389
394
|
);
|
|
390
395
|
const depositor = await this.#getDepositor();
|
|
391
396
|
this.#logger?.debug(`depositor: ${depositor.address}`);
|
|
392
|
-
|
|
397
|
+
try {
|
|
398
|
+
await this.#claimFromFaucet(depositor, "depositor", totalUSD);
|
|
399
|
+
} catch (e) {
|
|
400
|
+
if (this.#allowMint) {
|
|
401
|
+
this.#logger?.error(`depositor failed to claim from faucet: ${e}`);
|
|
402
|
+
} else {
|
|
403
|
+
throw e;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
393
406
|
const results = [];
|
|
394
407
|
for (const [pool, amount] of deposits) {
|
|
395
408
|
const result = await this.#depositToPool(pool, depositor, amount);
|
|
@@ -407,7 +420,9 @@ class AccountOpener extends import_sdk.SDKConstruct {
|
|
|
407
420
|
};
|
|
408
421
|
let txHash;
|
|
409
422
|
try {
|
|
410
|
-
const amnt = this.sdk.tokensMeta.formatBN(pool.underlying, amount
|
|
423
|
+
const amnt = this.sdk.tokensMeta.formatBN(pool.underlying, amount, {
|
|
424
|
+
symbol: true
|
|
425
|
+
});
|
|
411
426
|
this.#logger?.debug(`depositing ${amnt} into pool ${poolName}`);
|
|
412
427
|
let allowance = await this.#anvil.readContract({
|
|
413
428
|
address: underlying,
|
|
@@ -417,7 +432,7 @@ class AccountOpener extends import_sdk.SDKConstruct {
|
|
|
417
432
|
});
|
|
418
433
|
if (allowance < amount) {
|
|
419
434
|
this.#logger?.warn(
|
|
420
|
-
`depositor balance in underlying: ${this.sdk.tokensMeta.formatBN(pool.underlying, allowance)} is less than the amount to deposit: ${amnt}`
|
|
435
|
+
`depositor balance in underlying: ${this.sdk.tokensMeta.formatBN(pool.underlying, allowance, { symbol: true })} is less than the amount to deposit: ${amnt}`
|
|
421
436
|
);
|
|
422
437
|
if (this.#allowMint) {
|
|
423
438
|
allowance = await this.#tryMint(
|
|
@@ -428,7 +443,7 @@ class AccountOpener extends import_sdk.SDKConstruct {
|
|
|
428
443
|
}
|
|
429
444
|
}
|
|
430
445
|
this.#logger?.debug(
|
|
431
|
-
`depositor balance in underlying: ${this.sdk.tokensMeta.formatBN(pool.underlying, allowance)}`
|
|
446
|
+
`depositor balance in underlying: ${this.sdk.tokensMeta.formatBN(pool.underlying, allowance, { symbol: true })}`
|
|
432
447
|
);
|
|
433
448
|
txHash = await this.#anvil.writeContract({
|
|
434
449
|
account: depositor,
|
|
@@ -486,14 +501,11 @@ class AccountOpener extends import_sdk.SDKConstruct {
|
|
|
486
501
|
* @param amount
|
|
487
502
|
*/
|
|
488
503
|
async #tryMint(token, dest, amount) {
|
|
489
|
-
const
|
|
490
|
-
const amnt = this.sdk.tokensMeta.formatBN(token, amount);
|
|
504
|
+
const amnt = this.sdk.tokensMeta.formatBN(token, amount, { symbol: true });
|
|
491
505
|
try {
|
|
492
506
|
await this.#mint(token, dest, amount);
|
|
493
507
|
} catch (e) {
|
|
494
|
-
this.#logger?.warn(
|
|
495
|
-
`failed to mint ${amnt} ${symbol} to ${dest.address}: ${e}`
|
|
496
|
-
);
|
|
508
|
+
this.#logger?.warn(`failed to mint ${amnt} to ${dest.address}: ${e}`);
|
|
497
509
|
}
|
|
498
510
|
return await this.#anvil.readContract({
|
|
499
511
|
address: token,
|
|
@@ -515,8 +527,46 @@ class AccountOpener extends import_sdk.SDKConstruct {
|
|
|
515
527
|
await this.#anvil.impersonateAccount({ address: owner });
|
|
516
528
|
await this.#anvil.setBalance({
|
|
517
529
|
address: owner,
|
|
518
|
-
value: (0, import_viem.parseEther)("
|
|
530
|
+
value: (0, import_viem.parseEther)("1000000")
|
|
519
531
|
});
|
|
532
|
+
try {
|
|
533
|
+
await this.#mintDirectly(owner, token, amount, dest.address);
|
|
534
|
+
} catch (e) {
|
|
535
|
+
this.#logger?.warn(
|
|
536
|
+
`failed to mint directly ${amnt} ${symbol} to ${dest.address}: ${e}`
|
|
537
|
+
);
|
|
538
|
+
try {
|
|
539
|
+
await this.#mintAndTransfer(owner, token, amount, dest.address);
|
|
540
|
+
} catch (e2) {
|
|
541
|
+
this.#logger?.warn(
|
|
542
|
+
`failed to mint and transfer ${amnt} ${symbol} to ${dest.address}: ${e2}`
|
|
543
|
+
);
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
await this.#anvil.stopImpersonatingAccount({ address: owner });
|
|
547
|
+
}
|
|
548
|
+
async #mintDirectly(owner, token, amount, dest) {
|
|
549
|
+
const hash = await this.#anvil.writeContract({
|
|
550
|
+
account: owner,
|
|
551
|
+
address: token,
|
|
552
|
+
abi: (0, import_viem.parseAbi)([
|
|
553
|
+
"function mint(address to, uint256 amount) returns (bool)"
|
|
554
|
+
]),
|
|
555
|
+
functionName: "mint",
|
|
556
|
+
args: [dest, amount],
|
|
557
|
+
chain: this.#anvil.chain
|
|
558
|
+
});
|
|
559
|
+
this.#logger?.debug(
|
|
560
|
+
`mint ${this.sdk.tokensMeta.formatBN(token, amount, { symbol: true })} to ${owner} in tx ${hash}`
|
|
561
|
+
);
|
|
562
|
+
const receipt = await this.#anvil.waitForTransactionReceipt({ hash });
|
|
563
|
+
if (receipt.status === "reverted") {
|
|
564
|
+
throw new Error(
|
|
565
|
+
`failed to mint ${this.sdk.tokensMeta.formatBN(token, amount, { symbol: true })} to ${owner} in tx ${hash}: reverted`
|
|
566
|
+
);
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
async #mintAndTransfer(owner, token, amount, dest) {
|
|
520
570
|
let hash = await this.#anvil.writeContract({
|
|
521
571
|
account: owner,
|
|
522
572
|
address: token,
|
|
@@ -525,11 +575,13 @@ class AccountOpener extends import_sdk.SDKConstruct {
|
|
|
525
575
|
args: [amount],
|
|
526
576
|
chain: this.#anvil.chain
|
|
527
577
|
});
|
|
528
|
-
this.#logger?.debug(
|
|
578
|
+
this.#logger?.debug(
|
|
579
|
+
`mint ${this.sdk.tokensMeta.formatBN(token, amount, { symbol: true })} to ${owner} in tx ${hash}`
|
|
580
|
+
);
|
|
529
581
|
let receipt = await this.#anvil.waitForTransactionReceipt({ hash });
|
|
530
582
|
if (receipt.status === "reverted") {
|
|
531
583
|
throw new Error(
|
|
532
|
-
`failed to mint ${
|
|
584
|
+
`failed to mint ${this.sdk.tokensMeta.formatBN(token, amount, { symbol: true })} to ${owner} in tx ${hash}: reverted`
|
|
533
585
|
);
|
|
534
586
|
}
|
|
535
587
|
hash = await this.#anvil.writeContract({
|
|
@@ -537,19 +589,18 @@ class AccountOpener extends import_sdk.SDKConstruct {
|
|
|
537
589
|
address: token,
|
|
538
590
|
abi: import_iERC20.ierc20Abi,
|
|
539
591
|
functionName: "transfer",
|
|
540
|
-
args: [dest
|
|
592
|
+
args: [dest, amount],
|
|
541
593
|
chain: this.#anvil.chain
|
|
542
594
|
});
|
|
543
595
|
this.#logger?.debug(
|
|
544
|
-
`transfer ${
|
|
596
|
+
`transfer ${this.sdk.tokensMeta.formatBN(token, amount, { symbol: true })} from ${owner} to ${dest} in tx ${hash}`
|
|
545
597
|
);
|
|
546
598
|
receipt = await this.#anvil.waitForTransactionReceipt({ hash });
|
|
547
599
|
if (receipt.status === "reverted") {
|
|
548
600
|
throw new Error(
|
|
549
|
-
`failed to transfer ${
|
|
601
|
+
`failed to transfer ${this.sdk.tokensMeta.formatBN(token, amount, { symbol: true })} from ${owner} to ${dest} in tx ${hash}: reverted`
|
|
550
602
|
);
|
|
551
603
|
}
|
|
552
|
-
await this.#anvil.stopImpersonatingAccount({ address: owner });
|
|
553
604
|
}
|
|
554
605
|
/**
|
|
555
606
|
* Creates borrower wallet,
|
|
@@ -816,17 +867,23 @@ class AccountOpener extends import_sdk.SDKConstruct {
|
|
|
816
867
|
const desiredQuota = this.#calcQuota(amount, debt, collateralLT);
|
|
817
868
|
logger?.debug(
|
|
818
869
|
{
|
|
819
|
-
desiredQuota: this.sdk.tokensMeta.formatBN(underlying, desiredQuota
|
|
870
|
+
desiredQuota: this.sdk.tokensMeta.formatBN(underlying, desiredQuota, {
|
|
871
|
+
symbol: true
|
|
872
|
+
}),
|
|
820
873
|
availableQuota: this.sdk.tokensMeta.formatBN(
|
|
821
874
|
underlying,
|
|
822
|
-
availableQuota
|
|
875
|
+
availableQuota,
|
|
876
|
+
{ symbol: true }
|
|
823
877
|
),
|
|
824
878
|
collateralAmount: this.sdk.tokensMeta.formatBN(
|
|
825
879
|
collateral,
|
|
826
|
-
collateralAmount
|
|
880
|
+
collateralAmount,
|
|
881
|
+
{ symbol: true }
|
|
827
882
|
),
|
|
828
|
-
amount: this.sdk.tokensMeta.formatBN(underlying, amount
|
|
829
|
-
|
|
883
|
+
amount: this.sdk.tokensMeta.formatBN(underlying, amount, {
|
|
884
|
+
symbol: true
|
|
885
|
+
}),
|
|
886
|
+
debt: this.sdk.tokensMeta.formatBN(underlying, debt, { symbol: true }),
|
|
830
887
|
lt: Number(collateralLT)
|
|
831
888
|
},
|
|
832
889
|
"calculated quota"
|
|
@@ -250,11 +250,14 @@ class AccountOpener extends SDKConstruct {
|
|
|
250
250
|
}
|
|
251
251
|
logger?.debug(
|
|
252
252
|
{
|
|
253
|
-
minDebt: this.sdk.tokensMeta.formatBN(underlying, minDebt
|
|
253
|
+
minDebt: this.sdk.tokensMeta.formatBN(underlying, minDebt, {
|
|
254
|
+
symbol: true
|
|
255
|
+
}),
|
|
254
256
|
leverage: leverage.toString(),
|
|
255
257
|
expectedUnderlyingBalance: this.sdk.tokensMeta.formatBN(
|
|
256
258
|
underlying,
|
|
257
|
-
expectedUnderlyingBalance
|
|
259
|
+
expectedUnderlyingBalance,
|
|
260
|
+
{ symbol: true }
|
|
258
261
|
)
|
|
259
262
|
},
|
|
260
263
|
"looking for open strategy"
|
|
@@ -267,7 +270,7 @@ class AccountOpener extends SDKConstruct {
|
|
|
267
270
|
});
|
|
268
271
|
if (borrowerBalance < minDebt) {
|
|
269
272
|
this.#logger?.warn(
|
|
270
|
-
`borrower balance in underlying: ${this.sdk.tokensMeta.formatBN(underlying, borrowerBalance)} is less than the minimum debt: ${this.sdk.tokensMeta.formatBN(underlying, minDebt)}`
|
|
273
|
+
`borrower balance in underlying: ${this.sdk.tokensMeta.formatBN(underlying, borrowerBalance, { symbol: true })} is less than the minimum debt: ${this.sdk.tokensMeta.formatBN(underlying, minDebt, { symbol: true })}`
|
|
271
274
|
);
|
|
272
275
|
if (this.#allowMint) {
|
|
273
276
|
borrowerBalance = await this.#tryMint(
|
|
@@ -278,7 +281,7 @@ class AccountOpener extends SDKConstruct {
|
|
|
278
281
|
}
|
|
279
282
|
}
|
|
280
283
|
this.#logger?.debug(
|
|
281
|
-
`borrower balance in underlying: ${this.sdk.tokensMeta.formatBN(underlying, borrowerBalance
|
|
284
|
+
`borrower balance in underlying: ${this.sdk.tokensMeta.formatBN(underlying, borrowerBalance, { symbol: true })}`
|
|
282
285
|
);
|
|
283
286
|
const strategy = await this.sdk.routerFor(cm).findOpenStrategyPath({
|
|
284
287
|
creditManager: cm.creditManager,
|
|
@@ -353,7 +356,7 @@ class AccountOpener extends SDKConstruct {
|
|
|
353
356
|
amount = amount > cm.creditFacade.maxDebt ? cm.creditFacade.maxDebt : amount;
|
|
354
357
|
minAvailableByPool[cm.pool] = (minAvailableByPool[cm.pool] ?? 0n) + amount;
|
|
355
358
|
this.#logger?.debug(
|
|
356
|
-
`target #${i + 1} (${this.labelAddress(t.target)}) needs ${this.sdk.tokensMeta.formatBN(cm.underlying, amount
|
|
359
|
+
`target #${i + 1} (${this.labelAddress(t.target)}) needs ${this.sdk.tokensMeta.formatBN(cm.underlying, amount, { symbol: true })} in pool (leverage: ${Number(leverage / PERCENTAGE_FACTOR)}%)`
|
|
357
360
|
);
|
|
358
361
|
}
|
|
359
362
|
let totalUSD = 0n;
|
|
@@ -367,9 +370,11 @@ class AccountOpener extends SDKConstruct {
|
|
|
367
370
|
minAvailable,
|
|
368
371
|
pool.availableLiquidity,
|
|
369
372
|
diff
|
|
370
|
-
].map(
|
|
373
|
+
].map(
|
|
374
|
+
(v) => this.sdk.tokensMeta.formatBN(pool.underlying, v, { symbol: true })
|
|
375
|
+
);
|
|
371
376
|
this.#logger?.debug(
|
|
372
|
-
`Pool ${this.labelAddress(pool.address)} has ${availableS} liquidity, needs ${diffS} more for the minimum of ${minS}
|
|
377
|
+
`Pool ${this.labelAddress(pool.address)} has ${availableS} liquidity, needs ${diffS} more for the minimum of ${minS}`
|
|
373
378
|
);
|
|
374
379
|
if (diff > 0n) {
|
|
375
380
|
deposits.push([pool, diff]);
|
|
@@ -382,7 +387,15 @@ class AccountOpener extends SDKConstruct {
|
|
|
382
387
|
);
|
|
383
388
|
const depositor = await this.#getDepositor();
|
|
384
389
|
this.#logger?.debug(`depositor: ${depositor.address}`);
|
|
385
|
-
|
|
390
|
+
try {
|
|
391
|
+
await this.#claimFromFaucet(depositor, "depositor", totalUSD);
|
|
392
|
+
} catch (e) {
|
|
393
|
+
if (this.#allowMint) {
|
|
394
|
+
this.#logger?.error(`depositor failed to claim from faucet: ${e}`);
|
|
395
|
+
} else {
|
|
396
|
+
throw e;
|
|
397
|
+
}
|
|
398
|
+
}
|
|
386
399
|
const results = [];
|
|
387
400
|
for (const [pool, amount] of deposits) {
|
|
388
401
|
const result = await this.#depositToPool(pool, depositor, amount);
|
|
@@ -400,7 +413,9 @@ class AccountOpener extends SDKConstruct {
|
|
|
400
413
|
};
|
|
401
414
|
let txHash;
|
|
402
415
|
try {
|
|
403
|
-
const amnt = this.sdk.tokensMeta.formatBN(pool.underlying, amount
|
|
416
|
+
const amnt = this.sdk.tokensMeta.formatBN(pool.underlying, amount, {
|
|
417
|
+
symbol: true
|
|
418
|
+
});
|
|
404
419
|
this.#logger?.debug(`depositing ${amnt} into pool ${poolName}`);
|
|
405
420
|
let allowance = await this.#anvil.readContract({
|
|
406
421
|
address: underlying,
|
|
@@ -410,7 +425,7 @@ class AccountOpener extends SDKConstruct {
|
|
|
410
425
|
});
|
|
411
426
|
if (allowance < amount) {
|
|
412
427
|
this.#logger?.warn(
|
|
413
|
-
`depositor balance in underlying: ${this.sdk.tokensMeta.formatBN(pool.underlying, allowance)} is less than the amount to deposit: ${amnt}`
|
|
428
|
+
`depositor balance in underlying: ${this.sdk.tokensMeta.formatBN(pool.underlying, allowance, { symbol: true })} is less than the amount to deposit: ${amnt}`
|
|
414
429
|
);
|
|
415
430
|
if (this.#allowMint) {
|
|
416
431
|
allowance = await this.#tryMint(
|
|
@@ -421,7 +436,7 @@ class AccountOpener extends SDKConstruct {
|
|
|
421
436
|
}
|
|
422
437
|
}
|
|
423
438
|
this.#logger?.debug(
|
|
424
|
-
`depositor balance in underlying: ${this.sdk.tokensMeta.formatBN(pool.underlying, allowance)}`
|
|
439
|
+
`depositor balance in underlying: ${this.sdk.tokensMeta.formatBN(pool.underlying, allowance, { symbol: true })}`
|
|
425
440
|
);
|
|
426
441
|
txHash = await this.#anvil.writeContract({
|
|
427
442
|
account: depositor,
|
|
@@ -479,14 +494,11 @@ class AccountOpener extends SDKConstruct {
|
|
|
479
494
|
* @param amount
|
|
480
495
|
*/
|
|
481
496
|
async #tryMint(token, dest, amount) {
|
|
482
|
-
const
|
|
483
|
-
const amnt = this.sdk.tokensMeta.formatBN(token, amount);
|
|
497
|
+
const amnt = this.sdk.tokensMeta.formatBN(token, amount, { symbol: true });
|
|
484
498
|
try {
|
|
485
499
|
await this.#mint(token, dest, amount);
|
|
486
500
|
} catch (e) {
|
|
487
|
-
this.#logger?.warn(
|
|
488
|
-
`failed to mint ${amnt} ${symbol} to ${dest.address}: ${e}`
|
|
489
|
-
);
|
|
501
|
+
this.#logger?.warn(`failed to mint ${amnt} to ${dest.address}: ${e}`);
|
|
490
502
|
}
|
|
491
503
|
return await this.#anvil.readContract({
|
|
492
504
|
address: token,
|
|
@@ -508,8 +520,46 @@ class AccountOpener extends SDKConstruct {
|
|
|
508
520
|
await this.#anvil.impersonateAccount({ address: owner });
|
|
509
521
|
await this.#anvil.setBalance({
|
|
510
522
|
address: owner,
|
|
511
|
-
value: parseEther("
|
|
523
|
+
value: parseEther("1000000")
|
|
512
524
|
});
|
|
525
|
+
try {
|
|
526
|
+
await this.#mintDirectly(owner, token, amount, dest.address);
|
|
527
|
+
} catch (e) {
|
|
528
|
+
this.#logger?.warn(
|
|
529
|
+
`failed to mint directly ${amnt} ${symbol} to ${dest.address}: ${e}`
|
|
530
|
+
);
|
|
531
|
+
try {
|
|
532
|
+
await this.#mintAndTransfer(owner, token, amount, dest.address);
|
|
533
|
+
} catch (e2) {
|
|
534
|
+
this.#logger?.warn(
|
|
535
|
+
`failed to mint and transfer ${amnt} ${symbol} to ${dest.address}: ${e2}`
|
|
536
|
+
);
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
await this.#anvil.stopImpersonatingAccount({ address: owner });
|
|
540
|
+
}
|
|
541
|
+
async #mintDirectly(owner, token, amount, dest) {
|
|
542
|
+
const hash = await this.#anvil.writeContract({
|
|
543
|
+
account: owner,
|
|
544
|
+
address: token,
|
|
545
|
+
abi: parseAbi([
|
|
546
|
+
"function mint(address to, uint256 amount) returns (bool)"
|
|
547
|
+
]),
|
|
548
|
+
functionName: "mint",
|
|
549
|
+
args: [dest, amount],
|
|
550
|
+
chain: this.#anvil.chain
|
|
551
|
+
});
|
|
552
|
+
this.#logger?.debug(
|
|
553
|
+
`mint ${this.sdk.tokensMeta.formatBN(token, amount, { symbol: true })} to ${owner} in tx ${hash}`
|
|
554
|
+
);
|
|
555
|
+
const receipt = await this.#anvil.waitForTransactionReceipt({ hash });
|
|
556
|
+
if (receipt.status === "reverted") {
|
|
557
|
+
throw new Error(
|
|
558
|
+
`failed to mint ${this.sdk.tokensMeta.formatBN(token, amount, { symbol: true })} to ${owner} in tx ${hash}: reverted`
|
|
559
|
+
);
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
async #mintAndTransfer(owner, token, amount, dest) {
|
|
513
563
|
let hash = await this.#anvil.writeContract({
|
|
514
564
|
account: owner,
|
|
515
565
|
address: token,
|
|
@@ -518,11 +568,13 @@ class AccountOpener extends SDKConstruct {
|
|
|
518
568
|
args: [amount],
|
|
519
569
|
chain: this.#anvil.chain
|
|
520
570
|
});
|
|
521
|
-
this.#logger?.debug(
|
|
571
|
+
this.#logger?.debug(
|
|
572
|
+
`mint ${this.sdk.tokensMeta.formatBN(token, amount, { symbol: true })} to ${owner} in tx ${hash}`
|
|
573
|
+
);
|
|
522
574
|
let receipt = await this.#anvil.waitForTransactionReceipt({ hash });
|
|
523
575
|
if (receipt.status === "reverted") {
|
|
524
576
|
throw new Error(
|
|
525
|
-
`failed to mint ${
|
|
577
|
+
`failed to mint ${this.sdk.tokensMeta.formatBN(token, amount, { symbol: true })} to ${owner} in tx ${hash}: reverted`
|
|
526
578
|
);
|
|
527
579
|
}
|
|
528
580
|
hash = await this.#anvil.writeContract({
|
|
@@ -530,19 +582,18 @@ class AccountOpener extends SDKConstruct {
|
|
|
530
582
|
address: token,
|
|
531
583
|
abi: ierc20Abi,
|
|
532
584
|
functionName: "transfer",
|
|
533
|
-
args: [dest
|
|
585
|
+
args: [dest, amount],
|
|
534
586
|
chain: this.#anvil.chain
|
|
535
587
|
});
|
|
536
588
|
this.#logger?.debug(
|
|
537
|
-
`transfer ${
|
|
589
|
+
`transfer ${this.sdk.tokensMeta.formatBN(token, amount, { symbol: true })} from ${owner} to ${dest} in tx ${hash}`
|
|
538
590
|
);
|
|
539
591
|
receipt = await this.#anvil.waitForTransactionReceipt({ hash });
|
|
540
592
|
if (receipt.status === "reverted") {
|
|
541
593
|
throw new Error(
|
|
542
|
-
`failed to transfer ${
|
|
594
|
+
`failed to transfer ${this.sdk.tokensMeta.formatBN(token, amount, { symbol: true })} from ${owner} to ${dest} in tx ${hash}: reverted`
|
|
543
595
|
);
|
|
544
596
|
}
|
|
545
|
-
await this.#anvil.stopImpersonatingAccount({ address: owner });
|
|
546
597
|
}
|
|
547
598
|
/**
|
|
548
599
|
* Creates borrower wallet,
|
|
@@ -809,17 +860,23 @@ class AccountOpener extends SDKConstruct {
|
|
|
809
860
|
const desiredQuota = this.#calcQuota(amount, debt, collateralLT);
|
|
810
861
|
logger?.debug(
|
|
811
862
|
{
|
|
812
|
-
desiredQuota: this.sdk.tokensMeta.formatBN(underlying, desiredQuota
|
|
863
|
+
desiredQuota: this.sdk.tokensMeta.formatBN(underlying, desiredQuota, {
|
|
864
|
+
symbol: true
|
|
865
|
+
}),
|
|
813
866
|
availableQuota: this.sdk.tokensMeta.formatBN(
|
|
814
867
|
underlying,
|
|
815
|
-
availableQuota
|
|
868
|
+
availableQuota,
|
|
869
|
+
{ symbol: true }
|
|
816
870
|
),
|
|
817
871
|
collateralAmount: this.sdk.tokensMeta.formatBN(
|
|
818
872
|
collateral,
|
|
819
|
-
collateralAmount
|
|
873
|
+
collateralAmount,
|
|
874
|
+
{ symbol: true }
|
|
820
875
|
),
|
|
821
|
-
amount: this.sdk.tokensMeta.formatBN(underlying, amount
|
|
822
|
-
|
|
876
|
+
amount: this.sdk.tokensMeta.formatBN(underlying, amount, {
|
|
877
|
+
symbol: true
|
|
878
|
+
}),
|
|
879
|
+
debt: this.sdk.tokensMeta.formatBN(underlying, debt, { symbol: true }),
|
|
823
880
|
lt: Number(collateralLT)
|
|
824
881
|
},
|
|
825
882
|
"calculated quota"
|