@haven-fi/solauto-sdk 1.0.666 → 1.0.668

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 (44) hide show
  1. package/dist/constants/solautoConstants.d.ts +0 -1
  2. package/dist/constants/solautoConstants.d.ts.map +1 -1
  3. package/dist/constants/solautoConstants.js +1 -2
  4. package/dist/marginfi-sdk/errors/marginfi.d.ts +209 -23
  5. package/dist/marginfi-sdk/errors/marginfi.d.ts.map +1 -1
  6. package/dist/marginfi-sdk/errors/marginfi.js +465 -123
  7. package/dist/marginfi-sdk/instructions/lendingAccountDeposit.d.ts +3 -1
  8. package/dist/marginfi-sdk/instructions/lendingAccountDeposit.d.ts.map +1 -1
  9. package/dist/marginfi-sdk/instructions/lendingAccountDeposit.js +1 -0
  10. package/dist/services/solauto/solautoClient.d.ts.map +1 -1
  11. package/dist/services/solauto/solautoMarginfiClient.d.ts.map +1 -1
  12. package/dist/services/solauto/solautoMarginfiClient.js +2 -1
  13. package/dist/services/transactions/transactionUtils.d.ts.map +1 -1
  14. package/dist/services/transactions/transactionUtils.js +1 -0
  15. package/dist/services/transactions/transactionsManager.d.ts.map +1 -1
  16. package/dist/solautoPosition/marginfiSolautoPositionEx.d.ts.map +1 -1
  17. package/dist/solautoPosition/marginfiSolautoPositionEx.js +4 -5
  18. package/dist/solautoPosition/solautoPositionEx.d.ts +4 -2
  19. package/dist/solautoPosition/solautoPositionEx.d.ts.map +1 -1
  20. package/dist/solautoPosition/solautoPositionEx.js +8 -8
  21. package/dist/utils/generalUtils.d.ts +2 -1
  22. package/dist/utils/generalUtils.d.ts.map +1 -1
  23. package/dist/utils/generalUtils.js +4 -0
  24. package/dist/utils/marginfiUtils.d.ts.map +1 -1
  25. package/dist/utils/marginfiUtils.js +4 -7
  26. package/dist/utils/solanaUtils.d.ts.map +1 -1
  27. package/dist/utils/solanaUtils.js +12 -11
  28. package/dist/utils/solautoUtils.d.ts.map +1 -1
  29. package/dist/utils/solautoUtils.js +4 -5
  30. package/local/logPositions.ts +1 -1
  31. package/local/updateSolautoLUT.ts +0 -12
  32. package/package.json +1 -1
  33. package/src/marginfi-sdk/errors/marginfi.ts +583 -122
  34. package/src/marginfi-sdk/instructions/lendingAccountDeposit.ts +7 -0
  35. package/src/services/solauto/solautoMarginfiClient.ts +2 -1
  36. package/src/services/transactions/transactionUtils.ts +1 -0
  37. package/src/solautoPosition/marginfiSolautoPositionEx.ts +8 -7
  38. package/src/solautoPosition/solautoPositionEx.ts +9 -4
  39. package/src/utils/generalUtils.ts +5 -1
  40. package/src/utils/marginfiUtils.ts +6 -12
  41. package/src/utils/solanaUtils.ts +11 -16
  42. package/src/utils/solautoUtils.ts +4 -7
  43. package/tests/transactions/solautoMarginfi.ts +1 -1
  44. package/tests/unit/rebalanceCalculations.ts +6 -3
@@ -8,6 +8,8 @@
8
8
 
9
9
  import {
10
10
  Context,
11
+ Option,
12
+ OptionOrNullable,
11
13
  Pda,
12
14
  PublicKey,
13
15
  Signer,
@@ -17,7 +19,9 @@ import {
17
19
  import {
18
20
  Serializer,
19
21
  array,
22
+ bool,
20
23
  mapSerializer,
24
+ option,
21
25
  struct,
22
26
  u64,
23
27
  u8,
@@ -43,10 +47,12 @@ export type LendingAccountDepositInstructionAccounts = {
43
47
  export type LendingAccountDepositInstructionData = {
44
48
  discriminator: Array<number>;
45
49
  amount: bigint;
50
+ depositUpToLimit: Option<boolean>;
46
51
  };
47
52
 
48
53
  export type LendingAccountDepositInstructionDataArgs = {
49
54
  amount: number | bigint;
55
+ depositUpToLimit: OptionOrNullable<boolean>;
50
56
  };
51
57
 
52
58
  export function getLendingAccountDepositInstructionDataSerializer(): Serializer<
@@ -62,6 +68,7 @@ export function getLendingAccountDepositInstructionDataSerializer(): Serializer<
62
68
  [
63
69
  ['discriminator', array(u8(), { size: 8 })],
64
70
  ['amount', u64()],
71
+ ['depositUpToLimit', option(bool())],
65
72
  ],
66
73
  { description: 'LendingAccountDepositInstructionData' }
67
74
  ),
@@ -259,7 +259,6 @@ export class SolautoMarginfiClient extends SolautoClient {
259
259
  switch (args.__kind) {
260
260
  case "Deposit": {
261
261
  return lendingAccountDeposit(this.umi, {
262
- amount: args.fields[0],
263
262
  signer: this.signer,
264
263
  signerTokenAccount: publicKey(this.signerSupplyTa),
265
264
  marginfiAccount: publicKey(this.marginfiAccountPk),
@@ -268,6 +267,8 @@ export class SolautoMarginfiClient extends SolautoClient {
268
267
  bankLiquidityVault: publicKey(
269
268
  this.marginfiSupplyAccounts.liquidityVault
270
269
  ),
270
+ amount: args.fields[0],
271
+ depositUpToLimit: true,
271
272
  });
272
273
  }
273
274
  case "Borrow": {
@@ -417,6 +417,7 @@ function getSolautoActions(umi: Umi, tx: TransactionBuilder): SolautoAction[] {
417
417
  serializer
418
418
  .serialize({
419
419
  amount: 0,
420
+ depositUpToLimit: true
420
421
  })
421
422
  .slice(0, 8)
422
423
  );
@@ -14,6 +14,7 @@ import {
14
14
  getBankLiquidityAvailableBaseUnit,
15
15
  getMarginfiAccountPositionState,
16
16
  getMarginfiPriceOracle,
17
+ validPubkey,
17
18
  } from "../utils";
18
19
  import { getMarginfiAccounts } from "../constants";
19
20
  import { SolautoPositionEx } from "./solautoPositionEx";
@@ -31,14 +32,10 @@ export class MarginfiSolautoPositionEx extends SolautoPositionEx {
31
32
  return this.lp;
32
33
  }
33
34
 
34
- if (
35
- !this.marginfiAccountData &&
36
- this.lpUserAccount &&
37
- !this.lpUserAccount.equals(PublicKey.default)
38
- ) {
35
+ if (!this.marginfiAccountData && validPubkey(this.lpUserAccount)) {
39
36
  this.marginfiAccountData = await fetchMarginfiAccount(
40
37
  this.umi,
41
- publicKey(this.lpUserAccount),
38
+ publicKey(this.lpUserAccount!),
42
39
  { commitment: "confirmed" }
43
40
  );
44
41
  this.lp = toWeb3JsPublicKey(this.marginfiAccountData.group);
@@ -97,7 +94,11 @@ export class MarginfiSolautoPositionEx extends SolautoPositionEx {
97
94
  }
98
95
 
99
96
  async refreshPositionState(priceType?: PriceType): Promise<void> {
100
- const useDesignatedMint = !this._data.position || !this.selfManaged;
97
+ const useDesignatedMint =
98
+ !this.exists ||
99
+ !this.selfManaged ||
100
+ (this.selfManaged && !validPubkey(this.lpUserAccount));
101
+
101
102
  const resp = await getMarginfiAccountPositionState(
102
103
  this.umi,
103
104
  { pk: this.lpUserAccount },
@@ -83,10 +83,13 @@ export abstract class SolautoPositionEx {
83
83
  protected lp?: PublicKey = undefined;
84
84
  public lpUserAccount?: PublicKey = undefined;
85
85
  protected lpEnv!: ProgramEnv;
86
+ private _supplyMint?: PublicKey;
87
+ private _debtMint?: PublicKey;
86
88
 
87
89
  private readonly firstState!: PositionState;
88
- protected _supplyPrice?: number;
89
- protected _debtPrice?: number;
90
+
91
+ private _supplyPrice?: number;
92
+ private _debtPrice?: number;
90
93
 
91
94
  public rebalanceHelper!: PositionRebalanceHelper;
92
95
 
@@ -104,6 +107,8 @@ export abstract class SolautoPositionEx {
104
107
  this.positionId = args.positionId ?? args.data.positionId![0];
105
108
  this.authority = args.authority ?? toWeb3JsPublicKey(args.data.authority!);
106
109
 
110
+ this._supplyMint = args.customArgs?.supplyMint;
111
+ this._debtMint = args.customArgs?.debtMint;
107
112
  this.lp = args.customArgs?.lendingPool;
108
113
  this.lpUserAccount =
109
114
  args.customArgs?.lpUserAccount ??
@@ -169,7 +174,7 @@ export abstract class SolautoPositionEx {
169
174
  }
170
175
 
171
176
  get supplyMint(): PublicKey {
172
- return toWeb3JsPublicKey(this.state.supply.mint);
177
+ return this._supplyMint ?? toWeb3JsPublicKey(this.state.supply.mint);
173
178
  }
174
179
 
175
180
  get supplyMintInfo(): TokenInfo {
@@ -177,7 +182,7 @@ export abstract class SolautoPositionEx {
177
182
  }
178
183
 
179
184
  get debtMint(): PublicKey {
180
- return toWeb3JsPublicKey(this.state.debt.mint);
185
+ return this._debtMint ?? toWeb3JsPublicKey(this.state.debt.mint);
181
186
  }
182
187
 
183
188
  get debtMintInfo(): TokenInfo {
@@ -1,6 +1,6 @@
1
1
  import axios from "axios";
2
2
  import { PublicKey } from "@solana/web3.js";
3
- import { MaybeRpcAccount, publicKey, Umi } from "@metaplex-foundation/umi";
3
+ import { MaybeRpcAccount, publicKey, Umi, PublicKey as UmiPublicKey } from "@metaplex-foundation/umi";
4
4
  import { TOKEN_INFO, TokenInfo } from "../constants";
5
5
 
6
6
  export function buildHeliusApiUrl(heliusApiKey: string) {
@@ -195,4 +195,8 @@ export function u16ToArrayBufferLE(value: number): Uint8Array {
195
195
 
196
196
  // Return the buffer
197
197
  return new Uint8Array(buffer);
198
+ }
199
+
200
+ export function validPubkey(pubkey?: PublicKey | UmiPublicKey | string) {
201
+ return Boolean(pubkey) && pubkey!.toString() !== PublicKey.default.toString();
198
202
  }
@@ -31,7 +31,7 @@ import {
31
31
  } from "../marginfi-sdk";
32
32
  import { ContextUpdates } from "./solautoUtils";
33
33
  import { fetchTokenPrices, safeGetPrice } from "./priceUtils";
34
- import { currentUnixSeconds } from "./generalUtils";
34
+ import { currentUnixSeconds, validPubkey } from "./generalUtils";
35
35
  import {
36
36
  bytesToI80F48,
37
37
  calcNetWorthUsd,
@@ -245,7 +245,7 @@ export async function getMarginfiMaxLtvAndLiqThresholdBps(
245
245
  },
246
246
  supplyPrice?: number
247
247
  ): Promise<[number, number]> {
248
- if (!supply.bank && supply.mint.equals(PublicKey.default)) {
248
+ if (!supply.bank && !validPubkey(supply.mint)) {
249
249
  return [0, 0];
250
250
  }
251
251
 
@@ -264,10 +264,7 @@ export async function getMarginfiMaxLtvAndLiqThresholdBps(
264
264
  );
265
265
  }
266
266
 
267
- if (
268
- (!debt.bank || debt.bank === null) &&
269
- !debt.mint.equals(PublicKey.default)
270
- ) {
267
+ if ((!debt.bank || debt.bank === null) && !validPubkey(debt.mint)) {
271
268
  debt.bank = await safeFetchBank(
272
269
  umi,
273
270
  publicKey(
@@ -488,10 +485,7 @@ async function getBank(
488
485
  data: BankSelection,
489
486
  marginfiGroup: PublicKey
490
487
  ) {
491
- const mint =
492
- data?.mint && !data.mint.equals(PublicKey.default)
493
- ? data.mint.toString()
494
- : undefined;
488
+ const mint = validPubkey(data.mint) ? data.mint!.toString() : undefined;
495
489
 
496
490
  return data?.banksCache && mint
497
491
  ? data.banksCache[marginfiGroup.toString()][mint]
@@ -523,8 +517,8 @@ export async function getMarginfiAccountPositionState(
523
517
  > {
524
518
  let marginfiAccount =
525
519
  lpUserAccount.data ??
526
- (lpUserAccount.pk
527
- ? await safeFetchMarginfiAccount(umi, publicKey(lpUserAccount.pk), {
520
+ (validPubkey(lpUserAccount.pk)
521
+ ? await safeFetchMarginfiAccount(umi, publicKey(lpUserAccount.pk!), {
528
522
  commitment: "confirmed",
529
523
  })
530
524
  : null);
@@ -223,11 +223,14 @@ export function addTxOptimizations(
223
223
  : transactionBuilder();
224
224
 
225
225
  const allOptimizations = tx.prepend(computePriceIx).prepend(computeLimitIx);
226
- const oneOptimization = tx.prepend(computePriceIx);
226
+ const withCuPrice = tx.prepend(computePriceIx);
227
+ const withCuLimit = tx.prepend(computeLimitIx);
227
228
  if (allOptimizations.fitsInOneTransaction(umi)) {
228
229
  return allOptimizations;
229
- } else if (oneOptimization.fitsInOneTransaction(umi)) {
230
- return oneOptimization;
230
+ } else if (withCuPrice.fitsInOneTransaction(umi)) {
231
+ return withCuPrice;
232
+ } else if (withCuLimit.fitsInOneTransaction(umi)) {
233
+ return withCuLimit;
231
234
  } else {
232
235
  return tx;
233
236
  }
@@ -448,7 +451,7 @@ export async function sendSingleOptimizedTransaction(
448
451
 
449
452
  const blockhash = await connection.getLatestBlockhash("confirmed");
450
453
 
451
- let computeUnitLimit = undefined;
454
+ let cuLimit = undefined;
452
455
  if (txType !== "skip-simulation") {
453
456
  const simulationResult = await retryWithExponentialBackoff(
454
457
  async () =>
@@ -461,28 +464,20 @@ export async function sendSingleOptimizedTransaction(
461
464
  ),
462
465
  3
463
466
  );
464
- computeUnitLimit = Math.round(simulationResult.value.unitsConsumed! * 1.15);
465
- consoleLog("Compute unit limit: ", computeUnitLimit);
467
+ cuLimit = Math.round(simulationResult.value.unitsConsumed! * 1.15);
468
+ consoleLog("Compute unit limit: ", cuLimit);
466
469
  }
467
470
 
468
471
  let cuPrice: number | undefined;
469
472
  if (prioritySetting !== PriorityFeeSetting.None) {
470
473
  cuPrice = await getComputeUnitPriceEstimate(umi, tx, prioritySetting);
471
- if (!cuPrice) {
472
- cuPrice = 1_000_000;
473
- }
474
- cuPrice = Math.min(cuPrice, 100 * 1_000_000);
474
+ cuPrice = Math.min(cuPrice ?? 0, 100 * 1_000_000);
475
475
  consoleLog("Compute unit price: ", cuPrice);
476
476
  }
477
477
 
478
478
  if (txType !== "only-simulate") {
479
479
  onAwaitingSign?.();
480
- const signedTx = await assembleFinalTransaction(
481
- umi,
482
- tx,
483
- cuPrice,
484
- computeUnitLimit
485
- )
480
+ const signedTx = await assembleFinalTransaction(umi, tx, cuPrice, cuLimit)
486
481
  .setBlockhash(blockhash)
487
482
  .buildAndSign(umi);
488
483
  const txSig = await spamSendTransactionUntilConfirmed(
@@ -41,6 +41,7 @@ import {
41
41
  findMarginfiAccounts,
42
42
  getAllMarginfiAccountsByAuthority,
43
43
  } from "./marginfiUtils";
44
+ import { validPubkey } from "./generalUtils";
44
45
 
45
46
  export function createDynamicSolautoProgram(programId?: PublicKey): Program {
46
47
  return {
@@ -179,9 +180,8 @@ export async function getSolautoManagedPositions(
179
180
  findMarginfiAccounts(
180
181
  toWeb3JsPublicKey(position.position.lpSupplyAccount)
181
182
  ).mint,
182
- findMarginfiAccounts(
183
- toWeb3JsPublicKey(position.position.lpDebtAccount)
184
- ).mint,
183
+ findMarginfiAccounts(toWeb3JsPublicKey(position.position.lpDebtAccount))
184
+ .mint,
185
185
  ];
186
186
  }
187
187
  // TODO: PF
@@ -285,10 +285,7 @@ export async function getAllPositionsByAuthority(
285
285
  true
286
286
  );
287
287
  marginfiPositions = marginfiPositions.filter(
288
- (x) =>
289
- x.supplyMint &&
290
- (x.debtMint!.equals(PublicKey.default) ||
291
- ALL_SUPPORTED_TOKENS.includes(x.debtMint!.toString()))
288
+ (x) => validPubkey(x.supplyMint) && validPubkey(x.debtMint)
292
289
  );
293
290
  return marginfiPositions.map((x) => ({
294
291
  publicKey: x.marginfiAccount,
@@ -5,7 +5,7 @@ import { e2eTransactionTest } from "./shared";
5
5
 
6
6
  describe("Solauto Marginfi tests", async () => {
7
7
  const signer = setupTest();
8
- const testProgram = true;
8
+ const testProgram = false;
9
9
 
10
10
  it("open - deposit - borrow - rebalance to 0 - withdraw - close", async () => {
11
11
  await e2eTransactionTest(
@@ -92,16 +92,19 @@ async function getFakePosition(
92
92
  liqThresholdBps
93
93
  );
94
94
 
95
+ const defaultPk = publicKey(PublicKey.default);
95
96
  client.pos = new MarginfiSolautoPositionEx({
96
97
  umi: client.umi,
97
98
  publicKey: PublicKey.default,
98
99
  data: {
99
100
  state: fakeState,
101
+ positionId: [1],
102
+ authority: defaultPk,
100
103
  position: {
101
104
  lendingPlatform: LendingPlatform.Marginfi,
102
- lpUserAccount: publicKey(PublicKey.default),
103
- lpSupplyAccount: publicKey(PublicKey.default),
104
- lpDebtAccount: publicKey(PublicKey.default),
105
+ lpUserAccount: defaultPk,
106
+ lpSupplyAccount: defaultPk,
107
+ lpDebtAccount: defaultPk,
105
108
  settings,
106
109
  dca: null,
107
110
  padding: [],