@haven-fi/solauto-sdk 1.0.200 → 1.0.201

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.
@@ -5,6 +5,7 @@ import { MarginfiAssetAccounts } from "../types/accounts";
5
5
  import { PositionState } from "../generated";
6
6
  import { LivePositionUpdates } from "./solauto/generalUtils";
7
7
  export declare function findMarginfiAccounts(bank: PublicKey): MarginfiAssetAccounts;
8
+ export declare function marginfiMaxLtvAndLiqThresholdBps(supplyBank: Bank, debtBank: Bank, supplyPrice: number): [number, number];
8
9
  export declare function getMaxLtvAndLiqThreshold(umi: Umi, marginfiGroup: PublicKey, supply: {
9
10
  mint: PublicKey;
10
11
  bank?: Bank | null;
@@ -1 +1 @@
1
- {"version":3,"file":"marginfiUtils.d.ts","sourceRoot":"","sources":["../../src/utils/marginfiUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAa,GAAG,EAAE,MAAM,0BAA0B,CAAC;AAE1D,OAAO,EACL,IAAI,EAKL,MAAM,iBAAiB,CAAC;AAczB,OAAO,EAAE,qBAAqB,EAAE,MAAM,mBAAmB,CAAC;AAC1D,OAAO,EAAE,aAAa,EAAsB,MAAM,cAAc,CAAC;AAEjE,OAAO,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAC;AAG7D,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,SAAS,GAAG,qBAAqB,CAY3E;AAED,wBAAsB,wBAAwB,CAC5C,GAAG,EAAE,GAAG,EACR,aAAa,EAAE,SAAS,EACxB,MAAM,EAAE;IACN,IAAI,EAAE,SAAS,CAAC;IAChB,IAAI,CAAC,EAAE,IAAI,GAAG,IAAI,CAAC;CACpB,EACD,IAAI,EAAE;IACJ,IAAI,EAAE,SAAS,CAAC;IAChB,IAAI,CAAC,EAAE,IAAI,GAAG,IAAI,CAAC;CACpB,EACD,WAAW,CAAC,EAAE,MAA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,IAAI,EACd,WAAW,EAAE,MAAM,GAClB,CAAC,MAAM,EAAE,MAAM,CAAC,CA6BlB;AAED,wBAAsB,wBAAwB,CAC5C,GAAG,EAAE,GAAG,EACR,aAAa,EAAE,SAAS,EACxB,MAAM,EAAE;IACN,IAAI,EAAE,SAAS,CAAC;IAChB,IAAI,CAAC,EAAE,IAAI,GAAG,IAAI,CAAC;CACpB,EACD,IAAI,EAAE;IACJ,IAAI,EAAE,SAAS,CAAC;IAChB,IAAI,CAAC,EAAE,IAAI,GAAG,IAAI,CAAC;CACpB,EACD,WAAW,CAAC,EAAE,MAAM,GACnB,OAAO,CAAC,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAwC3B;AAED,wBAAsB,iCAAiC,CACrD,GAAG,EAAE,GAAG,EACR,SAAS,EAAE,SAAS,EACpB,qBAAqB,CAAC,EAAE,OAAO,GAC9B,OAAO,CACR;IAAE,eAAe,EAAE,SAAS,CAAC;IAAC,UAAU,CAAC,EAAE,SAAS,CAAC;IAAC,QAAQ,CAAC,EAAE,SAAS,CAAA;CAAE,EAAE,CAC/E,CAwDA;AAgED,wBAAsB,+BAA+B,CACnD,GAAG,EAAE,GAAG,EACR,iBAAiB,EAAE,SAAS,EAC5B,aAAa,CAAC,EAAE,SAAS,EACzB,UAAU,CAAC,EAAE,SAAS,EACtB,QAAQ,CAAC,EAAE,SAAS,EACpB,mBAAmB,CAAC,EAAE,mBAAmB,GACxC,OAAO,CAAC,aAAa,GAAG,SAAS,CAAC,CAmLpC;AA+DD,wBAAsB,sBAAsB,CAC1C,GAAG,EAAE,GAAG,EACR,IAAI,EAAE,IAAI,GACT,OAAO,CAAC,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAyB3B"}
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.findMarginfiAccounts = findMarginfiAccounts;
4
+ exports.marginfiMaxLtvAndLiqThresholdBps = marginfiMaxLtvAndLiqThresholdBps;
4
5
  exports.getMaxLtvAndLiqThreshold = getMaxLtvAndLiqThreshold;
5
6
  exports.getAllMarginfiAccountsByAuthority = getAllMarginfiAccountsByAuthority;
6
7
  exports.getMarginfiAccountPositionState = getMarginfiAccountPositionState;
@@ -25,6 +26,21 @@ function findMarginfiAccounts(bank) {
25
26
  }
26
27
  throw new Error(`Marginfi accounts not found by the bank: ${bank}`);
27
28
  }
29
+ function marginfiMaxLtvAndLiqThresholdBps(supplyBank, debtBank, supplyPrice) {
30
+ let maxLtv = (0, numberUtils_1.bytesToI80F48)(supplyBank.config.assetWeightInit.value) /
31
+ (0, numberUtils_1.bytesToI80F48)(debtBank.config.liabilityWeightInit.value);
32
+ const liqThreshold = (0, numberUtils_1.bytesToI80F48)(supplyBank.config.assetWeightMaint.value) /
33
+ (0, numberUtils_1.bytesToI80F48)(debtBank.config.liabilityWeightMaint.value);
34
+ const totalDepositedUsdValue = (0, numberUtils_1.fromBaseUnit)(BigInt(Math.round((0, numberUtils_1.bytesToI80F48)(supplyBank.totalAssetShares.value) *
35
+ (0, numberUtils_1.bytesToI80F48)(supplyBank.assetShareValue.value))), supplyBank.mintDecimals) * supplyPrice;
36
+ if (supplyBank.config.totalAssetValueInitLimit !== BigInt(0) &&
37
+ totalDepositedUsdValue > supplyBank.config.totalAssetValueInitLimit) {
38
+ const discount = Number(supplyBank.config.totalAssetValueInitLimit) /
39
+ totalDepositedUsdValue;
40
+ maxLtv = Math.round(maxLtv * Number(discount));
41
+ }
42
+ return [maxLtv, liqThreshold];
43
+ }
28
44
  async function getMaxLtvAndLiqThreshold(umi, marginfiGroup, supply, debt, supplyPrice) {
29
45
  if (!supply.bank && supply.mint.equals(web3_js_1.PublicKey.default)) {
30
46
  return [0, 0];
@@ -45,19 +61,7 @@ async function getMaxLtvAndLiqThreshold(umi, marginfiGroup, supply, debt, supply
45
61
  if (!debt.bank || debt.bank === null) {
46
62
  return [0, 0];
47
63
  }
48
- let maxLtv = (0, numberUtils_1.bytesToI80F48)(supply.bank.config.assetWeightInit.value) /
49
- (0, numberUtils_1.bytesToI80F48)(debt.bank.config.liabilityWeightInit.value);
50
- const liqThreshold = (0, numberUtils_1.bytesToI80F48)(supply.bank.config.assetWeightMaint.value) /
51
- (0, numberUtils_1.bytesToI80F48)(debt.bank.config.liabilityWeightMaint.value);
52
- const totalDepositedUsdValue = (0, numberUtils_1.fromBaseUnit)(BigInt(Math.round((0, numberUtils_1.bytesToI80F48)(supply.bank.totalAssetShares.value) *
53
- (0, numberUtils_1.bytesToI80F48)(supply.bank.assetShareValue.value))), supply.bank.mintDecimals) * supplyPrice;
54
- if (supply.bank.config.totalAssetValueInitLimit !== BigInt(0) &&
55
- totalDepositedUsdValue > supply.bank.config.totalAssetValueInitLimit) {
56
- const discount = Number(supply.bank.config.totalAssetValueInitLimit) /
57
- totalDepositedUsdValue;
58
- maxLtv = Math.round(maxLtv * Number(discount));
59
- }
60
- return [maxLtv, liqThreshold];
64
+ return marginfiMaxLtvAndLiqThresholdBps(supply.bank, debt.bank, supplyPrice);
61
65
  }
62
66
  async function getAllMarginfiAccountsByAuthority(umi, authority, compatibleWithSolauto) {
63
67
  const marginfiAccounts = await umi.rpc.getProgramAccounts(marginfi_sdk_1.MARGINFI_PROGRAM_ID, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@haven-fi/solauto-sdk",
3
- "version": "1.0.200",
3
+ "version": "1.0.201",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "description": "Typescript SDK for the Solauto program on the Solana blockchain",
@@ -20,7 +20,10 @@ import {
20
20
  toBaseUnit,
21
21
  toBps,
22
22
  } from "./numberUtils";
23
- import { DEFAULT_MARGINFI_GROUP, MARGINFI_ACCOUNTS } from "../constants/marginfiAccounts";
23
+ import {
24
+ DEFAULT_MARGINFI_GROUP,
25
+ MARGINFI_ACCOUNTS,
26
+ } from "../constants/marginfiAccounts";
24
27
  import { MarginfiAssetAccounts } from "../types/accounts";
25
28
  import { PositionState, PositionTokenUsage } from "../generated";
26
29
  import { USD_DECIMALS } from "../constants/generalAccounts";
@@ -41,6 +44,41 @@ export function findMarginfiAccounts(bank: PublicKey): MarginfiAssetAccounts {
41
44
  throw new Error(`Marginfi accounts not found by the bank: ${bank}`);
42
45
  }
43
46
 
47
+ export function marginfiMaxLtvAndLiqThresholdBps(
48
+ supplyBank: Bank,
49
+ debtBank: Bank,
50
+ supplyPrice: number
51
+ ): [number, number] {
52
+ let maxLtv =
53
+ bytesToI80F48(supplyBank.config.assetWeightInit.value) /
54
+ bytesToI80F48(debtBank.config.liabilityWeightInit.value);
55
+ const liqThreshold =
56
+ bytesToI80F48(supplyBank.config.assetWeightMaint.value) /
57
+ bytesToI80F48(debtBank.config.liabilityWeightMaint.value);
58
+
59
+ const totalDepositedUsdValue =
60
+ fromBaseUnit(
61
+ BigInt(
62
+ Math.round(
63
+ bytesToI80F48(supplyBank.totalAssetShares.value) *
64
+ bytesToI80F48(supplyBank.assetShareValue.value)
65
+ )
66
+ ),
67
+ supplyBank.mintDecimals
68
+ ) * supplyPrice!;
69
+ if (
70
+ supplyBank.config.totalAssetValueInitLimit !== BigInt(0) &&
71
+ totalDepositedUsdValue > supplyBank.config.totalAssetValueInitLimit
72
+ ) {
73
+ const discount =
74
+ Number(supplyBank.config.totalAssetValueInitLimit) /
75
+ totalDepositedUsdValue;
76
+ maxLtv = Math.round(maxLtv * Number(discount));
77
+ }
78
+
79
+ return [maxLtv, liqThreshold];
80
+ }
81
+
44
82
  export async function getMaxLtvAndLiqThreshold(
45
83
  umi: Umi,
46
84
  marginfiGroup: PublicKey,
@@ -61,7 +99,9 @@ export async function getMaxLtvAndLiqThreshold(
61
99
  if (!supply.bank || supply.bank === null) {
62
100
  supply.bank = await safeFetchBank(
63
101
  umi,
64
- publicKey(MARGINFI_ACCOUNTS[marginfiGroup.toString()][supply.mint.toString()].bank),
102
+ publicKey(
103
+ MARGINFI_ACCOUNTS[marginfiGroup.toString()][supply.mint.toString()].bank
104
+ ),
65
105
  { commitment: "confirmed" }
66
106
  );
67
107
  }
@@ -72,7 +112,9 @@ export async function getMaxLtvAndLiqThreshold(
72
112
  ) {
73
113
  debt.bank = await safeFetchBank(
74
114
  umi,
75
- publicKey(MARGINFI_ACCOUNTS[marginfiGroup.toString()][debt.mint.toString()].bank),
115
+ publicKey(
116
+ MARGINFI_ACCOUNTS[marginfiGroup.toString()][debt.mint.toString()].bank
117
+ ),
76
118
  { commitment: "confirmed" }
77
119
  );
78
120
  }
@@ -88,34 +130,7 @@ export async function getMaxLtvAndLiqThreshold(
88
130
  return [0, 0];
89
131
  }
90
132
 
91
- let maxLtv =
92
- bytesToI80F48(supply.bank!.config.assetWeightInit.value) /
93
- bytesToI80F48(debt.bank.config.liabilityWeightInit.value);
94
- const liqThreshold =
95
- bytesToI80F48(supply.bank!.config.assetWeightMaint.value) /
96
- bytesToI80F48(debt.bank.config.liabilityWeightMaint.value);
97
-
98
- const totalDepositedUsdValue =
99
- fromBaseUnit(
100
- BigInt(
101
- Math.round(
102
- bytesToI80F48(supply.bank!.totalAssetShares.value) *
103
- bytesToI80F48(supply.bank!.assetShareValue.value)
104
- )
105
- ),
106
- supply.bank!.mintDecimals
107
- ) * supplyPrice!;
108
- if (
109
- supply.bank!.config.totalAssetValueInitLimit !== BigInt(0) &&
110
- totalDepositedUsdValue > supply.bank!.config.totalAssetValueInitLimit
111
- ) {
112
- const discount =
113
- Number(supply.bank!.config.totalAssetValueInitLimit) /
114
- totalDepositedUsdValue;
115
- maxLtv = Math.round(maxLtv * Number(discount));
116
- }
117
-
118
- return [maxLtv, liqThreshold];
133
+ return marginfiMaxLtvAndLiqThresholdBps(supply.bank!, debt.bank, supplyPrice);
119
134
  }
120
135
 
121
136
  export async function getAllMarginfiAccountsByAuthority(
@@ -266,7 +281,11 @@ export async function getMarginfiAccountPositionState(
266
281
  supplyMint && supplyMint !== PublicKey.default
267
282
  ? await safeFetchBank(
268
283
  umi,
269
- publicKey(MARGINFI_ACCOUNTS[marginfiGroup?.toString() ?? ""][supplyMint.toString()].bank),
284
+ publicKey(
285
+ MARGINFI_ACCOUNTS[marginfiGroup?.toString() ?? ""][
286
+ supplyMint.toString()
287
+ ].bank
288
+ ),
270
289
  { commitment: "confirmed" }
271
290
  )
272
291
  : null;
@@ -274,7 +293,11 @@ export async function getMarginfiAccountPositionState(
274
293
  debtMint && debtMint !== PublicKey.default
275
294
  ? await safeFetchBank(
276
295
  umi,
277
- publicKey(MARGINFI_ACCOUNTS[marginfiGroup?.toString() ?? ""][debtMint.toString()].bank),
296
+ publicKey(
297
+ MARGINFI_ACCOUNTS[marginfiGroup?.toString() ?? ""][
298
+ debtMint.toString()
299
+ ].bank
300
+ ),
278
301
  { commitment: "confirmed" }
279
302
  )
280
303
  : null;
@@ -341,7 +364,11 @@ export async function getMarginfiAccountPositionState(
341
364
  return undefined;
342
365
  }
343
366
 
344
- if (!toWeb3JsPublicKey(supplyBank.group).equals(new PublicKey(DEFAULT_MARGINFI_GROUP))) {
367
+ if (
368
+ !toWeb3JsPublicKey(supplyBank.group).equals(
369
+ new PublicKey(DEFAULT_MARGINFI_GROUP)
370
+ )
371
+ ) {
345
372
  // Temporarily disabled for now
346
373
  return undefined;
347
374
  }