@haven-fi/solauto-sdk 1.0.144 → 1.0.146
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/transactions/transactionUtils.d.ts.map +1 -1
- package/dist/transactions/transactionUtils.js +7 -7
- package/dist/utils/jupiterUtils.d.ts +2 -1
- package/dist/utils/jupiterUtils.d.ts.map +1 -1
- package/dist/utils/jupiterUtils.js +8 -3
- package/dist/utils/marginfiUtils.d.ts.map +1 -1
- package/dist/utils/marginfiUtils.js +1 -3
- package/dist/utils/solanaUtils.d.ts +0 -1
- package/dist/utils/solanaUtils.d.ts.map +1 -1
- package/dist/utils/solanaUtils.js +0 -10
- package/dist/utils/solauto/rebalanceUtils.d.ts +1 -1
- package/dist/utils/solauto/rebalanceUtils.d.ts.map +1 -1
- package/dist/utils/solauto/rebalanceUtils.js +4 -4
- package/package.json +1 -1
- package/src/transactions/transactionUtils.ts +7 -6
- package/src/utils/jupiterUtils.ts +11 -4
- package/src/utils/marginfiUtils.ts +1 -3
- package/src/utils/solanaUtils.ts +0 -10
- package/src/utils/solauto/rebalanceUtils.ts +5 -4
- package/tests/transactions/solautoMarginfi.ts +2 -2
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"transactionUtils.d.ts","sourceRoot":"","sources":["../../src/transactions/transactionUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,kBAAkB,EAClB,GAAG,EAGJ,MAAM,0BAA0B,CAAC;AAElC,OAAO,EAAE,SAAS,EAA8B,MAAM,iBAAiB,CAAC;AAKxE,OAAO,EAEL,aAAa,EAUd,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAyNzD,wBAAsB,qBAAqB,CACzC,MAAM,EAAE,aAAa,EACrB,EAAE,EAAE,kBAAkB,EACtB,sBAAsB,EAAE,MAAM,EAAE,GAC/B,OAAO,CAAC,kBAAkB,CAAC,CA+G7B;AAmLD,wBAAsB,oBAAoB,CACxC,MAAM,EAAE,aAAa,EACrB,EAAE,EAAE,kBAAkB,GACrB,OAAO,CAAC,CAAC,kBAAkB,EAAE,kBAAkB,CAAC,CAAC,CA0BnD;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,
|
1
|
+
{"version":3,"file":"transactionUtils.d.ts","sourceRoot":"","sources":["../../src/transactions/transactionUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,kBAAkB,EAClB,GAAG,EAGJ,MAAM,0BAA0B,CAAC;AAElC,OAAO,EAAE,SAAS,EAA8B,MAAM,iBAAiB,CAAC;AAKxE,OAAO,EAEL,aAAa,EAUd,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAyNzD,wBAAsB,qBAAqB,CACzC,MAAM,EAAE,aAAa,EACrB,EAAE,EAAE,kBAAkB,EACtB,sBAAsB,EAAE,MAAM,EAAE,GAC/B,OAAO,CAAC,kBAAkB,CAAC,CA+G7B;AAmLD,wBAAsB,oBAAoB,CACxC,MAAM,EAAE,aAAa,EACrB,EAAE,EAAE,kBAAkB,GACrB,OAAO,CAAC,CAAC,kBAAkB,EAAE,kBAAkB,CAAC,CAAC,CA0BnD;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,CA6HA;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"}
|
@@ -333,8 +333,8 @@ async function buildSolautoRebalanceTransaction(client, targetLiqUtilizationRate
|
|
333
333
|
const values = (0, rebalanceUtils_1.getRebalanceValues)(client.solautoPositionState, client.solautoPositionSettings(), client.solautoPositionActiveDca(), (0, generalUtils_1.currentUnixSeconds)(), (0, generalUtils_1.safeGetPrice)(client.supplyMint), (0, generalUtils_1.safeGetPrice)(client.debtMint), targetLiqUtilizationRateBps);
|
334
334
|
client.log("Rebalance values: ", values);
|
335
335
|
const swapDetails = (0, rebalanceUtils_1.getJupSwapRebalanceDetails)(client, values, targetLiqUtilizationRateBps, attemptNum);
|
336
|
-
const { jupQuote, lookupTableAddresses, setupInstructions, tokenLedgerIx, swapIx, } = await (0, jupiterUtils_1.getJupSwapTransaction)(client.signer, swapDetails, attemptNum);
|
337
|
-
const flashLoan = (0, rebalanceUtils_1.getFlashLoanDetails)(client, values, jupQuote);
|
336
|
+
const { jupQuote, priceImpactBps, lookupTableAddresses, setupInstructions, tokenLedgerIx, swapIx, } = await (0, jupiterUtils_1.getJupSwapTransaction)(client.signer, swapDetails, attemptNum);
|
337
|
+
const flashLoan = (0, rebalanceUtils_1.getFlashLoanDetails)(client, values, jupQuote, priceImpactBps);
|
338
338
|
let tx = (0, umi_1.transactionBuilder)();
|
339
339
|
if (flashLoan) {
|
340
340
|
client.log("Flash loan details: ", flashLoan);
|
@@ -348,11 +348,11 @@ async function buildSolautoRebalanceTransaction(client, targetLiqUtilizationRate
|
|
348
348
|
client.flashBorrow(flashLoan, (0, accountUtils_1.getTokenAccount)((0, umi_web3js_adapters_1.toWeb3JsPublicKey)(client.signer.publicKey), swapDetails.inputMint)),
|
349
349
|
...(addFirstRebalance
|
350
350
|
? [
|
351
|
-
client.rebalance("A", swapDetails, rebalanceType,
|
351
|
+
client.rebalance("A", swapDetails, rebalanceType, priceImpactBps, flashLoan, targetLiqUtilizationRateBps),
|
352
352
|
]
|
353
353
|
: []),
|
354
354
|
swapIx,
|
355
|
-
client.rebalance("B", swapDetails, rebalanceType,
|
355
|
+
client.rebalance("B", swapDetails, rebalanceType, priceImpactBps, flashLoan, targetLiqUtilizationRateBps),
|
356
356
|
client.flashRepay(flashLoan),
|
357
357
|
]);
|
358
358
|
}
|
@@ -361,9 +361,9 @@ async function buildSolautoRebalanceTransaction(client, targetLiqUtilizationRate
|
|
361
361
|
tx = tx.add([
|
362
362
|
setupInstructions,
|
363
363
|
tokenLedgerIx,
|
364
|
-
client.rebalance("A", swapDetails, rebalanceType,
|
364
|
+
client.rebalance("A", swapDetails, rebalanceType, priceImpactBps, undefined, targetLiqUtilizationRateBps),
|
365
365
|
swapIx,
|
366
|
-
client.rebalance("B", swapDetails, rebalanceType,
|
366
|
+
client.rebalance("B", swapDetails, rebalanceType, priceImpactBps, undefined, targetLiqUtilizationRateBps),
|
367
367
|
]);
|
368
368
|
}
|
369
369
|
if (client.solautoPositionState.liqUtilizationRateBps >
|
@@ -386,7 +386,7 @@ async function convertReferralFeesToDestination(umi, referralState, tokenAccount
|
|
386
386
|
inputMint: tokenAccountData.mint,
|
387
387
|
outputMint: (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(referralState.destFeesMint),
|
388
388
|
exactIn: true,
|
389
|
-
|
389
|
+
slippageIncFactor: 0.15,
|
390
390
|
});
|
391
391
|
let tx = (0, umi_1.transactionBuilder)()
|
392
392
|
.add(setupInstructions)
|
@@ -6,12 +6,13 @@ export interface JupSwapDetails {
|
|
6
6
|
outputMint: PublicKey;
|
7
7
|
destinationWallet: PublicKey;
|
8
8
|
amount: bigint;
|
9
|
-
|
9
|
+
slippageIncFactor?: number;
|
10
10
|
exactOut?: boolean;
|
11
11
|
exactIn?: boolean;
|
12
12
|
}
|
13
13
|
export interface JupSwapTransaction {
|
14
14
|
jupQuote: QuoteResponse;
|
15
|
+
priceImpactBps: number;
|
15
16
|
lookupTableAddresses: string[];
|
16
17
|
setupInstructions: TransactionBuilder;
|
17
18
|
tokenLedgerIx: TransactionBuilder;
|
@@ -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;AAMrB,MAAM,WAAW,cAAc;IAC7B,SAAS,EAAE,SAAS,CAAC;IACrB,UAAU,EAAE,SAAS,CAAC;IACtB,iBAAiB,EAAE,SAAS,CAAC;IAC7B,MAAM,EAAE,MAAM,CAAC;IACf,
|
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;AAMrB,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,CA6E7B"}
|
@@ -31,11 +31,12 @@ async function getJupSwapTransaction(signer, swapDetails, attemptNum) {
|
|
31
31
|
: swapDetails.exactIn
|
32
32
|
? "ExactIn"
|
33
33
|
: undefined,
|
34
|
-
slippageBps:
|
34
|
+
slippageBps: 30,
|
35
35
|
maxAccounts: !swapDetails.exactOut ? 60 : undefined,
|
36
36
|
}), 3);
|
37
|
-
const
|
38
|
-
|
37
|
+
const priceImpactBps = (Math.round((0, numberUtils_1.toBps)(parseFloat(quoteResponse.priceImpactPct))) + 1);
|
38
|
+
const finalPriceSlippageBps = Math.round(Math.max(50, quoteResponse.slippageBps, priceImpactBps) *
|
39
|
+
(1 + (swapDetails.slippageIncFactor ?? 0)));
|
39
40
|
quoteResponse.slippageBps = finalPriceSlippageBps;
|
40
41
|
console.log(quoteResponse);
|
41
42
|
console.log("Getting jup instructions...");
|
@@ -51,8 +52,12 @@ async function getJupSwapTransaction(signer, swapDetails, attemptNum) {
|
|
51
52
|
if (!instructions.swapInstruction) {
|
52
53
|
throw new Error("No swap instruction was returned by Jupiter");
|
53
54
|
}
|
55
|
+
console.log("Raw price impact bps:", priceImpactBps);
|
56
|
+
const finalPriceImpactBps = priceImpactBps * (1 + (swapDetails.slippageIncFactor ?? 0));
|
57
|
+
console.log("Increased price impact bps:", finalPriceImpactBps);
|
54
58
|
return {
|
55
59
|
jupQuote: quoteResponse,
|
60
|
+
priceImpactBps: finalPriceImpactBps,
|
56
61
|
lookupTableAddresses: instructions.addressLookupTableAddresses,
|
57
62
|
setupInstructions: (0, umi_1.transactionBuilder)().add(instructions.setupInstructions.map((ix) => (0, solanaUtils_1.getWrappedInstruction)(signer, createTransactionInstruction(ix)))),
|
58
63
|
tokenLedgerIx: (0, umi_1.transactionBuilder)().add(instructions.tokenLedgerInstruction !== undefined
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"marginfiUtils.d.ts","sourceRoot":"","sources":["../../src/utils/marginfiUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAa,GAAG,EAAE,MAAM,0BAA0B,CAAC;AAE1D,OAAO,EACL,IAAI,EAKL,MAAM,iBAAiB,CAAC;AAUzB,OAAO,EAAE,qBAAqB,EAAE,MAAM,mBAAmB,CAAC;AAC1D,OAAO,EAAE,aAAa,EAAsB,MAAM,cAAc,CAAC;AAEjE,OAAO,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAC;
|
1
|
+
{"version":3,"file":"marginfiUtils.d.ts","sourceRoot":"","sources":["../../src/utils/marginfiUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAa,GAAG,EAAE,MAAM,0BAA0B,CAAC;AAE1D,OAAO,EACL,IAAI,EAKL,MAAM,iBAAiB,CAAC;AAUzB,OAAO,EAAE,qBAAqB,EAAE,MAAM,mBAAmB,CAAC;AAC1D,OAAO,EAAE,aAAa,EAAsB,MAAM,cAAc,CAAC;AAEjE,OAAO,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAC;AAE7D,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,SAAS,GAAG,qBAAqB,CAU3E;AAED,wBAAsB,wBAAwB,CAC5C,GAAG,EAAE,GAAG,EACR,MAAM,EAAE;IACN,IAAI,EAAE,SAAS,CAAC;IAChB,IAAI,CAAC,EAAE,IAAI,GAAG,IAAI,CAAC;CACpB,EACD,IAAI,EAAE;IACJ,IAAI,EAAE,SAAS,CAAC;IAChB,IAAI,CAAC,EAAE,IAAI,GAAG,IAAI,CAAC;CACpB,EACD,WAAW,CAAC,EAAE,MAAM,GACnB,OAAO,CAAC,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAiE3B;AAED,wBAAsB,iCAAiC,CACrD,GAAG,EAAE,GAAG,EACR,SAAS,EAAE,SAAS,EACpB,qBAAqB,CAAC,EAAE,OAAO,GAC9B,OAAO,CACR;IAAE,eAAe,EAAE,SAAS,CAAC;IAAC,UAAU,CAAC,EAAE,SAAS,CAAC;IAAC,QAAQ,CAAC,EAAE,SAAS,CAAA;CAAE,EAAE,CAC/E,CA6CA;AAgED,wBAAsB,+BAA+B,CACnD,GAAG,EAAE,GAAG,EACR,iBAAiB,EAAE,SAAS,EAC5B,UAAU,CAAC,EAAE,SAAS,EACtB,QAAQ,CAAC,EAAE,SAAS,EACpB,mBAAmB,CAAC,EAAE,mBAAmB,GACxC,OAAO,CAAC,aAAa,GAAG,SAAS,CAAC,CAkJpC;AA+DD,wBAAsB,sBAAsB,CAC1C,GAAG,EAAE,GAAG,EACR,IAAI,EAAE,IAAI,GACT,OAAO,CAAC,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAyB3B"}
|
@@ -13,7 +13,6 @@ const generalUtils_1 = require("./generalUtils");
|
|
13
13
|
const numberUtils_1 = require("./numberUtils");
|
14
14
|
const marginfiAccounts_1 = require("../constants/marginfiAccounts");
|
15
15
|
const generalAccounts_1 = require("../constants/generalAccounts");
|
16
|
-
const solanaUtils_1 = require("./solanaUtils");
|
17
16
|
function findMarginfiAccounts(bank) {
|
18
17
|
for (const key in marginfiAccounts_1.MARGINFI_ACCOUNTS) {
|
19
18
|
const account = marginfiAccounts_1.MARGINFI_ACCOUNTS[key];
|
@@ -236,8 +235,7 @@ function calcAccruedInterestPaymentPerPeriod(apr, timeDelta, shareValue) {
|
|
236
235
|
return newValue;
|
237
236
|
}
|
238
237
|
async function getUpToDateShareValues(umi, bank) {
|
239
|
-
|
240
|
-
let timeDelta = currentTime - Number(bank.lastUpdate);
|
238
|
+
let timeDelta = (0, generalUtils_1.currentUnixSeconds)() - Number(bank.lastUpdate);
|
241
239
|
const totalAssets = (0, numberUtils_1.bytesToI80F48)(bank.totalAssetShares.value) *
|
242
240
|
(0, numberUtils_1.bytesToI80F48)(bank.assetShareValue.value);
|
243
241
|
const totalLiabilities = (0, numberUtils_1.bytesToI80F48)(bank.totalLiabilityShares.value) *
|
@@ -2,7 +2,6 @@ import { AddressLookupTableInput, Signer, TransactionBuilder, Umi, WrappedInstru
|
|
2
2
|
import { Connection, PublicKey, TransactionInstruction } from "@solana/web3.js";
|
3
3
|
import { PriorityFeeSetting, TransactionRunType } from "../types";
|
4
4
|
export declare function getSolanaRpcConnection(heliusApiKey: string): [Connection, Umi];
|
5
|
-
export declare function currentUnixSecondsSolana(umi: Umi): Promise<number>;
|
6
5
|
export declare function getWrappedInstruction(signer: Signer, ix: TransactionInstruction): WrappedInstruction;
|
7
6
|
export declare function setComputeUnitLimitUmiIx(signer: Signer, maxComputeUnits: number): WrappedInstruction;
|
8
7
|
export declare function setComputeUnitPriceUmiIx(signer: Signer, lamports: number): WrappedInstruction;
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"solanaUtils.d.ts","sourceRoot":"","sources":["../../src/utils/solanaUtils.ts"],"names":[],"mappings":"AACA,OAAO,EACL,uBAAuB,EACvB,MAAM,EACN,kBAAkB,EAClB,GAAG,EACH,kBAAkB,EAGnB,MAAM,0BAA0B,CAAC;AAOlC,OAAO,EAGL,UAAU,EACV,SAAS,EAIT,sBAAsB,EAEvB,MAAM,iBAAiB,CAAC;AAYzB,OAAO,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAElE,wBAAgB,sBAAsB,CACpC,YAAY,EAAE,MAAM,GACnB,CAAC,UAAU,EAAE,GAAG,CAAC,CAOnB;AAED,
|
1
|
+
{"version":3,"file":"solanaUtils.d.ts","sourceRoot":"","sources":["../../src/utils/solanaUtils.ts"],"names":[],"mappings":"AACA,OAAO,EACL,uBAAuB,EACvB,MAAM,EACN,kBAAkB,EAClB,GAAG,EACH,kBAAkB,EAGnB,MAAM,0BAA0B,CAAC;AAOl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,gBAAgB,CAAC,EAAE,MAAM,sBA4D1B;AAmBD,wBAAsB,2BAA2B,CAC/C,GAAG,EAAE,GAAG,EACR,EAAE,EAAE,kBAAkB,EACtB,eAAe,EAAE,kBAAkB,GAClC,OAAO,CAAC,MAAM,CAAC,CAgBjB;AAED,wBAAsB,8BAA8B,CAClD,GAAG,EAAE,GAAG,EACR,UAAU,EAAE,UAAU,EACtB,EAAE,EAAE,kBAAkB,EACtB,MAAM,CAAC,EAAE,kBAAkB,EAC3B,UAAU,CAAC,EAAE,MAAM,EACnB,eAAe,GAAE,kBAA+C,EAChE,cAAc,CAAC,EAAE,MAAM,IAAI,GAC1B,OAAO,CAAC,UAAU,GAAG,SAAS,CAAC,CA8DjC"}
|
@@ -4,7 +4,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
4
4
|
};
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
6
6
|
exports.getSolanaRpcConnection = getSolanaRpcConnection;
|
7
|
-
exports.currentUnixSecondsSolana = currentUnixSecondsSolana;
|
8
7
|
exports.getWrappedInstruction = getWrappedInstruction;
|
9
8
|
exports.setComputeUnitLimitUmiIx = setComputeUnitLimitUmiIx;
|
10
9
|
exports.setComputeUnitPriceUmiIx = setComputeUnitPriceUmiIx;
|
@@ -31,15 +30,6 @@ function getSolanaRpcConnection(heliusApiKey) {
|
|
31
30
|
const umi = (0, umi_bundle_defaults_1.createUmi)(connection);
|
32
31
|
return [connection, umi];
|
33
32
|
}
|
34
|
-
async function currentUnixSecondsSolana(umi) {
|
35
|
-
return await (0, generalUtils_1.retryWithExponentialBackoff)(async () => {
|
36
|
-
const blockTime = await umi.rpc.getBlockTime(await umi.rpc.getSlot(), { commitment: "confirmed" });
|
37
|
-
if (blockTime === null) {
|
38
|
-
throw new Error("Unable to retrieve block time");
|
39
|
-
}
|
40
|
-
return Number(blockTime);
|
41
|
-
});
|
42
|
-
}
|
43
33
|
function getWrappedInstruction(signer, ix) {
|
44
34
|
return {
|
45
35
|
instruction: (0, umi_web3js_adapters_1.fromWeb3JsInstruction)(ix),
|
@@ -15,6 +15,6 @@ export interface FlashLoanDetails {
|
|
15
15
|
baseUnitAmount: bigint;
|
16
16
|
mint: PublicKey;
|
17
17
|
}
|
18
|
-
export declare function getFlashLoanDetails(client: SolautoClient, values: RebalanceValues, jupQuote: QuoteResponse): FlashLoanDetails | undefined;
|
18
|
+
export declare function getFlashLoanDetails(client: SolautoClient, values: RebalanceValues, jupQuote: QuoteResponse, priceImpactBps: number): FlashLoanDetails | undefined;
|
19
19
|
export declare function getJupSwapRebalanceDetails(client: SolautoClient, values: RebalanceValues, targetLiqUtilizationRateBps?: number, attemptNum?: number): JupSwapDetails;
|
20
20
|
//# sourceMappingURL=rebalanceUtils.d.ts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"rebalanceUtils.d.ts","sourceRoot":"","sources":["../../../src/utils/solauto/rebalanceUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAC5D,OAAO,EACL,WAAW,EACX,aAAa,EAEb,yBAAyB,EACzB,SAAS,EACV,MAAM,iBAAiB,CAAC;AAOzB,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAgJjD,MAAM,WAAW,eAAe;IAC9B,kBAAkB,EAAE,OAAO,CAAC;IAC5B,iBAAiB,EAAE,MAAM,CAAC;IAC1B,aAAa,EAAE,MAAM,CAAC;IACtB,gBAAgB,EAAE,MAAM,CAAC;IACzB,YAAY,CAAC,EAAE,SAAS,CAAC;CAC1B;AAED,wBAAgB,kBAAkB,CAChC,KAAK,EAAE,aAAa,EACpB,QAAQ,EAAE,yBAAyB,GAAG,SAAS,EAC/C,GAAG,EAAE,WAAW,GAAG,SAAS,EAC5B,eAAe,EAAE,MAAM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,MAAM,CAAC;IACvB,IAAI,EAAE,SAAS,CAAC;CACjB;AAED,wBAAgB,mBAAmB,CACjC,MAAM,EAAE,aAAa,EACrB,MAAM,EAAE,eAAe,EACvB,QAAQ,EAAE,aAAa,EACvB,cAAc,EAAE,MAAM,GACrB,gBAAgB,GAAG,SAAS,CAmE9B;AAED,wBAAgB,0BAA0B,CACxC,MAAM,EAAE,aAAa,EACrB,MAAM,EAAE,eAAe,EACvB,2BAA2B,CAAC,EAAE,MAAM,EACpC,UAAU,CAAC,EAAE,MAAM,GAClB,cAAc,CAkChB"}
|
@@ -114,11 +114,11 @@ function getRebalanceValues(state, settings, dca, currentUnixTime, supplyPrice,
|
|
114
114
|
dcaTokenType: dca?.tokenType
|
115
115
|
};
|
116
116
|
}
|
117
|
-
function getFlashLoanDetails(client, values, jupQuote) {
|
117
|
+
function getFlashLoanDetails(client, values, jupQuote, priceImpactBps) {
|
118
118
|
let supplyUsd = (0, numberUtils_1.fromBaseUnit)(client.solautoPositionState.supply.amountUsed.baseAmountUsdValue, generalAccounts_1.USD_DECIMALS) + (values.dcaTokenType === generated_1.TokenType.Supply ? values.amountUsdToDcaIn : 0);
|
119
119
|
let debtUsd = (0, numberUtils_1.fromBaseUnit)(client.solautoPositionState.debt.amountUsed.baseAmountUsdValue, generalAccounts_1.USD_DECIMALS);
|
120
120
|
const debtAdjustmentWithSlippage = Math.abs(values.debtAdjustmentUsd) +
|
121
|
-
Math.abs(values.debtAdjustmentUsd) * (0, numberUtils_1.fromBps)(
|
121
|
+
Math.abs(values.debtAdjustmentUsd) * (0, numberUtils_1.fromBps)(priceImpactBps);
|
122
122
|
supplyUsd =
|
123
123
|
values.debtAdjustmentUsd < 0
|
124
124
|
? supplyUsd - debtAdjustmentWithSlippage
|
@@ -148,7 +148,7 @@ function getFlashLoanDetails(client, values, jupQuote) {
|
|
148
148
|
? {
|
149
149
|
baseUnitAmount: exactAmountBaseUnit
|
150
150
|
? exactAmountBaseUnit +
|
151
|
-
BigInt(Math.round(Number(exactAmountBaseUnit) * (0, numberUtils_1.fromBps)(
|
151
|
+
BigInt(Math.round(Number(exactAmountBaseUnit) * (0, numberUtils_1.fromBps)(priceImpactBps)))
|
152
152
|
: (0, numberUtils_1.toBaseUnit)(debtAdjustmentWithSlippage / flashLoanTokenPrice, flashLoanToken.decimals),
|
153
153
|
mint: (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(flashLoanToken.mint),
|
154
154
|
}
|
@@ -171,7 +171,7 @@ function getJupSwapRebalanceDetails(client, values, targetLiqUtilizationRateBps,
|
|
171
171
|
inputMint: (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(input.mint),
|
172
172
|
outputMint: (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(output.mint),
|
173
173
|
destinationWallet: client.solautoPosition,
|
174
|
-
|
174
|
+
slippageIncFactor: 0.5 + (attemptNum ?? 0) * 0.2,
|
175
175
|
amount: rebalancingToZero
|
176
176
|
? client.solautoPositionState.debt.amountUsed.baseUnit +
|
177
177
|
BigInt(Math.round(Number(client.solautoPositionState.debt.amountUsed.baseUnit) *
|
package/package.json
CHANGED
@@ -611,12 +611,13 @@ export async function buildSolautoRebalanceTransaction(
|
|
611
611
|
);
|
612
612
|
const {
|
613
613
|
jupQuote,
|
614
|
+
priceImpactBps,
|
614
615
|
lookupTableAddresses,
|
615
616
|
setupInstructions,
|
616
617
|
tokenLedgerIx,
|
617
618
|
swapIx,
|
618
619
|
} = await getJupSwapTransaction(client.signer, swapDetails, attemptNum);
|
619
|
-
const flashLoan = getFlashLoanDetails(client, values, jupQuote);
|
620
|
+
const flashLoan = getFlashLoanDetails(client, values, jupQuote, priceImpactBps);
|
620
621
|
|
621
622
|
let tx = transactionBuilder();
|
622
623
|
|
@@ -643,7 +644,7 @@ export async function buildSolautoRebalanceTransaction(
|
|
643
644
|
"A",
|
644
645
|
swapDetails,
|
645
646
|
rebalanceType,
|
646
|
-
|
647
|
+
priceImpactBps,
|
647
648
|
flashLoan,
|
648
649
|
targetLiqUtilizationRateBps
|
649
650
|
),
|
@@ -654,7 +655,7 @@ export async function buildSolautoRebalanceTransaction(
|
|
654
655
|
"B",
|
655
656
|
swapDetails,
|
656
657
|
rebalanceType,
|
657
|
-
|
658
|
+
priceImpactBps,
|
658
659
|
flashLoan,
|
659
660
|
targetLiqUtilizationRateBps
|
660
661
|
),
|
@@ -669,7 +670,7 @@ export async function buildSolautoRebalanceTransaction(
|
|
669
670
|
"A",
|
670
671
|
swapDetails,
|
671
672
|
rebalanceType,
|
672
|
-
|
673
|
+
priceImpactBps,
|
673
674
|
undefined,
|
674
675
|
targetLiqUtilizationRateBps
|
675
676
|
),
|
@@ -678,7 +679,7 @@ export async function buildSolautoRebalanceTransaction(
|
|
678
679
|
"B",
|
679
680
|
swapDetails,
|
680
681
|
rebalanceType,
|
681
|
-
|
682
|
+
priceImpactBps,
|
682
683
|
undefined,
|
683
684
|
targetLiqUtilizationRateBps
|
684
685
|
),
|
@@ -719,7 +720,7 @@ export async function convertReferralFeesToDestination(
|
|
719
720
|
inputMint: tokenAccountData.mint,
|
720
721
|
outputMint: toWeb3JsPublicKey(referralState.destFeesMint),
|
721
722
|
exactIn: true,
|
722
|
-
|
723
|
+
slippageIncFactor: 0.15,
|
723
724
|
});
|
724
725
|
|
725
726
|
let tx = transactionBuilder()
|
@@ -21,7 +21,7 @@ export interface JupSwapDetails {
|
|
21
21
|
outputMint: PublicKey;
|
22
22
|
destinationWallet: PublicKey;
|
23
23
|
amount: bigint;
|
24
|
-
|
24
|
+
slippageIncFactor?: number;
|
25
25
|
exactOut?: boolean;
|
26
26
|
exactIn?: boolean;
|
27
27
|
}
|
@@ -42,6 +42,7 @@ function createTransactionInstruction(
|
|
42
42
|
|
43
43
|
export interface JupSwapTransaction {
|
44
44
|
jupQuote: QuoteResponse;
|
45
|
+
priceImpactBps: number;
|
45
46
|
lookupTableAddresses: string[];
|
46
47
|
setupInstructions: TransactionBuilder;
|
47
48
|
tokenLedgerIx: TransactionBuilder;
|
@@ -65,19 +66,20 @@ export async function getJupSwapTransaction(
|
|
65
66
|
: swapDetails.exactIn
|
66
67
|
? "ExactIn"
|
67
68
|
: undefined,
|
68
|
-
slippageBps:
|
69
|
+
slippageBps: 30,
|
69
70
|
maxAccounts: !swapDetails.exactOut ? 60 : undefined,
|
70
71
|
}),
|
71
72
|
3
|
72
73
|
);
|
73
74
|
|
75
|
+
const priceImpactBps = (Math.round(toBps(parseFloat(quoteResponse.priceImpactPct))) + 1);
|
74
76
|
const finalPriceSlippageBps = Math.round(
|
75
77
|
Math.max(
|
76
78
|
50,
|
77
79
|
quoteResponse.slippageBps,
|
78
|
-
|
80
|
+
priceImpactBps
|
79
81
|
) *
|
80
|
-
(1 + (swapDetails.
|
82
|
+
(1 + (swapDetails.slippageIncFactor ?? 0))
|
81
83
|
);
|
82
84
|
quoteResponse.slippageBps = finalPriceSlippageBps;
|
83
85
|
console.log(quoteResponse);
|
@@ -100,8 +102,13 @@ export async function getJupSwapTransaction(
|
|
100
102
|
throw new Error("No swap instruction was returned by Jupiter");
|
101
103
|
}
|
102
104
|
|
105
|
+
console.log("Raw price impact bps:", priceImpactBps);
|
106
|
+
const finalPriceImpactBps = priceImpactBps * (1 + (swapDetails.slippageIncFactor ?? 0));
|
107
|
+
console.log("Increased price impact bps:", finalPriceImpactBps);
|
108
|
+
|
103
109
|
return {
|
104
110
|
jupQuote: quoteResponse,
|
111
|
+
priceImpactBps: finalPriceImpactBps,
|
105
112
|
lookupTableAddresses: instructions.addressLookupTableAddresses,
|
106
113
|
setupInstructions: transactionBuilder().add(
|
107
114
|
instructions.setupInstructions.map((ix) =>
|
@@ -21,7 +21,6 @@ import { MarginfiAssetAccounts } from "../types/accounts";
|
|
21
21
|
import { PositionState, PositionTokenUsage } from "../generated";
|
22
22
|
import { USD_DECIMALS } from "../constants/generalAccounts";
|
23
23
|
import { LivePositionUpdates } from "./solauto/generalUtils";
|
24
|
-
import { currentUnixSecondsSolana } from "./solanaUtils";
|
25
24
|
|
26
25
|
export function findMarginfiAccounts(bank: PublicKey): MarginfiAssetAccounts {
|
27
26
|
for (const key in MARGINFI_ACCOUNTS) {
|
@@ -447,8 +446,7 @@ export async function getUpToDateShareValues(
|
|
447
446
|
umi: Umi,
|
448
447
|
bank: Bank
|
449
448
|
): Promise<[number, number]> {
|
450
|
-
|
451
|
-
let timeDelta = currentTime - Number(bank.lastUpdate);
|
449
|
+
let timeDelta = currentUnixSeconds() - Number(bank.lastUpdate);
|
452
450
|
|
453
451
|
const totalAssets =
|
454
452
|
bytesToI80F48(bank.totalAssetShares.value) *
|
package/src/utils/solanaUtils.ts
CHANGED
@@ -49,16 +49,6 @@ export function getSolanaRpcConnection(
|
|
49
49
|
return [connection, umi];
|
50
50
|
}
|
51
51
|
|
52
|
-
export async function currentUnixSecondsSolana(umi: Umi): Promise<number> {
|
53
|
-
return await retryWithExponentialBackoff(async () => {
|
54
|
-
const blockTime = await umi.rpc.getBlockTime(await umi.rpc.getSlot(), { commitment: "confirmed" });
|
55
|
-
if (blockTime === null) {
|
56
|
-
throw new Error("Unable to retrieve block time");
|
57
|
-
}
|
58
|
-
return Number(blockTime);
|
59
|
-
});
|
60
|
-
}
|
61
|
-
|
62
52
|
export function getWrappedInstruction(
|
63
53
|
signer: Signer,
|
64
54
|
ix: TransactionInstruction
|
@@ -250,7 +250,8 @@ export interface FlashLoanDetails {
|
|
250
250
|
export function getFlashLoanDetails(
|
251
251
|
client: SolautoClient,
|
252
252
|
values: RebalanceValues,
|
253
|
-
jupQuote: QuoteResponse
|
253
|
+
jupQuote: QuoteResponse,
|
254
|
+
priceImpactBps: number
|
254
255
|
): FlashLoanDetails | undefined {
|
255
256
|
let supplyUsd = fromBaseUnit(
|
256
257
|
client.solautoPositionState!.supply.amountUsed.baseAmountUsdValue,
|
@@ -263,7 +264,7 @@ export function getFlashLoanDetails(
|
|
263
264
|
|
264
265
|
const debtAdjustmentWithSlippage =
|
265
266
|
Math.abs(values.debtAdjustmentUsd) +
|
266
|
-
Math.abs(values.debtAdjustmentUsd) * fromBps(
|
267
|
+
Math.abs(values.debtAdjustmentUsd) * fromBps(priceImpactBps);
|
267
268
|
supplyUsd =
|
268
269
|
values.debtAdjustmentUsd < 0
|
269
270
|
? supplyUsd - debtAdjustmentWithSlippage
|
@@ -308,7 +309,7 @@ export function getFlashLoanDetails(
|
|
308
309
|
? exactAmountBaseUnit +
|
309
310
|
BigInt(
|
310
311
|
Math.round(
|
311
|
-
Number(exactAmountBaseUnit) * fromBps(
|
312
|
+
Number(exactAmountBaseUnit) * fromBps(priceImpactBps)
|
312
313
|
)
|
313
314
|
)
|
314
315
|
: toBaseUnit(
|
@@ -346,7 +347,7 @@ export function getJupSwapRebalanceDetails(
|
|
346
347
|
inputMint: toWeb3JsPublicKey(input.mint),
|
347
348
|
outputMint: toWeb3JsPublicKey(output.mint),
|
348
349
|
destinationWallet: client.solautoPosition,
|
349
|
-
|
350
|
+
slippageIncFactor: 0.5 + (attemptNum ?? 0) * 0.2,
|
350
351
|
amount: rebalancingToZero
|
351
352
|
? client.solautoPositionState!.debt.amountUsed.baseUnit +
|
352
353
|
BigInt(
|
@@ -23,7 +23,7 @@ describe("Solauto Marginfi tests", async () => {
|
|
23
23
|
// const signer = setupTest();
|
24
24
|
const signer = setupTest("solauto-manager");
|
25
25
|
|
26
|
-
const payForTransactions =
|
26
|
+
const payForTransactions = false;
|
27
27
|
const useJitoBundle = false;
|
28
28
|
const positionId = 1;
|
29
29
|
|
@@ -39,7 +39,7 @@ describe("Solauto Marginfi tests", async () => {
|
|
39
39
|
{
|
40
40
|
signer,
|
41
41
|
positionId,
|
42
|
-
authority: new PublicKey("
|
42
|
+
// authority: new PublicKey("")
|
43
43
|
// marginfiAccount: new PublicKey(
|
44
44
|
// "4nNvUXF5YqHFcH2nGweSiuvy1ct7V5FXfoCLKFYUN36z"
|
45
45
|
// ),
|