@haven-fi/solauto-sdk 1.0.205 → 1.0.207
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/solautoMarginfiClient.d.ts.map +1 -1
- package/dist/clients/solautoMarginfiClient.js +3 -3
- package/dist/transactions/transactionUtils.d.ts.map +1 -1
- package/dist/transactions/transactionUtils.js +1 -2
- package/dist/utils/marginfiUtils.d.ts +15 -2
- package/dist/utils/marginfiUtils.d.ts.map +1 -1
- package/dist/utils/marginfiUtils.js +29 -16
- package/package.json +1 -1
- package/src/clients/solautoMarginfiClient.ts +3 -4
- package/src/transactions/transactionUtils.ts +1 -2
- package/src/utils/marginfiUtils.ts +57 -38
- package/tests/transactions/solautoMarginfi.ts +20 -20
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"solautoMarginfiClient.d.ts","sourceRoot":"","sources":["../../src/clients/solautoMarginfiClient.ts"],"names":[],"mappings":"AAIA,OAAO,EACL,MAAM,EACN,kBAAkB,EAMnB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,SAAS,EAA8B,MAAM,iBAAiB,CAAC;AACxE,OAAO,EAAE,aAAa,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AACnE,OAAO,EAAE,qBAAqB,EAAE,MAAM,mBAAmB,CAAC;AAM1D,OAAO,EACL,kBAAkB,EAElB,aAAa,EAEb,iBAAiB,EACjB,wBAAwB,EACxB,gCAAgC,EAKjC,MAAM,cAAc,CAAC;AAGtB,OAAO,EAEL,eAAe,EAUhB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,gBAAgB,EAAE,MAAM,iCAAiC,CAAC;
|
|
1
|
+
{"version":3,"file":"solautoMarginfiClient.d.ts","sourceRoot":"","sources":["../../src/clients/solautoMarginfiClient.ts"],"names":[],"mappings":"AAIA,OAAO,EACL,MAAM,EACN,kBAAkB,EAMnB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,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wBAAwB,IAAI,OAAO,CAAC,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,SAAS,CAAC;IA0BvE,yBAAyB,IAAI,kBAAkB;IAS/C,YAAY,CACV,aAAa,CAAC,EAAE,gCAAgC,EAChD,GAAG,CAAC,EAAE,kBAAkB,GACvB,kBAAkB;IAMrB,OAAO,CAAC,sBAAsB;IA6C9B,OAAO,IAAI,kBAAkB;IAc7B,mBAAmB,CAAC,IAAI,EAAE,iBAAiB,GAAG,kBAAkB;IAUhE,OAAO,CAAC,6BAA6B;IAmErC,OAAO,CAAC,oCAAoC;IA0D5C,SAAS,CACP,aAAa,EAAE,GAAG,GAAG,GAAG,EACxB,QAAQ,EAAE,aAAa,EACvB,aAAa,EAAE,wBAAwB,EACvC,SAAS,CAAC,EAAE,gBAAgB,EAC5B,2BAA2B,CAAC,EAAE,MAAM,GACnC,kBAAkB;IAqErB,WAAW,CACT,gBAAgB,EAAE,gBAAgB,EAClC,uBAAuB,EAAE,SAAS,GACjC,kBAAkB;IA2BrB,UAAU,CAAC,gBAAgB,EAAE,gBAAgB,GAAG,kBAAkB;IAoFlE,iCAAiC,IAAI,kBAAkB;IASjD,qBAAqB,IAAI,OAAO,CAAC,aAAa,GAAG,SAAS,CAAC;CA6ClE"}
|
|
@@ -429,10 +429,10 @@ class SolautoMarginfiClient extends solautoClient_1.SolautoClient {
|
|
|
429
429
|
if (state) {
|
|
430
430
|
return state;
|
|
431
431
|
}
|
|
432
|
-
const freshState = await (0, marginfiUtils_1.getMarginfiAccountPositionState)(this.umi, this.marginfiAccountPk, this.marginfiGroup, !this.selfManaged && this.solautoPositionData === null
|
|
433
|
-
? this.supplyMint
|
|
432
|
+
const freshState = await (0, marginfiUtils_1.getMarginfiAccountPositionState)(this.umi, { pk: this.marginfiAccountPk }, this.marginfiGroup, !this.selfManaged && this.solautoPositionData === null
|
|
433
|
+
? { mint: this.supplyMint }
|
|
434
434
|
: undefined, !this.selfManaged && this.solautoPositionData === null
|
|
435
|
-
? this.debtMint
|
|
435
|
+
? { mint: this.debtMint }
|
|
436
436
|
: undefined, this.livePositionUpdates);
|
|
437
437
|
if (freshState) {
|
|
438
438
|
this.log("Fresh state", freshState);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transactionUtils.d.ts","sourceRoot":"","sources":["../../src/transactions/transactionUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,kBAAkB,EAClB,GAAG,EAGJ,MAAM,0BAA0B,CAAC;AAElC,OAAO,EAAE,SAAS,EAA8B,MAAM,iBAAiB,CAAC;AAKxE,OAAO,EAGL,aAAa,EAYd,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAqOzD,wBAAsB,qBAAqB,CACzC,MAAM,EAAE,aAAa,EACrB,EAAE,EAAE,kBAAkB,EACtB,sBAAsB,EAAE,MAAM,EAAE,GAC/B,OAAO,CAAC,kBAAkB,CAAC,CA+G7B;AAkLD,wBAAsB,oBAAoB,CACxC,MAAM,EAAE,aAAa,EACrB,EAAE,EAAE,kBAAkB,GACrB,OAAO,CAAC,CAAC,kBAAkB,EAAE,kBAAkB,CAAC,CAAC,CA0BnD;AAED,wBAAsB,8BAA8B,CAAC,MAAM,EAAE,aAAa,
|
|
1
|
+
{"version":3,"file":"transactionUtils.d.ts","sourceRoot":"","sources":["../../src/transactions/transactionUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,kBAAkB,EAClB,GAAG,EAGJ,MAAM,0BAA0B,CAAC;AAElC,OAAO,EAAE,SAAS,EAA8B,MAAM,iBAAiB,CAAC;AAKxE,OAAO,EAGL,aAAa,EAYd,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAqOzD,wBAAsB,qBAAqB,CACzC,MAAM,EAAE,aAAa,EACrB,EAAE,EAAE,kBAAkB,EACtB,sBAAsB,EAAE,MAAM,EAAE,GAC/B,OAAO,CAAC,kBAAkB,CAAC,CA+G7B;AAkLD,wBAAsB,oBAAoB,CACxC,MAAM,EAAE,aAAa,EACrB,EAAE,EAAE,kBAAkB,GACrB,OAAO,CAAC,CAAC,kBAAkB,EAAE,kBAAkB,CAAC,CAAC,CA0BnD;AAED,wBAAsB,8BAA8B,CAAC,MAAM,EAAE,aAAa,oBAwCzE;AAED,wBAAsB,gCAAgC,CACpD,MAAM,EAAE,aAAa,EACrB,2BAA2B,CAAC,EAAE,MAAM,EACpC,UAAU,CAAC,EAAE,MAAM,GAClB,OAAO,CACN;IACE,EAAE,EAAE,kBAAkB,CAAC;IACvB,oBAAoB,EAAE,MAAM,EAAE,CAAC;CAChC,GACD,SAAS,CACZ,CAkHA;AAED,wBAAsB,gCAAgC,CACpD,GAAG,EAAE,GAAG,EACR,aAAa,EAAE,aAAa,EAC5B,YAAY,EAAE,SAAS,GACtB,OAAO,CAAC,CAAC,kBAAkB,EAAE,MAAM,EAAE,CAAC,GAAG,SAAS,CAAC,CAmCrD;AAED,wBAAgB,YAAY,CAAC,EAAE,EAAE,kBAAkB,EAAE,KAAK,EAAE,GAAG;;;;EAwC9D"}
|
|
@@ -340,8 +340,7 @@ async function requiresRefreshBeforeRebalance(client) {
|
|
|
340
340
|
oldStateWithLatestPrices.liqUtilizationRateBps);
|
|
341
341
|
if (client.livePositionUpdates.supplyAdjustment === BigInt(0) &&
|
|
342
342
|
client.livePositionUpdates.debtAdjustment === BigInt(0) &&
|
|
343
|
-
utilizationRateDiff
|
|
344
|
-
0.005) {
|
|
343
|
+
utilizationRateDiff >= 20) {
|
|
345
344
|
return true;
|
|
346
345
|
}
|
|
347
346
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { PublicKey } from "@solana/web3.js";
|
|
2
2
|
import { Umi } from "@metaplex-foundation/umi";
|
|
3
|
-
import { Bank } from "../marginfi-sdk";
|
|
3
|
+
import { Bank, MarginfiAccount } from "../marginfi-sdk";
|
|
4
4
|
import { MarginfiAssetAccounts } from "../types/accounts";
|
|
5
5
|
import { PositionState } from "../generated";
|
|
6
6
|
import { LivePositionUpdates } from "./solauto/generalUtils";
|
|
@@ -18,7 +18,20 @@ export declare function getAllMarginfiAccountsByAuthority(umi: Umi, authority: P
|
|
|
18
18
|
supplyMint?: PublicKey;
|
|
19
19
|
debtMint?: PublicKey;
|
|
20
20
|
}[]>;
|
|
21
|
-
|
|
21
|
+
interface BankSelection {
|
|
22
|
+
mint?: PublicKey;
|
|
23
|
+
banksCache?: BanksCache;
|
|
24
|
+
}
|
|
25
|
+
type BanksCache = {
|
|
26
|
+
[group: string]: {
|
|
27
|
+
[mint: string]: Bank;
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
export declare function getMarginfiAccountPositionState(umi: Umi, protocolAccount: {
|
|
31
|
+
pk: PublicKey;
|
|
32
|
+
data?: MarginfiAccount;
|
|
33
|
+
}, marginfiGroup?: PublicKey, supply?: BankSelection, debt?: BankSelection, livePositionUpdates?: LivePositionUpdates): Promise<PositionState | undefined>;
|
|
22
34
|
export declare function calculateAnnualAPYs(bank: Bank): [number, number];
|
|
23
35
|
export declare function getUpToDateShareValues(bank: Bank): Promise<[number, number]>;
|
|
36
|
+
export {};
|
|
24
37
|
//# 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,CAAC;AAC5C,OAAO,EAAa,GAAG,EAAE,MAAM,0BAA0B,CAAC;AAE1D,OAAO,EACL,IAAI,
|
|
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,OA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AI,CAAA;KAAE,CAAA;CAAE,CAAC;AAEhE,wBAAsB,+BAA+B,CACnD,GAAG,EAAE,GAAG,EACR,eAAe,EAAE;IAAE,EAAE,EAAE,SAAS,CAAC;IAAC,IAAI,CAAC,EAAE,eAAe,CAAA;CAAE,EAC1D,aAAa,CAAC,EAAE,SAAS,EACzB,MAAM,CAAC,EAAE,aAAa,EACtB,IAAI,CAAC,EAAE,aAAa,EACpB,mBAAmB,CAAC,EAAE,mBAAmB,GACxC,OAAO,CAAC,aAAa,GAAG,SAAS,CAAC,CA0LpC;AA+DD,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,IAAI,oBAU7C;AAED,wBAAsB,sBAAsB,CAC1C,IAAI,EAAE,IAAI,GACT,OAAO,CAAC,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAgB3B"}
|
|
@@ -86,7 +86,7 @@ async function getAllMarginfiAccountsByAuthority(umi, authority, compatibleWithS
|
|
|
86
86
|
if (compatibleWithSolauto) {
|
|
87
87
|
const positionStates = await Promise.all(marginfiAccounts.map(async (x) => ({
|
|
88
88
|
publicKey: x.publicKey,
|
|
89
|
-
state: await getMarginfiAccountPositionState(umi, (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(x.publicKey)),
|
|
89
|
+
state: await getMarginfiAccountPositionState(umi, { pk: (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(x.publicKey) }),
|
|
90
90
|
})));
|
|
91
91
|
return positionStates
|
|
92
92
|
.sort((a, b) => (0, numberUtils_1.fromBaseUnit)(b.state?.netWorth.baseAmountUsdValue ?? BigInt(0), generalAccounts_1.USD_DECIMALS) -
|
|
@@ -143,17 +143,30 @@ async function getTokenUsage(bank, isAsset, shares, amountUsedAdjustment) {
|
|
|
143
143
|
padding: new Uint8Array([]),
|
|
144
144
|
};
|
|
145
145
|
}
|
|
146
|
-
async function getMarginfiAccountPositionState(umi,
|
|
147
|
-
let marginfiAccount =
|
|
146
|
+
async function getMarginfiAccountPositionState(umi, protocolAccount, marginfiGroup, supply, debt, livePositionUpdates) {
|
|
147
|
+
let marginfiAccount = protocolAccount.data ??
|
|
148
|
+
(await (0, marginfi_sdk_1.safeFetchMarginfiAccount)(umi, (0, umi_1.publicKey)(protocolAccount.pk), {
|
|
149
|
+
commitment: "confirmed",
|
|
150
|
+
}));
|
|
151
|
+
if (!supply) {
|
|
152
|
+
supply = {};
|
|
153
|
+
}
|
|
154
|
+
if (!debt) {
|
|
155
|
+
debt = {};
|
|
156
|
+
}
|
|
148
157
|
if (!marginfiGroup && marginfiAccount) {
|
|
149
158
|
marginfiGroup = (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(marginfiAccount.group);
|
|
150
159
|
}
|
|
151
|
-
let supplyBank =
|
|
152
|
-
?
|
|
153
|
-
:
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
160
|
+
let supplyBank = supply?.banksCache && supply.mint && marginfiGroup
|
|
161
|
+
? supply.banksCache[marginfiGroup.toString()][supply?.mint?.toString()]
|
|
162
|
+
: supply?.mint && supply?.mint !== web3_js_1.PublicKey.default
|
|
163
|
+
? await (0, marginfi_sdk_1.safeFetchBank)(umi, (0, umi_1.publicKey)(marginfiAccounts_1.MARGINFI_ACCOUNTS[marginfiGroup?.toString() ?? ""][supply?.mint.toString()].bank), { commitment: "confirmed" })
|
|
164
|
+
: null;
|
|
165
|
+
let debtBank = debt?.banksCache && debt.mint && marginfiGroup
|
|
166
|
+
? debt.banksCache[marginfiGroup.toString()][debt?.mint?.toString()]
|
|
167
|
+
: debt?.mint && debt?.mint !== web3_js_1.PublicKey.default
|
|
168
|
+
? await (0, marginfi_sdk_1.safeFetchBank)(umi, (0, umi_1.publicKey)(marginfiAccounts_1.MARGINFI_ACCOUNTS[marginfiGroup?.toString() ?? ""][debt?.mint.toString()].bank), { commitment: "confirmed" })
|
|
169
|
+
: null;
|
|
157
170
|
let supplyUsage = undefined;
|
|
158
171
|
let debtUsage = undefined;
|
|
159
172
|
if (marginfiAccount !== null &&
|
|
@@ -170,8 +183,8 @@ async function getMarginfiAccountPositionState(umi, marginfiAccountPk, marginfiG
|
|
|
170
183
|
commitment: "confirmed",
|
|
171
184
|
});
|
|
172
185
|
}
|
|
173
|
-
if (!
|
|
174
|
-
|
|
186
|
+
if (!supply.mint) {
|
|
187
|
+
supply.mint = (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(supplyBank.mint);
|
|
175
188
|
}
|
|
176
189
|
supplyUsage = await getTokenUsage(supplyBank, true, (0, numberUtils_1.bytesToI80F48)(supplyBalances[0].assetShares.value), livePositionUpdates?.supplyAdjustment);
|
|
177
190
|
}
|
|
@@ -181,8 +194,8 @@ async function getMarginfiAccountPositionState(umi, marginfiAccountPk, marginfiG
|
|
|
181
194
|
commitment: "confirmed",
|
|
182
195
|
});
|
|
183
196
|
}
|
|
184
|
-
if (!
|
|
185
|
-
|
|
197
|
+
if (!debt.mint) {
|
|
198
|
+
debt.mint = (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(debtBank.mint);
|
|
186
199
|
}
|
|
187
200
|
debtUsage = await getTokenUsage(debtBank, false, (0, numberUtils_1.bytesToI80F48)(debtBalances[0].liabilityShares.value), livePositionUpdates?.debtAdjustment);
|
|
188
201
|
}
|
|
@@ -204,7 +217,7 @@ async function getMarginfiAccountPositionState(umi, marginfiAccountPk, marginfiG
|
|
|
204
217
|
if (!debtUsage) {
|
|
205
218
|
debtUsage = await getTokenUsage(debtBank, false, 0, livePositionUpdates?.debtAdjustment);
|
|
206
219
|
}
|
|
207
|
-
const supplyPrice = (0, generalUtils_1.safeGetPrice)(
|
|
220
|
+
const supplyPrice = (0, generalUtils_1.safeGetPrice)(supply.mint);
|
|
208
221
|
let [maxLtv, liqThreshold] = await getMaxLtvAndLiqThreshold(umi, marginfiGroup ?? new web3_js_1.PublicKey(marginfiAccounts_1.DEFAULT_MARGINFI_GROUP), {
|
|
209
222
|
mint: (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(supplyBank.mint),
|
|
210
223
|
bank: supplyBank,
|
|
@@ -238,7 +251,7 @@ function marginfiInterestRateCurve(bank, utilizationRatio) {
|
|
|
238
251
|
return (utilizationRatio * plateauIr) / optimalUr;
|
|
239
252
|
}
|
|
240
253
|
else {
|
|
241
|
-
return (((
|
|
254
|
+
return (((utilizationRatio - optimalUr) / (1 - optimalUr)) * (maxIr - plateauIr) +
|
|
242
255
|
plateauIr);
|
|
243
256
|
}
|
|
244
257
|
}
|
|
@@ -251,7 +264,7 @@ function calcInterestRate(bank, utilizationRatio) {
|
|
|
251
264
|
const insuranceFixedFeeApr = (0, numberUtils_1.bytesToI80F48)(bank.config.interestRateConfig.insuranceFeeFixedApr.value);
|
|
252
265
|
const rateFee = protocolIrFee + insuranceIrFee;
|
|
253
266
|
const totalFixedFeeApr = protocolFixedFeeApr + insuranceFixedFeeApr;
|
|
254
|
-
const borrowingRate =
|
|
267
|
+
const borrowingRate = baseRate * (1 + rateFee) + totalFixedFeeApr;
|
|
255
268
|
return [lendingRate, borrowingRate];
|
|
256
269
|
}
|
|
257
270
|
function calcAccruedInterestPaymentPerPeriod(apr, timeDelta, shareValue) {
|
package/package.json
CHANGED
|
@@ -55,7 +55,6 @@ import {
|
|
|
55
55
|
getMaxLtvAndLiqThreshold,
|
|
56
56
|
} from "../utils/marginfiUtils";
|
|
57
57
|
import { bytesToI80F48, fromBaseUnit, toBps } from "../utils/numberUtils";
|
|
58
|
-
import { createFakePositionState } from "../utils";
|
|
59
58
|
import { QuoteResponse } from "@jup-ag/api";
|
|
60
59
|
|
|
61
60
|
export interface SolautoMarginfiClientArgs extends SolautoClientArgs {
|
|
@@ -650,13 +649,13 @@ export class SolautoMarginfiClient extends SolautoClient {
|
|
|
650
649
|
|
|
651
650
|
const freshState = await getMarginfiAccountPositionState(
|
|
652
651
|
this.umi,
|
|
653
|
-
this.marginfiAccountPk,
|
|
652
|
+
{ pk: this.marginfiAccountPk },
|
|
654
653
|
this.marginfiGroup,
|
|
655
654
|
!this.selfManaged && this.solautoPositionData === null
|
|
656
|
-
? this.supplyMint
|
|
655
|
+
? { mint: this.supplyMint }
|
|
657
656
|
: undefined,
|
|
658
657
|
!this.selfManaged && this.solautoPositionData === null
|
|
659
|
-
? this.debtMint
|
|
658
|
+
? { mint: this.debtMint }
|
|
660
659
|
: undefined,
|
|
661
660
|
this.livePositionUpdates
|
|
662
661
|
);
|
|
@@ -612,8 +612,7 @@ export async function requiresRefreshBeforeRebalance(client: SolautoClient) {
|
|
|
612
612
|
if (
|
|
613
613
|
client.livePositionUpdates.supplyAdjustment === BigInt(0) &&
|
|
614
614
|
client.livePositionUpdates.debtAdjustment === BigInt(0) &&
|
|
615
|
-
utilizationRateDiff
|
|
616
|
-
0.005
|
|
615
|
+
utilizationRateDiff >= 20
|
|
617
616
|
) {
|
|
618
617
|
return true;
|
|
619
618
|
}
|
|
@@ -5,6 +5,7 @@ import {
|
|
|
5
5
|
Bank,
|
|
6
6
|
getMarginfiAccountSize,
|
|
7
7
|
MARGINFI_PROGRAM_ID,
|
|
8
|
+
MarginfiAccount,
|
|
8
9
|
safeFetchBank,
|
|
9
10
|
safeFetchMarginfiAccount,
|
|
10
11
|
} from "../marginfi-sdk";
|
|
@@ -168,7 +169,7 @@ export async function getAllMarginfiAccountsByAuthority(
|
|
|
168
169
|
publicKey: x.publicKey,
|
|
169
170
|
state: await getMarginfiAccountPositionState(
|
|
170
171
|
umi,
|
|
171
|
-
toWeb3JsPublicKey(x.publicKey)
|
|
172
|
+
{ pk: toWeb3JsPublicKey(x.publicKey) }
|
|
172
173
|
),
|
|
173
174
|
}))
|
|
174
175
|
);
|
|
@@ -256,48 +257,66 @@ async function getTokenUsage(
|
|
|
256
257
|
};
|
|
257
258
|
}
|
|
258
259
|
|
|
260
|
+
interface BankSelection {
|
|
261
|
+
mint?: PublicKey;
|
|
262
|
+
banksCache?: BanksCache;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
type BanksCache = { [group: string]: { [mint: string]: Bank } };
|
|
266
|
+
|
|
259
267
|
export async function getMarginfiAccountPositionState(
|
|
260
268
|
umi: Umi,
|
|
261
|
-
|
|
269
|
+
protocolAccount: { pk: PublicKey; data?: MarginfiAccount },
|
|
262
270
|
marginfiGroup?: PublicKey,
|
|
263
|
-
|
|
264
|
-
|
|
271
|
+
supply?: BankSelection,
|
|
272
|
+
debt?: BankSelection,
|
|
265
273
|
livePositionUpdates?: LivePositionUpdates
|
|
266
274
|
): Promise<PositionState | undefined> {
|
|
267
|
-
let marginfiAccount =
|
|
268
|
-
|
|
269
|
-
publicKey(
|
|
270
|
-
|
|
271
|
-
|
|
275
|
+
let marginfiAccount =
|
|
276
|
+
protocolAccount.data ??
|
|
277
|
+
(await safeFetchMarginfiAccount(umi, publicKey(protocolAccount.pk), {
|
|
278
|
+
commitment: "confirmed",
|
|
279
|
+
}));
|
|
280
|
+
|
|
281
|
+
if (!supply) {
|
|
282
|
+
supply = {};
|
|
283
|
+
}
|
|
284
|
+
if (!debt) {
|
|
285
|
+
debt = {};
|
|
286
|
+
}
|
|
272
287
|
|
|
273
288
|
if (!marginfiGroup && marginfiAccount) {
|
|
274
289
|
marginfiGroup = toWeb3JsPublicKey(marginfiAccount.group);
|
|
275
290
|
}
|
|
276
291
|
|
|
277
292
|
let supplyBank: Bank | null =
|
|
278
|
-
|
|
279
|
-
?
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
293
|
+
supply?.banksCache && supply.mint && marginfiGroup
|
|
294
|
+
? supply.banksCache[marginfiGroup!.toString()][supply?.mint?.toString()]
|
|
295
|
+
: supply?.mint && supply?.mint !== PublicKey.default
|
|
296
|
+
? await safeFetchBank(
|
|
297
|
+
umi,
|
|
298
|
+
publicKey(
|
|
299
|
+
MARGINFI_ACCOUNTS[marginfiGroup?.toString() ?? ""][
|
|
300
|
+
supply?.mint.toString()
|
|
301
|
+
].bank
|
|
302
|
+
),
|
|
303
|
+
{ commitment: "confirmed" }
|
|
304
|
+
)
|
|
305
|
+
: null;
|
|
289
306
|
let debtBank: Bank | null =
|
|
290
|
-
|
|
291
|
-
?
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
307
|
+
debt?.banksCache && debt.mint && marginfiGroup
|
|
308
|
+
? debt.banksCache[marginfiGroup!.toString()][debt?.mint?.toString()]
|
|
309
|
+
: debt?.mint && debt?.mint !== PublicKey.default
|
|
310
|
+
? await safeFetchBank(
|
|
311
|
+
umi,
|
|
312
|
+
publicKey(
|
|
313
|
+
MARGINFI_ACCOUNTS[marginfiGroup?.toString() ?? ""][
|
|
314
|
+
debt?.mint.toString()
|
|
315
|
+
].bank
|
|
316
|
+
),
|
|
317
|
+
{ commitment: "confirmed" }
|
|
318
|
+
)
|
|
319
|
+
: null;
|
|
301
320
|
|
|
302
321
|
let supplyUsage: PositionTokenUsage | undefined = undefined;
|
|
303
322
|
let debtUsage: PositionTokenUsage | undefined = undefined;
|
|
@@ -326,8 +345,8 @@ export async function getMarginfiAccountPositionState(
|
|
|
326
345
|
commitment: "confirmed",
|
|
327
346
|
});
|
|
328
347
|
}
|
|
329
|
-
if (!
|
|
330
|
-
|
|
348
|
+
if (!supply.mint) {
|
|
349
|
+
supply.mint = toWeb3JsPublicKey(supplyBank!.mint);
|
|
331
350
|
}
|
|
332
351
|
supplyUsage = await getTokenUsage(
|
|
333
352
|
supplyBank!,
|
|
@@ -343,8 +362,8 @@ export async function getMarginfiAccountPositionState(
|
|
|
343
362
|
commitment: "confirmed",
|
|
344
363
|
});
|
|
345
364
|
}
|
|
346
|
-
if (!
|
|
347
|
-
|
|
365
|
+
if (!debt.mint) {
|
|
366
|
+
debt.mint = toWeb3JsPublicKey(debtBank!.mint);
|
|
348
367
|
}
|
|
349
368
|
debtUsage = await getTokenUsage(
|
|
350
369
|
debtBank!,
|
|
@@ -393,7 +412,7 @@ export async function getMarginfiAccountPositionState(
|
|
|
393
412
|
);
|
|
394
413
|
}
|
|
395
414
|
|
|
396
|
-
const supplyPrice = safeGetPrice(
|
|
415
|
+
const supplyPrice = safeGetPrice(supply.mint!)!;
|
|
397
416
|
let [maxLtv, liqThreshold] = await getMaxLtvAndLiqThreshold(
|
|
398
417
|
umi,
|
|
399
418
|
marginfiGroup ?? new PublicKey(DEFAULT_MARGINFI_GROUP),
|
|
@@ -458,7 +477,7 @@ function marginfiInterestRateCurve(
|
|
|
458
477
|
return (utilizationRatio * plateauIr) / optimalUr;
|
|
459
478
|
} else {
|
|
460
479
|
return (
|
|
461
|
-
((
|
|
480
|
+
((utilizationRatio - optimalUr) / (1 - optimalUr)) * (maxIr - plateauIr) +
|
|
462
481
|
plateauIr
|
|
463
482
|
);
|
|
464
483
|
}
|
|
@@ -486,7 +505,7 @@ function calcInterestRate(
|
|
|
486
505
|
);
|
|
487
506
|
const rateFee = protocolIrFee + insuranceIrFee;
|
|
488
507
|
const totalFixedFeeApr = protocolFixedFeeApr + insuranceFixedFeeApr;
|
|
489
|
-
const borrowingRate =
|
|
508
|
+
const borrowingRate = baseRate * (1 + rateFee) + totalFixedFeeApr;
|
|
490
509
|
|
|
491
510
|
return [lendingRate, borrowingRate];
|
|
492
511
|
}
|
|
@@ -1,16 +1,19 @@
|
|
|
1
1
|
import { describe, it } from "mocha";
|
|
2
2
|
import { none, publicKey, some } from "@metaplex-foundation/umi";
|
|
3
3
|
import { setupTest } from "../shared";
|
|
4
|
-
import {
|
|
5
|
-
SolautoMarginfiClient,
|
|
6
|
-
} from "../../src/clients/solautoMarginfiClient";
|
|
4
|
+
import { SolautoMarginfiClient } from "../../src/clients/solautoMarginfiClient";
|
|
7
5
|
import {
|
|
8
6
|
solautoAction,
|
|
9
7
|
SolautoSettingsParametersInpArgs,
|
|
10
8
|
} from "../../src/generated";
|
|
11
9
|
import { buildSolautoRebalanceTransaction } from "../../src/transactions/transactionUtils";
|
|
12
|
-
import {
|
|
13
|
-
|
|
10
|
+
import {
|
|
11
|
+
maxBoostToBps,
|
|
12
|
+
maxRepayFromBps,
|
|
13
|
+
maxRepayToBps,
|
|
14
|
+
toBaseUnit,
|
|
15
|
+
} from "../../src/utils/numberUtils";
|
|
16
|
+
import { NATIVE_MINT } from "@solana/spl-token";
|
|
14
17
|
import { fetchTokenPrices } from "../../src/utils/generalUtils";
|
|
15
18
|
import {
|
|
16
19
|
TransactionItem,
|
|
@@ -18,18 +21,17 @@ import {
|
|
|
18
21
|
} from "../../src/transactions/transactionsManager";
|
|
19
22
|
import { PublicKey } from "@solana/web3.js";
|
|
20
23
|
import { USDC } from "../../src/constants";
|
|
21
|
-
import { buildHeliusApiUrl
|
|
24
|
+
import { buildHeliusApiUrl } from "../../src/utils";
|
|
22
25
|
|
|
23
26
|
describe("Solauto Marginfi tests", async () => {
|
|
24
27
|
// const signer = setupTest();
|
|
25
28
|
const signer = setupTest("solauto-manager");
|
|
26
29
|
|
|
27
|
-
const payForTransactions =
|
|
30
|
+
const payForTransactions = false;
|
|
28
31
|
const useJitoBundle = false;
|
|
29
32
|
const positionId = 1;
|
|
30
33
|
|
|
31
34
|
it("open - deposit - borrow - rebalance to 0 - withdraw - close", async () => {
|
|
32
|
-
|
|
33
35
|
const client = new SolautoMarginfiClient(
|
|
34
36
|
buildHeliusApiUrl(process.env.HELIUS_API_KEY!),
|
|
35
37
|
true
|
|
@@ -39,18 +41,16 @@ describe("Solauto Marginfi tests", async () => {
|
|
|
39
41
|
const supplyDecimals = 9;
|
|
40
42
|
const debtDecimals = 6;
|
|
41
43
|
|
|
42
|
-
await client.initialize(
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
}
|
|
53
|
-
);
|
|
44
|
+
await client.initialize({
|
|
45
|
+
signer,
|
|
46
|
+
positionId,
|
|
47
|
+
authority: new PublicKey("AprYCPiVeKMCgjQ2ZufwChMzvQ5kFjJo2ekTLSkXsQDm"),
|
|
48
|
+
// marginfiAccount: new PublicKey(
|
|
49
|
+
// "4nNvUXF5YqHFcH2nGweSiuvy1ct7V5FXfoCLKFYUN36z"
|
|
50
|
+
// ),
|
|
51
|
+
// supplyMint: NATIVE_MINT,
|
|
52
|
+
// debtMint: new PublicKey(USDC),
|
|
53
|
+
});
|
|
54
54
|
|
|
55
55
|
const transactionItems: TransactionItem[] = [];
|
|
56
56
|
// const settingParams: SolautoSettingsParametersInpArgs = {
|