@haven-fi/solauto-sdk 1.0.8 → 1.0.10

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 (41) hide show
  1. package/dist/clients/index.d.ts +1 -0
  2. package/dist/clients/index.d.ts.map +1 -1
  3. package/dist/clients/index.js +1 -0
  4. package/dist/clients/solautoClient.d.ts +3 -3
  5. package/dist/clients/solautoClient.d.ts.map +1 -1
  6. package/dist/clients/solautoClient.js +15 -46
  7. package/dist/clients/solautoMarginfiClient.d.ts +1 -1
  8. package/dist/clients/solautoMarginfiClient.d.ts.map +1 -1
  9. package/dist/clients/solautoMarginfiClient.js +4 -5
  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 +1 -8
  13. package/dist/transactions/transactionUtils.d.ts +2 -2
  14. package/dist/transactions/transactionUtils.d.ts.map +1 -1
  15. package/dist/transactions/transactionUtils.js +9 -10
  16. package/dist/transactions/transactionsManager.d.ts.map +1 -1
  17. package/dist/transactions/transactionsManager.js +6 -7
  18. package/dist/utils/generalUtils.d.ts +2 -2
  19. package/dist/utils/generalUtils.d.ts.map +1 -1
  20. package/dist/utils/generalUtils.js +3 -3
  21. package/dist/utils/marginfiUtils.d.ts +4 -3
  22. package/dist/utils/marginfiUtils.d.ts.map +1 -1
  23. package/dist/utils/marginfiUtils.js +17 -16
  24. package/dist/utils/solanaUtils.d.ts +6 -7
  25. package/dist/utils/solanaUtils.d.ts.map +1 -1
  26. package/dist/utils/solanaUtils.js +20 -21
  27. package/dist/utils/solauto/generalUtils.d.ts +4 -4
  28. package/dist/utils/solauto/generalUtils.d.ts.map +1 -1
  29. package/dist/utils/solauto/generalUtils.js +50 -7
  30. package/package.json +1 -1
  31. package/src/clients/index.ts +2 -1
  32. package/src/clients/solautoClient.ts +39 -73
  33. package/src/clients/solautoMarginfiClient.ts +5 -8
  34. package/src/constants/solautoConstants.ts +1 -11
  35. package/src/transactions/transactionUtils.ts +11 -11
  36. package/src/transactions/transactionsManager.ts +11 -7
  37. package/src/utils/generalUtils.ts +5 -5
  38. package/src/utils/marginfiUtils.ts +28 -12
  39. package/src/utils/solanaUtils.ts +28 -23
  40. package/src/utils/solauto/generalUtils.ts +91 -14
  41. package/tests/transactions/solautoMarginfi.ts +2 -1
@@ -1,8 +1,8 @@
1
1
  import { PublicKey } from "@solana/web3.js";
2
- import { MaybeRpcAccount, publicKey } from "@metaplex-foundation/umi";
2
+ import { MaybeRpcAccount, publicKey, Umi } from "@metaplex-foundation/umi";
3
3
  import { PYTH_PRICE_FEED_IDS } from "../constants/pythConstants";
4
4
  import { fromBaseUnit, toBaseUnit } from "./numberUtils";
5
- import { PRICES, UMI } from "../constants/solautoConstants";
5
+ import { PRICES } from "../constants/solautoConstants";
6
6
 
7
7
  export function generateRandomU8(): number {
8
8
  return Math.floor(Math.random() * 255 + 1);
@@ -21,8 +21,8 @@ export function currentUnixSeconds(): number {
21
21
  return Math.round(new Date().getTime() / 1000);
22
22
  }
23
23
 
24
- export async function getSolanaAccountCreated(pk: PublicKey): Promise<boolean> {
25
- const account = await UMI.rpc.getAccount(publicKey(pk));
24
+ export async function getSolanaAccountCreated(umi: Umi, pk: PublicKey): Promise<boolean> {
25
+ const account = await umi.rpc.getAccount(publicKey(pk));
26
26
  return rpcAccountCreated(account);
27
27
  }
28
28
 
@@ -44,7 +44,7 @@ export function arraysAreEqual(arrayA: number[], arrayB: number[]): boolean {
44
44
 
45
45
  export async function getTokenPrices(mints: PublicKey[]): Promise<number[]> {
46
46
  const currentTime = currentUnixSeconds();
47
- if (!mints.some(mint => !(mint.toString() in PRICES) || currentTime - PRICES[mint.toString()].time > 3)) {
47
+ if (!mints.some(mint => !(mint.toString() in PRICES) || currentTime - PRICES[mint.toString()].time > 5)) {
48
48
  console.log("Accessing price cache");
49
49
  return mints.map(mint => PRICES[mint.toString()].price);
50
50
  }
@@ -1,5 +1,5 @@
1
1
  import { PublicKey } from "@solana/web3.js";
2
- import { publicKey } from "@metaplex-foundation/umi";
2
+ import { publicKey, Umi } from "@metaplex-foundation/umi";
3
3
  import { toWeb3JsPublicKey } from "@metaplex-foundation/umi-web3js-adapters";
4
4
  import {
5
5
  Bank,
@@ -8,6 +8,7 @@ import {
8
8
  MarginfiAccount,
9
9
  safeFetchAllMarginfiAccount,
10
10
  safeFetchBank,
11
+ safeFetchMarginfiAccount,
11
12
  } from "../marginfi-sdk";
12
13
  import { currentUnixSeconds, getTokenPrices } from "./generalUtils";
13
14
  import {
@@ -16,7 +17,7 @@ import {
16
17
  getLiqUtilzationRateBps,
17
18
  toBaseUnit,
18
19
  } from "./numberUtils";
19
- import { PRICES, UMI } from "../constants/solautoConstants";
20
+ import { PRICES } from "../constants/solautoConstants";
20
21
  import { MARGINFI_ACCOUNTS } from "../constants/marginfiAccounts";
21
22
  import { MarginfiTokenAccounts } from "../types/accounts";
22
23
  import { PositionState, PositionTokenUsage } from "../generated";
@@ -84,9 +85,10 @@ export async function getMaxLtvAndLiqThreshold(
84
85
  }
85
86
 
86
87
  export async function getAllMarginfiAccountsByAuthority(
88
+ umi: Umi,
87
89
  authority: PublicKey
88
90
  ): Promise<MarginfiAccount[]> {
89
- const marginfiAccounts = await UMI.rpc.getProgramAccounts(
91
+ const marginfiAccounts = await umi.rpc.getProgramAccounts(
90
92
  MARGINFI_PROGRAM_ID,
91
93
  {
92
94
  commitment: "finalized",
@@ -109,20 +111,23 @@ export async function getAllMarginfiAccountsByAuthority(
109
111
  );
110
112
 
111
113
  return safeFetchAllMarginfiAccount(
112
- UMI,
114
+ umi,
113
115
  marginfiAccounts.map((x) => x.publicKey)
114
116
  );
115
117
  }
116
118
 
117
119
  async function getTokenUsage(
120
+ umi: Umi,
118
121
  bank: Bank,
119
122
  isAsset: boolean,
120
123
  shares: number,
121
124
  amountUsedAdjustment?: bigint
122
125
  ): Promise<PositionTokenUsage> {
123
126
  let [marketPrice] = await getTokenPrices([toWeb3JsPublicKey(bank.mint)]);
124
- const [assetShareValue, liabilityShareValue] =
125
- await getUpToDateShareValues(bank);
127
+ const [assetShareValue, liabilityShareValue] = await getUpToDateShareValues(
128
+ umi,
129
+ bank
130
+ );
126
131
  const shareValue = isAsset ? assetShareValue : liabilityShareValue;
127
132
  const amountUsed = shares * shareValue + Number(amountUsedAdjustment ?? 0);
128
133
 
@@ -162,15 +167,21 @@ async function getTokenUsage(
162
167
  }
163
168
 
164
169
  export async function getMarginfiAccountPositionState(
170
+ umi: Umi,
165
171
  supplyMint: PublicKey,
166
172
  debtMint: PublicKey,
167
- marginfiAccount: MarginfiAccount | null,
173
+ marginfiAccountPk: PublicKey,
168
174
  livePositionUpdates?: LivePositionUpdates
169
175
  ): Promise<PositionState | undefined> {
176
+ let marginfiAccount = await safeFetchMarginfiAccount(
177
+ umi,
178
+ publicKey(marginfiAccountPk)
179
+ );
180
+
170
181
  let supplyBank: Bank | null =
171
182
  supplyMint && supplyMint !== PublicKey.default
172
183
  ? await safeFetchBank(
173
- UMI,
184
+ umi,
174
185
  publicKey(
175
186
  findMarginfiBankAccounts({ mint: supplyMint.toString() }).bank
176
187
  )
@@ -179,7 +190,7 @@ export async function getMarginfiAccountPositionState(
179
190
  let debtBank: Bank | null =
180
191
  debtMint && debtMint !== PublicKey.default
181
192
  ? await safeFetchBank(
182
- UMI,
193
+ umi,
183
194
  publicKey(
184
195
  findMarginfiBankAccounts({ mint: debtMint.toString() }).bank
185
196
  )
@@ -204,10 +215,11 @@ export async function getMarginfiAccountPositionState(
204
215
 
205
216
  for (const balance of supplyBalances) {
206
217
  if (supplyBank === null) {
207
- supplyBank = await safeFetchBank(UMI, balance.bankPk);
218
+ supplyBank = await safeFetchBank(umi, balance.bankPk);
208
219
  }
209
220
 
210
221
  supplyUsage = await getTokenUsage(
222
+ umi,
211
223
  supplyBank!,
212
224
  true,
213
225
  bytesToI80F48(balance.assetShares.value),
@@ -217,10 +229,11 @@ export async function getMarginfiAccountPositionState(
217
229
  }
218
230
  for (const balance of debtBalances) {
219
231
  if (debtBank === null) {
220
- debtBank = await safeFetchBank(UMI, balance.bankPk);
232
+ debtBank = await safeFetchBank(umi, balance.bankPk);
221
233
  }
222
234
 
223
235
  debtUsage = await getTokenUsage(
236
+ umi,
224
237
  debtBank!,
225
238
  false,
226
239
  bytesToI80F48(balance.liabilityShares.value),
@@ -232,6 +245,7 @@ export async function getMarginfiAccountPositionState(
232
245
 
233
246
  if (supplyBank !== null && !supplyUsage) {
234
247
  supplyUsage = await getTokenUsage(
248
+ umi,
235
249
  supplyBank,
236
250
  true,
237
251
  0,
@@ -241,6 +255,7 @@ export async function getMarginfiAccountPositionState(
241
255
 
242
256
  if (debtBank !== null && !debtUsage) {
243
257
  debtUsage = await getTokenUsage(
258
+ umi,
244
259
  debtBank,
245
260
  false,
246
261
  0,
@@ -353,9 +368,10 @@ function calcAccruedInterestPaymentPerPeriod(
353
368
  }
354
369
 
355
370
  export async function getUpToDateShareValues(
371
+ umi: Umi,
356
372
  bank: Bank
357
373
  ): Promise<[number, number]> {
358
- const currentTime = await currentUnixSecondsSolana();
374
+ const currentTime = await currentUnixSecondsSolana(umi);
359
375
  let timeDelta = currentTime - Number(bank.lastUpdate);
360
376
 
361
377
  const totalAssets =
@@ -3,6 +3,7 @@ import {
3
3
  AddressLookupTableInput,
4
4
  Signer,
5
5
  TransactionBuilder,
6
+ Umi,
6
7
  WrappedInstruction,
7
8
  publicKey,
8
9
  transactionBuilder,
@@ -15,6 +16,7 @@ import {
15
16
  import {
16
17
  AddressLookupTableAccount,
17
18
  ComputeBudgetProgram,
19
+ Connection,
18
20
  PublicKey,
19
21
  RpcResponseAndContext,
20
22
  SimulatedTransactionResponse,
@@ -28,21 +30,19 @@ import {
28
30
  createTransferInstruction,
29
31
  } from "@solana/spl-token";
30
32
  import { getTokenAccount } from "./accountUtils";
31
- import { SolautoClient } from "../clients/solautoClient";
32
- import { CONNECTION, UMI } from "../constants/solautoConstants";
33
33
  import { arraysAreEqual, retryWithExponentialBackoff } from "./generalUtils";
34
34
  import {
35
35
  getLendingAccountEndFlashloanInstructionDataSerializer,
36
36
  getLendingAccountStartFlashloanInstructionDataSerializer,
37
37
  } from "../marginfi-sdk";
38
38
 
39
- export async function currentUnixSecondsSolana(): Promise<number> {
39
+ export async function currentUnixSecondsSolana(umi: Umi): Promise<number> {
40
40
  return await retryWithExponentialBackoff(async () => {
41
- const blockTime = await CONNECTION.getBlockTime(await CONNECTION.getSlot());
41
+ const blockTime = await umi.rpc.getBlockTime(await umi.rpc.getSlot());
42
42
  if (blockTime === null) {
43
43
  throw new Error("Unable to retrieve block time");
44
44
  }
45
- return blockTime;
45
+ return Number(blockTime);
46
46
  });
47
47
  }
48
48
 
@@ -137,9 +137,10 @@ export function splTokenTransferUmiIx(
137
137
  }
138
138
 
139
139
  export async function getAdressLookupInputs(
140
+ umi: Umi,
140
141
  lookupTableAddresses: string[]
141
142
  ): Promise<AddressLookupTableInput[]> {
142
- const addressLookupTableAccountInfos = await UMI.rpc.getAccounts(
143
+ const addressLookupTableAccountInfos = await umi.rpc.getAccounts(
143
144
  lookupTableAddresses.map((key) => publicKey(key))
144
145
  );
145
146
 
@@ -225,13 +226,14 @@ export function assembleFinalTransaction(
225
226
  }
226
227
 
227
228
  async function simulateTransaction(
229
+ connection: Connection,
228
230
  transaction: VersionedTransaction
229
231
  ): Promise<RpcResponseAndContext<SimulatedTransactionResponse>> {
230
- const simulationResult = await CONNECTION.simulateTransaction(transaction, {
232
+ const simulationResult = await connection.simulateTransaction(transaction, {
231
233
  sigVerify: true,
232
234
  });
233
235
  if (simulationResult.value.err) {
234
- simulationResult.value.logs?.forEach((x) => {
236
+ simulationResult.value.logs?.forEach((x: any) => {
235
237
  console.log(x);
236
238
  });
237
239
  throw simulationResult.value.err;
@@ -240,14 +242,15 @@ async function simulateTransaction(
240
242
  }
241
243
 
242
244
  export async function getComputeUnitPriceEstimate(
245
+ umi: Umi,
243
246
  tx: TransactionBuilder,
244
247
  attemptNum?: number
245
248
  ): Promise<number> {
246
249
  const web3Transaction = toWeb3JsTransaction(
247
- (await tx.setLatestBlockhash(UMI, { commitment: "finalized" })).build(UMI)
250
+ (await tx.setLatestBlockhash(umi, { commitment: "finalized" })).build(umi)
248
251
  );
249
252
  const serializedTransaction = bs58.encode(web3Transaction.serialize());
250
- const resp = await UMI.rpc.call("getPriorityFeeEstimate", [
253
+ const resp = await umi.rpc.call("getPriorityFeeEstimate", [
251
254
  {
252
255
  transaction: serializedTransaction,
253
256
  options: { priorityLevel: attemptNum && attemptNum > 0 ? "VeryHigh" : "High" },
@@ -259,30 +262,32 @@ export async function getComputeUnitPriceEstimate(
259
262
  }
260
263
 
261
264
  export async function sendSingleOptimizedTransaction(
262
- client: SolautoClient,
265
+ umi: Umi,
266
+ connection: Connection,
263
267
  tx: TransactionBuilder,
264
268
  simulateOnly?: boolean,
265
269
  attemptNum?: number
266
270
  ): Promise<Uint8Array | undefined> {
267
- client.log("Sending single optimized transaction...");
268
- client.log("Instructions: ", tx.getInstructions().length);
269
- client.log("Serialized transaction size: ", tx.getTransactionSize(UMI));
271
+ console.log("Sending single optimized transaction...");
272
+ console.log("Instructions: ", tx.getInstructions().length);
273
+ console.log("Serialized transaction size: ", tx.getTransactionSize(umi));
270
274
 
271
- const feeEstimate = await getComputeUnitPriceEstimate(tx, attemptNum);
272
- client.log("Compute unit price: ", feeEstimate);
275
+ const feeEstimate = await getComputeUnitPriceEstimate(umi, tx, attemptNum);
276
+ console.log("Compute unit price: ", feeEstimate);
273
277
 
274
278
  const simulationResult = await retryWithExponentialBackoff(
275
279
  async () =>
276
280
  await simulateTransaction(
281
+ connection,
277
282
  toWeb3JsTransaction(
278
283
  await (
279
284
  await assembleFinalTransaction(
280
- client.signer,
285
+ umi.identity,
281
286
  tx,
282
287
  feeEstimate,
283
288
  1_400_000
284
- ).setLatestBlockhash(UMI)
285
- ).buildAndSign(UMI)
289
+ ).setLatestBlockhash(umi)
290
+ ).buildAndSign(umi)
286
291
  )
287
292
  )
288
293
  );
@@ -290,17 +295,17 @@ export async function sendSingleOptimizedTransaction(
290
295
  const computeUnitLimit = Math.round(
291
296
  simulationResult.value.unitsConsumed! * 1.15
292
297
  );
293
- client.log("Compute unit limit: ", computeUnitLimit);
298
+ console.log("Compute unit limit: ", computeUnitLimit);
294
299
 
295
300
  if (!simulateOnly) {
296
301
  const result = await retryWithExponentialBackoff(
297
302
  async () =>
298
303
  await assembleFinalTransaction(
299
- client.signer,
304
+ umi.identity,
300
305
  tx,
301
306
  feeEstimate,
302
307
  computeUnitLimit
303
- ).sendAndConfirm(UMI, {
308
+ ).sendAndConfirm(umi, {
304
309
  send: {
305
310
  skipPreflight: true,
306
311
  commitment: "finalized",
@@ -308,7 +313,7 @@ export async function sendSingleOptimizedTransaction(
308
313
  confirm: { commitment: "finalized" },
309
314
  })
310
315
  );
311
- client.log(`https://solscan.io/tx/${bs58.encode(result.signature)}`);
316
+ console.log(`https://solscan.io/tx/${bs58.encode(result.signature)}`);
312
317
  if (result.result.value.err !== null) {
313
318
  throw new Error(result.result.value.err.toString());
314
319
  }
@@ -1,4 +1,4 @@
1
- import { isOption, isSome, PublicKey } from "@metaplex-foundation/umi";
1
+ import { isOption, isSome, PublicKey, Umi } from "@metaplex-foundation/umi";
2
2
  import {
3
3
  AutomationSettings,
4
4
  DCASettings,
@@ -13,14 +13,19 @@ import {
13
13
  getSolautoPositionAccountDataSerializer,
14
14
  getSolautoPositionSize,
15
15
  } from "../../generated";
16
- import { currentUnixSeconds } from "../generalUtils";
17
- import { fromBps, toBps } from "../numberUtils";
16
+ import { currentUnixSeconds, getTokenPrices } from "../generalUtils";
18
17
  import {
19
- MAX_REPAY_GAP_BPS,
20
- UMI,
21
- } from "../../constants/solautoConstants";
18
+ fromBaseUnit,
19
+ fromBps,
20
+ getLiqUtilzationRateBps,
21
+ toBaseUnit,
22
+ toBps,
23
+ } from "../numberUtils";
24
+ import { MAX_REPAY_GAP_BPS } from "../../constants/solautoConstants";
22
25
  import { getReferralState } from "../accountUtils";
23
26
  import { toWeb3JsPublicKey } from "@metaplex-foundation/umi-web3js-adapters";
27
+ import { USD_DECIMALS } from "../../constants";
28
+ import { getMarginfiAccountPositionState } from "../marginfiUtils";
24
29
 
25
30
  function newPeriodsPassed(
26
31
  automation: AutomationSettings,
@@ -90,7 +95,10 @@ export function getAdjustedSettingsFromAutomation(
90
95
  };
91
96
  }
92
97
 
93
- export function getSolautoFeesBps(isReferred: boolean, feeType: FeeType): {
98
+ export function getSolautoFeesBps(
99
+ isReferred: boolean,
100
+ feeType: FeeType
101
+ ): {
94
102
  solauto: number;
95
103
  referrer: number;
96
104
  total: number;
@@ -116,11 +124,17 @@ export function getMaxLiqUtilizationRate(
116
124
  }
117
125
 
118
126
  export function maxRepayFrom(maxLtvBps: number, liqThresholdBps: number) {
119
- return Math.min(9000, getMaxLiqUtilizationRate(maxLtvBps, liqThresholdBps - 1000));
127
+ return Math.min(
128
+ 9000,
129
+ getMaxLiqUtilizationRate(maxLtvBps, liqThresholdBps - 1000)
130
+ );
120
131
  }
121
132
 
122
133
  export function maxRepayTo(maxLtvBps: number, liqThresholdBps: number) {
123
- return Math.min(maxRepayFrom(maxLtvBps, liqThresholdBps) - MAX_REPAY_GAP_BPS, getMaxLiqUtilizationRate(maxLtvBps, liqThresholdBps));
134
+ return Math.min(
135
+ maxRepayFrom(maxLtvBps, liqThresholdBps) - MAX_REPAY_GAP_BPS,
136
+ getMaxLiqUtilizationRate(maxLtvBps, liqThresholdBps)
137
+ );
124
138
  }
125
139
 
126
140
  export function eligibileForRebalance(
@@ -167,11 +181,15 @@ export function eligibleForRefresh(
167
181
  if (positionSettings.automation.targetPeriods > 0) {
168
182
  return eligibleForNextAutomationPeriod(positionSettings.automation);
169
183
  } else {
170
- return currentUnixSeconds() - Number(positionState.lastUpdated) > 60 * 60 * 24 * 7;
184
+ return (
185
+ currentUnixSeconds() - Number(positionState.lastUpdated) >
186
+ 60 * 60 * 24 * 7
187
+ );
171
188
  }
172
189
  }
173
190
 
174
191
  export async function getSolautoManagedPositions(
192
+ umi: Umi,
175
193
  authority?: PublicKey
176
194
  ): Promise<
177
195
  {
@@ -187,7 +205,7 @@ export async function getSolautoManagedPositions(
187
205
  // authority: Pubkey
188
206
  // lending_platform: u8
189
207
 
190
- const accounts = await UMI.rpc.getProgramAccounts(SOLAUTO_PROGRAM_ID, {
208
+ const accounts = await umi.rpc.getProgramAccounts(SOLAUTO_PROGRAM_ID, {
191
209
  commitment: "finalized",
192
210
  dataSlice: {
193
211
  offset: 0,
@@ -231,8 +249,8 @@ export async function getSolautoManagedPositions(
231
249
  });
232
250
  }
233
251
 
234
- export async function getAllReferralStates(): Promise<PublicKey[]> {
235
- const accounts = await UMI.rpc.getProgramAccounts(SOLAUTO_PROGRAM_ID, {
252
+ export async function getAllReferralStates(umi: Umi): Promise<PublicKey[]> {
253
+ const accounts = await umi.rpc.getProgramAccounts(SOLAUTO_PROGRAM_ID, {
236
254
  commitment: "finalized",
237
255
  dataSlice: {
238
256
  offset: 0,
@@ -249,6 +267,7 @@ export async function getAllReferralStates(): Promise<PublicKey[]> {
249
267
  }
250
268
 
251
269
  export async function getReferralsByUser(
270
+ umi: Umi,
252
271
  user: PublicKey
253
272
  ): Promise<PublicKey[]> {
254
273
  // bump: [u8; 1],
@@ -257,7 +276,7 @@ export async function getReferralsByUser(
257
276
  // referred_by_state: Pubkey,
258
277
 
259
278
  const userReferralState = await getReferralState(toWeb3JsPublicKey(user));
260
- const accounts = await UMI.rpc.getProgramAccounts(SOLAUTO_PROGRAM_ID, {
279
+ const accounts = await umi.rpc.getProgramAccounts(SOLAUTO_PROGRAM_ID, {
261
280
  commitment: "finalized",
262
281
  dataSlice: {
263
282
  offset: 0,
@@ -279,6 +298,64 @@ export async function getReferralsByUser(
279
298
  return accounts.map((x) => x.publicKey);
280
299
  }
281
300
 
301
+ async function positionStateWithLatestPrices(
302
+ umi: Umi,
303
+ state: PositionState,
304
+ protocolAccount: PublicKey,
305
+ lendingPlatform: LendingPlatform
306
+ ): Promise<PositionState | undefined> {
307
+ if (currentUnixSeconds() - Number(state.lastUpdated) > 60 * 60 * 24 * 7) {
308
+ if (lendingPlatform === LendingPlatform.Marginfi) {
309
+ return await getMarginfiAccountPositionState(
310
+ umi,
311
+ toWeb3JsPublicKey(state.supply.mint),
312
+ toWeb3JsPublicKey(state.debt.mint),
313
+ toWeb3JsPublicKey(protocolAccount)
314
+ );
315
+ } else {
316
+ throw new Error("Lending platorm not yet supported");
317
+ }
318
+ }
319
+
320
+ const [supplyPrice, debtPrice] = await getTokenPrices([
321
+ toWeb3JsPublicKey(state.supply.mint),
322
+ toWeb3JsPublicKey(state.debt.mint),
323
+ ]);
324
+
325
+ const supplyUsd =
326
+ fromBaseUnit(state.supply.amountUsed.baseUnit, state.supply.decimals) *
327
+ supplyPrice;
328
+ const debtUsd =
329
+ fromBaseUnit(state.debt.amountUsed.baseUnit, state.debt.decimals) *
330
+ debtPrice;
331
+ return {
332
+ ...state,
333
+ liqUtilizationRateBps: getLiqUtilzationRateBps(
334
+ supplyUsd,
335
+ debtUsd,
336
+ state.liqThresholdBps
337
+ ),
338
+ netWorth: {
339
+ ...state.netWorth,
340
+ baseAmountUsdValue: toBaseUnit(supplyUsd - debtUsd, USD_DECIMALS),
341
+ },
342
+ supply: {
343
+ ...state.supply,
344
+ amountUsed: {
345
+ ...state.supply.amountUsed,
346
+ baseAmountUsdValue: toBaseUnit(supplyUsd, USD_DECIMALS),
347
+ },
348
+ },
349
+ debt: {
350
+ ...state.debt,
351
+ amountUsed: {
352
+ ...state.debt.amountUsed,
353
+ baseAmountUsdValue: toBaseUnit(debtUsd, USD_DECIMALS),
354
+ },
355
+ },
356
+ };
357
+ }
358
+
282
359
  type PositionAdjustment =
283
360
  | { type: "supply"; value: bigint }
284
361
  | { type: "debt"; value: bigint }
@@ -43,7 +43,8 @@ describe("Solauto Marginfi tests", async () => {
43
43
  {
44
44
  signer,
45
45
  positionId,
46
- }
46
+ },
47
+ process.env.HELIUS_API_KEY!
47
48
  );
48
49
 
49
50
  const transactionItems: TransactionItem[] = [];