@haven-fi/solauto-sdk 1.0.624 → 1.0.626

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 (130) hide show
  1. package/dist/constants/{generalAccounts.d.ts → generalConstants.d.ts} +3 -1
  2. package/dist/constants/generalConstants.d.ts.map +1 -0
  3. package/dist/constants/{generalAccounts.js → generalConstants.js} +4 -1
  4. package/dist/constants/index.d.ts +6 -5
  5. package/dist/constants/index.d.ts.map +1 -1
  6. package/dist/constants/index.js +2 -1
  7. package/dist/constants/marginfiAccounts.d.ts +12 -4
  8. package/dist/constants/marginfiAccounts.d.ts.map +1 -1
  9. package/dist/constants/marginfiAccounts.js +62 -8
  10. package/dist/constants/solautoConstants.d.ts +0 -3
  11. package/dist/constants/solautoConstants.d.ts.map +1 -1
  12. package/dist/constants/solautoConstants.js +3 -8
  13. package/dist/services/flashLoans/flProviderAggregator.d.ts +3 -3
  14. package/dist/services/flashLoans/flProviderAggregator.d.ts.map +1 -1
  15. package/dist/services/flashLoans/flProviderAggregator.js +5 -5
  16. package/dist/services/flashLoans/flProviderBase.d.ts +4 -3
  17. package/dist/services/flashLoans/flProviderBase.d.ts.map +1 -1
  18. package/dist/services/flashLoans/flProviderBase.js +3 -2
  19. package/dist/services/flashLoans/marginfiFlProvider.d.ts +2 -2
  20. package/dist/services/flashLoans/marginfiFlProvider.d.ts.map +1 -1
  21. package/dist/services/flashLoans/marginfiFlProvider.js +6 -6
  22. package/dist/services/rebalance/rebalanceSwapManager.d.ts.map +1 -1
  23. package/dist/services/rebalance/rebalanceSwapManager.js +1 -1
  24. package/dist/services/rebalance/rebalanceTxBuilder.d.ts.map +1 -1
  25. package/dist/services/rebalance/rebalanceTxBuilder.js +4 -4
  26. package/dist/services/rebalance/rebalanceValues.d.ts.map +1 -1
  27. package/dist/services/rebalance/solautoFees.d.ts.map +1 -1
  28. package/dist/services/rebalance/solautoFees.js +4 -2
  29. package/dist/services/solauto/solautoClient.d.ts +4 -3
  30. package/dist/services/solauto/solautoClient.d.ts.map +1 -1
  31. package/dist/services/solauto/solautoClient.js +22 -24
  32. package/dist/services/solauto/solautoMarginfiClient.d.ts +5 -5
  33. package/dist/services/solauto/solautoMarginfiClient.d.ts.map +1 -1
  34. package/dist/services/solauto/solautoMarginfiClient.js +22 -20
  35. package/dist/services/solauto/txHandler.d.ts +4 -2
  36. package/dist/services/solauto/txHandler.d.ts.map +1 -1
  37. package/dist/services/solauto/txHandler.js +4 -6
  38. package/dist/services/swap/jupSwapManager.d.ts +1 -1
  39. package/dist/services/swap/jupSwapManager.d.ts.map +1 -1
  40. package/dist/services/swap/jupSwapManager.js +7 -10
  41. package/dist/services/transactions/transactionUtils.d.ts +2 -3
  42. package/dist/services/transactions/transactionUtils.d.ts.map +1 -1
  43. package/dist/services/transactions/transactionUtils.js +43 -46
  44. package/dist/services/transactions/transactionsManager.d.ts +2 -3
  45. package/dist/services/transactions/transactionsManager.d.ts.map +1 -1
  46. package/dist/services/transactions/transactionsManager.js +16 -20
  47. package/dist/solautoPosition/marginfiSolautoPositionEx.d.ts.map +1 -1
  48. package/dist/solautoPosition/marginfiSolautoPositionEx.js +6 -5
  49. package/dist/solautoPosition/solautoPositionEx.d.ts +4 -2
  50. package/dist/solautoPosition/solautoPositionEx.d.ts.map +1 -1
  51. package/dist/solautoPosition/solautoPositionEx.js +12 -5
  52. package/dist/solautoPosition/utils.d.ts +2 -2
  53. package/dist/solautoPosition/utils.d.ts.map +1 -1
  54. package/dist/solautoPosition/utils.js +2 -2
  55. package/dist/types/solauto.d.ts +1 -0
  56. package/dist/types/solauto.d.ts.map +1 -1
  57. package/dist/utils/generalUtils.d.ts +2 -0
  58. package/dist/utils/generalUtils.d.ts.map +1 -1
  59. package/dist/utils/generalUtils.js +9 -1
  60. package/dist/utils/index.d.ts +1 -0
  61. package/dist/utils/index.d.ts.map +1 -1
  62. package/dist/utils/index.js +1 -0
  63. package/dist/utils/jitoUtils.d.ts.map +1 -1
  64. package/dist/utils/jitoUtils.js +7 -8
  65. package/dist/utils/jupiterUtils.d.ts.map +1 -1
  66. package/dist/utils/marginfiUtils.d.ts +13 -4
  67. package/dist/utils/marginfiUtils.d.ts.map +1 -1
  68. package/dist/utils/marginfiUtils.js +78 -22
  69. package/dist/utils/numberUtils.d.ts.map +1 -1
  70. package/dist/utils/priceUtils.d.ts.map +1 -1
  71. package/dist/utils/priceUtils.js +13 -15
  72. package/dist/utils/solanaUtils.d.ts +3 -5
  73. package/dist/utils/solanaUtils.d.ts.map +1 -1
  74. package/dist/utils/solanaUtils.js +7 -15
  75. package/dist/utils/solautoUtils.d.ts +3 -3
  76. package/dist/utils/solautoUtils.d.ts.map +1 -1
  77. package/dist/utils/solautoUtils.js +5 -5
  78. package/dist/utils/stringUtils.d.ts.map +1 -1
  79. package/dist/utils/stringUtils.js +3 -3
  80. package/dist/utils/switchboardUtils.d.ts +1 -1
  81. package/dist/utils/switchboardUtils.d.ts.map +1 -1
  82. package/dist/utils/switchboardUtils.js +4 -4
  83. package/local/createISMAccounts.ts +29 -12
  84. package/local/createTokenAccounts.ts +12 -11
  85. package/local/logPositions.ts +5 -5
  86. package/local/shared.ts +16 -10
  87. package/local/txSandbox.ts +5 -5
  88. package/local/updateMarginfiLUT.ts +40 -49
  89. package/local/updateSolautoLUT.ts +11 -13
  90. package/package.json +1 -1
  91. package/src/constants/{generalAccounts.ts → generalConstants.ts} +6 -1
  92. package/src/constants/index.ts +6 -5
  93. package/src/constants/marginfiAccounts.ts +88 -10
  94. package/src/constants/solautoConstants.ts +2 -10
  95. package/src/services/flashLoans/flProviderAggregator.ts +9 -7
  96. package/src/services/flashLoans/flProviderBase.ts +5 -4
  97. package/src/services/flashLoans/marginfiFlProvider.ts +9 -8
  98. package/src/services/rebalance/rebalanceSwapManager.ts +1 -1
  99. package/src/services/rebalance/rebalanceTxBuilder.ts +5 -5
  100. package/src/services/rebalance/rebalanceValues.ts +1 -1
  101. package/src/services/rebalance/solautoFees.ts +3 -4
  102. package/src/services/solauto/solautoClient.ts +10 -15
  103. package/src/services/solauto/solautoMarginfiClient.ts +26 -30
  104. package/src/services/solauto/txHandler.ts +10 -9
  105. package/src/services/swap/jupSwapManager.ts +7 -6
  106. package/src/services/transactions/transactionUtils.ts +17 -18
  107. package/src/services/transactions/transactionsManager.ts +23 -22
  108. package/src/solautoPosition/marginfiSolautoPositionEx.ts +7 -8
  109. package/src/solautoPosition/solautoPositionEx.ts +21 -10
  110. package/src/solautoPosition/utils.ts +7 -7
  111. package/src/types/solauto.ts +2 -0
  112. package/src/utils/generalUtils.ts +9 -1
  113. package/src/utils/index.ts +1 -0
  114. package/src/utils/jitoUtils.ts +17 -14
  115. package/src/utils/jupiterUtils.ts +1 -1
  116. package/src/utils/marginfiUtils.ts +105 -28
  117. package/src/utils/numberUtils.ts +0 -1
  118. package/src/utils/priceUtils.ts +7 -5
  119. package/src/utils/solanaUtils.ts +24 -32
  120. package/src/utils/solautoUtils.ts +13 -13
  121. package/src/utils/stringUtils.ts +3 -3
  122. package/src/utils/switchboardUtils.ts +4 -4
  123. package/tests/transactions/shared.ts +9 -14
  124. package/tests/transactions/solautoMarginfi.ts +13 -3
  125. package/tests/unit/accounts.ts +14 -17
  126. package/tests/unit/lookupTables.ts +5 -5
  127. package/tests/unit/rebalanceCalculations.ts +17 -11
  128. package/README.md +0 -3
  129. package/dist/constants/generalAccounts.d.ts.map +0 -1
  130. package/src/constants/README.md +0 -7
@@ -1,10 +1,10 @@
1
- import { FlProviderBase } from "./flProviderBase";
2
1
  import { PublicKey } from "@solana/web3.js";
3
- import { FlashLoanDetails, FlashLoanRequirements } from "../../types";
4
2
  import { Signer, TransactionBuilder, Umi } from "@metaplex-foundation/umi";
3
+ import { toWeb3JsPublicKey } from "@metaplex-foundation/umi-web3js-adapters";
4
+ import { FlProviderBase } from "./flProviderBase";
5
+ import { FlashLoanDetails, ProgramEnv } from "../../types";
5
6
  import { TokenType } from "../../generated";
6
7
  import { MarginfiFlProvider } from "./marginfiFlProvider";
7
- import { toWeb3JsPublicKey } from "@metaplex-foundation/umi-web3js-adapters";
8
8
 
9
9
  export class FlProviderAggregator extends FlProviderBase {
10
10
  private marginfiFlProvider!: MarginfiFlProvider;
@@ -14,15 +14,17 @@ export class FlProviderAggregator extends FlProviderBase {
14
14
  signer: Signer,
15
15
  authority: PublicKey,
16
16
  supplyMint: PublicKey,
17
- debtMint: PublicKey
17
+ debtMint: PublicKey,
18
+ programEnv?: ProgramEnv
18
19
  ) {
19
- super(umi, signer, authority, supplyMint, debtMint);
20
+ super(umi, signer, authority, supplyMint, debtMint, programEnv);
20
21
  this.marginfiFlProvider = new MarginfiFlProvider(
21
22
  umi,
22
23
  signer,
23
24
  authority,
24
25
  supplyMint,
25
- debtMint
26
+ debtMint,
27
+ programEnv
26
28
  );
27
29
  }
28
30
 
@@ -60,7 +62,7 @@ export class FlProviderAggregator extends FlProviderBase {
60
62
  }
61
63
 
62
64
  flFeeBps(source: TokenType, signerFlashLoan?: boolean): number {
63
- return this.flProvider(source).flFeeBps(source);
65
+ return this.flProvider(source).flFeeBps(source, signerFlashLoan);
64
66
  }
65
67
 
66
68
  flashBorrow(
@@ -1,10 +1,11 @@
1
+ import { PublicKey } from "@solana/web3.js";
1
2
  import {
2
3
  Signer,
3
4
  transactionBuilder,
4
5
  TransactionBuilder,
5
6
  Umi,
6
7
  } from "@metaplex-foundation/umi";
7
- import { PublicKey } from "@solana/web3.js";
8
+ import { toWeb3JsPublicKey } from "@metaplex-foundation/umi-web3js-adapters";
8
9
  import {
9
10
  fromBaseUnit,
10
11
  getTokenAccount,
@@ -13,8 +14,7 @@ import {
13
14
  tokenInfo,
14
15
  } from "../../utils";
15
16
  import { TokenType } from "../../generated";
16
- import { FlashLoanDetails } from "../../types";
17
- import { toWeb3JsPublicKey } from "@metaplex-foundation/umi-web3js-adapters";
17
+ import { FlashLoanDetails, ProgramEnv } from "../../types";
18
18
 
19
19
  export abstract class FlProviderBase {
20
20
  protected flSigners: Signer[] = [];
@@ -24,7 +24,8 @@ export abstract class FlProviderBase {
24
24
  protected signer: Signer,
25
25
  protected authority: PublicKey,
26
26
  protected supplyMint: PublicKey,
27
- protected debtMint: PublicKey
27
+ protected debtMint: PublicKey,
28
+ protected programEnv: ProgramEnv = "Prod"
28
29
  ) {}
29
30
 
30
31
  abstract initialize(): Promise<void>;
@@ -1,3 +1,4 @@
1
+ import { PublicKey, SYSVAR_INSTRUCTIONS_PUBKEY } from "@solana/web3.js";
1
2
  import {
2
3
  AccountMeta,
3
4
  createSignerFromKeypair,
@@ -6,7 +7,8 @@ import {
6
7
  transactionBuilder,
7
8
  TransactionBuilder,
8
9
  } from "@metaplex-foundation/umi";
9
- import { MARGINFI_ACCOUNTS } from "../../constants";
10
+ import { toWeb3JsPublicKey } from "@metaplex-foundation/umi-web3js-adapters";
11
+ import { getMarginfiAccounts } from "../../constants";
10
12
  import {
11
13
  Bank,
12
14
  lendingAccountBorrow,
@@ -19,7 +21,6 @@ import {
19
21
  safeFetchAllBank,
20
22
  } from "../../marginfi-sdk";
21
23
  import { FlProviderBase } from "./flProviderBase";
22
- import { PublicKey, SYSVAR_INSTRUCTIONS_PUBKEY } from "@solana/web3.js";
23
24
  import {
24
25
  bytesToI80F48,
25
26
  consoleLog,
@@ -34,9 +35,8 @@ import {
34
35
  toBps,
35
36
  tokenInfo,
36
37
  } from "../../utils";
37
- import { TokenType } from "../../generated";
38
- import { toWeb3JsPublicKey } from "@metaplex-foundation/umi-web3js-adapters";
39
38
  import { FlashLoanDetails } from "../../types";
39
+ import { TokenType } from "../../generated";
40
40
 
41
41
  interface IMFIAccount {
42
42
  signer?: Signer;
@@ -69,14 +69,16 @@ export class MarginfiFlProvider extends FlProviderBase {
69
69
  }
70
70
 
71
71
  private async setAvailableBanks() {
72
+ const bankAccounts = getMarginfiAccounts(this.programEnv).bankAccounts;
73
+
72
74
  const availableBanks: string[] = [];
73
75
  const checkIfUsable = (group: string, mint: PublicKey) => {
74
- if (Object.keys(MARGINFI_ACCOUNTS[group]).includes(mint.toString())) {
75
- availableBanks.push(MARGINFI_ACCOUNTS[group][mint.toString()].bank);
76
+ if (Object.keys(bankAccounts[group]).includes(mint.toString())) {
77
+ availableBanks.push(bankAccounts[group][mint.toString()].bank);
76
78
  }
77
79
  };
78
80
 
79
- for (const group of Object.keys(MARGINFI_ACCOUNTS)) {
81
+ for (const group of Object.keys(bankAccounts)) {
80
82
  checkIfUsable(group, this.supplyMint);
81
83
  checkIfUsable(group, this.debtMint);
82
84
  }
@@ -116,7 +118,6 @@ export class MarginfiFlProvider extends FlProviderBase {
116
118
  (x) => x.group.toString() == this.liquidityBank(sources[0]).group
117
119
  );
118
120
 
119
- // TODO: instead of picking first compatibleMarginfiAccoutn, pick one where we already have it in the marginfi Lut
120
121
  const signer =
121
122
  compatibleMarginfiAccounts.length > 0
122
123
  ? undefined
@@ -1,4 +1,5 @@
1
1
  import { QuoteResponse } from "@jup-ag/api";
2
+ import { toWeb3JsPublicKey } from "@metaplex-foundation/umi-web3js-adapters";
2
3
  import { FlashLoanRequirements } from "../../types";
3
4
  import { SolautoClient } from "../solauto";
4
5
  import { JupSwapManager, SwapParams, SwapInput } from "../swap";
@@ -13,7 +14,6 @@ import {
13
14
  toBaseUnit,
14
15
  tokenInfo,
15
16
  } from "../../utils";
16
- import { toWeb3JsPublicKey } from "@metaplex-foundation/umi-web3js-adapters";
17
17
 
18
18
  export class RebalanceSwapManager {
19
19
  public swapParams!: SwapParams;
@@ -1,3 +1,6 @@
1
+ import { PublicKey } from "@solana/web3.js";
2
+ import { toWeb3JsPublicKey } from "@metaplex-foundation/umi-web3js-adapters";
3
+ import { transactionBuilder } from "@metaplex-foundation/umi";
1
4
  import { SolautoClient } from "../solauto";
2
5
  import {
3
6
  FlashLoanRequirements,
@@ -14,8 +17,6 @@ import {
14
17
  safeGetPrice,
15
18
  tokenInfo,
16
19
  } from "../../utils";
17
- import { getRebalanceValues, RebalanceValues } from "./rebalanceValues";
18
- import { SolautoFeesBps } from "./solautoFees";
19
20
  import {
20
21
  PositionTokenState,
21
22
  RebalanceDirection,
@@ -25,10 +26,9 @@ import {
25
26
  TokenBalanceChangeType,
26
27
  TokenType,
27
28
  } from "../../generated";
28
- import { PublicKey } from "@solana/web3.js";
29
+ import { getRebalanceValues, RebalanceValues } from "./rebalanceValues";
30
+ import { SolautoFeesBps } from "./solautoFees";
29
31
  import { RebalanceSwapManager } from "./rebalanceSwapManager";
30
- import { toWeb3JsPublicKey } from "@metaplex-foundation/umi-web3js-adapters";
31
- import { transactionBuilder } from "@metaplex-foundation/umi";
32
32
 
33
33
  export class RebalanceTxBuilder {
34
34
  private values!: RebalanceValues;
@@ -3,13 +3,13 @@ import {
3
3
  TokenBalanceChange,
4
4
  TokenBalanceChangeType,
5
5
  } from "../../generated";
6
- import { SolautoPositionEx } from "../../solautoPosition";
7
6
  import {
8
7
  fromBps,
9
8
  getLiqUtilzationRateBps,
10
9
  maxRepayToBps,
11
10
  toBps,
12
11
  } from "../../utils";
12
+ import { SolautoPositionEx } from "../../solautoPosition";
13
13
  import { SolautoFeesBps } from "./solautoFees";
14
14
 
15
15
  export interface PositionValues {
@@ -37,10 +37,9 @@ export class SolautoFeesBps {
37
37
 
38
38
  let feeBps: number = 0;
39
39
 
40
- if (
41
- this.targetLiqUtilizationRateBps !== undefined ||
42
- rebalanceDirection === RebalanceDirection.Repay
43
- ) {
40
+ if (this.targetLiqUtilizationRateBps !== undefined) {
41
+ feeBps = 15;
42
+ } else if (rebalanceDirection === RebalanceDirection.Repay) {
44
43
  feeBps = 25;
45
44
  } else if (this.positionNetWorthUsd <= minSize) {
46
45
  feeBps = maxFeeBps;
@@ -1,10 +1,5 @@
1
1
  import "rpc-websockets/dist/lib/client";
2
- import {
3
- AddressLookupTableProgram,
4
- PublicKey,
5
- RpcResponseAndContext,
6
- TokenAmount,
7
- } from "@solana/web3.js";
2
+ import { AddressLookupTableProgram, PublicKey } from "@solana/web3.js";
8
3
  import {
9
4
  TransactionBuilder,
10
5
  isOption,
@@ -28,15 +23,13 @@ import {
28
23
  import {
29
24
  getSolautoPositionAccount,
30
25
  getTokenAccount,
31
- } from "../../utils/accountUtils";
32
- import { SOLAUTO_FEES_WALLET } from "../../constants/generalAccounts";
33
- import {
34
26
  getWalletSplBalances,
35
27
  getWrappedInstruction,
36
28
  splTokenTransferUmiIx,
37
- } from "../../utils/solanaUtils";
38
- import { SOLAUTO_LUT } from "../../constants/solautoConstants";
39
- import { ContextUpdates } from "../../utils/solautoUtils";
29
+ ContextUpdates,
30
+ } from "../../utils";
31
+ import { SOLAUTO_FEES_WALLET, SOLAUTO_LUT } from "../../constants";
32
+ import { ProgramEnv, RebalanceDetails } from "../../types";
40
33
  import {
41
34
  ReferralStateManager,
42
35
  ReferralStateManagerArgs,
@@ -45,8 +38,7 @@ import {
45
38
  getOrCreatePositionEx,
46
39
  SolautoPositionEx,
47
40
  } from "../../solautoPosition";
48
- import { RebalanceDetails } from "../../types";
49
- import { FlProviderAggregator } from "../flashLoans/flProviderAggregator";
41
+ import { FlProviderAggregator } from "../flashLoans";
50
42
 
51
43
  export interface SolautoClientArgs extends ReferralStateManagerArgs {
52
44
  new?: boolean;
@@ -59,6 +51,7 @@ export interface SolautoClientArgs extends ReferralStateManagerArgs {
59
51
 
60
52
  export abstract class SolautoClient extends ReferralStateManager {
61
53
  public lendingPlatform!: LendingPlatform;
54
+ public lpEnv!: ProgramEnv;
62
55
 
63
56
  public authority!: PublicKey;
64
57
 
@@ -109,6 +102,7 @@ export abstract class SolautoClient extends ReferralStateManager {
109
102
  lendingPool: args.lendingPool,
110
103
  lpUserAccount: args.lpUserAccount,
111
104
  lendingPlatform: this.lendingPlatform,
105
+ lpEnv: this.lpEnv,
112
106
  },
113
107
  this.contextUpdates
114
108
  );
@@ -153,7 +147,8 @@ export abstract class SolautoClient extends ReferralStateManager {
153
147
  this.signer,
154
148
  this.authority,
155
149
  this.pos.supplyMint(),
156
- this.pos.debtMint()
150
+ this.pos.debtMint(),
151
+ this.lpEnv
157
152
  );
158
153
  await this.flProvider.initialize();
159
154
  this.otherSigners.push(...this.flProvider.otherSigners());
@@ -1,4 +1,4 @@
1
- import { toWeb3JsPublicKey } from "@metaplex-foundation/umi-web3js-adapters";
1
+ import { PublicKey, SYSVAR_INSTRUCTIONS_PUBKEY } from "@solana/web3.js";
2
2
  import {
3
3
  Signer,
4
4
  TransactionBuilder,
@@ -6,13 +6,9 @@ import {
6
6
  PublicKey as UmiPublicKey,
7
7
  createSignerFromKeypair,
8
8
  } from "@metaplex-foundation/umi";
9
- import { PublicKey, SYSVAR_INSTRUCTIONS_PUBKEY } from "@solana/web3.js";
10
- import { SolautoClient, SolautoClientArgs } from "./solautoClient";
11
- import { MarginfiAssetAccounts } from "../../types/accounts";
12
- import {
13
- MARGINFI_ACCOUNTS,
14
- MARGINFI_ACCOUNTS_LOOKUP_TABLE,
15
- } from "../../constants/marginfiAccounts";
9
+ import { toWeb3JsPublicKey } from "@metaplex-foundation/umi-web3js-adapters";
10
+ import { MarginfiAssetAccounts, RebalanceDetails } from "../../types";
11
+ import { getMarginfiAccounts, MarginfiProgramAccounts } from "../../constants";
16
12
  import {
17
13
  DCASettingsInpArgs,
18
14
  LendingPlatform,
@@ -29,9 +25,13 @@ import {
29
25
  marginfiRebalance,
30
26
  marginfiRefreshData,
31
27
  } from "../../generated";
32
- import { getTokenAccount } from "../../utils/accountUtils";
33
28
  import {
34
- MARGINFI_PROGRAM_ID,
29
+ getAllMarginfiAccountsByAuthority,
30
+ marginfiAccountEmpty,
31
+ getTokenAccount,
32
+ hasFirstRebalance,
33
+ } from "../../utils";
34
+ import {
35
35
  lendingAccountBorrow,
36
36
  lendingAccountDeposit,
37
37
  lendingAccountRepay,
@@ -39,17 +39,12 @@ import {
39
39
  marginfiAccountInitialize,
40
40
  safeFetchAllMarginfiAccount,
41
41
  } from "../../marginfi-sdk";
42
- import {
43
- getAllMarginfiAccountsByAuthority,
44
- marginfiAccountEmpty,
45
- } from "../../utils/marginfiUtils";
46
- import { hasFirstRebalance } from "../../utils/solautoUtils";
47
- import { RebalanceDetails } from "../../types";
42
+ import { SolautoClient, SolautoClientArgs } from "./solautoClient";
48
43
 
49
44
  export class SolautoMarginfiClient extends SolautoClient {
50
45
  public lendingPlatform = LendingPlatform.Marginfi;
51
46
 
52
- public marginfiProgram!: PublicKey;
47
+ public mfiAccounts!: MarginfiProgramAccounts;
53
48
 
54
49
  public marginfiAccount!: PublicKey | Signer;
55
50
  public marginfiAccountPk!: PublicKey;
@@ -64,6 +59,8 @@ export class SolautoMarginfiClient extends SolautoClient {
64
59
  async initialize(args: SolautoClientArgs) {
65
60
  await super.initialize(args);
66
61
 
62
+ this.mfiAccounts = getMarginfiAccounts(this.lpEnv);
63
+
67
64
  this.marginfiGroup = await this.pos.lendingPool();
68
65
 
69
66
  if (this.selfManaged) {
@@ -108,11 +105,11 @@ export class SolautoMarginfiClient extends SolautoClient {
108
105
  }
109
106
 
110
107
  this.marginfiSupplyAccounts =
111
- MARGINFI_ACCOUNTS[this.marginfiGroup.toString()][
108
+ this.mfiAccounts.bankAccounts[this.marginfiGroup.toString()][
112
109
  this.pos.supplyMint().toString()
113
110
  ]!;
114
111
  this.marginfiDebtAccounts =
115
- MARGINFI_ACCOUNTS[this.marginfiGroup.toString()][
112
+ this.mfiAccounts.bankAccounts[this.marginfiGroup.toString()][
116
113
  this.pos.debtMint().toString()
117
114
  ]!;
118
115
 
@@ -132,7 +129,10 @@ export class SolautoMarginfiClient extends SolautoClient {
132
129
  }
133
130
 
134
131
  defaultLookupTables(): string[] {
135
- return [MARGINFI_ACCOUNTS_LOOKUP_TABLE, ...super.defaultLookupTables()];
132
+ return [
133
+ this.mfiAccounts.lookupTable.toString(),
134
+ ...super.defaultLookupTables(),
135
+ ];
136
136
  }
137
137
 
138
138
  lutAccountsToAdd(): PublicKey[] {
@@ -169,7 +169,7 @@ export class SolautoMarginfiClient extends SolautoClient {
169
169
 
170
170
  return marginfiOpenPosition(this.umi, {
171
171
  signer: this.signer,
172
- marginfiProgram: publicKey(MARGINFI_PROGRAM_ID),
172
+ marginfiProgram: publicKey(this.mfiAccounts.program),
173
173
  signerReferralState: publicKey(this.referralState),
174
174
  referredByState: this.referredByState
175
175
  ? publicKey(this.referredByState)
@@ -214,7 +214,7 @@ export class SolautoMarginfiClient extends SolautoClient {
214
214
  refreshIx(): TransactionBuilder {
215
215
  return marginfiRefreshData(this.umi, {
216
216
  signer: this.signer,
217
- marginfiProgram: publicKey(MARGINFI_PROGRAM_ID),
217
+ marginfiProgram: publicKey(this.mfiAccounts.program),
218
218
  marginfiGroup: publicKey(this.marginfiGroup),
219
219
  marginfiAccount: publicKey(this.marginfiAccount),
220
220
  supplyBank: publicKey(this.marginfiSupplyAccounts.bank),
@@ -342,7 +342,7 @@ export class SolautoMarginfiClient extends SolautoClient {
342
342
 
343
343
  return marginfiProtocolInteraction(this.umi, {
344
344
  signer: this.signer,
345
- marginfiProgram: publicKey(MARGINFI_PROGRAM_ID),
345
+ marginfiProgram: publicKey(this.mfiAccounts.program),
346
346
  solautoPosition: publicKey(this.pos.publicKey),
347
347
  marginfiGroup: publicKey(this.marginfiGroup),
348
348
  marginfiAccount: publicKey(this.marginfiAccountPk),
@@ -393,7 +393,7 @@ export class SolautoMarginfiClient extends SolautoClient {
393
393
 
394
394
  return marginfiRebalance(this.umi, {
395
395
  signer: this.signer,
396
- marginfiProgram: publicKey(MARGINFI_PROGRAM_ID),
396
+ marginfiProgram: publicKey(this.mfiAccounts.program),
397
397
  ixsSysvar: publicKey(SYSVAR_INSTRUCTIONS_PUBKEY),
398
398
  solautoFeesTa: publicKey(
399
399
  data.values.rebalanceDirection === RebalanceDirection.Boost
@@ -425,9 +425,7 @@ export class SolautoMarginfiClient extends SolautoClient {
425
425
  supplyPriceOracle: publicKey(this.supplyPriceOracle),
426
426
  positionSupplyTa: publicKey(this.positionSupplyTa),
427
427
  authoritySupplyTa: addAuthorityTas
428
- ? publicKey(
429
- getTokenAccount(this.authority, this.pos.supplyMint())
430
- )
428
+ ? publicKey(getTokenAccount(this.authority, this.pos.supplyMint()))
431
429
  : undefined,
432
430
  vaultSupplyTa: needSupplyAccounts
433
431
  ? publicKey(this.marginfiSupplyAccounts.liquidityVault)
@@ -439,9 +437,7 @@ export class SolautoMarginfiClient extends SolautoClient {
439
437
  debtPriceOracle: publicKey(this.debtPriceOracle),
440
438
  positionDebtTa: publicKey(this.positionDebtTa),
441
439
  authorityDebtTa: addAuthorityTas
442
- ? publicKey(
443
- getTokenAccount(this.authority, this.pos.debtMint())
444
- )
440
+ ? publicKey(getTokenAccount(this.authority, this.pos.debtMint()))
445
441
  : undefined,
446
442
  vaultDebtTa: needDebtAccounts
447
443
  ? publicKey(this.marginfiDebtAccounts.liquidityVault)
@@ -1,11 +1,12 @@
1
- import { Signer, signerIdentity, Umi } from "@metaplex-foundation/umi";
2
1
  import { Connection, PublicKey } from "@solana/web3.js";
3
- import { consoleLog, getSolanaRpcConnection } from "../../utils";
4
- import { SOLAUTO_PROD_PROGRAM } from "../../constants";
2
+ import { Signer, signerIdentity, Umi } from "@metaplex-foundation/umi";
5
3
  import {
6
4
  WalletAdapter,
7
5
  walletAdapterIdentity,
8
6
  } from "@metaplex-foundation/umi-signer-wallet-adapters";
7
+ import { consoleLog, getSolanaRpcConnection } from "../../utils";
8
+ import { SOLAUTO_PROD_PROGRAM } from "../../constants";
9
+ import { ProgramEnv } from "../../types";
9
10
 
10
11
  export interface TxHandlerProps {
11
12
  signer?: Signer;
@@ -13,13 +14,14 @@ export interface TxHandlerProps {
13
14
  rpcUrl: string;
14
15
  showLogs?: boolean;
15
16
  programId?: PublicKey;
16
- wsEndpoint?: string;
17
+ lpEnv?: ProgramEnv;
17
18
  }
18
19
 
19
20
  export abstract class TxHandler {
20
21
  public rpcUrl!: string;
21
22
  public showLogs = false;
22
- public programId = SOLAUTO_PROD_PROGRAM;
23
+ public programId!: PublicKey;
24
+ public lpEnv!: ProgramEnv;
23
25
 
24
26
  public connection!: Connection;
25
27
  public umi!: Umi;
@@ -27,15 +29,14 @@ export abstract class TxHandler {
27
29
  public otherSigners: Signer[] = [];
28
30
 
29
31
  constructor(props: TxHandlerProps) {
30
- if (props.programId !== undefined) {
31
- this.programId = props.programId;
32
- }
32
+ this.programId = props.programId ?? SOLAUTO_PROD_PROGRAM;
33
+ this.lpEnv = props.lpEnv ?? "Prod";
33
34
 
34
35
  this.rpcUrl = props.rpcUrl;
35
36
  const [connection, umi] = getSolanaRpcConnection(
36
37
  this.rpcUrl,
37
38
  this.programId,
38
- props.wsEndpoint
39
+ this.lpEnv
39
40
  );
40
41
  this.connection = connection;
41
42
  this.umi = umi;
@@ -1,24 +1,25 @@
1
+ import { PublicKey } from "@solana/web3.js";
1
2
  import {
2
3
  Signer,
3
4
  TransactionBuilder,
4
5
  transactionBuilder,
5
6
  } from "@metaplex-foundation/umi";
7
+ import { toWeb3JsPublicKey } from "@metaplex-foundation/umi-web3js-adapters";
6
8
  import {
7
9
  createJupiterApiClient,
8
10
  QuoteResponse,
9
11
  SwapInstructionsResponse,
10
12
  } from "@jup-ag/api";
11
- import { getWrappedInstruction } from "../../utils/solanaUtils";
12
- import { fromBps, toBps } from "../../utils/numberUtils";
13
- import { getTokenAccount } from "../../utils/accountUtils";
14
- import { PublicKey } from "@solana/web3.js";
15
13
  import {
16
14
  consoleLog,
17
- jupIxToSolanaIx,
18
15
  retryWithExponentialBackoff,
16
+ getWrappedInstruction,
17
+ fromBps,
18
+ toBps,
19
+ getTokenAccount,
20
+ jupIxToSolanaIx,
19
21
  } from "../../utils";
20
22
  import { TransactionItemInputs } from "../../types";
21
- import { toWeb3JsPublicKey } from "@metaplex-foundation/umi-web3js-adapters";
22
23
 
23
24
  export interface SwapInput {
24
25
  inputMint: PublicKey;
@@ -1,3 +1,8 @@
1
+ import { PublicKey, SYSVAR_INSTRUCTIONS_PUBKEY } from "@solana/web3.js";
2
+ import {
3
+ ACCOUNT_SIZE as TOKEN_ACCOUNT_SIZE,
4
+ NATIVE_MINT,
5
+ } from "@solana/spl-token";
1
6
  import {
2
7
  Instruction,
3
8
  ProgramError,
@@ -8,11 +13,6 @@ import {
8
13
  transactionBuilder,
9
14
  } from "@metaplex-foundation/umi";
10
15
  import { toWeb3JsPublicKey } from "@metaplex-foundation/umi-web3js-adapters";
11
- import { PublicKey, SYSVAR_INSTRUCTIONS_PUBKEY } from "@solana/web3.js";
12
- import {
13
- ACCOUNT_SIZE as TOKEN_ACCOUNT_SIZE,
14
- NATIVE_MINT,
15
- } from "@solana/spl-token";
16
16
  import {
17
17
  InvalidRebalanceConditionError,
18
18
  SolautoAction,
@@ -27,19 +27,20 @@ import {
27
27
  isSolautoAction,
28
28
  solautoAction,
29
29
  } from "../../generated";
30
- import { SolautoClient } from "../solauto/solautoClient";
30
+ import { SolautoClient, ReferralStateManager } from "../solauto";
31
31
  import {
32
32
  closeTokenAccountUmiIx,
33
33
  createAssociatedTokenAccountUmiIx,
34
34
  systemTransferUmiIx,
35
- } from "../../utils/solanaUtils";
36
- import {
37
35
  consoleLog,
38
36
  getSolanaAccountCreated,
39
37
  rpcAccountCreated,
40
- } from "../../utils/generalUtils";
41
- import { uint8ArrayToBigInt } from "../../utils/numberUtils";
42
- import { getTokenAccount, getTokenAccountData } from "../../utils/accountUtils";
38
+ uint8ArrayToBigInt,
39
+ getTokenAccount,
40
+ getTokenAccountData,
41
+ isMarginfiClient,
42
+ isMarginfiProgram,
43
+ } from "../../utils";
43
44
  import {
44
45
  createMarginfiProgram,
45
46
  getLendingAccountBorrowInstructionDataSerializer,
@@ -47,17 +48,14 @@ import {
47
48
  getLendingAccountRepayInstructionDataSerializer,
48
49
  getLendingAccountWithdrawInstructionDataSerializer,
49
50
  getMarginfiErrorFromCode,
50
- MARGINFI_PROGRAM_ID,
51
51
  } from "../../marginfi-sdk";
52
- import { JupSwapManager, ReferralStateManager } from "..";
52
+ import { JupSwapManager } from "../swap";
53
53
  import {
54
54
  createJupiterProgram,
55
55
  getJupiterErrorFromCode,
56
56
  JUPITER_PROGRAM_ID,
57
57
  } from "../../jupiter-sdk";
58
- import { TransactionItemInputs } from "../../types";
59
- import { isMarginfiClient } from "../../utils";
60
- import { BundleSimulationError } from "../../types/transactions";
58
+ import { TransactionItemInputs, BundleSimulationError } from "../../types";
61
59
 
62
60
  interface wSolTokenUsage {
63
61
  wSolTokenAccount: PublicKey;
@@ -410,7 +408,7 @@ function getSolautoActions(umi: Umi, tx: TransactionBuilder): SolautoAction[] {
410
408
  } catch {}
411
409
  }
412
410
 
413
- if (x.programId === MARGINFI_PROGRAM_ID) {
411
+ if (isMarginfiProgram(toWeb3JsPublicKey(x.programId))) {
414
412
  try {
415
413
  const serializer = getLendingAccountDepositInstructionDataSerializer();
416
414
  const discriminator = uint8ArrayToBigInt(
@@ -680,7 +678,8 @@ export function getErrorInfo(
680
678
  }
681
679
  } else if (
682
680
  errCode !== undefined &&
683
- errIx?.programId === MARGINFI_PROGRAM_ID
681
+ errIx &&
682
+ isMarginfiProgram(toWeb3JsPublicKey(errIx.programId))
684
683
  ) {
685
684
  programName = "Marginfi";
686
685
  programError = marginfiError;
@@ -1,37 +1,38 @@
1
1
  import bs58 from "bs58";
2
+ import {
3
+ PublicKey,
4
+ TransactionExpiredBlockheightExceededError,
5
+ } from "@solana/web3.js";
2
6
  import {
3
7
  AddressLookupTableInput,
4
8
  transactionBuilder,
5
9
  TransactionBuilder,
6
10
  Umi,
7
11
  } from "@metaplex-foundation/umi";
8
- import { SolautoClient } from "../solauto/solautoClient";
9
- import {
10
- addTxOptimizations,
11
- getAddressLookupInputs,
12
- sendSingleOptimizedTransaction,
13
- } from "../../utils/solanaUtils";
14
- import {
15
- consoleLog,
16
- ErrorsToThrow,
17
- retryWithExponentialBackoff,
18
- } from "../../utils/generalUtils";
19
- import { getErrorInfo, getTransactionChores } from "./transactionUtils";
20
12
  import {
21
13
  PriorityFeeSetting,
22
14
  priorityFeeSettingValues,
23
15
  TransactionItemInputs,
24
16
  TransactionRunType,
25
17
  } from "../../types";
26
- import { ReferralStateManager, TxHandler } from "..";
27
18
  import {
28
- PublicKey,
29
- TransactionExpiredBlockheightExceededError,
30
- } from "@solana/web3.js";
31
- import { SWITCHBOARD_PRICE_FEED_IDS } from "../../constants/switchboardConstants";
32
- import { buildSwbSubmitResponseTx, getSwitchboardFeedData } from "../../utils";
33
- import { sendJitoBundledTransactions } from "../../utils/jitoUtils";
34
- import { SOLAUTO_PROD_PROGRAM, SOLAUTO_TEST_PROGRAM } from "../../constants";
19
+ SOLAUTO_PROD_PROGRAM,
20
+ SOLAUTO_TEST_PROGRAM,
21
+ SWITCHBOARD_PRICE_FEED_IDS,
22
+ } from "../../constants";
23
+ import {
24
+ consoleLog,
25
+ ErrorsToThrow,
26
+ retryWithExponentialBackoff,
27
+ addTxOptimizations,
28
+ getAddressLookupInputs,
29
+ sendSingleOptimizedTransaction,
30
+ buildSwbSubmitResponseTx,
31
+ getSwitchboardFeedData,
32
+ sendJitoBundledTransactions,
33
+ } from "../../utils";
34
+ import { SolautoClient, ReferralStateManager, TxHandler } from "../solauto";
35
+ import { getErrorInfo, getTransactionChores } from "./transactionUtils";
35
36
 
36
37
  const CHORES_TX_NAME = "account chores";
37
38
  const MAX_SUPPORTED_ACCOUNT_LOCKS = 64;
@@ -257,7 +258,7 @@ export class TransactionsManager {
257
258
  private statusCallback?: (statuses: TransactionManagerStatuses) => void,
258
259
  private txType?: TransactionRunType,
259
260
  private priorityFeeSetting: PriorityFeeSetting = PriorityFeeSetting.Min,
260
- private atomically: boolean = false,
261
+ private atomically: boolean = true,
261
262
  private errorsToThrow?: ErrorsToThrow,
262
263
  retryConfig?: RetryConfig
263
264
  ) {
@@ -266,7 +267,7 @@ export class TransactionsManager {
266
267
  this.txHandler.umi
267
268
  );
268
269
  this.signableRetries =
269
- retryConfig?.signableRetries ?? retryConfig?.totalRetries ?? 3;
270
+ retryConfig?.signableRetries ?? retryConfig?.totalRetries ?? 4;
270
271
  this.totalRetries =
271
272
  retryConfig?.totalRetries ?? retryConfig?.signableRetries ?? 4;
272
273
  this.retryDelay = retryConfig?.retryDelay ?? 150;