@strkfarm/sdk 2.0.0-dev.23 → 2.0.0-dev.24
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 +52 -5
- package/dist/index.browser.mjs +52 -5
- package/dist/index.d.ts +2 -1
- package/dist/index.js +53 -5
- package/dist/index.mjs +52 -5
- package/package.json +1 -1
- package/src/strategies/vesu-extended-strategy/utils/helper.ts +87 -32
- package/src/strategies/vesu-extended-strategy/vesu-extended-strategy.tsx +15 -2
|
@@ -28729,6 +28729,7 @@ ${r2}}` : "}", l2;
|
|
|
28729
28729
|
calculateDeltaDebtAmount: () => calculateDeltaDebtAmount,
|
|
28730
28730
|
calculateExposureDelta: () => calculateExposureDelta,
|
|
28731
28731
|
calculateExtendedLevergae: () => calculateExtendedLevergae,
|
|
28732
|
+
calculatePositionToCloseToWithdrawAmount: () => calculatePositionToCloseToWithdrawAmount,
|
|
28732
28733
|
calculateVesUPositionSizeGivenExtended: () => calculateVesUPositionSizeGivenExtended,
|
|
28733
28734
|
calculateVesuLeverage: () => calculateVesuLeverage,
|
|
28734
28735
|
calculateWBTCAmountToMaintainLTV: () => calculateWBTCAmountToMaintainLTV,
|
|
@@ -92523,7 +92524,9 @@ spurious results.`);
|
|
|
92523
92524
|
vesu_leverage
|
|
92524
92525
|
};
|
|
92525
92526
|
} catch (err2) {
|
|
92526
|
-
logger2.error(
|
|
92527
|
+
logger2.error(
|
|
92528
|
+
`error calculating amount distribution for withdrawal: ${err2}`
|
|
92529
|
+
);
|
|
92527
92530
|
return null;
|
|
92528
92531
|
}
|
|
92529
92532
|
};
|
|
@@ -92576,15 +92579,22 @@ spurious results.`);
|
|
|
92576
92579
|
const extended_leverage = calculateExtendedLevergae();
|
|
92577
92580
|
const latestPosition = (await client.getPositions()).data.pop();
|
|
92578
92581
|
if (!extendedHoldings || !latestPosition) {
|
|
92579
|
-
logger2.error(
|
|
92582
|
+
logger2.error(
|
|
92583
|
+
`error getting extended position: extendedHoldings=${extendedHoldings}, latestPosition=${latestPosition}`
|
|
92584
|
+
);
|
|
92580
92585
|
return null;
|
|
92581
92586
|
}
|
|
92582
|
-
const positionValueInUSD = new Web3Number(
|
|
92587
|
+
const positionValueInUSD = new Web3Number(
|
|
92588
|
+
latestPosition.value,
|
|
92589
|
+
USDC_TOKEN_DECIMALS
|
|
92590
|
+
);
|
|
92583
92591
|
const equity = extendedHoldings.data.equity;
|
|
92584
92592
|
const deposit = positionValueInUSD.dividedBy(extended_leverage).minus(equity).toFixed(2);
|
|
92585
92593
|
return new Web3Number(deposit, USDC_TOKEN_DECIMALS);
|
|
92586
92594
|
} catch (err2) {
|
|
92587
|
-
logger2.error(
|
|
92595
|
+
logger2.error(
|
|
92596
|
+
`error calculating amount deposit on extended when incurring losses: ${err2}`
|
|
92597
|
+
);
|
|
92588
92598
|
return null;
|
|
92589
92599
|
}
|
|
92590
92600
|
};
|
|
@@ -92636,6 +92646,30 @@ spurious results.`);
|
|
|
92636
92646
|
return null;
|
|
92637
92647
|
}
|
|
92638
92648
|
};
|
|
92649
|
+
var calculatePositionToCloseToWithdrawAmount = async (extendedBalance, extendedPositions, amountToWithdraw) => {
|
|
92650
|
+
try {
|
|
92651
|
+
const extendedPosition = extendedPositions.value;
|
|
92652
|
+
const unrealisedPnl = new Web3Number(
|
|
92653
|
+
extendedBalance.unrealisedPnl,
|
|
92654
|
+
USDC_TOKEN_DECIMALS
|
|
92655
|
+
);
|
|
92656
|
+
const availableForWithdrawal = new Web3Number(
|
|
92657
|
+
extendedBalance.availableForWithdrawal,
|
|
92658
|
+
USDC_TOKEN_DECIMALS
|
|
92659
|
+
);
|
|
92660
|
+
const netAmount = amountToWithdraw.minus(availableForWithdrawal);
|
|
92661
|
+
const upnlPercent = unrealisedPnl.dividedBy(extendedPosition);
|
|
92662
|
+
console.log(`upnlPercent: ${upnlPercent}`);
|
|
92663
|
+
const denm = upnlPercent.multipliedBy(3).plus(1);
|
|
92664
|
+
const amountToClose = netAmount.multipliedBy(3).dividedBy(denm);
|
|
92665
|
+
return new Web3Number(Math.ceil(amountToClose.toNumber()), USDC_TOKEN_DECIMALS);
|
|
92666
|
+
} catch (err2) {
|
|
92667
|
+
logger2.error(
|
|
92668
|
+
`error calculating position to close to withdraw amount: ${err2}`
|
|
92669
|
+
);
|
|
92670
|
+
return new Web3Number(0, USDC_TOKEN_DECIMALS);
|
|
92671
|
+
}
|
|
92672
|
+
};
|
|
92639
92673
|
|
|
92640
92674
|
// src/utils/health-factor-math.ts
|
|
92641
92675
|
var HealthFactorMath = class {
|
|
@@ -99224,6 +99258,17 @@ spurious results.`);
|
|
|
99224
99258
|
logger2.info(
|
|
99225
99259
|
`${_VesuExtendedMultiplierStrategy.name}::moveAssets extendedHoldingAmount: ${extendedHoldingAmount.toNumber()}`
|
|
99226
99260
|
);
|
|
99261
|
+
const extendedPositions = await extendedAdapter.getAllOpenPositions();
|
|
99262
|
+
if (!extendedPositions) {
|
|
99263
|
+
logger2.error(`error getting extended positions: ${extendedPositions} while moving assets from extended to vault`);
|
|
99264
|
+
return this.createTransactionResult(
|
|
99265
|
+
[],
|
|
99266
|
+
false,
|
|
99267
|
+
params,
|
|
99268
|
+
"NONE",
|
|
99269
|
+
params.cycleType
|
|
99270
|
+
);
|
|
99271
|
+
}
|
|
99227
99272
|
if (params.amount.abs().greaterThan(extendedHoldingAmount)) {
|
|
99228
99273
|
const leftAmountAfterWithdrawalAmountInAccount = new Web3Number(
|
|
99229
99274
|
Math.ceil(
|
|
@@ -99231,6 +99276,8 @@ spurious results.`);
|
|
|
99231
99276
|
),
|
|
99232
99277
|
USDC_TOKEN_DECIMALS
|
|
99233
99278
|
);
|
|
99279
|
+
const positionAmountToClose = await calculatePositionToCloseToWithdrawAmount(extendedHoldings, extendedPositions[0], params.amount);
|
|
99280
|
+
logger2.info(`positionAmountToClose: ${positionAmountToClose.toNumber()}`);
|
|
99234
99281
|
logger2.info(
|
|
99235
99282
|
`${_VesuExtendedMultiplierStrategy.name}::moveAssets leftAmountAfterWithdrawalAmountInAccount: ${leftAmountAfterWithdrawalAmountInAccount.toNumber()}`
|
|
99236
99283
|
);
|
|
@@ -99243,7 +99290,7 @@ spurious results.`);
|
|
|
99243
99290
|
logger2.error(`error fetching order book btc usdc: ${status}`);
|
|
99244
99291
|
priceOfBTC = collateralPrice.price;
|
|
99245
99292
|
}
|
|
99246
|
-
const btcAmount =
|
|
99293
|
+
const btcAmount = positionAmountToClose.dividedBy(priceOfBTC);
|
|
99247
99294
|
const openLongPosition = btcAmount.multipliedBy(3).greaterThan(MINIMUM_EXTENDED_POSITION_SIZE) ? await extendedAdapter.createOrder(
|
|
99248
99295
|
extendedLeverage.toString(),
|
|
99249
99296
|
btcAmount.toNumber(),
|
package/dist/index.browser.mjs
CHANGED
|
@@ -28479,7 +28479,9 @@ var calculateAmountDistributionForWithdrawal = async (amountInUsdc, collateralPr
|
|
|
28479
28479
|
vesu_leverage
|
|
28480
28480
|
};
|
|
28481
28481
|
} catch (err) {
|
|
28482
|
-
logger.error(
|
|
28482
|
+
logger.error(
|
|
28483
|
+
`error calculating amount distribution for withdrawal: ${err}`
|
|
28484
|
+
);
|
|
28483
28485
|
return null;
|
|
28484
28486
|
}
|
|
28485
28487
|
};
|
|
@@ -28532,15 +28534,22 @@ var calculateAmountDepositOnExtendedWhenIncurringLosses = async (client) => {
|
|
|
28532
28534
|
const extended_leverage = calculateExtendedLevergae();
|
|
28533
28535
|
const latestPosition = (await client.getPositions()).data.pop();
|
|
28534
28536
|
if (!extendedHoldings || !latestPosition) {
|
|
28535
|
-
logger.error(
|
|
28537
|
+
logger.error(
|
|
28538
|
+
`error getting extended position: extendedHoldings=${extendedHoldings}, latestPosition=${latestPosition}`
|
|
28539
|
+
);
|
|
28536
28540
|
return null;
|
|
28537
28541
|
}
|
|
28538
|
-
const positionValueInUSD = new Web3Number(
|
|
28542
|
+
const positionValueInUSD = new Web3Number(
|
|
28543
|
+
latestPosition.value,
|
|
28544
|
+
USDC_TOKEN_DECIMALS
|
|
28545
|
+
);
|
|
28539
28546
|
const equity = extendedHoldings.data.equity;
|
|
28540
28547
|
const deposit = positionValueInUSD.dividedBy(extended_leverage).minus(equity).toFixed(2);
|
|
28541
28548
|
return new Web3Number(deposit, USDC_TOKEN_DECIMALS);
|
|
28542
28549
|
} catch (err) {
|
|
28543
|
-
logger.error(
|
|
28550
|
+
logger.error(
|
|
28551
|
+
`error calculating amount deposit on extended when incurring losses: ${err}`
|
|
28552
|
+
);
|
|
28544
28553
|
return null;
|
|
28545
28554
|
}
|
|
28546
28555
|
};
|
|
@@ -28592,6 +28601,30 @@ var calculateDeltaDebtAmount = (maxLtv = MAX_LTV_BTC_USDC, existingVesuCollatera
|
|
|
28592
28601
|
return null;
|
|
28593
28602
|
}
|
|
28594
28603
|
};
|
|
28604
|
+
var calculatePositionToCloseToWithdrawAmount = async (extendedBalance, extendedPositions, amountToWithdraw) => {
|
|
28605
|
+
try {
|
|
28606
|
+
const extendedPosition = extendedPositions.value;
|
|
28607
|
+
const unrealisedPnl = new Web3Number(
|
|
28608
|
+
extendedBalance.unrealisedPnl,
|
|
28609
|
+
USDC_TOKEN_DECIMALS
|
|
28610
|
+
);
|
|
28611
|
+
const availableForWithdrawal = new Web3Number(
|
|
28612
|
+
extendedBalance.availableForWithdrawal,
|
|
28613
|
+
USDC_TOKEN_DECIMALS
|
|
28614
|
+
);
|
|
28615
|
+
const netAmount = amountToWithdraw.minus(availableForWithdrawal);
|
|
28616
|
+
const upnlPercent = unrealisedPnl.dividedBy(extendedPosition);
|
|
28617
|
+
console.log(`upnlPercent: ${upnlPercent}`);
|
|
28618
|
+
const denm = upnlPercent.multipliedBy(3).plus(1);
|
|
28619
|
+
const amountToClose = netAmount.multipliedBy(3).dividedBy(denm);
|
|
28620
|
+
return new Web3Number(Math.ceil(amountToClose.toNumber()), USDC_TOKEN_DECIMALS);
|
|
28621
|
+
} catch (err) {
|
|
28622
|
+
logger.error(
|
|
28623
|
+
`error calculating position to close to withdraw amount: ${err}`
|
|
28624
|
+
);
|
|
28625
|
+
return new Web3Number(0, USDC_TOKEN_DECIMALS);
|
|
28626
|
+
}
|
|
28627
|
+
};
|
|
28595
28628
|
|
|
28596
28629
|
// src/utils/health-factor-math.ts
|
|
28597
28630
|
var HealthFactorMath = class {
|
|
@@ -35187,6 +35220,17 @@ var VesuExtendedMultiplierStrategy = class _VesuExtendedMultiplierStrategy exten
|
|
|
35187
35220
|
logger.info(
|
|
35188
35221
|
`${_VesuExtendedMultiplierStrategy.name}::moveAssets extendedHoldingAmount: ${extendedHoldingAmount.toNumber()}`
|
|
35189
35222
|
);
|
|
35223
|
+
const extendedPositions = await extendedAdapter.getAllOpenPositions();
|
|
35224
|
+
if (!extendedPositions) {
|
|
35225
|
+
logger.error(`error getting extended positions: ${extendedPositions} while moving assets from extended to vault`);
|
|
35226
|
+
return this.createTransactionResult(
|
|
35227
|
+
[],
|
|
35228
|
+
false,
|
|
35229
|
+
params,
|
|
35230
|
+
"NONE",
|
|
35231
|
+
params.cycleType
|
|
35232
|
+
);
|
|
35233
|
+
}
|
|
35190
35234
|
if (params.amount.abs().greaterThan(extendedHoldingAmount)) {
|
|
35191
35235
|
const leftAmountAfterWithdrawalAmountInAccount = new Web3Number(
|
|
35192
35236
|
Math.ceil(
|
|
@@ -35194,6 +35238,8 @@ var VesuExtendedMultiplierStrategy = class _VesuExtendedMultiplierStrategy exten
|
|
|
35194
35238
|
),
|
|
35195
35239
|
USDC_TOKEN_DECIMALS
|
|
35196
35240
|
);
|
|
35241
|
+
const positionAmountToClose = await calculatePositionToCloseToWithdrawAmount(extendedHoldings, extendedPositions[0], params.amount);
|
|
35242
|
+
logger.info(`positionAmountToClose: ${positionAmountToClose.toNumber()}`);
|
|
35197
35243
|
logger.info(
|
|
35198
35244
|
`${_VesuExtendedMultiplierStrategy.name}::moveAssets leftAmountAfterWithdrawalAmountInAccount: ${leftAmountAfterWithdrawalAmountInAccount.toNumber()}`
|
|
35199
35245
|
);
|
|
@@ -35206,7 +35252,7 @@ var VesuExtendedMultiplierStrategy = class _VesuExtendedMultiplierStrategy exten
|
|
|
35206
35252
|
logger.error(`error fetching order book btc usdc: ${status}`);
|
|
35207
35253
|
priceOfBTC = collateralPrice.price;
|
|
35208
35254
|
}
|
|
35209
|
-
const btcAmount =
|
|
35255
|
+
const btcAmount = positionAmountToClose.dividedBy(priceOfBTC);
|
|
35210
35256
|
const openLongPosition = btcAmount.multipliedBy(3).greaterThan(MINIMUM_EXTENDED_POSITION_SIZE) ? await extendedAdapter.createOrder(
|
|
35211
35257
|
extendedLeverage.toString(),
|
|
35212
35258
|
btcAmount.toNumber(),
|
|
@@ -39988,6 +40034,7 @@ export {
|
|
|
39988
40034
|
calculateDeltaDebtAmount,
|
|
39989
40035
|
calculateExposureDelta,
|
|
39990
40036
|
calculateExtendedLevergae,
|
|
40037
|
+
calculatePositionToCloseToWithdrawAmount,
|
|
39991
40038
|
calculateVesUPositionSizeGivenExtended,
|
|
39992
40039
|
calculateVesuLeverage,
|
|
39993
40040
|
calculateWBTCAmountToMaintainLTV,
|
package/dist/index.d.ts
CHANGED
|
@@ -2800,6 +2800,7 @@ declare const calculateVesUPositionSizeGivenExtended: (extendedPositonValue: num
|
|
|
2800
2800
|
* @returns The debt amount to be repaid
|
|
2801
2801
|
*/
|
|
2802
2802
|
declare const calculateDeltaDebtAmount: (maxLtv: number | undefined, existingVesuCollateral: Web3Number, existingVesuDebt: Web3Number, collateralPrice: number, debtPrice: number, targetHf?: number) => Web3Number | null;
|
|
2803
|
+
declare const calculatePositionToCloseToWithdrawAmount: (extendedBalance: Balance, extendedPositions: Position, amountToWithdraw: Web3Number) => Promise<Web3Number>;
|
|
2803
2804
|
|
|
2804
2805
|
interface EkuboRouteNode {
|
|
2805
2806
|
pool_key: {
|
|
@@ -3146,4 +3147,4 @@ declare class PasswordJsonCryptoUtil {
|
|
|
3146
3147
|
decrypt(encryptedData: string, password: string): any;
|
|
3147
3148
|
}
|
|
3148
3149
|
|
|
3149
|
-
export { type APYInfo, APYType, AUDIT_URL, AUMTypes, AVNU_EXCHANGE, AVNU_EXCHANGE_FOR_LEGACY_USDC, AVNU_LEGACY_SANITIZER, AVNU_MIDDLEWARE, AVNU_QUOTE_URL, AbisConfig, type AccountInfo, type AdapterLeafType, AddressesConfig, type AllAccountsStore, type ApiResponse, type ApproveCallParams, type AssetOperation, AssetOperationStatus, AssetOperationType, AutoCompounderSTRK, AvnuAdapter, type AvnuAdapterConfig, type AvnuSwapCallParams, AvnuWrapper, type Balance, BaseAdapter, type BaseAdapterConfig, BaseStrategy, type CLVaultStrategySettings, type CancelOrderRequest, CommonAdapter, type CommonAdapterConfig, ContractAddr, type CreateOrderRequest, CycleType, type DecreaseLeverParams, Deployer, type DepositParams, type DualActionAmount, type DualTokenInfo, ERC20, EXTENDED_CONTRACT, EXTENDED_SANITIZER, type EkuboBounds, EkuboCLVault, EkuboCLVaultStrategies, type EkuboPoolKey, type EkuboQuote, EkuboQuoter, type EkuboRouteNode, type EkuboSplit, ExitType, ExtendedAdapter, type ExtendedAdapterConfig, type ExtendedApiResponse, ExtendedConfig, ExtendedWrapper, type ExtendedWrapperConfig, type FAQ, FatalError, type FlashloanCallParams, FlowChartColors, type FundingRate, type GenerateCallFn, Global, HyperLSTStrategies, type HyperLSTStrategySettings, type IConfig, type IInvestmentFlow, ILending, type ILendingMetadata, type ILendingPosition, type IProtocol, type IStrategyMetadata, type IncreaseLeverParams, Initializable, type L2Config, LSTAPRService, type LSTStats, type LeafAdapterFn, type LeafData, type LendingToken, type ManageCall, MarginType, type Market, type MarketStats, Midas, Network, type OpenOrder, OrderSide, OrderStatus, OrderStatusReason, OrderType, PRICE_ROUTER, PasswordJsonCryptoUtil, type PlacedOrder, type Position, type PositionAPY, type PositionAmount, type PositionHistory, type PositionInfo, PositionSide, PositionTypeAvnuExtended, Pragma, type PriceInfo, Pricer, PricerBase, PricerFromApi, PricerLST, PricerRedis, Protocols, type RequiredFields, type RequiredKeys, type RequiredStoreConfig, type RiskFactor, type RiskFactorConfig, RiskType, type Route, type RouteNode, SIMPLE_SANITIZER, SIMPLE_SANITIZER_V2, SIMPLE_SANITIZER_VESU_V1_DELEGATIONS, SenseiStrategies, SenseiVault, type SenseiVaultSettings, type SettlementSignature, type SignedWithdrawRequest, type SingleActionAmount, type SingleTokenInfo, StandardMerkleTree, type StandardMerkleTreeData, type StarkDebuggingOrderAmounts, type StarkSettlement, Store, type StoreConfig, type SupportedPosition, type Swap, type SwapInfo, TelegramGroupNotif, TelegramNotif, TimeInForce, type TokenAmount, type TokenInfo, TokenMarketData, type TradingConfig, type TransactionMetadata, type TransactionResult, UNIVERSAL_MANAGE_IDS, UniversalLstMultiplierStrategy, type UniversalManageCall, type UniversalStrategySettings, UnusedBalanceAdapter, type UnusedBalanceAdapterConfig, type UpdateLeverageRequest, VESU_SINGLETON, VESU_V2_MODIFY_POSITION_SANITIZER, type VaultPosition, VesuAdapter, type VesuAdapterConfig, type VesuAmount, VesuAmountDenomination, VesuAmountType, VesuConfig, type VesuDefiSpringRewardsCallParams, VesuExtendedMultiplierStrategy, type VesuExtendedStrategySettings, VesuExtendedTestStrategies, type VesuModifyDelegationCallParams, type VesuModifyPositionCallParams, VesuMultiplyAdapter, type VesuMultiplyAdapterConfig, type VesuMultiplyCallParams, VesuPools, VesuRebalance, type VesuRebalanceSettings, VesuRebalanceStrategies, VesuSupplyOnlyAdapter, type VesuSupplyOnlyAdapterConfig, Web3Number, type WithdrawParams, type WithdrawRequest, ZkLend, _riskFactor, assert, calculateAmountDepositOnExtendedWhenIncurringLosses, calculateAmountDistribution, calculateAmountDistributionForWithdrawal, calculateBTCPriceDelta, calculateDebtAmount, calculateDebtReductionAmountForWithdrawal, calculateDeltaDebtAmount, calculateExposureDelta, calculateExtendedLevergae, calculateVesUPositionSizeGivenExtended, calculateVesuLeverage, calculateWBTCAmountToMaintainLTV, extensionMap, getAPIUsingHeadlessBrowser, getContractDetails, getDefaultStoreConfig, getFAQs, getInvestmentSteps, getMainnetConfig, getNoRiskTags, getRiskColor, getRiskExplaination, getTrovesEndpoint, getVesuSingletonAddress, highlightTextWithLinks, type i257, logger, returnFormattedAmount, toBigInt };
|
|
3150
|
+
export { type APYInfo, APYType, AUDIT_URL, AUMTypes, AVNU_EXCHANGE, AVNU_EXCHANGE_FOR_LEGACY_USDC, AVNU_LEGACY_SANITIZER, AVNU_MIDDLEWARE, AVNU_QUOTE_URL, AbisConfig, type AccountInfo, type AdapterLeafType, AddressesConfig, type AllAccountsStore, type ApiResponse, type ApproveCallParams, type AssetOperation, AssetOperationStatus, AssetOperationType, AutoCompounderSTRK, AvnuAdapter, type AvnuAdapterConfig, type AvnuSwapCallParams, AvnuWrapper, type Balance, BaseAdapter, type BaseAdapterConfig, BaseStrategy, type CLVaultStrategySettings, type CancelOrderRequest, CommonAdapter, type CommonAdapterConfig, ContractAddr, type CreateOrderRequest, CycleType, type DecreaseLeverParams, Deployer, type DepositParams, type DualActionAmount, type DualTokenInfo, ERC20, EXTENDED_CONTRACT, EXTENDED_SANITIZER, type EkuboBounds, EkuboCLVault, EkuboCLVaultStrategies, type EkuboPoolKey, type EkuboQuote, EkuboQuoter, type EkuboRouteNode, type EkuboSplit, ExitType, ExtendedAdapter, type ExtendedAdapterConfig, type ExtendedApiResponse, ExtendedConfig, ExtendedWrapper, type ExtendedWrapperConfig, type FAQ, FatalError, type FlashloanCallParams, FlowChartColors, type FundingRate, type GenerateCallFn, Global, HyperLSTStrategies, type HyperLSTStrategySettings, type IConfig, type IInvestmentFlow, ILending, type ILendingMetadata, type ILendingPosition, type IProtocol, type IStrategyMetadata, type IncreaseLeverParams, Initializable, type L2Config, LSTAPRService, type LSTStats, type LeafAdapterFn, type LeafData, type LendingToken, type ManageCall, MarginType, type Market, type MarketStats, Midas, Network, type OpenOrder, OrderSide, OrderStatus, OrderStatusReason, OrderType, PRICE_ROUTER, PasswordJsonCryptoUtil, type PlacedOrder, type Position, type PositionAPY, type PositionAmount, type PositionHistory, type PositionInfo, PositionSide, PositionTypeAvnuExtended, Pragma, type PriceInfo, Pricer, PricerBase, PricerFromApi, PricerLST, PricerRedis, Protocols, type RequiredFields, type RequiredKeys, type RequiredStoreConfig, type RiskFactor, type RiskFactorConfig, RiskType, type Route, type RouteNode, SIMPLE_SANITIZER, SIMPLE_SANITIZER_V2, SIMPLE_SANITIZER_VESU_V1_DELEGATIONS, SenseiStrategies, SenseiVault, type SenseiVaultSettings, type SettlementSignature, type SignedWithdrawRequest, type SingleActionAmount, type SingleTokenInfo, StandardMerkleTree, type StandardMerkleTreeData, type StarkDebuggingOrderAmounts, type StarkSettlement, Store, type StoreConfig, type SupportedPosition, type Swap, type SwapInfo, TelegramGroupNotif, TelegramNotif, TimeInForce, type TokenAmount, type TokenInfo, TokenMarketData, type TradingConfig, type TransactionMetadata, type TransactionResult, UNIVERSAL_MANAGE_IDS, UniversalLstMultiplierStrategy, type UniversalManageCall, type UniversalStrategySettings, UnusedBalanceAdapter, type UnusedBalanceAdapterConfig, type UpdateLeverageRequest, VESU_SINGLETON, VESU_V2_MODIFY_POSITION_SANITIZER, type VaultPosition, VesuAdapter, type VesuAdapterConfig, type VesuAmount, VesuAmountDenomination, VesuAmountType, VesuConfig, type VesuDefiSpringRewardsCallParams, VesuExtendedMultiplierStrategy, type VesuExtendedStrategySettings, VesuExtendedTestStrategies, type VesuModifyDelegationCallParams, type VesuModifyPositionCallParams, VesuMultiplyAdapter, type VesuMultiplyAdapterConfig, type VesuMultiplyCallParams, VesuPools, VesuRebalance, type VesuRebalanceSettings, VesuRebalanceStrategies, VesuSupplyOnlyAdapter, type VesuSupplyOnlyAdapterConfig, Web3Number, type WithdrawParams, type WithdrawRequest, ZkLend, _riskFactor, assert, calculateAmountDepositOnExtendedWhenIncurringLosses, calculateAmountDistribution, calculateAmountDistributionForWithdrawal, calculateBTCPriceDelta, calculateDebtAmount, calculateDebtReductionAmountForWithdrawal, calculateDeltaDebtAmount, calculateExposureDelta, calculateExtendedLevergae, calculatePositionToCloseToWithdrawAmount, calculateVesUPositionSizeGivenExtended, calculateVesuLeverage, calculateWBTCAmountToMaintainLTV, extensionMap, getAPIUsingHeadlessBrowser, getContractDetails, getDefaultStoreConfig, getFAQs, getInvestmentSteps, getMainnetConfig, getNoRiskTags, getRiskColor, getRiskExplaination, getTrovesEndpoint, getVesuSingletonAddress, highlightTextWithLinks, type i257, logger, returnFormattedAmount, toBigInt };
|
package/dist/index.js
CHANGED
|
@@ -127,6 +127,7 @@ __export(index_exports, {
|
|
|
127
127
|
calculateDeltaDebtAmount: () => calculateDeltaDebtAmount,
|
|
128
128
|
calculateExposureDelta: () => calculateExposureDelta,
|
|
129
129
|
calculateExtendedLevergae: () => calculateExtendedLevergae,
|
|
130
|
+
calculatePositionToCloseToWithdrawAmount: () => calculatePositionToCloseToWithdrawAmount,
|
|
130
131
|
calculateVesUPositionSizeGivenExtended: () => calculateVesUPositionSizeGivenExtended,
|
|
131
132
|
calculateVesuLeverage: () => calculateVesuLeverage,
|
|
132
133
|
calculateWBTCAmountToMaintainLTV: () => calculateWBTCAmountToMaintainLTV,
|
|
@@ -28282,7 +28283,9 @@ var calculateAmountDistributionForWithdrawal = async (amountInUsdc, collateralPr
|
|
|
28282
28283
|
vesu_leverage
|
|
28283
28284
|
};
|
|
28284
28285
|
} catch (err) {
|
|
28285
|
-
logger.error(
|
|
28286
|
+
logger.error(
|
|
28287
|
+
`error calculating amount distribution for withdrawal: ${err}`
|
|
28288
|
+
);
|
|
28286
28289
|
return null;
|
|
28287
28290
|
}
|
|
28288
28291
|
};
|
|
@@ -28335,15 +28338,22 @@ var calculateAmountDepositOnExtendedWhenIncurringLosses = async (client) => {
|
|
|
28335
28338
|
const extended_leverage = calculateExtendedLevergae();
|
|
28336
28339
|
const latestPosition = (await client.getPositions()).data.pop();
|
|
28337
28340
|
if (!extendedHoldings || !latestPosition) {
|
|
28338
|
-
logger.error(
|
|
28341
|
+
logger.error(
|
|
28342
|
+
`error getting extended position: extendedHoldings=${extendedHoldings}, latestPosition=${latestPosition}`
|
|
28343
|
+
);
|
|
28339
28344
|
return null;
|
|
28340
28345
|
}
|
|
28341
|
-
const positionValueInUSD = new Web3Number(
|
|
28346
|
+
const positionValueInUSD = new Web3Number(
|
|
28347
|
+
latestPosition.value,
|
|
28348
|
+
USDC_TOKEN_DECIMALS
|
|
28349
|
+
);
|
|
28342
28350
|
const equity = extendedHoldings.data.equity;
|
|
28343
28351
|
const deposit = positionValueInUSD.dividedBy(extended_leverage).minus(equity).toFixed(2);
|
|
28344
28352
|
return new Web3Number(deposit, USDC_TOKEN_DECIMALS);
|
|
28345
28353
|
} catch (err) {
|
|
28346
|
-
logger.error(
|
|
28354
|
+
logger.error(
|
|
28355
|
+
`error calculating amount deposit on extended when incurring losses: ${err}`
|
|
28356
|
+
);
|
|
28347
28357
|
return null;
|
|
28348
28358
|
}
|
|
28349
28359
|
};
|
|
@@ -28395,6 +28405,30 @@ var calculateDeltaDebtAmount = (maxLtv = MAX_LTV_BTC_USDC, existingVesuCollatera
|
|
|
28395
28405
|
return null;
|
|
28396
28406
|
}
|
|
28397
28407
|
};
|
|
28408
|
+
var calculatePositionToCloseToWithdrawAmount = async (extendedBalance, extendedPositions, amountToWithdraw) => {
|
|
28409
|
+
try {
|
|
28410
|
+
const extendedPosition = extendedPositions.value;
|
|
28411
|
+
const unrealisedPnl = new Web3Number(
|
|
28412
|
+
extendedBalance.unrealisedPnl,
|
|
28413
|
+
USDC_TOKEN_DECIMALS
|
|
28414
|
+
);
|
|
28415
|
+
const availableForWithdrawal = new Web3Number(
|
|
28416
|
+
extendedBalance.availableForWithdrawal,
|
|
28417
|
+
USDC_TOKEN_DECIMALS
|
|
28418
|
+
);
|
|
28419
|
+
const netAmount = amountToWithdraw.minus(availableForWithdrawal);
|
|
28420
|
+
const upnlPercent = unrealisedPnl.dividedBy(extendedPosition);
|
|
28421
|
+
console.log(`upnlPercent: ${upnlPercent}`);
|
|
28422
|
+
const denm = upnlPercent.multipliedBy(3).plus(1);
|
|
28423
|
+
const amountToClose = netAmount.multipliedBy(3).dividedBy(denm);
|
|
28424
|
+
return new Web3Number(Math.ceil(amountToClose.toNumber()), USDC_TOKEN_DECIMALS);
|
|
28425
|
+
} catch (err) {
|
|
28426
|
+
logger.error(
|
|
28427
|
+
`error calculating position to close to withdraw amount: ${err}`
|
|
28428
|
+
);
|
|
28429
|
+
return new Web3Number(0, USDC_TOKEN_DECIMALS);
|
|
28430
|
+
}
|
|
28431
|
+
};
|
|
28398
28432
|
|
|
28399
28433
|
// src/utils/health-factor-math.ts
|
|
28400
28434
|
var HealthFactorMath = class {
|
|
@@ -35345,6 +35379,17 @@ var VesuExtendedMultiplierStrategy = class _VesuExtendedMultiplierStrategy exten
|
|
|
35345
35379
|
logger.info(
|
|
35346
35380
|
`${_VesuExtendedMultiplierStrategy.name}::moveAssets extendedHoldingAmount: ${extendedHoldingAmount.toNumber()}`
|
|
35347
35381
|
);
|
|
35382
|
+
const extendedPositions = await extendedAdapter.getAllOpenPositions();
|
|
35383
|
+
if (!extendedPositions) {
|
|
35384
|
+
logger.error(`error getting extended positions: ${extendedPositions} while moving assets from extended to vault`);
|
|
35385
|
+
return this.createTransactionResult(
|
|
35386
|
+
[],
|
|
35387
|
+
false,
|
|
35388
|
+
params,
|
|
35389
|
+
"NONE",
|
|
35390
|
+
params.cycleType
|
|
35391
|
+
);
|
|
35392
|
+
}
|
|
35348
35393
|
if (params.amount.abs().greaterThan(extendedHoldingAmount)) {
|
|
35349
35394
|
const leftAmountAfterWithdrawalAmountInAccount = new Web3Number(
|
|
35350
35395
|
Math.ceil(
|
|
@@ -35352,6 +35397,8 @@ var VesuExtendedMultiplierStrategy = class _VesuExtendedMultiplierStrategy exten
|
|
|
35352
35397
|
),
|
|
35353
35398
|
USDC_TOKEN_DECIMALS
|
|
35354
35399
|
);
|
|
35400
|
+
const positionAmountToClose = await calculatePositionToCloseToWithdrawAmount(extendedHoldings, extendedPositions[0], params.amount);
|
|
35401
|
+
logger.info(`positionAmountToClose: ${positionAmountToClose.toNumber()}`);
|
|
35355
35402
|
logger.info(
|
|
35356
35403
|
`${_VesuExtendedMultiplierStrategy.name}::moveAssets leftAmountAfterWithdrawalAmountInAccount: ${leftAmountAfterWithdrawalAmountInAccount.toNumber()}`
|
|
35357
35404
|
);
|
|
@@ -35364,7 +35411,7 @@ var VesuExtendedMultiplierStrategy = class _VesuExtendedMultiplierStrategy exten
|
|
|
35364
35411
|
logger.error(`error fetching order book btc usdc: ${status}`);
|
|
35365
35412
|
priceOfBTC = collateralPrice.price;
|
|
35366
35413
|
}
|
|
35367
|
-
const btcAmount =
|
|
35414
|
+
const btcAmount = positionAmountToClose.dividedBy(priceOfBTC);
|
|
35368
35415
|
const openLongPosition = btcAmount.multipliedBy(3).greaterThan(MINIMUM_EXTENDED_POSITION_SIZE) ? await extendedAdapter.createOrder(
|
|
35369
35416
|
extendedLeverage.toString(),
|
|
35370
35417
|
btcAmount.toNumber(),
|
|
@@ -40713,6 +40760,7 @@ var deployer_default = Deployer;
|
|
|
40713
40760
|
calculateDeltaDebtAmount,
|
|
40714
40761
|
calculateExposureDelta,
|
|
40715
40762
|
calculateExtendedLevergae,
|
|
40763
|
+
calculatePositionToCloseToWithdrawAmount,
|
|
40716
40764
|
calculateVesUPositionSizeGivenExtended,
|
|
40717
40765
|
calculateVesuLeverage,
|
|
40718
40766
|
calculateWBTCAmountToMaintainLTV,
|
package/dist/index.mjs
CHANGED
|
@@ -28135,7 +28135,9 @@ var calculateAmountDistributionForWithdrawal = async (amountInUsdc, collateralPr
|
|
|
28135
28135
|
vesu_leverage
|
|
28136
28136
|
};
|
|
28137
28137
|
} catch (err) {
|
|
28138
|
-
logger.error(
|
|
28138
|
+
logger.error(
|
|
28139
|
+
`error calculating amount distribution for withdrawal: ${err}`
|
|
28140
|
+
);
|
|
28139
28141
|
return null;
|
|
28140
28142
|
}
|
|
28141
28143
|
};
|
|
@@ -28188,15 +28190,22 @@ var calculateAmountDepositOnExtendedWhenIncurringLosses = async (client) => {
|
|
|
28188
28190
|
const extended_leverage = calculateExtendedLevergae();
|
|
28189
28191
|
const latestPosition = (await client.getPositions()).data.pop();
|
|
28190
28192
|
if (!extendedHoldings || !latestPosition) {
|
|
28191
|
-
logger.error(
|
|
28193
|
+
logger.error(
|
|
28194
|
+
`error getting extended position: extendedHoldings=${extendedHoldings}, latestPosition=${latestPosition}`
|
|
28195
|
+
);
|
|
28192
28196
|
return null;
|
|
28193
28197
|
}
|
|
28194
|
-
const positionValueInUSD = new Web3Number(
|
|
28198
|
+
const positionValueInUSD = new Web3Number(
|
|
28199
|
+
latestPosition.value,
|
|
28200
|
+
USDC_TOKEN_DECIMALS
|
|
28201
|
+
);
|
|
28195
28202
|
const equity = extendedHoldings.data.equity;
|
|
28196
28203
|
const deposit = positionValueInUSD.dividedBy(extended_leverage).minus(equity).toFixed(2);
|
|
28197
28204
|
return new Web3Number(deposit, USDC_TOKEN_DECIMALS);
|
|
28198
28205
|
} catch (err) {
|
|
28199
|
-
logger.error(
|
|
28206
|
+
logger.error(
|
|
28207
|
+
`error calculating amount deposit on extended when incurring losses: ${err}`
|
|
28208
|
+
);
|
|
28200
28209
|
return null;
|
|
28201
28210
|
}
|
|
28202
28211
|
};
|
|
@@ -28248,6 +28257,30 @@ var calculateDeltaDebtAmount = (maxLtv = MAX_LTV_BTC_USDC, existingVesuCollatera
|
|
|
28248
28257
|
return null;
|
|
28249
28258
|
}
|
|
28250
28259
|
};
|
|
28260
|
+
var calculatePositionToCloseToWithdrawAmount = async (extendedBalance, extendedPositions, amountToWithdraw) => {
|
|
28261
|
+
try {
|
|
28262
|
+
const extendedPosition = extendedPositions.value;
|
|
28263
|
+
const unrealisedPnl = new Web3Number(
|
|
28264
|
+
extendedBalance.unrealisedPnl,
|
|
28265
|
+
USDC_TOKEN_DECIMALS
|
|
28266
|
+
);
|
|
28267
|
+
const availableForWithdrawal = new Web3Number(
|
|
28268
|
+
extendedBalance.availableForWithdrawal,
|
|
28269
|
+
USDC_TOKEN_DECIMALS
|
|
28270
|
+
);
|
|
28271
|
+
const netAmount = amountToWithdraw.minus(availableForWithdrawal);
|
|
28272
|
+
const upnlPercent = unrealisedPnl.dividedBy(extendedPosition);
|
|
28273
|
+
console.log(`upnlPercent: ${upnlPercent}`);
|
|
28274
|
+
const denm = upnlPercent.multipliedBy(3).plus(1);
|
|
28275
|
+
const amountToClose = netAmount.multipliedBy(3).dividedBy(denm);
|
|
28276
|
+
return new Web3Number(Math.ceil(amountToClose.toNumber()), USDC_TOKEN_DECIMALS);
|
|
28277
|
+
} catch (err) {
|
|
28278
|
+
logger.error(
|
|
28279
|
+
`error calculating position to close to withdraw amount: ${err}`
|
|
28280
|
+
);
|
|
28281
|
+
return new Web3Number(0, USDC_TOKEN_DECIMALS);
|
|
28282
|
+
}
|
|
28283
|
+
};
|
|
28251
28284
|
|
|
28252
28285
|
// src/utils/health-factor-math.ts
|
|
28253
28286
|
var HealthFactorMath = class {
|
|
@@ -35198,6 +35231,17 @@ var VesuExtendedMultiplierStrategy = class _VesuExtendedMultiplierStrategy exten
|
|
|
35198
35231
|
logger.info(
|
|
35199
35232
|
`${_VesuExtendedMultiplierStrategy.name}::moveAssets extendedHoldingAmount: ${extendedHoldingAmount.toNumber()}`
|
|
35200
35233
|
);
|
|
35234
|
+
const extendedPositions = await extendedAdapter.getAllOpenPositions();
|
|
35235
|
+
if (!extendedPositions) {
|
|
35236
|
+
logger.error(`error getting extended positions: ${extendedPositions} while moving assets from extended to vault`);
|
|
35237
|
+
return this.createTransactionResult(
|
|
35238
|
+
[],
|
|
35239
|
+
false,
|
|
35240
|
+
params,
|
|
35241
|
+
"NONE",
|
|
35242
|
+
params.cycleType
|
|
35243
|
+
);
|
|
35244
|
+
}
|
|
35201
35245
|
if (params.amount.abs().greaterThan(extendedHoldingAmount)) {
|
|
35202
35246
|
const leftAmountAfterWithdrawalAmountInAccount = new Web3Number(
|
|
35203
35247
|
Math.ceil(
|
|
@@ -35205,6 +35249,8 @@ var VesuExtendedMultiplierStrategy = class _VesuExtendedMultiplierStrategy exten
|
|
|
35205
35249
|
),
|
|
35206
35250
|
USDC_TOKEN_DECIMALS
|
|
35207
35251
|
);
|
|
35252
|
+
const positionAmountToClose = await calculatePositionToCloseToWithdrawAmount(extendedHoldings, extendedPositions[0], params.amount);
|
|
35253
|
+
logger.info(`positionAmountToClose: ${positionAmountToClose.toNumber()}`);
|
|
35208
35254
|
logger.info(
|
|
35209
35255
|
`${_VesuExtendedMultiplierStrategy.name}::moveAssets leftAmountAfterWithdrawalAmountInAccount: ${leftAmountAfterWithdrawalAmountInAccount.toNumber()}`
|
|
35210
35256
|
);
|
|
@@ -35217,7 +35263,7 @@ var VesuExtendedMultiplierStrategy = class _VesuExtendedMultiplierStrategy exten
|
|
|
35217
35263
|
logger.error(`error fetching order book btc usdc: ${status}`);
|
|
35218
35264
|
priceOfBTC = collateralPrice.price;
|
|
35219
35265
|
}
|
|
35220
|
-
const btcAmount =
|
|
35266
|
+
const btcAmount = positionAmountToClose.dividedBy(priceOfBTC);
|
|
35221
35267
|
const openLongPosition = btcAmount.multipliedBy(3).greaterThan(MINIMUM_EXTENDED_POSITION_SIZE) ? await extendedAdapter.createOrder(
|
|
35222
35268
|
extendedLeverage.toString(),
|
|
35223
35269
|
btcAmount.toNumber(),
|
|
@@ -40565,6 +40611,7 @@ export {
|
|
|
40565
40611
|
calculateDeltaDebtAmount,
|
|
40566
40612
|
calculateExposureDelta,
|
|
40567
40613
|
calculateExtendedLevergae,
|
|
40614
|
+
calculatePositionToCloseToWithdrawAmount,
|
|
40568
40615
|
calculateVesUPositionSizeGivenExtended,
|
|
40569
40616
|
calculateVesuLeverage,
|
|
40570
40617
|
calculateWBTCAmountToMaintainLTV,
|
package/package.json
CHANGED
|
@@ -9,6 +9,8 @@ import {
|
|
|
9
9
|
USDC_TOKEN_DECIMALS,
|
|
10
10
|
MAX_LIQUIDATION_RATIO,
|
|
11
11
|
} from "./constants";
|
|
12
|
+
import { ExtendedAdapter } from "../../universal-adapters/extended-adapter";
|
|
13
|
+
import { Balance } from "@/modules/ExtendedWrapperSDk";
|
|
12
14
|
import { Web3Number } from "@/dataTypes";
|
|
13
15
|
import { Position } from "@/modules/ExtendedWrapperSDk";
|
|
14
16
|
// import { getAllOpenPositionsExtended } from "../services/extendedService";
|
|
@@ -126,9 +128,9 @@ export const calculateAmountDistributionForWithdrawal = async (
|
|
|
126
128
|
return null;
|
|
127
129
|
}
|
|
128
130
|
const extendedExposureUSD =
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
131
|
+
extendedPosition.length > 0
|
|
132
|
+
? new Web3Number(extendedPosition[0].value, USDC_TOKEN_DECIMALS)
|
|
133
|
+
: new Web3Number(0, USDC_TOKEN_DECIMALS);
|
|
132
134
|
const vesuExposureUSD = collateralUnits.multipliedBy(collateralPrice);
|
|
133
135
|
if (vesuExposureUSD.lessThan(0)) {
|
|
134
136
|
return {
|
|
@@ -153,8 +155,7 @@ export const calculateAmountDistributionForWithdrawal = async (
|
|
|
153
155
|
console.log("the vesu leverage is", vesu_leverage);
|
|
154
156
|
const numerator1 = amountInUsdc.multipliedBy(extended_leverage);
|
|
155
157
|
const numerator2 = vesuExposureUSD;
|
|
156
|
-
const numerator3 = extendedExposureUSD
|
|
157
|
-
.multipliedBy(-1);
|
|
158
|
+
const numerator3 = extendedExposureUSD.multipliedBy(-1);
|
|
158
159
|
const finalNumerator = numerator1.plus(numerator2).plus(numerator3);
|
|
159
160
|
const denominator = extended_leverage + vesu_leverage;
|
|
160
161
|
const vesuAmountInUSDC = finalNumerator.dividedBy(denominator);
|
|
@@ -174,7 +175,9 @@ export const calculateAmountDistributionForWithdrawal = async (
|
|
|
174
175
|
vesu_leverage,
|
|
175
176
|
};
|
|
176
177
|
} catch (err) {
|
|
177
|
-
logger.error(
|
|
178
|
+
logger.error(
|
|
179
|
+
`error calculating amount distribution for withdrawal: ${err}`
|
|
180
|
+
);
|
|
178
181
|
return null;
|
|
179
182
|
}
|
|
180
183
|
};
|
|
@@ -226,8 +229,8 @@ export const calculateDebtAmount = (
|
|
|
226
229
|
try {
|
|
227
230
|
// => X = (((collateral + legDepositAmount) * collateralPrice * ltv) - (debt * debtPrice * target hf)) / (target hf - ltv)
|
|
228
231
|
const addedCollateral = addedAmount.multipliedBy(isDeposit ? 1 : -1);
|
|
229
|
-
const numerator1 =
|
|
230
|
-
.plus(addedCollateral)
|
|
232
|
+
const numerator1 = collateralAmount
|
|
233
|
+
.plus(addedCollateral)
|
|
231
234
|
.multipliedBy(collateralPrice)
|
|
232
235
|
.multipliedBy(maxLtv);
|
|
233
236
|
const numerator2 = debtAmount
|
|
@@ -304,15 +307,25 @@ export const calculateAmountDepositOnExtendedWhenIncurringLosses = async (
|
|
|
304
307
|
const extended_leverage = calculateExtendedLevergae();
|
|
305
308
|
const latestPosition = (await client.getPositions()).data.pop();
|
|
306
309
|
if (!extendedHoldings || !latestPosition) {
|
|
307
|
-
logger.error(
|
|
310
|
+
logger.error(
|
|
311
|
+
`error getting extended position: extendedHoldings=${extendedHoldings}, latestPosition=${latestPosition}`
|
|
312
|
+
);
|
|
308
313
|
return null;
|
|
309
314
|
}
|
|
310
|
-
const positionValueInUSD = new Web3Number(
|
|
315
|
+
const positionValueInUSD = new Web3Number(
|
|
316
|
+
latestPosition.value,
|
|
317
|
+
USDC_TOKEN_DECIMALS
|
|
318
|
+
);
|
|
311
319
|
const equity = extendedHoldings.data.equity;
|
|
312
|
-
const deposit = positionValueInUSD
|
|
320
|
+
const deposit = positionValueInUSD
|
|
321
|
+
.dividedBy(extended_leverage)
|
|
322
|
+
.minus(equity)
|
|
323
|
+
.toFixed(2);
|
|
313
324
|
return new Web3Number(deposit, USDC_TOKEN_DECIMALS);
|
|
314
325
|
} catch (err) {
|
|
315
|
-
logger.error(
|
|
326
|
+
logger.error(
|
|
327
|
+
`error calculating amount deposit on extended when incurring losses: ${err}`
|
|
328
|
+
);
|
|
316
329
|
return null;
|
|
317
330
|
}
|
|
318
331
|
};
|
|
@@ -324,9 +337,9 @@ export const calculateAmountDepositOnExtendedWhenIncurringLosses = async (
|
|
|
324
337
|
* @param collateralAmount in collateral units
|
|
325
338
|
* @param debtAmount in debt units
|
|
326
339
|
* @param debtPrice in usd
|
|
327
|
-
* @param maxLtv
|
|
340
|
+
* @param maxLtv
|
|
328
341
|
* @param collateralPrice in usd
|
|
329
|
-
* @param targetHF
|
|
342
|
+
* @param targetHF
|
|
330
343
|
* @returns deltaCollateralAmountUnits in collateral units
|
|
331
344
|
* null if there is an error
|
|
332
345
|
*/
|
|
@@ -336,26 +349,30 @@ export const calculateWBTCAmountToMaintainLTV = (
|
|
|
336
349
|
debtPrice: number,
|
|
337
350
|
maxLtv: number = MAX_LIQUIDATION_RATIO,
|
|
338
351
|
collateralPrice: number,
|
|
339
|
-
targetHF: number = TARGET_HF
|
|
340
|
-
) =>{
|
|
352
|
+
targetHF: number = TARGET_HF
|
|
353
|
+
) => {
|
|
341
354
|
try {
|
|
342
|
-
const numerator1 =
|
|
355
|
+
const numerator1 = collateralAmount
|
|
343
356
|
.multipliedBy(collateralPrice)
|
|
344
|
-
.multipliedBy(maxLtv)
|
|
357
|
+
.multipliedBy(maxLtv);
|
|
345
358
|
const numerator2 = debtAmount
|
|
346
359
|
.multipliedBy(debtPrice)
|
|
347
360
|
.multipliedBy(targetHF);
|
|
348
361
|
const denominator = maxLtv;
|
|
349
|
-
const collateralAmountToMaintainLTV = numerator2
|
|
362
|
+
const collateralAmountToMaintainLTV = numerator2
|
|
363
|
+
.minus(numerator1)
|
|
364
|
+
.dividedBy(denominator);
|
|
350
365
|
let deltaCollateralAmountUnits = new Web3Number(
|
|
351
|
-
collateralAmountToMaintainLTV
|
|
366
|
+
collateralAmountToMaintainLTV
|
|
367
|
+
.dividedBy(collateralPrice)
|
|
368
|
+
.toFixed(WBTC_TOKEN_DECIMALS),
|
|
352
369
|
WBTC_TOKEN_DECIMALS
|
|
353
370
|
);
|
|
354
|
-
return {deltaCollateralAmountUnits};
|
|
371
|
+
return { deltaCollateralAmountUnits };
|
|
355
372
|
} catch (err) {
|
|
356
373
|
return { deltaCollateralAmountUnits: null };
|
|
357
374
|
}
|
|
358
|
-
}
|
|
375
|
+
};
|
|
359
376
|
|
|
360
377
|
export const calculateExposureDelta = (
|
|
361
378
|
exposure_extended: number,
|
|
@@ -392,14 +409,17 @@ export const calculateVesUPositionSizeGivenExtended = (
|
|
|
392
409
|
const extendedLeverage = calculateExtendedLevergae();
|
|
393
410
|
const vesuLeverage = calculateVesuLeverage();
|
|
394
411
|
const extendedAmount = extendedHoldingAmount;
|
|
395
|
-
const extendedAmountInBTC = extendedAmount
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
.plus(extendedPositonValue)
|
|
412
|
+
const extendedAmountInBTC = extendedAmount.dividedBy(collateralPrice);
|
|
413
|
+
const numerator1 = extendedAmount
|
|
414
|
+
.multipliedBy(extendedLeverage)
|
|
415
|
+
.plus(extendedPositonValue);
|
|
399
416
|
const numerator2 = collateralAmount
|
|
400
417
|
.multipliedBy(collateralPrice)
|
|
401
418
|
.multipliedBy(-1);
|
|
402
|
-
const vesuAmountInUsd = numerator1
|
|
419
|
+
const vesuAmountInUsd = numerator1
|
|
420
|
+
.plus(numerator2)
|
|
421
|
+
.dividedBy(vesuLeverage)
|
|
422
|
+
.plus(vesuDebtAmountToBeRepaid);
|
|
403
423
|
const vesuAmountInBTC = vesuAmountInUsd
|
|
404
424
|
.dividedBy(collateralPrice)
|
|
405
425
|
.toFixed(WBTC_TOKEN_DECIMALS);
|
|
@@ -422,19 +442,54 @@ export const calculateVesUPositionSizeGivenExtended = (
|
|
|
422
442
|
*/
|
|
423
443
|
export const calculateDeltaDebtAmount = (
|
|
424
444
|
maxLtv: number = MAX_LTV_BTC_USDC,
|
|
425
|
-
existingVesuCollateral: Web3Number,
|
|
445
|
+
existingVesuCollateral: Web3Number,
|
|
426
446
|
existingVesuDebt: Web3Number,
|
|
427
447
|
collateralPrice: number,
|
|
428
448
|
debtPrice: number,
|
|
429
|
-
targetHf: number = TARGET_HF
|
|
449
|
+
targetHf: number = TARGET_HF
|
|
430
450
|
) => {
|
|
431
451
|
try {
|
|
432
|
-
const term1 = existingVesuCollateral
|
|
433
|
-
|
|
452
|
+
const term1 = existingVesuCollateral
|
|
453
|
+
.multipliedBy(collateralPrice)
|
|
454
|
+
.multipliedBy(maxLtv)
|
|
455
|
+
.dividedBy(targetHf);
|
|
456
|
+
const term2 = existingVesuDebt
|
|
457
|
+
.multipliedBy(debtPrice)
|
|
458
|
+
.multipliedBy(targetHf)
|
|
459
|
+
.multipliedBy(-1);
|
|
434
460
|
const debtAmountToBeRepaid = term1.plus(term2).dividedBy(targetHf);
|
|
435
461
|
return debtAmountToBeRepaid;
|
|
436
462
|
} catch (err) {
|
|
437
463
|
logger.error(`error calculating delta position: ${err}`);
|
|
438
464
|
return null;
|
|
439
465
|
}
|
|
440
|
-
};
|
|
466
|
+
};
|
|
467
|
+
|
|
468
|
+
export const calculatePositionToCloseToWithdrawAmount = async (
|
|
469
|
+
extendedBalance: Balance,
|
|
470
|
+
extendedPositions: Position,
|
|
471
|
+
amountToWithdraw: Web3Number
|
|
472
|
+
) => {
|
|
473
|
+
try {
|
|
474
|
+
const extendedPosition = extendedPositions.value;
|
|
475
|
+
const unrealisedPnl = new Web3Number(
|
|
476
|
+
extendedBalance.unrealisedPnl,
|
|
477
|
+
USDC_TOKEN_DECIMALS
|
|
478
|
+
);
|
|
479
|
+
const availableForWithdrawal = new Web3Number(
|
|
480
|
+
extendedBalance.availableForWithdrawal,
|
|
481
|
+
USDC_TOKEN_DECIMALS
|
|
482
|
+
);
|
|
483
|
+
const netAmount = amountToWithdraw.minus(availableForWithdrawal);
|
|
484
|
+
const upnlPercent = unrealisedPnl.dividedBy(extendedPosition);
|
|
485
|
+
console.log(`upnlPercent: ${upnlPercent}`);
|
|
486
|
+
const denm = upnlPercent.multipliedBy(3).plus(1);
|
|
487
|
+
const amountToClose = netAmount.multipliedBy(3).dividedBy(denm);
|
|
488
|
+
return new Web3Number(Math.ceil(amountToClose.toNumber()), USDC_TOKEN_DECIMALS);
|
|
489
|
+
} catch (err) {
|
|
490
|
+
logger.error(
|
|
491
|
+
`error calculating position to close to withdraw amount: ${err}`
|
|
492
|
+
);
|
|
493
|
+
return new Web3Number(0, USDC_TOKEN_DECIMALS);
|
|
494
|
+
}
|
|
495
|
+
};
|
|
@@ -8,7 +8,7 @@ import {
|
|
|
8
8
|
UNIVERSAL_MANAGE_IDS,
|
|
9
9
|
UniversalStrategySettings,
|
|
10
10
|
} from "../universal-strategy";
|
|
11
|
-
import { calculateDebtAmount, calculateDeltaDebtAmount, calculateExtendedLevergae } from "./utils/helper";
|
|
11
|
+
import { calculateDebtAmount, calculateDeltaDebtAmount, calculateExtendedLevergae, calculatePositionToCloseToWithdrawAmount } from "./utils/helper";
|
|
12
12
|
import { logger } from "@/utils";
|
|
13
13
|
import { AUDIT_URL } from "../universal-lst-muliplier-strategy";
|
|
14
14
|
import { getNoRiskTags } from "@/interfaces";
|
|
@@ -1142,6 +1142,17 @@ export class VesuExtendedMultiplierStrategy<
|
|
|
1142
1142
|
VesuExtendedMultiplierStrategy.name
|
|
1143
1143
|
}::moveAssets extendedHoldingAmount: ${extendedHoldingAmount.toNumber()}`
|
|
1144
1144
|
);
|
|
1145
|
+
const extendedPositions = await extendedAdapter.getAllOpenPositions();
|
|
1146
|
+
if (!extendedPositions) {
|
|
1147
|
+
logger.error(`error getting extended positions: ${extendedPositions} while moving assets from extended to vault`);
|
|
1148
|
+
return this.createTransactionResult(
|
|
1149
|
+
[],
|
|
1150
|
+
false,
|
|
1151
|
+
params,
|
|
1152
|
+
"NONE",
|
|
1153
|
+
params.cycleType
|
|
1154
|
+
);
|
|
1155
|
+
}
|
|
1145
1156
|
if (params.amount.abs().greaterThan(extendedHoldingAmount)) {
|
|
1146
1157
|
const leftAmountAfterWithdrawalAmountInAccount = new Web3Number(
|
|
1147
1158
|
Math.ceil(
|
|
@@ -1149,6 +1160,8 @@ export class VesuExtendedMultiplierStrategy<
|
|
|
1149
1160
|
),
|
|
1150
1161
|
USDC_TOKEN_DECIMALS
|
|
1151
1162
|
);
|
|
1163
|
+
const positionAmountToClose = await calculatePositionToCloseToWithdrawAmount(extendedHoldings, extendedPositions[0], params.amount);
|
|
1164
|
+
logger.info(`positionAmountToClose: ${positionAmountToClose.toNumber()}`);
|
|
1152
1165
|
logger.info(
|
|
1153
1166
|
`${
|
|
1154
1167
|
VesuExtendedMultiplierStrategy.name
|
|
@@ -1165,7 +1178,7 @@ export class VesuExtendedMultiplierStrategy<
|
|
|
1165
1178
|
priceOfBTC = collateralPrice.price;
|
|
1166
1179
|
}
|
|
1167
1180
|
const btcAmount =
|
|
1168
|
-
|
|
1181
|
+
positionAmountToClose.dividedBy(priceOfBTC);
|
|
1169
1182
|
/**
|
|
1170
1183
|
* If amount for withdrawal is greater than the amount in extended available for withdrawal,
|
|
1171
1184
|
* then we need to open a long position depending on the difference between the two
|