@strkfarm/sdk 2.0.0-dev.23 → 2.0.0-dev.25
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 +56 -5
- package/dist/index.browser.mjs +56 -5
- package/dist/index.d.ts +2 -1
- package/dist/index.js +57 -5
- package/dist/index.mjs +56 -5
- package/package.json +1 -1
- package/src/strategies/vesu-extended-strategy/utils/helper.ts +86 -32
- package/src/strategies/vesu-extended-strategy/vesu-extended-strategy.tsx +32 -5
|
@@ -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,26 @@ spurious results.`);
|
|
|
92636
92646
|
return null;
|
|
92637
92647
|
}
|
|
92638
92648
|
};
|
|
92649
|
+
var calculatePositionToCloseToWithdrawAmount = async (extendedBalance, extendedPositions, amountToWithdraw) => {
|
|
92650
|
+
try {
|
|
92651
|
+
const extendedLeverage = calculateExtendedLevergae();
|
|
92652
|
+
const extendedPosition = extendedPositions.value;
|
|
92653
|
+
const marginRequired = new Web3Number(extendedBalance.initialMargin, USDC_TOKEN_DECIMALS);
|
|
92654
|
+
const unrealisedPnl = new Web3Number(extendedBalance.unrealisedPnl, USDC_TOKEN_DECIMALS);
|
|
92655
|
+
const availableForWithdrawal = new Web3Number(extendedBalance.availableForWithdrawal, USDC_TOKEN_DECIMALS);
|
|
92656
|
+
const upnlPercent = unrealisedPnl.dividedBy(extendedPosition);
|
|
92657
|
+
console.log(`upnlPercent: ${upnlPercent}`);
|
|
92658
|
+
const walletBalance = new Web3Number(extendedBalance.balance, USDC_TOKEN_DECIMALS);
|
|
92659
|
+
const term1 = marginRequired.minus(walletBalance).minus(availableForWithdrawal).plus(amountToWithdraw).multipliedBy(extendedLeverage);
|
|
92660
|
+
const denominator = upnlPercent.multipliedBy(extendedLeverage).plus(1);
|
|
92661
|
+
return new Web3Number(Math.ceil(term1.dividedBy(denominator).dividedBy(extendedLeverage).toNumber()), USDC_TOKEN_DECIMALS);
|
|
92662
|
+
} catch (err2) {
|
|
92663
|
+
logger2.error(
|
|
92664
|
+
`error calculating position to close to withdraw amount: ${err2}`
|
|
92665
|
+
);
|
|
92666
|
+
return new Web3Number(0, USDC_TOKEN_DECIMALS);
|
|
92667
|
+
}
|
|
92668
|
+
};
|
|
92639
92669
|
|
|
92640
92670
|
// src/utils/health-factor-math.ts
|
|
92641
92671
|
var HealthFactorMath = class {
|
|
@@ -99224,6 +99254,19 @@ spurious results.`);
|
|
|
99224
99254
|
logger2.info(
|
|
99225
99255
|
`${_VesuExtendedMultiplierStrategy.name}::moveAssets extendedHoldingAmount: ${extendedHoldingAmount.toNumber()}`
|
|
99226
99256
|
);
|
|
99257
|
+
const extendedPositions = await extendedAdapter.getAllOpenPositions();
|
|
99258
|
+
if (!extendedPositions) {
|
|
99259
|
+
logger2.error(
|
|
99260
|
+
`error getting extended positions: ${extendedPositions} while moving assets from extended to vault`
|
|
99261
|
+
);
|
|
99262
|
+
return this.createTransactionResult(
|
|
99263
|
+
[],
|
|
99264
|
+
false,
|
|
99265
|
+
params,
|
|
99266
|
+
"NONE",
|
|
99267
|
+
params.cycleType
|
|
99268
|
+
);
|
|
99269
|
+
}
|
|
99227
99270
|
if (params.amount.abs().greaterThan(extendedHoldingAmount)) {
|
|
99228
99271
|
const leftAmountAfterWithdrawalAmountInAccount = new Web3Number(
|
|
99229
99272
|
Math.ceil(
|
|
@@ -99231,6 +99274,14 @@ spurious results.`);
|
|
|
99231
99274
|
),
|
|
99232
99275
|
USDC_TOKEN_DECIMALS
|
|
99233
99276
|
);
|
|
99277
|
+
const positionAmountToClose = await calculatePositionToCloseToWithdrawAmount(
|
|
99278
|
+
extendedHoldings,
|
|
99279
|
+
extendedPositions[0],
|
|
99280
|
+
params.amount
|
|
99281
|
+
);
|
|
99282
|
+
logger2.info(
|
|
99283
|
+
`positionAmountToClose: ${positionAmountToClose} this is without leverage`
|
|
99284
|
+
);
|
|
99234
99285
|
logger2.info(
|
|
99235
99286
|
`${_VesuExtendedMultiplierStrategy.name}::moveAssets leftAmountAfterWithdrawalAmountInAccount: ${leftAmountAfterWithdrawalAmountInAccount.toNumber()}`
|
|
99236
99287
|
);
|
|
@@ -99243,7 +99294,7 @@ spurious results.`);
|
|
|
99243
99294
|
logger2.error(`error fetching order book btc usdc: ${status}`);
|
|
99244
99295
|
priceOfBTC = collateralPrice.price;
|
|
99245
99296
|
}
|
|
99246
|
-
const btcAmount =
|
|
99297
|
+
const btcAmount = positionAmountToClose.dividedBy(priceOfBTC);
|
|
99247
99298
|
const openLongPosition = btcAmount.multipliedBy(3).greaterThan(MINIMUM_EXTENDED_POSITION_SIZE) ? await extendedAdapter.createOrder(
|
|
99248
99299
|
extendedLeverage.toString(),
|
|
99249
99300
|
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,26 @@ 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 extendedLeverage = calculateExtendedLevergae();
|
|
28607
|
+
const extendedPosition = extendedPositions.value;
|
|
28608
|
+
const marginRequired = new Web3Number(extendedBalance.initialMargin, USDC_TOKEN_DECIMALS);
|
|
28609
|
+
const unrealisedPnl = new Web3Number(extendedBalance.unrealisedPnl, USDC_TOKEN_DECIMALS);
|
|
28610
|
+
const availableForWithdrawal = new Web3Number(extendedBalance.availableForWithdrawal, USDC_TOKEN_DECIMALS);
|
|
28611
|
+
const upnlPercent = unrealisedPnl.dividedBy(extendedPosition);
|
|
28612
|
+
console.log(`upnlPercent: ${upnlPercent}`);
|
|
28613
|
+
const walletBalance = new Web3Number(extendedBalance.balance, USDC_TOKEN_DECIMALS);
|
|
28614
|
+
const term1 = marginRequired.minus(walletBalance).minus(availableForWithdrawal).plus(amountToWithdraw).multipliedBy(extendedLeverage);
|
|
28615
|
+
const denominator = upnlPercent.multipliedBy(extendedLeverage).plus(1);
|
|
28616
|
+
return new Web3Number(Math.ceil(term1.dividedBy(denominator).dividedBy(extendedLeverage).toNumber()), USDC_TOKEN_DECIMALS);
|
|
28617
|
+
} catch (err) {
|
|
28618
|
+
logger.error(
|
|
28619
|
+
`error calculating position to close to withdraw amount: ${err}`
|
|
28620
|
+
);
|
|
28621
|
+
return new Web3Number(0, USDC_TOKEN_DECIMALS);
|
|
28622
|
+
}
|
|
28623
|
+
};
|
|
28595
28624
|
|
|
28596
28625
|
// src/utils/health-factor-math.ts
|
|
28597
28626
|
var HealthFactorMath = class {
|
|
@@ -35187,6 +35216,19 @@ var VesuExtendedMultiplierStrategy = class _VesuExtendedMultiplierStrategy exten
|
|
|
35187
35216
|
logger.info(
|
|
35188
35217
|
`${_VesuExtendedMultiplierStrategy.name}::moveAssets extendedHoldingAmount: ${extendedHoldingAmount.toNumber()}`
|
|
35189
35218
|
);
|
|
35219
|
+
const extendedPositions = await extendedAdapter.getAllOpenPositions();
|
|
35220
|
+
if (!extendedPositions) {
|
|
35221
|
+
logger.error(
|
|
35222
|
+
`error getting extended positions: ${extendedPositions} while moving assets from extended to vault`
|
|
35223
|
+
);
|
|
35224
|
+
return this.createTransactionResult(
|
|
35225
|
+
[],
|
|
35226
|
+
false,
|
|
35227
|
+
params,
|
|
35228
|
+
"NONE",
|
|
35229
|
+
params.cycleType
|
|
35230
|
+
);
|
|
35231
|
+
}
|
|
35190
35232
|
if (params.amount.abs().greaterThan(extendedHoldingAmount)) {
|
|
35191
35233
|
const leftAmountAfterWithdrawalAmountInAccount = new Web3Number(
|
|
35192
35234
|
Math.ceil(
|
|
@@ -35194,6 +35236,14 @@ var VesuExtendedMultiplierStrategy = class _VesuExtendedMultiplierStrategy exten
|
|
|
35194
35236
|
),
|
|
35195
35237
|
USDC_TOKEN_DECIMALS
|
|
35196
35238
|
);
|
|
35239
|
+
const positionAmountToClose = await calculatePositionToCloseToWithdrawAmount(
|
|
35240
|
+
extendedHoldings,
|
|
35241
|
+
extendedPositions[0],
|
|
35242
|
+
params.amount
|
|
35243
|
+
);
|
|
35244
|
+
logger.info(
|
|
35245
|
+
`positionAmountToClose: ${positionAmountToClose} this is without leverage`
|
|
35246
|
+
);
|
|
35197
35247
|
logger.info(
|
|
35198
35248
|
`${_VesuExtendedMultiplierStrategy.name}::moveAssets leftAmountAfterWithdrawalAmountInAccount: ${leftAmountAfterWithdrawalAmountInAccount.toNumber()}`
|
|
35199
35249
|
);
|
|
@@ -35206,7 +35256,7 @@ var VesuExtendedMultiplierStrategy = class _VesuExtendedMultiplierStrategy exten
|
|
|
35206
35256
|
logger.error(`error fetching order book btc usdc: ${status}`);
|
|
35207
35257
|
priceOfBTC = collateralPrice.price;
|
|
35208
35258
|
}
|
|
35209
|
-
const btcAmount =
|
|
35259
|
+
const btcAmount = positionAmountToClose.dividedBy(priceOfBTC);
|
|
35210
35260
|
const openLongPosition = btcAmount.multipliedBy(3).greaterThan(MINIMUM_EXTENDED_POSITION_SIZE) ? await extendedAdapter.createOrder(
|
|
35211
35261
|
extendedLeverage.toString(),
|
|
35212
35262
|
btcAmount.toNumber(),
|
|
@@ -39988,6 +40038,7 @@ export {
|
|
|
39988
40038
|
calculateDeltaDebtAmount,
|
|
39989
40039
|
calculateExposureDelta,
|
|
39990
40040
|
calculateExtendedLevergae,
|
|
40041
|
+
calculatePositionToCloseToWithdrawAmount,
|
|
39991
40042
|
calculateVesUPositionSizeGivenExtended,
|
|
39992
40043
|
calculateVesuLeverage,
|
|
39993
40044
|
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,26 @@ 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 extendedLeverage = calculateExtendedLevergae();
|
|
28411
|
+
const extendedPosition = extendedPositions.value;
|
|
28412
|
+
const marginRequired = new Web3Number(extendedBalance.initialMargin, USDC_TOKEN_DECIMALS);
|
|
28413
|
+
const unrealisedPnl = new Web3Number(extendedBalance.unrealisedPnl, USDC_TOKEN_DECIMALS);
|
|
28414
|
+
const availableForWithdrawal = new Web3Number(extendedBalance.availableForWithdrawal, USDC_TOKEN_DECIMALS);
|
|
28415
|
+
const upnlPercent = unrealisedPnl.dividedBy(extendedPosition);
|
|
28416
|
+
console.log(`upnlPercent: ${upnlPercent}`);
|
|
28417
|
+
const walletBalance = new Web3Number(extendedBalance.balance, USDC_TOKEN_DECIMALS);
|
|
28418
|
+
const term1 = marginRequired.minus(walletBalance).minus(availableForWithdrawal).plus(amountToWithdraw).multipliedBy(extendedLeverage);
|
|
28419
|
+
const denominator = upnlPercent.multipliedBy(extendedLeverage).plus(1);
|
|
28420
|
+
return new Web3Number(Math.ceil(term1.dividedBy(denominator).dividedBy(extendedLeverage).toNumber()), USDC_TOKEN_DECIMALS);
|
|
28421
|
+
} catch (err) {
|
|
28422
|
+
logger.error(
|
|
28423
|
+
`error calculating position to close to withdraw amount: ${err}`
|
|
28424
|
+
);
|
|
28425
|
+
return new Web3Number(0, USDC_TOKEN_DECIMALS);
|
|
28426
|
+
}
|
|
28427
|
+
};
|
|
28398
28428
|
|
|
28399
28429
|
// src/utils/health-factor-math.ts
|
|
28400
28430
|
var HealthFactorMath = class {
|
|
@@ -35345,6 +35375,19 @@ var VesuExtendedMultiplierStrategy = class _VesuExtendedMultiplierStrategy exten
|
|
|
35345
35375
|
logger.info(
|
|
35346
35376
|
`${_VesuExtendedMultiplierStrategy.name}::moveAssets extendedHoldingAmount: ${extendedHoldingAmount.toNumber()}`
|
|
35347
35377
|
);
|
|
35378
|
+
const extendedPositions = await extendedAdapter.getAllOpenPositions();
|
|
35379
|
+
if (!extendedPositions) {
|
|
35380
|
+
logger.error(
|
|
35381
|
+
`error getting extended positions: ${extendedPositions} while moving assets from extended to vault`
|
|
35382
|
+
);
|
|
35383
|
+
return this.createTransactionResult(
|
|
35384
|
+
[],
|
|
35385
|
+
false,
|
|
35386
|
+
params,
|
|
35387
|
+
"NONE",
|
|
35388
|
+
params.cycleType
|
|
35389
|
+
);
|
|
35390
|
+
}
|
|
35348
35391
|
if (params.amount.abs().greaterThan(extendedHoldingAmount)) {
|
|
35349
35392
|
const leftAmountAfterWithdrawalAmountInAccount = new Web3Number(
|
|
35350
35393
|
Math.ceil(
|
|
@@ -35352,6 +35395,14 @@ var VesuExtendedMultiplierStrategy = class _VesuExtendedMultiplierStrategy exten
|
|
|
35352
35395
|
),
|
|
35353
35396
|
USDC_TOKEN_DECIMALS
|
|
35354
35397
|
);
|
|
35398
|
+
const positionAmountToClose = await calculatePositionToCloseToWithdrawAmount(
|
|
35399
|
+
extendedHoldings,
|
|
35400
|
+
extendedPositions[0],
|
|
35401
|
+
params.amount
|
|
35402
|
+
);
|
|
35403
|
+
logger.info(
|
|
35404
|
+
`positionAmountToClose: ${positionAmountToClose} this is without leverage`
|
|
35405
|
+
);
|
|
35355
35406
|
logger.info(
|
|
35356
35407
|
`${_VesuExtendedMultiplierStrategy.name}::moveAssets leftAmountAfterWithdrawalAmountInAccount: ${leftAmountAfterWithdrawalAmountInAccount.toNumber()}`
|
|
35357
35408
|
);
|
|
@@ -35364,7 +35415,7 @@ var VesuExtendedMultiplierStrategy = class _VesuExtendedMultiplierStrategy exten
|
|
|
35364
35415
|
logger.error(`error fetching order book btc usdc: ${status}`);
|
|
35365
35416
|
priceOfBTC = collateralPrice.price;
|
|
35366
35417
|
}
|
|
35367
|
-
const btcAmount =
|
|
35418
|
+
const btcAmount = positionAmountToClose.dividedBy(priceOfBTC);
|
|
35368
35419
|
const openLongPosition = btcAmount.multipliedBy(3).greaterThan(MINIMUM_EXTENDED_POSITION_SIZE) ? await extendedAdapter.createOrder(
|
|
35369
35420
|
extendedLeverage.toString(),
|
|
35370
35421
|
btcAmount.toNumber(),
|
|
@@ -40713,6 +40764,7 @@ var deployer_default = Deployer;
|
|
|
40713
40764
|
calculateDeltaDebtAmount,
|
|
40714
40765
|
calculateExposureDelta,
|
|
40715
40766
|
calculateExtendedLevergae,
|
|
40767
|
+
calculatePositionToCloseToWithdrawAmount,
|
|
40716
40768
|
calculateVesUPositionSizeGivenExtended,
|
|
40717
40769
|
calculateVesuLeverage,
|
|
40718
40770
|
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,26 @@ 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 extendedLeverage = calculateExtendedLevergae();
|
|
28263
|
+
const extendedPosition = extendedPositions.value;
|
|
28264
|
+
const marginRequired = new Web3Number(extendedBalance.initialMargin, USDC_TOKEN_DECIMALS);
|
|
28265
|
+
const unrealisedPnl = new Web3Number(extendedBalance.unrealisedPnl, USDC_TOKEN_DECIMALS);
|
|
28266
|
+
const availableForWithdrawal = new Web3Number(extendedBalance.availableForWithdrawal, USDC_TOKEN_DECIMALS);
|
|
28267
|
+
const upnlPercent = unrealisedPnl.dividedBy(extendedPosition);
|
|
28268
|
+
console.log(`upnlPercent: ${upnlPercent}`);
|
|
28269
|
+
const walletBalance = new Web3Number(extendedBalance.balance, USDC_TOKEN_DECIMALS);
|
|
28270
|
+
const term1 = marginRequired.minus(walletBalance).minus(availableForWithdrawal).plus(amountToWithdraw).multipliedBy(extendedLeverage);
|
|
28271
|
+
const denominator = upnlPercent.multipliedBy(extendedLeverage).plus(1);
|
|
28272
|
+
return new Web3Number(Math.ceil(term1.dividedBy(denominator).dividedBy(extendedLeverage).toNumber()), USDC_TOKEN_DECIMALS);
|
|
28273
|
+
} catch (err) {
|
|
28274
|
+
logger.error(
|
|
28275
|
+
`error calculating position to close to withdraw amount: ${err}`
|
|
28276
|
+
);
|
|
28277
|
+
return new Web3Number(0, USDC_TOKEN_DECIMALS);
|
|
28278
|
+
}
|
|
28279
|
+
};
|
|
28251
28280
|
|
|
28252
28281
|
// src/utils/health-factor-math.ts
|
|
28253
28282
|
var HealthFactorMath = class {
|
|
@@ -35198,6 +35227,19 @@ var VesuExtendedMultiplierStrategy = class _VesuExtendedMultiplierStrategy exten
|
|
|
35198
35227
|
logger.info(
|
|
35199
35228
|
`${_VesuExtendedMultiplierStrategy.name}::moveAssets extendedHoldingAmount: ${extendedHoldingAmount.toNumber()}`
|
|
35200
35229
|
);
|
|
35230
|
+
const extendedPositions = await extendedAdapter.getAllOpenPositions();
|
|
35231
|
+
if (!extendedPositions) {
|
|
35232
|
+
logger.error(
|
|
35233
|
+
`error getting extended positions: ${extendedPositions} while moving assets from extended to vault`
|
|
35234
|
+
);
|
|
35235
|
+
return this.createTransactionResult(
|
|
35236
|
+
[],
|
|
35237
|
+
false,
|
|
35238
|
+
params,
|
|
35239
|
+
"NONE",
|
|
35240
|
+
params.cycleType
|
|
35241
|
+
);
|
|
35242
|
+
}
|
|
35201
35243
|
if (params.amount.abs().greaterThan(extendedHoldingAmount)) {
|
|
35202
35244
|
const leftAmountAfterWithdrawalAmountInAccount = new Web3Number(
|
|
35203
35245
|
Math.ceil(
|
|
@@ -35205,6 +35247,14 @@ var VesuExtendedMultiplierStrategy = class _VesuExtendedMultiplierStrategy exten
|
|
|
35205
35247
|
),
|
|
35206
35248
|
USDC_TOKEN_DECIMALS
|
|
35207
35249
|
);
|
|
35250
|
+
const positionAmountToClose = await calculatePositionToCloseToWithdrawAmount(
|
|
35251
|
+
extendedHoldings,
|
|
35252
|
+
extendedPositions[0],
|
|
35253
|
+
params.amount
|
|
35254
|
+
);
|
|
35255
|
+
logger.info(
|
|
35256
|
+
`positionAmountToClose: ${positionAmountToClose} this is without leverage`
|
|
35257
|
+
);
|
|
35208
35258
|
logger.info(
|
|
35209
35259
|
`${_VesuExtendedMultiplierStrategy.name}::moveAssets leftAmountAfterWithdrawalAmountInAccount: ${leftAmountAfterWithdrawalAmountInAccount.toNumber()}`
|
|
35210
35260
|
);
|
|
@@ -35217,7 +35267,7 @@ var VesuExtendedMultiplierStrategy = class _VesuExtendedMultiplierStrategy exten
|
|
|
35217
35267
|
logger.error(`error fetching order book btc usdc: ${status}`);
|
|
35218
35268
|
priceOfBTC = collateralPrice.price;
|
|
35219
35269
|
}
|
|
35220
|
-
const btcAmount =
|
|
35270
|
+
const btcAmount = positionAmountToClose.dividedBy(priceOfBTC);
|
|
35221
35271
|
const openLongPosition = btcAmount.multipliedBy(3).greaterThan(MINIMUM_EXTENDED_POSITION_SIZE) ? await extendedAdapter.createOrder(
|
|
35222
35272
|
extendedLeverage.toString(),
|
|
35223
35273
|
btcAmount.toNumber(),
|
|
@@ -40565,6 +40615,7 @@ export {
|
|
|
40565
40615
|
calculateDeltaDebtAmount,
|
|
40566
40616
|
calculateExposureDelta,
|
|
40567
40617
|
calculateExtendedLevergae,
|
|
40618
|
+
calculatePositionToCloseToWithdrawAmount,
|
|
40568
40619
|
calculateVesUPositionSizeGivenExtended,
|
|
40569
40620
|
calculateVesuLeverage,
|
|
40570
40621
|
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,53 @@ 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 extendedLeverage = calculateExtendedLevergae();
|
|
475
|
+
const extendedPosition = extendedPositions.value;
|
|
476
|
+
const marginRequired = new Web3Number(extendedBalance.initialMargin , USDC_TOKEN_DECIMALS);
|
|
477
|
+
const unrealisedPnl = new Web3Number(extendedBalance.unrealisedPnl, USDC_TOKEN_DECIMALS);
|
|
478
|
+
const availableForWithdrawal = new Web3Number(extendedBalance.availableForWithdrawal, USDC_TOKEN_DECIMALS);
|
|
479
|
+
const upnlPercent = unrealisedPnl.dividedBy(extendedPosition)
|
|
480
|
+
console.log(`upnlPercent: ${upnlPercent}`);
|
|
481
|
+
/**
|
|
482
|
+
* New Formula
|
|
483
|
+
*/
|
|
484
|
+
const walletBalance = new Web3Number(extendedBalance.balance, USDC_TOKEN_DECIMALS);
|
|
485
|
+
const term1 = marginRequired.minus(walletBalance).minus(availableForWithdrawal).plus(amountToWithdraw).multipliedBy(extendedLeverage);
|
|
486
|
+
const denominator = upnlPercent.multipliedBy(extendedLeverage).plus(1);
|
|
487
|
+
return new Web3Number(Math.ceil(term1.dividedBy(denominator).dividedBy(extendedLeverage).toNumber()), USDC_TOKEN_DECIMALS);
|
|
488
|
+
} catch (err) {
|
|
489
|
+
logger.error(
|
|
490
|
+
`error calculating position to close to withdraw amount: ${err}`
|
|
491
|
+
);
|
|
492
|
+
return new Web3Number(0, USDC_TOKEN_DECIMALS);
|
|
493
|
+
}
|
|
494
|
+
};
|
|
@@ -8,7 +8,12 @@ import {
|
|
|
8
8
|
UNIVERSAL_MANAGE_IDS,
|
|
9
9
|
UniversalStrategySettings,
|
|
10
10
|
} from "../universal-strategy";
|
|
11
|
-
import {
|
|
11
|
+
import {
|
|
12
|
+
calculateDebtAmount,
|
|
13
|
+
calculateDeltaDebtAmount,
|
|
14
|
+
calculateExtendedLevergae,
|
|
15
|
+
calculatePositionToCloseToWithdrawAmount,
|
|
16
|
+
} from "./utils/helper";
|
|
12
17
|
import { logger } from "@/utils";
|
|
13
18
|
import { AUDIT_URL } from "../universal-lst-muliplier-strategy";
|
|
14
19
|
import { getNoRiskTags } from "@/interfaces";
|
|
@@ -410,7 +415,7 @@ export class VesuExtendedMultiplierStrategy<
|
|
|
410
415
|
logger.info(
|
|
411
416
|
`${VesuExtendedMultiplierStrategy.name}::shouldInvest amountToInvest: ${amountToInvestNumber}`
|
|
412
417
|
);
|
|
413
|
-
|
|
418
|
+
|
|
414
419
|
if (amountToInvest.lessThan(LIMIT_BALANCE_VALUE)) {
|
|
415
420
|
return {
|
|
416
421
|
shouldInvest: false,
|
|
@@ -498,7 +503,8 @@ export class VesuExtendedMultiplierStrategy<
|
|
|
498
503
|
debtAmountToBeRepaid: new Web3Number(0, 0),
|
|
499
504
|
};
|
|
500
505
|
}
|
|
501
|
-
const amountToInvestAfterRepayingDebt =
|
|
506
|
+
const amountToInvestAfterRepayingDebt =
|
|
507
|
+
amountToInvest.minus(debtAmountToBeRepaid);
|
|
502
508
|
const { vesu_amount, extended_amount, extended_leverage, vesu_leverage } =
|
|
503
509
|
await calculateAmountDistribution(
|
|
504
510
|
amountToInvestAfterRepayingDebt.toNumber(),
|
|
@@ -1142,6 +1148,19 @@ export class VesuExtendedMultiplierStrategy<
|
|
|
1142
1148
|
VesuExtendedMultiplierStrategy.name
|
|
1143
1149
|
}::moveAssets extendedHoldingAmount: ${extendedHoldingAmount.toNumber()}`
|
|
1144
1150
|
);
|
|
1151
|
+
const extendedPositions = await extendedAdapter.getAllOpenPositions();
|
|
1152
|
+
if (!extendedPositions) {
|
|
1153
|
+
logger.error(
|
|
1154
|
+
`error getting extended positions: ${extendedPositions} while moving assets from extended to vault`
|
|
1155
|
+
);
|
|
1156
|
+
return this.createTransactionResult(
|
|
1157
|
+
[],
|
|
1158
|
+
false,
|
|
1159
|
+
params,
|
|
1160
|
+
"NONE",
|
|
1161
|
+
params.cycleType
|
|
1162
|
+
);
|
|
1163
|
+
}
|
|
1145
1164
|
if (params.amount.abs().greaterThan(extendedHoldingAmount)) {
|
|
1146
1165
|
const leftAmountAfterWithdrawalAmountInAccount = new Web3Number(
|
|
1147
1166
|
Math.ceil(
|
|
@@ -1149,6 +1168,15 @@ export class VesuExtendedMultiplierStrategy<
|
|
|
1149
1168
|
),
|
|
1150
1169
|
USDC_TOKEN_DECIMALS
|
|
1151
1170
|
);
|
|
1171
|
+
const positionAmountToClose =
|
|
1172
|
+
await calculatePositionToCloseToWithdrawAmount(
|
|
1173
|
+
extendedHoldings,
|
|
1174
|
+
extendedPositions[0],
|
|
1175
|
+
params.amount
|
|
1176
|
+
);
|
|
1177
|
+
logger.info(
|
|
1178
|
+
`positionAmountToClose: ${positionAmountToClose} this is without leverage`
|
|
1179
|
+
);
|
|
1152
1180
|
logger.info(
|
|
1153
1181
|
`${
|
|
1154
1182
|
VesuExtendedMultiplierStrategy.name
|
|
@@ -1164,8 +1192,7 @@ export class VesuExtendedMultiplierStrategy<
|
|
|
1164
1192
|
logger.error(`error fetching order book btc usdc: ${status}`);
|
|
1165
1193
|
priceOfBTC = collateralPrice.price;
|
|
1166
1194
|
}
|
|
1167
|
-
const btcAmount =
|
|
1168
|
-
leftAmountAfterWithdrawalAmountInAccount.dividedBy(priceOfBTC);
|
|
1195
|
+
const btcAmount = positionAmountToClose.dividedBy(priceOfBTC);
|
|
1169
1196
|
/**
|
|
1170
1197
|
* If amount for withdrawal is greater than the amount in extended available for withdrawal,
|
|
1171
1198
|
* then we need to open a long position depending on the difference between the two
|