@strkfarm/sdk 2.0.0-dev.2 → 2.0.0-dev.21

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.
@@ -786,7 +786,8 @@ function getLooperSettings(
786
786
  minHealthFactor: vaultSettings.minHealthFactor,
787
787
  quoteAmountToFetchPrice: vaultSettings.quoteAmountToFetchPrice,
788
788
  ...baseAdapterConfig,
789
- supportedPositions: [{asset: lstToken, isDebt: false}, {asset: Global.getDefaultTokens().find(token => token.symbol === position)!, isDebt: true}]
789
+ supportedPositions: [{asset: lstToken, isDebt: false}, {asset: Global.getDefaultTokens().find(token => token.symbol === position)!, isDebt: true}],
790
+ minimumVesuMovementAmount: 0
790
791
  }));
791
792
 
792
793
  const unusedBalanceAdapter = new UnusedBalanceAdapter({
@@ -38,6 +38,11 @@ export enum AUMTypes {
38
38
  DEFISPRING = 'defispring'
39
39
  }
40
40
 
41
+ export enum PositionTypeAvnuExtended {
42
+ OPEN = 'open',
43
+ CLOSE = 'close'
44
+ }
45
+
41
46
  // export class UniversalStrategy<
42
47
  // S extends UniversalStrategySettings
43
48
  // > extends BaseStrategy<
@@ -1,13 +1,16 @@
1
1
  import { Web3Number } from "@/dataTypes";
2
- import { TokenInfo } from "@/interfaces";
3
2
  import { ExtendedAdapter } from "@/strategies/universal-adapters/extended-adapter";
4
3
  import { VesuMultiplyAdapter } from "../../universal-adapters/vesu-multiply-adapter";
5
- import { Call } from "starknet";
4
+ import { CycleType, TransactionResult } from "../types/transaction-metadata";
5
+
6
6
  export abstract class Operations {
7
+
7
8
  abstract shouldMoveAssets(
8
9
  extendedAmount: Web3Number,
9
10
  vesuAmount: Web3Number
10
- ): Promise<Call[]>;
11
+ ): Promise<TransactionResult[]>;
12
+
13
+
11
14
  abstract shouldInvest(): Promise<{
12
15
  shouldInvest: boolean;
13
16
  vesuAmount: Web3Number;
@@ -15,20 +18,26 @@ export abstract class Operations {
15
18
  extendedLeverage: number;
16
19
  vesuLeverage: number;
17
20
  }>;
21
+
22
+ /**
23
+ * Move assets between protocols.
24
+ * Returns transaction calls and metadata for DB tracking.
25
+ */
18
26
  abstract moveAssets(
19
- params: { from: string; to: string; amount: Web3Number },
27
+ params: { from: string; to: string; amount: Web3Number, cycleType: CycleType },
20
28
  extendedAdapter: ExtendedAdapter,
21
- vesuAdapter: VesuMultiplyAdapter
22
- ): Promise<{
23
- calls: Call[];
24
- status: boolean;
25
- }>;
26
- abstract handleDeposit(): Promise<{
29
+ vesuAdapter: VesuMultiplyAdapter,
30
+ ): Promise<TransactionResult>;
31
+ /**
32
+ * Handle deposit operation.
33
+ * Returns transaction calls and metadata for DB tracking.
34
+ */
35
+ abstract handleDeposit(): Promise<TransactionResult<{
27
36
  extendedAmountInBTC: Web3Number;
28
- calls: Call[];
29
- }>;
30
- abstract handleWithdraw(amount: Web3Number): Promise<{
31
- calls: Call[];
32
- status: boolean;
33
- }> ;
37
+ }>>;
38
+ /**
39
+ * Handle withdrawal operation.
40
+ * Returns transaction calls and metadata for DB tracking.
41
+ */
42
+ abstract handleWithdraw(amount: Web3Number): Promise<TransactionResult[]>;
34
43
  }
@@ -0,0 +1,36 @@
1
+ import { Call } from "starknet";
2
+
3
+ /**
4
+ * Transaction metadata that SDK generates and risk engine stores in DB.
5
+ * This metadata is used to track transaction lifecycle without coupling SDK to DB.
6
+ */
7
+ export interface TransactionMetadata {
8
+ protocolFrom: string;
9
+ protocolTo: string;
10
+ transactionType: 'DEPOSIT' | 'WITHDRAWAL' | 'NONE';
11
+ usdAmount: string;
12
+ status: 'COMPLETED' | 'FAILED' | 'PENDING';
13
+ cycleType: CycleType;
14
+ }
15
+
16
+
17
+ export enum CycleType {
18
+ INVESTMENT = 'INVESTMENT',
19
+ REBALANCE_PRICE_DROP = 'REBALANCE_PRICE_DROP',
20
+ REBALANCE_PRICE_RISE = 'REBALANCE_PRICE_RISE',
21
+ WITHDRAWAL = 'WITHDRAWAL',
22
+ DELTA_NEUTRAL_ADJUSTMENT = 'DELTA_NEUTRAL_ADJUSTMENT',
23
+ }
24
+
25
+ /**
26
+ * Enhanced return type for operations that generate transactions.
27
+ * Includes both the calls (for execution) and metadata (for DB storage).
28
+ */
29
+ export interface TransactionResult<T = any> {
30
+ calls: Call[];
31
+ status: boolean;
32
+ transactionMetadata: TransactionMetadata;
33
+ }
34
+
35
+
36
+
@@ -12,17 +12,14 @@ export const MAX_LTV_BTC_USDC = 0.8428;
12
12
  export const MAX_LIQUIDATION_RATIO = 0.86;
13
13
  export const VAULT_ID_EXTENDED= process.env.VAULT_ID_EXTENDED ?? 220774;
14
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";
15
+ process.env.WALLET_ADDRESS ?? "0x007E24592287427aaE9d291770B17d582E8A45f3aE54228F998793Ec769B7D13";
20
16
  export const STRK_API_TEST_RPC = process.env.STRK_API_TEST_RPC ?? "https://sepolia.starknet.a5a.ch";
21
17
  export const STRK_API_RPC = process.env.STRK_API_RPC ?? "https://mainnet.starknet.a5a.ch";
22
18
  export const MAX_RETRIES = Number(process.env.MAX_RETRIES ?? 3);
23
19
  export const MAX_DELAY = Number(process.env.MAX_DELAY ?? 100);
24
20
  export const EXTEND_MARKET_NAME = "BTC-USD";
25
- export const LIMIT_BALANCE = Number(process.env.LIMIT_BALANCE ?? 10);
21
+ export const LIMIT_BALANCE = Number(process.env.LIMIT_BALANCE ?? 0.05);
22
+ export const LIMIT_BALANCE_VALUE=10;
26
23
  export const REBALANCER_INTERVAL = Number(process.env.REBALANCER_INTERVAL ?? 180000); //3 mins
27
24
  export const WITHDRAWAL_INTERVAL = Number(process.env.WITHDRAWAL_INTERVAL ?? 18000000); //5 hours
28
25
  export const INVESTING_INTERVAL = Number(process.env.INVESTING_INTERVAL ?? 180000); //3 mins
@@ -66,12 +66,10 @@ export const calculateAmountDistribution = async (
66
66
  vesu_leverage: 0,
67
67
  };
68
68
  }
69
- const extendedBTCExposure =
69
+ const extendedExposureUSD =
70
70
  extendedPosition.length > 0
71
- ? new Web3Number(extendedPosition[0].size, WBTC_TOKEN_DECIMALS)
71
+ ? new Web3Number(extendedPosition[0].value, WBTC_TOKEN_DECIMALS)
72
72
  : new Web3Number(0, WBTC_TOKEN_DECIMALS);
73
- const extendedExposureUSD =
74
- extendedBTCExposure.multipliedBy(collateralPrice);
75
73
  const vesuBTCExposureUSD = collateralUnits.multipliedBy(collateralPrice);
76
74
  const numerator1 = vesu_leverage * amount + vesuBTCExposureUSD.toNumber();
77
75
  const numerator2 = extendedExposureUSD.toNumber();
@@ -131,9 +129,6 @@ export const calculateAmountDistributionForWithdrawal = async (
131
129
  extendedPosition.length > 0
132
130
  ? new Web3Number(extendedPosition[0].value, USDC_TOKEN_DECIMALS)
133
131
  : new Web3Number(0, USDC_TOKEN_DECIMALS);
134
- console.log("THe collateral is", collateralPrice, collateralUnits);
135
- // console.log("the extended exposure usd is", extendedExposureUSD.toNumber());
136
- //console.log("the collateral units are", collateralUnits.toNumber());\
137
132
  const vesuExposureUSD = collateralUnits.multipliedBy(collateralPrice);
138
133
  if (vesuExposureUSD.lessThan(0)) {
139
134
  return {
@@ -223,16 +218,16 @@ export const calculateDebtAmount = (
223
218
  collateralAmount: Web3Number,
224
219
  debtAmount: Web3Number,
225
220
  debtPrice: number,
226
- maxLtv: number = MAX_LTV_BTC_USDC,
221
+ maxLtv: number = MAX_LIQUIDATION_RATIO,
227
222
  addedAmount: Web3Number, // this is in btc
228
223
  collateralPrice: number,
229
224
  isDeposit: boolean
230
225
  ) => {
231
226
  try {
232
227
  // => X = (((collateral + legDepositAmount) * collateralPrice * ltv) - (debt * debtPrice * target hf)) / (target hf - ltv)
233
- const marginAmount = addedAmount.multipliedBy(isDeposit ? 1 : -1);
234
- const numerator1 = collateralAmount
235
- .plus(addedAmount)
228
+ const addedCollateral = addedAmount.multipliedBy(isDeposit ? 1 : -1);
229
+ const numerator1 = (collateralAmount
230
+ .plus(addedCollateral))
236
231
  .multipliedBy(collateralPrice)
237
232
  .multipliedBy(maxLtv);
238
233
  const numerator2 = debtAmount
@@ -308,21 +303,60 @@ export const calculateAmountDepositOnExtendedWhenIncurringLosses = async (
308
303
  const extendedHoldings = await client.getHoldings();
309
304
  const extended_leverage = calculateExtendedLevergae();
310
305
  const latestPosition = (await client.getPositions()).data.pop();
311
- console.log("the latest position is", latestPosition, extendedHoldings);
312
306
  if (!extendedHoldings || !latestPosition) {
313
307
  logger.error(`error getting extended position: extendedHoldings=${extendedHoldings}, latestPosition=${latestPosition}`);
314
308
  return null;
315
309
  }
316
- const positionValueInUSD = latestPosition.value;
310
+ const positionValueInUSD = new Web3Number(latestPosition.value, USDC_TOKEN_DECIMALS);
317
311
  const equity = extendedHoldings.data.equity;
318
- const deposit =
319
- Number(positionValueInUSD) / extended_leverage - Number(equity);
320
- return new Web3Number(Math.floor(deposit / 0.2) * 0.2, USDC_TOKEN_DECIMALS);
312
+ const deposit = positionValueInUSD.dividedBy(extended_leverage).minus(equity).toFixed(2);
313
+ return new Web3Number(deposit, USDC_TOKEN_DECIMALS);
321
314
  } catch (err) {
315
+ logger.error(`error calculating amount deposit on extended when incurring losses: ${err}`);
322
316
  return null;
323
317
  }
324
318
  };
325
319
 
320
+ /**
321
+ * calculate the amount of collateral to deposit to maintain the ltv
322
+ * The formula is:
323
+ * ((debt * debtPrice * targetHF) - (collateral * collateralPrice * ltv)) / ltv
324
+ * @param collateralAmount in collateral units
325
+ * @param debtAmount in debt units
326
+ * @param debtPrice in usd
327
+ * @param maxLtv
328
+ * @param collateralPrice in usd
329
+ * @param targetHF
330
+ * @returns deltaCollateralAmountUnits in collateral units
331
+ * null if there is an error
332
+ */
333
+ export const calculateWBTCAmountToMaintainLTV = (
334
+ collateralAmount: Web3Number,
335
+ debtAmount: Web3Number,
336
+ debtPrice: number,
337
+ maxLtv: number = MAX_LIQUIDATION_RATIO,
338
+ collateralPrice: number,
339
+ targetHF: number = TARGET_HF,
340
+ ) =>{
341
+ try {
342
+ const numerator1 = (collateralAmount)
343
+ .multipliedBy(collateralPrice)
344
+ .multipliedBy(maxLtv)
345
+ const numerator2 = debtAmount
346
+ .multipliedBy(debtPrice)
347
+ .multipliedBy(targetHF);
348
+ const denominator = maxLtv;
349
+ const collateralAmountToMaintainLTV = numerator2.minus(numerator1).dividedBy(denominator);
350
+ let deltaCollateralAmountUnits = new Web3Number(
351
+ collateralAmountToMaintainLTV.dividedBy(collateralPrice).toFixed(WBTC_TOKEN_DECIMALS),
352
+ WBTC_TOKEN_DECIMALS
353
+ );
354
+ return {deltaCollateralAmountUnits};
355
+ } catch (err) {
356
+ return { deltaCollateralAmountUnits: null };
357
+ }
358
+ }
359
+
326
360
  export const calculateExposureDelta = (
327
361
  exposure_extended: number,
328
362
  exposure_vesu: number