@haven-fi/solauto-sdk 1.0.85 → 1.0.86
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/clients/referralStateManager.d.ts +1 -1
- package/dist/clients/referralStateManager.d.ts.map +1 -1
- package/dist/utils/marginfiUtils.d.ts.map +1 -1
- package/dist/utils/marginfiUtils.js +12 -3
- package/package.json +1 -1
- package/src/clients/referralStateManager.ts +2 -2
- package/src/utils/marginfiUtils.ts +32 -23
@@ -4,9 +4,9 @@ import { WalletAdapter } from "@metaplex-foundation/umi-signer-wallet-adapters";
|
|
4
4
|
import { ReferralState } from "../generated";
|
5
5
|
import { TxHandler } from "./txHandler";
|
6
6
|
interface ReferralStateManagerArgs {
|
7
|
-
referralAuthority?: PublicKey;
|
8
7
|
signer?: Signer;
|
9
8
|
wallet?: WalletAdapter;
|
9
|
+
referralAuthority?: PublicKey;
|
10
10
|
}
|
11
11
|
export declare class ReferralStateManager extends TxHandler {
|
12
12
|
localTest?: boolean | undefined;
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"referralStateManager.d.ts","sourceRoot":"","sources":["../../src/clients/referralStateManager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAE5C,OAAO,EAAa,MAAM,EAAkB,kBAAkB,EAAE,GAAG,EAAE,MAAM,0BAA0B,CAAC;AAEtG,OAAO,EAAE,aAAa,EAAyB,MAAM,iDAAiD,CAAC;AACvG,OAAO,EAA2C,aAAa,EAAgD,MAAM,cAAc,CAAC;AAEpI,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAExC,UAAU,wBAAwB;IAC9B,
|
1
|
+
{"version":3,"file":"referralStateManager.d.ts","sourceRoot":"","sources":["../../src/clients/referralStateManager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAE5C,OAAO,EAAa,MAAM,EAAkB,kBAAkB,EAAE,GAAG,EAAE,MAAM,0BAA0B,CAAC;AAEtG,OAAO,EAAE,aAAa,EAAyB,MAAM,iDAAiD,CAAC;AACvG,OAAO,EAA2C,aAAa,EAAgD,MAAM,cAAc,CAAC;AAEpI,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAExC,UAAU,wBAAwB;IAC9B,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,aAAa,CAAC;IACvB,iBAAiB,CAAC,EAAE,SAAS,CAAC;CACjC;AAED,qBAAa,oBAAqB,SAAQ,SAAS;IAQN,SAAS,CAAC,EAAE,OAAO;IAPrD,GAAG,EAAG,GAAG,CAAC;IACV,MAAM,EAAG,MAAM,CAAC;IAEhB,iBAAiB,EAAG,SAAS,CAAC;IAC9B,aAAa,EAAG,SAAS,CAAC;IAC1B,iBAAiB,EAAG,aAAa,GAAG,IAAI,CAAC;gBAEpC,YAAY,EAAE,MAAM,EAAS,SAAS,CAAC,EAAE,OAAO,YAAA;IAStD,UAAU,CAAC,IAAI,EAAE,wBAAwB;IAe/C,mBAAmB,IAAI,MAAM,EAAE;IAI/B,sBAAsB,CAAC,YAAY,CAAC,EAAE,SAAS,EAAE,UAAU,CAAC,EAAE,SAAS,EAAE,WAAW,CAAC,EAAE,SAAS,GAAG,kBAAkB;IAiBrH,mBAAmB,CAAC,YAAY,CAAC,EAAE,SAAS,GAAG,kBAAkB;IAwB3D,kBAAkB,IAAI,OAAO,CAAC,IAAI,CAAC;CAC5C"}
|
@@ -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,
|
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,EAAyB,GAAG,E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CAAC,CA0B3B"}
|
@@ -15,6 +15,7 @@ const solautoConstants_1 = require("../constants/solautoConstants");
|
|
15
15
|
const marginfiAccounts_1 = require("../constants/marginfiAccounts");
|
16
16
|
const generalAccounts_1 = require("../constants/generalAccounts");
|
17
17
|
const solanaUtils_1 = require("./solanaUtils");
|
18
|
+
const accountUtils_1 = require("./accountUtils");
|
18
19
|
function findMarginfiAccounts(bank) {
|
19
20
|
for (const key in marginfiAccounts_1.MARGINFI_ACCOUNTS) {
|
20
21
|
const account = marginfiAccounts_1.MARGINFI_ACCOUNTS[key];
|
@@ -59,7 +60,7 @@ async function getMaxLtvAndLiqThreshold(umi, supply, debt, supplyPrice) {
|
|
59
60
|
return [maxLtv, liqThreshold];
|
60
61
|
}
|
61
62
|
async function getAllMarginfiAccountsByAuthority(umi, authority, compatibleWithSolauto) {
|
62
|
-
const
|
63
|
+
const getAccounts = async (authorityPk) => await umi.rpc.getProgramAccounts(marginfi_sdk_1.MARGINFI_PROGRAM_ID, {
|
63
64
|
commitment: "finalized",
|
64
65
|
dataSlice: {
|
65
66
|
offset: 0,
|
@@ -71,14 +72,22 @@ async function getAllMarginfiAccountsByAuthority(umi, authority, compatibleWithS
|
|
71
72
|
},
|
72
73
|
{
|
73
74
|
memcmp: {
|
74
|
-
bytes: new Uint8Array(
|
75
|
+
bytes: new Uint8Array(authorityPk.toBuffer()),
|
75
76
|
offset: 40, // Anchor account discriminator + group pubkey
|
76
77
|
},
|
77
78
|
},
|
78
79
|
],
|
79
80
|
});
|
81
|
+
let marginfiAccounts = [];
|
82
|
+
let selfManagedSolautoPositions = [];
|
80
83
|
if (compatibleWithSolauto) {
|
81
|
-
|
84
|
+
[marginfiAccounts, selfManagedSolautoPositions] = await Promise.all([getAccounts(authority), getAccounts((0, accountUtils_1.getSolautoPositionAccount)(authority, 0))]);
|
85
|
+
}
|
86
|
+
else {
|
87
|
+
marginfiAccounts = await getAccounts(authority);
|
88
|
+
}
|
89
|
+
if (compatibleWithSolauto) {
|
90
|
+
const positionStates = await Promise.all([...selfManagedSolautoPositions, ...marginfiAccounts].map(async (x) => ({
|
82
91
|
publicKey: x.publicKey,
|
83
92
|
state: await getMarginfiAccountPositionState(umi, (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(x.publicKey)),
|
84
93
|
})));
|
package/package.json
CHANGED
@@ -4,13 +4,13 @@ import { publicKey, Signer, signerIdentity, TransactionBuilder, Umi } from "@met
|
|
4
4
|
import { toWeb3JsPublicKey } from "@metaplex-foundation/umi-web3js-adapters";
|
5
5
|
import { WalletAdapter, walletAdapterIdentity } from "@metaplex-foundation/umi-signer-wallet-adapters";
|
6
6
|
import { claimReferralFees, createSolautoProgram, ReferralState, safeFetchReferralState, updateReferralStates } from "../generated";
|
7
|
-
import { getReferralState,
|
7
|
+
import { getReferralState, getTokenAccount } from "../utils";
|
8
8
|
import { TxHandler } from "./txHandler";
|
9
9
|
|
10
10
|
interface ReferralStateManagerArgs {
|
11
|
-
referralAuthority?: PublicKey;
|
12
11
|
signer?: Signer;
|
13
12
|
wallet?: WalletAdapter;
|
13
|
+
referralAuthority?: PublicKey;
|
14
14
|
}
|
15
15
|
|
16
16
|
export class ReferralStateManager extends TxHandler {
|
@@ -1,5 +1,5 @@
|
|
1
1
|
import { PublicKey } from "@solana/web3.js";
|
2
|
-
import { publicKey, Umi } from "@metaplex-foundation/umi";
|
2
|
+
import { publicKey, RpcAccount, Umi } from "@metaplex-foundation/umi";
|
3
3
|
import { toWeb3JsPublicKey } from "@metaplex-foundation/umi-web3js-adapters";
|
4
4
|
import {
|
5
5
|
Bank,
|
@@ -23,6 +23,7 @@ import { PositionState, PositionTokenUsage } from "../generated";
|
|
23
23
|
import { USD_DECIMALS } from "../constants/generalAccounts";
|
24
24
|
import { LivePositionUpdates } from "./solauto/generalUtils";
|
25
25
|
import { currentUnixSecondsSolana } from "./solanaUtils";
|
26
|
+
import { getSolautoPositionAccount } from "./accountUtils";
|
26
27
|
|
27
28
|
export function findMarginfiAccounts(bank: PublicKey): MarginfiAssetAccounts {
|
28
29
|
for (const key in MARGINFI_ACCOUNTS) {
|
@@ -94,7 +95,7 @@ export async function getMaxLtvAndLiqThreshold(
|
|
94
95
|
BigInt(
|
95
96
|
Math.round(
|
96
97
|
bytesToI80F48(supply.bank!.totalAssetShares.value) *
|
97
|
-
|
98
|
+
bytesToI80F48(supply.bank!.assetShareValue.value)
|
98
99
|
)
|
99
100
|
),
|
100
101
|
supply.bank!.mintDecimals
|
@@ -119,7 +120,7 @@ export async function getAllMarginfiAccountsByAuthority(
|
|
119
120
|
): Promise<
|
120
121
|
{ marginfiAccount: PublicKey; supplyMint?: PublicKey; debtMint?: PublicKey }[]
|
121
122
|
> {
|
122
|
-
const
|
123
|
+
const getAccounts = async (authorityPk: PublicKey) => await umi.rpc.getProgramAccounts(
|
123
124
|
MARGINFI_PROGRAM_ID,
|
124
125
|
{
|
125
126
|
commitment: "finalized",
|
@@ -133,17 +134,25 @@ export async function getAllMarginfiAccountsByAuthority(
|
|
133
134
|
},
|
134
135
|
{
|
135
136
|
memcmp: {
|
136
|
-
bytes: new Uint8Array(
|
137
|
+
bytes: new Uint8Array(authorityPk.toBuffer()),
|
137
138
|
offset: 40, // Anchor account discriminator + group pubkey
|
138
139
|
},
|
139
140
|
},
|
140
141
|
],
|
141
142
|
}
|
142
|
-
)
|
143
|
+
)
|
144
|
+
|
145
|
+
let marginfiAccounts: RpcAccount[] = [];
|
146
|
+
let selfManagedSolautoPositions: RpcAccount[] = [];
|
147
|
+
if (compatibleWithSolauto) {
|
148
|
+
[marginfiAccounts, selfManagedSolautoPositions] = await Promise.all([getAccounts(authority), getAccounts(getSolautoPositionAccount(authority, 0))]);
|
149
|
+
} else {
|
150
|
+
marginfiAccounts = await getAccounts(authority);
|
151
|
+
}
|
143
152
|
|
144
153
|
if (compatibleWithSolauto) {
|
145
154
|
const positionStates = await Promise.all(
|
146
|
-
marginfiAccounts.map(async (x) => ({
|
155
|
+
[...selfManagedSolautoPositions, ...marginfiAccounts].map(async (x) => ({
|
147
156
|
publicKey: x.publicKey,
|
148
157
|
state: await getMarginfiAccountPositionState(
|
149
158
|
umi,
|
@@ -190,7 +199,7 @@ async function getTokenUsage(
|
|
190
199
|
amountCanBeUsed = isAsset
|
191
200
|
? Number(bank.config.depositLimit) - totalDeposited
|
192
201
|
: totalDeposited -
|
193
|
-
|
202
|
+
bytesToI80F48(bank.totalLiabilityShares.value) * liabilityShareValue;
|
194
203
|
}
|
195
204
|
|
196
205
|
return {
|
@@ -200,22 +209,22 @@ async function getTokenUsage(
|
|
200
209
|
baseUnit: BigInt(Math.round(amountUsed)),
|
201
210
|
baseAmountUsdValue: bank
|
202
211
|
? toBaseUnit(
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
212
|
+
fromBaseUnit(BigInt(Math.round(amountUsed)), bank.mintDecimals) *
|
213
|
+
marketPrice,
|
214
|
+
USD_DECIMALS
|
215
|
+
)
|
207
216
|
: BigInt(0),
|
208
217
|
},
|
209
218
|
amountCanBeUsed: {
|
210
219
|
baseUnit: BigInt(Math.round(amountCanBeUsed)),
|
211
220
|
baseAmountUsdValue: bank
|
212
221
|
? toBaseUnit(
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
222
|
+
fromBaseUnit(
|
223
|
+
BigInt(Math.round(amountCanBeUsed)),
|
224
|
+
bank.mintDecimals
|
225
|
+
) * marketPrice,
|
226
|
+
USD_DECIMALS
|
227
|
+
)
|
219
228
|
: BigInt(0),
|
220
229
|
},
|
221
230
|
baseAmountMarketPriceUsd: toBaseUnit(marketPrice, USD_DECIMALS),
|
@@ -242,16 +251,16 @@ export async function getMarginfiAccountPositionState(
|
|
242
251
|
let supplyBank: Bank | null =
|
243
252
|
supplyMint && supplyMint !== PublicKey.default
|
244
253
|
? await safeFetchBank(
|
245
|
-
|
246
|
-
|
247
|
-
|
254
|
+
umi,
|
255
|
+
publicKey(MARGINFI_ACCOUNTS[supplyMint.toString()].bank)
|
256
|
+
)
|
248
257
|
: null;
|
249
258
|
let debtBank: Bank | null =
|
250
259
|
debtMint && debtMint !== PublicKey.default
|
251
260
|
? await safeFetchBank(
|
252
|
-
|
253
|
-
|
254
|
-
|
261
|
+
umi,
|
262
|
+
publicKey(MARGINFI_ACCOUNTS[debtMint.toString()].bank)
|
263
|
+
)
|
255
264
|
: null;
|
256
265
|
|
257
266
|
let supplyUsage: PositionTokenUsage | undefined = undefined;
|