@gearbox-protocol/deploy-tools 6.9.2 → 6.9.3
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/index.mjs +94 -39
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -197366,22 +197366,17 @@ function discoverRwaCreditManagers(sdk, logger) {
|
|
|
197366
197366
|
);
|
|
197367
197367
|
continue;
|
|
197368
197368
|
}
|
|
197369
|
-
const dsToken = matched[0];
|
|
197370
|
-
const dsTokenMeta = sdk.tokensMeta.mustGet(dsToken);
|
|
197369
|
+
const dsToken = sdk.tokensMeta.mustGet(matched[0]);
|
|
197371
197370
|
const assetMeta = sdk.tokensMeta.mustGet(underlyingMeta.asset);
|
|
197372
197371
|
result.push({
|
|
197373
197372
|
liquidity,
|
|
197374
197373
|
pool: market.pool.pool.address,
|
|
197375
|
-
label: `${cm.creditManager.name} \u2014 ${
|
|
197374
|
+
label: `${cm.creditManager.name} \u2014 ${dsToken.symbol} ${liquidity}`,
|
|
197376
197375
|
creditManager: cm.creditManager.address,
|
|
197377
|
-
dsToken,
|
|
197378
197376
|
factory: factory.address,
|
|
197379
|
-
|
|
197380
|
-
|
|
197381
|
-
asset:
|
|
197382
|
-
assetDecimals: assetMeta.decimals,
|
|
197383
|
-
dsTokenDecimals: dsTokenMeta.decimals,
|
|
197384
|
-
dsTokenSymbol: dsTokenMeta.symbol
|
|
197377
|
+
dsToken,
|
|
197378
|
+
underlying: sdk.tokensMeta.mustGet(market.underlying),
|
|
197379
|
+
asset: assetMeta
|
|
197385
197380
|
});
|
|
197386
197381
|
}
|
|
197387
197382
|
}
|
|
@@ -197539,6 +197534,21 @@ var LEVERAGE_ONE = 10n ** BigInt(LEVERAGE_PRECISION);
|
|
|
197539
197534
|
function childLogger2(logger, context) {
|
|
197540
197535
|
return logger?.child?.(context) ?? logger;
|
|
197541
197536
|
}
|
|
197537
|
+
function calcQuota(amount, debt, lt) {
|
|
197538
|
+
let quota = amount * lt / PERCENTAGE_FACTOR;
|
|
197539
|
+
quota = debt < quota ? debt : quota;
|
|
197540
|
+
quota = quota * (PERCENTAGE_FACTOR + 500n) / PERCENTAGE_FACTOR;
|
|
197541
|
+
return quota / PERCENTAGE_FACTOR * PERCENTAGE_FACTOR;
|
|
197542
|
+
}
|
|
197543
|
+
function getDsTokenQuota(market, cm, dsToken, dsTokenAmount, debt) {
|
|
197544
|
+
const lt = BigInt(cm.creditManager.liquidationThresholds.mustGet(dsToken));
|
|
197545
|
+
const amountInUnderlying = market.priceOracle.convert(
|
|
197546
|
+
dsToken,
|
|
197547
|
+
cm.underlying,
|
|
197548
|
+
dsTokenAmount
|
|
197549
|
+
);
|
|
197550
|
+
return calcQuota(amountInUnderlying, debt, lt);
|
|
197551
|
+
}
|
|
197542
197552
|
async function runSecuritizeBorrowingFlow(props) {
|
|
197543
197553
|
const {
|
|
197544
197554
|
sdk,
|
|
@@ -197560,29 +197570,30 @@ async function runSecuritizeBorrowingFlow(props) {
|
|
|
197560
197570
|
const investor = wallet.account.address;
|
|
197561
197571
|
logger?.info(`created investor wallet ${investor}`);
|
|
197562
197572
|
const market = sdk.marketRegister.findByPool(entry.pool);
|
|
197573
|
+
const cm = sdk.marketRegister.findCreditManager(entry.creditManager);
|
|
197563
197574
|
const dsAmount = market.priceOracle.convertFromUSD(
|
|
197564
|
-
entry.dsToken,
|
|
197575
|
+
entry.dsToken.addr,
|
|
197565
197576
|
BigInt(usdAmount) * 10n ** 8n
|
|
197566
197577
|
);
|
|
197567
197578
|
await claimDSToken({
|
|
197568
197579
|
anvil,
|
|
197569
197580
|
claimer: investor,
|
|
197570
197581
|
adminPrivateKey,
|
|
197571
|
-
token: entry.dsToken,
|
|
197582
|
+
token: entry.dsToken.addr,
|
|
197572
197583
|
usdAmount,
|
|
197573
197584
|
marketConfigurators,
|
|
197574
197585
|
rwaFactories,
|
|
197575
197586
|
logger
|
|
197576
197587
|
});
|
|
197577
197588
|
logger?.info(
|
|
197578
|
-
`claimed ${dsAmount} ${entry.
|
|
197589
|
+
`claimed ${dsAmount} ${entry.dsToken.symbol} (${usdAmount} USD) for ${investor}`
|
|
197579
197590
|
);
|
|
197580
197591
|
const approvalTarget = await sdk.accounts.getApprovalAddress({
|
|
197581
197592
|
creditManager: entry.creditManager,
|
|
197582
197593
|
borrower: investor
|
|
197583
197594
|
});
|
|
197584
197595
|
let hash4 = await wallet.writeContract({
|
|
197585
|
-
address: entry.dsToken,
|
|
197596
|
+
address: entry.dsToken.addr,
|
|
197586
197597
|
abi: erc20Abi,
|
|
197587
197598
|
functionName: "approve",
|
|
197588
197599
|
args: [approvalTarget, MAX_UINT256],
|
|
@@ -197593,12 +197604,12 @@ async function runSecuritizeBorrowingFlow(props) {
|
|
|
197593
197604
|
pollingInterval: 100
|
|
197594
197605
|
});
|
|
197595
197606
|
logger?.info(
|
|
197596
|
-
`approved ${approvalTarget} for ${entry.
|
|
197607
|
+
`approved ${approvalTarget} for ${entry.dsToken.symbol}: ${hash4}`
|
|
197597
197608
|
);
|
|
197598
197609
|
const requirements = await sdk.accounts.getOpenAccountRequirements(
|
|
197599
197610
|
investor,
|
|
197600
197611
|
entry.creditManager,
|
|
197601
|
-
{ tokenOutAddress: entry.dsToken }
|
|
197612
|
+
{ tokenOutAddress: entry.dsToken.addr }
|
|
197602
197613
|
);
|
|
197603
197614
|
if (!requirements) {
|
|
197604
197615
|
throw new Error(
|
|
@@ -197613,7 +197624,7 @@ async function runSecuritizeBorrowingFlow(props) {
|
|
|
197613
197624
|
requirements.requiredSignatures
|
|
197614
197625
|
);
|
|
197615
197626
|
logger?.info(`signed ${signaturesToCache.length} register-vault message(s)`);
|
|
197616
|
-
const debtBn = parseUnits(debt, entry.
|
|
197627
|
+
const debtBn = parseUnits(debt, entry.underlying.decimals);
|
|
197617
197628
|
const unwrapCalls = await sdk.accounts.getRWAUnwrapCalls(
|
|
197618
197629
|
debtBn,
|
|
197619
197630
|
entry.creditManager
|
|
@@ -197622,21 +197633,31 @@ async function runSecuritizeBorrowingFlow(props) {
|
|
|
197622
197633
|
throw new Error(`getRWAUnwrapCalls returned undefined for ${entry.label}`);
|
|
197623
197634
|
}
|
|
197624
197635
|
logger?.info(`built ${unwrapCalls.length} unwrap call(s)`);
|
|
197636
|
+
const quota = getDsTokenQuota(
|
|
197637
|
+
market,
|
|
197638
|
+
cm,
|
|
197639
|
+
entry.dsToken.addr,
|
|
197640
|
+
dsAmount,
|
|
197641
|
+
debtBn
|
|
197642
|
+
);
|
|
197643
|
+
logger?.debug(
|
|
197644
|
+
`computed dsToken quota: ${sdk.tokensMeta.formatBN(entry.underlying.addr, quota, { symbol: true })}`
|
|
197645
|
+
);
|
|
197625
197646
|
const { tx } = await sdk.accounts.openCA({
|
|
197626
197647
|
creditManager: entry.creditManager,
|
|
197627
|
-
collateral: [{ token: entry.dsToken, balance: dsAmount }],
|
|
197648
|
+
collateral: [{ token: entry.dsToken.addr, balance: dsAmount }],
|
|
197628
197649
|
debt: debtBn,
|
|
197629
197650
|
calls: unwrapCalls,
|
|
197630
|
-
withdrawToken: entry.asset,
|
|
197631
|
-
averageQuota: [{ token: entry.dsToken, balance:
|
|
197632
|
-
minQuota: [{ token: entry.dsToken, balance:
|
|
197651
|
+
withdrawToken: entry.asset.addr,
|
|
197652
|
+
averageQuota: [{ token: entry.dsToken.addr, balance: quota }],
|
|
197653
|
+
minQuota: [{ token: entry.dsToken.addr, balance: quota }],
|
|
197633
197654
|
to: investor,
|
|
197634
197655
|
ethAmount: 0n,
|
|
197635
197656
|
permits: {},
|
|
197636
197657
|
referralCode: 0n,
|
|
197637
197658
|
rwaOptions: {
|
|
197638
197659
|
type: RWA_FACTORY_SECURITIZE,
|
|
197639
|
-
tokensToRegister: [entry.dsToken],
|
|
197660
|
+
tokensToRegister: [entry.dsToken.addr],
|
|
197640
197661
|
signaturesToCache
|
|
197641
197662
|
}
|
|
197642
197663
|
});
|
|
@@ -197663,8 +197684,16 @@ async function runSecuritizeBorrowingFlow(props) {
|
|
|
197663
197684
|
}
|
|
197664
197685
|
const creditAccount = logs[0].args.creditAccount;
|
|
197665
197686
|
logger?.info(
|
|
197666
|
-
`borrowing flow ok: credit account ${creditAccount} opened for ${investor} with ${sdk.tokensMeta.formatBN(entry.dsToken, dsAmount, { symbol: true })} collateral and ${sdk.tokensMeta.formatBN(entry.underlying, debtBn, { symbol: true })} debt, tx ${hash4}`
|
|
197687
|
+
`borrowing flow ok: credit account ${creditAccount} opened for ${investor} with ${sdk.tokensMeta.formatBN(entry.dsToken.addr, dsAmount, { symbol: true })} collateral and ${sdk.tokensMeta.formatBN(entry.underlying.addr, debtBn, { symbol: true })} debt, tx ${hash4}`
|
|
197667
197688
|
);
|
|
197689
|
+
const ca = await sdk.accounts.getCreditAccountData(creditAccount);
|
|
197690
|
+
for (const token of ca?.tokens ?? []) {
|
|
197691
|
+
if (token.balance > 1n || token.quota > 1n) {
|
|
197692
|
+
logger?.warn(
|
|
197693
|
+
`${entry.label}: credit account ${creditAccount} has ${sdk.tokensMeta.formatBN(token.token, token.balance, { symbol: true })} collateral with ${sdk.tokensMeta.formatBN(entry.underlying.addr, token.quota, { symbol: true })} quota`
|
|
197694
|
+
);
|
|
197695
|
+
}
|
|
197696
|
+
}
|
|
197668
197697
|
return { txHash: hash4, creditAccount };
|
|
197669
197698
|
}
|
|
197670
197699
|
async function runSecuritizeLeverageFlow(props) {
|
|
@@ -197682,19 +197711,19 @@ async function runSecuritizeLeverageFlow(props) {
|
|
|
197682
197711
|
anvil,
|
|
197683
197712
|
claimer: investor,
|
|
197684
197713
|
adminPrivateKey,
|
|
197685
|
-
token: entry.dsToken,
|
|
197714
|
+
token: entry.dsToken.addr,
|
|
197686
197715
|
logger
|
|
197687
197716
|
});
|
|
197688
|
-
logger?.info(`registered investor ${investor} for ${entry.
|
|
197717
|
+
logger?.info(`registered investor ${investor} for ${entry.dsToken.symbol}`);
|
|
197689
197718
|
const asset = entry.asset;
|
|
197690
|
-
const debtBn = parseUnits(debt, entry.
|
|
197719
|
+
const debtBn = parseUnits(debt, entry.underlying.decimals);
|
|
197691
197720
|
const leverageBn = parseUnits(leverage, LEVERAGE_PRECISION);
|
|
197692
197721
|
if (leverageBn <= LEVERAGE_ONE) {
|
|
197693
197722
|
throw new Error(`leverage must be > 1, got ${leverage}`);
|
|
197694
197723
|
}
|
|
197695
197724
|
const assetAmount = debtBn * LEVERAGE_ONE / (leverageBn - LEVERAGE_ONE);
|
|
197696
197725
|
await anvil.deal({
|
|
197697
|
-
erc20: asset,
|
|
197726
|
+
erc20: asset.addr,
|
|
197698
197727
|
account: investor,
|
|
197699
197728
|
amount: assetAmount
|
|
197700
197729
|
});
|
|
@@ -197706,7 +197735,7 @@ async function runSecuritizeLeverageFlow(props) {
|
|
|
197706
197735
|
borrower: investor
|
|
197707
197736
|
});
|
|
197708
197737
|
let hash4 = await wallet.writeContract({
|
|
197709
|
-
address: asset,
|
|
197738
|
+
address: asset.addr,
|
|
197710
197739
|
abi: erc20Abi,
|
|
197711
197740
|
functionName: "approve",
|
|
197712
197741
|
args: [approvalTarget, MAX_UINT256],
|
|
@@ -197716,11 +197745,11 @@ async function runSecuritizeLeverageFlow(props) {
|
|
|
197716
197745
|
hash: hash4,
|
|
197717
197746
|
pollingInterval: 100
|
|
197718
197747
|
});
|
|
197719
|
-
logger?.info(`approved ${approvalTarget} for asset: ${hash4}`);
|
|
197748
|
+
logger?.info(`approved ${approvalTarget} for ${asset.symbol}: ${hash4}`);
|
|
197720
197749
|
const requirements = await sdk.accounts.getOpenAccountRequirements(
|
|
197721
197750
|
investor,
|
|
197722
197751
|
entry.creditManager,
|
|
197723
|
-
{ tokenOutAddress: entry.dsToken }
|
|
197752
|
+
{ tokenOutAddress: entry.dsToken.addr }
|
|
197724
197753
|
);
|
|
197725
197754
|
if (!requirements) {
|
|
197726
197755
|
throw new Error(
|
|
@@ -197745,28 +197774,46 @@ async function runSecuritizeLeverageFlow(props) {
|
|
|
197745
197774
|
logger?.info(`built ${unwrapCalls.length} unwrap call(s)`);
|
|
197746
197775
|
const strategy = await sdk.routerFor(cm).findOpenStrategyPath({
|
|
197747
197776
|
creditManager: cm.creditManager,
|
|
197748
|
-
expectedBalances: [{ token: asset, balance: assetAmount + debtBn }],
|
|
197749
|
-
leftoverBalances: [{ token: asset, balance: 1n }],
|
|
197777
|
+
expectedBalances: [{ token: asset.addr, balance: assetAmount + debtBn }],
|
|
197778
|
+
leftoverBalances: [{ token: asset.addr, balance: 1n }],
|
|
197750
197779
|
slippage: 50,
|
|
197751
|
-
target: entry.dsToken
|
|
197780
|
+
target: entry.dsToken.addr
|
|
197752
197781
|
});
|
|
197753
197782
|
logger?.info(
|
|
197754
|
-
`router strategy: ${strategy.calls.length} call(s) to reach ${entry.
|
|
197783
|
+
`router strategy: ${strategy.calls.length} call(s) to reach ${entry.dsToken.symbol}`
|
|
197784
|
+
);
|
|
197785
|
+
const market = sdk.marketRegister.findByCreditManager(entry.creditManager);
|
|
197786
|
+
const averageQuotaBn = getDsTokenQuota(
|
|
197787
|
+
market,
|
|
197788
|
+
cm,
|
|
197789
|
+
entry.dsToken.addr,
|
|
197790
|
+
strategy.amount,
|
|
197791
|
+
debtBn
|
|
197792
|
+
);
|
|
197793
|
+
const minQuotaBn = getDsTokenQuota(
|
|
197794
|
+
market,
|
|
197795
|
+
cm,
|
|
197796
|
+
entry.dsToken.addr,
|
|
197797
|
+
strategy.minAmount,
|
|
197798
|
+
debtBn
|
|
197799
|
+
);
|
|
197800
|
+
logger?.debug(
|
|
197801
|
+
`computed dsToken quota: average=${sdk.tokensMeta.formatBN(entry.underlying.addr, averageQuotaBn, { symbol: true })}, min=${sdk.tokensMeta.formatBN(entry.underlying.addr, minQuotaBn, { symbol: true })}`
|
|
197755
197802
|
);
|
|
197756
197803
|
const { tx } = await sdk.accounts.openCA({
|
|
197757
197804
|
creditManager: entry.creditManager,
|
|
197758
|
-
collateral: [{ token: asset, balance: assetAmount }],
|
|
197805
|
+
collateral: [{ token: asset.addr, balance: assetAmount }],
|
|
197759
197806
|
debt: debtBn,
|
|
197760
197807
|
calls: [...unwrapCalls, ...strategy.calls],
|
|
197761
|
-
averageQuota: [{ token: entry.dsToken, balance:
|
|
197762
|
-
minQuota: [{ token: entry.dsToken, balance:
|
|
197808
|
+
averageQuota: [{ token: entry.dsToken.addr, balance: averageQuotaBn }],
|
|
197809
|
+
minQuota: [{ token: entry.dsToken.addr, balance: minQuotaBn }],
|
|
197763
197810
|
to: investor,
|
|
197764
197811
|
ethAmount: 0n,
|
|
197765
197812
|
permits: {},
|
|
197766
197813
|
referralCode: 0n,
|
|
197767
197814
|
rwaOptions: {
|
|
197768
197815
|
type: RWA_FACTORY_SECURITIZE,
|
|
197769
|
-
tokensToRegister: [entry.dsToken],
|
|
197816
|
+
tokensToRegister: [entry.dsToken.addr],
|
|
197770
197817
|
signaturesToCache
|
|
197771
197818
|
}
|
|
197772
197819
|
});
|
|
@@ -197793,8 +197840,16 @@ async function runSecuritizeLeverageFlow(props) {
|
|
|
197793
197840
|
}
|
|
197794
197841
|
const creditAccount = logs[0].args.creditAccount;
|
|
197795
197842
|
logger?.info(
|
|
197796
|
-
`leverage flow ok: credit account ${creditAccount} opened for ${investor} with ${sdk.tokensMeta.formatBN(asset, assetAmount, { symbol: true })} collateral and ${sdk.tokensMeta.formatBN(entry.underlying, debtBn, { symbol: true })} debt, tx ${hash4}`
|
|
197843
|
+
`leverage flow ok: credit account ${creditAccount} opened for ${investor} with ${sdk.tokensMeta.formatBN(asset.addr, assetAmount, { symbol: true })} collateral and ${sdk.tokensMeta.formatBN(entry.underlying.addr, debtBn, { symbol: true })} debt, tx ${hash4}`
|
|
197797
197844
|
);
|
|
197845
|
+
const ca = await sdk.accounts.getCreditAccountData(creditAccount);
|
|
197846
|
+
for (const token of ca?.tokens ?? []) {
|
|
197847
|
+
if (token.balance > 1n || token.quota > 1n) {
|
|
197848
|
+
logger?.warn(
|
|
197849
|
+
`${entry.label}: credit account ${creditAccount} has ${sdk.tokensMeta.formatBN(token.token, token.balance, { symbol: true })} collateral with ${sdk.tokensMeta.formatBN(entry.underlying.addr, token.quota, { symbol: true })} quota`
|
|
197850
|
+
);
|
|
197851
|
+
}
|
|
197852
|
+
}
|
|
197798
197853
|
return { txHash: hash4, creditAccount };
|
|
197799
197854
|
}
|
|
197800
197855
|
|