@strkfarm/sdk 2.0.0-dev.1 → 2.0.0-dev.2
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.browser.global.js +303 -83
- package/dist/index.browser.mjs +303 -83
- package/dist/index.d.ts +31 -5
- package/dist/index.js +303 -83
- package/dist/index.mjs +303 -83
- package/package.json +1 -1
- package/src/dataTypes/address.ts +1 -1
- package/src/modules/ekubo-quoter.ts +2 -1
- package/src/strategies/universal-adapters/avnu-adapter.ts +1 -8
- package/src/strategies/universal-adapters/extended-adapter.ts +128 -11
- package/src/strategies/universal-adapters/vesu-multiply-adapter.ts +16 -16
- package/src/strategies/vesu-extended-strategy/services/operationService.ts +8 -2
- package/src/strategies/vesu-extended-strategy/utils/constants.ts +1 -0
- package/src/strategies/vesu-extended-strategy/utils/helper.ts +12 -10
- package/src/strategies/vesu-extended-strategy/vesu-extended-strategy.tsx +199 -57
|
@@ -56069,6 +56069,7 @@ ${JSON.stringify(data, null, 2)}`;
|
|
|
56069
56069
|
return quote.splits.map((split2) => {
|
|
56070
56070
|
const isNegativeAmount = BigInt(split2.amount_specified) <= 0n;
|
|
56071
56071
|
const token = isNegativeAmount ? toTokenInfo : fromTokenInfo;
|
|
56072
|
+
console.log("token for withdrawal", token, isNegativeAmount);
|
|
56072
56073
|
return {
|
|
56073
56074
|
route: split2.route.map((_route) => ({
|
|
56074
56075
|
pool_key: {
|
|
@@ -92375,6 +92376,7 @@ spurious results.`);
|
|
|
92375
92376
|
var AVNU_API = "https://starknet.api.avnu.fi/swap/v2/quotes";
|
|
92376
92377
|
var USDC_TOKEN_DECIMALS = 6;
|
|
92377
92378
|
var USDC_TOKEN_ADDRESS = "0x053C91253BC9682c04929cA02ED00b3E423f6710D2ee7e0D5EBB06F3eCF368A8";
|
|
92379
|
+
var BUFFER_USDC_IN_WITHDRAWAL = 5;
|
|
92378
92380
|
var WBTC_TOKEN_ADDRESS = "0x3fe2b97c1fd336e750087d68b9b867997fd64a2661ff3ca5a7c771641e8e7ac";
|
|
92379
92381
|
var WBTC_TOKEN_DECIMALS = 8;
|
|
92380
92382
|
var MAINTENANCE_MARGIN = 0.01;
|
|
@@ -92466,8 +92468,8 @@ spurious results.`);
|
|
|
92466
92468
|
logger2.error("error getting extended positions");
|
|
92467
92469
|
return null;
|
|
92468
92470
|
}
|
|
92469
|
-
const
|
|
92470
|
-
|
|
92471
|
+
const extendedExposureUSD = extendedPosition.length > 0 ? new Web3Number(extendedPosition[0].value, USDC_TOKEN_DECIMALS) : new Web3Number(0, USDC_TOKEN_DECIMALS);
|
|
92472
|
+
console.log("THe collateral is", collateralPrice, collateralUnits);
|
|
92471
92473
|
const vesuExposureUSD = collateralUnits.multipliedBy(collateralPrice);
|
|
92472
92474
|
if (vesuExposureUSD.lessThan(0)) {
|
|
92473
92475
|
return {
|
|
@@ -92485,9 +92487,14 @@ spurious results.`);
|
|
|
92485
92487
|
vesu_leverage
|
|
92486
92488
|
};
|
|
92487
92489
|
}
|
|
92490
|
+
console.log("the vesu exposure usd is", vesuExposureUSD.toNumber());
|
|
92491
|
+
console.log("the extended exposure usd is", extendedExposureUSD.toNumber());
|
|
92492
|
+
console.log("the amount in usdc is", amountInUsdc.toNumber());
|
|
92493
|
+
console.log("the extended leverage is", extended_leverage);
|
|
92494
|
+
console.log("the vesu leverage is", vesu_leverage);
|
|
92488
92495
|
const numerator1 = amountInUsdc.multipliedBy(extended_leverage);
|
|
92489
|
-
const numerator2 = vesuExposureUSD
|
|
92490
|
-
const numerator3 = extendedExposureUSD.multipliedBy(
|
|
92496
|
+
const numerator2 = vesuExposureUSD;
|
|
92497
|
+
const numerator3 = extendedExposureUSD.multipliedBy(-1);
|
|
92491
92498
|
const finalNumerator = numerator1.plus(numerator2).plus(numerator3);
|
|
92492
92499
|
const denominator = extended_leverage + vesu_leverage;
|
|
92493
92500
|
const vesuAmountInUSDC = finalNumerator.dividedBy(denominator);
|
|
@@ -92497,6 +92504,7 @@ spurious results.`);
|
|
|
92497
92504
|
"the extended amount in usdc is",
|
|
92498
92505
|
extendedAmountInUSDC.toNumber()
|
|
92499
92506
|
);
|
|
92507
|
+
await new Promise((resolve) => setTimeout(resolve, 1e4));
|
|
92500
92508
|
return {
|
|
92501
92509
|
vesu_amount: vesuAmountInUSDC,
|
|
92502
92510
|
extended_amount: extendedAmountInUSDC,
|
|
@@ -92937,7 +92945,7 @@ spurious results.`);
|
|
|
92937
92945
|
this.config.debt.address.toBigInt(),
|
|
92938
92946
|
this.config.vaultAllocator.toBigInt()
|
|
92939
92947
|
],
|
|
92940
|
-
sanitizer: SIMPLE_SANITIZER_V2,
|
|
92948
|
+
sanitizer: isV2 ? SIMPLE_SANITIZER_V2 : SIMPLE_SANITIZER,
|
|
92941
92949
|
// vmw = vesu multiply withdraw
|
|
92942
92950
|
id: `vmw_${this.config.poolId.shortString()}_${collateral.symbol}_${debt.symbol}`
|
|
92943
92951
|
},
|
|
@@ -93086,7 +93094,7 @@ spurious results.`);
|
|
|
93086
93094
|
},
|
|
93087
93095
|
// Vesu multiply call
|
|
93088
93096
|
{
|
|
93089
|
-
sanitizer: SIMPLE_SANITIZER_V2,
|
|
93097
|
+
sanitizer: isV2 ? SIMPLE_SANITIZER_V2 : SIMPLE_SANITIZER,
|
|
93090
93098
|
call: {
|
|
93091
93099
|
contractAddress: vesuMultiply,
|
|
93092
93100
|
selector: hash_exports.getSelectorFromName("modify_lever"),
|
|
@@ -93174,19 +93182,13 @@ spurious results.`);
|
|
|
93174
93182
|
leverSwap = ekuboQuoter.getVesuMultiplyQuote(swapQuote, debtToken, collateralToken);
|
|
93175
93183
|
const MAX_SLIPPAGE = 2e-3;
|
|
93176
93184
|
if (debtAmount.greaterThan(0)) {
|
|
93177
|
-
|
|
93178
|
-
leverSwapLimitAmount = await ekuboQuoter.getSwapLimitAmount(debtToken, collateralToken, debtAmount, MAX_SLIPPAGE);
|
|
93179
|
-
const anotherleverSwapLimitAmount = debtAmount.multipliedBy(1 + MAX_SLIPPAGE);
|
|
93180
|
-
console.log("anotherleverSwapLimitAmount", anotherleverSwapLimitAmount, leverSwapLimitAmount);
|
|
93185
|
+
leverSwapLimitAmount = debtAmount.multipliedBy(1 + MAX_SLIPPAGE);
|
|
93181
93186
|
} else if (debtAmount.lessThan(0)) {
|
|
93182
|
-
leverSwapLimitAmount =
|
|
93183
|
-
const anotherleverSwapLimitAmount = debtAmount.abs().multipliedBy(1 - MAX_SLIPPAGE);
|
|
93184
|
-
console.log("anotherleverSwapLimitAmount", anotherleverSwapLimitAmount, leverSwapLimitAmount);
|
|
93187
|
+
leverSwapLimitAmount = debtAmount.abs().multipliedBy(1 - MAX_SLIPPAGE);
|
|
93185
93188
|
} else {
|
|
93186
93189
|
leverSwapLimitAmount = Web3Number.fromWei(0, this.config.debt.decimals);
|
|
93187
93190
|
}
|
|
93188
93191
|
await new Promise((resolve) => setTimeout(resolve, 1e4));
|
|
93189
|
-
console.log("leverSwapLimitAmount", leverSwapLimitAmount);
|
|
93190
93192
|
} else {
|
|
93191
93193
|
throw new Error(`VesuMultiplyAdapter: Price impact too high (${swapQuote.price_impact}), skipping swap`);
|
|
93192
93194
|
}
|
|
@@ -93256,26 +93258,23 @@ spurious results.`);
|
|
|
93256
93258
|
debtPrice.price,
|
|
93257
93259
|
debtToken.decimals
|
|
93258
93260
|
);
|
|
93259
|
-
console.log("debtAmountToRepay", debtAmountToRepay);
|
|
93260
93261
|
if (!debtAmountToRepay) {
|
|
93261
93262
|
throw new Error("error calculating debt amount to repay");
|
|
93262
93263
|
}
|
|
93263
93264
|
const ekuboQuoter = new EkuboQuoter(this.config.networkConfig, this.config.pricer);
|
|
93264
93265
|
const debtInDebtUnits = new Web3Number(debtAmountToRepay, debtToken.decimals).dividedBy(debtPrice.price).multipliedBy(10 ** debtToken.decimals);
|
|
93265
|
-
const debtInCollateralUnits = new Web3Number(debtAmountToRepay, debtToken.decimals).dividedBy(collateralPrice.price).multipliedBy(10 ** collateralToken.decimals);
|
|
93266
93266
|
const swapQuote = await ekuboQuoter.getQuote(
|
|
93267
93267
|
debtToken.address.address,
|
|
93268
93268
|
collateralToken.address.address,
|
|
93269
93269
|
debtInDebtUnits
|
|
93270
93270
|
);
|
|
93271
93271
|
const MAX_SLIPPAGE = 2e-3;
|
|
93272
|
-
if (swapQuote.price_impact <
|
|
93273
|
-
leverSwap = ekuboQuoter.getVesuMultiplyQuote(swapQuote,
|
|
93272
|
+
if (swapQuote.price_impact < 25e-4) {
|
|
93273
|
+
leverSwap = ekuboQuoter.getVesuMultiplyQuote(swapQuote, collateralToken, debtToken);
|
|
93274
93274
|
} else {
|
|
93275
93275
|
logger2.error(`VesuMultiplyAdapter: Price impact too high (${swapQuote.price_impact}), skipping swap`);
|
|
93276
93276
|
}
|
|
93277
|
-
|
|
93278
|
-
leverSwapLimitAmount = await ekuboQuoter.getSwapLimitAmount(debtToken, collateralToken, debtInCollateralUnits, MAX_SLIPPAGE);
|
|
93277
|
+
leverSwapLimitAmount = new Web3Number(debtAmountToRepay, debtToken.decimals).abs().multipliedBy(1 + MAX_SLIPPAGE);
|
|
93279
93278
|
const multiplyParams = await this.getLeverParams(false, params, leverSwap, leverSwapLimitAmount);
|
|
93280
93279
|
const call = multiplyContract.populate("modify_lever", {
|
|
93281
93280
|
modify_lever_params: this.formatMultiplyParams(false, multiplyParams)
|
|
@@ -93509,6 +93508,41 @@ spurious results.`);
|
|
|
93509
93508
|
}
|
|
93510
93509
|
];
|
|
93511
93510
|
}
|
|
93511
|
+
getSwapFromLegacyLeaf() {
|
|
93512
|
+
const leafConfigs = this._getSwapFromLegacyLeaf();
|
|
93513
|
+
const leaves = leafConfigs.map((config3) => {
|
|
93514
|
+
const { target, method, packedArguments, sanitizer, id } = config3;
|
|
93515
|
+
const leaf = this.constructSimpleLeafData({
|
|
93516
|
+
id,
|
|
93517
|
+
target,
|
|
93518
|
+
method,
|
|
93519
|
+
packedArguments
|
|
93520
|
+
}, sanitizer);
|
|
93521
|
+
return leaf;
|
|
93522
|
+
});
|
|
93523
|
+
return { leaves, callConstructor: this.getSwapFromLegacyCall.bind(this) };
|
|
93524
|
+
}
|
|
93525
|
+
_getSwapFromLegacyLeaf() {
|
|
93526
|
+
const usdceToken = Global.getDefaultTokens().find((token) => token.symbol === "USDCe");
|
|
93527
|
+
return [
|
|
93528
|
+
{
|
|
93529
|
+
target: usdceToken.address,
|
|
93530
|
+
method: "approve",
|
|
93531
|
+
packedArguments: [
|
|
93532
|
+
AVNU_EXCHANGE_FOR_LEGACY_USDC.toBigInt()
|
|
93533
|
+
],
|
|
93534
|
+
id: `extendedswaplegacyapprove_${usdceToken.symbol}`,
|
|
93535
|
+
sanitizer: AVNU_LEGACY_SANITIZER
|
|
93536
|
+
},
|
|
93537
|
+
{
|
|
93538
|
+
target: AVNU_EXCHANGE_FOR_LEGACY_USDC,
|
|
93539
|
+
method: "swap_to_new",
|
|
93540
|
+
packedArguments: [],
|
|
93541
|
+
id: `extended_swap_to_new_${usdceToken.symbol}`,
|
|
93542
|
+
sanitizer: AVNU_LEGACY_SANITIZER
|
|
93543
|
+
}
|
|
93544
|
+
];
|
|
93545
|
+
}
|
|
93512
93546
|
_getWithdrawLeaf() {
|
|
93513
93547
|
return [];
|
|
93514
93548
|
}
|
|
@@ -93589,6 +93623,70 @@ spurious results.`);
|
|
|
93589
93623
|
return [];
|
|
93590
93624
|
}
|
|
93591
93625
|
}
|
|
93626
|
+
getProofsForFromLegacySwap(tree) {
|
|
93627
|
+
let proofGroups = [];
|
|
93628
|
+
const ids = this.getSwapFromLegacyLeaf().leaves.map((l) => l.readableId);
|
|
93629
|
+
for (const [i, v] of tree.entries()) {
|
|
93630
|
+
if (ids.includes(v.readableId)) {
|
|
93631
|
+
proofGroups.push(tree.getProof(i));
|
|
93632
|
+
}
|
|
93633
|
+
}
|
|
93634
|
+
if (proofGroups.length != ids.length) {
|
|
93635
|
+
throw new Error(`Not all proofs found for IDs: ${ids.join(", ")}`);
|
|
93636
|
+
}
|
|
93637
|
+
return {
|
|
93638
|
+
proofs: proofGroups,
|
|
93639
|
+
callConstructor: this.getSwapFromLegacyCall.bind(this)
|
|
93640
|
+
};
|
|
93641
|
+
}
|
|
93642
|
+
async getSwapFromLegacyCall(params) {
|
|
93643
|
+
try {
|
|
93644
|
+
const usdcToken = this.config.supportedPositions[0].asset;
|
|
93645
|
+
const usdceToken = Global.getDefaultTokens().find((token) => token.symbol === "USDCe");
|
|
93646
|
+
const amount = uint256_exports.bnToUint256(params.amount.multipliedBy(10).toWei());
|
|
93647
|
+
const quotes = await this.config.avnuAdapter.getQuotesAvnu(
|
|
93648
|
+
usdceToken.address.toString(),
|
|
93649
|
+
usdcToken.address.toString(),
|
|
93650
|
+
params.amount.toNumber(),
|
|
93651
|
+
this.config.avnuAdapter.config.vaultAllocator.address.toString(),
|
|
93652
|
+
usdcToken.decimals,
|
|
93653
|
+
false
|
|
93654
|
+
);
|
|
93655
|
+
if (!quotes) {
|
|
93656
|
+
logger2.error("error getting quotes from avnu");
|
|
93657
|
+
return [];
|
|
93658
|
+
}
|
|
93659
|
+
const getCalldata = await this.config.avnuAdapter.getSwapCallData(quotes);
|
|
93660
|
+
const swapCallData = getCalldata[0];
|
|
93661
|
+
return [
|
|
93662
|
+
{
|
|
93663
|
+
sanitizer: AVNU_LEGACY_SANITIZER,
|
|
93664
|
+
call: {
|
|
93665
|
+
contractAddress: usdceToken.address,
|
|
93666
|
+
selector: hash_exports.getSelectorFromName("approve"),
|
|
93667
|
+
calldata: [
|
|
93668
|
+
AVNU_EXCHANGE_FOR_LEGACY_USDC.toBigInt(),
|
|
93669
|
+
toBigInt3(amount.low.toString()),
|
|
93670
|
+
// amount low
|
|
93671
|
+
toBigInt3(amount.high.toString())
|
|
93672
|
+
// amount high
|
|
93673
|
+
]
|
|
93674
|
+
}
|
|
93675
|
+
},
|
|
93676
|
+
{
|
|
93677
|
+
sanitizer: AVNU_LEGACY_SANITIZER,
|
|
93678
|
+
call: {
|
|
93679
|
+
contractAddress: AVNU_EXCHANGE_FOR_LEGACY_USDC,
|
|
93680
|
+
selector: hash_exports.getSelectorFromName("swap_to_new"),
|
|
93681
|
+
calldata: swapCallData
|
|
93682
|
+
}
|
|
93683
|
+
}
|
|
93684
|
+
];
|
|
93685
|
+
} catch (error2) {
|
|
93686
|
+
logger2.error(`Error creating Deposit Call: ${error2}`);
|
|
93687
|
+
return [];
|
|
93688
|
+
}
|
|
93689
|
+
}
|
|
93592
93690
|
//Swap wbtc to usdc
|
|
93593
93691
|
async getWithdrawCall(params) {
|
|
93594
93692
|
try {
|
|
@@ -93606,10 +93704,10 @@ spurious results.`);
|
|
|
93606
93704
|
if (!this.client) {
|
|
93607
93705
|
throw new Error("Client not initialized");
|
|
93608
93706
|
}
|
|
93609
|
-
const withdrawalRequest = this.client.withdrawUSDC(amount.
|
|
93610
|
-
if (
|
|
93611
|
-
|
|
93612
|
-
return
|
|
93707
|
+
const withdrawalRequest = await this.client.withdrawUSDC(amount.toFixed(2));
|
|
93708
|
+
if (withdrawalRequest.status === "OK") {
|
|
93709
|
+
const withdrawalStatus = await this.getDepositOrWithdrawalStatus(withdrawalRequest.data, "WITHDRAWAL" /* WITHDRAWAL */);
|
|
93710
|
+
return withdrawalStatus;
|
|
93613
93711
|
}
|
|
93614
93712
|
return false;
|
|
93615
93713
|
} catch (error2) {
|
|
@@ -93790,7 +93888,7 @@ spurious results.`);
|
|
|
93790
93888
|
logger2.error(`createShortOrder failed on attempt ${attempt}: ${err2.message}`);
|
|
93791
93889
|
if (attempt < maxAttempts) {
|
|
93792
93890
|
const backoff = 1200 * attempt;
|
|
93793
|
-
|
|
93891
|
+
logger2.info(`Retrying after ${backoff}ms...`);
|
|
93794
93892
|
await new Promise((resolve) => setTimeout(resolve, backoff));
|
|
93795
93893
|
return this.createOrder(
|
|
93796
93894
|
leverage,
|
|
@@ -93821,7 +93919,7 @@ spurious results.`);
|
|
|
93821
93919
|
}
|
|
93822
93920
|
return null;
|
|
93823
93921
|
} catch (err2) {
|
|
93824
|
-
|
|
93922
|
+
logger2.error(`Error opening short extended position, ${err2}`);
|
|
93825
93923
|
return null;
|
|
93826
93924
|
}
|
|
93827
93925
|
}
|
|
@@ -93831,17 +93929,16 @@ spurious results.`);
|
|
|
93831
93929
|
operationsType: [operationsType],
|
|
93832
93930
|
operationsStatus: ["COMPLETED" /* COMPLETED */]
|
|
93833
93931
|
});
|
|
93834
|
-
console.log("transferHistory", transferHistory);
|
|
93835
93932
|
if (operationsType === "DEPOSIT" /* DEPOSIT */) {
|
|
93836
93933
|
const myTransferStatus = transferHistory.data.find((operation) => operation.transactionHash === orderId);
|
|
93837
93934
|
if (!myTransferStatus) {
|
|
93838
|
-
return
|
|
93935
|
+
return true;
|
|
93839
93936
|
}
|
|
93840
93937
|
return true;
|
|
93841
93938
|
} else {
|
|
93842
|
-
const myTransferStatus = transferHistory.data.find((operation) => operation.id === orderId);
|
|
93939
|
+
const myTransferStatus = transferHistory.data.find((operation) => operation.id.toString() === orderId.toString());
|
|
93843
93940
|
if (!myTransferStatus) {
|
|
93844
|
-
return
|
|
93941
|
+
return true;
|
|
93845
93942
|
}
|
|
93846
93943
|
return true;
|
|
93847
93944
|
}
|
|
@@ -97425,7 +97522,7 @@ spurious results.`);
|
|
|
97425
97522
|
packedArguments: [
|
|
97426
97523
|
fromToken.address.toBigInt(),
|
|
97427
97524
|
//wbtc
|
|
97428
|
-
|
|
97525
|
+
toToken.address.toBigInt(),
|
|
97429
97526
|
//usdc
|
|
97430
97527
|
vaultAllocator.toBigInt()
|
|
97431
97528
|
],
|
|
@@ -97439,13 +97536,11 @@ spurious results.`);
|
|
|
97439
97536
|
}
|
|
97440
97537
|
async getDepositCall(params) {
|
|
97441
97538
|
try {
|
|
97442
|
-
console.log("params.amount", params.amount);
|
|
97443
97539
|
const fromToken = this.config.supportedPositions[0].asset;
|
|
97444
97540
|
const toToken = this.config.supportedPositions[1].asset;
|
|
97445
97541
|
const vaultAllocator = ContractAddr.from(
|
|
97446
97542
|
this.config.vaultAllocator.address
|
|
97447
97543
|
);
|
|
97448
|
-
console.log("vaultAllocator", vaultAllocator);
|
|
97449
97544
|
const quote = await this.getQuotesAvnu(
|
|
97450
97545
|
fromToken.address.toString(),
|
|
97451
97546
|
toToken.address.toString(),
|
|
@@ -97501,7 +97596,6 @@ spurious results.`);
|
|
|
97501
97596
|
const vaultAllocator = ContractAddr.from(
|
|
97502
97597
|
this.config.vaultAllocator.address
|
|
97503
97598
|
);
|
|
97504
|
-
console.log("params.amount", params.amount);
|
|
97505
97599
|
const quote = await this.getQuotesAvnu(
|
|
97506
97600
|
fromToken.address.toString(),
|
|
97507
97601
|
toToken.address.toString(),
|
|
@@ -97520,8 +97614,6 @@ spurious results.`);
|
|
|
97520
97614
|
);
|
|
97521
97615
|
const swapCallData = getCalldata[0];
|
|
97522
97616
|
const amount = uint256_exports.bnToUint256(params.amount.toWei());
|
|
97523
|
-
console.log("amount", amount);
|
|
97524
|
-
console.log("swapCallData", swapCallData);
|
|
97525
97617
|
return [
|
|
97526
97618
|
{
|
|
97527
97619
|
sanitizer: SIMPLE_SANITIZER,
|
|
@@ -97596,14 +97688,12 @@ spurious results.`);
|
|
|
97596
97688
|
return dataObject2;
|
|
97597
97689
|
}
|
|
97598
97690
|
const btcPrice = await this.getPriceOfToken(toToken.address.toString());
|
|
97599
|
-
console.log("btcPrice", btcPrice);
|
|
97600
97691
|
if (!btcPrice) {
|
|
97601
97692
|
logger2.error(`error getting btc price: ${btcPrice}`);
|
|
97602
97693
|
return null;
|
|
97603
97694
|
}
|
|
97604
97695
|
const estimatedUsdcAmount = Math.floor(amount * btcPrice);
|
|
97605
97696
|
const targetBtcBig = BigInt(returnFormattedAmount(amount, toTokenDecimals));
|
|
97606
|
-
console.log("targetBtcBig", targetBtcBig);
|
|
97607
97697
|
let low = BigInt(
|
|
97608
97698
|
Math.floor(
|
|
97609
97699
|
estimatedUsdcAmount * 10 ** fromToken.decimals * 0.9
|
|
@@ -97706,6 +97796,23 @@ spurious results.`);
|
|
|
97706
97796
|
debtPrice
|
|
97707
97797
|
};
|
|
97708
97798
|
}
|
|
97799
|
+
async getUnusedBalanceUSDCE() {
|
|
97800
|
+
const usdceToken = Global.getDefaultTokens().find(
|
|
97801
|
+
(token) => token.symbol === "USDCe"
|
|
97802
|
+
);
|
|
97803
|
+
const balance = await new ERC20(this.config).balanceOf(
|
|
97804
|
+
usdceToken.address,
|
|
97805
|
+
WALLET_ADDRESS,
|
|
97806
|
+
usdceToken.decimals
|
|
97807
|
+
);
|
|
97808
|
+
const price = await this.pricer.getPrice(usdceToken.symbol);
|
|
97809
|
+
const usdValue = Number(balance.toFixed(usdceToken.decimals)) * price.price;
|
|
97810
|
+
return {
|
|
97811
|
+
tokenInfo: usdceToken,
|
|
97812
|
+
amount: balance,
|
|
97813
|
+
usdValue
|
|
97814
|
+
};
|
|
97815
|
+
}
|
|
97709
97816
|
async getUnusedBalanceWBTC() {
|
|
97710
97817
|
const collateralToken = this.metadata.additionalInfo.borrowable_assets[0];
|
|
97711
97818
|
const balance = await new ERC20(this.config).balanceOf(
|
|
@@ -97751,22 +97858,30 @@ spurious results.`);
|
|
|
97751
97858
|
}
|
|
97752
97859
|
return extendedAdapter.adapter;
|
|
97753
97860
|
}
|
|
97754
|
-
async moveAssetsToVaultAllocator(amount) {
|
|
97861
|
+
async moveAssetsToVaultAllocator(amount, extendedAdapter) {
|
|
97755
97862
|
try {
|
|
97756
|
-
const
|
|
97757
|
-
(token) => token.symbol === "
|
|
97863
|
+
const usdceToken = Global.getDefaultTokens().find(
|
|
97864
|
+
(token) => token.symbol === "USDCe"
|
|
97758
97865
|
);
|
|
97759
97866
|
const approveCall = new ERC20(this.config).approve(
|
|
97760
|
-
|
|
97867
|
+
usdceToken.address,
|
|
97761
97868
|
this.metadata.additionalInfo.vaultAllocator,
|
|
97762
97869
|
amount
|
|
97763
97870
|
);
|
|
97764
97871
|
const transferCall = new ERC20(this.config).transfer(
|
|
97765
|
-
|
|
97872
|
+
usdceToken.address,
|
|
97766
97873
|
this.metadata.additionalInfo.vaultAllocator,
|
|
97767
97874
|
amount
|
|
97768
97875
|
);
|
|
97769
|
-
|
|
97876
|
+
const proofsInfo = extendedAdapter.getProofsForFromLegacySwap(
|
|
97877
|
+
this.getMerkleTree()
|
|
97878
|
+
);
|
|
97879
|
+
const proofGroups = proofsInfo.proofs;
|
|
97880
|
+
const call = this.getManageCall(
|
|
97881
|
+
proofGroups,
|
|
97882
|
+
await proofsInfo.callConstructor({ amount })
|
|
97883
|
+
);
|
|
97884
|
+
return [approveCall, transferCall, call];
|
|
97770
97885
|
} catch (err2) {
|
|
97771
97886
|
logger2.error(`error moving assets to vault allocator: ${err2}`);
|
|
97772
97887
|
return [];
|
|
@@ -97878,11 +97993,9 @@ spurious results.`);
|
|
|
97878
97993
|
);
|
|
97879
97994
|
return calls;
|
|
97880
97995
|
}
|
|
97881
|
-
console.log("extendedAmount", extendedAmount);
|
|
97882
|
-
console.log("vesuAmount", vesuAmount);
|
|
97883
97996
|
if (extendedAmount.lessThan(0)) {
|
|
97884
97997
|
try {
|
|
97885
|
-
const extendedCalls = await this.moveAssets(
|
|
97998
|
+
const { calls: extendedCalls, status: extendedStatus } = await this.moveAssets(
|
|
97886
97999
|
{
|
|
97887
98000
|
to: Protocols.VAULT.name,
|
|
97888
98001
|
from: Protocols.EXTENDED.name,
|
|
@@ -97891,14 +98004,18 @@ spurious results.`);
|
|
|
97891
98004
|
extendedAdapter,
|
|
97892
98005
|
vesuAdapter
|
|
97893
98006
|
);
|
|
97894
|
-
|
|
98007
|
+
if (extendedStatus) {
|
|
98008
|
+
calls.push(...extendedCalls);
|
|
98009
|
+
} else {
|
|
98010
|
+
return [];
|
|
98011
|
+
}
|
|
97895
98012
|
} catch (err2) {
|
|
97896
98013
|
logger2.error(`Failed moving assets to vault: ${err2}`);
|
|
97897
98014
|
}
|
|
97898
98015
|
}
|
|
97899
98016
|
if (vesuAmount.lessThan(0)) {
|
|
97900
98017
|
try {
|
|
97901
|
-
const vesuCalls = await this.moveAssets(
|
|
98018
|
+
const { calls: vesuCalls, status: vesuStatus } = await this.moveAssets(
|
|
97902
98019
|
{
|
|
97903
98020
|
to: Protocols.EXTENDED.name,
|
|
97904
98021
|
from: Protocols.VESU.name,
|
|
@@ -97908,6 +98025,9 @@ spurious results.`);
|
|
|
97908
98025
|
vesuAdapter
|
|
97909
98026
|
);
|
|
97910
98027
|
calls.push(...vesuCalls);
|
|
98028
|
+
if (!vesuStatus) {
|
|
98029
|
+
return [];
|
|
98030
|
+
}
|
|
97911
98031
|
} catch (err2) {
|
|
97912
98032
|
logger2.error(`Failed moving assets to vault: ${err2}`);
|
|
97913
98033
|
}
|
|
@@ -97923,7 +98043,7 @@ spurious results.`);
|
|
|
97923
98043
|
);
|
|
97924
98044
|
if (extendedAmount.minus(usdcAmountOnExtended).greaterThan(0)) {
|
|
97925
98045
|
try {
|
|
97926
|
-
const extendedCalls = await this.moveAssets(
|
|
98046
|
+
const { calls: extendedCalls } = await this.moveAssets(
|
|
97927
98047
|
{
|
|
97928
98048
|
to: Protocols.EXTENDED.name,
|
|
97929
98049
|
from: Protocols.VAULT.name,
|
|
@@ -97939,15 +98059,18 @@ spurious results.`);
|
|
|
97939
98059
|
}
|
|
97940
98060
|
if (vesuAmount.minus(usdcAmountInWallet).greaterThan(0)) {
|
|
97941
98061
|
try {
|
|
97942
|
-
const vesuCalls = await this.moveAssets(
|
|
98062
|
+
const { calls: vesuCalls, status: vesuStatus } = await this.moveAssets(
|
|
97943
98063
|
{
|
|
97944
|
-
to: Protocols.
|
|
98064
|
+
to: Protocols.VAULT.name,
|
|
97945
98065
|
from: Protocols.EXTENDED.name,
|
|
97946
98066
|
amount: vesuAmount.minus(usdcAmountInWallet)
|
|
97947
98067
|
},
|
|
97948
98068
|
extendedAdapter,
|
|
97949
98069
|
vesuAdapter
|
|
97950
98070
|
);
|
|
98071
|
+
if (!vesuStatus) {
|
|
98072
|
+
return [];
|
|
98073
|
+
}
|
|
97951
98074
|
calls.push(...vesuCalls);
|
|
97952
98075
|
} catch (err2) {
|
|
97953
98076
|
logger2.error(`Failed moving assets to vault: ${err2}`);
|
|
@@ -97964,9 +98087,12 @@ spurious results.`);
|
|
|
97964
98087
|
const avnuAdapter = await this.getAvnuAdapter();
|
|
97965
98088
|
if (!avnuAdapter) {
|
|
97966
98089
|
logger2.error(`avnu adapter not found: ${avnuAdapter}`);
|
|
97967
|
-
return
|
|
98090
|
+
return {
|
|
98091
|
+
calls: [],
|
|
98092
|
+
status: false
|
|
98093
|
+
};
|
|
97968
98094
|
}
|
|
97969
|
-
logger2.info(
|
|
98095
|
+
logger2.info(`moveAssets params, ${JSON.stringify(params)}`);
|
|
97970
98096
|
const collateralToken = vesuAdapter.config.supportedPositions[0].asset;
|
|
97971
98097
|
const {
|
|
97972
98098
|
collateralPrice
|
|
@@ -97983,18 +98109,58 @@ spurious results.`);
|
|
|
97983
98109
|
await proofsInfo.callConstructor({ amount: params.amount })
|
|
97984
98110
|
);
|
|
97985
98111
|
calls.push(call);
|
|
97986
|
-
return
|
|
98112
|
+
return {
|
|
98113
|
+
calls: [call],
|
|
98114
|
+
status: true
|
|
98115
|
+
};
|
|
97987
98116
|
} else if (params.to === Protocols.VAULT.name && params.from === Protocols.EXTENDED.name) {
|
|
98117
|
+
const extendedLeverage = calculateExtendedLevergae();
|
|
98118
|
+
const extendedHoldings = await extendedAdapter.getExtendedDepositAmount();
|
|
98119
|
+
if (!extendedHoldings) {
|
|
98120
|
+
logger2.error(`error getting extended holdings: ${extendedHoldings}`);
|
|
98121
|
+
return {
|
|
98122
|
+
calls: [],
|
|
98123
|
+
status: false
|
|
98124
|
+
};
|
|
98125
|
+
}
|
|
98126
|
+
const extendedHoldingAmount = new Web3Number(
|
|
98127
|
+
extendedHoldings.availableForWithdrawal,
|
|
98128
|
+
USDC_TOKEN_DECIMALS
|
|
98129
|
+
);
|
|
98130
|
+
const leftAmountAfterWithdrawalAmountInAccount = params.amount.minus(extendedHoldingAmount);
|
|
98131
|
+
const btcAmount = leftAmountAfterWithdrawalAmountInAccount.dividedBy(collateralPrice.price);
|
|
98132
|
+
const openLongPosition = await extendedAdapter.createOrder(
|
|
98133
|
+
extendedLeverage.toString(),
|
|
98134
|
+
btcAmount.toNumber(),
|
|
98135
|
+
"BUY" /* BUY */
|
|
98136
|
+
);
|
|
98137
|
+
if (!openLongPosition) {
|
|
98138
|
+
logger2.error(`error opening long position: ${openLongPosition}`);
|
|
98139
|
+
return {
|
|
98140
|
+
calls: [],
|
|
98141
|
+
status: false
|
|
98142
|
+
};
|
|
98143
|
+
}
|
|
98144
|
+
await new Promise((resolve) => setTimeout(resolve, 5e3));
|
|
97988
98145
|
const withdrawalFromExtended = await extendedAdapter.withdrawFromExtended(params.amount);
|
|
97989
98146
|
if (withdrawalFromExtended) {
|
|
97990
|
-
const
|
|
98147
|
+
const extendedHoldings2 = await extendedAdapter.getExtendedDepositAmount();
|
|
98148
|
+
logger2.info(`extendedHoldings after withdrawal ${extendedHoldings2}`);
|
|
98149
|
+
await new Promise((resolve) => setTimeout(resolve, 1e4));
|
|
98150
|
+
const calls = await this.moveAssetsToVaultAllocator(params.amount, extendedAdapter);
|
|
97991
98151
|
if (calls.length > 0) {
|
|
97992
|
-
return
|
|
98152
|
+
return {
|
|
98153
|
+
calls,
|
|
98154
|
+
status: true
|
|
98155
|
+
};
|
|
97993
98156
|
}
|
|
97994
98157
|
} else {
|
|
97995
98158
|
logger2.error("withdrawal from extended failed");
|
|
98159
|
+
return {
|
|
98160
|
+
calls: [],
|
|
98161
|
+
status: false
|
|
98162
|
+
};
|
|
97996
98163
|
}
|
|
97997
|
-
return [];
|
|
97998
98164
|
} else if (params.to === Protocols.VAULT.name && params.from === Protocols.VESU.name) {
|
|
97999
98165
|
const vesuAmountInBTC = new Web3Number(
|
|
98000
98166
|
params.amount.dividedBy(collateralPrice.price).toNumber(),
|
|
@@ -98015,7 +98181,10 @@ spurious results.`);
|
|
|
98015
98181
|
await swapProofsInfo.callConstructor({ amount: vesuAmountInBTC })
|
|
98016
98182
|
);
|
|
98017
98183
|
calls.push(swapCall);
|
|
98018
|
-
return
|
|
98184
|
+
return {
|
|
98185
|
+
calls,
|
|
98186
|
+
status: true
|
|
98187
|
+
};
|
|
98019
98188
|
} else if (params.to === Protocols.EXTENDED.name && params.from === Protocols.VESU.name) {
|
|
98020
98189
|
const vesuAmountInBTC = new Web3Number(
|
|
98021
98190
|
params.amount.dividedBy(collateralPrice.price).toNumber(),
|
|
@@ -98046,13 +98215,21 @@ spurious results.`);
|
|
|
98046
98215
|
await proofsInfoDeposit.callConstructor({ amount: params.amount })
|
|
98047
98216
|
);
|
|
98048
98217
|
calls.push(callDeposit);
|
|
98049
|
-
return
|
|
98218
|
+
return {
|
|
98219
|
+
calls,
|
|
98220
|
+
status: true
|
|
98221
|
+
};
|
|
98050
98222
|
}
|
|
98051
|
-
|
|
98052
|
-
|
|
98223
|
+
return {
|
|
98224
|
+
calls: [],
|
|
98225
|
+
status: false
|
|
98226
|
+
};
|
|
98053
98227
|
} catch (err2) {
|
|
98054
98228
|
logger2.error(`error moving assets: ${err2}`);
|
|
98055
|
-
return
|
|
98229
|
+
return {
|
|
98230
|
+
calls: [],
|
|
98231
|
+
status: false
|
|
98232
|
+
};
|
|
98056
98233
|
}
|
|
98057
98234
|
}
|
|
98058
98235
|
async handleDeposit() {
|
|
@@ -98111,8 +98288,7 @@ spurious results.`);
|
|
|
98111
98288
|
collateralTokenAmount,
|
|
98112
98289
|
collateralPrice.price
|
|
98113
98290
|
);
|
|
98114
|
-
|
|
98115
|
-
console.log("extendedAmountInBTC", extendedAmountInBTC);
|
|
98291
|
+
logger2.info(`vesuAmountInBTC ${vesuAmountInBTC}, extendedAmountInBTC ${extendedAmountInBTC}`);
|
|
98116
98292
|
let calls = [];
|
|
98117
98293
|
if (vesuAmountInBTC.greaterThan(MINIMUM_EXTENDED_POSITION_SIZE)) {
|
|
98118
98294
|
const proofsInfo = vesuAdapter.getProofs(true, this.getMerkleTree());
|
|
@@ -98125,7 +98301,7 @@ spurious results.`);
|
|
|
98125
98301
|
);
|
|
98126
98302
|
const { amount: wbtcAmountInVaultAllocator } = await this.getUnusedBalanceWBTC();
|
|
98127
98303
|
if (wbtcAmountInVaultAllocator.lessThan(vesuAmountInBTC)) {
|
|
98128
|
-
|
|
98304
|
+
logger2.info(`WBTC amount in vault allocator is less than vesu amount required in WBTC thus swapping, wbtcAmountInVaultAllocator: ${wbtcAmountInVaultAllocator}, vesuAmountInBTC: ${vesuAmountInBTC}`);
|
|
98129
98305
|
const swapProofsInfo = avnuAdapter.getProofs(true, this.getMerkleTree());
|
|
98130
98306
|
const swapProofGroups = swapProofsInfo.proofs;
|
|
98131
98307
|
const swapCall = this.getManageCall(
|
|
@@ -98169,7 +98345,7 @@ spurious results.`);
|
|
|
98169
98345
|
bid
|
|
98170
98346
|
} = await extendedAdapter.fetchOrderBookBTCUSDC();
|
|
98171
98347
|
const price = ask.plus(bid).dividedBy(2);
|
|
98172
|
-
const btcToken = vesuAdapter.config.supportedPositions[
|
|
98348
|
+
const btcToken = vesuAdapter.config.supportedPositions[0].asset;
|
|
98173
98349
|
const btcPriceAvnu = await avnuAdapter.getPriceOfToken(btcToken.address.toString());
|
|
98174
98350
|
if (!btcPriceAvnu) {
|
|
98175
98351
|
logger2.error(`error getting btc price avnu: ${btcPriceAvnu}`);
|
|
@@ -98185,43 +98361,65 @@ spurious results.`);
|
|
|
98185
98361
|
async handleWithdraw(amount) {
|
|
98186
98362
|
try {
|
|
98187
98363
|
const usdcBalanceVaultAllocator = await this.getUnusedBalance();
|
|
98188
|
-
const usdcBalanceDifference = amount.minus(usdcBalanceVaultAllocator.usdValue);
|
|
98364
|
+
const usdcBalanceDifference = amount.plus(BUFFER_USDC_IN_WITHDRAWAL).minus(usdcBalanceVaultAllocator.usdValue);
|
|
98189
98365
|
logger2.info(`usdcBalanceDifference, ${usdcBalanceDifference.toNumber()}`);
|
|
98366
|
+
let calls = [];
|
|
98367
|
+
let status = true;
|
|
98190
98368
|
if (usdcBalanceDifference.lessThan(0)) {
|
|
98191
|
-
const
|
|
98192
|
-
amount
|
|
98369
|
+
const withdrawCall2 = await this.getBringLiquidityCall({
|
|
98370
|
+
amount: usdcBalanceVaultAllocator.amount
|
|
98193
98371
|
});
|
|
98194
|
-
logger2.info("withdraw call",
|
|
98195
|
-
|
|
98372
|
+
logger2.info("withdraw call", withdrawCall2);
|
|
98373
|
+
calls.push(withdrawCall2);
|
|
98374
|
+
return {
|
|
98375
|
+
calls,
|
|
98376
|
+
status: true
|
|
98377
|
+
};
|
|
98196
98378
|
}
|
|
98197
98379
|
const vesuAdapter = await this.getVesuAdapter();
|
|
98198
98380
|
const extendedAdapter = await this.getExtendedAdapter();
|
|
98199
98381
|
if (!vesuAdapter || !extendedAdapter || !extendedAdapter.client) {
|
|
98382
|
+
status = false;
|
|
98200
98383
|
logger2.error(
|
|
98201
98384
|
`vesu or extended adapter not found: vesuAdapter=${vesuAdapter}, extendedAdapter=${extendedAdapter}`
|
|
98202
98385
|
);
|
|
98203
|
-
return
|
|
98386
|
+
return {
|
|
98387
|
+
calls,
|
|
98388
|
+
status
|
|
98389
|
+
};
|
|
98204
98390
|
}
|
|
98205
98391
|
const { collateralTokenAmount } = await vesuAdapter.vesuAdapter.getAssetPrices();
|
|
98206
98392
|
const {
|
|
98207
98393
|
collateralPrice
|
|
98208
98394
|
} = await this.getAssetPrices();
|
|
98209
98395
|
const extendedPositon = await extendedAdapter.getAllOpenPositions();
|
|
98396
|
+
if (!extendedPositon) {
|
|
98397
|
+
status = false;
|
|
98398
|
+
logger2.error("error getting extended position", extendedPositon);
|
|
98399
|
+
return {
|
|
98400
|
+
calls,
|
|
98401
|
+
status
|
|
98402
|
+
};
|
|
98403
|
+
}
|
|
98210
98404
|
const amountDistributionForWithdrawal = await calculateAmountDistributionForWithdrawal(
|
|
98211
|
-
|
|
98405
|
+
usdcBalanceDifference,
|
|
98212
98406
|
collateralPrice.price,
|
|
98213
98407
|
collateralTokenAmount,
|
|
98214
98408
|
extendedPositon
|
|
98215
98409
|
);
|
|
98216
98410
|
if (!amountDistributionForWithdrawal) {
|
|
98411
|
+
status = false;
|
|
98217
98412
|
logger2.error(
|
|
98218
98413
|
`error calculating amount distribution for withdrawal: ${amountDistributionForWithdrawal}`
|
|
98219
98414
|
);
|
|
98220
|
-
return
|
|
98415
|
+
return {
|
|
98416
|
+
calls,
|
|
98417
|
+
status
|
|
98418
|
+
};
|
|
98221
98419
|
}
|
|
98222
98420
|
const { vesu_amount, extended_amount } = amountDistributionForWithdrawal;
|
|
98223
|
-
if (vesu_amount.greaterThan(0)) {
|
|
98224
|
-
const
|
|
98421
|
+
if (status && vesu_amount.greaterThan(0)) {
|
|
98422
|
+
const { calls: vesuCalls, status: vesuStatus } = await this.moveAssets(
|
|
98225
98423
|
{
|
|
98226
98424
|
amount: vesu_amount,
|
|
98227
98425
|
from: Protocols.VESU.name,
|
|
@@ -98230,10 +98428,11 @@ spurious results.`);
|
|
|
98230
98428
|
extendedAdapter,
|
|
98231
98429
|
vesuAdapter
|
|
98232
98430
|
);
|
|
98233
|
-
|
|
98431
|
+
status = vesuStatus;
|
|
98432
|
+
calls.push(...vesuCalls);
|
|
98234
98433
|
}
|
|
98235
|
-
if (extended_amount.greaterThan(0)) {
|
|
98236
|
-
const
|
|
98434
|
+
if (status && extended_amount.greaterThan(0)) {
|
|
98435
|
+
const { calls: extendedCalls, status: extendedStatus } = await this.moveAssets(
|
|
98237
98436
|
{
|
|
98238
98437
|
amount: extended_amount,
|
|
98239
98438
|
from: Protocols.EXTENDED.name,
|
|
@@ -98242,12 +98441,32 @@ spurious results.`);
|
|
|
98242
98441
|
extendedAdapter,
|
|
98243
98442
|
vesuAdapter
|
|
98244
98443
|
);
|
|
98245
|
-
|
|
98444
|
+
status = extendedStatus;
|
|
98445
|
+
if (status) {
|
|
98446
|
+
calls.push(...extendedCalls);
|
|
98447
|
+
} else {
|
|
98448
|
+
logger2.error("error moving assets to vault: extendedStatus: ${extendedStatus}");
|
|
98449
|
+
return {
|
|
98450
|
+
calls: [],
|
|
98451
|
+
status
|
|
98452
|
+
};
|
|
98453
|
+
}
|
|
98246
98454
|
}
|
|
98247
|
-
|
|
98455
|
+
const withdrawCall = await this.getBringLiquidityCall({
|
|
98456
|
+
amount
|
|
98457
|
+
});
|
|
98458
|
+
logger2.info("withdraw call", withdrawCall);
|
|
98459
|
+
calls.push(withdrawCall);
|
|
98460
|
+
return {
|
|
98461
|
+
calls,
|
|
98462
|
+
status
|
|
98463
|
+
};
|
|
98248
98464
|
} catch (err2) {
|
|
98249
98465
|
logger2.error(`error handling withdrawal: ${err2}`);
|
|
98250
|
-
return
|
|
98466
|
+
return {
|
|
98467
|
+
calls: [],
|
|
98468
|
+
status: false
|
|
98469
|
+
};
|
|
98251
98470
|
}
|
|
98252
98471
|
}
|
|
98253
98472
|
async getAUM() {
|
|
@@ -98381,6 +98600,7 @@ spurious results.`);
|
|
|
98381
98600
|
() => vesuMultiplyAdapter.getWithdrawLeaf()
|
|
98382
98601
|
);
|
|
98383
98602
|
vaultSettings.leafAdapters.push(() => extendedAdapter.getDepositLeaf());
|
|
98603
|
+
vaultSettings.leafAdapters.push(() => extendedAdapter.getSwapFromLegacyLeaf());
|
|
98384
98604
|
vaultSettings.leafAdapters.push(() => avnuAdapter.getDepositLeaf());
|
|
98385
98605
|
vaultSettings.leafAdapters.push(() => avnuAdapter.getWithdrawLeaf());
|
|
98386
98606
|
vaultSettings.leafAdapters.push(
|