@haven-fi/solauto-sdk 1.0.302 → 1.0.304
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 +1 -2
- package/dist/clients/solautoMarginfiClient.d.ts.map +1 -1
- package/dist/clients/solautoMarginfiClient.js +20 -24
- package/dist/transactions/transactionUtils.d.ts.map +1 -1
- package/dist/transactions/transactionUtils.js +4 -3
- package/dist/transactions/transactionsManager.d.ts.map +1 -1
- package/dist/transactions/transactionsManager.js +15 -0
- package/dist/utils/generalUtils.d.ts +2 -6
- package/dist/utils/generalUtils.d.ts.map +1 -1
- package/dist/utils/generalUtils.js +0 -85
- package/dist/utils/index.d.ts +2 -0
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +2 -0
- package/dist/utils/jupiterUtils.d.ts.map +1 -1
- package/dist/utils/jupiterUtils.js +10 -2
- package/dist/utils/marginfiUtils.d.ts +2 -2
- package/dist/utils/marginfiUtils.d.ts.map +1 -1
- package/dist/utils/marginfiUtils.js +13 -16
- package/dist/utils/priceUtils.d.ts +12 -0
- package/dist/utils/priceUtils.d.ts.map +1 -0
- package/dist/utils/priceUtils.js +97 -0
- package/dist/utils/solanaUtils.d.ts.map +1 -1
- package/dist/utils/solanaUtils.js +1 -1
- package/dist/utils/solauto/generalUtils.d.ts.map +1 -1
- package/dist/utils/solauto/generalUtils.js +2 -1
- package/dist/utils/solauto/rebalanceUtils.d.ts.map +1 -1
- package/dist/utils/solauto/rebalanceUtils.js +5 -4
- package/dist/utils/switchboardUtils.d.ts +7 -0
- package/dist/utils/switchboardUtils.d.ts.map +1 -0
- package/dist/utils/switchboardUtils.js +41 -0
- package/local/createISMAccounts.ts +59 -0
- package/local/updateMarginfiLUT.ts +0 -1
- package/local/updateSolautoLUT.ts +38 -7
- package/package.json +2 -1
- package/src/clients/solautoMarginfiClient.ts +18 -27
- package/src/constants/README.md +0 -2
- package/src/transactions/transactionUtils.ts +8 -3
- package/src/transactions/transactionsManager.ts +33 -2
- package/src/utils/generalUtils.ts +2 -147
- package/src/utils/index.ts +3 -1
- package/src/utils/jupiterUtils.ts +13 -3
- package/src/utils/marginfiUtils.ts +5 -25
- package/src/utils/priceUtils.ts +154 -0
- package/src/utils/solanaUtils.ts +1 -2
- package/src/utils/solauto/generalUtils.ts +3 -2
- package/src/utils/solauto/rebalanceUtils.ts +2 -1
- package/src/utils/switchboardUtils.ts +66 -0
- package/tests/transactions/solautoMarginfi.ts +29 -24
- package/tests/unit/rebalanceCalculations.ts +6 -5
- package/local/createSolautoManagerAccount.ts +0 -48
@@ -31,7 +31,7 @@ export declare class SolautoMarginfiClient extends SolautoClient {
|
|
31
31
|
defaultLookupTables(): string[];
|
32
32
|
lutAccountsToAdd(): PublicKey[];
|
33
33
|
maxLtvAndLiqThresholdBps(): Promise<[number, number] | undefined>;
|
34
|
-
marginfiAccountInitialize(): TransactionBuilder;
|
34
|
+
marginfiAccountInitialize(marginfiAccount: Signer): TransactionBuilder;
|
35
35
|
openPosition(settingParams?: SolautoSettingsParametersInpArgs, dca?: DCASettingsInpArgs): TransactionBuilder;
|
36
36
|
private marginfiOpenPositionIx;
|
37
37
|
refresh(): TransactionBuilder;
|
@@ -41,7 +41,6 @@ export declare class SolautoMarginfiClient extends SolautoClient {
|
|
41
41
|
rebalance(rebalanceStep: "A" | "B", jupQuote: QuoteResponse, rebalanceType: SolautoRebalanceTypeArgs, rebalanceValues: RebalanceValues, flashLoan?: FlashLoanDetails, targetLiqUtilizationRateBps?: number): TransactionBuilder;
|
42
42
|
flashBorrow(flashLoanDetails: FlashLoanDetails, destinationTokenAccount: PublicKey): TransactionBuilder;
|
43
43
|
flashRepay(flashLoanDetails: FlashLoanDetails): TransactionBuilder;
|
44
|
-
createIntermediaryMarginfiAccount(): TransactionBuilder;
|
45
44
|
getFreshPositionState(): Promise<PositionState | undefined>;
|
46
45
|
}
|
47
46
|
//# sourceMappingURL=solautoMarginfiClient.d.ts.map
|
@@ -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,EAGb,iBAAiB,EACjB,wBAAwB,EACxB,gCAAgC,EAKjC,MAAM,cAAc,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,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,EAGb,iBAAiB,EACjB,wBAAwB,EACxB,gCAAgC,EAKjC,MAAM,cAAc,CAAC;AAGtB,OAAO,EAEL,eAAe,EAUhB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EACL,gBAAgB,EAChB,eAAe,EAChB,MAAM,iCAAiC,CAAC;AAQzC,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAG5C,MAAM,WAAW,yBAA0B,SAAQ,iBAAiB;IAClE,eAAe,CAAC,EAAE,SAAS,GAAG,MAAM,CAAC;IACrC,sBAAsB,CAAC,EAAE,MAAM,CAAC;IAChC,aAAa,CAAC,EAAE,SAAS,CAA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M,GACnC,kBAAkB;IAgFrB,WAAW,CACT,gBAAgB,EAAE,gBAAgB,EAClC,uBAAuB,EAAE,SAAS,GACjC,kBAAkB;IA2BrB,UAAU,CAAC,gBAAgB,EAAE,gBAAgB,GAAG,kBAAkB;IAoF5D,qBAAqB,IAAI,OAAO,CAAC,aAAa,GAAG,SAAS,CAAC;CA8ClE"}
|
@@ -12,6 +12,7 @@ const generalUtils_1 = require("../utils/generalUtils");
|
|
12
12
|
const marginfi_sdk_1 = require("../marginfi-sdk");
|
13
13
|
const marginfiUtils_1 = require("../utils/marginfiUtils");
|
14
14
|
const numberUtils_1 = require("../utils/numberUtils");
|
15
|
+
const utils_1 = require("../utils");
|
15
16
|
class SolautoMarginfiClient extends solautoClient_1.SolautoClient {
|
16
17
|
constructor() {
|
17
18
|
super(...arguments);
|
@@ -64,21 +65,24 @@ class SolautoMarginfiClient extends solautoClient_1.SolautoClient {
|
|
64
65
|
const existingMarginfiAccounts = (await (0, marginfiUtils_1.getAllMarginfiAccountsByAuthority)(this.connection, this.umi, (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(this.signer.publicKey), false))
|
65
66
|
.filter((x) => !x.marginfiAccount.equals(this.marginfiAccountPk))
|
66
67
|
.sort((a, b) => a.marginfiAccount.toString().localeCompare(b.marginfiAccount.toString()));
|
67
|
-
const
|
68
|
-
? (await (0, marginfi_sdk_1.safeFetchAllMarginfiAccount)(this.umi, existingMarginfiAccounts.map((x) => (0, umi_1.publicKey)(x.marginfiAccount)))).filter((x) => x.
|
69
|
-
|
70
|
-
Math.round((0, numberUtils_1.bytesToI80F48)(y.
|
68
|
+
const compatibleMarginfiAccounts = existingMarginfiAccounts.length > 0
|
69
|
+
? (await (0, marginfi_sdk_1.safeFetchAllMarginfiAccount)(this.umi, existingMarginfiAccounts.map((x) => (0, umi_1.publicKey)(x.marginfiAccount)))).filter((x) => x.group.toString() === this.marginfiGroup.toString() &&
|
70
|
+
x.lendingAccount.balances.find((y) => y.bankPk.toString() !== web3_js_1.PublicKey.default.toString() &&
|
71
|
+
(Math.round((0, numberUtils_1.bytesToI80F48)(y.assetShares.value)) != 0 ||
|
72
|
+
Math.round((0, numberUtils_1.bytesToI80F48)(y.liabilityShares.value)) != 0)) === undefined)
|
71
73
|
: [];
|
72
74
|
this.intermediaryMarginfiAccountSigner =
|
73
|
-
|
75
|
+
compatibleMarginfiAccounts.length > 0
|
74
76
|
? undefined
|
75
77
|
: (0, umi_1.createSignerFromKeypair)(this.umi, this.umi.eddsa.generateKeypair());
|
76
78
|
this.intermediaryMarginfiAccountPk =
|
77
|
-
|
78
|
-
? (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(
|
79
|
+
compatibleMarginfiAccounts.length > 0
|
80
|
+
? (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(compatibleMarginfiAccounts[0].publicKey)
|
79
81
|
: (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(this.intermediaryMarginfiAccountSigner.publicKey);
|
80
82
|
this.intermediaryMarginfiAccount =
|
81
|
-
|
83
|
+
compatibleMarginfiAccounts.length > 0
|
84
|
+
? compatibleMarginfiAccounts[0]
|
85
|
+
: undefined;
|
82
86
|
}
|
83
87
|
protocolAccount() {
|
84
88
|
return this.marginfiAccountPk;
|
@@ -105,7 +109,7 @@ class SolautoMarginfiClient extends solautoClient_1.SolautoClient {
|
|
105
109
|
return [0, 0];
|
106
110
|
}
|
107
111
|
else {
|
108
|
-
const [maxLtv, liqThreshold] = await (0, marginfiUtils_1.getMaxLtvAndLiqThreshold)(this.
|
112
|
+
const [maxLtv, liqThreshold] = await (0, marginfiUtils_1.getMaxLtvAndLiqThreshold)(this.umi, this.marginfiGroup, {
|
109
113
|
mint: this.supplyMint,
|
110
114
|
}, {
|
111
115
|
mint: this.debtMint,
|
@@ -115,9 +119,9 @@ class SolautoMarginfiClient extends solautoClient_1.SolautoClient {
|
|
115
119
|
return [this.maxLtvBps, this.liqThresholdBps];
|
116
120
|
}
|
117
121
|
}
|
118
|
-
marginfiAccountInitialize() {
|
122
|
+
marginfiAccountInitialize(marginfiAccount) {
|
119
123
|
return (0, marginfi_sdk_1.marginfiAccountInitialize)(this.umi, {
|
120
|
-
marginfiAccount:
|
124
|
+
marginfiAccount: marginfiAccount,
|
121
125
|
marginfiGroup: (0, umi_1.publicKey)(this.marginfiGroup),
|
122
126
|
authority: this.signer,
|
123
127
|
feePayer: this.signer,
|
@@ -364,7 +368,7 @@ class SolautoMarginfiClient extends solautoClient_1.SolautoClient {
|
|
364
368
|
bankLiquidityVaultAuthority: (0, umi_1.publicKey)(bank.vaultAuthority),
|
365
369
|
destinationTokenAccount: (0, umi_1.publicKey)(destinationTokenAccount),
|
366
370
|
marginfiAccount: (0, umi_1.publicKey)(this.intermediaryMarginfiAccountPk),
|
367
|
-
marginfiGroup: (0, umi_1.publicKey)(
|
371
|
+
marginfiGroup: (0, umi_1.publicKey)(this.marginfiGroup),
|
368
372
|
signer: this.signer,
|
369
373
|
}));
|
370
374
|
}
|
@@ -420,7 +424,7 @@ class SolautoMarginfiClient extends solautoClient_1.SolautoClient {
|
|
420
424
|
bank: (0, umi_1.publicKey)(accounts.data.bank),
|
421
425
|
bankLiquidityVault: (0, umi_1.publicKey)(accounts.data.liquidityVault),
|
422
426
|
marginfiAccount: (0, umi_1.publicKey)(this.intermediaryMarginfiAccountPk),
|
423
|
-
marginfiGroup: (0, umi_1.publicKey)(
|
427
|
+
marginfiGroup: (0, umi_1.publicKey)(this.marginfiGroup),
|
424
428
|
signer: this.signer,
|
425
429
|
signerTokenAccount: (0, umi_1.publicKey)((0, accountUtils_1.getTokenAccount)((0, umi_web3js_adapters_1.toWeb3JsPublicKey)(this.signer.publicKey), flashLoanDetails.mint)),
|
426
430
|
}))
|
@@ -429,14 +433,6 @@ class SolautoMarginfiClient extends solautoClient_1.SolautoClient {
|
|
429
433
|
signer: this.signer,
|
430
434
|
}).addRemainingAccounts(remainingAccounts));
|
431
435
|
}
|
432
|
-
createIntermediaryMarginfiAccount() {
|
433
|
-
return (0, marginfi_sdk_1.marginfiAccountInitialize)(this.umi, {
|
434
|
-
marginfiAccount: this.intermediaryMarginfiAccountSigner,
|
435
|
-
marginfiGroup: (0, umi_1.publicKey)(marginfiAccounts_1.DEFAULT_MARGINFI_GROUP),
|
436
|
-
authority: this.signer,
|
437
|
-
feePayer: this.signer,
|
438
|
-
});
|
439
|
-
}
|
440
436
|
async getFreshPositionState() {
|
441
437
|
const state = await super.getFreshPositionState();
|
442
438
|
if (state) {
|
@@ -445,11 +441,11 @@ class SolautoMarginfiClient extends solautoClient_1.SolautoClient {
|
|
445
441
|
const useDesignatedMint = !this.selfManaged &&
|
446
442
|
(this.solautoPositionData === null ||
|
447
443
|
!(0, umi_web3js_adapters_1.toWeb3JsPublicKey)(this.signer.publicKey).equals(this.authority));
|
448
|
-
const freshState = await (0, marginfiUtils_1.getMarginfiAccountPositionState)(this.
|
444
|
+
const freshState = await (0, marginfiUtils_1.getMarginfiAccountPositionState)(this.umi, { pk: this.marginfiAccountPk }, this.marginfiGroup, useDesignatedMint ? { mint: this.supplyMint } : undefined, useDesignatedMint ? { mint: this.debtMint } : undefined, this.livePositionUpdates);
|
449
445
|
if (freshState) {
|
450
446
|
this.log("Fresh state", freshState);
|
451
|
-
const supplyPrice = (0,
|
452
|
-
const debtPrice = (0,
|
447
|
+
const supplyPrice = (0, utils_1.safeGetPrice)(freshState?.supply.mint);
|
448
|
+
const debtPrice = (0, utils_1.safeGetPrice)(freshState?.debt.mint);
|
453
449
|
this.log("Supply price: ", supplyPrice);
|
454
450
|
this.log("Debt price: ", debtPrice);
|
455
451
|
this.log("Liq threshold bps:", freshState.liqThresholdBps);
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"transactionUtils.d.ts","sourceRoot":"","sources":["../../src/transactions/transactionUtils.ts"],"names":[],"mappings":"AAAA,OAAO,
|
1
|
+
{"version":3,"file":"transactionUtils.d.ts","sourceRoot":"","sources":["../../src/transactions/transactionUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,kBAAkB,EAClB,GAAG,EAGJ,MAAM,0BAA0B,CAAC;AAElC,OAAO,EAAE,SAAS,EAA8B,MAAM,iBAAiB,CAAC;AAmBxE,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAoCzD,OAAO,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAOlD,OAAO,EAAE,qBAAqB,EAAE,MAAM,UAAU,CAAC;AA2LjD,wBAAsB,qBAAqB,CACzC,MAAM,EAAE,aAAa,EACrB,EAAE,EAAE,kBAAkB,EACtB,sBAAsB,EAAE,MAAM,EAAE,GAC/B,OAAO,CAAC,kBAAkB,CAAC,CAoH7B;AA2LD,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,oBA4CzE;AAED,wBAAsB,gCAAgC,CACpD,MAAM,EAAE,aAAa,EACrB,2BAA2B,CAAC,EAAE,MAAM,EACpC,UAAU,CAAC,EAAE,MAAM,GAClB,OAAO,CAAC,qBAAqB,GAAG,SAAS,CAAC,CAsH5C;AAED,wBAAsB,gCAAgC,CACpD,eAAe,EAAE,oBAAoB,EACrC,YAAY,EAAE,SAAS,EACvB,eAAe,EAAE,SAAS,GACzB,OAAO,CAAC,qBAAqB,GAAG,SAAS,CAAC,CAsC5C;AAED,wBAAgB,YAAY,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE,EAAE,kBAAkB,EAAE,KAAK,EAAE,GAAG;;;;EA0DxE"}
|
@@ -21,6 +21,7 @@ const accountUtils_1 = require("../utils/accountUtils");
|
|
21
21
|
const marginfi_sdk_1 = require("../marginfi-sdk");
|
22
22
|
const jupiter_sdk_1 = require("../jupiter-sdk");
|
23
23
|
const constants_1 = require("../constants");
|
24
|
+
const utils_1 = require("../utils");
|
24
25
|
function getWSolUsage(client, solautoActions, initiatingDcaIn, cancellingDcaIn) {
|
25
26
|
const supplyIsWsol = client.supplyMint.equals(spl_token_1.NATIVE_MINT);
|
26
27
|
const debtIsWsol = client.debtMint.equals(spl_token_1.NATIVE_MINT);
|
@@ -64,7 +65,7 @@ async function transactionChoresBefore(client, accountsGettingCreated, solautoAc
|
|
64
65
|
}
|
65
66
|
else if (client.lendingPlatform === generated_1.LendingPlatform.Marginfi &&
|
66
67
|
!(await (0, generalUtils_1.getSolanaAccountCreated)(client.umi, client.marginfiAccountPk))) {
|
67
|
-
chores = chores.add(client.marginfiAccountInitialize());
|
68
|
+
chores = chores.add(client.marginfiAccountInitialize(client.marginfiAccount));
|
68
69
|
}
|
69
70
|
// TODO: PF
|
70
71
|
}
|
@@ -157,7 +158,7 @@ async function rebalanceChoresBefore(client, tx, accountsGettingCreated) {
|
|
157
158
|
if (checkIntermediaryMfiAccount &&
|
158
159
|
!(0, generalUtils_1.rpcAccountCreated)(intermediaryMarginfiAccount)) {
|
159
160
|
client.log("Creating intermediary marginfi account");
|
160
|
-
chores = chores.add(client.
|
161
|
+
chores = chores.add(client.marginfiAccountInitialize(client.intermediaryMarginfiAccountSigner));
|
161
162
|
}
|
162
163
|
if (checkSignerSupplyTa &&
|
163
164
|
!(0, generalUtils_1.rpcAccountCreated)(signerSupplyTa) &&
|
@@ -361,7 +362,7 @@ async function buildSolautoRebalanceTransaction(client, targetLiqUtilizationRate
|
|
361
362
|
client.log("Not eligible for a rebalance");
|
362
363
|
return undefined;
|
363
364
|
}
|
364
|
-
const values = (0, rebalanceUtils_1.getRebalanceValues)(client.solautoPositionState, client.solautoPositionSettings(), client.solautoPositionActiveDca(), (0, generalUtils_1.currentUnixSeconds)(), (0,
|
365
|
+
const values = (0, rebalanceUtils_1.getRebalanceValues)(client.solautoPositionState, client.solautoPositionSettings(), client.solautoPositionActiveDca(), (0, generalUtils_1.currentUnixSeconds)(), (0, utils_1.safeGetPrice)(client.supplyMint), (0, utils_1.safeGetPrice)(client.debtMint), targetLiqUtilizationRateBps);
|
365
366
|
client.log("Rebalance values: ", values);
|
366
367
|
const swapDetails = (0, rebalanceUtils_1.getJupSwapRebalanceDetails)(client, values, targetLiqUtilizationRateBps, attemptNum);
|
367
368
|
const { jupQuote, lookupTableAddresses, setupInstructions, tokenLedgerIx, swapIx, } = await (0, jupiterUtils_1.getJupSwapTransaction)(client.signer, swapDetails, attemptNum);
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"transactionsManager.d.ts","sourceRoot":"","sources":["../../src/transactions/transactionsManager.ts"],"names":[],"mappings":"AACA,OAAO,EAGL,kBAAkB,EAEnB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAMzD,OAAO,EACL,aAAa,EAEd,MAAM,uBAAuB,CAAC;AAE/B,OAAO,EACL,kBAAkB,EAElB,qBAAqB,EACrB,kBAAkB,EACnB,MAAM,UAAU,CAAC;AAClB,OAAO,EAAE,oBAAoB,EAAa,MAAM,YAAY,CAAC;
|
1
|
+
{"version":3,"file":"transactionsManager.d.ts","sourceRoot":"","sources":["../../src/transactions/transactionsManager.ts"],"names":[],"mappings":"AACA,OAAO,EAGL,kBAAkB,EAEnB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAMzD,OAAO,EACL,aAAa,EAEd,MAAM,uBAAuB,CAAC;AAE/B,OAAO,EACL,kBAAkB,EAElB,qBAAqB,EACrB,kBAAkB,EACnB,MAAM,UAAU,CAAC;AAClB,OAAO,EAAE,oBAAoB,EAAa,MAAM,YAAY,CAAC;AAW7D,qBAAa,wBAAyB,SAAQ,KAAK;gBACrC,OAAO,EAAE,MAAM;CAK5B;AAqCD,qBAAa,eAAe;IAMjB,OAAO,EAAE,CACd,UAAU,EAAE,MAAM,KACf,OAAO,CAAC,qBAAqB,GAAG,SAAS,CAAC;IACxC,IAAI,CAAC,EA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U,GAAE,MAAY;YAQpB,uBAAuB;IAwCrC,OAAO,CAAC,YAAY;YA6CN,aAAa;IAoB3B,OAAO,CAAC,4BAA4B;IAgBpC,OAAO,CAAC,mBAAmB;YAMb,SAAS;IAgBV,UAAU,CACrB,YAAY,EAAE,eAAe,EAAE,GAC9B,OAAO,CAAC,0BAA0B,CAAC;IA0FzB,IAAI,CACf,KAAK,EAAE,eAAe,EAAE,GACvB,OAAO,CAAC,0BAA0B,CAAC;YA+BxB,qBAAqB;YAuDrB,cAAc;YAqCd,eAAe;CAmD9B"}
|
@@ -11,6 +11,8 @@ const generalUtils_1 = require("../utils/generalUtils");
|
|
11
11
|
const transactionUtils_1 = require("./transactionUtils");
|
12
12
|
const types_1 = require("../types");
|
13
13
|
const web3_js_1 = require("@solana/web3.js");
|
14
|
+
const switchboardConstants_1 = require("../constants/switchboardConstants");
|
15
|
+
const utils_1 = require("../utils");
|
14
16
|
// import { sendJitoBundledTransactions } from "../utils/jitoUtils";
|
15
17
|
const CHORES_TX_NAME = "account chores";
|
16
18
|
class TransactionTooLargeError extends Error {
|
@@ -258,6 +260,19 @@ class TransactionsManager {
|
|
258
260
|
for (const item of items) {
|
259
261
|
await item.initialize();
|
260
262
|
}
|
263
|
+
const allAccounts = items.flatMap((x) => x.tx
|
264
|
+
?.getInstructions()
|
265
|
+
.flatMap((x) => x.keys.map((x) => x.pubkey.toString())));
|
266
|
+
const swbOracle = allAccounts.find((x) => Object.values(switchboardConstants_1.SWITCHBOARD_PRICE_FEED_IDS).includes(x ?? ""));
|
267
|
+
if (swbOracle) {
|
268
|
+
const mint = new web3_js_1.PublicKey(Object.keys(switchboardConstants_1.SWITCHBOARD_PRICE_FEED_IDS).find((x) => switchboardConstants_1.SWITCHBOARD_PRICE_FEED_IDS[x] === swbOracle));
|
269
|
+
const stale = (await (0, utils_1.getSwitchboardPrices)(client.connection, [mint]))[0]
|
270
|
+
.stale;
|
271
|
+
if (stale) {
|
272
|
+
const swbTx = new TransactionItem(async () => (0, utils_1.buildSwbSubmitResponseTx)(client.connection, client.signer, mint), items.length === 1 ? "Update oracle" : "");
|
273
|
+
items.unshift(swbTx);
|
274
|
+
}
|
275
|
+
}
|
261
276
|
let [choresBefore, choresAfter] = await (0, transactionUtils_1.getTransactionChores)(client, (0, umi_1.transactionBuilder)().add(items
|
262
277
|
.filter((x) => x.tx && x.tx.getInstructions().length > 0)
|
263
278
|
.map((x) => x.tx)));
|
@@ -1,5 +1,5 @@
|
|
1
|
-
import {
|
2
|
-
import { MaybeRpcAccount, Umi
|
1
|
+
import { PublicKey } from "@solana/web3.js";
|
2
|
+
import { MaybeRpcAccount, Umi } from "@metaplex-foundation/umi";
|
3
3
|
export declare function consoleLog(...args: any[]): void;
|
4
4
|
export declare function generateRandomU8(): number;
|
5
5
|
export declare function generateRandomU64(): bigint;
|
@@ -8,10 +8,6 @@ export declare function getSolanaAccountCreated(umi: Umi, pk: PublicKey): Promis
|
|
8
8
|
export declare function rpcAccountCreated(account: MaybeRpcAccount): boolean;
|
9
9
|
export declare function arraysAreEqual(arrayA: number[], arrayB: number[]): boolean;
|
10
10
|
export declare function zip<T, U>(list1: T[], list2: U[]): [T, U][];
|
11
|
-
export declare function fetchTokenPrices(conn: Connection, mints: PublicKey[]): Promise<number[]>;
|
12
|
-
export declare function getPythPrices(mints: PublicKey[]): Promise<number[]>;
|
13
|
-
export declare function getSwitchboardPrices(conn: Connection, mints: PublicKey[]): Promise<number[]>;
|
14
|
-
export declare function safeGetPrice(mint: PublicKey | UmiPublicKey | undefined): number | undefined;
|
15
11
|
export type ErrorsToThrow = Array<new (...args: any[]) => Error>;
|
16
12
|
export declare function retryWithExponentialBackoff<T>(fn: (attemptNum: number, prevErr?: Error) => Promise<T>, retries?: number, delay?: number, errorsToThrow?: ErrorsToThrow): Promise<T>;
|
17
13
|
//# sourceMappingURL=generalUtils.d.ts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"generalUtils.d.ts","sourceRoot":"","sources":["../../src/ut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,7 +1,4 @@
|
|
1
1
|
"use strict";
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
-
};
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
6
3
|
exports.consoleLog = consoleLog;
|
7
4
|
exports.generateRandomU8 = generateRandomU8;
|
@@ -11,20 +8,8 @@ exports.getSolanaAccountCreated = getSolanaAccountCreated;
|
|
11
8
|
exports.rpcAccountCreated = rpcAccountCreated;
|
12
9
|
exports.arraysAreEqual = arraysAreEqual;
|
13
10
|
exports.zip = zip;
|
14
|
-
exports.fetchTokenPrices = fetchTokenPrices;
|
15
|
-
exports.getPythPrices = getPythPrices;
|
16
|
-
exports.getSwitchboardPrices = getSwitchboardPrices;
|
17
|
-
exports.safeGetPrice = safeGetPrice;
|
18
11
|
exports.retryWithExponentialBackoff = retryWithExponentialBackoff;
|
19
|
-
const web3_js_1 = require("@solana/web3.js");
|
20
12
|
const umi_1 = require("@metaplex-foundation/umi");
|
21
|
-
const pythConstants_1 = require("../constants/pythConstants");
|
22
|
-
const numberUtils_1 = require("./numberUtils");
|
23
|
-
const solautoConstants_1 = require("../constants/solautoConstants");
|
24
|
-
const on_demand_1 = require("@switchboard-xyz/on-demand");
|
25
|
-
const anchor_1 = require("@coral-xyz/anchor");
|
26
|
-
const switchboard_json_1 = __importDefault(require("../idls/switchboard.json"));
|
27
|
-
const switchboardConstants_1 = require("../constants/switchboardConstants");
|
28
13
|
function consoleLog(...args) {
|
29
14
|
if (globalThis.LOCAL_TEST) {
|
30
15
|
console.log(...args);
|
@@ -72,76 +57,6 @@ function zip(list1, list2) {
|
|
72
57
|
}
|
73
58
|
return result;
|
74
59
|
}
|
75
|
-
async function fetchTokenPrices(conn, mints) {
|
76
|
-
const currentTime = currentUnixSeconds();
|
77
|
-
if (!mints.some((mint) => !(mint.toString() in solautoConstants_1.PRICES) ||
|
78
|
-
currentTime - solautoConstants_1.PRICES[mint.toString()].time > 3)) {
|
79
|
-
return mints.map((mint) => solautoConstants_1.PRICES[mint.toString()].price);
|
80
|
-
}
|
81
|
-
const pythMints = mints.filter((x) => x.toString() in pythConstants_1.PYTH_PRICE_FEED_IDS);
|
82
|
-
const switchboardMints = mints.filter((x) => x.toString() in switchboardConstants_1.SWITCHBOARD_PRICE_FEED_IDS);
|
83
|
-
const [pythData, switchboardData] = await Promise.all([
|
84
|
-
zip(pythMints, await getPythPrices(pythMints)),
|
85
|
-
zip(switchboardMints, await getSwitchboardPrices(conn, switchboardMints)),
|
86
|
-
]);
|
87
|
-
const prices = mints.map((mint) => {
|
88
|
-
const item = [...pythData, ...switchboardData].find((data) => data[0].equals(mint));
|
89
|
-
return item ? item[1] : 0;
|
90
|
-
});
|
91
|
-
for (var i = 0; i < mints.length; i++) {
|
92
|
-
solautoConstants_1.PRICES[mints[i].toString()] = {
|
93
|
-
price: prices[i],
|
94
|
-
time: currentUnixSeconds(),
|
95
|
-
};
|
96
|
-
}
|
97
|
-
return prices;
|
98
|
-
}
|
99
|
-
async function getPythPrices(mints) {
|
100
|
-
const priceFeedIds = mints.map((mint) => pythConstants_1.PYTH_PRICE_FEED_IDS[mint.toString()]);
|
101
|
-
const getReq = async () => await fetch(`https://hermes.pyth.network/v2/updates/price/latest?${priceFeedIds.map((x) => `ids%5B%5D=${x}`).join("&")}`);
|
102
|
-
const prices = await retryWithExponentialBackoff(async () => {
|
103
|
-
let resp = await getReq();
|
104
|
-
let status = resp.status;
|
105
|
-
if (status !== 200) {
|
106
|
-
throw new Error(JSON.stringify(resp));
|
107
|
-
}
|
108
|
-
const json = await resp.json();
|
109
|
-
const prices = json.parsed.map((x) => {
|
110
|
-
if (x.price.expo > 0) {
|
111
|
-
return Number((0, numberUtils_1.toBaseUnit)(Number(x.price.price), x.price.expo));
|
112
|
-
}
|
113
|
-
else if (x.price.expo < 0) {
|
114
|
-
return (0, numberUtils_1.fromBaseUnit)(BigInt(x.price.price), Math.abs(x.price.expo));
|
115
|
-
}
|
116
|
-
else {
|
117
|
-
return Number(x.price.price);
|
118
|
-
}
|
119
|
-
});
|
120
|
-
return prices;
|
121
|
-
}, 5, 200);
|
122
|
-
return prices;
|
123
|
-
}
|
124
|
-
async function getSwitchboardPrices(conn, mints) {
|
125
|
-
const dummyWallet = {
|
126
|
-
publicKey: new web3_js_1.PublicKey("11111111111111111111111111111111"),
|
127
|
-
signTransaction: async (tx) => tx,
|
128
|
-
signAllTransactions: async (txs) => txs,
|
129
|
-
};
|
130
|
-
const provider = new anchor_1.AnchorProvider(conn, dummyWallet, anchor_1.AnchorProvider.defaultOptions());
|
131
|
-
const program = new anchor_1.Program(switchboard_json_1.default, provider);
|
132
|
-
const results = await Promise.all(mints.map(async (mint) => {
|
133
|
-
const feed = new on_demand_1.PullFeed(program, new web3_js_1.PublicKey(switchboardConstants_1.SWITCHBOARD_PRICE_FEED_IDS[mint.toString()]));
|
134
|
-
const result = await feed.loadData();
|
135
|
-
return Number(result.result.value) / Math.pow(10, 18);
|
136
|
-
}));
|
137
|
-
return results;
|
138
|
-
}
|
139
|
-
function safeGetPrice(mint) {
|
140
|
-
if (mint && mint?.toString() in solautoConstants_1.PRICES) {
|
141
|
-
return solautoConstants_1.PRICES[mint.toString()].price;
|
142
|
-
}
|
143
|
-
return undefined;
|
144
|
-
}
|
145
60
|
function retryWithExponentialBackoff(fn, retries = 5, delay = 150, errorsToThrow) {
|
146
61
|
return new Promise((resolve, reject) => {
|
147
62
|
const attempt = (attemptNum, prevErr) => {
|
package/dist/utils/index.d.ts
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,iBAAiB,CAAC;AAChC,cAAc,eAAe,CAAC;AAC9B,cAAc,eAAe,CAAC"}
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,iBAAiB,CAAC;AAChC,cAAc,eAAe,CAAC;AAC9B,cAAc,eAAe,CAAC;AAC9B,cAAc,cAAc,CAAC;AAC7B,cAAc,oBAAoB,CAAC"}
|
package/dist/utils/index.js
CHANGED
@@ -21,3 +21,5 @@ __exportStar(require("./jupiterUtils"), exports);
|
|
21
21
|
__exportStar(require("./marginfiUtils"), exports);
|
22
22
|
__exportStar(require("./numberUtils"), exports);
|
23
23
|
__exportStar(require("./solanaUtils"), exports);
|
24
|
+
__exportStar(require("./priceUtils"), exports);
|
25
|
+
__exportStar(require("./switchboardUtils"), exports);
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"jupiterUtils.d.ts","sourceRoot":"","sources":["../../src/utils/jupiterUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,MAAM,EACN,kBAAkB,EAEnB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,SAAS,EAA0B,MAAM,iBAAiB,CAAC;AAGpE,OAAO,EAGL,aAAa,EACd,MAAM,aAAa,CAAC;
|
1
|
+
{"version":3,"file":"jupiterUtils.d.ts","sourceRoot":"","sources":["../../src/utils/jupiterUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,MAAM,EACN,kBAAkB,EAEnB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,SAAS,EAA0B,MAAM,iBAAiB,CAAC;AAGpE,OAAO,EAGL,aAAa,EACd,MAAM,aAAa,CAAC;AAOrB,MAAM,WAAW,cAAc;IAC7B,SAAS,EAAE,SAAS,CAAC;IACrB,UAAU,EAAE,SAAS,CAAC;IACtB,iBAAiB,EAAE,SAAS,CAAC;IAC7B,MAAM,EAAE,MAAM,CAAC;IACf,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB;AAgBD,MAAM,WAAW,kBAAkB;IACjC,QAAQ,EAAE,aAAa,CAAC;IACxB,cAAc,EAAE,MAAM,CAAC;IACvB,oBAAoB,EAAE,MAAM,EAAE,CAAC;IAC/B,iBAAiB,EAAE,kBAAkB,CAAC;IACtC,aAAa,EAAE,kBAAkB,CAAC;IAClC,MAAM,EAAE,kBAAkB,CAAC;CAC5B;AAED,wBAAsB,qBAAqB,CACzC,MAAM,EAAE,MAAM,EACd,WAAW,EAAE,cAAc,EAC3B,UAAU,CAAC,EAAE,MAAM,GAClB,OAAO,CAAC,kBAAkB,CAAC,CAmG7B"}
|
@@ -8,6 +8,7 @@ const numberUtils_1 = require("./numberUtils");
|
|
8
8
|
const api_1 = require("@jup-ag/api");
|
9
9
|
const accountUtils_1 = require("./accountUtils");
|
10
10
|
const generalUtils_1 = require("./generalUtils");
|
11
|
+
const constants_1 = require("../constants");
|
11
12
|
const jupApi = (0, api_1.createJupiterApiClient)();
|
12
13
|
function createTransactionInstruction(instruction) {
|
13
14
|
return new web3_js_1.TransactionInstruction({
|
@@ -21,7 +22,8 @@ function createTransactionInstruction(instruction) {
|
|
21
22
|
});
|
22
23
|
}
|
23
24
|
async function getJupSwapTransaction(signer, swapDetails, attemptNum) {
|
24
|
-
|
25
|
+
const memecoinSwap = constants_1.TOKEN_INFO[swapDetails.inputMint.toString()].isMeme ||
|
26
|
+
constants_1.TOKEN_INFO[swapDetails.outputMint.toString()].isMeme;
|
25
27
|
const quoteResponse = await (0, generalUtils_1.retryWithExponentialBackoff)(async () => await jupApi.quoteGet({
|
26
28
|
amount: Number(swapDetails.amount),
|
27
29
|
inputMint: swapDetails.inputMint.toString(),
|
@@ -31,7 +33,7 @@ async function getJupSwapTransaction(signer, swapDetails, attemptNum) {
|
|
31
33
|
: swapDetails.exactIn
|
32
34
|
? "ExactIn"
|
33
35
|
: undefined,
|
34
|
-
slippageBps: 50,
|
36
|
+
slippageBps: memecoinSwap ? 150 : 50,
|
35
37
|
maxAccounts: !swapDetails.exactOut ? 60 : undefined,
|
36
38
|
}), 4, 200);
|
37
39
|
const priceImpactBps = Math.round((0, numberUtils_1.toBps)(parseFloat(quoteResponse.priceImpactPct))) + 1;
|
@@ -43,6 +45,12 @@ async function getJupSwapTransaction(signer, swapDetails, attemptNum) {
|
|
43
45
|
quoteResponse.inAmount = (parseInt(quoteResponse.inAmount) +
|
44
46
|
Math.ceil(parseInt(quoteResponse.inAmount) * (0, numberUtils_1.fromBps)(finalPriceSlippageBps))).toString();
|
45
47
|
}
|
48
|
+
// else {
|
49
|
+
// quoteResponse.inAmount = (
|
50
|
+
// parseInt(quoteResponse.inAmount) +
|
51
|
+
// Math.ceil(parseInt(quoteResponse.inAmount) * fromBps(priceImpactBps))
|
52
|
+
// ).toString();
|
53
|
+
// }
|
46
54
|
(0, generalUtils_1.consoleLog)("Getting jup instructions...");
|
47
55
|
const instructions = await (0, generalUtils_1.retryWithExponentialBackoff)(async () => await jupApi.swapInstructionsPost({
|
48
56
|
swapRequest: {
|
@@ -9,7 +9,7 @@ interface AllMarginfiAssetAccounts extends MarginfiAssetAccounts {
|
|
9
9
|
}
|
10
10
|
export declare function findMarginfiAccounts(bank: PublicKey): AllMarginfiAssetAccounts;
|
11
11
|
export declare function calcMaxLtvAndLiqThreshold(supplyBank: Bank, debtBank: Bank, supplyPrice: number): [number, number];
|
12
|
-
export declare function getMaxLtvAndLiqThreshold(
|
12
|
+
export declare function getMaxLtvAndLiqThreshold(umi: Umi, marginfiGroup: PublicKey, supply: {
|
13
13
|
mint: PublicKey;
|
14
14
|
bank?: Bank | null;
|
15
15
|
}, debt: {
|
@@ -30,7 +30,7 @@ type BanksCache = {
|
|
30
30
|
[mint: string]: Bank;
|
31
31
|
};
|
32
32
|
};
|
33
|
-
export declare function getMarginfiAccountPositionState(
|
33
|
+
export declare function getMarginfiAccountPositionState(umi: Umi, protocolAccount: {
|
34
34
|
pk: PublicKey;
|
35
35
|
data?: MarginfiAccount;
|
36
36
|
}, marginfiGroup?: PublicKey, supply?: BankSelection, debt?: BankSelection, livePositionUpdates?: LivePositionUpdates): Promise<PositionState | undefined>;
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"marginfiUtils.d.ts","sourceRoot":"","sources":["../../src/utils/marginfiUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AACxD,OAAO,EAAa,GAAG,EAAE,MAAM,0BAA0B,CAAC;AAE1D,OAAO,EACL,IAAI,EAGJ,eAAe,EAGhB,MAAM,iBAAiB,CAAC;
|
1
|
+
{"version":3,"file":"marginfiUtils.d.ts","sourceRoot":"","sources":["../../src/utils/marginfiUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AACxD,OAAO,EAAa,GAAG,EAAE,MAAM,0BAA0B,CAAC;AAE1D,OAAO,EACL,IAAI,EAGJ,eAAe,EAGhB,MAAM,iBAAiB,CAAC;AAazB,OAAO,EAAE,qBAAqB,EAAE,MAAM,mBAAmB,CAAC;AAC1D,OAAO,EAAE,aAAa,EAAsB,MAAM,cAAc,CAAC;AAEjE,OAAO,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAC;AAI7D,UAAU,wBAAyB,SAAQ,qBAAqB;IAC9D,IAAI,EAAE,SAAS,CAAC;CACjB;AAED,wBAAgB,oBAAoB,CAClC,IAAI,EAAE,SAAS,GACd,wBAAwB,CAY1B;AAED,wBAAgB,yBAAyB,CACvC,UAAU,EAAE,IAAI,EAChB,QAAQ,EAAE,IAAI,EACd,WAAW,EAAE,MAAM,GAClB,CAAC,MAAM,EAAE,MAAM,CAAC,CA6BlB;AAED,wBAAsB,wBAAwB,CAC5C,GAA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IAAI,EAAE,IAAI,GACT,OAAO,CAAC,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAgB3B"}
|
@@ -16,6 +16,7 @@ const numberUtils_1 = require("./numberUtils");
|
|
16
16
|
const marginfiAccounts_1 = require("../constants/marginfiAccounts");
|
17
17
|
const generalAccounts_1 = require("../constants/generalAccounts");
|
18
18
|
const constants_1 = require("../constants");
|
19
|
+
const priceUtils_1 = require("./priceUtils");
|
19
20
|
function findMarginfiAccounts(bank) {
|
20
21
|
for (const group in marginfiAccounts_1.MARGINFI_ACCOUNTS) {
|
21
22
|
for (const key in marginfiAccounts_1.MARGINFI_ACCOUNTS[group]) {
|
@@ -42,7 +43,7 @@ function calcMaxLtvAndLiqThreshold(supplyBank, debtBank, supplyPrice) {
|
|
42
43
|
}
|
43
44
|
return [maxLtv, liqThreshold];
|
44
45
|
}
|
45
|
-
async function getMaxLtvAndLiqThreshold(
|
46
|
+
async function getMaxLtvAndLiqThreshold(umi, marginfiGroup, supply, debt, supplyPrice) {
|
46
47
|
if (!supply.bank && supply.mint.equals(web3_js_1.PublicKey.default)) {
|
47
48
|
return [0, 0];
|
48
49
|
}
|
@@ -54,7 +55,7 @@ async function getMaxLtvAndLiqThreshold(conn, umi, marginfiGroup, supply, debt,
|
|
54
55
|
debt.bank = await (0, marginfi_sdk_1.safeFetchBank)(umi, (0, umi_1.publicKey)(marginfiAccounts_1.MARGINFI_ACCOUNTS[marginfiGroup.toString()][debt.mint.toString()].bank), { commitment: "confirmed" });
|
55
56
|
}
|
56
57
|
if (!supplyPrice) {
|
57
|
-
const [price] = await (0,
|
58
|
+
const [price] = await (0, priceUtils_1.fetchTokenPrices)([
|
58
59
|
(0, umi_web3js_adapters_1.toWeb3JsPublicKey)(supply.bank.mint),
|
59
60
|
]);
|
60
61
|
supplyPrice = price;
|
@@ -86,7 +87,7 @@ async function getAllMarginfiAccountsByAuthority(conn, umi, authority, compatibl
|
|
86
87
|
if (compatibleWithSolauto) {
|
87
88
|
const positionStates = await Promise.all(marginfiAccounts.map(async (x) => ({
|
88
89
|
publicKey: x.publicKey,
|
89
|
-
state: await getMarginfiAccountPositionState(
|
90
|
+
state: await getMarginfiAccountPositionState(umi, {
|
90
91
|
pk: (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(x.publicKey),
|
91
92
|
}),
|
92
93
|
})));
|
@@ -106,12 +107,12 @@ async function getAllMarginfiAccountsByAuthority(conn, umi, authority, compatibl
|
|
106
107
|
}));
|
107
108
|
}
|
108
109
|
}
|
109
|
-
async function getTokenUsage(
|
110
|
+
async function getTokenUsage(bank, isAsset, shares, amountUsedAdjustment) {
|
110
111
|
let amountUsed = 0;
|
111
112
|
let amountCanBeUsed = 0;
|
112
113
|
let marketPrice = 0;
|
113
114
|
if (bank !== null) {
|
114
|
-
[marketPrice] = await (0,
|
115
|
+
[marketPrice] = await (0, priceUtils_1.fetchTokenPrices)([
|
115
116
|
(0, umi_web3js_adapters_1.toWeb3JsPublicKey)(bank.mint),
|
116
117
|
]);
|
117
118
|
const [assetShareValue, liabilityShareValue] = await getUpToDateShareValues(bank);
|
@@ -147,7 +148,7 @@ async function getTokenUsage(conn, bank, isAsset, shares, amountUsedAdjustment)
|
|
147
148
|
padding: new Uint8Array([]),
|
148
149
|
};
|
149
150
|
}
|
150
|
-
async function getMarginfiAccountPositionState(
|
151
|
+
async function getMarginfiAccountPositionState(umi, protocolAccount, marginfiGroup, supply, debt, livePositionUpdates) {
|
151
152
|
let marginfiAccount = protocolAccount.data ??
|
152
153
|
(await (0, marginfi_sdk_1.safeFetchMarginfiAccount)(umi, (0, umi_1.publicKey)(protocolAccount.pk), {
|
153
154
|
commitment: "confirmed",
|
@@ -190,7 +191,7 @@ async function getMarginfiAccountPositionState(conn, umi, protocolAccount, margi
|
|
190
191
|
if (!supply.mint) {
|
191
192
|
supply.mint = (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(supplyBank.mint);
|
192
193
|
}
|
193
|
-
supplyUsage = await getTokenUsage(
|
194
|
+
supplyUsage = await getTokenUsage(supplyBank, true, (0, numberUtils_1.bytesToI80F48)(supplyBalances[0].assetShares.value), livePositionUpdates?.supplyAdjustment);
|
194
195
|
}
|
195
196
|
if (debtBalances.length > 0) {
|
196
197
|
if (debtBank === null) {
|
@@ -201,28 +202,24 @@ async function getMarginfiAccountPositionState(conn, umi, protocolAccount, margi
|
|
201
202
|
if (!debt.mint) {
|
202
203
|
debt.mint = (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(debtBank.mint);
|
203
204
|
}
|
204
|
-
debtUsage = await getTokenUsage(
|
205
|
+
debtUsage = await getTokenUsage(debtBank, false, (0, numberUtils_1.bytesToI80F48)(debtBalances[0].liabilityShares.value), livePositionUpdates?.debtAdjustment);
|
205
206
|
}
|
206
207
|
}
|
207
208
|
if (supplyBank === null) {
|
208
209
|
return undefined;
|
209
210
|
}
|
210
|
-
if (!(0, umi_web3js_adapters_1.toWeb3JsPublicKey)(supplyBank.group).equals(new web3_js_1.PublicKey(marginfiAccounts_1.DEFAULT_MARGINFI_GROUP))) {
|
211
|
-
// Temporarily disabled for now
|
212
|
-
return undefined;
|
213
|
-
}
|
214
211
|
if (!supplyUsage) {
|
215
|
-
supplyUsage = await getTokenUsage(
|
212
|
+
supplyUsage = await getTokenUsage(supplyBank, true, 0, livePositionUpdates?.supplyAdjustment);
|
216
213
|
}
|
217
214
|
if (constants_1.TOKEN_INFO[supplyBank.mint.toString()].isStableCoin &&
|
218
215
|
(debtBank === null || constants_1.TOKEN_INFO[debtBank.mint.toString()].isStableCoin)) {
|
219
216
|
return undefined;
|
220
217
|
}
|
221
218
|
if (!debtUsage) {
|
222
|
-
debtUsage = await getTokenUsage(
|
219
|
+
debtUsage = await getTokenUsage(debtBank, false, 0, livePositionUpdates?.debtAdjustment);
|
223
220
|
}
|
224
|
-
const supplyPrice = (0,
|
225
|
-
let [maxLtv, liqThreshold] = await getMaxLtvAndLiqThreshold(
|
221
|
+
const supplyPrice = (0, priceUtils_1.safeGetPrice)(supply.mint);
|
222
|
+
let [maxLtv, liqThreshold] = await getMaxLtvAndLiqThreshold(umi, marginfiGroup ?? new web3_js_1.PublicKey(marginfiAccounts_1.DEFAULT_MARGINFI_GROUP), {
|
226
223
|
mint: (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(supplyBank.mint),
|
227
224
|
bank: supplyBank,
|
228
225
|
}, {
|
@@ -0,0 +1,12 @@
|
|
1
|
+
import { Connection, PublicKey } from "@solana/web3.js";
|
2
|
+
import { PublicKey as UmiPublicKey } from "@metaplex-foundation/umi";
|
3
|
+
export declare function fetchTokenPrices(mints: PublicKey[]): Promise<number[]>;
|
4
|
+
export declare function getPythPrices(mints: PublicKey[]): Promise<number[]>;
|
5
|
+
export declare function getSwitchboardPrices(conn: Connection, mints: PublicKey[]): Promise<{
|
6
|
+
mint: PublicKey;
|
7
|
+
price: number;
|
8
|
+
stale: boolean;
|
9
|
+
}[]>;
|
10
|
+
export declare function safeGetPrice(mint: PublicKey | UmiPublicKey | undefined): number | undefined;
|
11
|
+
export declare function getJupTokenPrices(mints: PublicKey[]): Promise<number[]>;
|
12
|
+
//# sourceMappingURL=priceUtils.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"priceUtils.d.ts","sourceRoot":"","sources":["../../src/utils/priceUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AACxD,OAAO,EAAE,SAAS,IAAI,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAYrE,wBAAsB,gBAAgB,CACpC,KAAK,EAAE,SAAS,EAAE,GACjB,OAAO,CAAC,MAAM,EAAE,CAAC,CAqCnB;AAED,wBAAsB,aAAa,CAAC,KAAK,EAAE,SAAS,EAAE,qBAwCrD;AAED,wBAAsB,oBAAoB,CACxC,IAAI,EAAE,UAAU,EAChB,KAAK,EAAE,SAAS,EAAE,GACjB,OAAO,CAAC;IAAE,IAAI,EAAE,SAAS,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,OAAO,CAAA;CAAE,EAAE,CAAC,CAuB/D;AAED,wBAAgB,YAAY,CAC1B,IAAI,EAAE,SAAS,GAAG,YAAY,GAAG,SAAS,GACzC,MAAM,GAAG,SAAS,CAKpB;AAED,wBAAsB,iBAAiB,CAAC,KAAK,EAAE,SAAS,EAAE,qBAoBzD"}
|