@strkfarm/sdk 2.0.0-dev.9 → 2.0.0-staging.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. package/dist/index.browser.global.js +111371 -93151
  2. package/dist/index.browser.mjs +27815 -32690
  3. package/dist/index.d.ts +1095 -2011
  4. package/dist/index.js +27425 -32309
  5. package/dist/index.mjs +27590 -32452
  6. package/package.json +6 -5
  7. package/src/data/ekubo-price-fethcer.abi.json +265 -0
  8. package/src/data/universal-vault.abi.json +20 -135
  9. package/src/dataTypes/address.ts +0 -7
  10. package/src/dataTypes/index.ts +3 -2
  11. package/src/dataTypes/mynumber.ts +141 -0
  12. package/src/global.ts +296 -288
  13. package/src/index.browser.ts +6 -5
  14. package/src/interfaces/common.tsx +324 -184
  15. package/src/modules/apollo-client-config.ts +28 -0
  16. package/src/modules/avnu.ts +4 -17
  17. package/src/modules/ekubo-pricer.ts +79 -0
  18. package/src/modules/ekubo-quoter.ts +11 -88
  19. package/src/modules/erc20.ts +21 -67
  20. package/src/modules/harvests.ts +26 -15
  21. package/src/modules/index.ts +11 -13
  22. package/src/modules/lst-apr.ts +0 -36
  23. package/src/modules/pragma.ts +23 -8
  24. package/src/modules/pricer-from-api.ts +150 -14
  25. package/src/modules/pricer.ts +2 -1
  26. package/src/modules/pricerBase.ts +2 -1
  27. package/src/node/deployer.ts +36 -1
  28. package/src/node/pricer-redis.ts +2 -1
  29. package/src/strategies/autoCompounderStrk.ts +1 -1
  30. package/src/strategies/base-strategy.ts +5 -22
  31. package/src/strategies/ekubo-cl-vault.tsx +2904 -2175
  32. package/src/strategies/factory.ts +165 -0
  33. package/src/strategies/index.ts +10 -11
  34. package/src/strategies/registry.ts +268 -0
  35. package/src/strategies/sensei.ts +416 -292
  36. package/src/strategies/universal-adapters/adapter-utils.ts +1 -5
  37. package/src/strategies/universal-adapters/baseAdapter.ts +153 -181
  38. package/src/strategies/universal-adapters/common-adapter.ts +77 -98
  39. package/src/strategies/universal-adapters/index.ts +1 -5
  40. package/src/strategies/universal-adapters/vesu-adapter.ts +218 -220
  41. package/src/strategies/universal-adapters/vesu-supply-only-adapter.ts +51 -58
  42. package/src/strategies/universal-lst-muliplier-strategy.tsx +1952 -992
  43. package/src/strategies/universal-strategy.tsx +1713 -1150
  44. package/src/strategies/vesu-rebalance.tsx +1189 -986
  45. package/src/utils/health-factor-math.ts +5 -11
  46. package/src/utils/index.ts +8 -9
  47. package/src/utils/strategy-utils.ts +57 -0
  48. package/src/data/extended-deposit.abi.json +0 -3613
  49. package/src/modules/ExtendedWrapperSDk/index.ts +0 -62
  50. package/src/modules/ExtendedWrapperSDk/types.ts +0 -311
  51. package/src/modules/ExtendedWrapperSDk/wrapper.ts +0 -395
  52. package/src/modules/midas.ts +0 -159
  53. package/src/modules/token-market-data.ts +0 -202
  54. package/src/strategies/svk-strategy.ts +0 -247
  55. package/src/strategies/universal-adapters/adapter-optimizer.ts +0 -65
  56. package/src/strategies/universal-adapters/avnu-adapter.ts +0 -413
  57. package/src/strategies/universal-adapters/extended-adapter.ts +0 -972
  58. package/src/strategies/universal-adapters/unused-balance-adapter.ts +0 -109
  59. package/src/strategies/universal-adapters/vesu-multiply-adapter.ts +0 -1306
  60. package/src/strategies/vesu-extended-strategy/services/operationService.ts +0 -34
  61. package/src/strategies/vesu-extended-strategy/utils/config.runtime.ts +0 -77
  62. package/src/strategies/vesu-extended-strategy/utils/constants.ts +0 -49
  63. package/src/strategies/vesu-extended-strategy/utils/helper.ts +0 -370
  64. package/src/strategies/vesu-extended-strategy/vesu-extended-strategy.tsx +0 -1379
@@ -1,34 +0,0 @@
1
- import { Web3Number } from "@/dataTypes";
2
- import { TokenInfo } from "@/interfaces";
3
- import { ExtendedAdapter } from "@/strategies/universal-adapters/extended-adapter";
4
- import { VesuMultiplyAdapter } from "../../universal-adapters/vesu-multiply-adapter";
5
- import { Call } from "starknet";
6
- export abstract class Operations {
7
- abstract shouldMoveAssets(
8
- extendedAmount: Web3Number,
9
- vesuAmount: Web3Number
10
- ): Promise<Call[]>;
11
- abstract shouldInvest(): Promise<{
12
- shouldInvest: boolean;
13
- vesuAmount: Web3Number;
14
- extendedAmount: Web3Number;
15
- extendedLeverage: number;
16
- vesuLeverage: number;
17
- }>;
18
- abstract moveAssets(
19
- params: { from: string; to: string; amount: Web3Number },
20
- extendedAdapter: ExtendedAdapter,
21
- vesuAdapter: VesuMultiplyAdapter
22
- ): Promise<{
23
- calls: Call[];
24
- status: boolean;
25
- }>;
26
- abstract handleDeposit(): Promise<{
27
- extendedAmountInBTC: Web3Number;
28
- calls: Call[];
29
- }>;
30
- abstract handleWithdraw(amount: Web3Number): Promise<{
31
- calls: Call[];
32
- status: boolean;
33
- }> ;
34
- }
@@ -1,77 +0,0 @@
1
- import {
2
- AVNU_API,
3
- USDC_TOKEN_ADDRESS,
4
- USDC_TOKEN_DECIMALS,
5
- WBTC_TOKEN_ADDRESS,
6
- WBTC_TOKEN_DECIMALS,
7
- WALLET_ADDRESS,
8
- EXTEND_CONTRACT_ADDRESS,
9
- MULTIPLY_CONTRACT_ADDRESS,
10
- EXTENDED_BASE_URL,
11
- EXTEND_MARKET_NAME,
12
- EXTENDED_QTY_PRECISION,
13
- EXTENDED_FEES,
14
- MAINTENANCE_MARGIN,
15
- VESU_POOL_ID,
16
- MAX_LTV_BTC_USDC,
17
- MAX_LIQUIDATION_RATIO,
18
- TARGET_HF,
19
- EKUBO_ENDPOINT,
20
- PRICE_MAX_SLIPPAGE_EKUBO,
21
- MINIMUM_DEBT_AMOUNT_VESU_FOR_REBALANCING,
22
- MINIMUM_EXTENDED_POSITION_SIZE
23
- } from "./constants"
24
- import VesuPoolV2Abi from '@/data/vesu-pool-v2.abi.json';
25
- import ExtendedDepositAbi from '@/data/extended-deposit.abi.json';
26
- import VesuMultiplyAbi from '@/data/vesu-multiple.abi.json';
27
-
28
- // Grouped, readable configuration (no behavior change). Consumers can migrate gradually.
29
-
30
- export const AddressesConfig = {
31
- tokens: {
32
- USDC: { address: USDC_TOKEN_ADDRESS, decimals: USDC_TOKEN_DECIMALS },
33
- WBTC: { address: WBTC_TOKEN_ADDRESS, decimals: WBTC_TOKEN_DECIMALS },
34
- },
35
- contracts: {
36
- EXTENDED: EXTEND_CONTRACT_ADDRESS,
37
- MULTIPLY: MULTIPLY_CONTRACT_ADDRESS,
38
- },
39
- wallet: {
40
- address: WALLET_ADDRESS,
41
- },
42
- } as const;
43
-
44
- export const ExtendedConfig = {
45
- baseUrl: EXTENDED_BASE_URL,
46
- marketName: EXTEND_MARKET_NAME,
47
- maintenanceMargin: MAINTENANCE_MARGIN,
48
- precision: EXTENDED_QTY_PRECISION,
49
- fees: EXTENDED_FEES,
50
- minPositionSize: MINIMUM_EXTENDED_POSITION_SIZE,
51
- } as const;
52
-
53
- export const VesuConfig = {
54
- poolId: VESU_POOL_ID,
55
- maxLtv: MAX_LTV_BTC_USDC,
56
- maxLiquidationRatio: MAX_LIQUIDATION_RATIO,
57
- targetHealthFactor: TARGET_HF,
58
- ekubo: {
59
- endpoint: EKUBO_ENDPOINT,
60
- priceMaxSlippage: PRICE_MAX_SLIPPAGE_EKUBO,
61
- },
62
- avnu: {
63
- api: AVNU_API,
64
- },
65
- minDebtForVesuRebalacing:MINIMUM_DEBT_AMOUNT_VESU_FOR_REBALANCING
66
- } as const;
67
-
68
- export const AbisConfig = {
69
- vesu: {
70
- multiply: VesuMultiplyAbi,
71
- pool: VesuPoolV2Abi,
72
- },
73
- extended: {
74
- contract: ExtendedDepositAbi,
75
- },
76
- } as const;
77
-
@@ -1,49 +0,0 @@
1
- export const AVNU_API = "https://starknet.api.avnu.fi/swap/v2/quotes";
2
- export const SLIPPAGE = 0.01;
3
- export const USDC_TOKEN_DECIMALS = 6;
4
- export const USDC_TOKEN_ADDRESS =
5
- "0x053C91253BC9682c04929cA02ED00b3E423f6710D2ee7e0D5EBB06F3eCF368A8";
6
- export const BUFFER_USDC_IN_WITHDRAWAL=5;
7
- export const WBTC_TOKEN_ADDRESS = "0x3fe2b97c1fd336e750087d68b9b867997fd64a2661ff3ca5a7c771641e8e7ac";
8
- export const WBTC_TOKEN_DECIMALS = 8;
9
- export const MAINTENANCE_MARGIN = 0.01;
10
- export const MAX_PRICE_DROP_PERCENTAGE = Number(process.env.MAX_PRICE_DROP_PERCENTAGE ?? 0.25);
11
- export const MAX_LTV_BTC_USDC = 0.8428;
12
- export const MAX_LIQUIDATION_RATIO = 0.86;
13
- export const VAULT_ID_EXTENDED= process.env.VAULT_ID_EXTENDED ?? 220774;
14
- export const WALLET_ADDRESS =
15
- process.env.WALLET_ADDRESS ?? "0x07b84bb6E87588BdAde0bfe6173A615b3C220F9C3803456aE183C50EA1d15Ba1";
16
- export const TESTNET_WALLET_ADDRESS =
17
- process.env.TESTNET_WALLET_ADDRESS ?? "0x07b84bb6E87588BdAde0bfe6173A615b3C220F9C3803456aE183C50EA1d15Ba1";
18
- export const TEST_WALLET_2 =
19
- process.env.TEST_WALLET_2 ?? "0x004C1bdC61DAc7947F3C93d0163d660012E2aB0521567f7155fcf502848791A7";
20
- export const STRK_API_TEST_RPC = process.env.STRK_API_TEST_RPC ?? "https://sepolia.starknet.a5a.ch";
21
- export const STRK_API_RPC = process.env.STRK_API_RPC ?? "https://mainnet.starknet.a5a.ch";
22
- export const MAX_RETRIES = Number(process.env.MAX_RETRIES ?? 3);
23
- export const MAX_DELAY = Number(process.env.MAX_DELAY ?? 100);
24
- export const EXTEND_MARKET_NAME = "BTC-USD";
25
- export const LIMIT_BALANCE = Number(process.env.LIMIT_BALANCE ?? 0.05);
26
- export const REBALANCER_INTERVAL = Number(process.env.REBALANCER_INTERVAL ?? 180000); //3 mins
27
- export const WITHDRAWAL_INTERVAL = Number(process.env.WITHDRAWAL_INTERVAL ?? 18000000); //5 hours
28
- export const INVESTING_INTERVAL = Number(process.env.INVESTING_INTERVAL ?? 180000); //3 mins
29
- export const MAXIMUM_DELTA = Number(process.env.MAXIMUM_DELTA ?? 5);
30
- export const EXTENDED_BASE_URL = process.env.EXTENDED_BASE_URL ?? "https://api.starknet.extended.exchange";
31
- export const VESU_POOL_ID = "0x02eef0c13b10b487ea5916b54c0a7f98ec43fb3048f60fdeedaf5b08f6f88aaf";
32
- export const EKUBO_ENDPOINT = "https://quoter-mainnet-api.ekubo.org/{{AMOUNT}}/{{TOKEN_FROM_ADDRESS}}/{{TOKEN_TO_ADDRESS}}"; // e.g. ETH/USDC'
33
- export const TARGET_HF = Number(process.env.TARGET_HF ?? 1.4);
34
- export const MULTIPLY_CONTRACT_ADDRESS = "0x07964760e90baa28841ec94714151e03fbc13321797e68a874e88f27c9d58513";
35
- export const EXTEND_CONTRACT_ADDRESS = "0x062da0780fae50d68cecaa5a051606dc21217ba290969b302db4dd99d2e9b470";
36
- export const EXTENDED_QTY_PRECISION = 5;
37
- export const EXTENDED_FEES= Number(process.env.EXTENDED_FEES ?? 0.006);
38
- export const REBALANCE_PRICE_DROP_PERCENTAGE = Number(process.env.REBALANCE_PRICE_DROP_PERCENTAGE ?? 3);
39
- export const PRICE_MAX_SLIPPAGE_EKUBO = Number(process.env.PRICE_MAX_SLIPPAGE_EKUBO ?? 0.005);
40
- export const MINIMUM_DEBT_AMOUNT_VESU_FOR_REBALANCING = Number(process.env.MINIMUM_DEBT_AMOUNT_VESU_FOR_REBALANCING ?? 1);
41
- export const MINIMUM_EXTENDED_POSITION_SIZE = 0.0001;
42
- export const MINIMUM_WBTC_DIFFERENCE_FOR_AVNU_SWAP = 0.00001;
43
- export const MAX_PRICE_DIFFERENCE_BETWEEN_AVNU_AND_EXTENDED = 700;
44
- export const MIN_PRICE_DIFFERENCE_BETWEEN_AVNU_AND_EXTENDED = -100;
45
-
46
- //STATUS CODES
47
- export const SUCCESS_STATUS_CODE = 200;
48
- export const ERROR_STATUS_CODE = 500;
49
- export const BAD_REQUEST_STATUS_CODE = 400;
@@ -1,370 +0,0 @@
1
- import {
2
- MAINTENANCE_MARGIN,
3
- MAX_LTV_BTC_USDC,
4
- MAX_PRICE_DROP_PERCENTAGE,
5
- TARGET_HF,
6
- WBTC_TOKEN_ADDRESS,
7
- USDC_TOKEN_ADDRESS,
8
- WBTC_TOKEN_DECIMALS,
9
- USDC_TOKEN_DECIMALS,
10
- MAX_LIQUIDATION_RATIO,
11
- } from "./constants";
12
- import { Web3Number } from "@/dataTypes";
13
- import { Position } from "@/modules/ExtendedWrapperSDk";
14
- // import { getAllOpenPositionsExtended } from "../services/extendedService";
15
- import ExtendedWrapper from "@/modules/ExtendedWrapperSDk";
16
- import { logger } from "@/utils";
17
- // import {
18
- // calculatePositionOnVesu,
19
- // getAssetPrice,
20
- // } from "../services/vesuService";
21
- /**
22
- * Function to return formatted amount to BigInt
23
- * Converts a decimal amount to the proper format for blockchain transactions
24
- * @param {number} amount - The decimal amount to convert
25
- * @param {number} fromTokenDecimals - The decimal precision of the token
26
- * @returns {string} The formatted amount as a hexadecimal string
27
- */
28
- export const returnFormattedAmount = (
29
- amount: number,
30
- toTokenDecimals: number
31
- ) => {
32
- const formattedAmount =
33
- "0x" + BigInt(Math.floor(amount * 10 ** toTokenDecimals)).toString(16);
34
- return formattedAmount;
35
- };
36
-
37
- /**
38
- * calculates the amount to distribute to Extend and Vesu
39
- * Determines how much to allocate to each platform based on leverage calculations
40
- * @param {number} amount - The total amount to distribute
41
- * @returns {object} Object containing avnu_amount, extended_amount, and extended_leverage
42
- */
43
- export const calculateAmountDistribution = async (
44
- amount: number,
45
- client: ExtendedWrapper,
46
- marketName: string,
47
- collateralPrice: number,
48
- debtPrice: number,
49
- collateralUnits: Web3Number,
50
- extendedPosition: Position[] | null
51
- ): Promise<{
52
- vesu_amount: Web3Number;
53
- extended_amount: Web3Number;
54
- extended_leverage: number;
55
- vesu_leverage: number;
56
- }> => {
57
- try {
58
- const extended_leverage = calculateExtendedLevergae();
59
- const vesu_leverage = calculateVesuLeverage();
60
- if (extendedPosition === null) {
61
- logger.error("error getting extended positions");
62
- return {
63
- vesu_amount: new Web3Number(0, 0),
64
- extended_amount: new Web3Number(0, 0),
65
- extended_leverage: 0,
66
- vesu_leverage: 0,
67
- };
68
- }
69
- const extendedExposureUSD =
70
- extendedPosition.length > 0
71
- ? new Web3Number(extendedPosition[0].value, WBTC_TOKEN_DECIMALS)
72
- : new Web3Number(0, WBTC_TOKEN_DECIMALS);
73
- const vesuBTCExposureUSD = collateralUnits.multipliedBy(collateralPrice);
74
- const numerator1 = vesu_leverage * amount + vesuBTCExposureUSD.toNumber();
75
- const numerator2 = extendedExposureUSD.toNumber();
76
- const denominator = vesu_leverage + extended_leverage;
77
- const ExtendedAmount = new Web3Number(
78
- ((numerator1 - numerator2) / denominator).toFixed(2),
79
- USDC_TOKEN_DECIMALS
80
- );
81
-
82
- const VesuAmount = new Web3Number(
83
- amount.toFixed(2),
84
- USDC_TOKEN_DECIMALS
85
- ).minus(ExtendedAmount);
86
-
87
- return {
88
- vesu_amount: VesuAmount,
89
- extended_amount: ExtendedAmount,
90
- extended_leverage,
91
- vesu_leverage,
92
- };
93
- } catch (err) {
94
- return {
95
- vesu_amount: new Web3Number(0, 0),
96
- extended_amount: new Web3Number(0, 0),
97
- extended_leverage: 0,
98
- vesu_leverage: 0,
99
- };
100
- }
101
- };
102
-
103
- /**
104
- * calculate the amount distribution for withdrawal
105
- * @param amount - The amount to withdraw
106
- * @param client - The client
107
- * @param marketName - The market name
108
- * @returns {object} Object containing avnu_amount and extended_amount
109
- */
110
- export const calculateAmountDistributionForWithdrawal = async (
111
- amountInUsdc: Web3Number,
112
- collateralPrice: number,
113
- collateralUnits: Web3Number,
114
- extendedPosition: Position[] | null
115
- ): Promise<{
116
- vesu_amount: Web3Number;
117
- extended_amount: Web3Number;
118
- extended_leverage: number;
119
- vesu_leverage: number;
120
- } | null> => {
121
- try {
122
- const extended_leverage = calculateExtendedLevergae();
123
- const vesu_leverage = calculateVesuLeverage();
124
- if (extendedPosition === null) {
125
- logger.error("error getting extended positions");
126
- return null;
127
- }
128
- const extendedExposureUSD =
129
- extendedPosition.length > 0
130
- ? new Web3Number(extendedPosition[0].value, USDC_TOKEN_DECIMALS)
131
- : new Web3Number(0, USDC_TOKEN_DECIMALS);
132
- const vesuExposureUSD = collateralUnits.multipliedBy(collateralPrice);
133
- if (vesuExposureUSD.lessThan(0)) {
134
- return {
135
- vesu_amount: new Web3Number(0, USDC_TOKEN_DECIMALS),
136
- extended_amount: amountInUsdc,
137
- extended_leverage,
138
- vesu_leverage,
139
- };
140
- }
141
- if (extendedExposureUSD.lessThan(0)) {
142
- return {
143
- vesu_amount: amountInUsdc,
144
- extended_amount: new Web3Number(0, USDC_TOKEN_DECIMALS),
145
- extended_leverage,
146
- vesu_leverage,
147
- };
148
- }
149
- console.log("the vesu exposure usd is", vesuExposureUSD.toNumber());
150
- console.log("the extended exposure usd is", extendedExposureUSD.toNumber());
151
- console.log("the amount in usdc is", amountInUsdc.toNumber());
152
- console.log("the extended leverage is", extended_leverage);
153
- console.log("the vesu leverage is", vesu_leverage);
154
- const numerator1 = amountInUsdc.multipliedBy(extended_leverage);
155
- const numerator2 = vesuExposureUSD;
156
- const numerator3 = extendedExposureUSD
157
- .multipliedBy(-1);
158
- const finalNumerator = numerator1.plus(numerator2).plus(numerator3);
159
- const denominator = extended_leverage + vesu_leverage;
160
- const vesuAmountInUSDC = finalNumerator.dividedBy(denominator);
161
- console.log("the vesu amount in usdc is", vesuAmountInUSDC.toNumber());
162
- const extendedAmountInUSDC = amountInUsdc.minus(vesuAmountInUSDC);
163
- console.log(
164
- "the extended amount in usdc is",
165
- extendedAmountInUSDC.toNumber()
166
- );
167
- //console.log("the vesu amount in usdc is", vesuAmountInUSDC.toNumber());
168
- //console.log("the extended amount in usdc is", extendedAmountInUSDC.toNumber());\
169
- await new Promise((resolve) => setTimeout(resolve, 10000));
170
- return {
171
- vesu_amount: vesuAmountInUSDC,
172
- extended_amount: extendedAmountInUSDC,
173
- extended_leverage,
174
- vesu_leverage,
175
- };
176
- } catch (err) {
177
- logger.error(`error calculating amount distribution for withdrawal: ${err}`);
178
- return null;
179
- }
180
- };
181
- /**
182
- * calculate the leverage required for Avnu
183
- * calculates the optimal leverage for Avnu based on LTV ratios and price drop protection
184
- * @returns {number} The calculated leverage value
185
- */
186
- export const calculateVesuLeverage = () => {
187
- const max_ltv_ratio = (1 - MAX_PRICE_DROP_PERCENTAGE) * MAX_LIQUIDATION_RATIO; //0.75
188
- const our_ltv_ratio = Math.floor(max_ltv_ratio * 100) - 5; //buffer of 5% to prevent liquidation
189
- const leverage = 1 / (1 - our_ltv_ratio / 100);
190
- return Math.ceil(leverage * 10) / 10;
191
- };
192
-
193
- /**
194
- * calculate leverage for extended
195
- * calculates the maximum safe leverage for Extended based on maintenance margin and price drop protection
196
- * @returns {number} The calculated leverage value
197
- */
198
- export const calculateExtendedLevergae = () => {
199
- const extended_leverage_max =
200
- 1 / (MAINTENANCE_MARGIN + MAX_PRICE_DROP_PERCENTAGE);
201
- return Math.floor(extended_leverage_max);
202
- };
203
-
204
- /**
205
- * calculates the debt amount for leverage operations
206
- * Determines how much debt to add or remove based on collateral changes and target health factor
207
- * @param {Web3Number} collateralAmount - Current collateral amount
208
- * @param {Web3Number} debtAmount - Current debt amount
209
- * @param {number} debtPrice - Current price of the debt token
210
- * @param {number} maxLtv - Maximum loan-to-value ratio (default: MAX_LTV_BTC_USDC)
211
- * @param {number} addedAmount - Amount being added to collateral
212
- * @param {number} collateralPrice - Current price of the collateral token
213
- * @param {boolean} isDeposit - Whether this is a deposit (true) or withdrawal (false)
214
- * @returns {object} Object containing deltadebtAmountUnits and isIncrease flag
215
- */
216
- // In case BTC_PRICE DROPS the added amount will be zero, and use this formula to calculated the debt that needs to be paid to maintain the ltv
217
- export const calculateDebtAmount = (
218
- collateralAmount: Web3Number,
219
- debtAmount: Web3Number,
220
- debtPrice: number,
221
- maxLtv: number = MAX_LIQUIDATION_RATIO,
222
- addedAmount: Web3Number, // this is in btc
223
- collateralPrice: number,
224
- isDeposit: boolean
225
- ) => {
226
- try {
227
- // => X = (((collateral + legDepositAmount) * collateralPrice * ltv) - (debt * debtPrice * target hf)) / (target hf - ltv)
228
- const addedCollateral = addedAmount.multipliedBy(isDeposit ? 1 : -1);
229
- const numerator1 = (collateralAmount
230
- .plus(addedCollateral))
231
- .multipliedBy(collateralPrice)
232
- .multipliedBy(maxLtv);
233
- const numerator2 = debtAmount
234
- .multipliedBy(debtPrice)
235
- .multipliedBy(TARGET_HF);
236
- const denominator = TARGET_HF - maxLtv;
237
- const x_debt_usd = numerator1.minus(numerator2).dividedBy(denominator);
238
- let deltadebtAmountUnits = new Web3Number(
239
- x_debt_usd.dividedBy(debtPrice).toFixed(2),
240
- 2
241
- );
242
- let isIncrease = x_debt_usd.greaterThan(0);
243
- return { deltadebtAmountUnits, isIncrease };
244
- } catch (err) {
245
- return { deltadebtAmountUnits: null, isIncrease: null };
246
- }
247
- };
248
-
249
- /**
250
- * calculate the debt amount to be repaid for withdrawal
251
- * @param debtAmount in debt units
252
- * @param collateralAmount in collateral units
253
- * @param maxLtv in percentage
254
- * @param withdrawalAmount in collateral units
255
- * @param collateralPrice in usd
256
- * @param debtPrice in usd
257
- * @returns deltadebtAmountUnits in debt units
258
- * isIncrease: true if the debt amount is increasing, false if it is decreasing
259
- */
260
- export const calculateDebtReductionAmountForWithdrawal = (
261
- debtAmount: Web3Number,
262
- collateralAmount: Web3Number,
263
- maxLtv: number = MAX_LTV_BTC_USDC,
264
- withdrawalAmount: Web3Number,
265
- collateralPrice: number,
266
- debtPrice: number,
267
- usdcDecimals: number
268
- ) => {
269
- try {
270
- const vesuLeverage = calculateVesuLeverage();
271
- const numerator1 = collateralAmount
272
- .multipliedBy(collateralPrice)
273
- .multipliedBy(maxLtv)
274
- .multipliedBy(-1);
275
- withdrawalAmount = withdrawalAmount.multipliedBy(vesuLeverage);
276
- const numerator2 = debtAmount
277
- .multipliedBy(debtPrice)
278
- .multipliedBy(TARGET_HF);
279
- const numerator3 = withdrawalAmount
280
- .multipliedBy(collateralPrice)
281
- .multipliedBy(maxLtv);
282
- const numeratorTotal = numerator1.plus(numerator2).plus(numerator3);
283
- const denominator = debtPrice * TARGET_HF;
284
- const x_debt_usdc = numeratorTotal.dividedBy(denominator);
285
- let deltadebtAmountUnits = x_debt_usdc
286
- .multipliedBy(-1)
287
- .toFixed(usdcDecimals); // means we are paying debt
288
- return { deltadebtAmountUnits };
289
- } catch (err) {
290
- return { deltadebtAmountUnits: null };
291
- }
292
- };
293
-
294
- /**
295
- * calculate the amount to deposit on extended when incurring losses
296
- * @param client - The client
297
- * @returns The amount to deposit on extended when incurring losses
298
- */
299
- export const calculateAmountDepositOnExtendedWhenIncurringLosses = async (
300
- client: ExtendedWrapper
301
- ) => {
302
- try {
303
- const extendedHoldings = await client.getHoldings();
304
- const extended_leverage = calculateExtendedLevergae();
305
- const latestPosition = (await client.getPositions()).data.pop();
306
- if (!extendedHoldings || !latestPosition) {
307
- logger.error(`error getting extended position: extendedHoldings=${extendedHoldings}, latestPosition=${latestPosition}`);
308
- return null;
309
- }
310
- const positionValueInUSD = new Web3Number(latestPosition.value, USDC_TOKEN_DECIMALS);
311
- const equity = extendedHoldings.data.equity;
312
- const deposit = positionValueInUSD.dividedBy(extended_leverage).minus(equity).toFixed(2);
313
- return new Web3Number(deposit, USDC_TOKEN_DECIMALS);
314
- } catch (err) {
315
- logger.error(`error calculating amount deposit on extended when incurring losses: ${err}`);
316
- return null;
317
- }
318
- };
319
-
320
- export const calculateExposureDelta = (
321
- exposure_extended: number,
322
- exposure_vesu: number
323
- ) => {
324
- const exposure_delta = new Web3Number(exposure_extended - exposure_vesu, 2);
325
- return exposure_delta.absoluteValue().toNumber();
326
- };
327
-
328
- /// In case BTC PRICE DROPS
329
- // 1. calculate the ltv on vesu
330
- // 2. Find the debt that needs to be paid to maintain the ltv
331
-
332
- /**
333
- * calculate the delta percentage between the current btc price and the last btc price
334
- * @param {number} btcPrice - The current btc price
335
- * @param {number} lastBtcPrice - The last btc price
336
- * @returns {number} The delta percentage
337
- */
338
- export const calculateBTCPriceDelta = (
339
- btcPrice: number,
340
- lastBtcPrice: number
341
- ) => {
342
- return ((btcPrice - lastBtcPrice) / lastBtcPrice) * 100;
343
- };
344
-
345
- export const calculateVesUPositionSizeGivenExtended = (
346
- extendedPositonValue: number,
347
- extendedHoldingAmount: Web3Number,
348
- collateralAmount: Web3Number,
349
- collateralPrice: number
350
- ) => {
351
- const extendedLeverage = calculateExtendedLevergae();
352
- const vesuLeverage = calculateVesuLeverage();
353
- const extendedAmount = extendedHoldingAmount;
354
- const extendedAmountInBTC = extendedAmount
355
- .dividedBy(collateralPrice)
356
- const numerator1 = extendedAmount.multipliedBy(extendedLeverage)
357
- .plus(extendedPositonValue)
358
- const numerator2 = collateralAmount
359
- .multipliedBy(collateralPrice)
360
- .multipliedBy(-1);
361
- const vesuAmountInUsd = numerator1.plus(numerator2).dividedBy(vesuLeverage);
362
- const vesuAmountInBTC = vesuAmountInUsd
363
- .dividedBy(collateralPrice)
364
- .toFixed(WBTC_TOKEN_DECIMALS);
365
- return {
366
- vesuAmountInUsd: vesuAmountInUsd.toFixed(2),
367
- vesuAmountInBTC: new Web3Number(vesuAmountInBTC, WBTC_TOKEN_DECIMALS),
368
- extendedAmountInBTC: extendedAmountInBTC,
369
- };
370
- };