@haven-fi/solauto-sdk 1.0.133 → 1.0.135

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. package/dist/clients/solautoClient.d.ts.map +1 -1
  2. package/dist/clients/solautoClient.js +49 -39
  3. package/dist/generated/accounts/solautoPosition.d.ts +1 -7
  4. package/dist/generated/accounts/solautoPosition.d.ts.map +1 -1
  5. package/dist/generated/accounts/solautoPosition.js +2 -6
  6. package/dist/generated/instructions/cancelDCA.d.ts +3 -3
  7. package/dist/generated/instructions/cancelDCA.d.ts.map +1 -1
  8. package/dist/generated/instructions/cancelDCA.js +6 -6
  9. package/dist/generated/instructions/closePosition.d.ts +1 -2
  10. package/dist/generated/instructions/closePosition.d.ts.map +1 -1
  11. package/dist/generated/instructions/closePosition.js +6 -11
  12. package/dist/generated/instructions/updatePosition.d.ts +3 -3
  13. package/dist/generated/instructions/updatePosition.d.ts.map +1 -1
  14. package/dist/generated/instructions/updatePosition.js +6 -6
  15. package/dist/generated/types/dCASettings.d.ts +7 -5
  16. package/dist/generated/types/dCASettings.d.ts.map +1 -1
  17. package/dist/generated/types/dCASettings.js +3 -2
  18. package/dist/generated/types/dCASettingsInp.d.ts +5 -3
  19. package/dist/generated/types/dCASettingsInp.d.ts.map +1 -1
  20. package/dist/generated/types/dCASettingsInp.js +2 -1
  21. package/dist/generated/types/index.d.ts +1 -1
  22. package/dist/generated/types/index.d.ts.map +1 -1
  23. package/dist/generated/types/index.js +1 -1
  24. package/dist/generated/types/{feeType.d.ts → tokenType.d.ts} +6 -6
  25. package/dist/generated/types/tokenType.d.ts.map +1 -0
  26. package/dist/generated/types/tokenType.js +22 -0
  27. package/dist/transactions/transactionUtils.d.ts.map +1 -1
  28. package/dist/transactions/transactionUtils.js +15 -15
  29. package/dist/utils/marginfiUtils.d.ts.map +1 -1
  30. package/dist/utils/solauto/generalUtils.d.ts +15 -5
  31. package/dist/utils/solauto/generalUtils.d.ts.map +1 -1
  32. package/dist/utils/solauto/generalUtils.js +54 -37
  33. package/dist/utils/solauto/rebalanceUtils.d.ts +2 -1
  34. package/dist/utils/solauto/rebalanceUtils.d.ts.map +1 -1
  35. package/dist/utils/solauto/rebalanceUtils.js +7 -5
  36. package/package.json +1 -1
  37. package/src/clients/solautoClient.ts +50 -45
  38. package/src/generated/accounts/solautoPosition.ts +2 -15
  39. package/src/generated/instructions/cancelDCA.ts +9 -9
  40. package/src/generated/instructions/closePosition.ts +7 -13
  41. package/src/generated/instructions/updatePosition.ts +9 -9
  42. package/src/generated/types/dCASettings.ts +14 -7
  43. package/src/generated/types/dCASettingsInp.ts +9 -3
  44. package/src/generated/types/index.ts +1 -1
  45. package/src/generated/types/{feeType.ts → tokenType.ts} +8 -9
  46. package/src/transactions/transactionUtils.ts +35 -25
  47. package/src/utils/marginfiUtils.ts +2 -2
  48. package/src/utils/solauto/generalUtils.ts +75 -59
  49. package/src/utils/solauto/rebalanceUtils.ts +9 -8
  50. package/tests/unit/rebalanceCalculations.ts +14 -14
  51. package/dist/generated/types/feeType.d.ts.map +0 -1
  52. package/dist/generated/types/feeType.js +0 -20
@@ -8,26 +8,32 @@
8
8
 
9
9
  import {
10
10
  Serializer,
11
- bytes,
11
+ array,
12
12
  struct,
13
13
  u64,
14
+ u8,
14
15
  } from '@metaplex-foundation/umi/serializers';
15
16
  import {
16
17
  AutomationSettings,
17
18
  AutomationSettingsArgs,
19
+ TokenType,
20
+ TokenTypeArgs,
18
21
  getAutomationSettingsSerializer,
22
+ getTokenTypeSerializer,
19
23
  } from '.';
20
24
 
21
25
  export type DCASettings = {
22
26
  automation: AutomationSettings;
23
- debtToAddBaseUnit: bigint;
24
- padding: Uint8Array;
27
+ dcaInBaseUnit: bigint;
28
+ tokenType: TokenType;
29
+ padding: Array<number>;
25
30
  };
26
31
 
27
32
  export type DCASettingsArgs = {
28
33
  automation: AutomationSettingsArgs;
29
- debtToAddBaseUnit: number | bigint;
30
- padding: Uint8Array;
34
+ dcaInBaseUnit: number | bigint;
35
+ tokenType: TokenTypeArgs;
36
+ padding: Array<number>;
31
37
  };
32
38
 
33
39
  export function getDCASettingsSerializer(): Serializer<
@@ -37,8 +43,9 @@ export function getDCASettingsSerializer(): Serializer<
37
43
  return struct<DCASettings>(
38
44
  [
39
45
  ['automation', getAutomationSettingsSerializer()],
40
- ['debtToAddBaseUnit', u64()],
41
- ['padding', bytes({ size: 32 })],
46
+ ['dcaInBaseUnit', u64()],
47
+ ['tokenType', getTokenTypeSerializer()],
48
+ ['padding', array(u8(), { size: 31 })],
42
49
  ],
43
50
  { description: 'DCASettings' }
44
51
  ) as Serializer<DCASettingsArgs, DCASettings>;
@@ -10,17 +10,22 @@ import { Serializer, struct, u64 } from '@metaplex-foundation/umi/serializers';
10
10
  import {
11
11
  AutomationSettingsInp,
12
12
  AutomationSettingsInpArgs,
13
+ TokenType,
14
+ TokenTypeArgs,
13
15
  getAutomationSettingsInpSerializer,
16
+ getTokenTypeSerializer,
14
17
  } from '.';
15
18
 
16
19
  export type DCASettingsInp = {
17
20
  automation: AutomationSettingsInp;
18
- debtToAddBaseUnit: bigint;
21
+ dcaInBaseUnit: bigint;
22
+ tokenType: TokenType;
19
23
  };
20
24
 
21
25
  export type DCASettingsInpArgs = {
22
26
  automation: AutomationSettingsInpArgs;
23
- debtToAddBaseUnit: number | bigint;
27
+ dcaInBaseUnit: number | bigint;
28
+ tokenType: TokenTypeArgs;
24
29
  };
25
30
 
26
31
  export function getDCASettingsInpSerializer(): Serializer<
@@ -30,7 +35,8 @@ export function getDCASettingsInpSerializer(): Serializer<
30
35
  return struct<DCASettingsInp>(
31
36
  [
32
37
  ['automation', getAutomationSettingsInpSerializer()],
33
- ['debtToAddBaseUnit', u64()],
38
+ ['dcaInBaseUnit', u64()],
39
+ ['tokenType', getTokenTypeSerializer()],
34
40
  ],
35
41
  { description: 'DCASettingsInp' }
36
42
  ) as Serializer<DCASettingsInpArgs, DCASettingsInp>;
@@ -10,7 +10,6 @@ export * from './automationSettings';
10
10
  export * from './automationSettingsInp';
11
11
  export * from './dCASettings';
12
12
  export * from './dCASettingsInp';
13
- export * from './feeType';
14
13
  export * from './lendingPlatform';
15
14
  export * from './podBool';
16
15
  export * from './positionData';
@@ -23,4 +22,5 @@ export * from './solautoSettingsParameters';
23
22
  export * from './solautoSettingsParametersInp';
24
23
  export * from './tokenAmount';
25
24
  export * from './tokenBalanceAmount';
25
+ export * from './tokenType';
26
26
  export * from './updatePositionData';
@@ -8,16 +8,15 @@
8
8
 
9
9
  import { Serializer, scalarEnum } from '@metaplex-foundation/umi/serializers';
10
10
 
11
- export enum FeeType {
12
- Small,
13
- Default,
11
+ export enum TokenType {
12
+ Supply,
13
+ Debt,
14
14
  }
15
15
 
16
- export type FeeTypeArgs = FeeType;
16
+ export type TokenTypeArgs = TokenType;
17
17
 
18
- export function getFeeTypeSerializer(): Serializer<FeeTypeArgs, FeeType> {
19
- return scalarEnum<FeeType>(FeeType, { description: 'FeeType' }) as Serializer<
20
- FeeTypeArgs,
21
- FeeType
22
- >;
18
+ export function getTokenTypeSerializer(): Serializer<TokenTypeArgs, TokenType> {
19
+ return scalarEnum<TokenType>(TokenType, {
20
+ description: 'TokenType',
21
+ }) as Serializer<TokenTypeArgs, TokenType>;
23
22
  }
@@ -17,6 +17,7 @@ import {
17
17
  SOLAUTO_PROGRAM_ID,
18
18
  SolautoAction,
19
19
  SolautoRebalanceType,
20
+ TokenType,
20
21
  convertReferralFees,
21
22
  getMarginfiProtocolInteractionInstructionDataSerializer,
22
23
  getMarginfiRebalanceInstructionDataSerializer,
@@ -55,18 +56,20 @@ import {
55
56
  getLendingAccountWithdrawInstructionDataSerializer,
56
57
  MARGINFI_PROGRAM_ID,
57
58
  } from "../marginfi-sdk";
58
- import { PRICES } from "../constants";
59
59
 
60
60
  interface wSolTokenUsage {
61
61
  wSolTokenAccount: PublicKey;
62
- solautoAction: SolautoAction;
62
+ solautoAction?: SolautoAction;
63
63
  }
64
64
 
65
65
  function getWSolUsage(
66
66
  client: SolautoClient,
67
67
  solautoActions?: SolautoAction[],
68
- initiatingDcaIn?: bigint,
69
- cancellingDcaIn?: boolean
68
+ initiatingDcaIn?: {
69
+ amount: bigint;
70
+ tokenType: TokenType;
71
+ },
72
+ cancellingDcaIn?: TokenType
70
73
  ): wSolTokenUsage | undefined {
71
74
  const supplyIsWsol = client.supplyMint.equals(NATIVE_MINT);
72
75
  const debtIsWsol = client.debtMint.equals(NATIVE_MINT);
@@ -79,18 +82,23 @@ function getWSolUsage(
79
82
  isSolautoAction("Deposit", args) || isSolautoAction("Withdraw", args)
80
83
  );
81
84
  const usingDebtTaAction = solautoActions?.find(
82
- (args) =>
83
- isSolautoAction("Borrow", args) ||
84
- isSolautoAction("Repay", args) ||
85
- initiatingDcaIn ||
86
- cancellingDcaIn
85
+ (args) => isSolautoAction("Borrow", args) || isSolautoAction("Repay", args)
87
86
  );
88
- if (supplyIsWsol && usingSupplyTaAction) {
87
+
88
+ const dcaSupply =
89
+ (initiatingDcaIn && initiatingDcaIn.tokenType === TokenType.Supply) ||
90
+ (cancellingDcaIn !== undefined && cancellingDcaIn === TokenType.Supply);
91
+
92
+ const dcaDebt =
93
+ (initiatingDcaIn && initiatingDcaIn.tokenType === TokenType.Debt) ||
94
+ (cancellingDcaIn !== undefined && cancellingDcaIn === TokenType.Debt);
95
+
96
+ if (supplyIsWsol && (usingSupplyTaAction || dcaSupply)) {
89
97
  return {
90
98
  wSolTokenAccount: client.signerSupplyTa,
91
99
  solautoAction: usingSupplyTaAction,
92
100
  };
93
- } else if (debtIsWsol && usingDebtTaAction) {
101
+ } else if (debtIsWsol && (usingDebtTaAction || dcaDebt)) {
94
102
  return {
95
103
  wSolTokenAccount: client.signerDebtTa,
96
104
  solautoAction: usingDebtTaAction,
@@ -104,7 +112,10 @@ async function transactionChoresBefore(
104
112
  client: SolautoClient,
105
113
  accountsGettingCreated: string[],
106
114
  solautoActions?: SolautoAction[],
107
- initiatingDcaIn?: bigint
115
+ initiatingDcaIn?: {
116
+ amount: bigint;
117
+ tokenType: TokenType;
118
+ }
108
119
  ): Promise<TransactionBuilder> {
109
120
  let chores = transactionBuilder();
110
121
 
@@ -162,18 +173,19 @@ async function transactionChoresBefore(
162
173
  }
163
174
 
164
175
  let amountToTransfer = BigInt(0);
165
- if (isSolautoAction("Deposit", wSolUsage.solautoAction)) {
176
+ if (
177
+ wSolUsage.solautoAction &&
178
+ isSolautoAction("Deposit", wSolUsage.solautoAction)
179
+ ) {
166
180
  amountToTransfer = BigInt(wSolUsage.solautoAction.fields[0]);
167
181
  } else if (
182
+ wSolUsage.solautoAction &&
168
183
  isSolautoAction("Repay", wSolUsage.solautoAction) &&
169
184
  wSolUsage.solautoAction.fields[0].__kind === "Some"
170
185
  ) {
171
186
  amountToTransfer = BigInt(wSolUsage.solautoAction.fields[0].fields[0]);
172
- } else if (
173
- initiatingDcaIn &&
174
- client.debtMint.toString() === NATIVE_MINT.toString()
175
- ) {
176
- amountToTransfer = initiatingDcaIn;
187
+ } else if (initiatingDcaIn) {
188
+ amountToTransfer = initiatingDcaIn.amount;
177
189
  }
178
190
 
179
191
  if (amountToTransfer > 0) {
@@ -349,7 +361,7 @@ export async function rebalanceChoresBefore(
349
361
  function transactionChoresAfter(
350
362
  client: SolautoClient,
351
363
  solautoActions?: SolautoAction[],
352
- cancellingDcaIn?: boolean
364
+ cancellingDcaIn?: TokenType
353
365
  ): TransactionBuilder {
354
366
  let chores = transactionBuilder();
355
367
 
@@ -538,9 +550,7 @@ export async function getTransactionChores(
538
550
  client,
539
551
  accountsGettingCreated,
540
552
  solautoActions,
541
- client.livePositionUpdates.debtTaBalanceAdjustment > 0
542
- ? client.livePositionUpdates.debtTaBalanceAdjustment
543
- : undefined
553
+ client.livePositionUpdates.dcaInBalance
544
554
  ),
545
555
  await rebalanceChoresBefore(client, tx, accountsGettingCreated),
546
556
  ]);
@@ -549,7 +559,7 @@ export async function getTransactionChores(
549
559
  transactionChoresAfter(
550
560
  client,
551
561
  solautoActions,
552
- client.livePositionUpdates.debtTaBalanceAdjustment < 0
562
+ client.livePositionUpdates.cancellingDca
553
563
  )
554
564
  );
555
565
 
@@ -668,8 +678,8 @@ export async function buildSolautoRebalanceTransaction(
668
678
  "B",
669
679
  swapDetails,
670
680
  rebalanceType,
671
- jupQuote.slippageBps,
672
- undefined,
681
+ jupQuote.slippageBps,
682
+ undefined,
673
683
  targetLiqUtilizationRateBps
674
684
  ),
675
685
  ]);
@@ -1,5 +1,5 @@
1
1
  import { PublicKey } from "@solana/web3.js";
2
- import { publicKey, RpcAccount, Umi } from "@metaplex-foundation/umi";
2
+ import { publicKey, Umi } from "@metaplex-foundation/umi";
3
3
  import { toWeb3JsPublicKey } from "@metaplex-foundation/umi-web3js-adapters";
4
4
  import {
5
5
  Bank,
@@ -16,13 +16,13 @@ import {
16
16
  toBaseUnit,
17
17
  toBps,
18
18
  } from "./numberUtils";
19
- import { PRICES } from "../constants/solautoConstants";
20
19
  import { MARGINFI_ACCOUNTS } from "../constants/marginfiAccounts";
21
20
  import { MarginfiAssetAccounts } from "../types/accounts";
22
21
  import { PositionState, PositionTokenUsage } from "../generated";
23
22
  import { USD_DECIMALS } from "../constants/generalAccounts";
24
23
  import { LivePositionUpdates } from "./solauto/generalUtils";
25
24
  import { currentUnixSecondsSolana } from "./solanaUtils";
25
+ import { USDC_MINT } from "../constants";
26
26
 
27
27
  export function findMarginfiAccounts(bank: PublicKey): MarginfiAssetAccounts {
28
28
  for (const key in MARGINFI_ACCOUNTS) {
@@ -9,9 +9,11 @@ import {
9
9
  SOLAUTO_PROGRAM_ID,
10
10
  SolautoSettingsParameters,
11
11
  SolautoSettingsParametersInpArgs,
12
+ TokenType,
12
13
  getReferralStateSize,
13
14
  getSolautoPositionAccountDataSerializer,
14
15
  getSolautoPositionSize,
16
+ safeFetchAllSolautoPosition,
15
17
  } from "../../generated";
16
18
  import { currentUnixSeconds, fetchTokenPrices } from "../generalUtils";
17
19
  import {
@@ -33,7 +35,8 @@ export function findMintByTicker(ticker: string): PublicKey {
33
35
  for (const key in TOKEN_INFO) {
34
36
  const account = TOKEN_INFO[key];
35
37
  if (
36
- account.ticker.toString().toLowerCase() === ticker.toString().toLowerCase()
38
+ account.ticker.toString().toLowerCase() ===
39
+ ticker.toString().toLowerCase()
37
40
  ) {
38
41
  return new PublicKey(key);
39
42
  }
@@ -176,14 +179,18 @@ export async function getSolautoManagedPositions(
176
179
  // position_id: [u8; 1]
177
180
  // self_managed: u8 - (1 for true, 0 for false)
178
181
  // padding: [u8; 5]
179
- // authority: Pubkey
182
+ // authority: pubkey
180
183
  // lending_platform: u8
184
+ // padding: [u8; 7]
185
+ // protocol account: pubkey
186
+ // supply mint: pubkey
187
+ // debt mint: pubkey
181
188
 
182
189
  const accounts = await umi.rpc.getProgramAccounts(SOLAUTO_PROGRAM_ID, {
183
190
  commitment: "confirmed",
184
191
  dataSlice: {
185
192
  offset: 0,
186
- length: 1 + 1 + 1 + 5 + 32 + 1, // bump + position_id + self_managed + padding + authority (pubkey) + lending_platform
193
+ length: 1 + 1 + 1 + 5 + 32 + 1 + 7 + 32 + 32 + 32, // bump + position_id + self_managed + padding (5) + authority (pubkey) + lending_platform + padding (7) + protocol account (pubkey) + supply mint (pubkey) + debt mint (pubkey)
187
194
  },
188
195
  filters: [
189
196
  {
@@ -220,6 +227,9 @@ export async function getSolautoManagedPositions(
220
227
  authority: toWeb3JsPublicKey(position.authority),
221
228
  positionId: position.positionId[0],
222
229
  lendingPlatform: position.position.lendingPlatform,
230
+ protocolAccount: toWeb3JsPublicKey(position.position.protocolAccount),
231
+ supplyMint: toWeb3JsPublicKey(position.position.supplyMint),
232
+ debtMint: toWeb3JsPublicKey(position.position.debtMint),
223
233
  };
224
234
  });
225
235
  }
@@ -277,44 +287,44 @@ export async function getAllPositionsByAuthority(
277
287
  umi: Umi,
278
288
  user: PublicKey
279
289
  ): Promise<SolautoPositionDetails[]> {
280
- const allPositions: SolautoPositionDetails[] = [];
281
-
282
- const solautoManagedPositions = await getSolautoManagedPositions(umi, user);
283
- allPositions.push(
284
- ...solautoManagedPositions.map((x) => ({
285
- publicKey: x.publicKey,
286
- authority: user,
287
- positionId: x.positionId,
288
- lendingPlatform: x.lendingPlatform,
289
- }))
290
- );
291
-
292
- let marginfiPositions = await getAllMarginfiAccountsByAuthority(
293
- umi,
294
- user,
295
- true
296
- );
297
- marginfiPositions = marginfiPositions.filter(
298
- (x) =>
299
- x.supplyMint &&
300
- (x.debtMint!.equals(PublicKey.default) ||
301
- ALL_SUPPORTED_TOKENS.includes(x.debtMint!.toString()))
302
- );
303
- allPositions.push(
304
- ...marginfiPositions.map((x) => ({
305
- publicKey: x.marginfiAccount,
306
- authority: user,
307
- positionId: 0,
308
- lendingPlatform: LendingPlatform.Marginfi,
309
- protocolAccount: x.marginfiAccount,
310
- supplyMint: x.supplyMint,
311
- debtMint: x.debtMint,
312
- }))
313
- );
314
-
315
- // TODO support other platforms
290
+ const solautoCompatiblePositions: SolautoPositionDetails[][] =
291
+ await Promise.all([
292
+ (async () => {
293
+ const solautoManagedPositions = await getSolautoManagedPositions(
294
+ umi,
295
+ user
296
+ );
297
+ return solautoManagedPositions.map((x) => ({
298
+ ...x,
299
+ authority: user,
300
+ }));
301
+ })(),
302
+ (async () => {
303
+ let marginfiPositions = await getAllMarginfiAccountsByAuthority(
304
+ umi,
305
+ user,
306
+ true
307
+ );
308
+ marginfiPositions = marginfiPositions.filter(
309
+ (x) =>
310
+ x.supplyMint &&
311
+ (x.debtMint!.equals(PublicKey.default) ||
312
+ ALL_SUPPORTED_TOKENS.includes(x.debtMint!.toString()))
313
+ );
314
+ return marginfiPositions.map((x) => ({
315
+ publicKey: x.marginfiAccount,
316
+ authority: user,
317
+ positionId: 0,
318
+ lendingPlatform: LendingPlatform.Marginfi,
319
+ protocolAccount: x.marginfiAccount,
320
+ supplyMint: x.supplyMint,
321
+ debtMint: x.debtMint,
322
+ }));
323
+ })(),
324
+ // TODO support other platforms
325
+ ]);
316
326
 
317
- return allPositions;
327
+ return solautoCompatiblePositions.flat();
318
328
  }
319
329
 
320
330
  export async function positionStateWithLatestPrices(
@@ -441,15 +451,15 @@ export function createFakePositionState(
441
451
  };
442
452
  }
443
453
 
444
- export function createSolautoSettings(settings: SolautoSettingsParametersInpArgs): SolautoSettingsParameters {
454
+ export function createSolautoSettings(
455
+ settings: SolautoSettingsParametersInpArgs
456
+ ): SolautoSettingsParameters {
445
457
  return {
446
458
  automation:
447
459
  isOption(settings.automation) && isSome(settings.automation)
448
460
  ? {
449
461
  ...settings.automation.value,
450
- intervalSeconds: BigInt(
451
- settings.automation.value.intervalSeconds
452
- ),
462
+ intervalSeconds: BigInt(settings.automation.value.intervalSeconds),
453
463
  unixStartDate: BigInt(settings.automation.value.unixStartDate),
454
464
  padding: new Uint8Array([]),
455
465
  padding1: [],
@@ -463,8 +473,7 @@ export function createSolautoSettings(settings: SolautoSettingsParametersInpArgs
463
473
  padding1: [],
464
474
  },
465
475
  targetBoostToBps:
466
- isOption(settings.targetBoostToBps) &&
467
- isSome(settings.targetBoostToBps)
476
+ isOption(settings.targetBoostToBps) && isSome(settings.targetBoostToBps)
468
477
  ? settings.targetBoostToBps.value
469
478
  : 0,
470
479
  boostGap: settings.boostGap,
@@ -479,24 +488,24 @@ export function createSolautoSettings(settings: SolautoSettingsParametersInpArgs
479
488
  type PositionAdjustment =
480
489
  | { type: "supply"; value: bigint }
481
490
  | { type: "debt"; value: bigint }
482
- | { type: "debtDcaIn"; value: bigint }
483
491
  | { type: "settings"; value: SolautoSettingsParametersInpArgs }
484
- | { type: "dca"; value: DCASettingsInpArgs };
492
+ | { type: "dca"; value: DCASettingsInpArgs }
493
+ | { type: "dcaInBalance"; value: { amount: bigint; tokenType: TokenType; } }
494
+ | { type: "cancellingDca"; value: TokenType; };
485
495
 
486
496
  export class LivePositionUpdates {
487
- public supplyAdjustment: bigint = BigInt(0);
488
- public debtAdjustment: bigint = BigInt(0);
489
- public debtTaBalanceAdjustment: bigint = BigInt(0);
497
+ public supplyAdjustment = BigInt(0);
498
+ public debtAdjustment = BigInt(0);
490
499
  public settings: SolautoSettingsParameters | undefined = undefined;
491
500
  public activeDca: DCASettings | undefined = undefined;
492
-
501
+ public dcaInBalance?: { amount: bigint; tokenType: TokenType; } = undefined;
502
+ public cancellingDca: TokenType | undefined = undefined;
503
+
493
504
  new(update: PositionAdjustment) {
494
505
  if (update.type === "supply") {
495
506
  this.supplyAdjustment += update.value;
496
507
  } else if (update.type === "debt") {
497
508
  this.debtAdjustment += update.value;
498
- } else if (update.type === "debtDcaIn") {
499
- this.debtTaBalanceAdjustment += update.value;
500
509
  } else if (update.type === "settings") {
501
510
  const settings = update.value;
502
511
  this.settings = createSolautoSettings(settings);
@@ -510,26 +519,33 @@ export class LivePositionUpdates {
510
519
  padding: new Uint8Array([]),
511
520
  padding1: [],
512
521
  },
513
- debtToAddBaseUnit: BigInt(dca.debtToAddBaseUnit),
514
- padding: new Uint8Array([]),
522
+ dcaInBaseUnit: BigInt(dca.dcaInBaseUnit),
523
+ tokenType: dca.tokenType,
524
+ padding: [],
515
525
  };
526
+ } else if (update.type === "cancellingDca") {
527
+ this.cancellingDca = update.value;
528
+ } else if (update.type === "dcaInBalance") {
529
+ this.dcaInBalance = update.value;
516
530
  }
517
531
  }
518
532
 
519
533
  reset() {
520
534
  this.supplyAdjustment = BigInt(0);
521
535
  this.debtAdjustment = BigInt(0);
522
- this.debtTaBalanceAdjustment = BigInt(0);
523
536
  this.settings = undefined;
524
537
  this.activeDca = undefined;
538
+ this.dcaInBalance = undefined;
539
+ this.cancellingDca = undefined;
525
540
  }
526
541
 
527
542
  hasUpdates(): boolean {
528
543
  return (
529
544
  this.supplyAdjustment !== BigInt(0) ||
530
545
  this.debtAdjustment !== BigInt(0) ||
531
- this.debtTaBalanceAdjustment !== BigInt(0) ||
532
- this.settings !== undefined
546
+ this.dcaInBalance !== undefined ||
547
+ this.settings !== undefined ||
548
+ this.cancellingDca !== undefined
533
549
  );
534
550
  }
535
551
  }
@@ -5,6 +5,7 @@ import {
5
5
  PositionState,
6
6
  PositionTokenUsage,
7
7
  SolautoSettingsParameters,
8
+ TokenType,
8
9
  } from "../../generated";
9
10
  import {
10
11
  eligibleForNextAutomationPeriod,
@@ -27,16 +28,14 @@ import {
27
28
  import { USD_DECIMALS } from "../../constants/generalAccounts";
28
29
  import {
29
30
  DEFAULT_LIMIT_GAP_BPS,
30
- MIN_POSITION_STATE_FRESHNESS_SECS,
31
- PRICES,
32
31
  } from "../../constants/solautoConstants";
33
32
 
34
33
  function getAdditionalAmountToDcaIn(dca: DCASettings): number {
35
- if (dca.debtToAddBaseUnit === BigInt(0)) {
34
+ if (dca.dcaInBaseUnit === BigInt(0)) {
36
35
  return 0;
37
36
  }
38
37
 
39
- const debtBalance = Number(dca.debtToAddBaseUnit);
38
+ const debtBalance = Number(dca.dcaInBaseUnit);
40
39
  const updatedDebtBalance = getUpdatedValueFromAutomation(
41
40
  debtBalance,
42
41
  0,
@@ -80,7 +79,7 @@ function targetLiqUtilizationRateBpsFromDCA(
80
79
  );
81
80
 
82
81
  let targetRateBps = 0;
83
- if (dca.debtToAddBaseUnit > BigInt(0)) {
82
+ if (dca.dcaInBaseUnit > BigInt(0)) {
84
83
  targetRateBps = Math.max(
85
84
  state.liqUtilizationRateBps,
86
85
  adjustedSettings.boostToBps
@@ -126,7 +125,7 @@ function getTargetRateAndDcaAmount(
126
125
  dca: DCASettings | undefined,
127
126
  currentUnixTime: number,
128
127
  targetLiqUtilizationRateBps?: number
129
- ): { targetRateBps: number; amountToDcaIn?: number } {
128
+ ): { targetRateBps: number; amountToDcaIn?: number; } {
130
129
  if (targetLiqUtilizationRateBps !== undefined) {
131
130
  return {
132
131
  targetRateBps: targetLiqUtilizationRateBps,
@@ -164,6 +163,7 @@ export interface RebalanceValues {
164
163
  debtAdjustmentUsd: number;
165
164
  amountToDcaIn: number;
166
165
  amountUsdToDcaIn: number;
166
+ dcaTokenType?: TokenType;
167
167
  }
168
168
 
169
169
  export function getRebalanceValues(
@@ -238,6 +238,7 @@ export function getRebalanceValues(
238
238
  debtAdjustmentUsd,
239
239
  amountToDcaIn: amountToDcaIn ?? 0,
240
240
  amountUsdToDcaIn,
241
+ dcaTokenType: dca?.tokenType
241
242
  };
242
243
  }
243
244
 
@@ -254,7 +255,7 @@ export function getFlashLoanDetails(
254
255
  let supplyUsd = fromBaseUnit(
255
256
  client.solautoPositionState!.supply.amountUsed.baseAmountUsdValue,
256
257
  USD_DECIMALS
257
- );
258
+ ) + (values.dcaTokenType === TokenType.Supply ? values.amountUsdToDcaIn : 0);
258
259
  let debtUsd = fromBaseUnit(
259
260
  client.solautoPositionState!.debt.amountUsed.baseAmountUsdValue,
260
261
  USD_DECIMALS
@@ -333,7 +334,7 @@ export function getJupSwapRebalanceDetails(
333
334
  : client.solautoPositionState!.debt;
334
335
 
335
336
  const usdToSwap =
336
- Math.abs(values.debtAdjustmentUsd) + values.amountUsdToDcaIn;
337
+ Math.abs(values.debtAdjustmentUsd) + (values.dcaTokenType === TokenType.Debt ? values.amountUsdToDcaIn : 0);
337
338
 
338
339
  const inputPrice = values.increasingLeverage
339
340
  ? safeGetPrice(client.debtMint)