@strkfarm/sdk 2.0.0-dev.13 → 2.0.0-dev.15
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 +557 -556
- package/dist/index.browser.mjs +1333 -1332
- package/dist/index.d.ts +78 -28
- package/dist/index.js +565 -562
- package/dist/index.mjs +1333 -1332
- package/package.json +1 -1
- package/src/strategies/index.ts +2 -1
- package/src/strategies/universal-adapters/extended-adapter.ts +40 -11
- package/src/strategies/universal-adapters/index.ts +2 -1
- package/src/strategies/vesu-extended-strategy/services/operationService.ts +23 -13
- package/src/strategies/vesu-extended-strategy/types/transaction-metadata.ts +25 -0
- package/src/strategies/vesu-extended-strategy/utils/helper.ts +40 -0
- package/src/strategies/vesu-extended-strategy/vesu-extended-strategy.tsx +203 -235
|
@@ -28653,6 +28653,7 @@ ${r2}}` : "}", l2;
|
|
|
28653
28653
|
AssetOperationStatus: () => AssetOperationStatus,
|
|
28654
28654
|
AssetOperationType: () => AssetOperationType,
|
|
28655
28655
|
AutoCompounderSTRK: () => AutoCompounderSTRK,
|
|
28656
|
+
AvnuAdapter: () => AvnuAdapter,
|
|
28656
28657
|
AvnuWrapper: () => AvnuWrapper,
|
|
28657
28658
|
BaseAdapter: () => BaseAdapter,
|
|
28658
28659
|
BaseStrategy: () => BaseStrategy,
|
|
@@ -28728,6 +28729,7 @@ ${r2}}` : "}", l2;
|
|
|
28728
28729
|
calculateExtendedLevergae: () => calculateExtendedLevergae,
|
|
28729
28730
|
calculateVesUPositionSizeGivenExtended: () => calculateVesUPositionSizeGivenExtended,
|
|
28730
28731
|
calculateVesuLeverage: () => calculateVesuLeverage,
|
|
28732
|
+
calculateWBTCAmountToMaintainLTV: () => calculateWBTCAmountToMaintainLTV,
|
|
28731
28733
|
extensionMap: () => extensionMap,
|
|
28732
28734
|
getContractDetails: () => getContractDetails,
|
|
28733
28735
|
getFAQs: () => getFAQs,
|
|
@@ -92580,6 +92582,21 @@ spurious results.`);
|
|
|
92580
92582
|
return null;
|
|
92581
92583
|
}
|
|
92582
92584
|
};
|
|
92585
|
+
var calculateWBTCAmountToMaintainLTV = (collateralAmount, debtAmount, debtPrice, maxLtv = MAX_LIQUIDATION_RATIO, collateralPrice, targetHF = TARGET_HF) => {
|
|
92586
|
+
try {
|
|
92587
|
+
const numerator1 = collateralAmount.multipliedBy(collateralPrice).multipliedBy(maxLtv);
|
|
92588
|
+
const numerator2 = debtAmount.multipliedBy(debtPrice).multipliedBy(targetHF);
|
|
92589
|
+
const denominator = maxLtv;
|
|
92590
|
+
const collateralAmountToMaintainLTV = numerator2.minus(numerator1).dividedBy(denominator);
|
|
92591
|
+
let deltaCollateralAmountUnits = new Web3Number(
|
|
92592
|
+
collateralAmountToMaintainLTV.dividedBy(collateralPrice).toFixed(WBTC_TOKEN_DECIMALS),
|
|
92593
|
+
WBTC_TOKEN_DECIMALS
|
|
92594
|
+
);
|
|
92595
|
+
return { deltaCollateralAmountUnits };
|
|
92596
|
+
} catch (err2) {
|
|
92597
|
+
return { deltaCollateralAmountUnits: null };
|
|
92598
|
+
}
|
|
92599
|
+
};
|
|
92583
92600
|
var calculateExposureDelta = (exposure_extended, exposure_vesu) => {
|
|
92584
92601
|
const exposure_delta = new Web3Number(exposure_extended - exposure_vesu, 2);
|
|
92585
92602
|
return exposure_delta.absoluteValue().toNumber();
|
|
@@ -93964,26 +93981,38 @@ spurious results.`);
|
|
|
93964
93981
|
try {
|
|
93965
93982
|
if (!this.client) {
|
|
93966
93983
|
logger2.error("Client not initialized");
|
|
93967
|
-
return
|
|
93984
|
+
return {
|
|
93985
|
+
status: false,
|
|
93986
|
+
receivedTxnHash: false
|
|
93987
|
+
};
|
|
93968
93988
|
}
|
|
93969
93989
|
if (amount.lessThanOrEqualTo(0)) {
|
|
93970
93990
|
logger2.error(
|
|
93971
93991
|
`Invalid withdrawal amount: ${amount.toNumber()}. Amount must be positive.`
|
|
93972
93992
|
);
|
|
93973
|
-
return
|
|
93993
|
+
return {
|
|
93994
|
+
status: false,
|
|
93995
|
+
receivedTxnHash: false
|
|
93996
|
+
};
|
|
93974
93997
|
}
|
|
93975
93998
|
if (amount.lessThanOrEqualTo(this.minimumExtendedMovementAmount)) {
|
|
93976
93999
|
logger2.warn(
|
|
93977
94000
|
`Withdrawal amount ${amount.toNumber()} is below minimum Extended movement amount ${this.minimumExtendedMovementAmount}. Skipping withdrawal.`
|
|
93978
94001
|
);
|
|
93979
|
-
return
|
|
94002
|
+
return {
|
|
94003
|
+
status: false,
|
|
94004
|
+
receivedTxnHash: false
|
|
94005
|
+
};
|
|
93980
94006
|
}
|
|
93981
94007
|
const holdings = await this.getExtendedDepositAmount();
|
|
93982
94008
|
if (!holdings) {
|
|
93983
94009
|
logger2.error(
|
|
93984
94010
|
"Cannot get holdings - unable to validate withdrawal amount"
|
|
93985
94011
|
);
|
|
93986
|
-
return
|
|
94012
|
+
return {
|
|
94013
|
+
status: false,
|
|
94014
|
+
receivedTxnHash: false
|
|
94015
|
+
};
|
|
93987
94016
|
}
|
|
93988
94017
|
const availableForWithdrawal = parseFloat(
|
|
93989
94018
|
holdings.availableForWithdrawal
|
|
@@ -93992,14 +94021,20 @@ spurious results.`);
|
|
|
93992
94021
|
logger2.error(
|
|
93993
94022
|
`Invalid availableForWithdrawal: ${holdings.availableForWithdrawal}. Expected a finite, non-negative number.`
|
|
93994
94023
|
);
|
|
93995
|
-
return
|
|
94024
|
+
return {
|
|
94025
|
+
status: false,
|
|
94026
|
+
receivedTxnHash: false
|
|
94027
|
+
};
|
|
93996
94028
|
}
|
|
93997
94029
|
const withdrawalAmount = amount.toNumber();
|
|
93998
94030
|
if (withdrawalAmount > availableForWithdrawal) {
|
|
93999
94031
|
logger2.error(
|
|
94000
94032
|
`Withdrawal amount ${withdrawalAmount} exceeds available balance ${availableForWithdrawal}`
|
|
94001
94033
|
);
|
|
94002
|
-
return
|
|
94034
|
+
return {
|
|
94035
|
+
status: false,
|
|
94036
|
+
receivedTxnHash: false
|
|
94037
|
+
};
|
|
94003
94038
|
}
|
|
94004
94039
|
logger2.info(
|
|
94005
94040
|
`Withdrawing ${withdrawalAmount} from Extended. Available balance: ${availableForWithdrawal}`
|
|
@@ -94012,15 +94047,24 @@ spurious results.`);
|
|
|
94012
94047
|
withdrawalRequest.data,
|
|
94013
94048
|
"WITHDRAWAL" /* WITHDRAWAL */
|
|
94014
94049
|
);
|
|
94015
|
-
return
|
|
94050
|
+
return {
|
|
94051
|
+
status: true,
|
|
94052
|
+
receivedTxnHash: withdrawalStatus
|
|
94053
|
+
};
|
|
94016
94054
|
}
|
|
94017
94055
|
logger2.error(
|
|
94018
94056
|
`Withdrawal request failed with status: ${withdrawalRequest.status}`
|
|
94019
94057
|
);
|
|
94020
|
-
return
|
|
94058
|
+
return {
|
|
94059
|
+
status: false,
|
|
94060
|
+
receivedTxnHash: false
|
|
94061
|
+
};
|
|
94021
94062
|
} catch (error2) {
|
|
94022
94063
|
logger2.error(`Error creating Withdraw Call: ${error2}`);
|
|
94023
|
-
return
|
|
94064
|
+
return {
|
|
94065
|
+
status: false,
|
|
94066
|
+
receivedTxnHash: false
|
|
94067
|
+
};
|
|
94024
94068
|
}
|
|
94025
94069
|
}
|
|
94026
94070
|
async getHealthFactor() {
|
|
@@ -94506,6 +94550,334 @@ spurious results.`);
|
|
|
94506
94550
|
}
|
|
94507
94551
|
};
|
|
94508
94552
|
|
|
94553
|
+
// src/strategies/universal-adapters/avnu-adapter.ts
|
|
94554
|
+
var AvnuAdapter = class _AvnuAdapter extends BaseAdapter {
|
|
94555
|
+
constructor(config3) {
|
|
94556
|
+
super(config3, _AvnuAdapter.name, Protocols.AVNU);
|
|
94557
|
+
this.config = config3;
|
|
94558
|
+
this.avnuWrapper = new AvnuWrapper();
|
|
94559
|
+
}
|
|
94560
|
+
//abstract means the method has no implementation in this class; instead, child classes must implement it.
|
|
94561
|
+
async getAPY(supportedPosition) {
|
|
94562
|
+
return Promise.resolve({ apy: 0, type: "base" /* BASE */ });
|
|
94563
|
+
}
|
|
94564
|
+
async getPosition(supportedPosition) {
|
|
94565
|
+
return Promise.resolve({ amount: new Web3Number(0, 0), remarks: "" });
|
|
94566
|
+
}
|
|
94567
|
+
async maxDeposit(amount) {
|
|
94568
|
+
return Promise.resolve({
|
|
94569
|
+
tokenInfo: this.config.baseToken,
|
|
94570
|
+
amount: new Web3Number(0, 0),
|
|
94571
|
+
usdValue: 0,
|
|
94572
|
+
apy: { apy: 0, type: "base" /* BASE */ },
|
|
94573
|
+
protocol: Protocols.AVNU,
|
|
94574
|
+
remarks: ""
|
|
94575
|
+
});
|
|
94576
|
+
}
|
|
94577
|
+
async maxWithdraw() {
|
|
94578
|
+
return Promise.resolve({
|
|
94579
|
+
tokenInfo: this.config.baseToken,
|
|
94580
|
+
amount: new Web3Number(0, 0),
|
|
94581
|
+
usdValue: 0,
|
|
94582
|
+
apy: { apy: 0, type: "base" /* BASE */ },
|
|
94583
|
+
protocol: Protocols.AVNU,
|
|
94584
|
+
remarks: ""
|
|
94585
|
+
});
|
|
94586
|
+
}
|
|
94587
|
+
_getDepositLeaf() {
|
|
94588
|
+
const vaultAllocator = ContractAddr.from(
|
|
94589
|
+
this.config.vaultAllocator.address
|
|
94590
|
+
);
|
|
94591
|
+
return [
|
|
94592
|
+
{
|
|
94593
|
+
target: this.config.supportedPositions[0].asset.address,
|
|
94594
|
+
method: "approve",
|
|
94595
|
+
packedArguments: [
|
|
94596
|
+
AVNU_EXCHANGE.toBigInt()
|
|
94597
|
+
],
|
|
94598
|
+
sanitizer: SIMPLE_SANITIZER,
|
|
94599
|
+
id: `approve_${this.config.supportedPositions[0].asset.symbol}`
|
|
94600
|
+
},
|
|
94601
|
+
{
|
|
94602
|
+
target: AVNU_EXCHANGE,
|
|
94603
|
+
method: "multi_route_swap",
|
|
94604
|
+
packedArguments: [
|
|
94605
|
+
this.config.supportedPositions[0].asset.address.toBigInt(),
|
|
94606
|
+
//usdc
|
|
94607
|
+
this.config.supportedPositions[1].asset.address.toBigInt(),
|
|
94608
|
+
//wbtc
|
|
94609
|
+
vaultAllocator.toBigInt()
|
|
94610
|
+
],
|
|
94611
|
+
sanitizer: SIMPLE_SANITIZER,
|
|
94612
|
+
id: `asutb_${this.config.supportedPositions[0].asset.symbol}_${this.config.supportedPositions[1].asset.symbol}`
|
|
94613
|
+
}
|
|
94614
|
+
];
|
|
94615
|
+
}
|
|
94616
|
+
_getWithdrawLeaf() {
|
|
94617
|
+
const vaultAllocator = ContractAddr.from(
|
|
94618
|
+
this.config.vaultAllocator.address
|
|
94619
|
+
);
|
|
94620
|
+
const toToken = this.config.supportedPositions[0].asset;
|
|
94621
|
+
const fromToken = this.config.supportedPositions[1].asset;
|
|
94622
|
+
return [
|
|
94623
|
+
{
|
|
94624
|
+
target: fromToken.address,
|
|
94625
|
+
method: "approve",
|
|
94626
|
+
packedArguments: [
|
|
94627
|
+
AVNU_EXCHANGE.toBigInt()
|
|
94628
|
+
],
|
|
94629
|
+
sanitizer: SIMPLE_SANITIZER,
|
|
94630
|
+
id: `approve_${fromToken.symbol}`
|
|
94631
|
+
},
|
|
94632
|
+
{
|
|
94633
|
+
target: AVNU_EXCHANGE,
|
|
94634
|
+
method: "multi_route_swap",
|
|
94635
|
+
packedArguments: [
|
|
94636
|
+
fromToken.address.toBigInt(),
|
|
94637
|
+
//wbtc
|
|
94638
|
+
toToken.address.toBigInt(),
|
|
94639
|
+
//usdc
|
|
94640
|
+
vaultAllocator.toBigInt()
|
|
94641
|
+
],
|
|
94642
|
+
sanitizer: SIMPLE_SANITIZER,
|
|
94643
|
+
id: `asbtu_${fromToken.symbol}_${fromToken.symbol}`
|
|
94644
|
+
}
|
|
94645
|
+
];
|
|
94646
|
+
}
|
|
94647
|
+
_getLegacySwapLeaf() {
|
|
94648
|
+
return [];
|
|
94649
|
+
}
|
|
94650
|
+
async getDepositCall(params) {
|
|
94651
|
+
try {
|
|
94652
|
+
const fromToken = this.config.supportedPositions[0].asset;
|
|
94653
|
+
const toToken = this.config.supportedPositions[1].asset;
|
|
94654
|
+
const vaultAllocator = ContractAddr.from(
|
|
94655
|
+
this.config.vaultAllocator.address
|
|
94656
|
+
);
|
|
94657
|
+
const quote = await this.getQuotesAvnu(
|
|
94658
|
+
fromToken.address.toString(),
|
|
94659
|
+
toToken.address.toString(),
|
|
94660
|
+
params.amount.toNumber(),
|
|
94661
|
+
vaultAllocator.address.toString(),
|
|
94662
|
+
toToken.decimals,
|
|
94663
|
+
true
|
|
94664
|
+
);
|
|
94665
|
+
if (!quote) {
|
|
94666
|
+
logger2.error("error getting quote from avnu");
|
|
94667
|
+
return [];
|
|
94668
|
+
}
|
|
94669
|
+
const getCalldata = await this.avnuWrapper.getSwapCallData(
|
|
94670
|
+
quote,
|
|
94671
|
+
vaultAllocator.address
|
|
94672
|
+
);
|
|
94673
|
+
const swapCallData = getCalldata[0];
|
|
94674
|
+
const amount = uint256_exports.bnToUint256(quote.sellAmountInUsd * 10 ** 7);
|
|
94675
|
+
return [
|
|
94676
|
+
{
|
|
94677
|
+
sanitizer: SIMPLE_SANITIZER,
|
|
94678
|
+
call: {
|
|
94679
|
+
contractAddress: fromToken.address,
|
|
94680
|
+
selector: hash_exports.getSelectorFromName("approve"),
|
|
94681
|
+
calldata: [
|
|
94682
|
+
AVNU_EXCHANGE.toBigInt(),
|
|
94683
|
+
toBigInt3(amount.low.toString()),
|
|
94684
|
+
// amount low
|
|
94685
|
+
toBigInt3(amount.high.toString())
|
|
94686
|
+
// amount high
|
|
94687
|
+
]
|
|
94688
|
+
}
|
|
94689
|
+
},
|
|
94690
|
+
{
|
|
94691
|
+
sanitizer: SIMPLE_SANITIZER,
|
|
94692
|
+
call: {
|
|
94693
|
+
contractAddress: AVNU_EXCHANGE,
|
|
94694
|
+
selector: hash_exports.getSelectorFromName("multi_route_swap"),
|
|
94695
|
+
calldata: swapCallData
|
|
94696
|
+
}
|
|
94697
|
+
}
|
|
94698
|
+
];
|
|
94699
|
+
} catch (error2) {
|
|
94700
|
+
logger2.error(`Error getting Avnu quote: ${error2}`);
|
|
94701
|
+
return [];
|
|
94702
|
+
}
|
|
94703
|
+
}
|
|
94704
|
+
//Swap wbtc to usdc
|
|
94705
|
+
async getWithdrawCall(params) {
|
|
94706
|
+
try {
|
|
94707
|
+
const toToken = this.config.supportedPositions[0].asset;
|
|
94708
|
+
const fromToken = this.config.supportedPositions[1].asset;
|
|
94709
|
+
const vaultAllocator = ContractAddr.from(
|
|
94710
|
+
this.config.vaultAllocator.address
|
|
94711
|
+
);
|
|
94712
|
+
const quote = await this.getQuotesAvnu(
|
|
94713
|
+
fromToken.address.toString(),
|
|
94714
|
+
toToken.address.toString(),
|
|
94715
|
+
params.amount.toNumber(),
|
|
94716
|
+
vaultAllocator.address.toString(),
|
|
94717
|
+
fromToken.decimals,
|
|
94718
|
+
false
|
|
94719
|
+
);
|
|
94720
|
+
if (!quote) {
|
|
94721
|
+
logger2.error("No quotes available for this swap, error in quotes avnu");
|
|
94722
|
+
return [];
|
|
94723
|
+
}
|
|
94724
|
+
const getCalldata = await this.avnuWrapper.getSwapCallData(
|
|
94725
|
+
quote,
|
|
94726
|
+
vaultAllocator.address
|
|
94727
|
+
);
|
|
94728
|
+
const swapCallData = getCalldata[0];
|
|
94729
|
+
const amount = uint256_exports.bnToUint256(params.amount.toWei());
|
|
94730
|
+
return [
|
|
94731
|
+
{
|
|
94732
|
+
sanitizer: SIMPLE_SANITIZER,
|
|
94733
|
+
call: {
|
|
94734
|
+
contractAddress: fromToken.address,
|
|
94735
|
+
selector: hash_exports.getSelectorFromName("approve"),
|
|
94736
|
+
calldata: [
|
|
94737
|
+
AVNU_EXCHANGE.toBigInt(),
|
|
94738
|
+
toBigInt3(amount.low.toString()),
|
|
94739
|
+
// amount low
|
|
94740
|
+
toBigInt3(amount.high.toString())
|
|
94741
|
+
// amount high
|
|
94742
|
+
]
|
|
94743
|
+
}
|
|
94744
|
+
},
|
|
94745
|
+
{
|
|
94746
|
+
sanitizer: SIMPLE_SANITIZER,
|
|
94747
|
+
call: {
|
|
94748
|
+
contractAddress: AVNU_EXCHANGE,
|
|
94749
|
+
selector: hash_exports.getSelectorFromName("multi_route_swap"),
|
|
94750
|
+
calldata: swapCallData
|
|
94751
|
+
}
|
|
94752
|
+
}
|
|
94753
|
+
];
|
|
94754
|
+
} catch (error2) {
|
|
94755
|
+
logger2.error(`Error getting Avnu quote: ${error2}`);
|
|
94756
|
+
return [];
|
|
94757
|
+
}
|
|
94758
|
+
}
|
|
94759
|
+
async getSwapCallData(quote) {
|
|
94760
|
+
return await this.avnuWrapper.getSwapCallData(quote, this.config.vaultAllocator.address);
|
|
94761
|
+
}
|
|
94762
|
+
async getHealthFactor() {
|
|
94763
|
+
return Promise.resolve(1);
|
|
94764
|
+
}
|
|
94765
|
+
async fetchQuoteWithRetry(params, retries = 5) {
|
|
94766
|
+
for (let attempt = 0; attempt < retries; attempt++) {
|
|
94767
|
+
try {
|
|
94768
|
+
const response = await axios_default.get(this.config.baseUrl, { params });
|
|
94769
|
+
if (response.data && response.data.length > 0) {
|
|
94770
|
+
return response;
|
|
94771
|
+
}
|
|
94772
|
+
throw new Error("Empty response data");
|
|
94773
|
+
} catch (err2) {
|
|
94774
|
+
logger2.error(`Error fetching quote with retry: ${err2}`);
|
|
94775
|
+
if (attempt === retries - 1) {
|
|
94776
|
+
throw err2;
|
|
94777
|
+
}
|
|
94778
|
+
await new Promise((resolve) => setTimeout(resolve, MAX_DELAY));
|
|
94779
|
+
}
|
|
94780
|
+
}
|
|
94781
|
+
throw new Error("Failed to fetch quote after retries");
|
|
94782
|
+
}
|
|
94783
|
+
async getQuotesAvnu(from_token_address, to_token_address, amount, takerAddress, toTokenDecimals, usdcToBtc, maxIterations = 5, tolerance = 5e3) {
|
|
94784
|
+
try {
|
|
94785
|
+
const fromToken = this.config.supportedPositions[0].asset;
|
|
94786
|
+
const toToken = this.config.supportedPositions[1].asset;
|
|
94787
|
+
if (!usdcToBtc) {
|
|
94788
|
+
const sellAmount2 = returnFormattedAmount(amount, toTokenDecimals);
|
|
94789
|
+
const params2 = {
|
|
94790
|
+
sellTokenAddress: from_token_address,
|
|
94791
|
+
buyTokenAddress: to_token_address,
|
|
94792
|
+
takerAddress,
|
|
94793
|
+
sellAmount: sellAmount2
|
|
94794
|
+
};
|
|
94795
|
+
const finalQuote2 = await this.fetchQuoteWithRetry(params2);
|
|
94796
|
+
if (!finalQuote2.data.length) {
|
|
94797
|
+
logger2.error("No quotes available for this swap, error in quotes avnu");
|
|
94798
|
+
return null;
|
|
94799
|
+
}
|
|
94800
|
+
const dataObject2 = finalQuote2.data[0];
|
|
94801
|
+
return dataObject2;
|
|
94802
|
+
}
|
|
94803
|
+
const btcPrice = await this.getPriceOfToken(toToken.address.toString());
|
|
94804
|
+
if (!btcPrice) {
|
|
94805
|
+
logger2.error(`error getting btc price: ${btcPrice}`);
|
|
94806
|
+
return null;
|
|
94807
|
+
}
|
|
94808
|
+
const estimatedUsdcAmount = Math.floor(amount * btcPrice);
|
|
94809
|
+
const targetBtcBig = BigInt(returnFormattedAmount(amount, toTokenDecimals));
|
|
94810
|
+
let low = BigInt(
|
|
94811
|
+
Math.floor(
|
|
94812
|
+
estimatedUsdcAmount * 10 ** fromToken.decimals * 0.9
|
|
94813
|
+
)
|
|
94814
|
+
);
|
|
94815
|
+
let high = BigInt(
|
|
94816
|
+
Math.floor(
|
|
94817
|
+
estimatedUsdcAmount * 10 ** fromToken.decimals * 1.1
|
|
94818
|
+
)
|
|
94819
|
+
);
|
|
94820
|
+
let mid = 0n;
|
|
94821
|
+
for (let i = 0; i < maxIterations; i++) {
|
|
94822
|
+
mid = (low + high) / 2n;
|
|
94823
|
+
const sellAmount2 = returnFormattedAmount(Number(mid), 0);
|
|
94824
|
+
const quote = await this.fetchQuoteWithRetry({
|
|
94825
|
+
sellTokenAddress: from_token_address,
|
|
94826
|
+
buyTokenAddress: to_token_address,
|
|
94827
|
+
takerAddress,
|
|
94828
|
+
sellAmount: sellAmount2
|
|
94829
|
+
});
|
|
94830
|
+
const gotBtc = BigInt(quote.data[0].buyAmount);
|
|
94831
|
+
if (gotBtc === targetBtcBig) return quote.data[0];
|
|
94832
|
+
if (gotBtc > targetBtcBig) {
|
|
94833
|
+
high = mid;
|
|
94834
|
+
} else {
|
|
94835
|
+
low = mid;
|
|
94836
|
+
}
|
|
94837
|
+
if (gotBtc >= targetBtcBig && gotBtc <= targetBtcBig + BigInt(tolerance)) {
|
|
94838
|
+
return quote.data[0];
|
|
94839
|
+
}
|
|
94840
|
+
}
|
|
94841
|
+
let sellAmount = returnFormattedAmount(
|
|
94842
|
+
Number(mid),
|
|
94843
|
+
0
|
|
94844
|
+
);
|
|
94845
|
+
const params = {
|
|
94846
|
+
sellTokenAddress: from_token_address,
|
|
94847
|
+
buyTokenAddress: to_token_address,
|
|
94848
|
+
takerAddress,
|
|
94849
|
+
sellAmount
|
|
94850
|
+
};
|
|
94851
|
+
const finalQuote = await this.fetchQuoteWithRetry(params);
|
|
94852
|
+
if (!finalQuote.data.length) {
|
|
94853
|
+
logger2.error("No quotes available for this swap, error in quotes avnu");
|
|
94854
|
+
return null;
|
|
94855
|
+
}
|
|
94856
|
+
const dataObject = finalQuote.data[0];
|
|
94857
|
+
return dataObject;
|
|
94858
|
+
} catch (err2) {
|
|
94859
|
+
logger2.error(`No quotes available for this swap: ${err2}`);
|
|
94860
|
+
return null;
|
|
94861
|
+
}
|
|
94862
|
+
}
|
|
94863
|
+
async getPriceOfToken(tokenAddress, retries = MAX_RETRIES) {
|
|
94864
|
+
try {
|
|
94865
|
+
const url = `https://starknet.impulse.avnu.fi/v1/tokens/${tokenAddress}/prices/line`;
|
|
94866
|
+
const response = await axios_default.get(url);
|
|
94867
|
+
const length = response.data.length;
|
|
94868
|
+
return response.data[length - 1].value;
|
|
94869
|
+
} catch (err2) {
|
|
94870
|
+
if (retries > 0) {
|
|
94871
|
+
await new Promise((resolve) => setTimeout(resolve, MAX_DELAY));
|
|
94872
|
+
return this.getPriceOfToken(tokenAddress, retries - 1);
|
|
94873
|
+
} else {
|
|
94874
|
+
logger2.error(`Failed to fetch price for ${tokenAddress} after ${MAX_RETRIES} attempts`);
|
|
94875
|
+
return null;
|
|
94876
|
+
}
|
|
94877
|
+
}
|
|
94878
|
+
}
|
|
94879
|
+
};
|
|
94880
|
+
|
|
94509
94881
|
// src/strategies/universal-strategy.tsx
|
|
94510
94882
|
var AUMTypes = /* @__PURE__ */ ((AUMTypes2) => {
|
|
94511
94883
|
AUMTypes2["FINALISED"] = "finalised";
|
|
@@ -97924,334 +98296,6 @@ spurious results.`);
|
|
|
97924
98296
|
getStrategySettings("mRe7YIELD", "mRe7YIELD", hypermRe7YIELD, false, false)
|
|
97925
98297
|
];
|
|
97926
98298
|
|
|
97927
|
-
// src/strategies/universal-adapters/avnu-adapter.ts
|
|
97928
|
-
var AvnuAdapter = class _AvnuAdapter extends BaseAdapter {
|
|
97929
|
-
constructor(config3) {
|
|
97930
|
-
super(config3, _AvnuAdapter.name, Protocols.AVNU);
|
|
97931
|
-
this.config = config3;
|
|
97932
|
-
this.avnuWrapper = new AvnuWrapper();
|
|
97933
|
-
}
|
|
97934
|
-
//abstract means the method has no implementation in this class; instead, child classes must implement it.
|
|
97935
|
-
async getAPY(supportedPosition) {
|
|
97936
|
-
return Promise.resolve({ apy: 0, type: "base" /* BASE */ });
|
|
97937
|
-
}
|
|
97938
|
-
async getPosition(supportedPosition) {
|
|
97939
|
-
return Promise.resolve({ amount: new Web3Number(0, 0), remarks: "" });
|
|
97940
|
-
}
|
|
97941
|
-
async maxDeposit(amount) {
|
|
97942
|
-
return Promise.resolve({
|
|
97943
|
-
tokenInfo: this.config.baseToken,
|
|
97944
|
-
amount: new Web3Number(0, 0),
|
|
97945
|
-
usdValue: 0,
|
|
97946
|
-
apy: { apy: 0, type: "base" /* BASE */ },
|
|
97947
|
-
protocol: Protocols.AVNU,
|
|
97948
|
-
remarks: ""
|
|
97949
|
-
});
|
|
97950
|
-
}
|
|
97951
|
-
async maxWithdraw() {
|
|
97952
|
-
return Promise.resolve({
|
|
97953
|
-
tokenInfo: this.config.baseToken,
|
|
97954
|
-
amount: new Web3Number(0, 0),
|
|
97955
|
-
usdValue: 0,
|
|
97956
|
-
apy: { apy: 0, type: "base" /* BASE */ },
|
|
97957
|
-
protocol: Protocols.AVNU,
|
|
97958
|
-
remarks: ""
|
|
97959
|
-
});
|
|
97960
|
-
}
|
|
97961
|
-
_getDepositLeaf() {
|
|
97962
|
-
const vaultAllocator = ContractAddr.from(
|
|
97963
|
-
this.config.vaultAllocator.address
|
|
97964
|
-
);
|
|
97965
|
-
return [
|
|
97966
|
-
{
|
|
97967
|
-
target: this.config.supportedPositions[0].asset.address,
|
|
97968
|
-
method: "approve",
|
|
97969
|
-
packedArguments: [
|
|
97970
|
-
AVNU_EXCHANGE.toBigInt()
|
|
97971
|
-
],
|
|
97972
|
-
sanitizer: SIMPLE_SANITIZER,
|
|
97973
|
-
id: `approve_${this.config.supportedPositions[0].asset.symbol}`
|
|
97974
|
-
},
|
|
97975
|
-
{
|
|
97976
|
-
target: AVNU_EXCHANGE,
|
|
97977
|
-
method: "multi_route_swap",
|
|
97978
|
-
packedArguments: [
|
|
97979
|
-
this.config.supportedPositions[0].asset.address.toBigInt(),
|
|
97980
|
-
//usdc
|
|
97981
|
-
this.config.supportedPositions[1].asset.address.toBigInt(),
|
|
97982
|
-
//wbtc
|
|
97983
|
-
vaultAllocator.toBigInt()
|
|
97984
|
-
],
|
|
97985
|
-
sanitizer: SIMPLE_SANITIZER,
|
|
97986
|
-
id: `asutb_${this.config.supportedPositions[0].asset.symbol}_${this.config.supportedPositions[1].asset.symbol}`
|
|
97987
|
-
}
|
|
97988
|
-
];
|
|
97989
|
-
}
|
|
97990
|
-
_getWithdrawLeaf() {
|
|
97991
|
-
const vaultAllocator = ContractAddr.from(
|
|
97992
|
-
this.config.vaultAllocator.address
|
|
97993
|
-
);
|
|
97994
|
-
const toToken = this.config.supportedPositions[0].asset;
|
|
97995
|
-
const fromToken = this.config.supportedPositions[1].asset;
|
|
97996
|
-
return [
|
|
97997
|
-
{
|
|
97998
|
-
target: fromToken.address,
|
|
97999
|
-
method: "approve",
|
|
98000
|
-
packedArguments: [
|
|
98001
|
-
AVNU_EXCHANGE.toBigInt()
|
|
98002
|
-
],
|
|
98003
|
-
sanitizer: SIMPLE_SANITIZER,
|
|
98004
|
-
id: `approve_${fromToken.symbol}`
|
|
98005
|
-
},
|
|
98006
|
-
{
|
|
98007
|
-
target: AVNU_EXCHANGE,
|
|
98008
|
-
method: "multi_route_swap",
|
|
98009
|
-
packedArguments: [
|
|
98010
|
-
fromToken.address.toBigInt(),
|
|
98011
|
-
//wbtc
|
|
98012
|
-
toToken.address.toBigInt(),
|
|
98013
|
-
//usdc
|
|
98014
|
-
vaultAllocator.toBigInt()
|
|
98015
|
-
],
|
|
98016
|
-
sanitizer: SIMPLE_SANITIZER,
|
|
98017
|
-
id: `asbtu_${fromToken.symbol}_${fromToken.symbol}`
|
|
98018
|
-
}
|
|
98019
|
-
];
|
|
98020
|
-
}
|
|
98021
|
-
_getLegacySwapLeaf() {
|
|
98022
|
-
return [];
|
|
98023
|
-
}
|
|
98024
|
-
async getDepositCall(params) {
|
|
98025
|
-
try {
|
|
98026
|
-
const fromToken = this.config.supportedPositions[0].asset;
|
|
98027
|
-
const toToken = this.config.supportedPositions[1].asset;
|
|
98028
|
-
const vaultAllocator = ContractAddr.from(
|
|
98029
|
-
this.config.vaultAllocator.address
|
|
98030
|
-
);
|
|
98031
|
-
const quote = await this.getQuotesAvnu(
|
|
98032
|
-
fromToken.address.toString(),
|
|
98033
|
-
toToken.address.toString(),
|
|
98034
|
-
params.amount.toNumber(),
|
|
98035
|
-
vaultAllocator.address.toString(),
|
|
98036
|
-
toToken.decimals,
|
|
98037
|
-
true
|
|
98038
|
-
);
|
|
98039
|
-
if (!quote) {
|
|
98040
|
-
logger2.error("error getting quote from avnu");
|
|
98041
|
-
return [];
|
|
98042
|
-
}
|
|
98043
|
-
const getCalldata = await this.avnuWrapper.getSwapCallData(
|
|
98044
|
-
quote,
|
|
98045
|
-
vaultAllocator.address
|
|
98046
|
-
);
|
|
98047
|
-
const swapCallData = getCalldata[0];
|
|
98048
|
-
const amount = uint256_exports.bnToUint256(quote.sellAmountInUsd * 10 ** 7);
|
|
98049
|
-
return [
|
|
98050
|
-
{
|
|
98051
|
-
sanitizer: SIMPLE_SANITIZER,
|
|
98052
|
-
call: {
|
|
98053
|
-
contractAddress: fromToken.address,
|
|
98054
|
-
selector: hash_exports.getSelectorFromName("approve"),
|
|
98055
|
-
calldata: [
|
|
98056
|
-
AVNU_EXCHANGE.toBigInt(),
|
|
98057
|
-
toBigInt3(amount.low.toString()),
|
|
98058
|
-
// amount low
|
|
98059
|
-
toBigInt3(amount.high.toString())
|
|
98060
|
-
// amount high
|
|
98061
|
-
]
|
|
98062
|
-
}
|
|
98063
|
-
},
|
|
98064
|
-
{
|
|
98065
|
-
sanitizer: SIMPLE_SANITIZER,
|
|
98066
|
-
call: {
|
|
98067
|
-
contractAddress: AVNU_EXCHANGE,
|
|
98068
|
-
selector: hash_exports.getSelectorFromName("multi_route_swap"),
|
|
98069
|
-
calldata: swapCallData
|
|
98070
|
-
}
|
|
98071
|
-
}
|
|
98072
|
-
];
|
|
98073
|
-
} catch (error2) {
|
|
98074
|
-
logger2.error(`Error getting Avnu quote: ${error2}`);
|
|
98075
|
-
return [];
|
|
98076
|
-
}
|
|
98077
|
-
}
|
|
98078
|
-
//Swap wbtc to usdc
|
|
98079
|
-
async getWithdrawCall(params) {
|
|
98080
|
-
try {
|
|
98081
|
-
const toToken = this.config.supportedPositions[0].asset;
|
|
98082
|
-
const fromToken = this.config.supportedPositions[1].asset;
|
|
98083
|
-
const vaultAllocator = ContractAddr.from(
|
|
98084
|
-
this.config.vaultAllocator.address
|
|
98085
|
-
);
|
|
98086
|
-
const quote = await this.getQuotesAvnu(
|
|
98087
|
-
fromToken.address.toString(),
|
|
98088
|
-
toToken.address.toString(),
|
|
98089
|
-
params.amount.toNumber(),
|
|
98090
|
-
vaultAllocator.address.toString(),
|
|
98091
|
-
fromToken.decimals,
|
|
98092
|
-
false
|
|
98093
|
-
);
|
|
98094
|
-
if (!quote) {
|
|
98095
|
-
logger2.error("No quotes available for this swap, error in quotes avnu");
|
|
98096
|
-
return [];
|
|
98097
|
-
}
|
|
98098
|
-
const getCalldata = await this.avnuWrapper.getSwapCallData(
|
|
98099
|
-
quote,
|
|
98100
|
-
vaultAllocator.address
|
|
98101
|
-
);
|
|
98102
|
-
const swapCallData = getCalldata[0];
|
|
98103
|
-
const amount = uint256_exports.bnToUint256(params.amount.toWei());
|
|
98104
|
-
return [
|
|
98105
|
-
{
|
|
98106
|
-
sanitizer: SIMPLE_SANITIZER,
|
|
98107
|
-
call: {
|
|
98108
|
-
contractAddress: fromToken.address,
|
|
98109
|
-
selector: hash_exports.getSelectorFromName("approve"),
|
|
98110
|
-
calldata: [
|
|
98111
|
-
AVNU_EXCHANGE.toBigInt(),
|
|
98112
|
-
toBigInt3(amount.low.toString()),
|
|
98113
|
-
// amount low
|
|
98114
|
-
toBigInt3(amount.high.toString())
|
|
98115
|
-
// amount high
|
|
98116
|
-
]
|
|
98117
|
-
}
|
|
98118
|
-
},
|
|
98119
|
-
{
|
|
98120
|
-
sanitizer: SIMPLE_SANITIZER,
|
|
98121
|
-
call: {
|
|
98122
|
-
contractAddress: AVNU_EXCHANGE,
|
|
98123
|
-
selector: hash_exports.getSelectorFromName("multi_route_swap"),
|
|
98124
|
-
calldata: swapCallData
|
|
98125
|
-
}
|
|
98126
|
-
}
|
|
98127
|
-
];
|
|
98128
|
-
} catch (error2) {
|
|
98129
|
-
logger2.error(`Error getting Avnu quote: ${error2}`);
|
|
98130
|
-
return [];
|
|
98131
|
-
}
|
|
98132
|
-
}
|
|
98133
|
-
async getSwapCallData(quote) {
|
|
98134
|
-
return await this.avnuWrapper.getSwapCallData(quote, this.config.vaultAllocator.address);
|
|
98135
|
-
}
|
|
98136
|
-
async getHealthFactor() {
|
|
98137
|
-
return Promise.resolve(1);
|
|
98138
|
-
}
|
|
98139
|
-
async fetchQuoteWithRetry(params, retries = 5) {
|
|
98140
|
-
for (let attempt = 0; attempt < retries; attempt++) {
|
|
98141
|
-
try {
|
|
98142
|
-
const response = await axios_default.get(this.config.baseUrl, { params });
|
|
98143
|
-
if (response.data && response.data.length > 0) {
|
|
98144
|
-
return response;
|
|
98145
|
-
}
|
|
98146
|
-
throw new Error("Empty response data");
|
|
98147
|
-
} catch (err2) {
|
|
98148
|
-
logger2.error(`Error fetching quote with retry: ${err2}`);
|
|
98149
|
-
if (attempt === retries - 1) {
|
|
98150
|
-
throw err2;
|
|
98151
|
-
}
|
|
98152
|
-
await new Promise((resolve) => setTimeout(resolve, MAX_DELAY));
|
|
98153
|
-
}
|
|
98154
|
-
}
|
|
98155
|
-
throw new Error("Failed to fetch quote after retries");
|
|
98156
|
-
}
|
|
98157
|
-
async getQuotesAvnu(from_token_address, to_token_address, amount, takerAddress, toTokenDecimals, usdcToBtc, maxIterations = 5, tolerance = 5e3) {
|
|
98158
|
-
try {
|
|
98159
|
-
const fromToken = this.config.supportedPositions[0].asset;
|
|
98160
|
-
const toToken = this.config.supportedPositions[1].asset;
|
|
98161
|
-
if (!usdcToBtc) {
|
|
98162
|
-
const sellAmount2 = returnFormattedAmount(amount, toTokenDecimals);
|
|
98163
|
-
const params2 = {
|
|
98164
|
-
sellTokenAddress: from_token_address,
|
|
98165
|
-
buyTokenAddress: to_token_address,
|
|
98166
|
-
takerAddress,
|
|
98167
|
-
sellAmount: sellAmount2
|
|
98168
|
-
};
|
|
98169
|
-
const finalQuote2 = await this.fetchQuoteWithRetry(params2);
|
|
98170
|
-
if (!finalQuote2.data.length) {
|
|
98171
|
-
logger2.error("No quotes available for this swap, error in quotes avnu");
|
|
98172
|
-
return null;
|
|
98173
|
-
}
|
|
98174
|
-
const dataObject2 = finalQuote2.data[0];
|
|
98175
|
-
return dataObject2;
|
|
98176
|
-
}
|
|
98177
|
-
const btcPrice = await this.getPriceOfToken(toToken.address.toString());
|
|
98178
|
-
if (!btcPrice) {
|
|
98179
|
-
logger2.error(`error getting btc price: ${btcPrice}`);
|
|
98180
|
-
return null;
|
|
98181
|
-
}
|
|
98182
|
-
const estimatedUsdcAmount = Math.floor(amount * btcPrice);
|
|
98183
|
-
const targetBtcBig = BigInt(returnFormattedAmount(amount, toTokenDecimals));
|
|
98184
|
-
let low = BigInt(
|
|
98185
|
-
Math.floor(
|
|
98186
|
-
estimatedUsdcAmount * 10 ** fromToken.decimals * 0.9
|
|
98187
|
-
)
|
|
98188
|
-
);
|
|
98189
|
-
let high = BigInt(
|
|
98190
|
-
Math.floor(
|
|
98191
|
-
estimatedUsdcAmount * 10 ** fromToken.decimals * 1.1
|
|
98192
|
-
)
|
|
98193
|
-
);
|
|
98194
|
-
let mid = 0n;
|
|
98195
|
-
for (let i = 0; i < maxIterations; i++) {
|
|
98196
|
-
mid = (low + high) / 2n;
|
|
98197
|
-
const sellAmount2 = returnFormattedAmount(Number(mid), 0);
|
|
98198
|
-
const quote = await this.fetchQuoteWithRetry({
|
|
98199
|
-
sellTokenAddress: from_token_address,
|
|
98200
|
-
buyTokenAddress: to_token_address,
|
|
98201
|
-
takerAddress,
|
|
98202
|
-
sellAmount: sellAmount2
|
|
98203
|
-
});
|
|
98204
|
-
const gotBtc = BigInt(quote.data[0].buyAmount);
|
|
98205
|
-
if (gotBtc === targetBtcBig) return quote.data[0];
|
|
98206
|
-
if (gotBtc > targetBtcBig) {
|
|
98207
|
-
high = mid;
|
|
98208
|
-
} else {
|
|
98209
|
-
low = mid;
|
|
98210
|
-
}
|
|
98211
|
-
if (gotBtc >= targetBtcBig && gotBtc <= targetBtcBig + BigInt(tolerance)) {
|
|
98212
|
-
return quote.data[0];
|
|
98213
|
-
}
|
|
98214
|
-
}
|
|
98215
|
-
let sellAmount = returnFormattedAmount(
|
|
98216
|
-
Number(mid),
|
|
98217
|
-
0
|
|
98218
|
-
);
|
|
98219
|
-
const params = {
|
|
98220
|
-
sellTokenAddress: from_token_address,
|
|
98221
|
-
buyTokenAddress: to_token_address,
|
|
98222
|
-
takerAddress,
|
|
98223
|
-
sellAmount
|
|
98224
|
-
};
|
|
98225
|
-
const finalQuote = await this.fetchQuoteWithRetry(params);
|
|
98226
|
-
if (!finalQuote.data.length) {
|
|
98227
|
-
logger2.error("No quotes available for this swap, error in quotes avnu");
|
|
98228
|
-
return null;
|
|
98229
|
-
}
|
|
98230
|
-
const dataObject = finalQuote.data[0];
|
|
98231
|
-
return dataObject;
|
|
98232
|
-
} catch (err2) {
|
|
98233
|
-
logger2.error(`No quotes available for this swap: ${err2}`);
|
|
98234
|
-
return null;
|
|
98235
|
-
}
|
|
98236
|
-
}
|
|
98237
|
-
async getPriceOfToken(tokenAddress, retries = MAX_RETRIES) {
|
|
98238
|
-
try {
|
|
98239
|
-
const url = `https://starknet.impulse.avnu.fi/v1/tokens/${tokenAddress}/prices/line`;
|
|
98240
|
-
const response = await axios_default.get(url);
|
|
98241
|
-
const length = response.data.length;
|
|
98242
|
-
return response.data[length - 1].value;
|
|
98243
|
-
} catch (err2) {
|
|
98244
|
-
if (retries > 0) {
|
|
98245
|
-
await new Promise((resolve) => setTimeout(resolve, MAX_DELAY));
|
|
98246
|
-
return this.getPriceOfToken(tokenAddress, retries - 1);
|
|
98247
|
-
} else {
|
|
98248
|
-
logger2.error(`Failed to fetch price for ${tokenAddress} after ${MAX_RETRIES} attempts`);
|
|
98249
|
-
return null;
|
|
98250
|
-
}
|
|
98251
|
-
}
|
|
98252
|
-
}
|
|
98253
|
-
};
|
|
98254
|
-
|
|
98255
98299
|
// src/strategies/vesu-extended-strategy/vesu-extended-strategy.tsx
|
|
98256
98300
|
var import_jsx_runtime5 = __toESM(require_jsx_runtime());
|
|
98257
98301
|
var VesuExtendedMultiplierStrategy = class _VesuExtendedMultiplierStrategy extends SVKStrategy {
|
|
@@ -98368,10 +98412,16 @@ spurious results.`);
|
|
|
98368
98412
|
proofGroups,
|
|
98369
98413
|
await proofsInfo.callConstructor({ amount })
|
|
98370
98414
|
);
|
|
98371
|
-
return
|
|
98415
|
+
return {
|
|
98416
|
+
calls: [approveCall, transferCall, call],
|
|
98417
|
+
status: true
|
|
98418
|
+
};
|
|
98372
98419
|
} catch (err2) {
|
|
98373
98420
|
logger2.error(`error moving assets to vault allocator: ${err2}`);
|
|
98374
|
-
return
|
|
98421
|
+
return {
|
|
98422
|
+
calls: [],
|
|
98423
|
+
status: false
|
|
98424
|
+
};
|
|
98375
98425
|
}
|
|
98376
98426
|
}
|
|
98377
98427
|
async shouldInvest() {
|
|
@@ -98620,9 +98670,10 @@ spurious results.`);
|
|
|
98620
98670
|
const vesuAmountDifferenceAbs = vesuAmountDifference.abs();
|
|
98621
98671
|
logger2.info(`${_VesuExtendedMultiplierStrategy.name}::shouldMoveAssets calculated movements - Extended withdrawal: ${totalExtendedWithdrawal.toNumber()}, Extended deposit: ${totalExtendedDeposit.toNumber()}, Extended diff: ${extendedAmountDifference.toNumber()}, Projected wallet: ${projectedWalletBalance.toNumber()}, Vesu diff: ${vesuAmountDifference.toNumber()}`);
|
|
98622
98672
|
let calls = [];
|
|
98673
|
+
let transactionResults = [];
|
|
98623
98674
|
if (extendedAmount.isNegative() && extendedAmount.abs().greaterThan(extendedAdapter.minimumExtendedMovementAmount)) {
|
|
98624
98675
|
try {
|
|
98625
|
-
const { calls: extendedCalls, status: extendedStatus } = await this.moveAssets(
|
|
98676
|
+
const { calls: extendedCalls, status: extendedStatus, transactionMetadata: extendedTransactionMetadata } = await this.moveAssets(
|
|
98626
98677
|
{
|
|
98627
98678
|
to: Protocols.VAULT.name,
|
|
98628
98679
|
from: Protocols.EXTENDED.name,
|
|
@@ -98632,17 +98683,25 @@ spurious results.`);
|
|
|
98632
98683
|
vesuAdapter
|
|
98633
98684
|
);
|
|
98634
98685
|
if (extendedStatus) {
|
|
98635
|
-
|
|
98686
|
+
transactionResults.push({
|
|
98687
|
+
status: extendedStatus,
|
|
98688
|
+
calls: extendedCalls,
|
|
98689
|
+
transactionMetadata: {
|
|
98690
|
+
...extendedTransactionMetadata,
|
|
98691
|
+
transactionType: "DEPOSIT"
|
|
98692
|
+
}
|
|
98693
|
+
});
|
|
98636
98694
|
} else {
|
|
98637
|
-
return [];
|
|
98695
|
+
return [this.createTransactionResult([], false, { from: Protocols.EXTENDED.name, to: Protocols.VAULT.name, amount: extendedAmount.abs() }, "NONE")];
|
|
98638
98696
|
}
|
|
98639
98697
|
} catch (err2) {
|
|
98640
98698
|
logger2.error(`Failed moving assets to vault: ${err2}`);
|
|
98699
|
+
return [this.createTransactionResult([], false, { from: Protocols.EXTENDED.name, to: Protocols.VAULT.name, amount: extendedAmount.abs() }, "NONE")];
|
|
98641
98700
|
}
|
|
98642
98701
|
}
|
|
98643
98702
|
if (vesuAmount.isNegative() && vesuAmount.abs().greaterThan(vesuAdapter.minimumVesuMovementAmount)) {
|
|
98644
98703
|
try {
|
|
98645
|
-
const { calls: vesuCalls, status: vesuStatus } = await this.moveAssets(
|
|
98704
|
+
const { calls: vesuCalls, status: vesuStatus, transactionMetadata: vesuTransactionMetadata } = await this.moveAssets(
|
|
98646
98705
|
{
|
|
98647
98706
|
to: Protocols.EXTENDED.name,
|
|
98648
98707
|
from: Protocols.VESU.name,
|
|
@@ -98651,18 +98710,26 @@ spurious results.`);
|
|
|
98651
98710
|
extendedAdapter,
|
|
98652
98711
|
vesuAdapter
|
|
98653
98712
|
);
|
|
98654
|
-
calls.push(...vesuCalls);
|
|
98655
98713
|
if (!vesuStatus) {
|
|
98656
|
-
return [];
|
|
98657
|
-
}
|
|
98714
|
+
return [this.createTransactionResult([], false, { from: Protocols.VESU.name, to: Protocols.EXTENDED.name, amount: vesuAmount.abs() }, "NONE")];
|
|
98715
|
+
}
|
|
98716
|
+
transactionResults.push({
|
|
98717
|
+
status: vesuStatus,
|
|
98718
|
+
calls: vesuCalls,
|
|
98719
|
+
transactionMetadata: {
|
|
98720
|
+
...vesuTransactionMetadata,
|
|
98721
|
+
transactionType: "DEPOSIT"
|
|
98722
|
+
}
|
|
98723
|
+
});
|
|
98658
98724
|
} catch (err2) {
|
|
98659
|
-
logger2.error(`Failed moving assets to vault: ${err2}`);
|
|
98725
|
+
logger2.error(`Failed moving assets to extended via vault allocator: ${err2}`);
|
|
98726
|
+
return [this.createTransactionResult([], false, { from: Protocols.VESU.name, to: Protocols.EXTENDED.name, amount: vesuAmount.abs() }, "NONE")];
|
|
98660
98727
|
}
|
|
98661
98728
|
}
|
|
98662
98729
|
if (extendedAmountDifferenceAbs.greaterThan(extendedAdapter.minimumExtendedMovementAmount)) {
|
|
98663
98730
|
if (extendedAmountDifference.greaterThan(0)) {
|
|
98664
98731
|
try {
|
|
98665
|
-
const { calls: extendedCalls, status: extendedStatus } = await this.moveAssets(
|
|
98732
|
+
const { calls: extendedCalls, status: extendedStatus, transactionMetadata: extendedTransactionMetadata } = await this.moveAssets(
|
|
98666
98733
|
{
|
|
98667
98734
|
to: Protocols.EXTENDED.name,
|
|
98668
98735
|
from: Protocols.VAULT.name,
|
|
@@ -98672,18 +98739,22 @@ spurious results.`);
|
|
|
98672
98739
|
vesuAdapter
|
|
98673
98740
|
);
|
|
98674
98741
|
if (extendedStatus) {
|
|
98675
|
-
|
|
98742
|
+
transactionResults.push({
|
|
98743
|
+
status: extendedStatus,
|
|
98744
|
+
calls: extendedCalls,
|
|
98745
|
+
transactionMetadata: extendedTransactionMetadata
|
|
98746
|
+
});
|
|
98676
98747
|
} else {
|
|
98677
98748
|
logger2.error(`Failed to move assets to extended - operation returned false status`);
|
|
98678
|
-
return [];
|
|
98749
|
+
return [this.createTransactionResult([], false, { from: Protocols.VAULT.name, to: Protocols.EXTENDED.name, amount: extendedAmountDifference }, "NONE")];
|
|
98679
98750
|
}
|
|
98680
98751
|
} catch (err2) {
|
|
98681
98752
|
logger2.error(`Failed moving assets to extended: ${err2}`);
|
|
98682
|
-
return [];
|
|
98753
|
+
return [this.createTransactionResult([], false, { from: Protocols.VAULT.name, to: Protocols.EXTENDED.name, amount: extendedAmountDifference }, "NONE")];
|
|
98683
98754
|
}
|
|
98684
98755
|
} else if (extendedAmountDifference.lessThan(0)) {
|
|
98685
98756
|
try {
|
|
98686
|
-
const { calls: extendedCalls, status: extendedStatus } = await this.moveAssets(
|
|
98757
|
+
const { calls: extendedCalls, status: extendedStatus, transactionMetadata: extendedTransactionMetadata } = await this.moveAssets(
|
|
98687
98758
|
{
|
|
98688
98759
|
to: Protocols.VAULT.name,
|
|
98689
98760
|
from: Protocols.EXTENDED.name,
|
|
@@ -98693,14 +98764,21 @@ spurious results.`);
|
|
|
98693
98764
|
vesuAdapter
|
|
98694
98765
|
);
|
|
98695
98766
|
if (extendedStatus) {
|
|
98696
|
-
|
|
98767
|
+
transactionResults.push({
|
|
98768
|
+
status: extendedStatus,
|
|
98769
|
+
calls: extendedCalls,
|
|
98770
|
+
transactionMetadata: {
|
|
98771
|
+
...extendedTransactionMetadata,
|
|
98772
|
+
transactionType: "DEPOSIT"
|
|
98773
|
+
}
|
|
98774
|
+
});
|
|
98697
98775
|
} else {
|
|
98698
98776
|
logger2.error(`Failed to withdraw from extended - operation returned false status`);
|
|
98699
|
-
return [];
|
|
98777
|
+
return [this.createTransactionResult([], false, { from: Protocols.EXTENDED.name, to: Protocols.VAULT.name, amount: extendedAmountDifferenceAbs }, "NONE")];
|
|
98700
98778
|
}
|
|
98701
98779
|
} catch (err2) {
|
|
98702
98780
|
logger2.error(`Failed moving assets from extended to vault: ${err2}`);
|
|
98703
|
-
return [];
|
|
98781
|
+
return [this.createTransactionResult([], false, { from: Protocols.EXTENDED.name, to: Protocols.VAULT.name, amount: extendedAmountDifferenceAbs }, "NONE")];
|
|
98704
98782
|
}
|
|
98705
98783
|
}
|
|
98706
98784
|
}
|
|
@@ -98711,7 +98789,7 @@ spurious results.`);
|
|
|
98711
98789
|
);
|
|
98712
98790
|
} else {
|
|
98713
98791
|
try {
|
|
98714
|
-
const { calls: vesuCalls, status: vesuStatus } = await this.moveAssets(
|
|
98792
|
+
const { calls: vesuCalls, status: vesuStatus, transactionMetadata: vesuTransactionMetadata } = await this.moveAssets(
|
|
98715
98793
|
{
|
|
98716
98794
|
to: Protocols.VAULT.name,
|
|
98717
98795
|
from: Protocols.EXTENDED.name,
|
|
@@ -98722,20 +98800,46 @@ spurious results.`);
|
|
|
98722
98800
|
);
|
|
98723
98801
|
if (!vesuStatus) {
|
|
98724
98802
|
logger2.error(`Failed to move assets to vesu - operation returned false status`);
|
|
98725
|
-
return [];
|
|
98803
|
+
return [this.createTransactionResult([], false, { from: Protocols.EXTENDED.name, to: Protocols.VAULT.name, amount: vesuAmountDifference }, "NONE")];
|
|
98726
98804
|
}
|
|
98727
|
-
|
|
98805
|
+
transactionResults.push({
|
|
98806
|
+
status: vesuStatus,
|
|
98807
|
+
calls: vesuCalls,
|
|
98808
|
+
transactionMetadata: {
|
|
98809
|
+
...vesuTransactionMetadata,
|
|
98810
|
+
transactionType: "DEPOSIT"
|
|
98811
|
+
}
|
|
98812
|
+
});
|
|
98728
98813
|
} catch (err2) {
|
|
98729
98814
|
logger2.error(`Failed moving assets to vault: ${err2}`);
|
|
98730
|
-
return [];
|
|
98815
|
+
return [this.createTransactionResult([], false, { from: Protocols.EXTENDED.name, to: Protocols.VAULT.name, amount: vesuAmountDifference }, "NONE")];
|
|
98731
98816
|
}
|
|
98732
98817
|
}
|
|
98733
98818
|
}
|
|
98734
|
-
return
|
|
98819
|
+
return transactionResults;
|
|
98735
98820
|
} catch (err2) {
|
|
98736
98821
|
logger2.error(`Failed moving assets to vesu: ${err2}`);
|
|
98737
|
-
return [];
|
|
98822
|
+
return [this.createTransactionResult([], false, { from: Protocols.EXTENDED.name, to: Protocols.VAULT.name, amount: new Web3Number(0, USDC_TOKEN_DECIMALS) }, "NONE")];
|
|
98823
|
+
}
|
|
98824
|
+
}
|
|
98825
|
+
/**
|
|
98826
|
+
* Helper method to create transaction result with metadata
|
|
98827
|
+
*/
|
|
98828
|
+
createTransactionResult(calls, status, params, transactionType) {
|
|
98829
|
+
if (status) {
|
|
98830
|
+
return {
|
|
98831
|
+
calls,
|
|
98832
|
+
status,
|
|
98833
|
+
transactionMetadata: {
|
|
98834
|
+
protocolFrom: params.from,
|
|
98835
|
+
protocolTo: params.to,
|
|
98836
|
+
transactionType,
|
|
98837
|
+
usdAmount: params.amount.abs().toFixed(),
|
|
98838
|
+
status: "PENDING"
|
|
98839
|
+
}
|
|
98840
|
+
};
|
|
98738
98841
|
}
|
|
98842
|
+
return { calls: [], status: false, transactionMetadata: { protocolFrom: "", protocolTo: "", transactionType: "DEPOSIT", usdAmount: "0", status: "FAILED" } };
|
|
98739
98843
|
}
|
|
98740
98844
|
async moveAssets(params, extendedAdapter, vesuAdapter) {
|
|
98741
98845
|
try {
|
|
@@ -98743,10 +98847,7 @@ spurious results.`);
|
|
|
98743
98847
|
logger2.error(
|
|
98744
98848
|
`Invalid amount for moveAssets: ${params.amount.toNumber()}. Amount must be positive.`
|
|
98745
98849
|
);
|
|
98746
|
-
return
|
|
98747
|
-
calls: [],
|
|
98748
|
-
status: false
|
|
98749
|
-
};
|
|
98850
|
+
return this.createTransactionResult([], false, params, "NONE");
|
|
98750
98851
|
}
|
|
98751
98852
|
const amountAbs = params.amount.abs();
|
|
98752
98853
|
if (params.from === Protocols.EXTENDED.name || params.to === Protocols.EXTENDED.name) {
|
|
@@ -98754,10 +98855,7 @@ spurious results.`);
|
|
|
98754
98855
|
logger2.warn(
|
|
98755
98856
|
`Amount ${amountAbs.toNumber()} is below minimum Extended movement amount ${extendedAdapter.minimumExtendedMovementAmount}. Skipping operation.`
|
|
98756
98857
|
);
|
|
98757
|
-
return
|
|
98758
|
-
calls: [],
|
|
98759
|
-
status: false
|
|
98760
|
-
};
|
|
98858
|
+
return this.createTransactionResult([], false, params, "NONE");
|
|
98761
98859
|
}
|
|
98762
98860
|
}
|
|
98763
98861
|
if (params.from === Protocols.VESU.name || params.to === Protocols.VESU.name) {
|
|
@@ -98765,19 +98863,13 @@ spurious results.`);
|
|
|
98765
98863
|
logger2.warn(
|
|
98766
98864
|
`Amount ${amountAbs.toNumber()} is below minimum Vesu movement amount ${vesuAdapter.minimumVesuMovementAmount}. Skipping operation.`
|
|
98767
98865
|
);
|
|
98768
|
-
return
|
|
98769
|
-
calls: [],
|
|
98770
|
-
status: false
|
|
98771
|
-
};
|
|
98866
|
+
return this.createTransactionResult([], false, params, "NONE");
|
|
98772
98867
|
}
|
|
98773
98868
|
}
|
|
98774
98869
|
const avnuAdapter = await this.getAvnuAdapter();
|
|
98775
98870
|
if (!avnuAdapter) {
|
|
98776
98871
|
logger2.error(`avnu adapter not found: ${avnuAdapter}`);
|
|
98777
|
-
return
|
|
98778
|
-
calls: [],
|
|
98779
|
-
status: false
|
|
98780
|
-
};
|
|
98872
|
+
return this.createTransactionResult([], false, params, "NONE");
|
|
98781
98873
|
}
|
|
98782
98874
|
logger2.info(`moveAssets params, ${JSON.stringify(params)}`);
|
|
98783
98875
|
const collateralToken = vesuAdapter.config.supportedPositions[0].asset;
|
|
@@ -98796,19 +98888,13 @@ spurious results.`);
|
|
|
98796
98888
|
await proofsInfo.callConstructor({ amount: params.amount })
|
|
98797
98889
|
);
|
|
98798
98890
|
calls.push(call);
|
|
98799
|
-
return
|
|
98800
|
-
calls: [call],
|
|
98801
|
-
status: true
|
|
98802
|
-
};
|
|
98891
|
+
return this.createTransactionResult(calls, true, params, "DEPOSIT");
|
|
98803
98892
|
} else if (params.to === Protocols.VAULT.name && params.from === Protocols.EXTENDED.name) {
|
|
98804
98893
|
const extendedLeverage = calculateExtendedLevergae();
|
|
98805
98894
|
const extendedHoldings = await extendedAdapter.getExtendedDepositAmount();
|
|
98806
98895
|
if (!extendedHoldings) {
|
|
98807
98896
|
logger2.error(`error getting extended holdings: ${extendedHoldings}`);
|
|
98808
|
-
return
|
|
98809
|
-
calls: [],
|
|
98810
|
-
status: false
|
|
98811
|
-
};
|
|
98897
|
+
return this.createTransactionResult([], false, params, "NONE");
|
|
98812
98898
|
}
|
|
98813
98899
|
const extendedHoldingAmount = new Web3Number(
|
|
98814
98900
|
extendedHoldings.availableForWithdrawal,
|
|
@@ -98835,36 +98921,36 @@ spurious results.`);
|
|
|
98835
98921
|
const updatedHoldings = await extendedAdapter.getExtendedDepositAmount();
|
|
98836
98922
|
if (!updatedHoldings || new Web3Number(updatedHoldings.availableForWithdrawal, USDC_TOKEN_DECIMALS).lessThan(params.amount.abs())) {
|
|
98837
98923
|
logger2.error(`Insufficient balance after opening position. Available: ${updatedHoldings?.availableForWithdrawal}, Needed: ${params.amount.abs()}`);
|
|
98838
|
-
return
|
|
98924
|
+
return this.createTransactionResult([], false, params, "NONE");
|
|
98839
98925
|
}
|
|
98840
98926
|
}
|
|
98841
|
-
const
|
|
98842
|
-
|
|
98927
|
+
const {
|
|
98928
|
+
status: withdrawalFromExtendedStatus,
|
|
98929
|
+
receivedTxnHash: withdrawalFromExtendedTxnHash
|
|
98930
|
+
} = await extendedAdapter.withdrawFromExtended(params.amount);
|
|
98931
|
+
logger2.info(`withdrawalFromExtendedStatus: ${withdrawalFromExtendedStatus}, withdrawalFromExtendedTxnHash: ${withdrawalFromExtendedTxnHash}`);
|
|
98932
|
+
if (withdrawalFromExtendedStatus && withdrawalFromExtendedTxnHash) {
|
|
98843
98933
|
const extendedHoldings2 = await extendedAdapter.getExtendedDepositAmount();
|
|
98844
98934
|
logger2.info(`extendedHoldings after withdrawal ${extendedHoldings2?.availableForWithdrawal}`);
|
|
98845
98935
|
await new Promise((resolve) => setTimeout(resolve, 5e3));
|
|
98846
|
-
const calls = await this.moveAssetsToVaultAllocator(params.amount, extendedAdapter);
|
|
98847
|
-
if (calls.length > 0) {
|
|
98848
|
-
return
|
|
98849
|
-
|
|
98850
|
-
|
|
98851
|
-
};
|
|
98936
|
+
const { calls, status } = await this.moveAssetsToVaultAllocator(params.amount, extendedAdapter);
|
|
98937
|
+
if (calls.length > 0 && status) {
|
|
98938
|
+
return this.createTransactionResult(calls, true, params, "WITHDRAWAL");
|
|
98939
|
+
} else {
|
|
98940
|
+
return this.createTransactionResult([], true, params, "WITHDRAWAL");
|
|
98852
98941
|
}
|
|
98942
|
+
} else if (withdrawalFromExtendedStatus && !withdrawalFromExtendedTxnHash) {
|
|
98943
|
+
logger2.error("withdrawal from extended successful, but funds didn't get transferred to the wallet");
|
|
98944
|
+
return this.createTransactionResult([], true, params, "WITHDRAWAL");
|
|
98853
98945
|
} else {
|
|
98854
98946
|
logger2.error("withdrawal from extended failed");
|
|
98855
|
-
return
|
|
98856
|
-
calls: [],
|
|
98857
|
-
status: false
|
|
98858
|
-
};
|
|
98947
|
+
return this.createTransactionResult([], false, params, "NONE");
|
|
98859
98948
|
}
|
|
98860
98949
|
} else if (params.to === Protocols.VAULT.name && params.from === Protocols.VESU.name) {
|
|
98861
98950
|
const isPriceDifferenceBetweenAvnuAndExtended = await this.checkPriceDifferenceBetweenAvnuAndExtended(extendedAdapter, vesuAdapter, avnuAdapter, "close" /* CLOSE */);
|
|
98862
98951
|
if (!isPriceDifferenceBetweenAvnuAndExtended) {
|
|
98863
98952
|
logger2.warn(`price difference between avnu and extended doesn't fit the range for close position, ${avnuAdapter.config.maximumExtendedPriceDifferenceForSwapClosing}`);
|
|
98864
|
-
return
|
|
98865
|
-
calls: [],
|
|
98866
|
-
status: false
|
|
98867
|
-
};
|
|
98953
|
+
return this.createTransactionResult([], false, params, "NONE");
|
|
98868
98954
|
}
|
|
98869
98955
|
const vesuAmountInBTC = new Web3Number(
|
|
98870
98956
|
params.amount.dividedBy(collateralPrice.price).toFixed(WBTC_TOKEN_DECIMALS),
|
|
@@ -98885,18 +98971,12 @@ spurious results.`);
|
|
|
98885
98971
|
await swapProofsInfo.callConstructor({ amount: vesuAmountInBTC })
|
|
98886
98972
|
);
|
|
98887
98973
|
calls.push(swapCall);
|
|
98888
|
-
return
|
|
98889
|
-
calls,
|
|
98890
|
-
status: true
|
|
98891
|
-
};
|
|
98974
|
+
return this.createTransactionResult(calls, true, params, "WITHDRAWAL");
|
|
98892
98975
|
} else if (params.to === Protocols.EXTENDED.name && params.from === Protocols.VESU.name) {
|
|
98893
98976
|
const isPriceDifferenceBetweenAvnuAndExtended = await this.checkPriceDifferenceBetweenAvnuAndExtended(extendedAdapter, vesuAdapter, avnuAdapter, "close" /* CLOSE */);
|
|
98894
98977
|
if (!isPriceDifferenceBetweenAvnuAndExtended) {
|
|
98895
98978
|
logger2.warn(`price difference between avnu and extended doesn't fit the range for close position, ${avnuAdapter.config.maximumExtendedPriceDifferenceForSwapClosing}`);
|
|
98896
|
-
return
|
|
98897
|
-
calls: [],
|
|
98898
|
-
status: false
|
|
98899
|
-
};
|
|
98979
|
+
return this.createTransactionResult([], false, params, "NONE");
|
|
98900
98980
|
}
|
|
98901
98981
|
const vesuAmountInBTC = new Web3Number(
|
|
98902
98982
|
params.amount.dividedBy(collateralPrice.price).toNumber(),
|
|
@@ -98927,127 +99007,21 @@ spurious results.`);
|
|
|
98927
99007
|
await proofsInfoDeposit.callConstructor({ amount: params.amount })
|
|
98928
99008
|
);
|
|
98929
99009
|
calls.push(callDeposit);
|
|
98930
|
-
return
|
|
98931
|
-
calls,
|
|
98932
|
-
status: true
|
|
98933
|
-
};
|
|
99010
|
+
return this.createTransactionResult(calls, true, params, "DEPOSIT");
|
|
98934
99011
|
}
|
|
98935
|
-
|
|
98936
|
-
|
|
98937
|
-
status: false
|
|
98938
|
-
};
|
|
99012
|
+
logger2.error(`Unsupported assets movement: ${params.from} to ${params.to}`);
|
|
99013
|
+
return this.createTransactionResult([], false, params, "NONE");
|
|
98939
99014
|
} catch (err2) {
|
|
98940
99015
|
logger2.error(`error moving assets: ${err2}`);
|
|
98941
|
-
return
|
|
98942
|
-
calls: [],
|
|
98943
|
-
status: false
|
|
98944
|
-
};
|
|
99016
|
+
return this.createTransactionResult([], false, params, "NONE");
|
|
98945
99017
|
}
|
|
98946
99018
|
}
|
|
98947
99019
|
async handleDeposit() {
|
|
98948
99020
|
try {
|
|
98949
|
-
|
|
98950
|
-
const extendedAdapter = await this.getExtendedAdapter();
|
|
98951
|
-
const avnuAdapter = await this.getAvnuAdapter();
|
|
98952
|
-
if (!vesuAdapter || !extendedAdapter || !extendedAdapter.client || !avnuAdapter) {
|
|
98953
|
-
logger2.error(
|
|
98954
|
-
"vesu or extended adapter not found",
|
|
98955
|
-
vesuAdapter,
|
|
98956
|
-
extendedAdapter
|
|
98957
|
-
);
|
|
98958
|
-
return {
|
|
98959
|
-
extendedAmountInBTC: new Web3Number(0, 0),
|
|
98960
|
-
calls: []
|
|
98961
|
-
};
|
|
98962
|
-
}
|
|
98963
|
-
const extendedLeverage = calculateExtendedLevergae();
|
|
98964
|
-
const isPriceDifferenceBetweenAvnuAndExtended = await this.checkPriceDifferenceBetweenAvnuAndExtended(extendedAdapter, vesuAdapter, avnuAdapter, "open" /* OPEN */);
|
|
98965
|
-
if (!isPriceDifferenceBetweenAvnuAndExtended) {
|
|
98966
|
-
logger2.error("price difference between avnu and extended doesn't fit the range");
|
|
98967
|
-
return {
|
|
98968
|
-
extendedAmountInBTC: new Web3Number(0, 0),
|
|
98969
|
-
calls: []
|
|
98970
|
-
};
|
|
98971
|
-
}
|
|
98972
|
-
const position = await extendedAdapter.getAllOpenPositions();
|
|
98973
|
-
if (!position) {
|
|
98974
|
-
logger2.error("error getting extended position", position);
|
|
98975
|
-
return {
|
|
98976
|
-
extendedAmountInBTC: new Web3Number(0, 0),
|
|
98977
|
-
calls: []
|
|
98978
|
-
};
|
|
98979
|
-
}
|
|
98980
|
-
const extendedPositionValue = position.length > 0 ? parseFloat(position[0].value) : 0;
|
|
98981
|
-
const BUFFER_AMOUNT_IN_AVAILABLE_FOR_TRADE = BUFFER_USDC_IN_WITHDRAWAL;
|
|
98982
|
-
const extendedHoldings = await extendedAdapter.getExtendedDepositAmount();
|
|
98983
|
-
if (!extendedHoldings) {
|
|
98984
|
-
logger2.error(`error getting extended holdings: ${extendedHoldings}`);
|
|
98985
|
-
return {
|
|
98986
|
-
extendedAmountInBTC: new Web3Number(0, 0),
|
|
98987
|
-
calls: []
|
|
98988
|
-
};
|
|
98989
|
-
}
|
|
98990
|
-
const extendedHoldingAmount = new Web3Number(
|
|
98991
|
-
extendedHoldings.availableForWithdrawal,
|
|
98992
|
-
USDC_TOKEN_DECIMALS
|
|
98993
|
-
);
|
|
98994
|
-
const {
|
|
98995
|
-
collateralTokenAmount
|
|
98996
|
-
} = await vesuAdapter.vesuAdapter.getAssetPrices();
|
|
98997
|
-
const { collateralPrice } = await this.getAssetPrices();
|
|
98998
|
-
const { vesuAmountInBTC, extendedAmountInBTC } = calculateVesUPositionSizeGivenExtended(
|
|
98999
|
-
extendedPositionValue,
|
|
99000
|
-
extendedHoldingAmount.minus(BUFFER_AMOUNT_IN_AVAILABLE_FOR_TRADE),
|
|
99001
|
-
collateralTokenAmount,
|
|
99002
|
-
collateralPrice.price
|
|
99003
|
-
);
|
|
99004
|
-
logger2.info(`vesuAmountInBTC ${vesuAmountInBTC}, extendedAmountInBTC ${extendedAmountInBTC}`);
|
|
99005
|
-
let calls = [];
|
|
99006
|
-
if (vesuAmountInBTC.greaterThan(MINIMUM_EXTENDED_POSITION_SIZE)) {
|
|
99007
|
-
const proofsInfo = vesuAdapter.getProofs(true, this.getMerkleTree());
|
|
99008
|
-
const proofGroups = proofsInfo.proofs;
|
|
99009
|
-
const call = this.getManageCall(
|
|
99010
|
-
proofGroups,
|
|
99011
|
-
await proofsInfo.callConstructor({
|
|
99012
|
-
amount: vesuAmountInBTC
|
|
99013
|
-
})
|
|
99014
|
-
);
|
|
99015
|
-
const { amount: wbtcAmountInVaultAllocator } = await this.getUnusedBalanceWBTC();
|
|
99016
|
-
if (wbtcAmountInVaultAllocator.lessThan(vesuAmountInBTC)) {
|
|
99017
|
-
const swapProofsInfo = avnuAdapter.getProofs(true, this.getMerkleTree());
|
|
99018
|
-
const swapProofGroups = swapProofsInfo.proofs;
|
|
99019
|
-
const swapCall = this.getManageCall(
|
|
99020
|
-
swapProofGroups,
|
|
99021
|
-
await swapProofsInfo.callConstructor({
|
|
99022
|
-
amount: vesuAmountInBTC
|
|
99023
|
-
})
|
|
99024
|
-
);
|
|
99025
|
-
calls.push(swapCall);
|
|
99026
|
-
}
|
|
99027
|
-
calls.push(call);
|
|
99028
|
-
}
|
|
99029
|
-
const shortPosition = extendedAmountInBTC.multipliedBy(3).abs().greaterThan(MINIMUM_EXTENDED_POSITION_SIZE) ? await extendedAdapter.createOrder(
|
|
99030
|
-
extendedLeverage.toString(),
|
|
99031
|
-
extendedAmountInBTC.toNumber(),
|
|
99032
|
-
"SELL" /* SELL */
|
|
99033
|
-
) : null;
|
|
99034
|
-
if (!shortPosition && extendedAmountInBTC.multipliedBy(3).abs().greaterThan(MINIMUM_EXTENDED_POSITION_SIZE)) {
|
|
99035
|
-
logger2.error(`error creating short position thus no position to be opened on vesu: ${shortPosition}`);
|
|
99036
|
-
return {
|
|
99037
|
-
extendedAmountInBTC: new Web3Number(0, 0),
|
|
99038
|
-
calls: []
|
|
99039
|
-
};
|
|
99040
|
-
}
|
|
99041
|
-
return {
|
|
99042
|
-
extendedAmountInBTC,
|
|
99043
|
-
calls
|
|
99044
|
-
};
|
|
99021
|
+
return this.createTransactionResult([], false, { from: Protocols.VAULT.name, to: Protocols.VAULT.name, amount: new Web3Number(0, 0) }, "NONE");
|
|
99045
99022
|
} catch (err2) {
|
|
99046
99023
|
logger2.error(`error handling deposit: ${err2}`);
|
|
99047
|
-
return {
|
|
99048
|
-
extendedAmountInBTC: new Web3Number(0, 0),
|
|
99049
|
-
calls: []
|
|
99050
|
-
};
|
|
99024
|
+
return this.createTransactionResult([], false, { from: Protocols.VAULT.name, to: Protocols.VAULT.name, amount: new Web3Number(0, 0) }, "NONE");
|
|
99051
99025
|
}
|
|
99052
99026
|
}
|
|
99053
99027
|
async checkPriceDifferenceBetweenAvnuAndExtended(extendedAdapter, vesuAdapter, avnuAdapter, positionType) {
|
|
@@ -99092,12 +99066,8 @@ spurious results.`);
|
|
|
99092
99066
|
const withdrawCall2 = await this.getBringLiquidityCall({
|
|
99093
99067
|
amount: usdcBalanceVaultAllocator.amount
|
|
99094
99068
|
});
|
|
99095
|
-
logger2.info("withdraw call", withdrawCall2);
|
|
99096
99069
|
calls.push(withdrawCall2);
|
|
99097
|
-
return {
|
|
99098
|
-
calls,
|
|
99099
|
-
status: true
|
|
99100
|
-
};
|
|
99070
|
+
return [this.createTransactionResult(calls, true, { from: Protocols.VAULT.name, to: Protocols.NONE.name, amount }, "WITHDRAWAL")];
|
|
99101
99071
|
}
|
|
99102
99072
|
const vesuAdapter = await this.getVesuAdapter();
|
|
99103
99073
|
const extendedAdapter = await this.getExtendedAdapter();
|
|
@@ -99106,11 +99076,9 @@ spurious results.`);
|
|
|
99106
99076
|
logger2.error(
|
|
99107
99077
|
`vesu or extended adapter not found: vesuAdapter=${vesuAdapter}, extendedAdapter=${extendedAdapter}`
|
|
99108
99078
|
);
|
|
99109
|
-
return {
|
|
99110
|
-
calls,
|
|
99111
|
-
status
|
|
99112
|
-
};
|
|
99079
|
+
return [this.createTransactionResult(calls, status, { from: Protocols.VAULT.name, to: Protocols.NONE.name, amount }, "NONE")];
|
|
99113
99080
|
}
|
|
99081
|
+
let transactionResults = [];
|
|
99114
99082
|
const { collateralTokenAmount } = await vesuAdapter.vesuAdapter.getAssetPrices();
|
|
99115
99083
|
const {
|
|
99116
99084
|
collateralPrice
|
|
@@ -99119,10 +99087,7 @@ spurious results.`);
|
|
|
99119
99087
|
if (!extendedPositon) {
|
|
99120
99088
|
status = false;
|
|
99121
99089
|
logger2.error("error getting extended position", extendedPositon);
|
|
99122
|
-
return {
|
|
99123
|
-
calls,
|
|
99124
|
-
status
|
|
99125
|
-
};
|
|
99090
|
+
return [this.createTransactionResult(calls, status, { from: Protocols.VAULT.name, to: Protocols.NONE.name, amount }, "NONE")];
|
|
99126
99091
|
}
|
|
99127
99092
|
const amountDistributionForWithdrawal = await calculateAmountDistributionForWithdrawal(
|
|
99128
99093
|
usdcBalanceDifference,
|
|
@@ -99135,14 +99100,11 @@ spurious results.`);
|
|
|
99135
99100
|
logger2.error(
|
|
99136
99101
|
`error calculating amount distribution for withdrawal: ${amountDistributionForWithdrawal}`
|
|
99137
99102
|
);
|
|
99138
|
-
return {
|
|
99139
|
-
calls,
|
|
99140
|
-
status
|
|
99141
|
-
};
|
|
99103
|
+
return [this.createTransactionResult(calls, status, { from: Protocols.VAULT.name, to: Protocols.NONE.name, amount }, "NONE")];
|
|
99142
99104
|
}
|
|
99143
99105
|
const { vesu_amount, extended_amount } = amountDistributionForWithdrawal;
|
|
99144
99106
|
if (status && vesu_amount.greaterThan(0)) {
|
|
99145
|
-
const { calls: vesuCalls, status: vesuStatus } = await this.moveAssets(
|
|
99107
|
+
const { calls: vesuCalls, status: vesuStatus, transactionMetadata: vesuTransactionMetadata } = await this.moveAssets(
|
|
99146
99108
|
{
|
|
99147
99109
|
amount: vesu_amount,
|
|
99148
99110
|
from: Protocols.VESU.name,
|
|
@@ -99152,10 +99114,14 @@ spurious results.`);
|
|
|
99152
99114
|
vesuAdapter
|
|
99153
99115
|
);
|
|
99154
99116
|
status = vesuStatus;
|
|
99155
|
-
|
|
99117
|
+
transactionResults.push({
|
|
99118
|
+
status: vesuStatus,
|
|
99119
|
+
calls: vesuCalls,
|
|
99120
|
+
transactionMetadata: vesuTransactionMetadata
|
|
99121
|
+
});
|
|
99156
99122
|
}
|
|
99157
99123
|
if (status && extended_amount.greaterThan(0)) {
|
|
99158
|
-
const { calls: extendedCalls, status: extendedStatus } = await this.moveAssets(
|
|
99124
|
+
const { calls: extendedCalls, status: extendedStatus, transactionMetadata: extendedTransactionMetadata } = await this.moveAssets(
|
|
99159
99125
|
{
|
|
99160
99126
|
amount: extended_amount,
|
|
99161
99127
|
from: Protocols.EXTENDED.name,
|
|
@@ -99166,30 +99132,35 @@ spurious results.`);
|
|
|
99166
99132
|
);
|
|
99167
99133
|
status = extendedStatus;
|
|
99168
99134
|
if (status) {
|
|
99169
|
-
|
|
99135
|
+
transactionResults.push({
|
|
99136
|
+
status: extendedStatus,
|
|
99137
|
+
calls: extendedCalls,
|
|
99138
|
+
transactionMetadata: extendedTransactionMetadata
|
|
99139
|
+
});
|
|
99170
99140
|
} else {
|
|
99171
99141
|
logger2.error("error moving assets to vault: extendedStatus: ${extendedStatus}");
|
|
99172
|
-
return {
|
|
99173
|
-
calls: [],
|
|
99174
|
-
status
|
|
99175
|
-
};
|
|
99142
|
+
return [this.createTransactionResult([], status, { from: Protocols.VAULT.name, to: Protocols.NONE.name, amount }, "NONE")];
|
|
99176
99143
|
}
|
|
99177
99144
|
}
|
|
99178
99145
|
const withdrawCall = await this.getBringLiquidityCall({
|
|
99179
99146
|
amount
|
|
99180
99147
|
});
|
|
99181
99148
|
logger2.info("withdraw call", withdrawCall);
|
|
99182
|
-
|
|
99183
|
-
|
|
99184
|
-
calls,
|
|
99185
|
-
|
|
99186
|
-
|
|
99149
|
+
transactionResults.push({
|
|
99150
|
+
status,
|
|
99151
|
+
calls: [withdrawCall],
|
|
99152
|
+
transactionMetadata: {
|
|
99153
|
+
protocolFrom: Protocols.VAULT.name,
|
|
99154
|
+
protocolTo: Protocols.NONE.name,
|
|
99155
|
+
transactionType: "WITHDRAWAL",
|
|
99156
|
+
usdAmount: amount.toFixed(),
|
|
99157
|
+
status: "PENDING"
|
|
99158
|
+
}
|
|
99159
|
+
});
|
|
99160
|
+
return transactionResults;
|
|
99187
99161
|
} catch (err2) {
|
|
99188
99162
|
logger2.error(`error handling withdrawal: ${err2}`);
|
|
99189
|
-
return {
|
|
99190
|
-
calls: [],
|
|
99191
|
-
status: false
|
|
99192
|
-
};
|
|
99163
|
+
return [this.createTransactionResult([], false, { from: Protocols.VAULT.name, to: Protocols.NONE.name, amount }, "NONE")];
|
|
99193
99164
|
}
|
|
99194
99165
|
}
|
|
99195
99166
|
async getAUM() {
|
|
@@ -99236,6 +99207,36 @@ spurious results.`);
|
|
|
99236
99207
|
splits: [realAUM, estimatedAUMDelta]
|
|
99237
99208
|
};
|
|
99238
99209
|
}
|
|
99210
|
+
async processTransactionDataFromSDK(txnData) {
|
|
99211
|
+
try {
|
|
99212
|
+
const txnsToBeExecuted = txnData.filter((txn) => {
|
|
99213
|
+
return txn.transactionMetadata.transactionType !== "NONE" && txn.transactionMetadata.protocolFrom !== "" && txn.transactionMetadata.protocolTo !== "";
|
|
99214
|
+
});
|
|
99215
|
+
const callsToBeExecutedFinal = txnsToBeExecuted.flatMap((txn) => txn.calls);
|
|
99216
|
+
const txnMetadata = txnsToBeExecuted.map((txn) => txn.transactionMetadata);
|
|
99217
|
+
return { callsToBeExecutedFinal, txnMetadata };
|
|
99218
|
+
} catch (err2) {
|
|
99219
|
+
logger2.error(`error processing transaction data from SDK: ${err2}`);
|
|
99220
|
+
return null;
|
|
99221
|
+
}
|
|
99222
|
+
}
|
|
99223
|
+
async processTransactionMetadata(txnMetadata, extendedIntentFulfilled) {
|
|
99224
|
+
try {
|
|
99225
|
+
const txnMetadataNew = txnMetadata.map((txn) => {
|
|
99226
|
+
const isExtendedProtocol = txn.protocolFrom === Protocols.EXTENDED.name || txn.protocolTo === Protocols.EXTENDED.name;
|
|
99227
|
+
if (isExtendedProtocol) {
|
|
99228
|
+
txn.status = extendedIntentFulfilled ? "COMPLETED" : "PENDING";
|
|
99229
|
+
} else {
|
|
99230
|
+
txn.status = "COMPLETED";
|
|
99231
|
+
}
|
|
99232
|
+
return txn;
|
|
99233
|
+
});
|
|
99234
|
+
return txnMetadataNew;
|
|
99235
|
+
} catch (err2) {
|
|
99236
|
+
logger2.error(`error processing transaction data from SDK: ${err2}`);
|
|
99237
|
+
return null;
|
|
99238
|
+
}
|
|
99239
|
+
}
|
|
99239
99240
|
};
|
|
99240
99241
|
function getLooperSettings2(lstSymbol, underlyingSymbol, vaultSettings, pool1, extendedBackendUrl, extendedApiKey, vaultIdExtended, minimumExtendedMovementAmount, minimumVesuMovementAmount, minimumExtendedRetriesDelayForOrderStatus, minimumExtendedPriceDifferenceForSwapOpen, maximumExtendedPriceDifferenceForSwapClosing) {
|
|
99241
99242
|
vaultSettings.leafAdapters = [];
|