@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,5 +1,4 @@
1
1
  import { PublicKey } from "@solana/web3.js";
2
- import { SolautoPositionEx } from "./solautoPositionEx";
3
2
  import {
4
3
  Bank,
5
4
  fetchMarginfiAccount,
@@ -14,9 +13,9 @@ import {
14
13
  fromBaseUnit,
15
14
  getBankLiquidityAvailableBaseUnit,
16
15
  getMarginfiAccountPositionState,
17
- toBps,
18
16
  } from "../utils";
19
- import { DEFAULT_MARGINFI_GROUP, MARGINFI_ACCOUNTS } from "../constants";
17
+ import { getMarginfiAccounts } from "../constants";
18
+ import { SolautoPositionEx } from "./solautoPositionEx";
20
19
 
21
20
  export class MarginfiSolautoPositionEx extends SolautoPositionEx {
22
21
  private marginfiAccountData: MarginfiAccount | null = null;
@@ -42,7 +41,7 @@ export class MarginfiSolautoPositionEx extends SolautoPositionEx {
42
41
  }
43
42
 
44
43
  if (!this.lp) {
45
- this.lp = new PublicKey(DEFAULT_MARGINFI_GROUP);
44
+ this.lp = getMarginfiAccounts(this.lpEnv).defaultGroup;
46
45
  }
47
46
 
48
47
  return this.lp;
@@ -51,10 +50,9 @@ export class MarginfiSolautoPositionEx extends SolautoPositionEx {
51
50
  async maxLtvAndLiqThresholdBps(): Promise<[number, number]> {
52
51
  if (!this.supplyBank || !this.debtBank) {
53
52
  const group = (await this.lendingPool()).toString();
54
- const supplyBank =
55
- MARGINFI_ACCOUNTS[group][this.supplyMint().toString()].bank;
56
- const debtBank =
57
- MARGINFI_ACCOUNTS[group][this.debtMint().toString()].bank;
53
+ const bankAccounts = getMarginfiAccounts(this.lpEnv).bankAccounts;
54
+ const supplyBank = bankAccounts[group][this.supplyMint().toString()].bank;
55
+ const debtBank = bankAccounts[group][this.debtMint().toString()].bank;
58
56
 
59
57
  [this.supplyBank, this.debtBank] = await safeFetchAllBank(this.umi, [
60
58
  publicKey(supplyBank),
@@ -95,6 +93,7 @@ export class MarginfiSolautoPositionEx extends SolautoPositionEx {
95
93
  useDesignatedMint
96
94
  ? { mint: toWeb3JsPublicKey(this.state().debt.mint) }
97
95
  : undefined,
96
+ this.lpEnv,
98
97
  this.contextUpdates
99
98
  );
100
99
 
@@ -1,4 +1,9 @@
1
1
  import { PublicKey } from "@solana/web3.js";
2
+ import { Umi } from "@metaplex-foundation/umi";
3
+ import {
4
+ fromWeb3JsPublicKey,
5
+ toWeb3JsPublicKey,
6
+ } from "@metaplex-foundation/umi-web3js-adapters";
2
7
  import {
3
8
  DCASettings,
4
9
  fetchSolautoPosition,
@@ -7,7 +12,6 @@ import {
7
12
  SolautoPosition,
8
13
  SolautoSettingsParameters,
9
14
  } from "../generated";
10
- import { Umi } from "@metaplex-foundation/umi";
11
15
  import {
12
16
  calcDebtUsd,
13
17
  calcNetWorth,
@@ -33,17 +37,13 @@ import {
33
37
  tokenInfo,
34
38
  toRoundedUsdValue,
35
39
  } from "../utils";
36
- import { RebalanceAction } from "../types";
40
+ import { ProgramEnv, RebalanceAction } from "../types";
37
41
  import {
38
42
  getDebtAdjustment,
39
43
  getRebalanceValues,
40
44
  SolautoFeesBps,
41
45
  } from "../services/rebalance";
42
46
  import { MIN_POSITION_STATE_FRESHNESS_SECS, TokenInfo } from "../constants";
43
- import {
44
- fromWeb3JsPublicKey,
45
- toWeb3JsPublicKey,
46
- } from "@metaplex-foundation/umi-web3js-adapters";
47
47
 
48
48
  export interface PositionCustomArgs {
49
49
  lendingPlatform: LendingPlatform;
@@ -51,6 +51,7 @@ export interface PositionCustomArgs {
51
51
  debtMint?: PublicKey;
52
52
  lendingPool?: PublicKey;
53
53
  lpUserAccount?: PublicKey;
54
+ lpEnv?: ProgramEnv;
54
55
  }
55
56
 
56
57
  interface SolautoPositionExData extends Partial<SolautoPosition> {
@@ -70,6 +71,7 @@ export abstract class SolautoPositionEx {
70
71
  public publicKey!: PublicKey;
71
72
  protected _data!: SolautoPositionExData;
72
73
  protected lp?: PublicKey = undefined;
74
+ protected lpEnv!: ProgramEnv;
73
75
  public lpUserAccount?: PublicKey = undefined;
74
76
  protected contextUpdates?: ContextUpdates;
75
77
 
@@ -86,6 +88,7 @@ export abstract class SolautoPositionEx {
86
88
  (args.data.position
87
89
  ? toWeb3JsPublicKey(args.data.position!.protocolUserAccount)
88
90
  : undefined);
91
+ this.lpEnv = args.customArgs?.lpEnv ?? "Prod";
89
92
 
90
93
  this._data = args.data;
91
94
  this.firstState = { ...args.data.state };
@@ -183,7 +186,10 @@ export abstract class SolautoPositionEx {
183
186
  }
184
187
 
185
188
  supplyUsd() {
186
- return calcSupplyUsd(this.state());
189
+ const supplyPrice = safeGetPrice(this.supplyMint());
190
+ return supplyPrice
191
+ ? calcTotalSupply(this.state()) * supplyPrice
192
+ : calcSupplyUsd(this.state());
187
193
  }
188
194
 
189
195
  totalDebt() {
@@ -191,7 +197,10 @@ export abstract class SolautoPositionEx {
191
197
  }
192
198
 
193
199
  debtUsd() {
194
- return calcDebtUsd(this.state());
200
+ const debtPrice = safeGetPrice(this.debtMint());
201
+ return debtPrice
202
+ ? calcTotalDebt(this.state()) * debtPrice
203
+ : calcDebtUsd(this.state());
195
204
  }
196
205
 
197
206
  supplyLiquidityDepositable() {
@@ -203,7 +212,9 @@ export abstract class SolautoPositionEx {
203
212
  }
204
213
 
205
214
  supplyLiquidityUsdAvailable() {
206
- return this.supplyLiquidityAvailable() * (safeGetPrice(this.supplyMint()) ?? 0);
215
+ return (
216
+ this.supplyLiquidityAvailable() * (safeGetPrice(this.supplyMint()) ?? 0)
217
+ );
207
218
  }
208
219
 
209
220
  debtLiquidityAvailable() {
@@ -244,7 +255,7 @@ export abstract class SolautoPositionEx {
244
255
  }
245
256
 
246
257
  eligibleForRebalance(bpsDistanceThreshold = 0): RebalanceAction | undefined {
247
- if (!this.settings() || !calcSupplyUsd(this.state())) {
258
+ if (!this.settings() || !this.supplyUsd()) {
248
259
  return undefined;
249
260
  }
250
261
 
@@ -1,4 +1,9 @@
1
+ import { PublicKey } from "@solana/web3.js";
1
2
  import { Umi } from "@metaplex-foundation/umi";
3
+ import {
4
+ fromWeb3JsPublicKey,
5
+ toWeb3JsPublicKey,
6
+ } from "@metaplex-foundation/umi-web3js-adapters";
2
7
  import {
3
8
  LendingPlatform,
4
9
  PositionState,
@@ -7,13 +12,6 @@ import {
7
12
  SolautoSettingsParameters,
8
13
  SolautoSettingsParametersInpArgs,
9
14
  } from "../generated";
10
- import { PublicKey } from "@solana/web3.js";
11
- import { PositionCustomArgs, SolautoPositionEx } from "./solautoPositionEx";
12
- import {
13
- fromWeb3JsPublicKey,
14
- toWeb3JsPublicKey,
15
- } from "@metaplex-foundation/umi-web3js-adapters";
16
- import { MarginfiSolautoPositionEx } from "./marginfiSolautoPositionEx";
17
15
  import {
18
16
  ContextUpdates,
19
17
  currentUnixSeconds,
@@ -24,6 +22,8 @@ import {
24
22
  tokenInfo,
25
23
  toRoundedUsdValue,
26
24
  } from "../utils";
25
+ import { PositionCustomArgs, SolautoPositionEx } from "./solautoPositionEx";
26
+ import { MarginfiSolautoPositionEx } from "./marginfiSolautoPositionEx";
27
27
 
28
28
  export function createSolautoSettings(
29
29
  settings: SolautoSettingsParametersInpArgs
@@ -61,3 +61,5 @@ export interface RebalanceDetails {
61
61
  swapQuote: QuoteResponse;
62
62
  targetLiqUtilizationRateBps?: number;
63
63
  }
64
+
65
+ export type ProgramEnv = "Prod" | "Staging";
@@ -1,7 +1,15 @@
1
+ import axios from "axios";
1
2
  import { PublicKey } from "@solana/web3.js";
2
3
  import { MaybeRpcAccount, publicKey, Umi } from "@metaplex-foundation/umi";
3
4
  import { TOKEN_INFO, TokenInfo } from "../constants";
4
- import axios from "axios";
5
+
6
+ export function buildHeliusApiUrl(heliusApiKey: string) {
7
+ return `https://mainnet.helius-rpc.com/?api-key=${heliusApiKey}`;
8
+ }
9
+
10
+ export function buildIronforgeApiUrl(ironforgeApiKey: string) {
11
+ return `https://rpc.ironforge.network/mainnet?apiKey=${ironforgeApiKey}`;
12
+ }
5
13
 
6
14
  export function consoleLog(...args: any[]): void {
7
15
  if ((globalThis as any).SHOW_LOGS) {
@@ -1,5 +1,6 @@
1
1
  export * from "./accountUtils";
2
2
  export * from "./generalUtils";
3
+ export * from "./jitoUtils";
3
4
  export * from "./jupiterUtils";
4
5
  export * from "./marginfiUtils";
5
6
  export * from "./numberUtils";
@@ -5,8 +5,6 @@ import {
5
5
  TransactionExpiredBlockheightExceededError,
6
6
  VersionedTransaction,
7
7
  } from "@solana/web3.js";
8
- import { toWeb3JsTransaction } from "@metaplex-foundation/umi-web3js-adapters";
9
- import { JITO_TIP_ACCOUNTS } from "../constants/solautoConstants";
10
8
  import {
11
9
  Signer,
12
10
  TransactionBuilder,
@@ -14,6 +12,10 @@ import {
14
12
  WrappedInstruction,
15
13
  TransactionMessage,
16
14
  } from "@metaplex-foundation/umi";
15
+ import { toWeb3JsTransaction } from "@metaplex-foundation/umi-web3js-adapters";
16
+ import { JITO_TIP_ACCOUNTS } from "../constants";
17
+ import { PriorityFeeSetting, TransactionRunType } from "../types";
18
+ import { BundleSimulationError } from "../types";
17
19
  import {
18
20
  assembleFinalTransaction,
19
21
  getComputeUnitPriceEstimate,
@@ -25,10 +27,7 @@ import {
25
27
  customRpcCall,
26
28
  retryWithExponentialBackoff,
27
29
  } from "./generalUtils";
28
- import { PriorityFeeSetting, TransactionRunType } from "../types";
29
30
  import base58 from "bs58";
30
- import { BundleSimulationError } from "../types/transactions";
31
- import { bs58 } from "@coral-xyz/anchor/dist/cjs/utils/bytes";
32
31
 
33
32
  export function getRandomTipAccount(): PublicKey {
34
33
  const randomInt = Math.floor(Math.random() * JITO_TIP_ACCOUNTS.length);
@@ -190,14 +189,18 @@ async function pollBundleStatus(
190
189
  while (Date.now() < endTime) {
191
190
  await new Promise((resolve) => setTimeout(resolve, interval));
192
191
 
193
- const statuses = await retryWithExponentialBackoff(async () => {
194
- const resp = await getBundleStatus(umi, bundleId);
195
- // TODO: remove me
196
- if (resp?.value?.length > 0 && resp.value[0] === null) {
197
- throw new Error("No confirmation status");
198
- }
199
- return resp;
200
- }, 3, 250);
192
+ const statuses = await retryWithExponentialBackoff(
193
+ async () => {
194
+ const resp = await getBundleStatus(umi, bundleId);
195
+ // TODO: remove me
196
+ if (resp?.value?.length > 0 && resp.value[0] === null) {
197
+ throw new Error("No confirmation status");
198
+ }
199
+ return resp;
200
+ },
201
+ 3,
202
+ 250
203
+ );
201
204
 
202
205
  if (statuses?.value?.length > 0) {
203
206
  consoleLog("Statuses:", statuses);
@@ -283,7 +286,7 @@ export async function sendJitoBundledTransactions(
283
286
  priorityFeeSetting,
284
287
  onAwaitingSign
285
288
  );
286
- return resp ? [bs58.encode(resp)] : undefined;
289
+ return resp ? [base58.encode(resp)] : undefined;
287
290
  }
288
291
 
289
292
  consoleLog("Sending Jito bundle...");
@@ -1,6 +1,6 @@
1
1
  import { PublicKey, TransactionInstruction } from "@solana/web3.js";
2
- import { retryWithExponentialBackoff } from "./generalUtils";
3
2
  import { Instruction } from "@jup-ag/api";
3
+ import { retryWithExponentialBackoff } from "./generalUtils";
4
4
 
5
5
  export function jupIxToSolanaIx(
6
6
  instruction: Instruction
@@ -1,16 +1,33 @@
1
1
  import { PublicKey } from "@solana/web3.js";
2
- import { publicKey, Umi } from "@metaplex-foundation/umi";
3
- import { toWeb3JsPublicKey } from "@metaplex-foundation/umi-web3js-adapters";
2
+ import { Program, publicKey, Umi } from "@metaplex-foundation/umi";
3
+ import {
4
+ fromWeb3JsPublicKey,
5
+ toWeb3JsPublicKey,
6
+ } from "@metaplex-foundation/umi-web3js-adapters";
7
+ import { ProgramEnv, MarginfiAssetAccounts } from "../types";
8
+ import { PositionState, PositionTokenState } from "../generated";
9
+ import {
10
+ ALL_SUPPORTED_TOKENS,
11
+ getMarginfiAccounts,
12
+ MARGINFI_PROD_PROGRAM,
13
+ MARGINFI_STAGING_PROGRAM,
14
+ MarginfiAccountsMap,
15
+ TOKEN_INFO,
16
+ USD_DECIMALS,
17
+ } from "../constants";
4
18
  import {
5
19
  Bank,
6
20
  deserializeMarginfiAccount,
7
21
  getMarginfiAccountSize,
8
- MARGINFI_PROGRAM_ID,
22
+ getMarginfiErrorFromCode,
23
+ getMarginfiErrorFromName,
9
24
  MarginfiAccount,
10
25
  OracleSetup,
11
26
  safeFetchBank,
12
27
  safeFetchMarginfiAccount,
13
28
  } from "../marginfi-sdk";
29
+ import { ContextUpdates } from "./solautoUtils";
30
+ import { fetchTokenPrices, safeGetPrice } from "./priceUtils";
14
31
  import { currentUnixSeconds } from "./generalUtils";
15
32
  import {
16
33
  bytesToI80F48,
@@ -20,16 +37,57 @@ import {
20
37
  toBaseUnit,
21
38
  toBps,
22
39
  } from "./numberUtils";
23
- import {
24
- DEFAULT_MARGINFI_GROUP,
25
- MARGINFI_ACCOUNTS,
26
- } from "../constants/marginfiAccounts";
27
- import { MarginfiAssetAccounts } from "../types/accounts";
28
- import { PositionState, PositionTokenState } from "../generated";
29
- import { USD_DECIMALS } from "../constants/generalAccounts";
30
- import { ContextUpdates } from "./solautoUtils";
31
- import { ALL_SUPPORTED_TOKENS, TOKEN_INFO } from "../constants";
32
- import { fetchTokenPrices, safeGetPrice } from "./priceUtils";
40
+ import { getTokenAccountData } from "./accountUtils";
41
+
42
+ export function getMarginfiProgram(env: ProgramEnv) {
43
+ return env === "Prod" ? MARGINFI_PROD_PROGRAM : MARGINFI_STAGING_PROGRAM;
44
+ }
45
+
46
+ export function isMarginfiProgram(programId: PublicKey) {
47
+ return (
48
+ programId.equals(MARGINFI_PROD_PROGRAM) ||
49
+ programId.equals(MARGINFI_STAGING_PROGRAM)
50
+ );
51
+ }
52
+
53
+ export function createDynamicMarginfiProgram(env?: ProgramEnv): Program {
54
+ return {
55
+ name: "marginfi",
56
+ publicKey: publicKey(getMarginfiProgram(env ?? "Prod")),
57
+ getErrorFromCode(code: number, cause?: Error) {
58
+ return getMarginfiErrorFromCode(code, this, cause);
59
+ },
60
+ getErrorFromName(name: string, cause?: Error) {
61
+ return getMarginfiErrorFromName(name, this, cause);
62
+ },
63
+ isOnCluster() {
64
+ return true;
65
+ },
66
+ };
67
+ }
68
+
69
+ export function umiWithMarginfiProgram(umi: Umi, marginfiEnv?: ProgramEnv) {
70
+ return umi.use({
71
+ install(umi) {
72
+ umi.programs.add(
73
+ createDynamicMarginfiProgram(marginfiEnv ?? "Prod"),
74
+ false
75
+ );
76
+ },
77
+ });
78
+ }
79
+
80
+ export async function fetchBankAddresses(umi: Umi, bankPk: PublicKey) {
81
+ const bank = await safeFetchBank(umi, fromWeb3JsPublicKey(bankPk));
82
+ const liquidityVault = toWeb3JsPublicKey(bank!.liquidityVault);
83
+ const vaultAuthority = (await getTokenAccountData(umi, liquidityVault))
84
+ ?.owner;
85
+ return {
86
+ bank: bankPk,
87
+ liquidityVault,
88
+ vaultAuthority,
89
+ };
90
+ }
33
91
 
34
92
  interface AllMarginfiAssetAccounts extends MarginfiAssetAccounts {
35
93
  mint: PublicKey;
@@ -38,16 +96,29 @@ interface AllMarginfiAssetAccounts extends MarginfiAssetAccounts {
38
96
  export function findMarginfiAccounts(
39
97
  bank: PublicKey
40
98
  ): AllMarginfiAssetAccounts {
41
- for (const group in MARGINFI_ACCOUNTS) {
42
- for (const key in MARGINFI_ACCOUNTS[group]) {
43
- const account = MARGINFI_ACCOUNTS[group][key];
44
- if (
45
- account.bank.toString().toLowerCase() === bank.toString().toLowerCase()
46
- ) {
47
- return { ...account, mint: new PublicKey(key) };
99
+ const search = (bankAccounts: MarginfiAccountsMap) => {
100
+ for (const group in bankAccounts) {
101
+ for (const key in bankAccounts[group]) {
102
+ const account = bankAccounts[group][key];
103
+ if (
104
+ account.bank.toString().toLowerCase() ===
105
+ bank.toString().toLowerCase()
106
+ ) {
107
+ return { ...account, mint: new PublicKey(key) };
108
+ }
48
109
  }
49
110
  }
111
+ };
112
+
113
+ let res = search(getMarginfiAccounts("Prod").bankAccounts);
114
+ if (res) {
115
+ return res;
116
+ }
117
+ res = search(getMarginfiAccounts("Staging").bankAccounts);
118
+ if (res) {
119
+ return res;
50
120
  }
121
+
51
122
  throw new Error(`Marginfi accounts not found by the bank: ${bank}`);
52
123
  }
53
124
 
@@ -103,11 +174,16 @@ export async function getMarginfiMaxLtvAndLiqThresholdBps(
103
174
  return [0, 0];
104
175
  }
105
176
 
177
+ const bankAccounts = getMarginfiAccounts(
178
+ undefined,
179
+ marginfiGroup
180
+ ).bankAccounts;
181
+
106
182
  if (!supply.bank || supply.bank === null) {
107
183
  supply.bank = await safeFetchBank(
108
184
  umi,
109
185
  publicKey(
110
- MARGINFI_ACCOUNTS[marginfiGroup.toString()][supply.mint.toString()].bank
186
+ bankAccounts[marginfiGroup.toString()][supply.mint.toString()].bank
111
187
  ),
112
188
  { commitment: "confirmed" }
113
189
  );
@@ -120,7 +196,7 @@ export async function getMarginfiMaxLtvAndLiqThresholdBps(
120
196
  debt.bank = await safeFetchBank(
121
197
  umi,
122
198
  publicKey(
123
- MARGINFI_ACCOUNTS[marginfiGroup.toString()][debt.mint.toString()].bank
199
+ bankAccounts[marginfiGroup.toString()][debt.mint.toString()].bank
124
200
  ),
125
201
  { commitment: "confirmed" }
126
202
  );
@@ -149,7 +225,7 @@ export async function getEmptyMarginfiAccountsByAuthority(
149
225
  authority: PublicKey
150
226
  ): Promise<MarginfiAccount[]> {
151
227
  const marginfiAccounts = await umi.rpc.getProgramAccounts(
152
- MARGINFI_PROGRAM_ID,
228
+ umi.programs.get("marginfi").publicKey,
153
229
  {
154
230
  commitment: "confirmed",
155
231
  filters: [
@@ -187,7 +263,7 @@ export async function getAllMarginfiAccountsByAuthority(
187
263
  { marginfiAccount: PublicKey; supplyMint?: PublicKey; debtMint?: PublicKey }[]
188
264
  > {
189
265
  const marginfiAccounts = await umi.rpc.getProgramAccounts(
190
- MARGINFI_PROGRAM_ID,
266
+ umi.programs.get("marginfi").publicKey,
191
267
  {
192
268
  commitment: "confirmed",
193
269
  dataSlice: {
@@ -339,9 +415,9 @@ async function getBank(
339
415
  ? await safeFetchBank(
340
416
  umi,
341
417
  publicKey(
342
- MARGINFI_ACCOUNTS[marginfiGroup?.toString() ?? ""][
343
- data?.mint.toString()
344
- ].bank
418
+ getMarginfiAccounts(undefined, marginfiGroup).bankAccounts[
419
+ marginfiGroup?.toString() ?? ""
420
+ ][data?.mint.toString()].bank
345
421
  ),
346
422
  { commitment: "confirmed" }
347
423
  )
@@ -354,6 +430,7 @@ export async function getMarginfiAccountPositionState(
354
430
  marginfiGroup?: PublicKey,
355
431
  supply?: BankSelection,
356
432
  debt?: BankSelection,
433
+ programEnv?: ProgramEnv,
357
434
  contextUpdates?: ContextUpdates
358
435
  ): Promise<
359
436
  | { supplyBank: Bank | null; debtBank: Bank | null; state: PositionState }
@@ -481,7 +558,7 @@ export async function getMarginfiAccountPositionState(
481
558
  const supplyPrice = safeGetPrice(supply.mint!)!;
482
559
  let [maxLtvBps, liqThresholdBps] = await getMarginfiMaxLtvAndLiqThresholdBps(
483
560
  umi,
484
- marginfiGroup ?? new PublicKey(DEFAULT_MARGINFI_GROUP),
561
+ marginfiGroup ?? getMarginfiAccounts(programEnv).defaultGroup,
485
562
  {
486
563
  mint: toWeb3JsPublicKey(supplyBank.mint),
487
564
  bank: supplyBank,
@@ -1,6 +1,5 @@
1
1
  import { BASIS_POINTS, MIN_REPAY_GAP_BPS, USD_DECIMALS } from "../constants";
2
2
  import { PositionState } from "../generated";
3
- import { tokenInfo } from "./generalUtils";
4
3
 
5
4
  export function calcNetWorthUsd(state?: PositionState) {
6
5
  return fromRoundedUsdValue(state?.netWorth.baseAmountUsdValue ?? BigInt(0));
@@ -1,16 +1,18 @@
1
1
  import { PublicKey } from "@solana/web3.js";
2
2
  import { PublicKey as UmiPublicKey } from "@metaplex-foundation/umi";
3
- import { PYTH_PRICE_FEED_IDS } from "../constants/pythConstants";
4
- import { fromBaseUnit, toBaseUnit, toBps } from "./numberUtils";
5
- import { PRICES } from "../constants/solautoConstants";
6
- import { SWITCHBOARD_PRICE_FEED_IDS } from "../constants/switchboardConstants";
3
+ import * as SwbCommon from "@switchboard-xyz/common";
4
+ import {
5
+ PYTH_PRICE_FEED_IDS,
6
+ PRICES,
7
+ SWITCHBOARD_PRICE_FEED_IDS,
8
+ } from "../constants";
9
+ import { fromBaseUnit, toBaseUnit } from "./numberUtils";
7
10
  import {
8
11
  consoleLog,
9
12
  currentUnixSeconds,
10
13
  retryWithExponentialBackoff,
11
14
  zip,
12
15
  } from "./generalUtils";
13
- import * as SwbCommon from "@switchboard-xyz/common";
14
16
  import { getJupPriceData } from "./jupiterUtils";
15
17
 
16
18
  export async function fetchTokenPrices(mints: PublicKey[]): Promise<number[]> {
@@ -1,19 +1,4 @@
1
1
  import bs58 from "bs58";
2
- import {
3
- AddressLookupTableInput,
4
- Signer,
5
- TransactionBuilder,
6
- Umi,
7
- WrappedInstruction,
8
- publicKey,
9
- transactionBuilder,
10
- } from "@metaplex-foundation/umi";
11
- import {
12
- fromWeb3JsInstruction,
13
- toWeb3JsPublicKey,
14
- toWeb3JsTransaction,
15
- } from "@metaplex-foundation/umi-web3js-adapters";
16
- import { createUmi } from "@metaplex-foundation/umi-bundle-defaults";
17
2
  import {
18
3
  AddressLookupTableAccount,
19
4
  BlockhashWithExpiryBlockHeight,
@@ -32,6 +17,26 @@ import {
32
17
  createCloseAccountInstruction,
33
18
  createTransferInstruction,
34
19
  } from "@solana/spl-token";
20
+ import {
21
+ AddressLookupTableInput,
22
+ Signer,
23
+ TransactionBuilder,
24
+ Umi,
25
+ WrappedInstruction,
26
+ publicKey,
27
+ transactionBuilder,
28
+ } from "@metaplex-foundation/umi";
29
+ import {
30
+ fromWeb3JsInstruction,
31
+ toWeb3JsPublicKey,
32
+ toWeb3JsTransaction,
33
+ } from "@metaplex-foundation/umi-web3js-adapters";
34
+ import { createUmi } from "@metaplex-foundation/umi-bundle-defaults";
35
+ import { PriorityFeeSetting, ProgramEnv, TransactionRunType } from "../types";
36
+ import {
37
+ getLendingAccountEndFlashloanInstructionDataSerializer,
38
+ getLendingAccountStartFlashloanInstructionDataSerializer,
39
+ } from "../marginfi-sdk";
35
40
  import { getTokenAccount } from "./accountUtils";
36
41
  import {
37
42
  arraysAreEqual,
@@ -39,34 +44,21 @@ import {
39
44
  customRpcCall,
40
45
  retryWithExponentialBackoff,
41
46
  } from "./generalUtils";
42
- import {
43
- getLendingAccountEndFlashloanInstructionDataSerializer,
44
- getLendingAccountStartFlashloanInstructionDataSerializer,
45
- } from "../marginfi-sdk";
46
- import { PriorityFeeSetting, TransactionRunType } from "../types";
47
- import { SOLAUTO_PROD_PROGRAM } from "../constants";
48
47
  import { createDynamicSolautoProgram } from "./solautoUtils";
49
-
50
- export function buildHeliusApiUrl(heliusApiKey: string) {
51
- return `https://mainnet.helius-rpc.com/?api-key=${heliusApiKey}`;
52
- }
53
-
54
- export function buildIronforgeApiUrl(ironforgeApiKey: string) {
55
- return `https://rpc.ironforge.network/mainnet?apiKey=${ironforgeApiKey}`;
56
- }
48
+ import { createDynamicMarginfiProgram } from "./marginfiUtils";
57
49
 
58
50
  export function getSolanaRpcConnection(
59
51
  rpcUrl: string,
60
- programId: PublicKey = SOLAUTO_PROD_PROGRAM,
61
- wsEndpoint?: string
52
+ programId?: PublicKey,
53
+ lpEnv?: ProgramEnv
62
54
  ): [Connection, Umi] {
63
55
  const connection = new Connection(rpcUrl, {
64
56
  commitment: "confirmed",
65
- wsEndpoint: wsEndpoint,
66
57
  });
67
58
  const umi = createUmi(connection).use({
68
59
  install(umi) {
69
60
  umi.programs.add(createDynamicSolautoProgram(programId), false);
61
+ umi.programs.add(createDynamicMarginfiProgram(lpEnv), false);
70
62
  },
71
63
  });
72
64
  return [connection, umi];
@@ -1,5 +1,7 @@
1
1
  import { PublicKey } from "@solana/web3.js";
2
2
  import { Program, publicKey, Umi } from "@metaplex-foundation/umi";
3
+ import { toWeb3JsPublicKey } from "@metaplex-foundation/umi-web3js-adapters";
4
+ import { QuoteResponse } from "@jup-ag/api";
3
5
  import {
4
6
  AutomationSettings,
5
7
  DCASettings,
@@ -17,21 +19,15 @@ import {
17
19
  getSolautoPositionAccountDataSerializer,
18
20
  getSolautoPositionSize,
19
21
  } from "../generated";
20
- import { getReferralState } from "./accountUtils";
21
- import { toWeb3JsPublicKey } from "@metaplex-foundation/umi-web3js-adapters";
22
- import { ALL_SUPPORTED_TOKENS } from "../constants";
23
- import {
24
- findMarginfiAccounts,
25
- getAllMarginfiAccountsByAuthority,
26
- } from "./marginfiUtils";
27
- import { SolautoPositionDetails } from "../types/solauto";
28
- import { QuoteResponse } from "@jup-ag/api";
29
- import { createSolautoSettings } from "../solautoPosition";
22
+ import { ALL_SUPPORTED_TOKENS, SOLAUTO_PROD_PROGRAM } from "../constants";
23
+ import { SolautoPositionDetails } from "../types";
30
24
  import {
31
25
  SolautoClient,
32
26
  SolautoMarginfiClient,
33
27
  TxHandlerProps,
34
28
  } from "../services";
29
+ import { createSolautoSettings } from "../solautoPosition";
30
+ import { getReferralState } from "./accountUtils";
35
31
  import {
36
32
  calcTotalDebt,
37
33
  calcTotalSupply,
@@ -41,11 +37,15 @@ import {
41
37
  toRoundedUsdValue,
42
38
  } from "./numberUtils";
43
39
  import { fetchTokenPrices } from "./priceUtils";
40
+ import {
41
+ findMarginfiAccounts,
42
+ getAllMarginfiAccountsByAuthority,
43
+ } from "./marginfiUtils";
44
44
 
45
- export function createDynamicSolautoProgram(programId: PublicKey): Program {
45
+ export function createDynamicSolautoProgram(programId?: PublicKey): Program {
46
46
  return {
47
47
  name: "solauto",
48
- publicKey: publicKey(programId),
48
+ publicKey: publicKey(programId ?? SOLAUTO_PROD_PROGRAM),
49
49
  getErrorFromCode(code: number, cause?: Error) {
50
50
  return getSolautoErrorFromCode(code, this, cause);
51
51
  },
@@ -471,4 +471,4 @@ export function hasLastRebalance(rebalanceType: SolautoRebalanceType) {
471
471
  SolautoRebalanceType.DoubleRebalanceWithFL,
472
472
  SolautoRebalanceType.FLSwapThenRebalance,
473
473
  ].includes(rebalanceType);
474
- }
474
+ }