@strkfarm/sdk 1.2.0 → 2.0.0-dev-strategy2.1
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 +76556 -66640
- package/dist/index.browser.mjs +34235 -24392
- package/dist/index.d.ts +2372 -793
- package/dist/index.js +31967 -22084
- package/dist/index.mjs +25545 -15719
- package/package.json +86 -76
- package/readme.md +56 -1
- package/src/data/extended-deposit.abi.json +3613 -0
- package/src/data/universal-vault.abi.json +135 -20
- package/src/dataTypes/_bignumber.ts +11 -0
- package/src/dataTypes/address.ts +7 -0
- package/src/global.ts +240 -193
- package/src/interfaces/common.tsx +26 -2
- package/src/modules/ExtendedWrapperSDk/index.ts +62 -0
- package/src/modules/ExtendedWrapperSDk/types.ts +311 -0
- package/src/modules/ExtendedWrapperSDk/wrapper.ts +448 -0
- package/src/modules/avnu.ts +17 -4
- package/src/modules/ekubo-quoter.ts +89 -10
- package/src/modules/erc20.ts +67 -21
- package/src/modules/harvests.ts +29 -43
- package/src/modules/index.ts +5 -1
- package/src/modules/lst-apr.ts +36 -0
- package/src/modules/midas.ts +159 -0
- package/src/modules/pricer-from-api.ts +2 -2
- package/src/modules/pricer-lst.ts +1 -1
- package/src/modules/pricer.ts +3 -38
- package/src/modules/token-market-data.ts +202 -0
- package/src/node/deployer.ts +1 -36
- package/src/strategies/autoCompounderStrk.ts +1 -1
- package/src/strategies/base-strategy.ts +20 -3
- package/src/strategies/btc-vesu-extended-strategy/core-strategy.tsx +1486 -0
- package/src/strategies/btc-vesu-extended-strategy/services/operationService.ts +32 -0
- package/src/strategies/btc-vesu-extended-strategy/utils/constants.ts +3 -0
- package/src/strategies/btc-vesu-extended-strategy/utils/helper.ts +396 -0
- package/src/strategies/btc-vesu-extended-strategy/utils/types.ts +5 -0
- package/src/strategies/ekubo-cl-vault.tsx +123 -306
- package/src/strategies/index.ts +7 -1
- package/src/strategies/svk-strategy.ts +247 -0
- package/src/strategies/universal-adapters/adapter-optimizer.ts +65 -0
- package/src/strategies/universal-adapters/adapter-utils.ts +5 -1
- package/src/strategies/universal-adapters/avnu-adapter.ts +432 -0
- package/src/strategies/universal-adapters/baseAdapter.ts +181 -153
- package/src/strategies/universal-adapters/common-adapter.ts +98 -77
- package/src/strategies/universal-adapters/extended-adapter.ts +976 -0
- package/src/strategies/universal-adapters/index.ts +7 -1
- package/src/strategies/universal-adapters/unused-balance-adapter.ts +109 -0
- package/src/strategies/universal-adapters/vesu-adapter.ts +230 -230
- package/src/strategies/universal-adapters/vesu-borrow-adapter.ts +1247 -0
- package/src/strategies/universal-adapters/vesu-multiply-adapter.ts +1306 -0
- package/src/strategies/universal-adapters/vesu-supply-only-adapter.ts +58 -51
- package/src/strategies/universal-lst-muliplier-strategy.tsx +716 -844
- package/src/strategies/universal-strategy.tsx +1103 -1181
- package/src/strategies/vesu-extended-strategy/services/operationService.ts +34 -0
- package/src/strategies/vesu-extended-strategy/types/transaction-metadata.ts +25 -0
- package/src/strategies/vesu-extended-strategy/utils/config.runtime.ts +77 -0
- package/src/strategies/vesu-extended-strategy/utils/constants.ts +50 -0
- package/src/strategies/vesu-extended-strategy/utils/helper.ts +367 -0
- package/src/strategies/vesu-extended-strategy/vesu-extended-strategy.tsx +1420 -0
- package/src/strategies/vesu-rebalance.tsx +16 -20
- package/src/utils/health-factor-math.ts +11 -5
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Web3Number } from "@/dataTypes";
|
|
2
|
+
import { ExtendedAdapter } from "@/strategies/universal-adapters/extended-adapter";
|
|
3
|
+
import { VesuMultiplyAdapter } from "../../universal-adapters/vesu-multiply-adapter";
|
|
4
|
+
import { Call } from "starknet";
|
|
5
|
+
export abstract class Operations {
|
|
6
|
+
abstract shouldMoveAssets(
|
|
7
|
+
extendedAmount: Web3Number,
|
|
8
|
+
vesuAmount: Web3Number
|
|
9
|
+
): Promise<Call[]>;
|
|
10
|
+
abstract shouldInvest(): Promise<{
|
|
11
|
+
shouldInvest: boolean;
|
|
12
|
+
collateralPrice: number;
|
|
13
|
+
wbtcAmountForStage1: Web3Number;
|
|
14
|
+
} | null>;
|
|
15
|
+
abstract moveAssets(
|
|
16
|
+
params: { from: string; to: string; amount: Web3Number },
|
|
17
|
+
extendedAdapter: ExtendedAdapter,
|
|
18
|
+
vesuAdapter: VesuMultiplyAdapter
|
|
19
|
+
): Promise<{
|
|
20
|
+
calls: Call[];
|
|
21
|
+
status: boolean;
|
|
22
|
+
}>;
|
|
23
|
+
abstract handleWithdraw(amount: Web3Number): Promise<{
|
|
24
|
+
calls: Call[];
|
|
25
|
+
status: boolean;
|
|
26
|
+
}>;
|
|
27
|
+
abstract handleStage2Positions(): Promise<{
|
|
28
|
+
decisiveFactor: Web3Number;
|
|
29
|
+
vesuAmountInUSDC: Web3Number;
|
|
30
|
+
extendedAmountInUSDC: Web3Number;
|
|
31
|
+
}>;
|
|
32
|
+
}
|
|
@@ -0,0 +1,396 @@
|
|
|
1
|
+
import { Web3Number } from "@/dataTypes";
|
|
2
|
+
import {
|
|
3
|
+
MAX_LTV_BTC_USDC,
|
|
4
|
+
USDC_TOKEN_DECIMALS,
|
|
5
|
+
WBTC_TOKEN_DECIMALS
|
|
6
|
+
} from "@/strategies/vesu-extended-strategy/utils/constants";
|
|
7
|
+
import { TARGET_HF } from "@/strategies/vesu-extended-strategy/utils/constants";
|
|
8
|
+
import { logger } from "@/utils";
|
|
9
|
+
import {
|
|
10
|
+
calculateExtendedLevergae,
|
|
11
|
+
calculateVesuLeverage,
|
|
12
|
+
} from "../../vesu-extended-strategy/utils/helper";
|
|
13
|
+
import { Position } from "@/modules";
|
|
14
|
+
|
|
15
|
+
export const calculateDeltaDebtAmount = (
|
|
16
|
+
maxLtv: number = MAX_LTV_BTC_USDC,
|
|
17
|
+
extendedExposureWBTC: Web3Number, // Ee
|
|
18
|
+
assetsUnderVaultAllocator: Web3Number, // this is the aum
|
|
19
|
+
targetHf: number = TARGET_HF,
|
|
20
|
+
adddedCollateral: Web3Number, // wbtc terms
|
|
21
|
+
existingVesuCollateral: Web3Number, // C existing
|
|
22
|
+
existingVesuDebt: Web3Number, // Yc, should be in debt units
|
|
23
|
+
collateralPrice: number,
|
|
24
|
+
debtPrice: number
|
|
25
|
+
) => {
|
|
26
|
+
try {
|
|
27
|
+
|
|
28
|
+
const maxLtvWeb3 = new Web3Number(maxLtv, 2);
|
|
29
|
+
const targetHfWeb3 = new Web3Number(targetHf, 2);
|
|
30
|
+
// calculation of Ce, this is the existing value in the position
|
|
31
|
+
const collateralExposureVesuInMultiplier =
|
|
32
|
+
calculateVesuExposureExtraToDepositedCollateral(
|
|
33
|
+
assetsUnderVaultAllocator,
|
|
34
|
+
adddedCollateral,
|
|
35
|
+
existingVesuCollateral
|
|
36
|
+
);
|
|
37
|
+
if (collateralExposureVesuInMultiplier === null) {
|
|
38
|
+
logger.error(
|
|
39
|
+
`error calculating collateral exposure vesu: ${collateralExposureVesuInMultiplier}`
|
|
40
|
+
);
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
// console.log("collateralExposureVesuInMultiplier", collateralExposureVesuInMultiplier)
|
|
44
|
+
//LHS - Maintain precision throughout calculations, only round at the end
|
|
45
|
+
const term1 = adddedCollateral.multipliedBy(maxLtv).dividedBy(targetHf);
|
|
46
|
+
const term2 = extendedExposureWBTC
|
|
47
|
+
.minus(collateralExposureVesuInMultiplier)
|
|
48
|
+
.multipliedBy(maxLtv)
|
|
49
|
+
.dividedBy(targetHf)
|
|
50
|
+
.dividedBy(4)
|
|
51
|
+
.toFixed(WBTC_TOKEN_DECIMALS);
|
|
52
|
+
|
|
53
|
+
const term3 = existingVesuCollateral
|
|
54
|
+
const term4 = existingVesuDebt
|
|
55
|
+
.multipliedBy(debtPrice)
|
|
56
|
+
.dividedBy(collateralPrice)
|
|
57
|
+
.multipliedBy(-1)
|
|
58
|
+
.toFixed(WBTC_TOKEN_DECIMALS)
|
|
59
|
+
|
|
60
|
+
console.log("term2", term2.toString(), term1.toString(), term3.toString(), term4.toString(), existingVesuDebt.toString());
|
|
61
|
+
// Perform addition with full precision, only round final result
|
|
62
|
+
const lhs = new Web3Number(term1.plus(term2).plus(term3).plus(term4).toFixed(WBTC_TOKEN_DECIMALS), WBTC_TOKEN_DECIMALS);
|
|
63
|
+
|
|
64
|
+
console.log("lhs", lhs);
|
|
65
|
+
// RHS
|
|
66
|
+
const termrhs = maxLtvWeb3
|
|
67
|
+
.dividedBy(targetHfWeb3)
|
|
68
|
+
.multipliedBy(0.75)
|
|
69
|
+
.multipliedBy(-1)
|
|
70
|
+
.plus(1)
|
|
71
|
+
.toFixed(4);
|
|
72
|
+
|
|
73
|
+
console.log("termrhs", termrhs.toString());
|
|
74
|
+
// THis will be in collateral units, since we have to match the exposure in collateral
|
|
75
|
+
// Maintain full precision during division, only round at the end
|
|
76
|
+
const calculateDeltaDebtAmount = lhs.dividedBy(termrhs);
|
|
77
|
+
// Round to WBTC decimals only at the final step to preserve precision
|
|
78
|
+
return new Web3Number(calculateDeltaDebtAmount.toFixed(WBTC_TOKEN_DECIMALS), WBTC_TOKEN_DECIMALS);
|
|
79
|
+
} catch (err) {
|
|
80
|
+
logger.error(`error calculating delta position: ${err}`);
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
export const calculateDecisiveFactorF = (
|
|
86
|
+
extendedExposureWBTC: Web3Number, // Ee
|
|
87
|
+
assetsUnderVaultAllocator: Web3Number,
|
|
88
|
+
adddedCollateral: Web3Number,
|
|
89
|
+
existingVesuCollateral: Web3Number, // C existing
|
|
90
|
+
unusedBalanceUSDC: Web3Number,
|
|
91
|
+
existingVesuDebt: Web3Number,
|
|
92
|
+
collateralPrice: number,
|
|
93
|
+
debtPrice: number,
|
|
94
|
+
maxLtv: number = MAX_LTV_BTC_USDC,
|
|
95
|
+
targetHf: number = TARGET_HF,
|
|
96
|
+
) => {
|
|
97
|
+
try {
|
|
98
|
+
const maxLtvWeb3 = new Web3Number(maxLtv, 4);
|
|
99
|
+
const extendedLeverage= calculateExtendedLevergae();
|
|
100
|
+
// calculation of Ce
|
|
101
|
+
let collateralExposureVesu =
|
|
102
|
+
calculateVesuExposureExtraToDepositedCollateral(
|
|
103
|
+
assetsUnderVaultAllocator,
|
|
104
|
+
adddedCollateral,
|
|
105
|
+
existingVesuCollateral
|
|
106
|
+
)
|
|
107
|
+
if (collateralExposureVesu === null) {
|
|
108
|
+
logger.error(
|
|
109
|
+
`error calculating collateral exposure vesu: ${collateralExposureVesu}`
|
|
110
|
+
);
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
collateralExposureVesu = new Web3Number(collateralExposureVesu.toFixed(USDC_TOKEN_DECIMALS), USDC_TOKEN_DECIMALS);
|
|
114
|
+
console.log("collateralExposureVesu", collateralExposureVesu)
|
|
115
|
+
const num1 = extendedExposureWBTC.minus(collateralExposureVesu).multipliedBy(collateralPrice)
|
|
116
|
+
const num2 = unusedBalanceUSDC.multipliedBy(extendedLeverage)
|
|
117
|
+
console.log("num1", num1)
|
|
118
|
+
console.log("num2", num2)
|
|
119
|
+
const lhsterm1 = num1.plus(num2).dividedBy(1+extendedLeverage)
|
|
120
|
+
|
|
121
|
+
console.log("lhsterm1", lhsterm1)
|
|
122
|
+
const num3 = existingVesuDebt.multipliedBy(debtPrice).multipliedBy(targetHf)
|
|
123
|
+
const num4 = existingVesuCollateral.multipliedBy(collateralPrice).multipliedBy(maxLtv).multipliedBy(-1)
|
|
124
|
+
const den1 = new Web3Number((maxLtv - targetHf).toFixed(4), 4);
|
|
125
|
+
|
|
126
|
+
console.log("num3", num3)
|
|
127
|
+
console.log("num4", num4)
|
|
128
|
+
console.log("den1", den1)
|
|
129
|
+
|
|
130
|
+
const lhsterm2 = num3.plus(num4).dividedBy(den1).dividedBy(1+extendedLeverage);
|
|
131
|
+
console.log("lhsterm2", lhsterm2)
|
|
132
|
+
const finalLhs = lhsterm1.minus(lhsterm2)
|
|
133
|
+
console.log("finalLhs", finalLhs)
|
|
134
|
+
const num5 = maxLtvWeb3.dividedBy(den1).dividedBy(1+extendedLeverage).minus(1).multipliedBy(-1).multipliedBy(unusedBalanceUSDC)
|
|
135
|
+
|
|
136
|
+
const precisionFactor = finalLhs.dividedBy(num5).toFixed(3);
|
|
137
|
+
// Maintain precision during subtraction, avoid premature rounding
|
|
138
|
+
return new Web3Number(precisionFactor, 3);
|
|
139
|
+
} catch (err) {
|
|
140
|
+
logger.error(`error calculating decisive factor f: ${err}`);
|
|
141
|
+
return null;
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
export const calculateDebtAmountForStage2 = (
|
|
146
|
+
debtAmountVesu : Web3Number,
|
|
147
|
+
collateralPrice: number,
|
|
148
|
+
debtPrice: number,
|
|
149
|
+
maxLtv: number = MAX_LTV_BTC_USDC,
|
|
150
|
+
targetHf: number = TARGET_HF,
|
|
151
|
+
collateralAmountVesu: Web3Number,
|
|
152
|
+
addedeCollateral: Web3Number,
|
|
153
|
+
) => {
|
|
154
|
+
try{
|
|
155
|
+
console.log("debtAmountVesu", debtAmountVesu)
|
|
156
|
+
console.log("collateralAmountVesu", collateralAmountVesu)
|
|
157
|
+
console.log("addedeCollateral", addedeCollateral)
|
|
158
|
+
const num1 = debtAmountVesu.multipliedBy(debtPrice).multipliedBy(targetHf);
|
|
159
|
+
console.log("num1", num1)
|
|
160
|
+
const num2 = collateralAmountVesu.plus(addedeCollateral).multipliedBy(collateralPrice).multipliedBy(maxLtv).multipliedBy(-1);
|
|
161
|
+
console.log("num2", num2)
|
|
162
|
+
const den1 = maxLtv - targetHf;
|
|
163
|
+
const finalNum = num1.plus(num2);
|
|
164
|
+
console.log("finalNum", finalNum)
|
|
165
|
+
const debtAmountStage2Vesu = finalNum.dividedBy(den1).toFixed(USDC_TOKEN_DECIMALS);
|
|
166
|
+
console.log("debtAmountStage2Vesu", debtAmountStage2Vesu)
|
|
167
|
+
return new Web3Number(debtAmountStage2Vesu, USDC_TOKEN_DECIMALS);
|
|
168
|
+
}catch(err){
|
|
169
|
+
logger.error(`error calculating debt amount for stage 2: ${err}`);
|
|
170
|
+
return null;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Fetches the amount of exposure we have on vesu, apart from the deposited collateral
|
|
177
|
+
* @param assetsUnderVault
|
|
178
|
+
* @param userDepositedAdditionalCollateral
|
|
179
|
+
* @param vesuCollateralExposure
|
|
180
|
+
* @returns
|
|
181
|
+
*/
|
|
182
|
+
export const calculateVesuExposureExtraToDepositedCollateral = (
|
|
183
|
+
assetsUnderVault: Web3Number,
|
|
184
|
+
userDepositedAdditionalCollateral: Web3Number,
|
|
185
|
+
vesuCollateralExposure: Web3Number
|
|
186
|
+
) => {
|
|
187
|
+
try {
|
|
188
|
+
console.log("assetsUnderVault", assetsUnderVault)
|
|
189
|
+
console.log("userDepositedAdditionalCollateral", userDepositedAdditionalCollateral)
|
|
190
|
+
console.log("vesuCollateralExposure", vesuCollateralExposure)
|
|
191
|
+
const vesuExposureExtraToDepositedCollateral = vesuCollateralExposure
|
|
192
|
+
.minus(assetsUnderVault)
|
|
193
|
+
.plus(userDepositedAdditionalCollateral).toFixed(WBTC_TOKEN_DECIMALS);
|
|
194
|
+
return new Web3Number(vesuExposureExtraToDepositedCollateral, WBTC_TOKEN_DECIMALS);
|
|
195
|
+
} catch (err) {
|
|
196
|
+
logger.error(
|
|
197
|
+
`error calculating vesu exposure extra to deposited collateral: ${err}`
|
|
198
|
+
);
|
|
199
|
+
return null;
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Calculates the debt amount to be taken/repaid to maintain the ltv on vesu
|
|
206
|
+
* @param existingVesuDebt - existing vesu debt in usdc terms
|
|
207
|
+
* @param existingVesuCollateral - existing vesu collateral in wbtc terms
|
|
208
|
+
* @param addedCollateral - added collateral in wbtc terms
|
|
209
|
+
* @param collateralPrice - collateral price in usdc terms
|
|
210
|
+
* @param debtPrice - debt price in usdc terms
|
|
211
|
+
* @param maxLtv - max ltv
|
|
212
|
+
* @param targetHf - target hf
|
|
213
|
+
* @returns delta debt amount in usdc terms
|
|
214
|
+
*/
|
|
215
|
+
export const calculateDebtAmountForMainatiningLtvOnVesu = (
|
|
216
|
+
existingVesuDebt: Web3Number,
|
|
217
|
+
existingVesuCollateral: Web3Number,
|
|
218
|
+
addedCollateral: Web3Number,
|
|
219
|
+
collateralPrice: number,
|
|
220
|
+
debtPrice: number,
|
|
221
|
+
maxLtv: number = MAX_LTV_BTC_USDC,
|
|
222
|
+
targetHf: number = TARGET_HF,
|
|
223
|
+
):Web3Number | null=>{
|
|
224
|
+
try{
|
|
225
|
+
logger.info(`existingVesuDebt: ${existingVesuDebt} existingVesuCollateral: ${existingVesuCollateral} addedCollateral: ${addedCollateral} collateralPrice: ${collateralPrice} debtPrice: ${debtPrice} maxLtv: ${maxLtv} targetHf: ${targetHf}`);
|
|
226
|
+
const num1 = existingVesuCollateral.plus(addedCollateral).multipliedBy(collateralPrice).multipliedBy(maxLtv);
|
|
227
|
+
const num2 = existingVesuDebt.multipliedBy(debtPrice).multipliedBy(targetHf);
|
|
228
|
+
logger.info(`num1: ${num1}`);
|
|
229
|
+
logger.info(`num2: ${num2}`);
|
|
230
|
+
const deltaDebtAmount = num1.minus(num2).dividedBy(targetHf);
|
|
231
|
+
logger.info(`delta debt amount for mainatining ltv on vesu: ${deltaDebtAmount}`);
|
|
232
|
+
return new Web3Number(deltaDebtAmount.toFixed(USDC_TOKEN_DECIMALS), USDC_TOKEN_DECIMALS);
|
|
233
|
+
}catch(err){
|
|
234
|
+
logger.error(`error calculating debt amount for mainatining ltv on vesu: ${err}`);
|
|
235
|
+
return null;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Calculates the vesu deposit amount for multiplier
|
|
242
|
+
* @param existingVesuDebt - existing vesu debt in usdc terms
|
|
243
|
+
* @param existingVesuCollateral - existing vesu collateral in wbtc terms
|
|
244
|
+
* @param addedCollateral - added collateral in wbtc terms
|
|
245
|
+
* @param collateralPrice - collateral price in usdc terms
|
|
246
|
+
* @param debtPrice - debt price in usdc terms
|
|
247
|
+
* @param maxLtv - max ltv
|
|
248
|
+
* @param targetHf - target hf
|
|
249
|
+
* @returns vesu deposit amount in Wbtc terms
|
|
250
|
+
*/
|
|
251
|
+
export const calculateVesuDepositAmountForMultiplier = (
|
|
252
|
+
decisiveFactorF: Web3Number,
|
|
253
|
+
deltaDebtAmount: Web3Number,
|
|
254
|
+
collateralPrice: number
|
|
255
|
+
) => {
|
|
256
|
+
try {
|
|
257
|
+
const vesuLeverage = calculateVesuLeverage();
|
|
258
|
+
const vesuDepositAmount = decisiveFactorF
|
|
259
|
+
.multipliedBy(collateralPrice)
|
|
260
|
+
.multipliedBy(deltaDebtAmount)
|
|
261
|
+
.dividedBy(vesuLeverage)
|
|
262
|
+
.toFixed(2);
|
|
263
|
+
// base amount in usdc needed for swap to btc and open multiplier position
|
|
264
|
+
return new Web3Number(vesuDepositAmount, 2);
|
|
265
|
+
} catch (err) {
|
|
266
|
+
logger.error(`error calculating vesu deposit amount: ${err}`);
|
|
267
|
+
return null;
|
|
268
|
+
}
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Calculates the extended deposit amount in usdc terms
|
|
273
|
+
* @param decisiveFactorF is the decisive factor f
|
|
274
|
+
* @param deltaDebtAmount is the delta debt amount
|
|
275
|
+
* @param collateralPrice is the collateral price in usdc terms
|
|
276
|
+
* @returns the extended deposit amount in usdc terms
|
|
277
|
+
*/
|
|
278
|
+
export const calculateExtendedDepositAmount = (
|
|
279
|
+
decisiveFactorF: Web3Number,
|
|
280
|
+
deltaDebtAmount: Web3Number,
|
|
281
|
+
collateralPrice: number
|
|
282
|
+
) => {
|
|
283
|
+
try {
|
|
284
|
+
const multiplier = decisiveFactorF.minus(1).multipliedBy(-1);
|
|
285
|
+
// Extended position size in WBTC without Leverage, position will not be opened for whole amount, this is rather just to move assets
|
|
286
|
+
const extendedDepositAmount = deltaDebtAmount
|
|
287
|
+
.multipliedBy(multiplier).multipliedBy(collateralPrice).toFixed(2);
|
|
288
|
+
logger.info(`:calculateExtendedDepositAmount extendedPositionSize: ${extendedDepositAmount}`);
|
|
289
|
+
return new Web3Number(extendedDepositAmount,2);
|
|
290
|
+
} catch (err) {
|
|
291
|
+
logger.error(`error calculating extende deposit amount: ${err}`);
|
|
292
|
+
return null;
|
|
293
|
+
}
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* Calculates the borrow amount for stage 1 in usdc terms
|
|
299
|
+
* @param targetHealthFactor is the target health factor
|
|
300
|
+
* @param maxLtv is the max ltv
|
|
301
|
+
* @param existingVesuCollateral is the existing vesu collateral in wbtc terms
|
|
302
|
+
* @param addedCollateral is the added collateral in wbtc terms
|
|
303
|
+
* @param collateralPrice is the collateral price in usdc terms
|
|
304
|
+
* @param debtPrice
|
|
305
|
+
* @returns the borrow amount for stage 1 in usdc terms
|
|
306
|
+
*/
|
|
307
|
+
export const calculateBorrowAmountForStage1 =(
|
|
308
|
+
targetHealthFactor: number,
|
|
309
|
+
maxLtv: number = MAX_LTV_BTC_USDC,
|
|
310
|
+
existingVesuCollateral: Web3Number, // this is equal to the assets under management in wbtc terms
|
|
311
|
+
addedCollateral: Web3Number,
|
|
312
|
+
collateralPrice: number,
|
|
313
|
+
debtPrice: number
|
|
314
|
+
)=>{
|
|
315
|
+
try{
|
|
316
|
+
console.log("collateralPrice", collateralPrice)
|
|
317
|
+
console.log("debtPrice", debtPrice)
|
|
318
|
+
console.log("existingVesuCollateral", existingVesuCollateral.toString())
|
|
319
|
+
console.log("addedCollateral", addedCollateral.toString())
|
|
320
|
+
console.log("maxLtv", maxLtv)
|
|
321
|
+
console.log("targetHealthFactor", targetHealthFactor)
|
|
322
|
+
// Maintain full precision throughout calculations
|
|
323
|
+
const numerator1 = existingVesuCollateral.plus(addedCollateral).multipliedBy(collateralPrice).multipliedBy(maxLtv).dividedBy(targetHealthFactor);
|
|
324
|
+
logger.info(`numerator1 :: ${numerator1}`);
|
|
325
|
+
const existingVesuDebt = existingVesuCollateral.multipliedBy(collateralPrice).multipliedBy(maxLtv).dividedBy(targetHealthFactor).dividedBy(debtPrice);
|
|
326
|
+
const numerator3 = existingVesuDebt.multipliedBy(debtPrice).multipliedBy(-1);
|
|
327
|
+
logger.info(`numerator3 :: ${numerator3}`);
|
|
328
|
+
const deltaYStage1 = numerator1.plus(numerator3);
|
|
329
|
+
logger.info(`deltaYStage1 :: ${deltaYStage1}`);
|
|
330
|
+
return new Web3Number(deltaYStage1.toFixed(USDC_TOKEN_DECIMALS), USDC_TOKEN_DECIMALS);
|
|
331
|
+
}catch(err){
|
|
332
|
+
logger.error(`error calculating borrow amount for stage 1: ${err}`);
|
|
333
|
+
return null;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* Calculates the amount distribution for withdrawal in wbtc strategy
|
|
340
|
+
* @param amountInWbtc is the amount in wbtc terms
|
|
341
|
+
* @param collateralPrice is the collateral price in usdc terms
|
|
342
|
+
* @param vesuCollateralExposure is the vesu collateral exposure in wbtc terms
|
|
343
|
+
* @param extendedPosition is the extended position exposure in wbtc terms
|
|
344
|
+
* @returns the amount distribution for withdrawal in wbtc strategy
|
|
345
|
+
*/
|
|
346
|
+
export const calculateAmountDistributionForWithdrawalInWbtcStrategy = (
|
|
347
|
+
amountInWbtc: Web3Number,
|
|
348
|
+
vesuCollateralExposure: Web3Number,
|
|
349
|
+
extendedPosition: Position[] | null
|
|
350
|
+
) => {
|
|
351
|
+
try {
|
|
352
|
+
const extended_leverage = calculateExtendedLevergae();
|
|
353
|
+
const vesu_leverage = calculateVesuLeverage();
|
|
354
|
+
if (extendedPosition === null) {
|
|
355
|
+
logger.error("error getting extended positions");
|
|
356
|
+
return null;
|
|
357
|
+
}
|
|
358
|
+
const extendedExposureWBTC =
|
|
359
|
+
extendedPosition.length > 0
|
|
360
|
+
? new Web3Number(extendedPosition[0].size, WBTC_TOKEN_DECIMALS)
|
|
361
|
+
: new Web3Number(0,WBTC_TOKEN_DECIMALS);
|
|
362
|
+
if (vesuCollateralExposure.lessThanOrEqualTo(0)) {
|
|
363
|
+
return {
|
|
364
|
+
vesuAmount: new Web3Number(0, USDC_TOKEN_DECIMALS),
|
|
365
|
+
extendedAmount: amountInWbtc,
|
|
366
|
+
extended_leverage,
|
|
367
|
+
vesu_leverage,
|
|
368
|
+
};
|
|
369
|
+
}
|
|
370
|
+
if (extendedExposureWBTC.lessThanOrEqualTo(0)) {
|
|
371
|
+
return {
|
|
372
|
+
vesuAmount: amountInWbtc,
|
|
373
|
+
extendedAmount: new Web3Number(0, WBTC_TOKEN_DECIMALS),
|
|
374
|
+
extended_leverage,
|
|
375
|
+
vesu_leverage,
|
|
376
|
+
};
|
|
377
|
+
}
|
|
378
|
+
const numerator1 = amountInWbtc.multipliedBy(vesu_leverage);
|
|
379
|
+
const numerator2 = extendedExposureWBTC.multipliedBy(extended_leverage);
|
|
380
|
+
const numerator3 = vesuCollateralExposure.multipliedBy(vesu_leverage).multipliedBy(-1);
|
|
381
|
+
const finalNumerator = numerator1.plus(numerator2).plus(numerator3);
|
|
382
|
+
const denominator = vesu_leverage + extended_leverage;
|
|
383
|
+
const extendedAmountInWBTC = finalNumerator.dividedBy(denominator);
|
|
384
|
+
const vesuAmountInWBTC = amountInWbtc.minus(extendedAmountInWBTC);
|
|
385
|
+
return {
|
|
386
|
+
vesuAmount: vesuAmountInWBTC,
|
|
387
|
+
extendedAmount: extendedAmountInWBTC,
|
|
388
|
+
extended_leverage,
|
|
389
|
+
vesu_leverage,
|
|
390
|
+
}
|
|
391
|
+
}catch(err){
|
|
392
|
+
logger.error(`error calculating amount distribution for withdrawal in wbtc strategy: ${err}`);
|
|
393
|
+
return null;
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
|