@haven-fi/solauto-sdk 1.0.25 → 1.0.27

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 (46) hide show
  1. package/dist/clients/solautoClient.d.ts +4 -4
  2. package/dist/clients/solautoClient.d.ts.map +1 -1
  3. package/dist/clients/solautoClient.js +25 -23
  4. package/dist/clients/solautoMarginfiClient.d.ts +1 -0
  5. package/dist/clients/solautoMarginfiClient.d.ts.map +1 -1
  6. package/dist/clients/solautoMarginfiClient.js +84 -23
  7. package/dist/generated/instructions/closePosition.d.ts +4 -4
  8. package/dist/generated/instructions/closePosition.d.ts.map +1 -1
  9. package/dist/generated/instructions/closePosition.js +8 -8
  10. package/dist/generated/instructions/marginfiProtocolInteraction.d.ts +2 -2
  11. package/dist/generated/instructions/marginfiProtocolInteraction.d.ts.map +1 -1
  12. package/dist/generated/instructions/marginfiProtocolInteraction.js +4 -4
  13. package/dist/generated/instructions/marginfiRebalance.d.ts +2 -0
  14. package/dist/generated/instructions/marginfiRebalance.d.ts.map +1 -1
  15. package/dist/generated/instructions/marginfiRebalance.js +17 -7
  16. package/dist/transactions/transactionUtils.d.ts.map +1 -1
  17. package/dist/transactions/transactionUtils.js +111 -21
  18. package/dist/transactions/transactionsManager.d.ts.map +1 -1
  19. package/dist/transactions/transactionsManager.js +6 -2
  20. package/dist/types/solauto.d.ts +13 -0
  21. package/dist/types/solauto.d.ts.map +1 -0
  22. package/dist/types/solauto.js +2 -0
  23. package/dist/utils/jupiterUtils.d.ts.map +1 -1
  24. package/dist/utils/jupiterUtils.js +1 -0
  25. package/dist/utils/marginfiUtils.d.ts +5 -1
  26. package/dist/utils/marginfiUtils.d.ts.map +1 -1
  27. package/dist/utils/marginfiUtils.js +8 -2
  28. package/dist/utils/solauto/generalUtils.d.ts +1 -5
  29. package/dist/utils/solauto/generalUtils.d.ts.map +1 -1
  30. package/dist/utils/solauto/generalUtils.js +6 -2
  31. package/dist/utils/solauto/rebalanceUtils.js +2 -1
  32. package/package.json +1 -1
  33. package/src/clients/solautoClient.ts +33 -31
  34. package/src/clients/solautoMarginfiClient.ts +89 -31
  35. package/src/generated/instructions/closePosition.ts +12 -12
  36. package/src/generated/instructions/marginfiProtocolInteraction.ts +6 -6
  37. package/src/generated/instructions/marginfiRebalance.ts +19 -7
  38. package/src/transactions/transactionUtils.ts +140 -36
  39. package/src/transactions/transactionsManager.ts +8 -6
  40. package/src/types/solauto.ts +14 -0
  41. package/src/utils/jupiterUtils.ts +1 -0
  42. package/src/utils/marginfiUtils.ts +11 -3
  43. package/src/utils/solauto/generalUtils.ts +21 -12
  44. package/src/utils/solauto/rebalanceUtils.ts +2 -2
  45. package/tests/transactions/solautoMarginfi.ts +44 -25
  46. package/tests/unit/lookupTables.ts +4 -3
@@ -11,9 +11,10 @@ import {
11
11
  transactionBuilder,
12
12
  createSignerFromKeypair,
13
13
  AccountMeta,
14
- createUmi,
15
14
  } from "@metaplex-foundation/umi";
15
+ import { createUmi } from "@metaplex-foundation/umi-bundle-defaults";
16
16
  import {
17
+ clusterApiUrl,
17
18
  Keypair,
18
19
  PublicKey,
19
20
  SYSVAR_INSTRUCTIONS_PUBKEY,
@@ -42,9 +43,11 @@ import {
42
43
  MARGINFI_PROGRAM_ID,
43
44
  MarginfiAccount,
44
45
  lendingAccountBorrow,
46
+ lendingAccountDeposit,
45
47
  lendingAccountEndFlashloan,
46
48
  lendingAccountRepay,
47
49
  lendingAccountStartFlashloan,
50
+ lendingAccountWithdraw,
48
51
  marginfiAccountInitialize,
49
52
  safeFetchAllMarginfiAccount,
50
53
  safeFetchMarginfiAccount,
@@ -73,16 +76,14 @@ export function newSolautoMarginfiPositionArgs(
73
76
  debtMint: PublicKey,
74
77
  referredByAuthority?: PublicKey
75
78
  ): SolautoMarginfiClientArgs {
79
+ const umi = createUmi(clusterApiUrl("mainnet-beta"));
76
80
  return {
77
81
  positionId,
78
82
  signer,
79
83
  wallet,
80
84
  marginfiAccount:
81
85
  positionId === 0
82
- ? createSignerFromKeypair(
83
- createUmi(),
84
- fromWeb3JsKeypair(Keypair.generate())
85
- )
86
+ ? createSignerFromKeypair(umi, umi.eddsa.generateKeypair())
86
87
  : undefined,
87
88
  marginfiAccountSeedIdx: positionId > 0 ? generateRandomU64() : undefined,
88
89
  supplyMint: supplyMint,
@@ -151,7 +152,7 @@ export class SolautoMarginfiClient extends SolautoClient {
151
152
  const emptyMarginfiAccounts = (
152
153
  await safeFetchAllMarginfiAccount(
153
154
  this.umi,
154
- existingMarginfiAccounts.map((x) => publicKey(x))
155
+ existingMarginfiAccounts.map((x) => publicKey(x.marginfiAccount))
155
156
  )
156
157
  ).filter(
157
158
  (x) =>
@@ -166,10 +167,7 @@ export class SolautoMarginfiClient extends SolautoClient {
166
167
  this.intermediaryMarginfiAccountSigner =
167
168
  emptyMarginfiAccounts.length > 0
168
169
  ? undefined
169
- : createSignerFromKeypair(
170
- this.umi,
171
- fromWeb3JsKeypair(Keypair.generate())
172
- );
170
+ : createSignerFromKeypair(this.umi, this.umi.eddsa.generateKeypair());
173
171
  this.intermediaryMarginfiAccountPk =
174
172
  emptyMarginfiAccounts.length > 0
175
173
  ? toWeb3JsPublicKey(emptyMarginfiAccounts[0].publicKey)
@@ -203,9 +201,9 @@ export class SolautoMarginfiClient extends SolautoClient {
203
201
  settingParams?: SolautoSettingsParametersInpArgs,
204
202
  dca?: DCASettingsInpArgs
205
203
  ): TransactionBuilder {
206
- let signerDebtLiquidityTa: UmiPublicKey | undefined = undefined;
204
+ let signerDebtTa: UmiPublicKey | undefined = undefined;
207
205
  if (dca) {
208
- signerDebtLiquidityTa = publicKey(this.signerDebtLiquidityTa);
206
+ signerDebtTa = publicKey(this.signerDebtTa);
209
207
  }
210
208
 
211
209
  return marginfiOpenPosition(this.umi, {
@@ -228,11 +226,11 @@ export class SolautoMarginfiClient extends SolautoClient {
228
226
  : (this.marginfiAccount as Signer),
229
227
  supplyMint: publicKey(this.supplyMint),
230
228
  supplyBank: publicKey(this.marginfiSupplyBankAccounts.bank),
231
- positionSupplyTa: publicKey(this.positionSupplyLiquidityTa),
229
+ positionSupplyTa: publicKey(this.positionSupplyTa),
232
230
  debtMint: publicKey(this.debtMint),
233
231
  debtBank: publicKey(this.marginfiDebtBankAccounts.bank),
234
- positionDebtTa: publicKey(this.positionDebtLiquidityTa),
235
- signerDebtTa: signerDebtLiquidityTa,
232
+ positionDebtTa: publicKey(this.positionDebtTa),
233
+ signerDebtTa: signerDebtTa,
236
234
  positionData: {
237
235
  positionId: this.positionId!,
238
236
  settingParams: settingParams ?? null,
@@ -257,22 +255,80 @@ export class SolautoMarginfiClient extends SolautoClient {
257
255
  }
258
256
 
259
257
  protocolInteraction(args: SolautoActionArgs): TransactionBuilder {
260
- return super
261
- .protocolInteraction(args)
262
- .add(this.marginfiProtocolInteractionIx(args));
258
+ let tx = super.protocolInteraction(args);
259
+
260
+ if (this.selfManaged) {
261
+ return tx.add(this.marginfiProtocolInteractionIx(args));
262
+ } else {
263
+ return tx.add(this.marginfiSolautoProtocolInteractionIx(args));
264
+ }
265
+ }
266
+
267
+ private marginfiProtocolInteractionIx(args: SolautoActionArgs) {
268
+ switch (args.__kind) {
269
+ case "Deposit": {
270
+ console.log(args.fields[0]);
271
+ return lendingAccountDeposit(this.umi, {
272
+ amount: args.fields[0],
273
+ signer: this.signer,
274
+ signerTokenAccount: publicKey(this.signerSupplyTa),
275
+ marginfiAccount: publicKey(this.marginfiAccountPk),
276
+ marginfiGroup: publicKey(this.marginfiGroup),
277
+ bank: publicKey(this.marginfiSupplyBankAccounts.bank),
278
+ bankLiquidityVault: publicKey(this.marginfiSupplyBankAccounts.liquidityVault),
279
+ });
280
+ }
281
+ case "Borrow": {
282
+ return lendingAccountBorrow(this.umi, {
283
+ amount: args.fields[0],
284
+ signer: this.signer,
285
+ destinationTokenAccount: publicKey(this.signerDebtTa),
286
+ marginfiAccount: publicKey(this.marginfiAccountPk),
287
+ marginfiGroup: publicKey(this.marginfiGroup),
288
+ bank: publicKey(this.marginfiDebtBankAccounts.bank),
289
+ bankLiquidityVault: publicKey(this.marginfiDebtBankAccounts.liquidityVault),
290
+ bankLiquidityVaultAuthority: publicKey(this.marginfiDebtBankAccounts.vaultAuthority),
291
+ });
292
+ }
293
+ case "Repay": {
294
+ return lendingAccountRepay(this.umi, {
295
+ amount: args.fields[0].__kind === "Some" ? args.fields[0].fields[0] : 0,
296
+ repayAll: args.fields[0].__kind === "All" ? true : false,
297
+ signer: this.signer,
298
+ signerTokenAccount: publicKey(this.signerDebtTa),
299
+ marginfiAccount: publicKey(this.marginfiAccountPk),
300
+ marginfiGroup: publicKey(this.marginfiGroup),
301
+ bank: publicKey(this.marginfiDebtBankAccounts.bank),
302
+ bankLiquidityVault: publicKey(this.marginfiDebtBankAccounts.liquidityVault),
303
+ });
304
+ }
305
+ case "Withdraw": {
306
+ return lendingAccountWithdraw(this.umi, {
307
+ amount: args.fields[0].__kind === "Some" ? args.fields[0].fields[0] : 0,
308
+ withdrawAll: args.fields[0].__kind === "All" ? true : false,
309
+ signer: this.signer,
310
+ destinationTokenAccount: publicKey(this.signerSupplyTa),
311
+ marginfiAccount: publicKey(this.marginfiAccountPk),
312
+ marginfiGroup: publicKey(this.marginfiGroup),
313
+ bank: publicKey(this.marginfiSupplyBankAccounts.bank),
314
+ bankLiquidityVault: publicKey(this.marginfiSupplyBankAccounts.liquidityVault),
315
+ bankLiquidityVaultAuthority: publicKey(this.marginfiSupplyBankAccounts.vaultAuthority),
316
+ });
317
+ }
318
+ }
263
319
  }
264
320
 
265
- private marginfiProtocolInteractionIx(
321
+ private marginfiSolautoProtocolInteractionIx(
266
322
  args: SolautoActionArgs
267
323
  ): TransactionBuilder {
268
- let sourceSupplyTa: UmiPublicKey | undefined = undefined;
324
+ let positionSupplyTa: UmiPublicKey | undefined = undefined;
269
325
  let vaultSupplyTa: UmiPublicKey | undefined = undefined;
270
326
  let supplyVaultAuthority: UmiPublicKey | undefined = undefined;
271
327
  if (args.__kind === "Deposit" || args.__kind === "Withdraw") {
272
- sourceSupplyTa = publicKey(
328
+ positionSupplyTa = publicKey(
273
329
  args.__kind === "Withdraw" || this.selfManaged
274
- ? this.signerSupplyLiquidityTa
275
- : this.positionSupplyLiquidityTa
330
+ ? this.signerSupplyTa
331
+ : this.positionSupplyTa
276
332
  );
277
333
  vaultSupplyTa = publicKey(this.marginfiSupplyBankAccounts.liquidityVault);
278
334
  supplyVaultAuthority = publicKey(
@@ -280,14 +336,14 @@ export class SolautoMarginfiClient extends SolautoClient {
280
336
  );
281
337
  }
282
338
 
283
- let sourceDebtTa: UmiPublicKey | undefined = undefined;
339
+ let positionDebtTa: UmiPublicKey | undefined = undefined;
284
340
  let vaultDebtTa: UmiPublicKey | undefined = undefined;
285
341
  let debtVaultAuthority: UmiPublicKey | undefined = undefined;
286
342
  if (args.__kind === "Borrow" || args.__kind === "Repay") {
287
- sourceDebtTa = publicKey(
343
+ positionDebtTa = publicKey(
288
344
  args.__kind === "Borrow" || this.selfManaged
289
- ? this.signerDebtLiquidityTa
290
- : this.positionDebtLiquidityTa
345
+ ? this.signerDebtTa
346
+ : this.positionDebtTa
291
347
  );
292
348
  vaultDebtTa = publicKey(this.marginfiDebtBankAccounts.liquidityVault);
293
349
  debtVaultAuthority = publicKey(
@@ -312,12 +368,12 @@ export class SolautoMarginfiClient extends SolautoClient {
312
368
  marginfiAccount: publicKey(this.marginfiAccountPk),
313
369
  supplyBank: publicKey(this.marginfiSupplyBankAccounts.bank),
314
370
  supplyPriceOracle,
315
- sourceSupplyTa,
371
+ positionSupplyTa,
316
372
  vaultSupplyTa,
317
373
  supplyVaultAuthority,
318
374
  debtBank: publicKey(this.marginfiDebtBankAccounts.bank),
319
375
  debtPriceOracle,
320
- sourceDebtTa,
376
+ positionDebtTa,
321
377
  vaultDebtTa,
322
378
  debtVaultAuthority,
323
379
  solautoAction: args,
@@ -356,14 +412,16 @@ export class SolautoMarginfiClient extends SolautoClient {
356
412
  ),
357
413
  supplyBank: publicKey(this.marginfiSupplyBankAccounts.bank),
358
414
  supplyPriceOracle: publicKey(this.marginfiSupplyBankAccounts.priceOracle),
359
- positionSupplyTa: publicKey(this.positionSupplyLiquidityTa),
415
+ positionSupplyTa: publicKey(this.positionSupplyTa),
416
+ signerSupplyTa: this.selfManaged ? publicKey(this.signerSupplyTa) : undefined,
360
417
  vaultSupplyTa: publicKey(this.marginfiSupplyBankAccounts.liquidityVault),
361
418
  supplyVaultAuthority: publicKey(
362
419
  this.marginfiSupplyBankAccounts.vaultAuthority
363
420
  ),
364
421
  debtBank: publicKey(this.marginfiDebtBankAccounts.bank),
365
422
  debtPriceOracle: publicKey(this.marginfiDebtBankAccounts.priceOracle),
366
- positionDebtTa: publicKey(this.positionDebtLiquidityTa),
423
+ positionDebtTa: publicKey(this.positionDebtTa),
424
+ signerDebtTa: this.selfManaged ? publicKey(this.signerDebtTa) : undefined,
367
425
  vaultDebtTa: publicKey(this.marginfiDebtBankAccounts.liquidityVault),
368
426
  debtVaultAuthority: publicKey(
369
427
  this.marginfiDebtBankAccounts.vaultAuthority
@@ -33,11 +33,11 @@ export type ClosePositionInstructionAccounts = {
33
33
  tokenProgram?: PublicKey | Pda;
34
34
  ataProgram?: PublicKey | Pda;
35
35
  solautoPosition: PublicKey | Pda;
36
- signerSupplyLiquidityTa: PublicKey | Pda;
37
- positionSupplyLiquidityTa: PublicKey | Pda;
36
+ signerSupplyTa: PublicKey | Pda;
37
+ positionSupplyTa: PublicKey | Pda;
38
38
  positionSupplyCollateralTa?: PublicKey | Pda;
39
- positionDebtLiquidityTa: PublicKey | Pda;
40
- signerDebtLiquidityTa: PublicKey | Pda;
39
+ positionDebtTa: PublicKey | Pda;
40
+ signerDebtTa: PublicKey | Pda;
41
41
  };
42
42
 
43
43
  // Data.
@@ -102,30 +102,30 @@ export function closePosition(
102
102
  isWritable: true as boolean,
103
103
  value: input.solautoPosition ?? null,
104
104
  },
105
- signerSupplyLiquidityTa: {
105
+ signerSupplyTa: {
106
106
  index: 5,
107
107
  isWritable: true as boolean,
108
- value: input.signerSupplyLiquidityTa ?? null,
108
+ value: input.signerSupplyTa ?? null,
109
109
  },
110
- positionSupplyLiquidityTa: {
110
+ positionSupplyTa: {
111
111
  index: 6,
112
112
  isWritable: true as boolean,
113
- value: input.positionSupplyLiquidityTa ?? null,
113
+ value: input.positionSupplyTa ?? null,
114
114
  },
115
115
  positionSupplyCollateralTa: {
116
116
  index: 7,
117
117
  isWritable: true as boolean,
118
118
  value: input.positionSupplyCollateralTa ?? null,
119
119
  },
120
- positionDebtLiquidityTa: {
120
+ positionDebtTa: {
121
121
  index: 8,
122
122
  isWritable: true as boolean,
123
- value: input.positionDebtLiquidityTa ?? null,
123
+ value: input.positionDebtTa ?? null,
124
124
  },
125
- signerDebtLiquidityTa: {
125
+ signerDebtTa: {
126
126
  index: 9,
127
127
  isWritable: true as boolean,
128
- value: input.signerDebtLiquidityTa ?? null,
128
+ value: input.signerDebtTa ?? null,
129
129
  },
130
130
  } satisfies ResolvedAccountsWithIndices;
131
131
 
@@ -45,12 +45,12 @@ export type MarginfiProtocolInteractionInstructionAccounts = {
45
45
  marginfiAccount: PublicKey | Pda;
46
46
  supplyBank: PublicKey | Pda;
47
47
  supplyPriceOracle?: PublicKey | Pda;
48
- sourceSupplyTa?: PublicKey | Pda;
48
+ positionSupplyTa?: PublicKey | Pda;
49
49
  vaultSupplyTa?: PublicKey | Pda;
50
50
  supplyVaultAuthority?: PublicKey | Pda;
51
51
  debtBank: PublicKey | Pda;
52
52
  debtPriceOracle?: PublicKey | Pda;
53
- sourceDebtTa?: PublicKey | Pda;
53
+ positionDebtTa?: PublicKey | Pda;
54
54
  vaultDebtTa?: PublicKey | Pda;
55
55
  debtVaultAuthority?: PublicKey | Pda;
56
56
  };
@@ -157,10 +157,10 @@ export function marginfiProtocolInteraction(
157
157
  isWritable: false as boolean,
158
158
  value: input.supplyPriceOracle ?? null,
159
159
  },
160
- sourceSupplyTa: {
160
+ positionSupplyTa: {
161
161
  index: 11,
162
162
  isWritable: true as boolean,
163
- value: input.sourceSupplyTa ?? null,
163
+ value: input.positionSupplyTa ?? null,
164
164
  },
165
165
  vaultSupplyTa: {
166
166
  index: 12,
@@ -182,10 +182,10 @@ export function marginfiProtocolInteraction(
182
182
  isWritable: false as boolean,
183
183
  value: input.debtPriceOracle ?? null,
184
184
  },
185
- sourceDebtTa: {
185
+ positionDebtTa: {
186
186
  index: 16,
187
187
  isWritable: true as boolean,
188
- value: input.sourceDebtTa ?? null,
188
+ value: input.positionDebtTa ?? null,
189
189
  },
190
190
  vaultDebtTa: {
191
191
  index: 17,
@@ -52,11 +52,13 @@ export type MarginfiRebalanceInstructionAccounts = {
52
52
  supplyBank: PublicKey | Pda;
53
53
  supplyPriceOracle?: PublicKey | Pda;
54
54
  positionSupplyTa: PublicKey | Pda;
55
+ signerSupplyTa?: PublicKey | Pda;
55
56
  vaultSupplyTa?: PublicKey | Pda;
56
57
  supplyVaultAuthority?: PublicKey | Pda;
57
58
  debtBank: PublicKey | Pda;
58
59
  debtPriceOracle?: PublicKey | Pda;
59
60
  positionDebtTa: PublicKey | Pda;
61
+ signerDebtTa?: PublicKey | Pda;
60
62
  vaultDebtTa?: PublicKey | Pda;
61
63
  debtVaultAuthority?: PublicKey | Pda;
62
64
  };
@@ -192,38 +194,48 @@ export function marginfiRebalance(
192
194
  isWritable: true as boolean,
193
195
  value: input.positionSupplyTa ?? null,
194
196
  },
195
- vaultSupplyTa: {
197
+ signerSupplyTa: {
196
198
  index: 15,
197
199
  isWritable: true as boolean,
200
+ value: input.signerSupplyTa ?? null,
201
+ },
202
+ vaultSupplyTa: {
203
+ index: 16,
204
+ isWritable: true as boolean,
198
205
  value: input.vaultSupplyTa ?? null,
199
206
  },
200
207
  supplyVaultAuthority: {
201
- index: 16,
208
+ index: 17,
202
209
  isWritable: true as boolean,
203
210
  value: input.supplyVaultAuthority ?? null,
204
211
  },
205
212
  debtBank: {
206
- index: 17,
213
+ index: 18,
207
214
  isWritable: true as boolean,
208
215
  value: input.debtBank ?? null,
209
216
  },
210
217
  debtPriceOracle: {
211
- index: 18,
218
+ index: 19,
212
219
  isWritable: false as boolean,
213
220
  value: input.debtPriceOracle ?? null,
214
221
  },
215
222
  positionDebtTa: {
216
- index: 19,
223
+ index: 20,
217
224
  isWritable: true as boolean,
218
225
  value: input.positionDebtTa ?? null,
219
226
  },
227
+ signerDebtTa: {
228
+ index: 21,
229
+ isWritable: true as boolean,
230
+ value: input.signerDebtTa ?? null,
231
+ },
220
232
  vaultDebtTa: {
221
- index: 20,
233
+ index: 22,
222
234
  isWritable: true as boolean,
223
235
  value: input.vaultDebtTa ?? null,
224
236
  },
225
237
  debtVaultAuthority: {
226
- index: 21,
238
+ index: 23,
227
239
  isWritable: true as boolean,
228
240
  value: input.debtVaultAuthority ?? null,
229
241
  },
@@ -40,11 +40,19 @@ import {
40
40
  rpcAccountCreated,
41
41
  } from "../utils/generalUtils";
42
42
  import { SolautoMarginfiClient } from "../clients/solautoMarginfiClient";
43
- import { getMaxLiqUtilizationRate, uint8ArrayToBigInt } from "../utils/numberUtils";
44
43
  import {
45
- eligibileForRebalance,
46
- } from "../utils/solauto/generalUtils";
44
+ getMaxLiqUtilizationRate,
45
+ uint8ArrayToBigInt,
46
+ } from "../utils/numberUtils";
47
+ import { eligibileForRebalance } from "../utils/solauto/generalUtils";
47
48
  import { getTokenAccount } from "../utils/accountUtils";
49
+ import {
50
+ getLendingAccountBorrowInstructionDataSerializer,
51
+ getLendingAccountDepositInstructionDataSerializer,
52
+ getLendingAccountRepayInstructionDataSerializer,
53
+ getLendingAccountWithdrawInstructionDataSerializer,
54
+ MARGINFI_PROGRAM_ID,
55
+ } from "../marginfi-sdk";
48
56
 
49
57
  interface wSolTokenUsage {
50
58
  wSolTokenAccount: PublicKey;
@@ -76,12 +84,12 @@ function getWSolUsage(
76
84
  );
77
85
  if (supplyIsWsol && usingSupplyTaAction) {
78
86
  return {
79
- wSolTokenAccount: client.signerSupplyLiquidityTa,
87
+ wSolTokenAccount: client.signerSupplyTa,
80
88
  solautoAction: usingSupplyTaAction,
81
89
  };
82
90
  } else if (debtIsWsol && usingDebtTaAction) {
83
91
  return {
84
- wSolTokenAccount: client.signerDebtLiquidityTa,
92
+ wSolTokenAccount: client.signerDebtTa,
85
93
  solautoAction: usingDebtTaAction,
86
94
  };
87
95
  } else {
@@ -120,7 +128,7 @@ async function transactionChoresBefore(
120
128
  undefined
121
129
  );
122
130
  if (wSolUsage !== undefined) {
123
- if (!getSolanaAccountCreated(client.umi, wSolUsage.wSolTokenAccount)) {
131
+ if (await getSolanaAccountCreated(client.umi, wSolUsage.wSolTokenAccount)) {
124
132
  client.log(`Closing signer wSol TA`);
125
133
  chores = chores.add(
126
134
  closeTokenAccountUmiIx(
@@ -133,17 +141,17 @@ async function transactionChoresBefore(
133
141
 
134
142
  let amountToTransfer = BigInt(0);
135
143
  if (isSolautoAction("Deposit", wSolUsage.solautoAction)) {
136
- amountToTransfer += BigInt(wSolUsage.solautoAction.fields[0]);
144
+ amountToTransfer = BigInt(wSolUsage.solautoAction.fields[0]);
137
145
  } else if (
138
146
  isSolautoAction("Repay", wSolUsage.solautoAction) &&
139
147
  wSolUsage.solautoAction.fields[0].__kind === "Some"
140
148
  ) {
141
- amountToTransfer += BigInt(wSolUsage.solautoAction.fields[0].fields[0]);
149
+ amountToTransfer = BigInt(wSolUsage.solautoAction.fields[0].fields[0]);
142
150
  } else if (
143
151
  initiatingDcaIn &&
144
152
  client.debtMint.toString() === NATIVE_MINT.toString()
145
153
  ) {
146
- amountToTransfer += initiatingDcaIn;
154
+ amountToTransfer = initiatingDcaIn;
147
155
  }
148
156
 
149
157
  if (amountToTransfer > 0) {
@@ -176,8 +184,8 @@ async function transactionChoresBefore(
176
184
  }
177
185
 
178
186
  const tokenAccount = isSolautoAction("Withdraw", solautoAction)
179
- ? client.signerSupplyLiquidityTa
180
- : client.signerDebtLiquidityTa;
187
+ ? client.signerSupplyTa
188
+ : client.signerDebtTa;
181
189
  if (accountsGettingCreated.includes(tokenAccount.toString())) {
182
190
  continue;
183
191
  }
@@ -235,10 +243,7 @@ export async function rebalanceChoresBefore(
235
243
  let chores = transactionBuilder();
236
244
 
237
245
  if (checkReferralSupplyTa && !rpcAccountCreated(referredBySupplyTa)) {
238
- client.log(
239
- "Creating referred-by TA for ",
240
- client.supplyMint.toString()
241
- );
246
+ client.log("Creating referred-by TA for ", client.supplyMint.toString());
242
247
  chores = chores.add(
243
248
  createAssociatedTokenAccountUmiIx(
244
249
  client.signer,
@@ -249,10 +254,7 @@ export async function rebalanceChoresBefore(
249
254
  }
250
255
 
251
256
  if (checkSolautoFeesTa && !rpcAccountCreated(solautoFeesSupplyTa)) {
252
- client.log(
253
- "Creating Solauto fees TA for ",
254
- client.supplyMint.toString()
255
- );
257
+ client.log("Creating Solauto fees TA for ", client.supplyMint.toString());
256
258
  chores = chores.add(
257
259
  createAssociatedTokenAccountUmiIx(
258
260
  client.signer,
@@ -304,25 +306,127 @@ function getSolautoActions(tx: TransactionBuilder): SolautoAction[] {
304
306
  let solautoActions: SolautoAction[] = [];
305
307
 
306
308
  tx.getInstructions().forEach((x) => {
307
- if (x.programId !== SOLAUTO_PROGRAM_ID) {
308
- return;
309
- }
310
-
311
- try {
312
- const serializer =
313
- getMarginfiProtocolInteractionInstructionDataSerializer();
314
- const discriminator = uint8ArrayToBigInt(
315
- serializer
309
+ if (x.programId === SOLAUTO_PROGRAM_ID) {
310
+ try {
311
+ const serializer =
312
+ getMarginfiProtocolInteractionInstructionDataSerializer();
313
+ const discriminator = serializer
316
314
  .serialize({
317
315
  solautoAction: solautoAction("Deposit", [BigInt(0)]),
318
316
  })
319
- .slice(0, 8)
320
- );
321
- const [data, _] = serializer.deserialize(x.data);
322
- if (BigInt(data.discriminator) === discriminator) {
323
- solautoActions?.push(data.solautoAction);
324
- }
325
- } catch {}
317
+ .slice(0, 1)[0];
318
+ const [data, _] = serializer.deserialize(x.data);
319
+ if (data.discriminator === discriminator) {
320
+ solautoActions?.push(data.solautoAction);
321
+ }
322
+ } catch {}
323
+ }
324
+
325
+ if (x.programId === MARGINFI_PROGRAM_ID) {
326
+ try {
327
+ const serializer = getLendingAccountDepositInstructionDataSerializer();
328
+ const discriminator = uint8ArrayToBigInt(
329
+ serializer
330
+ .serialize({
331
+ amount: 0,
332
+ })
333
+ .slice(0, 8)
334
+ );
335
+ const [data, _] = serializer.deserialize(x.data);
336
+ if (
337
+ uint8ArrayToBigInt(new Uint8Array(data.discriminator)) ===
338
+ discriminator
339
+ ) {
340
+ solautoActions?.push({
341
+ __kind: "Deposit",
342
+ fields: [data.amount],
343
+ });
344
+ }
345
+ } catch {}
346
+
347
+ try {
348
+ const serializer = getLendingAccountBorrowInstructionDataSerializer();
349
+ const discriminator = uint8ArrayToBigInt(
350
+ serializer
351
+ .serialize({
352
+ amount: 0,
353
+ })
354
+ .slice(0, 8)
355
+ );
356
+ const [data, _] = serializer.deserialize(x.data);
357
+ if (
358
+ uint8ArrayToBigInt(new Uint8Array(data.discriminator)) ===
359
+ discriminator
360
+ ) {
361
+ solautoActions?.push({
362
+ __kind: "Borrow",
363
+ fields: [data.amount],
364
+ });
365
+ }
366
+ } catch {}
367
+
368
+ try {
369
+ const serializer = getLendingAccountWithdrawInstructionDataSerializer();
370
+ const discriminator = uint8ArrayToBigInt(
371
+ serializer
372
+ .serialize({
373
+ amount: 0,
374
+ withdrawAll: false,
375
+ })
376
+ .slice(0, 8)
377
+ );
378
+ const [data, _] = serializer.deserialize(x.data);
379
+ if (
380
+ uint8ArrayToBigInt(new Uint8Array(data.discriminator)) ===
381
+ discriminator
382
+ ) {
383
+ solautoActions?.push({
384
+ __kind: "Withdraw",
385
+ fields: [
386
+ data.withdrawAll
387
+ ? {
388
+ __kind: "All",
389
+ }
390
+ : {
391
+ __kind: "Some",
392
+ fields: [data.amount],
393
+ },
394
+ ],
395
+ });
396
+ }
397
+ } catch {}
398
+
399
+ try {
400
+ const serializer = getLendingAccountRepayInstructionDataSerializer();
401
+ const discriminator = uint8ArrayToBigInt(
402
+ serializer
403
+ .serialize({
404
+ amount: 0,
405
+ repayAll: false,
406
+ })
407
+ .slice(0, 8)
408
+ );
409
+ const [data, _] = serializer.deserialize(x.data);
410
+ if (
411
+ uint8ArrayToBigInt(new Uint8Array(data.discriminator)) ===
412
+ discriminator
413
+ ) {
414
+ solautoActions?.push({
415
+ __kind: "Repay",
416
+ fields: [
417
+ data.repayAll
418
+ ? {
419
+ __kind: "All",
420
+ }
421
+ : {
422
+ __kind: "Some",
423
+ fields: [data.amount],
424
+ },
425
+ ],
426
+ });
427
+ }
428
+ } catch {}
429
+ }
326
430
 
327
431
  // TODO support other platforms
328
432
  });
@@ -483,7 +587,7 @@ export async function buildSolautoRebalanceTransaction(
483
587
  export async function convertReferralFeesToDestination(
484
588
  umi: Umi,
485
589
  referralState: ReferralState,
486
- tokenAccount: SplTokenAccount,
590
+ tokenAccount: SplTokenAccount
487
591
  ): Promise<[TransactionBuilder, string[]]> {
488
592
  const { lookupTableAddresses, setupInstructions, swapIx } =
489
593
  await getJupSwapTransaction(umi.identity, {
@@ -282,16 +282,18 @@ export class TransactionsManager {
282
282
  choresBefore.prepend(updateLookupTable.updateLutTx);
283
283
  }
284
284
  if (choresBefore.getInstructions().length > 0) {
285
- this.items.unshift(
286
- new TransactionItem(
287
- async () => ({ tx: choresBefore }),
288
- "create account(s)"
289
- )
285
+ const chore = new TransactionItem(
286
+ async () => ({ tx: choresBefore }),
287
+ "create account(s)"
290
288
  );
289
+ await chore.initialize();
290
+ this.items.unshift(chore);
291
291
  this.client.log("Chores before: ", choresBefore.getInstructions().length);
292
292
  }
293
293
  if (choresAfter.getInstructions().length > 0) {
294
- this.items.push(new TransactionItem(async () => ({ tx: choresAfter })));
294
+ const chore = new TransactionItem(async () => ({ tx: choresAfter }));
295
+ await chore.initialize();
296
+ this.items.push(chore);
295
297
  this.client.log("Chores after: ", choresAfter.getInstructions().length);
296
298
  }
297
299
 
@@ -0,0 +1,14 @@
1
+ import { PublicKey } from "@solana/web3.js";
2
+ import { LendingPlatform } from "../generated";
3
+
4
+ export interface SolautoPositionDetails {
5
+ positionId: number;
6
+ selfManaged: boolean;
7
+ lendingPlatform: LendingPlatform;
8
+ }
9
+
10
+ export interface SelfManagedPositionDetails extends SolautoPositionDetails {
11
+ protocolAccount: PublicKey;
12
+ supplyMint: PublicKey;
13
+ debtMint: PublicKey;
14
+ }