@haven-fi/solauto-sdk 1.0.24 → 1.0.26

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) 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 +83 -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.js +8 -8
  17. package/dist/transactions/transactionsManager.d.ts.map +1 -1
  18. package/dist/transactions/transactionsManager.js +6 -2
  19. package/dist/types/solauto.d.ts +13 -0
  20. package/dist/types/solauto.d.ts.map +1 -0
  21. package/dist/types/solauto.js +2 -0
  22. package/dist/utils/marginfiUtils.d.ts +5 -1
  23. package/dist/utils/marginfiUtils.d.ts.map +1 -1
  24. package/dist/utils/marginfiUtils.js +8 -2
  25. package/dist/utils/solauto/generalUtils.d.ts +1 -5
  26. package/dist/utils/solauto/generalUtils.d.ts.map +1 -1
  27. package/dist/utils/solauto/generalUtils.js +6 -2
  28. package/dist/utils/solauto/rebalanceUtils.d.ts.map +1 -1
  29. package/dist/utils/solauto/rebalanceUtils.js +2 -2
  30. package/package.json +1 -1
  31. package/src/clients/solautoClient.ts +33 -31
  32. package/src/clients/solautoMarginfiClient.ts +88 -31
  33. package/src/generated/instructions/closePosition.ts +12 -12
  34. package/src/generated/instructions/marginfiProtocolInteraction.ts +6 -6
  35. package/src/generated/instructions/marginfiRebalance.ts +19 -7
  36. package/src/transactions/transactionUtils.ts +8 -8
  37. package/src/transactions/transactionsManager.ts +8 -6
  38. package/src/types/solauto.ts +14 -0
  39. package/src/utils/marginfiUtils.ts +11 -3
  40. package/src/utils/solauto/generalUtils.ts +21 -12
  41. package/src/utils/solauto/rebalanceUtils.ts +2 -3
  42. package/tests/transactions/solautoMarginfi.ts +28 -25
  43. package/tests/unit/lookupTables.ts +4 -3
@@ -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
  },
@@ -76,12 +76,12 @@ function getWSolUsage(
76
76
  );
77
77
  if (supplyIsWsol && usingSupplyTaAction) {
78
78
  return {
79
- wSolTokenAccount: client.signerSupplyLiquidityTa,
79
+ wSolTokenAccount: client.signerSupplyTa,
80
80
  solautoAction: usingSupplyTaAction,
81
81
  };
82
82
  } else if (debtIsWsol && usingDebtTaAction) {
83
83
  return {
84
- wSolTokenAccount: client.signerDebtLiquidityTa,
84
+ wSolTokenAccount: client.signerDebtTa,
85
85
  solautoAction: usingDebtTaAction,
86
86
  };
87
87
  } else {
@@ -120,7 +120,7 @@ async function transactionChoresBefore(
120
120
  undefined
121
121
  );
122
122
  if (wSolUsage !== undefined) {
123
- if (!getSolanaAccountCreated(client.umi, wSolUsage.wSolTokenAccount)) {
123
+ if (await getSolanaAccountCreated(client.umi, wSolUsage.wSolTokenAccount)) {
124
124
  client.log(`Closing signer wSol TA`);
125
125
  chores = chores.add(
126
126
  closeTokenAccountUmiIx(
@@ -133,17 +133,17 @@ async function transactionChoresBefore(
133
133
 
134
134
  let amountToTransfer = BigInt(0);
135
135
  if (isSolautoAction("Deposit", wSolUsage.solautoAction)) {
136
- amountToTransfer += BigInt(wSolUsage.solautoAction.fields[0]);
136
+ amountToTransfer = BigInt(wSolUsage.solautoAction.fields[0]);
137
137
  } else if (
138
138
  isSolautoAction("Repay", wSolUsage.solautoAction) &&
139
139
  wSolUsage.solautoAction.fields[0].__kind === "Some"
140
140
  ) {
141
- amountToTransfer += BigInt(wSolUsage.solautoAction.fields[0].fields[0]);
141
+ amountToTransfer = BigInt(wSolUsage.solautoAction.fields[0].fields[0]);
142
142
  } else if (
143
143
  initiatingDcaIn &&
144
144
  client.debtMint.toString() === NATIVE_MINT.toString()
145
145
  ) {
146
- amountToTransfer += initiatingDcaIn;
146
+ amountToTransfer = initiatingDcaIn;
147
147
  }
148
148
 
149
149
  if (amountToTransfer > 0) {
@@ -176,8 +176,8 @@ async function transactionChoresBefore(
176
176
  }
177
177
 
178
178
  const tokenAccount = isSolautoAction("Withdraw", solautoAction)
179
- ? client.signerSupplyLiquidityTa
180
- : client.signerDebtLiquidityTa;
179
+ ? client.signerSupplyTa
180
+ : client.signerDebtTa;
181
181
  if (accountsGettingCreated.includes(tokenAccount.toString())) {
182
182
  continue;
183
183
  }
@@ -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
+ }
@@ -88,7 +88,9 @@ export async function getAllMarginfiAccountsByAuthority(
88
88
  umi: Umi,
89
89
  authority: PublicKey,
90
90
  compatibleWithSolauto?: boolean
91
- ): Promise<PublicKey[]> {
91
+ ): Promise<
92
+ { marginfiAccount: PublicKey; supplyMint?: PublicKey; debtMint?: PublicKey }[]
93
+ > {
92
94
  const marginfiAccounts = await umi.rpc.getProgramAccounts(
93
95
  MARGINFI_PROGRAM_ID,
94
96
  {
@@ -123,9 +125,15 @@ export async function getAllMarginfiAccountsByAuthority(
123
125
  );
124
126
  return positionStates
125
127
  .filter((x) => x.state !== undefined)
126
- .map((x) => toWeb3JsPublicKey(x.publicKey));
128
+ .map((x) => ({
129
+ marginfiAccount: toWeb3JsPublicKey(x.publicKey),
130
+ supplyMint: toWeb3JsPublicKey(x.state!.supply.mint),
131
+ debtMint: toWeb3JsPublicKey(x.state!.debt.mint),
132
+ }));
127
133
  } else {
128
- return marginfiAccounts.map((x) => toWeb3JsPublicKey(x.publicKey));
134
+ return marginfiAccounts.map((x) => ({
135
+ marginfiAccount: toWeb3JsPublicKey(x.publicKey),
136
+ }));
129
137
  }
130
138
  }
131
139
 
@@ -27,6 +27,10 @@ import {
27
27
  getAllMarginfiAccountsByAuthority,
28
28
  getMarginfiAccountPositionState,
29
29
  } from "../marginfiUtils";
30
+ import {
31
+ SelfManagedPositionDetails,
32
+ SolautoPositionDetails,
33
+ } from "../../types/solauto";
30
34
 
31
35
  function newPeriodsPassed(
32
36
  automation: AutomationSettings,
@@ -278,12 +282,6 @@ export async function getReferralsByUser(
278
282
  return accounts.map((x) => toWeb3JsPublicKey(x.publicKey));
279
283
  }
280
284
 
281
- export interface SolautoPositionDetails {
282
- positionId: number;
283
- lendingPlatform: LendingPlatform;
284
- protocolAccount?: PublicKey;
285
- }
286
-
287
285
  export async function getAllPositionsByAuthority(
288
286
  umi: Umi,
289
287
  user: PublicKey
@@ -295,16 +293,27 @@ export async function getAllPositionsByAuthority(
295
293
  ...solautoManagedPositions.map((x) => ({
296
294
  positionId: x.positionId,
297
295
  lendingPlatform: x.lendingPlatform,
296
+ selfManaged: false,
298
297
  }))
299
298
  );
300
299
 
301
- const marginfiPositions = await getAllMarginfiAccountsByAuthority(umi, user, true);
300
+ const marginfiPositions = await getAllMarginfiAccountsByAuthority(
301
+ umi,
302
+ user,
303
+ true
304
+ );
302
305
  allPositions.push(
303
- ...marginfiPositions.map((pubkey) => ({
304
- positionId: 0,
305
- lendingPlatform: LendingPlatform.Marginfi,
306
- protocolAccount: pubkey,
307
- }))
306
+ ...marginfiPositions.map(
307
+ (x) =>
308
+ ({
309
+ positionId: 0,
310
+ selfManaged: true,
311
+ lendingPlatform: LendingPlatform.Marginfi,
312
+ protocolAccount: x.marginfiAccount,
313
+ supplyMint: x.supplyMint,
314
+ debtMint: x.debtMint,
315
+ }) as SelfManagedPositionDetails
316
+ )
308
317
  );
309
318
 
310
319
  // TODO support other platforms
@@ -1,6 +1,6 @@
1
1
  import { PublicKey } from "@solana/web3.js";
2
2
  import { SolautoClient } from "../../clients/solautoClient";
3
- import { PositionTokenUsage } from "../../generated";
3
+ import { FeeType, PositionTokenUsage } from "../../generated";
4
4
  import {
5
5
  eligibleForNextAutomationPeriod,
6
6
  getAdjustedSettingsFromAutomation,
@@ -188,7 +188,7 @@ export function getRebalanceValues(
188
188
  if (increasingLeverage) {
189
189
  adjustmentFeeBps = getSolautoFeesBps(
190
190
  client.referredByState !== undefined,
191
- client.solautoPositionData!.feeType
191
+ client.solautoPositionData?.feeType ?? FeeType.Small
192
192
  ).total;
193
193
  }
194
194
 
@@ -208,7 +208,6 @@ export function getRebalanceValues(
208
208
  targetRateBps,
209
209
  adjustmentFeeBps
210
210
  );
211
- debtAdjustmentUsd *= 500;
212
211
 
213
212
  const input = increasingLeverage
214
213
  ? client.solautoPositionState!.debt
@@ -25,7 +25,7 @@ describe("Solauto Marginfi tests", async () => {
25
25
 
26
26
  const payForTransactions = false;
27
27
  const useJitoBundle = false;
28
- const positionId = 1;
28
+ const positionId = 0;
29
29
 
30
30
  it("open - deposit - borrow - rebalance to 0 - withdraw - close", async () => {
31
31
  const client = new SolautoMarginfiClient(process.env.HELIUS_API_KEY!, true);
@@ -45,6 +45,9 @@ describe("Solauto Marginfi tests", async () => {
45
45
  {
46
46
  signer,
47
47
  positionId,
48
+ marginfiAccount: new PublicKey("4nNvUXF5YqHFcH2nGweSiuvy1ct7V5FXfoCLKFYUN36z"),
49
+ supplyMint: NATIVE_MINT,
50
+ debtMint: new PublicKey(USDC_MINT)
48
51
  }
49
52
  );
50
53
 
@@ -58,29 +61,29 @@ describe("Solauto Marginfi tests", async () => {
58
61
  targetBoostToBps: none(),
59
62
  };
60
63
 
61
- if (client.solautoPositionData === null) {
62
- transactionItems.push(
63
- new TransactionItem(async () => {
64
- return {
65
- tx: client.openPosition(settingParams),
66
- };
67
- }, "open position")
68
- );
64
+ // if (client.solautoPositionData === null) {
65
+ // transactionItems.push(
66
+ // new TransactionItem(async () => {
67
+ // return {
68
+ // tx: client.openPosition(),
69
+ // };
70
+ // }, "open position")
71
+ // );
69
72
 
70
- const initialSupplyUsd = 50;
71
- transactionItems.push(
72
- new TransactionItem(async () => {
73
- const [supplyPrice] = await getTokenPrices([supply]);
74
- return {
75
- tx: client.protocolInteraction(
76
- solautoAction("Deposit", [
77
- toBaseUnit(initialSupplyUsd / supplyPrice, supplyDecimals),
78
- ])
79
- ),
80
- };
81
- }, "deposit")
82
- );
83
- }
73
+ // const initialSupplyUsd = 50;
74
+ // transactionItems.push(
75
+ // new TransactionItem(async () => {
76
+ // const [supplyPrice] = await getTokenPrices([supply]);
77
+ // return {
78
+ // tx: client.protocolInteraction(
79
+ // solautoAction("Deposit", [
80
+ // toBaseUnit(initialSupplyUsd / supplyPrice, supplyDecimals),
81
+ // ])
82
+ // ),
83
+ // };
84
+ // }, "deposit")
85
+ // );
86
+ // }
84
87
 
85
88
  // transactionItems.push(
86
89
  // new TransactionItem(
@@ -104,7 +107,7 @@ describe("Solauto Marginfi tests", async () => {
104
107
  transactionItems.push(
105
108
  new TransactionItem(
106
109
  async (attemptNum) =>
107
- await buildSolautoRebalanceTransaction(client, undefined, attemptNum),
110
+ await buildSolautoRebalanceTransaction(client, 5000, attemptNum),
108
111
  "rebalance"
109
112
  )
110
113
  );
@@ -141,7 +144,7 @@ describe("Solauto Marginfi tests", async () => {
141
144
  transactionItems,
142
145
  undefined,
143
146
  !payForTransactions,
144
- useJitoBundle,
147
+ useJitoBundle
145
148
  ).send();
146
149
  });
147
150
  });
@@ -1,14 +1,15 @@
1
1
  import { describe, it } from 'mocha';
2
- import { PublicKey } from "@solana/web3.js";
2
+ import { clusterApiUrl, Connection, PublicKey } from "@solana/web3.js";
3
3
  import {
4
4
  MARGINFI_ACCOUNTS,
5
5
  MARGINFI_ACCOUNTS_LOOKUP_TABLE,
6
6
  } from "../../src/constants/marginfiAccounts";
7
- import { CONNECTION } from "../../src/constants/solautoConstants";
7
+
8
+ const conn = new Connection(clusterApiUrl("mainnet-beta"), "confirmed");
8
9
 
9
10
  describe("Assert lookup tables up-to-date", async () => {
10
11
  it("marginfi accounts LUT should have everything", async () => {
11
- const lookupTable = await CONNECTION.getAddressLookupTable(
12
+ const lookupTable = await conn.getAddressLookupTable(
12
13
  new PublicKey(MARGINFI_ACCOUNTS_LOOKUP_TABLE)
13
14
  );
14
15
  if (lookupTable === null) {