@haven-fi/solauto-sdk 1.0.12 → 1.0.14
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.
- package/dist/clients/solautoClient.d.ts.map +1 -1
- package/dist/clients/solautoClient.js +1 -1
- package/dist/clients/solautoMarginfiClient.d.ts.map +1 -1
- package/dist/clients/solautoMarginfiClient.js +3 -3
- package/dist/utils/deleteme.js +1 -1
- package/dist/utils/marginfiUtils.d.ts +3 -3
- package/dist/utils/marginfiUtils.d.ts.map +1 -1
- package/dist/utils/marginfiUtils.js +30 -11
- package/dist/utils/solauto/generalUtils.js +4 -4
- package/package.json +1 -1
- package/src/clients/solautoClient.ts +1 -1
- package/src/clients/solautoMarginfiClient.ts +11 -4
- package/src/utils/deleteme.ts +2 -2
- package/src/utils/marginfiUtils.ts +40 -19
- package/src/utils/solauto/generalUtils.ts +4 -4
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"solautoClient.d.ts","sourceRoot":"","sources":["../../src/clients/solautoClient.ts"],"names":[],"mappings":"AAAA,OAAO,gCAAgC,CAAC;AACxC,OAAO,EAEL,UAAU,EACV,SAAS,EACV,MAAM,iBAAiB,CAAC;AACzB,OAAO,EACL,MAAM,EACN,kBAAkB,EAClB,GAAG,EAQJ,MAAM,0BAA0B,CAAC;
|
|
1
|
+
{"version":3,"file":"solautoClient.d.ts","sourceRoot":"","sources":["../../src/clients/solautoClient.ts"],"names":[],"mappings":"AAAA,OAAO,gCAAgC,CAAC;AACxC,OAAO,EAEL,UAAU,EACV,SAAS,EACV,MAAM,iBAAiB,CAAC;AACzB,OAAO,EACL,MAAM,EACN,kBAAkB,EAClB,GAAG,EAQJ,MAAM,0BAA0B,CAAC;AAGlC,OAAO,EACL,aAAa,EAEd,MAAM,iDAAiD,CAAC;AACzD,OAAO,EACL,WAAW,EACX,kBAAkB,EAClB,eAAe,EACf,aAAa,EACb,aAAa,EACb,iBAAiB,EACjB,eAAe,EACf,wBA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B,GAAG,kBAAkB;IAuFhE,QAAQ,CAAC,WAAW,CAClB,gBAAgB,EAAE,gBAAgB,EAClC,uBAAuB,EAAE,SAAS,GACjC,kBAAkB;IAErB,QAAQ,CAAC,UAAU,CAAC,gBAAgB,EAAE,gBAAgB,GAAG,kBAAkB;IAE3E,QAAQ,CAAC,SAAS,CAChB,aAAa,EAAE,GAAG,GAAG,GAAG,EACxB,WAAW,EAAE,cAAc,EAC3B,aAAa,EAAE,wBAAwB,EACvC,2BAA2B,CAAC,EAAE,MAAM,EACpC,WAAW,CAAC,EAAE,MAAM,GACnB,kBAAkB;IAEf,qBAAqB,IAAI,OAAO,CAAC,aAAa,GAAG,SAAS,CAAC;CAYlE"}
|
|
@@ -5,6 +5,7 @@ require("rpc-websockets/dist/lib/client");
|
|
|
5
5
|
const web3_js_1 = require("@solana/web3.js");
|
|
6
6
|
const umi_1 = require("@metaplex-foundation/umi");
|
|
7
7
|
const umi_web3js_adapters_1 = require("@metaplex-foundation/umi-web3js-adapters");
|
|
8
|
+
const umi_bundle_defaults_1 = require("@metaplex-foundation/umi-bundle-defaults");
|
|
8
9
|
const umi_signer_wallet_adapters_1 = require("@metaplex-foundation/umi-signer-wallet-adapters");
|
|
9
10
|
const generated_1 = require("../generated");
|
|
10
11
|
const accountUtils_1 = require("../utils/accountUtils");
|
|
@@ -14,7 +15,6 @@ const spl_token_1 = require("@solana/spl-token");
|
|
|
14
15
|
const solautoConstants_1 = require("../constants/solautoConstants");
|
|
15
16
|
const generalUtils_1 = require("../utils/generalUtils");
|
|
16
17
|
const generalUtils_2 = require("../utils/solauto/generalUtils");
|
|
17
|
-
const umi_bundle_defaults_1 = require("@metaplex-foundation/umi-bundle-defaults");
|
|
18
18
|
class SolautoClient {
|
|
19
19
|
constructor(heliusApiKey, localTest) {
|
|
20
20
|
this.heliusApiKey = heliusApiKey;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"solautoMarginfiClient.d.ts","sourceRoot":"","sources":["../../src/clients/solautoMarginfiClient.ts"],"names":[],"mappings":"AAKA,OAAO,EACL,MAAM,EACN,kBAAkB,EAMnB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAEL,SAAS,EAEV,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,aAAa,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AACnE,OAAO,EAAE,qBAAqB,EAAE,MAAM,mBAAmB,CAAC;AAK1D,OAAO,EACL,kBAAkB,EAElB,aAAa,EACb,iBAAiB,EACjB,wBAAwB,EACxB,gCAAgC,EAKjC,MAAM,cAAc,CAAC;AAGtB,OAAO,EAEL,eAAe,
|
|
1
|
+
{"version":3,"file":"solautoMarginfiClient.d.ts","sourceRoot":"","sources":["../../src/clients/solautoMarginfiClient.ts"],"names":[],"mappings":"AAKA,OAAO,EACL,MAAM,EACN,kBAAkB,EAMnB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAEL,SAAS,EAEV,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,aAAa,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AACnE,OAAO,EAAE,qBAAqB,EAAE,MAAM,mBAAmB,CAAC;AAK1D,OAAO,EACL,kBAAkB,EAElB,aAAa,EACb,iBAAiB,EACjB,wBAAwB,EACxB,gCAAgC,EAKjC,MAAM,cAAc,CAAC;AAGtB,OAAO,EAEL,eAAe,EAQhB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AACvD,OAAO,EAAE,gBAAgB,EAAE,MAAM,iCAAiC,CAAC;AAOnE,OAAO,EAAE,aAAa,EAAE,MAAM,iDAAiD,CAAC;AAEhF,MAAM,WAAW,yBAA0B,SAAQ,iBAAiB;IAClE,eAAe,CAAC,EAAE,SAAS,GAAG,MAAM,CAAC;IACrC,sBAAsB,CAAC,EAAE,MAAM,CAAC;IAChC,aAAa,CAAC,EAAE,SAAS,CAAC;CAC3B;AAED,wBAAgB,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AgB,EAAE,gBAAgB,GAAG,kBAAkB;IA8ElE,iCAAiC,IAAI,kBAAkB;IASjD,qBAAqB,IAAI,OAAO,CAAC,aAAa,GAAG,SAAS,CAAC;CAiBlE"}
|
|
@@ -50,8 +50,8 @@ class SolautoMarginfiClient extends solautoClient_1.SolautoClient {
|
|
|
50
50
|
this.marginfiDebtBankAccounts = (0, marginfiUtils_1.findMarginfiBankAccounts)({
|
|
51
51
|
mint: this.debtLiquidityMint.toString(),
|
|
52
52
|
});
|
|
53
|
-
const existingMarginfiAccounts = await (0, marginfiUtils_1.getAllMarginfiAccountsByAuthority)(this.umi, (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(this.signer.publicKey));
|
|
54
|
-
const emptyMarginfiAccounts = existingMarginfiAccounts.filter((x) => x.lendingAccount.balances.find((y) => y.bankPk.toString() !== web3_js_1.PublicKey.default.toString() &&
|
|
53
|
+
const existingMarginfiAccounts = await (0, marginfiUtils_1.getAllMarginfiAccountsByAuthority)(this.umi, (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(this.signer.publicKey), false);
|
|
54
|
+
const emptyMarginfiAccounts = (await (0, marginfi_sdk_1.safeFetchAllMarginfiAccount)(this.umi, existingMarginfiAccounts.map((x) => (0, umi_1.publicKey)(x)))).filter((x) => x.lendingAccount.balances.find((y) => y.bankPk.toString() !== web3_js_1.PublicKey.default.toString() &&
|
|
55
55
|
(Math.round((0, numberUtils_1.bytesToI80F48)(y.assetShares.value)) != 0 ||
|
|
56
56
|
Math.round((0, numberUtils_1.bytesToI80F48)(y.liabilityShares.value)) != 0)) === undefined);
|
|
57
57
|
this.intermediaryMarginfiAccountSigner =
|
|
@@ -305,7 +305,7 @@ class SolautoMarginfiClient extends solautoClient_1.SolautoClient {
|
|
|
305
305
|
if (state) {
|
|
306
306
|
return state;
|
|
307
307
|
}
|
|
308
|
-
const freshState = await (0, marginfiUtils_1.getMarginfiAccountPositionState)(this.umi, this.
|
|
308
|
+
const freshState = await (0, marginfiUtils_1.getMarginfiAccountPositionState)(this.umi, this.marginfiAccountPk, this.supplyLiquidityMint, this.debtLiquidityMint, this.livePositionUpdates);
|
|
309
309
|
this.log(freshState);
|
|
310
310
|
return freshState;
|
|
311
311
|
}
|
package/dist/utils/deleteme.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { PublicKey } from "@solana/web3.js";
|
|
2
2
|
import { Umi } from "@metaplex-foundation/umi";
|
|
3
|
-
import { Bank
|
|
3
|
+
import { Bank } from "../marginfi-sdk";
|
|
4
4
|
import { MarginfiTokenAccounts } from "../types/accounts";
|
|
5
5
|
import { PositionState } from "../generated";
|
|
6
6
|
import { LivePositionUpdates } from "./solauto/generalUtils";
|
|
@@ -9,7 +9,7 @@ export declare function findMarginfiBankAccounts({ mint, bank, }: {
|
|
|
9
9
|
bank?: string;
|
|
10
10
|
}): MarginfiTokenAccounts;
|
|
11
11
|
export declare function getMaxLtvAndLiqThreshold(supplyBank: Bank, debtBank: Bank, supplyPrice?: number): Promise<[number, number]>;
|
|
12
|
-
export declare function getAllMarginfiAccountsByAuthority(umi: Umi, authority: PublicKey): Promise<
|
|
13
|
-
export declare function getMarginfiAccountPositionState(umi: Umi,
|
|
12
|
+
export declare function getAllMarginfiAccountsByAuthority(umi: Umi, authority: PublicKey, compatibleWithSolauto?: boolean): Promise<PublicKey[]>;
|
|
13
|
+
export declare function getMarginfiAccountPositionState(umi: Umi, marginfiAccountPk: PublicKey, supplyMint?: PublicKey, debtMint?: PublicKey, livePositionUpdates?: LivePositionUpdates): Promise<PositionState | undefined>;
|
|
14
14
|
export declare function getUpToDateShareValues(umi: Umi, bank: Bank): Promise<[number, number]>;
|
|
15
15
|
//# sourceMappingURL=marginfiUtils.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"marginfiUtils.d.ts","sourceRoot":"","sources":["../../src/utils/marginfiUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,C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IAAI,EAAE,IAAI,GACT,OAAO,CAAC,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CA0B3B"}
|
|
@@ -44,7 +44,7 @@ async function getMaxLtvAndLiqThreshold(supplyBank, debtBank, supplyPrice) {
|
|
|
44
44
|
return [maxLtv, liqThreshold];
|
|
45
45
|
}
|
|
46
46
|
exports.getMaxLtvAndLiqThreshold = getMaxLtvAndLiqThreshold;
|
|
47
|
-
async function getAllMarginfiAccountsByAuthority(umi, authority) {
|
|
47
|
+
async function getAllMarginfiAccountsByAuthority(umi, authority, compatibleWithSolauto) {
|
|
48
48
|
const marginfiAccounts = await umi.rpc.getProgramAccounts(marginfi_sdk_1.MARGINFI_PROGRAM_ID, {
|
|
49
49
|
commitment: "finalized",
|
|
50
50
|
dataSlice: {
|
|
@@ -63,7 +63,18 @@ async function getAllMarginfiAccountsByAuthority(umi, authority) {
|
|
|
63
63
|
},
|
|
64
64
|
],
|
|
65
65
|
});
|
|
66
|
-
|
|
66
|
+
if (compatibleWithSolauto) {
|
|
67
|
+
const positionStates = await Promise.all(marginfiAccounts.map(async (x) => ({
|
|
68
|
+
publicKey: x.publicKey,
|
|
69
|
+
state: await getMarginfiAccountPositionState(umi, (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(x.publicKey)),
|
|
70
|
+
})));
|
|
71
|
+
return positionStates
|
|
72
|
+
.filter((x) => x.state !== undefined)
|
|
73
|
+
.map((x) => (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(x.publicKey));
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
return marginfiAccounts.map((x) => (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(x.publicKey));
|
|
77
|
+
}
|
|
67
78
|
}
|
|
68
79
|
exports.getAllMarginfiAccountsByAuthority = getAllMarginfiAccountsByAuthority;
|
|
69
80
|
async function getTokenUsage(umi, bank, isAsset, shares, amountUsedAdjustment) {
|
|
@@ -97,7 +108,7 @@ async function getTokenUsage(umi, bank, isAsset, shares, amountUsedAdjustment) {
|
|
|
97
108
|
padding: new Uint8Array([]),
|
|
98
109
|
};
|
|
99
110
|
}
|
|
100
|
-
async function getMarginfiAccountPositionState(umi, supplyMint, debtMint,
|
|
111
|
+
async function getMarginfiAccountPositionState(umi, marginfiAccountPk, supplyMint, debtMint, livePositionUpdates) {
|
|
101
112
|
let marginfiAccount = await (0, marginfi_sdk_1.safeFetchMarginfiAccount)(umi, (0, umi_1.publicKey)(marginfiAccountPk));
|
|
102
113
|
let supplyBank = supplyMint && supplyMint !== web3_js_1.PublicKey.default
|
|
103
114
|
? await (0, marginfi_sdk_1.safeFetchBank)(umi, (0, umi_1.publicKey)(findMarginfiBankAccounts({ mint: supplyMint.toString() }).bank))
|
|
@@ -111,19 +122,27 @@ async function getMarginfiAccountPositionState(umi, supplyMint, debtMint, margin
|
|
|
111
122
|
marginfiAccount.lendingAccount.balances.filter((x) => x.active).length > 0) {
|
|
112
123
|
const supplyBalances = marginfiAccount.lendingAccount.balances.filter((balance) => balance.active && (0, numberUtils_1.bytesToI80F48)(balance.assetShares.value) > 0);
|
|
113
124
|
const debtBalances = marginfiAccount.lendingAccount.balances.filter((balance) => balance.active && (0, numberUtils_1.bytesToI80F48)(balance.liabilityShares.value) > 0);
|
|
114
|
-
|
|
125
|
+
if (supplyBalances.length > 1 || debtBalances.length > 1) {
|
|
126
|
+
// Not compatible with Solauto
|
|
127
|
+
return undefined;
|
|
128
|
+
}
|
|
129
|
+
if (supplyBalances.length > 0) {
|
|
115
130
|
if (supplyBank === null) {
|
|
116
|
-
supplyBank = await (0, marginfi_sdk_1.safeFetchBank)(umi,
|
|
131
|
+
supplyBank = await (0, marginfi_sdk_1.safeFetchBank)(umi, supplyBalances[0].bankPk);
|
|
132
|
+
}
|
|
133
|
+
if (!supplyMint) {
|
|
134
|
+
supplyMint = (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(supplyBank.mint);
|
|
117
135
|
}
|
|
118
|
-
supplyUsage = await getTokenUsage(umi, supplyBank, true, (0, numberUtils_1.bytesToI80F48)(
|
|
119
|
-
break;
|
|
136
|
+
supplyUsage = await getTokenUsage(umi, supplyBank, true, (0, numberUtils_1.bytesToI80F48)(supplyBalances[0].assetShares.value), livePositionUpdates?.supplyAdjustment);
|
|
120
137
|
}
|
|
121
|
-
|
|
138
|
+
if (debtBalances.length > 0) {
|
|
122
139
|
if (debtBank === null) {
|
|
123
|
-
debtBank = await (0, marginfi_sdk_1.safeFetchBank)(umi,
|
|
140
|
+
debtBank = await (0, marginfi_sdk_1.safeFetchBank)(umi, debtBalances[0].bankPk);
|
|
141
|
+
}
|
|
142
|
+
if (!debtMint) {
|
|
143
|
+
debtMint = (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(debtBank.mint);
|
|
124
144
|
}
|
|
125
|
-
debtUsage = await getTokenUsage(umi, debtBank, false, (0, numberUtils_1.bytesToI80F48)(
|
|
126
|
-
break;
|
|
145
|
+
debtUsage = await getTokenUsage(umi, debtBank, false, (0, numberUtils_1.bytesToI80F48)(debtBalances[0].liabilityShares.value), livePositionUpdates?.debtAdjustment);
|
|
127
146
|
}
|
|
128
147
|
}
|
|
129
148
|
if (supplyBank !== null && !supplyUsage) {
|
|
@@ -200,14 +200,14 @@ async function getAllPositionsByAuthority(umi, user) {
|
|
|
200
200
|
positionId: x.positionId,
|
|
201
201
|
lendingPlatform: x.lendingPlatform,
|
|
202
202
|
})));
|
|
203
|
-
const marginfiPositions = await (0, marginfiUtils_1.getAllMarginfiAccountsByAuthority)(umi, user);
|
|
204
|
-
allPositions.push(...marginfiPositions.map((
|
|
203
|
+
const marginfiPositions = await (0, marginfiUtils_1.getAllMarginfiAccountsByAuthority)(umi, user, true);
|
|
204
|
+
allPositions.push(...marginfiPositions.map((pubkey) => ({
|
|
205
205
|
positionId: 0,
|
|
206
206
|
lendingPlatform: generated_1.LendingPlatform.Marginfi,
|
|
207
|
-
protocolAccount:
|
|
207
|
+
protocolAccount: pubkey,
|
|
208
208
|
})));
|
|
209
209
|
// TODO support other platforms
|
|
210
|
-
return
|
|
210
|
+
return allPositions;
|
|
211
211
|
}
|
|
212
212
|
exports.getAllPositionsByAuthority = getAllPositionsByAuthority;
|
|
213
213
|
async function positionStateWithLatestPrices(umi, state, protocolAccount, lendingPlatform) {
|
package/package.json
CHANGED
|
@@ -17,6 +17,7 @@ import {
|
|
|
17
17
|
some,
|
|
18
18
|
} from "@metaplex-foundation/umi";
|
|
19
19
|
import { toWeb3JsPublicKey } from "@metaplex-foundation/umi-web3js-adapters";
|
|
20
|
+
import { createUmi } from "@metaplex-foundation/umi-bundle-defaults";
|
|
20
21
|
import {
|
|
21
22
|
WalletAdapter,
|
|
22
23
|
walletAdapterIdentity,
|
|
@@ -58,7 +59,6 @@ import { NATIVE_MINT } from "@solana/spl-token";
|
|
|
58
59
|
import { MIN_POSITION_STATE_FRESHNESS_SECS } from "../constants/solautoConstants";
|
|
59
60
|
import { currentUnixSeconds } from "../utils/generalUtils";
|
|
60
61
|
import { LivePositionUpdates } from "../utils/solauto/generalUtils";
|
|
61
|
-
import { createUmi } from "@metaplex-foundation/umi-bundle-defaults";
|
|
62
62
|
|
|
63
63
|
export interface SolautoClientArgs {
|
|
64
64
|
authority?: PublicKey;
|
|
@@ -45,6 +45,7 @@ import {
|
|
|
45
45
|
lendingAccountRepay,
|
|
46
46
|
lendingAccountStartFlashloan,
|
|
47
47
|
marginfiAccountInitialize,
|
|
48
|
+
safeFetchAllMarginfiAccount,
|
|
48
49
|
safeFetchMarginfiAccount,
|
|
49
50
|
} from "../marginfi-sdk";
|
|
50
51
|
import { JupSwapDetails } from "../utils/jupiterUtils";
|
|
@@ -136,9 +137,15 @@ export class SolautoMarginfiClient extends SolautoClient {
|
|
|
136
137
|
|
|
137
138
|
const existingMarginfiAccounts = await getAllMarginfiAccountsByAuthority(
|
|
138
139
|
this.umi,
|
|
139
|
-
toWeb3JsPublicKey(this.signer.publicKey)
|
|
140
|
+
toWeb3JsPublicKey(this.signer.publicKey),
|
|
141
|
+
false
|
|
140
142
|
);
|
|
141
|
-
const emptyMarginfiAccounts =
|
|
143
|
+
const emptyMarginfiAccounts = (
|
|
144
|
+
await safeFetchAllMarginfiAccount(
|
|
145
|
+
this.umi,
|
|
146
|
+
existingMarginfiAccounts.map((x) => publicKey(x))
|
|
147
|
+
)
|
|
148
|
+
).filter(
|
|
142
149
|
(x) =>
|
|
143
150
|
x.lendingAccount.balances.find(
|
|
144
151
|
(y) =>
|
|
@@ -484,13 +491,13 @@ export class SolautoMarginfiClient extends SolautoClient {
|
|
|
484
491
|
|
|
485
492
|
const freshState = await getMarginfiAccountPositionState(
|
|
486
493
|
this.umi,
|
|
494
|
+
this.marginfiAccountPk,
|
|
487
495
|
this.supplyLiquidityMint,
|
|
488
496
|
this.debtLiquidityMint,
|
|
489
|
-
this.marginfiAccountPk,
|
|
490
497
|
this.livePositionUpdates
|
|
491
498
|
);
|
|
492
499
|
this.log(freshState);
|
|
493
|
-
|
|
500
|
+
|
|
494
501
|
return freshState;
|
|
495
502
|
}
|
|
496
503
|
}
|
package/src/utils/deleteme.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { getDebtAdjustmentUsd, getLiqUtilzationRateBps } from "./numberUtils";
|
|
2
2
|
|
|
3
|
-
//
|
|
3
|
+
// TODO delete this file
|
|
4
4
|
function check() {
|
|
5
5
|
let supply = 458948;
|
|
6
6
|
let debt = 212846;
|
|
@@ -37,4 +37,4 @@ function check() {
|
|
|
37
37
|
console.log!(liq_utilization_rate);
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
check();
|
|
40
|
+
check();
|
|
@@ -86,8 +86,9 @@ export async function getMaxLtvAndLiqThreshold(
|
|
|
86
86
|
|
|
87
87
|
export async function getAllMarginfiAccountsByAuthority(
|
|
88
88
|
umi: Umi,
|
|
89
|
-
authority: PublicKey
|
|
90
|
-
|
|
89
|
+
authority: PublicKey,
|
|
90
|
+
compatibleWithSolauto?: boolean
|
|
91
|
+
): Promise<PublicKey[]> {
|
|
91
92
|
const marginfiAccounts = await umi.rpc.getProgramAccounts(
|
|
92
93
|
MARGINFI_PROGRAM_ID,
|
|
93
94
|
{
|
|
@@ -110,10 +111,22 @@ export async function getAllMarginfiAccountsByAuthority(
|
|
|
110
111
|
}
|
|
111
112
|
);
|
|
112
113
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
114
|
+
if (compatibleWithSolauto) {
|
|
115
|
+
const positionStates = await Promise.all(
|
|
116
|
+
marginfiAccounts.map(async (x) => ({
|
|
117
|
+
publicKey: x.publicKey,
|
|
118
|
+
state: await getMarginfiAccountPositionState(
|
|
119
|
+
umi,
|
|
120
|
+
toWeb3JsPublicKey(x.publicKey)
|
|
121
|
+
),
|
|
122
|
+
}))
|
|
123
|
+
);
|
|
124
|
+
return positionStates
|
|
125
|
+
.filter((x) => x.state !== undefined)
|
|
126
|
+
.map((x) => toWeb3JsPublicKey(x.publicKey));
|
|
127
|
+
} else {
|
|
128
|
+
return marginfiAccounts.map((x) => toWeb3JsPublicKey(x.publicKey));
|
|
129
|
+
}
|
|
117
130
|
}
|
|
118
131
|
|
|
119
132
|
async function getTokenUsage(
|
|
@@ -168,9 +181,9 @@ async function getTokenUsage(
|
|
|
168
181
|
|
|
169
182
|
export async function getMarginfiAccountPositionState(
|
|
170
183
|
umi: Umi,
|
|
171
|
-
supplyMint: PublicKey,
|
|
172
|
-
debtMint: PublicKey,
|
|
173
184
|
marginfiAccountPk: PublicKey,
|
|
185
|
+
supplyMint?: PublicKey,
|
|
186
|
+
debtMint?: PublicKey,
|
|
174
187
|
livePositionUpdates?: LivePositionUpdates
|
|
175
188
|
): Promise<PositionState | undefined> {
|
|
176
189
|
let marginfiAccount = await safeFetchMarginfiAccount(
|
|
@@ -213,33 +226,41 @@ export async function getMarginfiAccountPositionState(
|
|
|
213
226
|
balance.active && bytesToI80F48(balance.liabilityShares.value) > 0
|
|
214
227
|
);
|
|
215
228
|
|
|
216
|
-
|
|
229
|
+
if (supplyBalances.length > 1 || debtBalances.length > 1) {
|
|
230
|
+
// Not compatible with Solauto
|
|
231
|
+
return undefined;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
if (supplyBalances.length > 0) {
|
|
217
235
|
if (supplyBank === null) {
|
|
218
|
-
supplyBank = await safeFetchBank(umi,
|
|
236
|
+
supplyBank = await safeFetchBank(umi, supplyBalances[0].bankPk);
|
|
237
|
+
}
|
|
238
|
+
if (!supplyMint) {
|
|
239
|
+
supplyMint = toWeb3JsPublicKey(supplyBank!.mint);
|
|
219
240
|
}
|
|
220
|
-
|
|
221
241
|
supplyUsage = await getTokenUsage(
|
|
222
242
|
umi,
|
|
223
243
|
supplyBank!,
|
|
224
244
|
true,
|
|
225
|
-
bytesToI80F48(
|
|
245
|
+
bytesToI80F48(supplyBalances[0].assetShares.value),
|
|
226
246
|
livePositionUpdates?.supplyAdjustment
|
|
227
247
|
);
|
|
228
|
-
break;
|
|
229
248
|
}
|
|
230
|
-
|
|
249
|
+
|
|
250
|
+
if (debtBalances.length > 0) {
|
|
231
251
|
if (debtBank === null) {
|
|
232
|
-
debtBank = await safeFetchBank(umi,
|
|
252
|
+
debtBank = await safeFetchBank(umi, debtBalances[0].bankPk);
|
|
253
|
+
}
|
|
254
|
+
if (!debtMint) {
|
|
255
|
+
debtMint = toWeb3JsPublicKey(debtBank!.mint);
|
|
233
256
|
}
|
|
234
|
-
|
|
235
257
|
debtUsage = await getTokenUsage(
|
|
236
258
|
umi,
|
|
237
259
|
debtBank!,
|
|
238
260
|
false,
|
|
239
|
-
bytesToI80F48(
|
|
261
|
+
bytesToI80F48(debtBalances[0].liabilityShares.value),
|
|
240
262
|
livePositionUpdates?.debtAdjustment
|
|
241
263
|
);
|
|
242
|
-
break;
|
|
243
264
|
}
|
|
244
265
|
}
|
|
245
266
|
|
|
@@ -267,7 +288,7 @@ export async function getMarginfiAccountPositionState(
|
|
|
267
288
|
return undefined;
|
|
268
289
|
}
|
|
269
290
|
|
|
270
|
-
const supplyPrice = PRICES[supplyMint
|
|
291
|
+
const supplyPrice = PRICES[supplyMint!.toString()].price;
|
|
271
292
|
let [maxLtv, liqThreshold] = await getMaxLtvAndLiqThreshold(
|
|
272
293
|
supplyBank,
|
|
273
294
|
debtBank,
|
|
@@ -322,18 +322,18 @@ export async function getAllPositionsByAuthority(
|
|
|
322
322
|
}))
|
|
323
323
|
);
|
|
324
324
|
|
|
325
|
-
const marginfiPositions = await getAllMarginfiAccountsByAuthority(umi, user);
|
|
325
|
+
const marginfiPositions = await getAllMarginfiAccountsByAuthority(umi, user, true);
|
|
326
326
|
allPositions.push(
|
|
327
|
-
...marginfiPositions.map((
|
|
327
|
+
...marginfiPositions.map((pubkey) => ({
|
|
328
328
|
positionId: 0,
|
|
329
329
|
lendingPlatform: LendingPlatform.Marginfi,
|
|
330
|
-
protocolAccount:
|
|
330
|
+
protocolAccount: pubkey,
|
|
331
331
|
}))
|
|
332
332
|
);
|
|
333
333
|
|
|
334
334
|
// TODO support other platforms
|
|
335
335
|
|
|
336
|
-
return
|
|
336
|
+
return allPositions;
|
|
337
337
|
}
|
|
338
338
|
|
|
339
339
|
async function positionStateWithLatestPrices(
|