@haven-fi/solauto-sdk 1.0.581 → 1.0.583
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/constants/switchboardConstants.d.ts.map +1 -1
- package/dist/constants/switchboardConstants.js +5 -5
- 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/priceUtils.js +1 -1
- 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} +48 -173
- package/dist/utils/switchboardUtils.d.ts +1 -1
- package/dist/utils/switchboardUtils.d.ts.map +1 -1
- package/dist/utils/switchboardUtils.js +10 -9
- package/local/createTokenAccounts.ts +1 -5
- package/local/logPositions.ts +22 -68
- package/local/shared.ts +29 -24
- package/local/txSandbox.ts +27 -0
- package/local/updateMarginfiLUT.ts +80 -4
- package/package.json +4 -7
- package/src/constants/solautoConstants.ts +4 -9
- package/src/constants/switchboardConstants.ts +10 -5
- 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} +66 -288
- package/src/utils/switchboardUtils.ts +15 -17
- 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
@@ -0,0 +1,322 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.SolautoMarginfiClient = void 0;
|
4
|
+
const umi_web3js_adapters_1 = require("@metaplex-foundation/umi-web3js-adapters");
|
5
|
+
const umi_1 = require("@metaplex-foundation/umi");
|
6
|
+
const web3_js_1 = require("@solana/web3.js");
|
7
|
+
const solautoClient_1 = require("./solautoClient");
|
8
|
+
const marginfiAccounts_1 = require("../../constants/marginfiAccounts");
|
9
|
+
const generated_1 = require("../../generated");
|
10
|
+
const accountUtils_1 = require("../../utils/accountUtils");
|
11
|
+
const marginfi_sdk_1 = require("../../marginfi-sdk");
|
12
|
+
const marginfiUtils_1 = require("../../utils/marginfiUtils");
|
13
|
+
const solautoUtils_1 = require("../../utils/solautoUtils");
|
14
|
+
class SolautoMarginfiClient extends solautoClient_1.SolautoClient {
|
15
|
+
constructor() {
|
16
|
+
super(...arguments);
|
17
|
+
this.lendingPlatform = generated_1.LendingPlatform.Marginfi;
|
18
|
+
}
|
19
|
+
async initialize(args) {
|
20
|
+
await super.initialize(args);
|
21
|
+
this.marginfiGroup = await this.solautoPosition.lendingPool();
|
22
|
+
if (this.selfManaged) {
|
23
|
+
this.marginfiAccount =
|
24
|
+
args.marginfiAccount ??
|
25
|
+
(0, umi_1.createSignerFromKeypair)(this.umi, this.umi.eddsa.generateKeypair());
|
26
|
+
}
|
27
|
+
else {
|
28
|
+
if (this.solautoPosition.exists()) {
|
29
|
+
this.marginfiAccount = this.solautoPosition.lpUserAccount;
|
30
|
+
}
|
31
|
+
else {
|
32
|
+
const accounts = await (0, marginfiUtils_1.getAllMarginfiAccountsByAuthority)(this.umi, this.solautoPosition.publicKey, this.marginfiGroup, false);
|
33
|
+
const reusableAccounts = accounts.length > 0
|
34
|
+
? (await (0, marginfi_sdk_1.safeFetchAllMarginfiAccount)(this.umi, accounts.map((x) => (0, umi_1.publicKey)(x.marginfiAccount)))).filter((x) => (0, marginfiUtils_1.marginfiAccountEmpty)(x))
|
35
|
+
: [];
|
36
|
+
this.marginfiAccount =
|
37
|
+
reusableAccounts.length > 0
|
38
|
+
? (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(reusableAccounts[0].publicKey)
|
39
|
+
: (0, umi_1.createSignerFromKeypair)(this.umi, this.umi.eddsa.generateKeypair());
|
40
|
+
}
|
41
|
+
}
|
42
|
+
this.marginfiAccountPk =
|
43
|
+
"publicKey" in this.marginfiAccount
|
44
|
+
? (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(this.marginfiAccount.publicKey)
|
45
|
+
: this.marginfiAccount;
|
46
|
+
if ("publicKey" in this.marginfiAccount) {
|
47
|
+
this.otherSigners.push(this.marginfiAccount);
|
48
|
+
}
|
49
|
+
this.marginfiSupplyAccounts =
|
50
|
+
marginfiAccounts_1.MARGINFI_ACCOUNTS[this.marginfiGroup.toString()][this.solautoPosition.supplyMint().toString()];
|
51
|
+
this.marginfiDebtAccounts =
|
52
|
+
marginfiAccounts_1.MARGINFI_ACCOUNTS[this.marginfiGroup.toString()][this.solautoPosition.debtMint().toString()];
|
53
|
+
// TODO: Don't dynamically pull oracle from bank until Marginfi sorts out their price oracle issues.
|
54
|
+
// const [supplyBank, debtBank] = await safeFetchAllBank(this.umi, [
|
55
|
+
// publicKey(this.marginfiSupplyAccounts.bank),
|
56
|
+
// publicKey(this.marginfiDebtAccounts.bank),
|
57
|
+
// ]);
|
58
|
+
// this.supplyPriceOracle = toWeb3JsPublicKey(supplyBank.config.oracleKeys[0]);
|
59
|
+
// this.debtPriceOracle = toWeb3JsPublicKey(debtBank.config.oracleKeys[0]);
|
60
|
+
this.supplyPriceOracle = new web3_js_1.PublicKey(this.marginfiSupplyAccounts.priceOracle);
|
61
|
+
this.debtPriceOracle = new web3_js_1.PublicKey(this.marginfiDebtAccounts.priceOracle);
|
62
|
+
this.log("Marginfi account:", this.marginfiAccountPk.toString());
|
63
|
+
}
|
64
|
+
defaultLookupTables() {
|
65
|
+
return [marginfiAccounts_1.MARGINFI_ACCOUNTS_LOOKUP_TABLE, ...super.defaultLookupTables()];
|
66
|
+
}
|
67
|
+
lutAccountsToAdd() {
|
68
|
+
return [...super.lutAccountsToAdd(), this.marginfiAccountPk];
|
69
|
+
}
|
70
|
+
marginfiAccountInitialize(marginfiAccount) {
|
71
|
+
return (0, marginfi_sdk_1.marginfiAccountInitialize)(this.umi, {
|
72
|
+
marginfiAccount: marginfiAccount,
|
73
|
+
marginfiGroup: (0, umi_1.publicKey)(this.marginfiGroup),
|
74
|
+
authority: this.signer,
|
75
|
+
feePayer: this.signer,
|
76
|
+
});
|
77
|
+
}
|
78
|
+
openPositionIx(settings, dca) {
|
79
|
+
return super
|
80
|
+
.openPositionIx(settings, dca)
|
81
|
+
.add(this.marginfiOpenPositionIx(settings, dca));
|
82
|
+
}
|
83
|
+
marginfiOpenPositionIx(settings, dca, positionType) {
|
84
|
+
let signerDebtTa = undefined;
|
85
|
+
if (dca) {
|
86
|
+
signerDebtTa = (0, umi_1.publicKey)(this.signerDebtTa);
|
87
|
+
}
|
88
|
+
return (0, generated_1.marginfiOpenPosition)(this.umi, {
|
89
|
+
signer: this.signer,
|
90
|
+
marginfiProgram: (0, umi_1.publicKey)(marginfi_sdk_1.MARGINFI_PROGRAM_ID),
|
91
|
+
signerReferralState: (0, umi_1.publicKey)(this.referralState),
|
92
|
+
referredByState: this.referredByState
|
93
|
+
? (0, umi_1.publicKey)(this.referredByState)
|
94
|
+
: undefined,
|
95
|
+
referredBySupplyTa: this.referredBySupplyTa()
|
96
|
+
? (0, umi_1.publicKey)(this.referredBySupplyTa())
|
97
|
+
: undefined,
|
98
|
+
solautoPosition: (0, umi_1.publicKey)(this.solautoPosition.publicKey),
|
99
|
+
marginfiGroup: (0, umi_1.publicKey)(this.marginfiGroup),
|
100
|
+
marginfiAccount: "publicKey" in this.marginfiAccount
|
101
|
+
? this.marginfiAccount
|
102
|
+
: (0, umi_1.publicKey)(this.marginfiAccount),
|
103
|
+
supplyMint: (0, umi_1.publicKey)(this.solautoPosition.supplyMint()),
|
104
|
+
supplyBank: (0, umi_1.publicKey)(this.marginfiSupplyAccounts.bank),
|
105
|
+
positionSupplyTa: (0, umi_1.publicKey)(this.positionSupplyTa),
|
106
|
+
debtMint: (0, umi_1.publicKey)(this.solautoPosition.debtMint()),
|
107
|
+
debtBank: (0, umi_1.publicKey)(this.marginfiDebtAccounts.bank),
|
108
|
+
positionDebtTa: (0, umi_1.publicKey)(this.positionDebtTa),
|
109
|
+
signerDebtTa: signerDebtTa,
|
110
|
+
positionType: positionType ?? generated_1.PositionType.Leverage,
|
111
|
+
positionData: {
|
112
|
+
positionId: this.positionId,
|
113
|
+
settings: settings ?? null,
|
114
|
+
dca: dca ?? null,
|
115
|
+
},
|
116
|
+
});
|
117
|
+
}
|
118
|
+
closePositionIx() {
|
119
|
+
return (0, generated_1.closePosition)(this.umi, {
|
120
|
+
signer: this.signer,
|
121
|
+
solautoPosition: (0, umi_1.publicKey)(this.solautoPosition.publicKey),
|
122
|
+
signerSupplyTa: (0, umi_1.publicKey)(this.signerSupplyTa),
|
123
|
+
positionSupplyTa: (0, umi_1.publicKey)(this.positionSupplyTa),
|
124
|
+
positionDebtTa: (0, umi_1.publicKey)(this.positionDebtTa),
|
125
|
+
signerDebtTa: (0, umi_1.publicKey)(this.signerDebtTa),
|
126
|
+
lpUserAccount: (0, umi_1.publicKey)(this.marginfiAccountPk),
|
127
|
+
});
|
128
|
+
}
|
129
|
+
refreshIx() {
|
130
|
+
return (0, generated_1.marginfiRefreshData)(this.umi, {
|
131
|
+
signer: this.signer,
|
132
|
+
marginfiProgram: (0, umi_1.publicKey)(marginfi_sdk_1.MARGINFI_PROGRAM_ID),
|
133
|
+
marginfiGroup: (0, umi_1.publicKey)(this.marginfiGroup),
|
134
|
+
marginfiAccount: (0, umi_1.publicKey)(this.marginfiAccount),
|
135
|
+
supplyBank: (0, umi_1.publicKey)(this.marginfiSupplyAccounts.bank),
|
136
|
+
supplyPriceOracle: (0, umi_1.publicKey)(this.supplyPriceOracle),
|
137
|
+
debtBank: (0, umi_1.publicKey)(this.marginfiDebtAccounts.bank),
|
138
|
+
debtPriceOracle: (0, umi_1.publicKey)(this.debtPriceOracle),
|
139
|
+
solautoPosition: (0, umi_1.publicKey)(this.solautoPosition.publicKey),
|
140
|
+
});
|
141
|
+
}
|
142
|
+
protocolInteractionIx(args) {
|
143
|
+
let tx = super.protocolInteractionIx(args);
|
144
|
+
if (this.selfManaged) {
|
145
|
+
return tx.add(this.marginfiProtocolInteractionIx(args));
|
146
|
+
}
|
147
|
+
else {
|
148
|
+
return tx.add(this.marginfiSolautoProtocolInteractionIx(args));
|
149
|
+
}
|
150
|
+
}
|
151
|
+
marginfiProtocolInteractionIx(args) {
|
152
|
+
switch (args.__kind) {
|
153
|
+
case "Deposit": {
|
154
|
+
return (0, marginfi_sdk_1.lendingAccountDeposit)(this.umi, {
|
155
|
+
amount: args.fields[0],
|
156
|
+
signer: this.signer,
|
157
|
+
signerTokenAccount: (0, umi_1.publicKey)(this.signerSupplyTa),
|
158
|
+
marginfiAccount: (0, umi_1.publicKey)(this.marginfiAccountPk),
|
159
|
+
marginfiGroup: (0, umi_1.publicKey)(this.marginfiGroup),
|
160
|
+
bank: (0, umi_1.publicKey)(this.marginfiSupplyAccounts.bank),
|
161
|
+
bankLiquidityVault: (0, umi_1.publicKey)(this.marginfiSupplyAccounts.liquidityVault),
|
162
|
+
});
|
163
|
+
}
|
164
|
+
case "Borrow": {
|
165
|
+
return (0, marginfi_sdk_1.lendingAccountBorrow)(this.umi, {
|
166
|
+
amount: args.fields[0],
|
167
|
+
signer: this.signer,
|
168
|
+
destinationTokenAccount: (0, umi_1.publicKey)(this.signerDebtTa),
|
169
|
+
marginfiAccount: (0, umi_1.publicKey)(this.marginfiAccountPk),
|
170
|
+
marginfiGroup: (0, umi_1.publicKey)(this.marginfiGroup),
|
171
|
+
bank: (0, umi_1.publicKey)(this.marginfiDebtAccounts.bank),
|
172
|
+
bankLiquidityVault: (0, umi_1.publicKey)(this.marginfiDebtAccounts.liquidityVault),
|
173
|
+
bankLiquidityVaultAuthority: (0, umi_1.publicKey)(this.marginfiDebtAccounts.vaultAuthority),
|
174
|
+
});
|
175
|
+
}
|
176
|
+
case "Repay": {
|
177
|
+
return (0, marginfi_sdk_1.lendingAccountRepay)(this.umi, {
|
178
|
+
amount: args.fields[0].__kind === "Some" ? args.fields[0].fields[0] : 0,
|
179
|
+
repayAll: args.fields[0].__kind === "All" ? true : false,
|
180
|
+
signer: this.signer,
|
181
|
+
signerTokenAccount: (0, umi_1.publicKey)(this.signerDebtTa),
|
182
|
+
marginfiAccount: (0, umi_1.publicKey)(this.marginfiAccountPk),
|
183
|
+
marginfiGroup: (0, umi_1.publicKey)(this.marginfiGroup),
|
184
|
+
bank: (0, umi_1.publicKey)(this.marginfiDebtAccounts.bank),
|
185
|
+
bankLiquidityVault: (0, umi_1.publicKey)(this.marginfiDebtAccounts.liquidityVault),
|
186
|
+
});
|
187
|
+
}
|
188
|
+
case "Withdraw": {
|
189
|
+
return (0, marginfi_sdk_1.lendingAccountWithdraw)(this.umi, {
|
190
|
+
amount: args.fields[0].__kind === "Some" ? args.fields[0].fields[0] : 0,
|
191
|
+
withdrawAll: args.fields[0].__kind === "All" ? true : false,
|
192
|
+
signer: this.signer,
|
193
|
+
destinationTokenAccount: (0, umi_1.publicKey)(this.signerSupplyTa),
|
194
|
+
marginfiAccount: (0, umi_1.publicKey)(this.marginfiAccountPk),
|
195
|
+
marginfiGroup: (0, umi_1.publicKey)(this.marginfiGroup),
|
196
|
+
bank: (0, umi_1.publicKey)(this.marginfiSupplyAccounts.bank),
|
197
|
+
bankLiquidityVault: (0, umi_1.publicKey)(this.marginfiSupplyAccounts.liquidityVault),
|
198
|
+
bankLiquidityVaultAuthority: (0, umi_1.publicKey)(this.marginfiSupplyAccounts.vaultAuthority),
|
199
|
+
});
|
200
|
+
}
|
201
|
+
}
|
202
|
+
}
|
203
|
+
marginfiSolautoProtocolInteractionIx(args) {
|
204
|
+
let positionSupplyTa = undefined;
|
205
|
+
let vaultSupplyTa = undefined;
|
206
|
+
let supplyVaultAuthority = undefined;
|
207
|
+
if (args.__kind === "Deposit" || args.__kind === "Withdraw") {
|
208
|
+
positionSupplyTa = (0, umi_1.publicKey)(args.__kind === "Withdraw" || this.selfManaged
|
209
|
+
? this.signerSupplyTa
|
210
|
+
: this.positionSupplyTa);
|
211
|
+
vaultSupplyTa = (0, umi_1.publicKey)(this.marginfiSupplyAccounts.liquidityVault);
|
212
|
+
supplyVaultAuthority = (0, umi_1.publicKey)(this.marginfiSupplyAccounts.vaultAuthority);
|
213
|
+
}
|
214
|
+
let positionDebtTa = undefined;
|
215
|
+
let vaultDebtTa = undefined;
|
216
|
+
let debtVaultAuthority = undefined;
|
217
|
+
if (args.__kind === "Borrow" || args.__kind === "Repay") {
|
218
|
+
positionDebtTa = (0, umi_1.publicKey)(args.__kind === "Borrow" || this.selfManaged
|
219
|
+
? this.signerDebtTa
|
220
|
+
: this.positionDebtTa);
|
221
|
+
vaultDebtTa = (0, umi_1.publicKey)(this.marginfiDebtAccounts.liquidityVault);
|
222
|
+
debtVaultAuthority = (0, umi_1.publicKey)(this.marginfiDebtAccounts.vaultAuthority);
|
223
|
+
}
|
224
|
+
let supplyPriceOracle = undefined;
|
225
|
+
let debtPriceOracle = undefined;
|
226
|
+
if (args.__kind === "Withdraw" || args.__kind === "Borrow") {
|
227
|
+
supplyPriceOracle = (0, umi_1.publicKey)(this.supplyPriceOracle);
|
228
|
+
debtPriceOracle = (0, umi_1.publicKey)(this.debtPriceOracle);
|
229
|
+
}
|
230
|
+
return (0, generated_1.marginfiProtocolInteraction)(this.umi, {
|
231
|
+
signer: this.signer,
|
232
|
+
marginfiProgram: (0, umi_1.publicKey)(marginfi_sdk_1.MARGINFI_PROGRAM_ID),
|
233
|
+
solautoPosition: (0, umi_1.publicKey)(this.solautoPosition.publicKey),
|
234
|
+
marginfiGroup: (0, umi_1.publicKey)(this.marginfiGroup),
|
235
|
+
marginfiAccount: (0, umi_1.publicKey)(this.marginfiAccountPk),
|
236
|
+
supplyBank: (0, umi_1.publicKey)(this.marginfiSupplyAccounts.bank),
|
237
|
+
supplyPriceOracle,
|
238
|
+
positionSupplyTa,
|
239
|
+
vaultSupplyTa,
|
240
|
+
supplyVaultAuthority,
|
241
|
+
debtBank: (0, umi_1.publicKey)(this.marginfiDebtAccounts.bank),
|
242
|
+
debtPriceOracle,
|
243
|
+
positionDebtTa,
|
244
|
+
vaultDebtTa,
|
245
|
+
debtVaultAuthority,
|
246
|
+
solautoAction: args,
|
247
|
+
});
|
248
|
+
}
|
249
|
+
rebalanceIx(rebalanceStep, data) {
|
250
|
+
const inputIsSupply = new web3_js_1.PublicKey(data.swapQuote.inputMint).equals(this.solautoPosition.supplyMint());
|
251
|
+
const outputIsSupply = new web3_js_1.PublicKey(data.swapQuote.outputMint).equals(this.solautoPosition.supplyMint());
|
252
|
+
const preSwapRebalance = rebalanceStep === generated_1.RebalanceStep.PreSwap;
|
253
|
+
const postSwapRebalance = rebalanceStep === generated_1.RebalanceStep.PostSwap;
|
254
|
+
const needSupplyAccounts = (inputIsSupply && preSwapRebalance) ||
|
255
|
+
(outputIsSupply && postSwapRebalance) ||
|
256
|
+
(inputIsSupply && data.flashLoan !== undefined && postSwapRebalance);
|
257
|
+
const needDebtAccounts = (!inputIsSupply && preSwapRebalance) ||
|
258
|
+
(!outputIsSupply && postSwapRebalance) ||
|
259
|
+
(!inputIsSupply && data.flashLoan !== undefined && postSwapRebalance);
|
260
|
+
const isFirstRebalance = (preSwapRebalance && (0, solautoUtils_1.hasFirstRebalance)(data.rebalanceType)) ||
|
261
|
+
(postSwapRebalance &&
|
262
|
+
data.rebalanceType === generated_1.SolautoRebalanceType.FLSwapThenRebalance);
|
263
|
+
const addAuthorityTas = this.selfManaged || data.values.tokenBalanceChange !== undefined;
|
264
|
+
return (0, generated_1.marginfiRebalance)(this.umi, {
|
265
|
+
signer: this.signer,
|
266
|
+
marginfiProgram: (0, umi_1.publicKey)(marginfi_sdk_1.MARGINFI_PROGRAM_ID),
|
267
|
+
ixsSysvar: (0, umi_1.publicKey)(web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY),
|
268
|
+
solautoFeesTa: (0, umi_1.publicKey)(data.values.rebalanceDirection === generated_1.RebalanceDirection.Boost
|
269
|
+
? this.solautoFeesSupplyTa
|
270
|
+
: this.solautoFeesDebtTa),
|
271
|
+
authorityReferralState: (0, umi_1.publicKey)(this.referralState),
|
272
|
+
referredByTa: this.referredByState
|
273
|
+
? (0, umi_1.publicKey)(data.values.rebalanceDirection === generated_1.RebalanceDirection.Boost
|
274
|
+
? this.referredBySupplyTa()
|
275
|
+
: this.referredByDebtTa())
|
276
|
+
: undefined,
|
277
|
+
positionAuthority: data.values.tokenBalanceChange !== undefined
|
278
|
+
? (0, umi_1.publicKey)(this.authority)
|
279
|
+
: undefined,
|
280
|
+
solautoPosition: (0, umi_1.publicKey)(this.solautoPosition.publicKey),
|
281
|
+
marginfiGroup: (0, umi_1.publicKey)(this.marginfiGroup),
|
282
|
+
marginfiAccount: (0, umi_1.publicKey)(this.marginfiAccountPk),
|
283
|
+
intermediaryTa: (0, umi_1.publicKey)((0, accountUtils_1.getTokenAccount)((0, umi_web3js_adapters_1.toWeb3JsPublicKey)(this.signer.publicKey), new web3_js_1.PublicKey(data.swapQuote.inputMint))),
|
284
|
+
supplyBank: (0, umi_1.publicKey)(this.marginfiSupplyAccounts.bank),
|
285
|
+
supplyPriceOracle: (0, umi_1.publicKey)(this.supplyPriceOracle),
|
286
|
+
positionSupplyTa: (0, umi_1.publicKey)(this.positionSupplyTa),
|
287
|
+
authoritySupplyTa: addAuthorityTas
|
288
|
+
? (0, umi_1.publicKey)((0, accountUtils_1.getTokenAccount)(this.authority, this.solautoPosition.supplyMint()))
|
289
|
+
: undefined,
|
290
|
+
vaultSupplyTa: needSupplyAccounts
|
291
|
+
? (0, umi_1.publicKey)(this.marginfiSupplyAccounts.liquidityVault)
|
292
|
+
: undefined,
|
293
|
+
supplyVaultAuthority: needSupplyAccounts
|
294
|
+
? (0, umi_1.publicKey)(this.marginfiSupplyAccounts.vaultAuthority)
|
295
|
+
: undefined,
|
296
|
+
debtBank: (0, umi_1.publicKey)(this.marginfiDebtAccounts.bank),
|
297
|
+
debtPriceOracle: (0, umi_1.publicKey)(this.debtPriceOracle),
|
298
|
+
positionDebtTa: (0, umi_1.publicKey)(this.positionDebtTa),
|
299
|
+
authorityDebtTa: addAuthorityTas
|
300
|
+
? (0, umi_1.publicKey)((0, accountUtils_1.getTokenAccount)(this.authority, this.solautoPosition.debtMint()))
|
301
|
+
: undefined,
|
302
|
+
vaultDebtTa: needDebtAccounts
|
303
|
+
? (0, umi_1.publicKey)(this.marginfiDebtAccounts.liquidityVault)
|
304
|
+
: undefined,
|
305
|
+
debtVaultAuthority: needDebtAccounts
|
306
|
+
? (0, umi_1.publicKey)(this.marginfiDebtAccounts.vaultAuthority)
|
307
|
+
: undefined,
|
308
|
+
rebalanceType: data.rebalanceType,
|
309
|
+
targetLiqUtilizationRateBps: data.targetLiqUtilizationRateBps ?? null,
|
310
|
+
swapInAmountBaseUnit: isFirstRebalance
|
311
|
+
? parseInt(data.swapQuote.inAmount)
|
312
|
+
: null,
|
313
|
+
swapType: data.swapQuote.swapMode === "ExactOut" && isFirstRebalance
|
314
|
+
? generated_1.SwapType.ExactOut
|
315
|
+
: null,
|
316
|
+
flashLoanFeeBps: data.flashLoan?.flFeeBps && isFirstRebalance
|
317
|
+
? data.flashLoan.flFeeBps
|
318
|
+
: null,
|
319
|
+
});
|
320
|
+
}
|
321
|
+
}
|
322
|
+
exports.SolautoMarginfiClient = SolautoMarginfiClient;
|
@@ -1,14 +1,23 @@
|
|
1
1
|
import { Signer, Umi } from "@metaplex-foundation/umi";
|
2
2
|
import { Connection, PublicKey } from "@solana/web3.js";
|
3
|
+
import { WalletAdapter } from "@metaplex-foundation/umi-signer-wallet-adapters";
|
4
|
+
export interface TxHandlerProps {
|
5
|
+
signer?: Signer;
|
6
|
+
wallet?: WalletAdapter;
|
7
|
+
rpcUrl: string;
|
8
|
+
showLogs?: boolean;
|
9
|
+
programId?: PublicKey;
|
10
|
+
wsEndpoint?: string;
|
11
|
+
}
|
3
12
|
export declare abstract class TxHandler {
|
4
13
|
rpcUrl: string;
|
14
|
+
showLogs: boolean;
|
5
15
|
programId: PublicKey;
|
6
|
-
wsEndpoint?: string | undefined;
|
7
16
|
connection: Connection;
|
8
17
|
umi: Umi;
|
9
18
|
signer: Signer;
|
10
19
|
otherSigners: Signer[];
|
11
|
-
constructor(
|
20
|
+
constructor(props: TxHandlerProps);
|
12
21
|
log(...args: any[]): void;
|
13
22
|
abstract defaultLookupTables(): string[];
|
14
23
|
abstract resetLiveTxUpdates(success?: boolean): Promise<void>;
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"txHandler.d.ts","sourceRoot":"","sources":["../../../src/services/solauto/txHandler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAkB,GAAG,EAAE,MAAM,0BAA0B,CAAC;AACvE,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAGxD,OAAO,EACL,aAAa,EAEd,MAAM,iDAAiD,CAAC;AAEzD,MAAM,WAAW,cAAc;IAC7B,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,aAAa,CAAC;IACvB,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,SAAS,CAAC,EAAE,SAAS,CAAC;IACtB,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED,8BAAsB,SAAS;IACtB,MAAM,EAAG,MAAM,CAAC;IAChB,QAAQ,UAAS;IACjB,SAAS,YAAwB;IAEjC,UAAU,EAAG,UAAU,CAAC;IACxB,GAAG,EAAG,GAAG,CAAC;IACV,MAAM,EAAG,MAAM,CAAC;IAChB,YAAY,EAAE,MAAM,EAAE,CAAM;gBAEvB,KAAK,EAAE,cAAc;IAiCjC,GAAG,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,IAAI;IAIzB,QAAQ,CAAC,mBAAmB,IAAI,MAAM,EAAE;IAExC,QAAQ,CAAC,kBAAkB,CAAC,OAAO,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC;CAC9D"}
|
@@ -0,0 +1,38 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.TxHandler = void 0;
|
4
|
+
const umi_1 = require("@metaplex-foundation/umi");
|
5
|
+
const utils_1 = require("../../utils");
|
6
|
+
const constants_1 = require("../../constants");
|
7
|
+
const umi_signer_wallet_adapters_1 = require("@metaplex-foundation/umi-signer-wallet-adapters");
|
8
|
+
class TxHandler {
|
9
|
+
constructor(props) {
|
10
|
+
this.showLogs = false;
|
11
|
+
this.programId = constants_1.SOLAUTO_PROD_PROGRAM;
|
12
|
+
this.otherSigners = [];
|
13
|
+
if (props.programId !== undefined) {
|
14
|
+
this.programId = props.programId;
|
15
|
+
}
|
16
|
+
this.rpcUrl = props.rpcUrl;
|
17
|
+
const [connection, umi] = (0, utils_1.getSolanaRpcConnection)(this.rpcUrl, this.programId, props.wsEndpoint);
|
18
|
+
this.connection = connection;
|
19
|
+
this.umi = umi;
|
20
|
+
if (!props.signer && !props.wallet) {
|
21
|
+
throw new Error("Signer or wallet must be provided");
|
22
|
+
}
|
23
|
+
this.umi = this.umi.use(props.signer
|
24
|
+
? (0, umi_1.signerIdentity)(props.signer, true)
|
25
|
+
: (0, umi_signer_wallet_adapters_1.walletAdapterIdentity)(props.wallet, true));
|
26
|
+
this.signer = this.umi.identity;
|
27
|
+
if (props.showLogs !== undefined) {
|
28
|
+
this.showLogs = props.showLogs;
|
29
|
+
}
|
30
|
+
if (!globalThis.SHOW_LOGS && this.showLogs) {
|
31
|
+
globalThis.SHOW_LOGS = Boolean(this.showLogs);
|
32
|
+
}
|
33
|
+
}
|
34
|
+
log(...args) {
|
35
|
+
(0, utils_1.consoleLog)(...args);
|
36
|
+
}
|
37
|
+
}
|
38
|
+
exports.TxHandler = TxHandler;
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/services/swap/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAC"}
|
@@ -14,5 +14,4 @@ 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("./
|
18
|
-
__exportStar(require("./rebalanceUtils"), exports);
|
17
|
+
__exportStar(require("./jupSwapManager"), exports);
|
@@ -0,0 +1,37 @@
|
|
1
|
+
import { Signer, TransactionBuilder } from "@metaplex-foundation/umi";
|
2
|
+
import { QuoteResponse } from "@jup-ag/api";
|
3
|
+
import { PublicKey } from "@solana/web3.js";
|
4
|
+
import { TransactionItemInputs } from "../../types";
|
5
|
+
export interface SwapInput {
|
6
|
+
inputMint: PublicKey;
|
7
|
+
outputMint: PublicKey;
|
8
|
+
amount: bigint;
|
9
|
+
exactIn?: boolean;
|
10
|
+
exactOut?: boolean;
|
11
|
+
}
|
12
|
+
export interface SwapParams extends SwapInput {
|
13
|
+
destinationWallet: PublicKey;
|
14
|
+
slippageIncFactor?: number;
|
15
|
+
wrapAndUnwrapSol?: boolean;
|
16
|
+
}
|
17
|
+
export interface JupSwapTransactionData {
|
18
|
+
jupQuote: QuoteResponse;
|
19
|
+
setupInstructions: TransactionBuilder;
|
20
|
+
swapIx: TransactionBuilder;
|
21
|
+
cleanupIx: TransactionBuilder;
|
22
|
+
lookupTableAddresses: string[];
|
23
|
+
}
|
24
|
+
export declare class JupSwapManager {
|
25
|
+
private signer;
|
26
|
+
jupApi: import("@jup-ag/api").DefaultApi;
|
27
|
+
jupQuote: QuoteResponse | undefined;
|
28
|
+
constructor(signer: Signer);
|
29
|
+
getQuote(data: SwapInput): Promise<QuoteResponse>;
|
30
|
+
private getJupInstructions;
|
31
|
+
priceImpactBps(): number;
|
32
|
+
private adaptSlippageToPriceImpact;
|
33
|
+
private addInAmountSlippagePadding;
|
34
|
+
getJupSwapTxData(data: SwapParams): Promise<JupSwapTransactionData>;
|
35
|
+
getSwapTx(data: SwapParams): Promise<TransactionItemInputs>;
|
36
|
+
}
|
37
|
+
//# sourceMappingURL=jupSwapManager.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"jupSwapManager.d.ts","sourceRoot":"","sources":["../../../src/services/swap/jupSwapManager.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,MAAM,EACN,kBAAkB,EAEnB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAEL,aAAa,EAEd,MAAM,aAAa,CAAC;AAIrB,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAM5C,OAAO,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AAEpD,MAAM,WAAW,SAAS;IACxB,SAAS,EAAE,SAAS,CAAC;IACrB,UAAU,EAAE,SAAS,CAAC;IACtB,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB;AAED,MAAM,WAAW,UAAW,SAAQ,SAAS;IAC3C,iBAAiB,EAAE,SAAS,CAAC;IAC7B,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,gBAAgB,CAAC,EAAE,OAAO,CAAC;CAC5B;AAED,MAAM,WAAW,sBAAsB;IACrC,QAAQ,EAAE,aAAa,CAAC;IACxB,iBAAiB,EAAE,kBAAkB,CAAC;IACtC,MAAM,EAAE,kBAAkB,CAAC;IAC3B,SAAS,EAAE,kBAAkB,CAAC;IAC9B,oBAAoB,EAAE,MAAM,EAAE,CAAC;CAChC;AAED,qBAAa,cAAc;IAKb,OAAO,CAAC,MAAM;IAJ1B,MAAM,mCAA4B;IAE3B,QAAQ,EAAE,aAAa,GAAG,SAAS,CAAa;gBAEnC,MAAM,EAAE,MAAM;IAErB,QAAQ,CAAC,IAAI,EAAE,SAAS,GAAG,OAAO,CAAC,aAAa,CAAC;YAoBhD,kBAAkB;IAqChC,cAAc;IAId,OAAO,CAAC,0BAA0B;IAQlC,OAAO,CAAC,0BAA0B;IAc5B,gBAAgB,CAAC,IAAI,EAAE,UAAU,GAAG,OAAO,CAAC,sBAAsB,CAAC;IA2CnE,SAAS,CAAC,IAAI,EAAE,UAAU,GAAG,OAAO,CAAC,qBAAqB,CAAC;CAYlE"}
|
@@ -0,0 +1,108 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.JupSwapManager = void 0;
|
4
|
+
const umi_1 = require("@metaplex-foundation/umi");
|
5
|
+
const api_1 = require("@jup-ag/api");
|
6
|
+
const solanaUtils_1 = require("../../utils/solanaUtils");
|
7
|
+
const numberUtils_1 = require("../../utils/numberUtils");
|
8
|
+
const accountUtils_1 = require("../../utils/accountUtils");
|
9
|
+
const utils_1 = require("../../utils");
|
10
|
+
class JupSwapManager {
|
11
|
+
constructor(signer) {
|
12
|
+
this.signer = signer;
|
13
|
+
this.jupApi = (0, api_1.createJupiterApiClient)();
|
14
|
+
this.jupQuote = undefined;
|
15
|
+
}
|
16
|
+
async getQuote(data) {
|
17
|
+
return await (0, utils_1.retryWithExponentialBackoff)(async (attemptNum) => await this.jupApi.quoteGet({
|
18
|
+
amount: Number(data.amount),
|
19
|
+
inputMint: data.inputMint.toString(),
|
20
|
+
outputMint: data.outputMint.toString(),
|
21
|
+
swapMode: data.exactOut
|
22
|
+
? "ExactOut"
|
23
|
+
: data.exactIn
|
24
|
+
? "ExactIn"
|
25
|
+
: undefined,
|
26
|
+
slippageBps: 10,
|
27
|
+
maxAccounts: !data.exactOut ? 15 + attemptNum * 5 : undefined,
|
28
|
+
}), 3, 200);
|
29
|
+
}
|
30
|
+
async getJupInstructions(data) {
|
31
|
+
if (!this.jupQuote) {
|
32
|
+
throw new Error("Fetch a quote first before getting Jupiter instructions");
|
33
|
+
}
|
34
|
+
const instructions = await (0, utils_1.retryWithExponentialBackoff)(async () => {
|
35
|
+
const res = await this.jupApi.swapInstructionsPost({
|
36
|
+
swapRequest: {
|
37
|
+
userPublicKey: this.signer.publicKey.toString(),
|
38
|
+
quoteResponse: this.jupQuote,
|
39
|
+
wrapAndUnwrapSol: data.wrapAndUnwrapSol ?? false,
|
40
|
+
useTokenLedger: !data.exactOut && !data.exactIn,
|
41
|
+
destinationTokenAccount: (0, accountUtils_1.getTokenAccount)(data.destinationWallet, data.outputMint).toString(),
|
42
|
+
},
|
43
|
+
});
|
44
|
+
if (!res) {
|
45
|
+
throw new Error("No instructions retrieved");
|
46
|
+
}
|
47
|
+
return res;
|
48
|
+
}, 4, 200);
|
49
|
+
if (!instructions.swapInstruction) {
|
50
|
+
throw new Error("No swap instruction was returned by Jupiter");
|
51
|
+
}
|
52
|
+
return instructions;
|
53
|
+
}
|
54
|
+
priceImpactBps() {
|
55
|
+
return Math.round((0, numberUtils_1.toBps)(parseFloat(this.jupQuote.priceImpactPct))) + 1;
|
56
|
+
}
|
57
|
+
adaptSlippageToPriceImpact(slippageIncFactor) {
|
58
|
+
const finalPriceSlippageBps = Math.round(Math.max(20, this.jupQuote.slippageBps, this.priceImpactBps()) *
|
59
|
+
(1 + slippageIncFactor));
|
60
|
+
this.jupQuote.slippageBps = finalPriceSlippageBps;
|
61
|
+
}
|
62
|
+
addInAmountSlippagePadding() {
|
63
|
+
(0, utils_1.consoleLog)("Raw inAmount:", this.jupQuote.inAmount);
|
64
|
+
const inc = Math.max((0, numberUtils_1.fromBps)(this.priceImpactBps()) * 1.1, (0, numberUtils_1.fromBps)(this.jupQuote.slippageBps) * 0.1);
|
65
|
+
(0, utils_1.consoleLog)("Inc:", inc);
|
66
|
+
this.jupQuote.inAmount = Math.round(parseInt(this.jupQuote.inAmount) +
|
67
|
+
parseInt(this.jupQuote.inAmount) * inc).toString();
|
68
|
+
(0, utils_1.consoleLog)("Increased inAmount:", this.jupQuote.inAmount);
|
69
|
+
}
|
70
|
+
async getJupSwapTxData(data) {
|
71
|
+
if (!this.jupQuote) {
|
72
|
+
this.jupQuote = await this.getQuote(data);
|
73
|
+
}
|
74
|
+
if (data.slippageIncFactor) {
|
75
|
+
this.adaptSlippageToPriceImpact(data.slippageIncFactor);
|
76
|
+
}
|
77
|
+
(0, utils_1.consoleLog)("Quote:", this.jupQuote);
|
78
|
+
const instructions = await this.getJupInstructions(data);
|
79
|
+
if (data.exactOut) {
|
80
|
+
this.addInAmountSlippagePadding();
|
81
|
+
}
|
82
|
+
return {
|
83
|
+
jupQuote: this.jupQuote,
|
84
|
+
lookupTableAddresses: instructions.addressLookupTableAddresses,
|
85
|
+
setupInstructions: (0, umi_1.transactionBuilder)((instructions.setupInstructions ?? []).map((ix) => (0, solanaUtils_1.getWrappedInstruction)(this.signer, (0, utils_1.jupIxToSolanaIx)(ix)))),
|
86
|
+
swapIx: (0, umi_1.transactionBuilder)([
|
87
|
+
(0, solanaUtils_1.getWrappedInstruction)(this.signer, (0, utils_1.jupIxToSolanaIx)(instructions.swapInstruction)),
|
88
|
+
]),
|
89
|
+
cleanupIx: (0, umi_1.transactionBuilder)(instructions.cleanupInstruction
|
90
|
+
? [
|
91
|
+
(0, solanaUtils_1.getWrappedInstruction)(this.signer, (0, utils_1.jupIxToSolanaIx)(instructions.cleanupInstruction)),
|
92
|
+
]
|
93
|
+
: []),
|
94
|
+
};
|
95
|
+
}
|
96
|
+
async getSwapTx(data) {
|
97
|
+
const swapData = await this.getJupSwapTxData(data);
|
98
|
+
return {
|
99
|
+
tx: (0, umi_1.transactionBuilder)().add([
|
100
|
+
swapData.setupInstructions,
|
101
|
+
swapData.swapIx,
|
102
|
+
swapData.cleanupIx,
|
103
|
+
]),
|
104
|
+
lookupTableAddresses: swapData.lookupTableAddresses,
|
105
|
+
};
|
106
|
+
}
|
107
|
+
}
|
108
|
+
exports.JupSwapManager = JupSwapManager;
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/services/transactions/index.ts"],"names":[],"mappings":"AAAA,cAAc,uBAAuB,CAAC;AACtC,cAAc,oBAAoB,CAAC"}
|
@@ -1,12 +1,10 @@
|
|
1
1
|
import { TransactionBuilder, Umi } from "@metaplex-foundation/umi";
|
2
2
|
import { PublicKey } from "@solana/web3.js";
|
3
|
-
import { SolautoClient } from "../
|
4
|
-
import { ReferralStateManager } from "
|
5
|
-
import { TransactionItemInputs } from "
|
3
|
+
import { SolautoClient } from "../solauto/solautoClient";
|
4
|
+
import { ReferralStateManager } from "..";
|
5
|
+
import { TransactionItemInputs } from "../../types";
|
6
6
|
export declare function rebalanceChoresBefore(client: SolautoClient, tx: TransactionBuilder, accountsGettingCreated: string[]): Promise<TransactionBuilder>;
|
7
7
|
export declare function getTransactionChores(client: SolautoClient, tx: TransactionBuilder): Promise<[TransactionBuilder, TransactionBuilder]>;
|
8
|
-
export declare function requiresRefreshBeforeRebalance(client: SolautoClient): Promise<boolean>;
|
9
|
-
export declare function buildSolautoRebalanceTransaction(client: SolautoClient, targetLiqUtilizationRateBps?: number, attemptNum?: number): Promise<TransactionItemInputs | undefined>;
|
10
8
|
export declare function convertReferralFeesToDestination(referralManager: ReferralStateManager, tokenAccount: PublicKey, destinationMint: PublicKey): Promise<TransactionItemInputs | undefined>;
|
11
9
|
export declare function getErrorInfo(umi: Umi, txs: TransactionBuilder[], error: Error, simulationSuccessful?: boolean): {
|
12
10
|
errorName: string | undefined;
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"transactionUtils.d.ts","sourceRoot":"","sources":["../../../src/services/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;AAsBzD,OAAO,EAAkB,oBAAoB,EAAE,MAAM,IAAI,CAAC;AAM1D,OAAO,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AAyLpD,wBAAsB,qBAAqB,CACzC,MAAM,EAAE,aAAa,EACrB,EAAE,EAAE,kBAAkB,EACtB,sBAAsB,EAAE,MAAM,EAAE,GAC/B,OAAO,CAAC,kBAAkB,CAAC,CAyF7B;AA6LD,wBAAsB,oBAAoB,CACxC,MAAM,EAAE,aAAa,EACrB,EAAE,EAAE,kBAAkB,GACrB,OAAO,CAAC,CAAC,kBAAkB,EAAE,kBAAkB,CAAC,CAAC,CA0BnD;AAED,wBAAsB,gCAAgC,CACpD,eAAe,EAAE,oBAAoB,EACrC,YAAY,EAAE,SAAS,EACvB,eAAe,EAAE,SAAS,GACzB,OAAO,CAAC,qBAAqB,GAAG,SAAS,CAAC,CAuC5C;AAED,wBAAgB,YAAY,CAC1B,GAAG,EAAE,GAAG,EACR,GAAG,EAAE,kBAAkB,EAAE,EACzB,KAAK,EAAE,KAAK,EACZ,oBAAoB,CAAC,EAAE,OAAO;;;;EA6G/B"}
|