@haven-fi/solauto-sdk 1.0.582 → 1.0.584
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/constants/solautoConstants.d.ts +1 -0
- package/dist/constants/solautoConstants.d.ts.map +1 -1
- package/dist/constants/solautoConstants.js +4 -10
- package/dist/generated/accounts/solautoPosition.js +2 -2
- package/dist/generated/errors/solauto.d.ts +14 -2
- package/dist/generated/errors/solauto.d.ts.map +1 -1
- package/dist/generated/errors/solauto.js +27 -5
- package/dist/generated/instructions/claimReferralFees.d.ts +1 -1
- package/dist/generated/instructions/claimReferralFees.d.ts.map +1 -1
- package/dist/generated/instructions/closePosition.d.ts +1 -1
- package/dist/generated/instructions/closePosition.d.ts.map +1 -1
- package/dist/generated/instructions/closePosition.js +2 -2
- package/dist/generated/instructions/marginfiOpenPosition.d.ts +1 -3
- package/dist/generated/instructions/marginfiOpenPosition.d.ts.map +1 -1
- package/dist/generated/instructions/marginfiOpenPosition.js +0 -1
- package/dist/generated/instructions/marginfiRebalance.d.ts +7 -3
- package/dist/generated/instructions/marginfiRebalance.d.ts.map +1 -1
- package/dist/generated/instructions/marginfiRebalance.js +3 -1
- package/dist/generated/types/index.d.ts +7 -1
- package/dist/generated/types/index.d.ts.map +1 -1
- package/dist/generated/types/index.js +7 -1
- package/dist/generated/types/positionData.d.ts +2 -2
- package/dist/generated/types/positionData.d.ts.map +1 -1
- package/dist/generated/types/positionData.js +1 -1
- package/dist/generated/types/positionState.d.ts +7 -7
- package/dist/generated/types/positionState.d.ts.map +1 -1
- package/dist/generated/types/positionState.js +3 -3
- package/dist/generated/types/{positionTokenUsage.d.ts → positionTokenState.d.ts} +6 -8
- package/dist/generated/types/positionTokenState.d.ts.map +1 -0
- package/dist/generated/types/{positionTokenUsage.js → positionTokenState.js} +6 -7
- package/dist/generated/types/rebalanceData.d.ts +7 -13
- package/dist/generated/types/rebalanceData.d.ts.map +1 -1
- package/dist/generated/types/rebalanceData.js +3 -6
- package/dist/generated/types/rebalanceDirection.d.ts +3 -2
- package/dist/generated/types/rebalanceDirection.d.ts.map +1 -1
- package/dist/generated/types/rebalanceDirection.js +3 -2
- package/dist/generated/types/rebalanceInstructionData.d.ts +27 -0
- package/dist/generated/types/rebalanceInstructionData.d.ts.map +1 -0
- package/dist/generated/types/rebalanceInstructionData.js +22 -0
- package/dist/generated/types/rebalanceStateValues.d.ts +27 -0
- package/dist/generated/types/rebalanceStateValues.d.ts.map +1 -0
- package/dist/generated/types/rebalanceStateValues.js +22 -0
- package/dist/generated/types/rebalanceStep.d.ts +15 -0
- package/dist/generated/types/rebalanceStep.d.ts.map +1 -0
- package/dist/generated/types/rebalanceStep.js +22 -0
- package/dist/generated/types/solautoRebalanceType.d.ts +4 -5
- package/dist/generated/types/solautoRebalanceType.d.ts.map +1 -1
- package/dist/generated/types/solautoRebalanceType.js +4 -5
- package/dist/generated/types/solautoSettingsParameters.d.ts +2 -15
- package/dist/generated/types/solautoSettingsParameters.d.ts.map +1 -1
- package/dist/generated/types/solautoSettingsParameters.js +1 -5
- package/dist/generated/types/solautoSettingsParametersInp.d.ts +1 -12
- package/dist/generated/types/solautoSettingsParametersInp.d.ts.map +1 -1
- package/dist/generated/types/solautoSettingsParametersInp.js +0 -3
- package/dist/generated/types/swapType.d.ts +15 -0
- package/dist/generated/types/swapType.d.ts.map +1 -0
- package/dist/generated/types/swapType.js +22 -0
- package/dist/generated/types/tokenBalanceChange.d.ts +21 -0
- package/dist/generated/types/tokenBalanceChange.d.ts.map +1 -0
- package/dist/generated/types/tokenBalanceChange.js +19 -0
- package/dist/generated/types/tokenBalanceChangeType.d.ts +18 -0
- package/dist/generated/types/tokenBalanceChangeType.d.ts.map +1 -0
- package/dist/generated/types/tokenBalanceChangeType.js +25 -0
- package/dist/generated/types/updatePositionData.d.ts +2 -2
- package/dist/generated/types/updatePositionData.d.ts.map +1 -1
- package/dist/generated/types/updatePositionData.js +1 -1
- package/dist/index.d.ts +8 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -5
- package/dist/marginfi-sdk/types/interestRateConfig.d.ts +3 -1
- package/dist/marginfi-sdk/types/interestRateConfig.d.ts.map +1 -1
- package/dist/marginfi-sdk/types/interestRateConfig.js +2 -1
- package/dist/services/flashLoans/flProviderAggregator.d.ts +19 -0
- package/dist/services/flashLoans/flProviderAggregator.d.ts.map +1 -0
- package/dist/services/flashLoans/flProviderAggregator.js +46 -0
- package/dist/services/flashLoans/flProviderBase.d.ts +23 -0
- package/dist/services/flashLoans/flProviderBase.d.ts.map +1 -0
- package/dist/services/flashLoans/flProviderBase.js +37 -0
- package/dist/services/flashLoans/index.d.ts +4 -0
- package/dist/services/flashLoans/index.d.ts.map +1 -0
- package/dist/services/flashLoans/index.js +19 -0
- package/dist/services/flashLoans/marginfiFlProvider.d.ts +25 -0
- package/dist/services/flashLoans/marginfiFlProvider.d.ts.map +1 -0
- package/dist/services/flashLoans/marginfiFlProvider.js +246 -0
- package/dist/services/index.d.ts +6 -0
- package/dist/services/index.d.ts.map +1 -0
- package/dist/services/index.js +21 -0
- package/dist/services/rebalance/index.d.ts +3 -0
- package/dist/services/rebalance/index.d.ts.map +1 -0
- package/dist/services/rebalance/index.js +18 -0
- package/dist/services/rebalance/rebalanceSwapManager.d.ts +30 -0
- package/dist/services/rebalance/rebalanceSwapManager.d.ts.map +1 -0
- package/dist/services/rebalance/rebalanceSwapManager.js +144 -0
- package/dist/services/rebalance/rebalanceTxBuilder.d.ts +22 -0
- package/dist/services/rebalance/rebalanceTxBuilder.d.ts.map +1 -0
- package/dist/services/rebalance/rebalanceTxBuilder.js +200 -0
- package/dist/services/rebalance/rebalanceValues.d.ts +31 -0
- package/dist/services/rebalance/rebalanceValues.d.ts.map +1 -0
- package/dist/services/rebalance/rebalanceValues.js +118 -0
- package/dist/services/rebalance/solautoFees.d.ts +13 -0
- package/dist/services/rebalance/solautoFees.d.ts.map +1 -0
- package/dist/services/rebalance/solautoFees.js +54 -0
- package/dist/services/solauto/index.d.ts +5 -0
- package/dist/services/solauto/index.d.ts.map +1 -0
- package/dist/{clients → services/solauto}/referralStateManager.d.ts +4 -5
- package/dist/services/solauto/referralStateManager.d.ts.map +1 -0
- package/dist/{clients → services/solauto}/referralStateManager.js +10 -12
- package/dist/services/solauto/solautoClient.d.ts +59 -0
- package/dist/services/solauto/solautoClient.d.ts.map +1 -0
- package/dist/{clients → services/solauto}/solautoClient.js +78 -109
- package/dist/services/solauto/solautoMarginfiClient.d.ts +34 -0
- package/dist/services/solauto/solautoMarginfiClient.d.ts.map +1 -0
- package/dist/services/solauto/solautoMarginfiClient.js +322 -0
- package/dist/{clients → services/solauto}/txHandler.d.ts +11 -2
- package/dist/services/solauto/txHandler.d.ts.map +1 -0
- package/dist/services/solauto/txHandler.js +38 -0
- package/dist/services/swap/index.d.ts +2 -0
- package/dist/services/swap/index.d.ts.map +1 -0
- package/dist/{utils/solauto → services/swap}/index.js +1 -2
- package/dist/services/swap/jupSwapManager.d.ts +37 -0
- package/dist/services/swap/jupSwapManager.d.ts.map +1 -0
- package/dist/services/swap/jupSwapManager.js +108 -0
- package/dist/services/transactions/index.d.ts.map +1 -0
- package/dist/{transactions → services/transactions}/transactionUtils.d.ts +3 -5
- package/dist/services/transactions/transactionUtils.d.ts.map +1 -0
- package/dist/{transactions → services/transactions}/transactionUtils.js +29 -137
- package/dist/{transactions → services/transactions}/transactionsManager.d.ts +4 -4
- package/dist/services/transactions/transactionsManager.d.ts.map +1 -0
- package/dist/{transactions → services/transactions}/transactionsManager.js +10 -8
- package/dist/solautoPosition/index.d.ts +4 -0
- package/dist/solautoPosition/index.d.ts.map +1 -0
- package/dist/solautoPosition/index.js +19 -0
- package/dist/solautoPosition/marginfiSolautoPositionEx.d.ts +14 -0
- package/dist/solautoPosition/marginfiSolautoPositionEx.d.ts.map +1 -0
- package/dist/solautoPosition/marginfiSolautoPositionEx.js +73 -0
- package/dist/solautoPosition/solautoPositionEx.d.ts +67 -0
- package/dist/solautoPosition/solautoPositionEx.d.ts.map +1 -0
- package/dist/solautoPosition/solautoPositionEx.js +143 -0
- package/dist/solautoPosition/utils.d.ts +17 -0
- package/dist/solautoPosition/utils.d.ts.map +1 -0
- package/dist/solautoPosition/utils.js +115 -0
- package/dist/types/solauto.d.ts +19 -1
- package/dist/types/solauto.d.ts.map +1 -1
- package/dist/utils/generalUtils.d.ts +1 -0
- package/dist/utils/generalUtils.d.ts.map +1 -1
- package/dist/utils/generalUtils.js +9 -1
- package/dist/utils/index.d.ts +1 -1
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +1 -1
- package/dist/utils/jitoUtils.d.ts.map +1 -1
- package/dist/utils/jupiterUtils.d.ts +3 -28
- package/dist/utils/jupiterUtils.d.ts.map +1 -1
- package/dist/utils/jupiterUtils.js +2 -73
- package/dist/utils/marginfiUtils.d.ts +4 -3
- package/dist/utils/marginfiUtils.d.ts.map +1 -1
- package/dist/utils/marginfiUtils.js +47 -18
- package/dist/utils/numberUtils.d.ts +3 -6
- package/dist/utils/numberUtils.d.ts.map +1 -1
- package/dist/utils/numberUtils.js +13 -48
- package/dist/utils/solanaUtils.js +2 -2
- package/dist/utils/{solauto/generalUtils.d.ts → solautoUtils.d.ts} +9 -16
- package/dist/utils/solautoUtils.d.ts.map +1 -0
- package/dist/utils/{solauto/generalUtils.js → solautoUtils.js} +49 -173
- package/local/createTokenAccounts.ts +1 -5
- package/local/logPositions.ts +22 -68
- package/local/shared.ts +29 -24
- package/local/txSandbox.ts +66 -0
- package/local/updateMarginfiLUT.ts +80 -4
- package/package.json +3 -6
- package/src/constants/solautoConstants.ts +4 -9
- package/src/generated/accounts/solautoPosition.ts +2 -2
- package/src/generated/errors/solauto.ts +45 -4
- package/src/generated/instructions/claimReferralFees.ts +1 -1
- package/src/generated/instructions/closePosition.ts +3 -3
- package/src/generated/instructions/marginfiOpenPosition.ts +0 -7
- package/src/generated/instructions/marginfiRebalance.ts +12 -3
- package/src/generated/types/index.ts +7 -1
- package/src/generated/types/positionData.ts +3 -3
- package/src/generated/types/positionState.ts +12 -12
- package/src/generated/types/{positionTokenUsage.ts → positionTokenState.ts} +13 -16
- package/src/generated/types/rebalanceData.ts +16 -27
- package/src/generated/types/rebalanceDirection.ts +1 -0
- package/src/generated/types/rebalanceInstructionData.ts +62 -0
- package/src/generated/types/rebalanceStateValues.ts +59 -0
- package/src/generated/types/rebalanceStep.ts +25 -0
- package/src/generated/types/solautoRebalanceType.ts +0 -1
- package/src/generated/types/solautoSettingsParameters.ts +4 -25
- package/src/generated/types/solautoSettingsParametersInp.ts +2 -24
- package/src/generated/types/swapType.ts +22 -0
- package/src/generated/types/tokenBalanceChange.ts +46 -0
- package/src/generated/types/tokenBalanceChangeType.ts +28 -0
- package/src/generated/types/updatePositionData.ts +3 -3
- package/src/index.ts +8 -8
- package/src/marginfi-sdk/types/interestRateConfig.ts +6 -2
- package/src/services/flashLoans/flProviderAggregator.ts +74 -0
- package/src/services/flashLoans/flProviderBase.ts +88 -0
- package/src/services/flashLoans/index.ts +3 -0
- package/src/services/flashLoans/marginfiFlProvider.ts +392 -0
- package/src/services/index.ts +5 -0
- package/src/services/rebalance/index.ts +2 -0
- package/src/services/rebalance/rebalanceSwapManager.ts +218 -0
- package/src/services/rebalance/rebalanceTxBuilder.ts +330 -0
- package/src/services/rebalance/rebalanceValues.ts +232 -0
- package/src/services/rebalance/solautoFees.ts +61 -0
- package/src/services/solauto/index.ts +4 -0
- package/src/{clients → services/solauto}/referralStateManager.ts +17 -31
- package/src/{clients → services/solauto}/solautoClient.ts +142 -205
- package/src/services/solauto/solautoMarginfiClient.ts +472 -0
- package/src/services/solauto/txHandler.ts +69 -0
- package/src/services/swap/index.ts +1 -0
- package/src/services/swap/jupSwapManager.ts +189 -0
- package/src/{transactions → services/transactions}/transactionUtils.ts +40 -280
- package/src/{transactions → services/transactions}/transactionsManager.ts +15 -10
- package/src/solautoPosition/index.ts +3 -0
- package/src/solautoPosition/marginfiSolautoPositionEx.ts +111 -0
- package/src/solautoPosition/solautoPositionEx.ts +281 -0
- package/src/solautoPosition/utils.ts +188 -0
- package/src/types/solauto.ts +30 -2
- package/src/utils/generalUtils.ts +9 -1
- package/src/utils/index.ts +2 -2
- package/src/utils/jitoUtils.ts +0 -4
- package/src/utils/jupiterUtils.ts +3 -164
- package/src/utils/marginfiUtils.ts +77 -43
- package/src/utils/numberUtils.ts +18 -77
- package/src/utils/solanaUtils.ts +1 -1
- package/src/utils/{solauto/generalUtils.ts → solautoUtils.ts} +67 -288
- package/tests/transactions/shared.ts +135 -0
- package/tests/transactions/solautoMarginfi.ts +8 -198
- package/tests/unit/accounts.ts +3 -10
- package/tests/unit/lookupTables.ts +23 -2
- package/tests/unit/rebalanceCalculations.ts +65 -343
- package/dist/clients/index.d.ts +0 -5
- package/dist/clients/index.d.ts.map +0 -1
- package/dist/clients/referralStateManager.d.ts.map +0 -1
- package/dist/clients/solautoClient.d.ts +0 -71
- package/dist/clients/solautoClient.d.ts.map +0 -1
- package/dist/clients/solautoMarginfiClient.d.ts +0 -51
- package/dist/clients/solautoMarginfiClient.d.ts.map +0 -1
- package/dist/clients/solautoMarginfiClient.js +0 -497
- package/dist/clients/txHandler.d.ts.map +0 -1
- package/dist/clients/txHandler.js +0 -23
- package/dist/generated/types/positionTokenUsage.d.ts.map +0 -1
- package/dist/transactions/index.d.ts.map +0 -1
- package/dist/transactions/transactionUtils.d.ts.map +0 -1
- package/dist/transactions/transactionsManager.d.ts.map +0 -1
- package/dist/utils/solauto/generalUtils.d.ts.map +0 -1
- package/dist/utils/solauto/index.d.ts +0 -3
- package/dist/utils/solauto/index.d.ts.map +0 -1
- package/dist/utils/solauto/rebalanceUtils.d.ts +0 -30
- package/dist/utils/solauto/rebalanceUtils.d.ts.map +0 -1
- package/dist/utils/solauto/rebalanceUtils.js +0 -287
- package/src/clients/index.ts +0 -4
- package/src/clients/solautoMarginfiClient.ts +0 -774
- package/src/clients/txHandler.ts +0 -38
- package/src/utils/solauto/index.ts +0 -2
- package/src/utils/solauto/rebalanceUtils.ts +0 -562
- package/dist/{clients → services/solauto}/index.js +2 -2
- /package/dist/{transactions → services/transactions}/index.d.ts +0 -0
- /package/dist/{transactions → services/transactions}/index.js +0 -0
- /package/src/{transactions → services/transactions}/index.ts +0 -0
package/src/clients/txHandler.ts
DELETED
@@ -1,38 +0,0 @@
|
|
1
|
-
import { Signer, Umi } from "@metaplex-foundation/umi";
|
2
|
-
import { Connection, PublicKey } from "@solana/web3.js";
|
3
|
-
import { consoleLog, getSolanaRpcConnection } from "../utils";
|
4
|
-
import { SOLAUTO_PROD_PROGRAM } from "../constants";
|
5
|
-
|
6
|
-
export abstract class TxHandler {
|
7
|
-
public connection!: Connection;
|
8
|
-
public umi!: Umi;
|
9
|
-
public signer!: Signer;
|
10
|
-
public otherSigners: Signer[] = [];
|
11
|
-
|
12
|
-
constructor(
|
13
|
-
public rpcUrl: string,
|
14
|
-
localTest?: boolean,
|
15
|
-
public programId: PublicKey = SOLAUTO_PROD_PROGRAM,
|
16
|
-
public wsEndpoint?: string
|
17
|
-
) {
|
18
|
-
const [connection, umi] = getSolanaRpcConnection(
|
19
|
-
this.rpcUrl,
|
20
|
-
this.programId,
|
21
|
-
wsEndpoint
|
22
|
-
);
|
23
|
-
this.connection = connection;
|
24
|
-
this.umi = umi;
|
25
|
-
|
26
|
-
if (!(globalThis as any).LOCAL_TEST && localTest) {
|
27
|
-
(globalThis as any).LOCAL_TEST = Boolean(localTest);
|
28
|
-
}
|
29
|
-
}
|
30
|
-
|
31
|
-
log(...args: any[]): void {
|
32
|
-
consoleLog(...args);
|
33
|
-
}
|
34
|
-
|
35
|
-
abstract defaultLookupTables(): string[];
|
36
|
-
|
37
|
-
abstract resetLiveTxUpdates(success?: boolean): Promise<void>;
|
38
|
-
}
|
@@ -1,562 +0,0 @@
|
|
1
|
-
import { PublicKey } from "@solana/web3.js";
|
2
|
-
import { SolautoClient } from "../../clients/solautoClient";
|
3
|
-
import {
|
4
|
-
DCASettings,
|
5
|
-
PositionState,
|
6
|
-
PositionTokenUsage,
|
7
|
-
RebalanceDirection,
|
8
|
-
SolautoSettingsParameters,
|
9
|
-
TokenType,
|
10
|
-
} from "../../generated";
|
11
|
-
import {
|
12
|
-
eligibleForNextAutomationPeriod,
|
13
|
-
getAdjustedSettingsFromAutomation,
|
14
|
-
getUpdatedValueFromAutomation,
|
15
|
-
} from "./generalUtils";
|
16
|
-
import { toWeb3JsPublicKey } from "@metaplex-foundation/umi-web3js-adapters";
|
17
|
-
import { QuoteResponse } from "@jup-ag/api";
|
18
|
-
import { getJupQuote, JupSwapDetails, JupSwapInput } from "../jupiterUtils";
|
19
|
-
import { consoleLog, currentUnixSeconds, tokenInfo } from "../generalUtils";
|
20
|
-
import {
|
21
|
-
calcDebtUsd,
|
22
|
-
calcNetWorthUsd,
|
23
|
-
calcSupplyUsd,
|
24
|
-
fromBaseUnit,
|
25
|
-
fromBps,
|
26
|
-
getDebtAdjustmentUsd,
|
27
|
-
getLiqUtilzationRateBps,
|
28
|
-
getMaxLiqUtilizationRateBps,
|
29
|
-
getSolautoFeesBps,
|
30
|
-
maxBoostToBps,
|
31
|
-
maxRepayToBps,
|
32
|
-
toBaseUnit,
|
33
|
-
} from "../numberUtils";
|
34
|
-
import { USD_DECIMALS } from "../../constants/generalAccounts";
|
35
|
-
import { RebalanceAction } from "../../types";
|
36
|
-
import { safeGetPrice } from "../priceUtils";
|
37
|
-
import { BROKEN_TOKENS, JUP, USDC, USDT } from "../../constants";
|
38
|
-
import { Umi } from "@metaplex-foundation/umi";
|
39
|
-
|
40
|
-
function getAdditionalAmountToDcaIn(dca: DCASettings): number {
|
41
|
-
if (dca.dcaInBaseUnit === BigInt(0)) {
|
42
|
-
return 0;
|
43
|
-
}
|
44
|
-
|
45
|
-
const debtBalance = Number(dca.dcaInBaseUnit);
|
46
|
-
const updatedDebtBalance = getUpdatedValueFromAutomation(
|
47
|
-
debtBalance,
|
48
|
-
0,
|
49
|
-
dca.automation,
|
50
|
-
currentUnixSeconds()
|
51
|
-
);
|
52
|
-
|
53
|
-
return debtBalance - updatedDebtBalance;
|
54
|
-
}
|
55
|
-
|
56
|
-
function getStandardTargetLiqUtilizationRateBps(
|
57
|
-
state: PositionState,
|
58
|
-
settings: SolautoSettingsParameters
|
59
|
-
): number {
|
60
|
-
const adjustedSettings = getAdjustedSettingsFromAutomation(
|
61
|
-
settings,
|
62
|
-
currentUnixSeconds()
|
63
|
-
);
|
64
|
-
|
65
|
-
if (state.liqUtilizationRateBps < adjustedSettings.boostToBps) {
|
66
|
-
return adjustedSettings.boostToBps;
|
67
|
-
} else if (state.liqUtilizationRateBps > settings.repayToBps) {
|
68
|
-
return adjustedSettings.repayToBps;
|
69
|
-
} else {
|
70
|
-
throw new Error("Invalid rebalance condition");
|
71
|
-
}
|
72
|
-
}
|
73
|
-
|
74
|
-
function targetLiqUtilizationRateBpsFromDCA(
|
75
|
-
state: PositionState,
|
76
|
-
settings: SolautoSettingsParameters,
|
77
|
-
dca: DCASettings,
|
78
|
-
currentUnixTime: number
|
79
|
-
) {
|
80
|
-
const adjustedSettings = getAdjustedSettingsFromAutomation(
|
81
|
-
settings,
|
82
|
-
currentUnixTime
|
83
|
-
);
|
84
|
-
|
85
|
-
let targetRateBps = 0;
|
86
|
-
if (dca.dcaInBaseUnit > BigInt(0)) {
|
87
|
-
targetRateBps = Math.max(
|
88
|
-
state.liqUtilizationRateBps,
|
89
|
-
adjustedSettings.boostToBps
|
90
|
-
);
|
91
|
-
} else {
|
92
|
-
targetRateBps = adjustedSettings.boostToBps;
|
93
|
-
}
|
94
|
-
return targetRateBps;
|
95
|
-
}
|
96
|
-
|
97
|
-
function isDcaRebalance(
|
98
|
-
state: PositionState,
|
99
|
-
settings: SolautoSettingsParameters,
|
100
|
-
dca: DCASettings | undefined,
|
101
|
-
currentUnixTime: number
|
102
|
-
): boolean {
|
103
|
-
if (dca === undefined || dca.automation.targetPeriods === 0) {
|
104
|
-
return false;
|
105
|
-
}
|
106
|
-
|
107
|
-
const adjustedSettings = getAdjustedSettingsFromAutomation(
|
108
|
-
settings,
|
109
|
-
currentUnixTime
|
110
|
-
);
|
111
|
-
|
112
|
-
if (
|
113
|
-
state.liqUtilizationRateBps >
|
114
|
-
adjustedSettings.repayToBps + adjustedSettings.repayGap
|
115
|
-
) {
|
116
|
-
return false;
|
117
|
-
}
|
118
|
-
|
119
|
-
if (!eligibleForNextAutomationPeriod(dca.automation, currentUnixTime)) {
|
120
|
-
return false;
|
121
|
-
}
|
122
|
-
|
123
|
-
return true;
|
124
|
-
}
|
125
|
-
|
126
|
-
function getTargetRateAndDcaAmount(
|
127
|
-
state: PositionState,
|
128
|
-
settings: SolautoSettingsParameters | undefined,
|
129
|
-
dca: DCASettings | undefined,
|
130
|
-
currentUnixTime: number,
|
131
|
-
targetLiqUtilizationRateBps?: number
|
132
|
-
): { targetRateBps: number; amountToDcaIn?: number } {
|
133
|
-
if (targetLiqUtilizationRateBps !== undefined) {
|
134
|
-
return {
|
135
|
-
targetRateBps: targetLiqUtilizationRateBps,
|
136
|
-
};
|
137
|
-
}
|
138
|
-
|
139
|
-
if (settings === undefined) {
|
140
|
-
throw new Error(
|
141
|
-
"If rebalancing a self-managed position, settings and DCA should be provided"
|
142
|
-
);
|
143
|
-
}
|
144
|
-
|
145
|
-
if (isDcaRebalance(state, settings, dca, currentUnixTime)) {
|
146
|
-
const targetLiqUtilizationRateBps = targetLiqUtilizationRateBpsFromDCA(
|
147
|
-
state,
|
148
|
-
settings,
|
149
|
-
dca!,
|
150
|
-
currentUnixTime
|
151
|
-
);
|
152
|
-
const amountToDcaIn = getAdditionalAmountToDcaIn(dca!);
|
153
|
-
|
154
|
-
return {
|
155
|
-
targetRateBps: targetLiqUtilizationRateBps,
|
156
|
-
amountToDcaIn,
|
157
|
-
};
|
158
|
-
} else {
|
159
|
-
return {
|
160
|
-
targetRateBps: getStandardTargetLiqUtilizationRateBps(state, settings),
|
161
|
-
};
|
162
|
-
}
|
163
|
-
}
|
164
|
-
|
165
|
-
export interface RebalanceValues {
|
166
|
-
debtAdjustmentUsd: number;
|
167
|
-
repayingCloseToMaxLtv: boolean;
|
168
|
-
amountToDcaIn: number;
|
169
|
-
amountUsdToDcaIn: number;
|
170
|
-
dcaTokenType?: TokenType;
|
171
|
-
rebalanceAction: RebalanceAction;
|
172
|
-
rebalanceDirection: RebalanceDirection;
|
173
|
-
feesUsd: number;
|
174
|
-
targetRateBps: number;
|
175
|
-
}
|
176
|
-
|
177
|
-
export function getRebalanceValues(
|
178
|
-
state: PositionState,
|
179
|
-
settings: SolautoSettingsParameters | undefined,
|
180
|
-
dca: DCASettings | undefined,
|
181
|
-
currentUnixTime: number,
|
182
|
-
supplyPrice: number,
|
183
|
-
debtPrice: number,
|
184
|
-
targetLiqUtilizationRateBps?: number
|
185
|
-
): RebalanceValues {
|
186
|
-
let { targetRateBps, amountToDcaIn } = getTargetRateAndDcaAmount(
|
187
|
-
state,
|
188
|
-
settings,
|
189
|
-
dca,
|
190
|
-
currentUnixTime,
|
191
|
-
targetLiqUtilizationRateBps
|
192
|
-
);
|
193
|
-
|
194
|
-
// TODO: REVERT ME AND GET TO THE ROOT OF THIS ISSUE
|
195
|
-
const supplyMint = toWeb3JsPublicKey(state.supply.mint);
|
196
|
-
if (
|
197
|
-
BROKEN_TOKENS.includes(supplyMint.toString()) &&
|
198
|
-
(toWeb3JsPublicKey(state.debt.mint).equals(new PublicKey(USDC)) ||
|
199
|
-
toWeb3JsPublicKey(state.debt.mint).equals(new PublicKey(USDT))) &&
|
200
|
-
settings &&
|
201
|
-
settings.boostToBps ===
|
202
|
-
maxBoostToBps(state.maxLtvBps, state.liqThresholdBps) &&
|
203
|
-
targetRateBps === settings.boostToBps
|
204
|
-
) {
|
205
|
-
targetRateBps = 6500;
|
206
|
-
}
|
207
|
-
|
208
|
-
const amountUsdToDcaIn =
|
209
|
-
fromBaseUnit(BigInt(Math.round(amountToDcaIn ?? 0)), state.debt.decimals) *
|
210
|
-
(dca?.tokenType === TokenType.Debt ? debtPrice : supplyPrice);
|
211
|
-
|
212
|
-
const rebalanceDirection =
|
213
|
-
amountUsdToDcaIn > 0 || state.liqUtilizationRateBps <= targetRateBps
|
214
|
-
? RebalanceDirection.Boost
|
215
|
-
: RebalanceDirection.Repay;
|
216
|
-
const adjustmentFeeBps = getSolautoFeesBps(
|
217
|
-
false,
|
218
|
-
targetLiqUtilizationRateBps,
|
219
|
-
calcNetWorthUsd(state),
|
220
|
-
rebalanceDirection
|
221
|
-
).total;
|
222
|
-
|
223
|
-
let debtAdjustmentUsd = getDebtAdjustmentUsd(
|
224
|
-
state.liqThresholdBps,
|
225
|
-
calcSupplyUsd(state) + amountUsdToDcaIn,
|
226
|
-
calcDebtUsd(state),
|
227
|
-
targetRateBps,
|
228
|
-
adjustmentFeeBps
|
229
|
-
);
|
230
|
-
|
231
|
-
const maxRepayTo = maxRepayToBps(state.maxLtvBps, state.liqThresholdBps);
|
232
|
-
return {
|
233
|
-
debtAdjustmentUsd,
|
234
|
-
repayingCloseToMaxLtv:
|
235
|
-
state.liqUtilizationRateBps > maxRepayTo && targetRateBps >= maxRepayTo,
|
236
|
-
amountToDcaIn: amountToDcaIn ?? 0,
|
237
|
-
amountUsdToDcaIn,
|
238
|
-
dcaTokenType: dca?.tokenType,
|
239
|
-
rebalanceAction:
|
240
|
-
(amountToDcaIn ?? 0) > 0
|
241
|
-
? "dca"
|
242
|
-
: rebalanceDirection === RebalanceDirection.Boost
|
243
|
-
? "boost"
|
244
|
-
: "repay",
|
245
|
-
rebalanceDirection,
|
246
|
-
feesUsd: Math.abs(debtAdjustmentUsd * fromBps(adjustmentFeeBps)),
|
247
|
-
targetRateBps,
|
248
|
-
};
|
249
|
-
}
|
250
|
-
|
251
|
-
function postRebalanceLiqUtilizationRateBps(
|
252
|
-
client: SolautoClient,
|
253
|
-
values: RebalanceValues,
|
254
|
-
swapOutputAmount?: bigint
|
255
|
-
) {
|
256
|
-
let supplyUsd =
|
257
|
-
calcSupplyUsd(client.solautoPositionState) + values.amountUsdToDcaIn;
|
258
|
-
let debtUsd = calcDebtUsd(client.solautoPositionState);
|
259
|
-
|
260
|
-
const boost = values.rebalanceDirection === RebalanceDirection.Boost;
|
261
|
-
|
262
|
-
const outputToken = toWeb3JsPublicKey(
|
263
|
-
boost
|
264
|
-
? client.solautoPositionState!.supply.mint
|
265
|
-
: client.solautoPositionState!.debt.mint
|
266
|
-
);
|
267
|
-
const debtAdjustmentUsdAbs = Math.abs(values.debtAdjustmentUsd);
|
268
|
-
const swapOutputUsd = swapOutputAmount
|
269
|
-
? fromBaseUnit(swapOutputAmount, tokenInfo(outputToken).decimals) *
|
270
|
-
(safeGetPrice(outputToken) ?? 0)
|
271
|
-
: debtAdjustmentUsdAbs;
|
272
|
-
|
273
|
-
supplyUsd = boost
|
274
|
-
? supplyUsd + swapOutputUsd
|
275
|
-
: supplyUsd - debtAdjustmentUsdAbs;
|
276
|
-
debtUsd = boost ? debtUsd + debtAdjustmentUsdAbs : debtUsd - swapOutputUsd;
|
277
|
-
|
278
|
-
return getLiqUtilzationRateBps(
|
279
|
-
supplyUsd,
|
280
|
-
debtUsd,
|
281
|
-
client.solautoPositionState?.liqThresholdBps ?? 0
|
282
|
-
);
|
283
|
-
}
|
284
|
-
|
285
|
-
function insufficientLiquidity(
|
286
|
-
amountNeededUsd: number,
|
287
|
-
liquidity: bigint,
|
288
|
-
tokenDecimals: number,
|
289
|
-
tokenPrice: number
|
290
|
-
) {
|
291
|
-
return amountNeededUsd > fromBaseUnit(liquidity, tokenDecimals) * tokenPrice;
|
292
|
-
}
|
293
|
-
|
294
|
-
export interface FlashLoanRequirements {
|
295
|
-
useDebtLiquidity: boolean;
|
296
|
-
signerFlashLoan: boolean;
|
297
|
-
}
|
298
|
-
|
299
|
-
export async function getFlashLoanRequirements(
|
300
|
-
client: SolautoClient,
|
301
|
-
values: RebalanceValues,
|
302
|
-
attemptNum?: number
|
303
|
-
): Promise<FlashLoanRequirements | undefined> {
|
304
|
-
let supplyUsd =
|
305
|
-
calcSupplyUsd(client.solautoPositionState) +
|
306
|
-
(values.dcaTokenType === TokenType.Supply ? values.amountUsdToDcaIn : 0);
|
307
|
-
let debtUsd = calcDebtUsd(client.solautoPositionState);
|
308
|
-
|
309
|
-
const debtAdjustmentUsdAbs = Math.abs(values.debtAdjustmentUsd);
|
310
|
-
supplyUsd =
|
311
|
-
values.rebalanceDirection === RebalanceDirection.Repay
|
312
|
-
? supplyUsd - debtAdjustmentUsdAbs
|
313
|
-
: supplyUsd;
|
314
|
-
debtUsd =
|
315
|
-
values.rebalanceDirection === RebalanceDirection.Boost
|
316
|
-
? debtUsd + debtAdjustmentUsdAbs
|
317
|
-
: debtUsd;
|
318
|
-
|
319
|
-
const tempLiqUtilizationRateBps = getLiqUtilzationRateBps(
|
320
|
-
supplyUsd,
|
321
|
-
debtUsd,
|
322
|
-
client.solautoPositionState!.liqThresholdBps
|
323
|
-
);
|
324
|
-
const maxLiqUtilizationRateBps = getMaxLiqUtilizationRateBps(
|
325
|
-
client.solautoPositionState!.maxLtvBps,
|
326
|
-
client.solautoPositionState!.liqThresholdBps,
|
327
|
-
0.02
|
328
|
-
);
|
329
|
-
const requiresFlashLoan =
|
330
|
-
supplyUsd <= 0 || tempLiqUtilizationRateBps > maxLiqUtilizationRateBps;
|
331
|
-
|
332
|
-
const supplyPrice = safeGetPrice(client.supplyMint) ?? 0;
|
333
|
-
const debtPrice = safeGetPrice(client.debtMint) ?? 0;
|
334
|
-
const debtAdjustmentUsd = Math.abs(values.debtAdjustmentUsd);
|
335
|
-
|
336
|
-
const insufficientSupplyLiquidity = insufficientLiquidity(
|
337
|
-
debtAdjustmentUsd,
|
338
|
-
client.supplyLiquidityAvailable(),
|
339
|
-
tokenInfo(client.supplyMint).decimals,
|
340
|
-
supplyPrice
|
341
|
-
);
|
342
|
-
const insufficientDebtLiquidity = insufficientLiquidity(
|
343
|
-
debtAdjustmentUsd,
|
344
|
-
client.debtLiquidityAvailable(),
|
345
|
-
tokenInfo(client.debtMint).decimals,
|
346
|
-
debtPrice
|
347
|
-
);
|
348
|
-
|
349
|
-
let useDebtLiquidity =
|
350
|
-
values.rebalanceDirection === RebalanceDirection.Boost ||
|
351
|
-
insufficientSupplyLiquidity;
|
352
|
-
|
353
|
-
let signerFlashLoan = false;
|
354
|
-
if (
|
355
|
-
(attemptNum ?? 0) >= 3 ||
|
356
|
-
(insufficientSupplyLiquidity && insufficientDebtLiquidity)
|
357
|
-
) {
|
358
|
-
const { supplyBalance, debtBalance } = await client.signerBalances();
|
359
|
-
const sufficientSignerSupplyLiquidity = !insufficientLiquidity(
|
360
|
-
debtAdjustmentUsd,
|
361
|
-
supplyBalance,
|
362
|
-
tokenInfo(client.supplyMint).decimals,
|
363
|
-
supplyPrice
|
364
|
-
);
|
365
|
-
const sufficientSignerDebtLiquidity = !insufficientLiquidity(
|
366
|
-
debtAdjustmentUsd,
|
367
|
-
debtBalance,
|
368
|
-
tokenInfo(client.debtMint).decimals,
|
369
|
-
debtPrice
|
370
|
-
);
|
371
|
-
|
372
|
-
signerFlashLoan =
|
373
|
-
sufficientSignerSupplyLiquidity || sufficientSignerDebtLiquidity;
|
374
|
-
if (signerFlashLoan) {
|
375
|
-
useDebtLiquidity =
|
376
|
-
values.rebalanceDirection === RebalanceDirection.Boost ||
|
377
|
-
!sufficientSignerSupplyLiquidity;
|
378
|
-
} else {
|
379
|
-
throw new Error(
|
380
|
-
`Insufficient liquidity to perform the transaction`
|
381
|
-
);
|
382
|
-
}
|
383
|
-
}
|
384
|
-
|
385
|
-
consoleLog("Requires flash loan:", requiresFlashLoan);
|
386
|
-
consoleLog("Use debt liquidity:", useDebtLiquidity);
|
387
|
-
consoleLog(
|
388
|
-
"Intermediary liq utilization rate:",
|
389
|
-
tempLiqUtilizationRateBps,
|
390
|
-
`$${supplyUsd}`,
|
391
|
-
`$${debtUsd}`,
|
392
|
-
"Max:",
|
393
|
-
maxLiqUtilizationRateBps
|
394
|
-
);
|
395
|
-
|
396
|
-
return requiresFlashLoan ? { useDebtLiquidity, signerFlashLoan } : undefined;
|
397
|
-
}
|
398
|
-
|
399
|
-
export interface FlashLoanDetails extends FlashLoanRequirements {
|
400
|
-
baseUnitAmount: bigint;
|
401
|
-
mint: PublicKey;
|
402
|
-
}
|
403
|
-
|
404
|
-
export function getFlashLoanDetails(
|
405
|
-
client: SolautoClient,
|
406
|
-
flRequirements: FlashLoanRequirements,
|
407
|
-
values: RebalanceValues,
|
408
|
-
jupQuote: QuoteResponse
|
409
|
-
): FlashLoanDetails | undefined {
|
410
|
-
let flashLoanToken: PositionTokenUsage | undefined = undefined;
|
411
|
-
|
412
|
-
const inAmount = BigInt(parseInt(jupQuote.inAmount));
|
413
|
-
const outAmount = BigInt(parseInt(jupQuote.outAmount));
|
414
|
-
|
415
|
-
const boosting = values.rebalanceDirection === RebalanceDirection.Boost;
|
416
|
-
if (boosting || flRequirements.useDebtLiquidity) {
|
417
|
-
flashLoanToken = client.solautoPositionState!.debt;
|
418
|
-
} else {
|
419
|
-
flashLoanToken = client.solautoPositionState!.supply;
|
420
|
-
}
|
421
|
-
|
422
|
-
if (jupQuote.swapMode !== "ExactOut" && jupQuote.swapMode !== "ExactIn") {
|
423
|
-
throw new Error("Token ledger swap not currently supported");
|
424
|
-
}
|
425
|
-
|
426
|
-
const baseUnitAmount =
|
427
|
-
boosting || (!boosting && !flRequirements.useDebtLiquidity)
|
428
|
-
? inAmount
|
429
|
-
: outAmount;
|
430
|
-
|
431
|
-
return {
|
432
|
-
...flRequirements,
|
433
|
-
baseUnitAmount,
|
434
|
-
mint: toWeb3JsPublicKey(flashLoanToken.mint),
|
435
|
-
};
|
436
|
-
}
|
437
|
-
|
438
|
-
async function findSufficientQuote(
|
439
|
-
client: SolautoClient,
|
440
|
-
values: RebalanceValues,
|
441
|
-
jupSwapInput: JupSwapInput,
|
442
|
-
criteria: {
|
443
|
-
minOutputAmount?: bigint;
|
444
|
-
minLiqUtilizationRateBps?: number;
|
445
|
-
maxLiqUtilizationRateBps?: number;
|
446
|
-
}
|
447
|
-
): Promise<QuoteResponse> {
|
448
|
-
let jupQuote: QuoteResponse;
|
449
|
-
let insufficient: boolean = false;
|
450
|
-
|
451
|
-
for (let i = 0; i < 10; i++) {
|
452
|
-
consoleLog("Finding sufficient quote...");
|
453
|
-
jupQuote = await getJupQuote(jupSwapInput);
|
454
|
-
|
455
|
-
const outputAmount = parseInt(jupQuote.outAmount);
|
456
|
-
const postRebalanceRate = postRebalanceLiqUtilizationRateBps(
|
457
|
-
client,
|
458
|
-
values,
|
459
|
-
BigInt(outputAmount)
|
460
|
-
);
|
461
|
-
insufficient = criteria.minOutputAmount
|
462
|
-
? outputAmount < Number(criteria.minOutputAmount)
|
463
|
-
: criteria.minLiqUtilizationRateBps
|
464
|
-
? postRebalanceRate < criteria.minLiqUtilizationRateBps
|
465
|
-
: postRebalanceRate > criteria.maxLiqUtilizationRateBps!;
|
466
|
-
|
467
|
-
if (insufficient) {
|
468
|
-
consoleLog(jupQuote);
|
469
|
-
jupSwapInput.amount =
|
470
|
-
jupSwapInput.amount +
|
471
|
-
BigInt(Math.round(Number(jupSwapInput.amount) * 0.01));
|
472
|
-
} else {
|
473
|
-
break;
|
474
|
-
}
|
475
|
-
}
|
476
|
-
|
477
|
-
return jupQuote!;
|
478
|
-
}
|
479
|
-
|
480
|
-
export async function getJupSwapRebalanceDetails(
|
481
|
-
client: SolautoClient,
|
482
|
-
values: RebalanceValues,
|
483
|
-
flRequirements?: FlashLoanRequirements,
|
484
|
-
targetLiqUtilizationRateBps?: number,
|
485
|
-
attemptNum?: number
|
486
|
-
): Promise<JupSwapDetails> {
|
487
|
-
const input =
|
488
|
-
values.rebalanceDirection === RebalanceDirection.Boost
|
489
|
-
? client.solautoPositionState!.debt
|
490
|
-
: client.solautoPositionState!.supply;
|
491
|
-
const output =
|
492
|
-
values.rebalanceDirection === RebalanceDirection.Boost
|
493
|
-
? client.solautoPositionState!.supply
|
494
|
-
: client.solautoPositionState!.debt;
|
495
|
-
|
496
|
-
const rebalanceToZero = targetLiqUtilizationRateBps === 0;
|
497
|
-
const usdToSwap =
|
498
|
-
Math.abs(values.debtAdjustmentUsd) +
|
499
|
-
(values.dcaTokenType === TokenType.Debt ? values.amountUsdToDcaIn : 0);
|
500
|
-
|
501
|
-
let inputAmount = toBaseUnit(
|
502
|
-
usdToSwap / safeGetPrice(input.mint)!,
|
503
|
-
input.decimals
|
504
|
-
);
|
505
|
-
let outputAmount = rebalanceToZero
|
506
|
-
? output.amountUsed.baseUnit +
|
507
|
-
BigInt(
|
508
|
-
Math.round(
|
509
|
-
Number(output.amountUsed.baseUnit) *
|
510
|
-
// Add this small percentage to account for the APR on the debt between now and the transaction
|
511
|
-
0.0001
|
512
|
-
)
|
513
|
-
)
|
514
|
-
: toBaseUnit(usdToSwap / safeGetPrice(output.mint)!, output.decimals);
|
515
|
-
|
516
|
-
const repaying = values.rebalanceDirection === RebalanceDirection.Repay;
|
517
|
-
|
518
|
-
const flashLoanRepayFromDebt =
|
519
|
-
repaying && flRequirements && flRequirements.useDebtLiquidity;
|
520
|
-
|
521
|
-
const exactOut = flashLoanRepayFromDebt && !rebalanceToZero;
|
522
|
-
// || rebalanceToZero
|
523
|
-
const exactIn = !exactOut;
|
524
|
-
|
525
|
-
if (exactIn && (rebalanceToZero || values.repayingCloseToMaxLtv)) {
|
526
|
-
inputAmount = inputAmount + BigInt(Math.round(Number(inputAmount) * 0.005));
|
527
|
-
}
|
528
|
-
|
529
|
-
const jupSwapInput: JupSwapInput = {
|
530
|
-
inputMint: toWeb3JsPublicKey(input.mint),
|
531
|
-
outputMint: toWeb3JsPublicKey(output.mint),
|
532
|
-
exactIn,
|
533
|
-
exactOut,
|
534
|
-
amount: exactOut ? outputAmount : inputAmount,
|
535
|
-
};
|
536
|
-
consoleLog(jupSwapInput);
|
537
|
-
|
538
|
-
let jupQuote: QuoteResponse | undefined = undefined;
|
539
|
-
if (exactIn && (rebalanceToZero || values.repayingCloseToMaxLtv)) {
|
540
|
-
jupQuote = await findSufficientQuote(client, values, jupSwapInput, {
|
541
|
-
minOutputAmount: rebalanceToZero ? outputAmount : undefined,
|
542
|
-
maxLiqUtilizationRateBps: values.repayingCloseToMaxLtv
|
543
|
-
? maxRepayToBps(
|
544
|
-
client.solautoPositionState?.maxLtvBps ?? 0,
|
545
|
-
client.solautoPositionState?.liqThresholdBps ?? 0
|
546
|
-
) - 15
|
547
|
-
: undefined,
|
548
|
-
});
|
549
|
-
}
|
550
|
-
|
551
|
-
const addPadding = exactOut;
|
552
|
-
|
553
|
-
return {
|
554
|
-
...jupSwapInput,
|
555
|
-
destinationWallet: flashLoanRepayFromDebt
|
556
|
-
? toWeb3JsPublicKey(client.signer.publicKey)
|
557
|
-
: client.solautoPosition,
|
558
|
-
slippageIncFactor: 0.2 + (attemptNum ?? 0) * 0.25,
|
559
|
-
addPadding,
|
560
|
-
jupQuote,
|
561
|
-
};
|
562
|
-
}
|
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
15
15
|
};
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
17
|
-
__exportStar(require("./solautoMarginfiClient"), exports);
|
18
|
-
__exportStar(require("./solautoClient"), exports);
|
19
17
|
__exportStar(require("./referralStateManager"), exports);
|
18
|
+
__exportStar(require("./solautoClient"), exports);
|
19
|
+
__exportStar(require("./solautoMarginfiClient"), exports);
|
20
20
|
__exportStar(require("./txHandler"), exports);
|
File without changes
|
File without changes
|
File without changes
|