@haven-fi/solauto-sdk 1.0.58 → 1.0.60

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,7 +4,6 @@ import { NATIVE_MINT } from "@solana/spl-token";
4
4
  import { assert } from "chai";
5
5
  import { SolautoMarginfiClient } from "../../src/clients/solautoMarginfiClient";
6
6
  import { setupTest } from "../shared";
7
- import { MARGINFI_ACCOUNTS } from "../../src/constants/marginfiAccounts";
8
7
  import { getRebalanceValues } from "../../src/utils/solauto/rebalanceUtils";
9
8
  import { publicKey } from "@metaplex-foundation/umi";
10
9
  import { SolautoClient } from "../../src/clients/solautoClient";
@@ -23,18 +22,21 @@ import {
23
22
  } from "../../src/utils/numberUtils";
24
23
  import { USD_DECIMALS } from "../../src/constants/generalAccounts";
25
24
  import {
25
+ createFakePositionState,
26
26
  eligibleForNextAutomationPeriod,
27
27
  getAdjustedSettingsFromAutomation,
28
28
  getSolautoFeesBps,
29
29
  getUpdatedValueFromAutomation,
30
+ positionStateWithPrices,
30
31
  } from "../../src/utils/solauto/generalUtils";
31
32
  import {
32
33
  currentUnixSeconds,
33
34
  getTokenPrices,
34
35
  } from "../../src/utils/generalUtils";
35
36
  import { USDC_MINT } from "../../src/constants/tokenConstants";
37
+ import { PRICES } from "../../src/constants";
36
38
 
37
- const signer = setupTest();
39
+ const signer = setupTest(undefined, true);
38
40
 
39
41
  function assertAccurateRebalance(
40
42
  client: SolautoClient,
@@ -43,7 +45,16 @@ function assertAccurateRebalance(
43
45
  expectedUsdToDcaIn?: number
44
46
  ) {
45
47
  const { increasingLeverage, debtAdjustmentUsd, amountUsdToDcaIn } =
46
- getRebalanceValues(client, targetLiqUtilizationRateBps);
48
+ getRebalanceValues(
49
+ client.solautoPositionState!,
50
+ client.solautoPositionSettings(),
51
+ client.solautoPositionActiveDca(),
52
+ client.solautoPositionData?.feeType ?? FeeType.Small,
53
+ currentUnixSeconds(),
54
+ PRICES[client.supplyMint.toString()].price,
55
+ PRICES[client.debtMint.toString()].price,
56
+ targetLiqUtilizationRateBps
57
+ );
47
58
 
48
59
  let adjustmentFeeBps = 0;
49
60
  if (increasingLeverage) {
@@ -55,7 +66,9 @@ function assertAccurateRebalance(
55
66
 
56
67
  assert(
57
68
  Math.round(amountUsdToDcaIn) === Math.round(expectedUsdToDcaIn ?? 0),
58
- `Expected DCA-in amount does not match ${Math.round(amountUsdToDcaIn)}, ${Math.round(expectedUsdToDcaIn ?? 0)}`
69
+ `Expected DCA-in amount does not match ${Math.round(
70
+ amountUsdToDcaIn
71
+ )}, ${Math.round(expectedUsdToDcaIn ?? 0)}`
59
72
  );
60
73
 
61
74
  const newSupply =
@@ -77,8 +90,9 @@ function assertAccurateRebalance(
77
90
  client.solautoPositionState!.liqThresholdBps
78
91
  );
79
92
  assert(
80
- Math.round(newLiqUtilizationRateBps) === expectedLiqUtilizationRateBps,
81
- `Expected liq utilization rate does not match ${Math.round(newLiqUtilizationRateBps)}, ${expectedLiqUtilizationRateBps}`
93
+ Math.round(newLiqUtilizationRateBps) ===
94
+ Math.round(expectedLiqUtilizationRateBps),
95
+ `Expected liq utilization rate does not match ${newLiqUtilizationRateBps}, ${expectedLiqUtilizationRateBps}`
82
96
  );
83
97
  }
84
98
 
@@ -97,10 +111,37 @@ async function getFakePosition(
97
111
  positionId: 1,
98
112
  signer,
99
113
  supplyMint: new PublicKey(NATIVE_MINT),
100
- debtMint: new PublicKey(MARGINFI_ACCOUNTS.USDC.mint),
114
+ debtMint: new PublicKey(USDC_MINT),
115
+ });
116
+
117
+ const supplyUsd = 1000;
118
+ const maxLtvBps = 6400;
119
+ const liqThresholdBps = 8181;
120
+ client.solautoPositionState = await positionStateWithPrices({
121
+ state: createFakePositionState(
122
+ {
123
+ amountUsedBaseUnit: toBaseUnit(supplyUsd / supplyPrice, 9),
124
+ decimals: 9,
125
+ price: PRICES[NATIVE_MINT.toString()].price,
126
+ mint: NATIVE_MINT,
127
+ },
128
+ {
129
+ amountUsedBaseUnit: toBaseUnit(
130
+ (supplyUsd *
131
+ fromBps(liqThresholdBps) *
132
+ fromBps(fakeLiqUtilizationRateBps)) /
133
+ debtPrice,
134
+ 6
135
+ ),
136
+ decimals: 6,
137
+ price: 1,
138
+ mint: new PublicKey(USDC_MINT),
139
+ },
140
+ maxLtvBps,
141
+ liqThresholdBps
142
+ ),
101
143
  });
102
144
 
103
- const state = await client.getFreshPositionState();
104
145
  client.solautoPositionData = {
105
146
  positionId: [1],
106
147
  bump: [0],
@@ -129,7 +170,7 @@ async function getFakePosition(
129
170
  padding1: [],
130
171
  padding: [],
131
172
  },
132
- state: state!,
173
+ state: client.solautoPositionState!,
133
174
  rebalance: {
134
175
  rebalanceType: SolautoRebalanceType.Regular,
135
176
  targetLiqUtilizationRateBps: 0,
@@ -139,7 +180,7 @@ async function getFakePosition(
139
180
  padding2: [],
140
181
  padding: new Uint8Array([]),
141
182
  },
142
- feeType: FeeType.Small,
183
+ feeType: FeeType.Default,
143
184
  padding1: [],
144
185
  padding2: [],
145
186
  padding: [],
@@ -155,25 +196,14 @@ async function getFakePosition(
155
196
  },
156
197
  };
157
198
 
158
- const supplyUsd = 1000;
159
- client.livePositionUpdates.new({
160
- type: "supply",
161
- value: toBaseUnit(supplyUsd / supplyPrice, state!.supply.decimals),
162
- });
163
- client.livePositionUpdates.new({
164
- type: "debt",
165
- value: toBaseUnit(
166
- (supplyUsd *
167
- fromBps(state!.liqThresholdBps) *
168
- fromBps(fakeLiqUtilizationRateBps)) /
169
- debtPrice,
170
- state!.debt.decimals
171
- ),
172
- });
173
-
174
- client.solautoPositionState = await client.getFreshPositionState();
175
199
  client.solautoPositionState!.lastUpdated = BigInt(currentUnixSeconds());
176
200
 
201
+ assert(
202
+ fakeLiqUtilizationRateBps ===
203
+ client.solautoPositionState!.liqUtilizationRateBps,
204
+ "Fake position not set up correctly"
205
+ );
206
+
177
207
  return client;
178
208
  }
179
209
 
@@ -227,7 +257,8 @@ async function dcaRebalanceFromFakePosition(
227
257
  : adjustedSettings.boostToBps;
228
258
 
229
259
  const expectedDcaInAmount =
230
- dca.debtToAddBaseUnit > 0 && eligibleForNextAutomationPeriod(dca.automation)
260
+ dca.debtToAddBaseUnit > 0 &&
261
+ eligibleForNextAutomationPeriod(dca.automation, currentUnixSeconds())
231
262
  ? dca.debtToAddBaseUnit -
232
263
  BigInt(
233
264
  Math.round(
@@ -265,23 +296,23 @@ describe("Rebalance tests", async () => {
265
296
  });
266
297
 
267
298
  it("Standard rebalance with target rate", async () => {
268
- const client = new SolautoMarginfiClient(
269
- process.env.HELIUS_API_KEY ?? "",
270
- true
271
- );
272
- await client.initialize({
273
- positionId: 1,
274
- signer,
275
- supplyMint: new PublicKey(NATIVE_MINT),
276
- debtMint: new PublicKey(MARGINFI_ACCOUNTS.USDC.mint),
277
- });
278
-
279
- client.livePositionUpdates.new({
280
- type: "supply",
281
- value: BigInt(10000000000),
299
+ const client = await getFakePosition(supplyPrice, debtPrice, 3450, {
300
+ boostToBps: 500,
301
+ boostGap: 100,
302
+ repayToBps: 7000,
303
+ repayGap: 250,
304
+ automation: {
305
+ targetPeriods: 0,
306
+ periodsPassed: 0,
307
+ unixStartDate: BigInt(0),
308
+ intervalSeconds: BigInt(0),
309
+ padding1: [],
310
+ padding: new Uint8Array([]),
311
+ },
312
+ targetBoostToBps: 0,
313
+ padding1: [],
314
+ padding: new Uint8Array([]),
282
315
  });
283
- client.solautoPositionState = await client.getFreshPositionState();
284
- client.solautoPositionState!.lastUpdated = BigInt(currentUnixSeconds());
285
316
 
286
317
  assertAccurateRebalance(client, 5000, 5000);
287
318
  assertAccurateRebalance(client, 1000, 1000);
@@ -393,14 +424,14 @@ describe("Rebalance tests", async () => {
393
424
  it("Rebalance DCA in", async () => {
394
425
  const settings: SolautoSettingsParameters = {
395
426
  automation: {
396
- targetPeriods: 4,
427
+ targetPeriods: 3,
397
428
  periodsPassed: 0,
398
429
  intervalSeconds: BigInt(5),
399
430
  unixStartDate: BigInt(currentUnixSeconds()),
400
431
  padding1: [],
401
432
  padding: new Uint8Array([]),
402
433
  },
403
- targetBoostToBps: 0,
434
+ targetBoostToBps: 5000,
404
435
  boostGap: 1000,
405
436
  boostToBps: 4000,
406
437
  repayGap: 1000,