@haven-fi/solauto-sdk 1.0.588 → 1.0.590
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/services/rebalance/rebalanceSwapManager.js +11 -11
- package/dist/services/rebalance/rebalanceTxBuilder.js +12 -12
- package/dist/services/solauto/solautoClient.d.ts +1 -1
- package/dist/services/solauto/solautoClient.d.ts.map +1 -1
- package/dist/services/solauto/solautoClient.js +33 -34
- package/dist/services/solauto/solautoMarginfiClient.js +17 -17
- package/dist/services/transactions/transactionUtils.js +9 -9
- package/dist/solautoPosition/solautoPositionEx.d.ts +1 -1
- package/dist/solautoPosition/solautoPositionEx.d.ts.map +1 -1
- package/dist/solautoPosition/solautoPositionEx.js +3 -1
- package/package.json +1 -1
- package/src/services/rebalance/rebalanceSwapManager.ts +12 -12
- package/src/services/rebalance/rebalanceTxBuilder.ts +14 -14
- package/src/services/solauto/solautoClient.ts +37 -46
- package/src/services/solauto/solautoMarginfiClient.ts +17 -17
- package/src/services/transactions/transactionUtils.ts +9 -9
- package/src/solautoPosition/solautoPositionEx.ts +4 -2
- package/tests/transactions/shared.ts +1 -1
- package/tests/unit/rebalanceCalculations.ts +4 -4
@@ -20,12 +20,12 @@ class RebalanceSwapManager {
|
|
20
20
|
return Math.abs(this.values.debtAdjustmentUsd);
|
21
21
|
}
|
22
22
|
postRebalanceLiqUtilizationRateBps(swapOutputAmount) {
|
23
|
-
let supplyUsd = this.client.
|
23
|
+
let supplyUsd = this.client.pos.supplyUsd();
|
24
24
|
// TODO: add token balance change
|
25
|
-
let debtUsd = this.client.
|
25
|
+
let debtUsd = this.client.pos.debtUsd();
|
26
26
|
const outputToken = (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(this.isBoost()
|
27
|
-
? this.client.
|
28
|
-
: this.client.
|
27
|
+
? this.client.pos.state().supply.mint
|
28
|
+
: this.client.pos.state().debt.mint);
|
29
29
|
const swapOutputUsd = swapOutputAmount
|
30
30
|
? (0, utils_1.fromBaseUnit)(swapOutputAmount, (0, utils_1.tokenInfo)(outputToken).decimals) *
|
31
31
|
((0, utils_1.safeGetPrice)(outputToken) ?? 0)
|
@@ -36,7 +36,7 @@ class RebalanceSwapManager {
|
|
36
36
|
debtUsd = this.isBoost()
|
37
37
|
? debtUsd + this.usdToSwap()
|
38
38
|
: debtUsd - swapOutputUsd;
|
39
|
-
return (0, utils_1.getLiqUtilzationRateBps)(supplyUsd, debtUsd, this.client.
|
39
|
+
return (0, utils_1.getLiqUtilzationRateBps)(supplyUsd, debtUsd, this.client.pos.state().liqThresholdBps ?? 0);
|
40
40
|
}
|
41
41
|
async findSufficientQuote(swapInput, criteria) {
|
42
42
|
let swapQuote;
|
@@ -63,11 +63,11 @@ class RebalanceSwapManager {
|
|
63
63
|
}
|
64
64
|
swapDetails() {
|
65
65
|
const input = this.isBoost()
|
66
|
-
? this.client.
|
67
|
-
: this.client.
|
66
|
+
? this.client.pos.state().debt
|
67
|
+
: this.client.pos.state().supply;
|
68
68
|
const output = this.isBoost()
|
69
|
-
? this.client.
|
70
|
-
: this.client.
|
69
|
+
? this.client.pos.state().supply
|
70
|
+
: this.client.pos.state().debt;
|
71
71
|
let inputAmount = (0, utils_1.toBaseUnit)(this.usdToSwap() / (0, utils_1.safeGetPrice)(input.mint), input.decimals);
|
72
72
|
return {
|
73
73
|
input,
|
@@ -112,7 +112,7 @@ class RebalanceSwapManager {
|
|
112
112
|
this.swapQuote = await this.findSufficientQuote(swapInput, {
|
113
113
|
minOutputAmount: rebalanceToZero ? outputAmount : undefined,
|
114
114
|
maxLiqUtilizationRateBps: this.values.repayingCloseToMaxLtv
|
115
|
-
? (0, utils_1.maxRepayToBps)(this.client.
|
115
|
+
? (0, utils_1.maxRepayToBps)(this.client.pos.state().maxLtvBps ?? 0, this.client.pos.state().liqThresholdBps ?? 0) - 15
|
116
116
|
: undefined,
|
117
117
|
});
|
118
118
|
}
|
@@ -127,7 +127,7 @@ class RebalanceSwapManager {
|
|
127
127
|
...swapInput,
|
128
128
|
destinationWallet: flashLoanRepayFromDebt
|
129
129
|
? (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(this.client.signer.publicKey)
|
130
|
-
: this.client.
|
130
|
+
: this.client.pos.publicKey,
|
131
131
|
slippageIncFactor: 0.2 + attemptNum * 0.25,
|
132
132
|
};
|
133
133
|
}
|
@@ -15,12 +15,12 @@ class RebalanceTxBuilder {
|
|
15
15
|
this.targetLiqUtilizationRateBps = targetLiqUtilizationRateBps;
|
16
16
|
}
|
17
17
|
async shouldProceedWithRebalance() {
|
18
|
-
return (this.client.
|
18
|
+
return (this.client.pos.supplyUsd() > 0 &&
|
19
19
|
(this.targetLiqUtilizationRateBps !== undefined ||
|
20
|
-
this.client.
|
20
|
+
this.client.pos.eligibleForRebalance()));
|
21
21
|
}
|
22
22
|
getRebalanceValues(flFee) {
|
23
|
-
return (0, rebalanceValues_1.getRebalanceValues)(this.client.
|
23
|
+
return (0, rebalanceValues_1.getRebalanceValues)(this.client.pos, new solautoFees_1.SolautoFeesBps(this.client.isReferred(), this.targetLiqUtilizationRateBps, this.client.pos.netWorthUsd()), flFee ?? 0, this.targetLiqUtilizationRateBps);
|
24
24
|
}
|
25
25
|
getFlLiquiditySource(supplyLiquidityAvailable, debtLiquidityAvailable) {
|
26
26
|
const debtAdjustmentUsd = Math.abs(this.values.debtAdjustmentUsd);
|
@@ -30,8 +30,8 @@ class RebalanceTxBuilder {
|
|
30
30
|
((0, utils_1.safeGetPrice)(tokenMint) ?? 0) *
|
31
31
|
0.95);
|
32
32
|
};
|
33
|
-
const insufficientSupplyLiquidity = insufficientLiquidity(debtAdjustmentUsd, supplyLiquidityAvailable, this.client.
|
34
|
-
const insufficientDebtLiquidity = insufficientLiquidity(debtAdjustmentUsd, debtLiquidityAvailable, this.client.
|
33
|
+
const insufficientSupplyLiquidity = insufficientLiquidity(debtAdjustmentUsd, supplyLiquidityAvailable, this.client.pos.supplyMint());
|
34
|
+
const insufficientDebtLiquidity = insufficientLiquidity(debtAdjustmentUsd, debtLiquidityAvailable, this.client.pos.debtMint());
|
35
35
|
let useDebtLiquidity = this.values.rebalanceDirection === generated_1.RebalanceDirection.Boost ||
|
36
36
|
insufficientSupplyLiquidity;
|
37
37
|
if (useDebtLiquidity) {
|
@@ -42,7 +42,7 @@ class RebalanceTxBuilder {
|
|
42
42
|
}
|
43
43
|
}
|
44
44
|
async flashLoanRequirements(attemptNum) {
|
45
|
-
const maxLtvRateBps = (0, utils_1.getMaxLiqUtilizationRateBps)(this.client.
|
45
|
+
const maxLtvRateBps = (0, utils_1.getMaxLiqUtilizationRateBps)(this.client.pos.state().maxLtvBps, this.client.pos.state().liqThresholdBps, 0.02);
|
46
46
|
if (this.values.intermediaryLiqUtilizationRateBps < maxLtvRateBps) {
|
47
47
|
return undefined;
|
48
48
|
}
|
@@ -75,10 +75,10 @@ class RebalanceTxBuilder {
|
|
75
75
|
const useDebtLiquidity = this.flRequirements.liquiditySource === generated_1.TokenType.Debt;
|
76
76
|
let flashLoanToken = undefined;
|
77
77
|
if (boosting || useDebtLiquidity) {
|
78
|
-
flashLoanToken = this.client.
|
78
|
+
flashLoanToken = this.client.pos.state().debt;
|
79
79
|
}
|
80
80
|
else {
|
81
|
-
flashLoanToken = this.client.
|
81
|
+
flashLoanToken = this.client.pos.state().supply;
|
82
82
|
}
|
83
83
|
return {
|
84
84
|
...this.flRequirements,
|
@@ -129,11 +129,11 @@ class RebalanceTxBuilder {
|
|
129
129
|
if (this.client.selfManaged ||
|
130
130
|
this.client.contextUpdates.supplyAdjustment > BigInt(0) ||
|
131
131
|
this.client.contextUpdates.debtAdjustment > BigInt(0) ||
|
132
|
-
!this.client.
|
132
|
+
!this.client.pos.exists()) {
|
133
133
|
return false;
|
134
134
|
}
|
135
135
|
// Rebalance ix will already refresh internally if position is self managed
|
136
|
-
const utilizationRateDiff = Math.abs(await this.client.
|
136
|
+
const utilizationRateDiff = Math.abs(await this.client.pos.utilizationRateBpsDrift());
|
137
137
|
(0, utils_1.consoleLog)("Liq utilization rate diff:", utilizationRateDiff);
|
138
138
|
if (utilizationRateDiff >= 10) {
|
139
139
|
(0, utils_1.consoleLog)("Refreshing before rebalance");
|
@@ -169,7 +169,7 @@ class RebalanceTxBuilder {
|
|
169
169
|
const addFirstRebalance = (0, utils_1.hasFirstRebalance)(this.rebalanceType);
|
170
170
|
const addLastRebalance = (0, utils_1.hasLastRebalance)(this.rebalanceType);
|
171
171
|
const flashBorrowDest = exactOut
|
172
|
-
? (0, utils_1.getTokenAccount)(this.client.
|
172
|
+
? (0, utils_1.getTokenAccount)(this.client.pos.publicKey, new web3_js_1.PublicKey(swapQuote.outputMint))
|
173
173
|
: (0, utils_1.getTokenAccount)((0, umi_web3js_adapters_1.toWeb3JsPublicKey)(this.client.signer.publicKey), new web3_js_1.PublicKey(swapQuote.inputMint));
|
174
174
|
tx = tx.add([
|
175
175
|
setupInstructions,
|
@@ -186,7 +186,7 @@ class RebalanceTxBuilder {
|
|
186
186
|
};
|
187
187
|
}
|
188
188
|
async buildRebalanceTx(attemptNum) {
|
189
|
-
await this.client.
|
189
|
+
await this.client.pos.refreshPositionState();
|
190
190
|
if (!this.shouldProceedWithRebalance()) {
|
191
191
|
this.client.log("Not eligible for a rebalance");
|
192
192
|
return undefined;
|
@@ -20,7 +20,7 @@ export declare abstract class SolautoClient extends ReferralStateManager {
|
|
20
20
|
authority: PublicKey;
|
21
21
|
positionId: number;
|
22
22
|
selfManaged: boolean;
|
23
|
-
|
23
|
+
pos: SolautoPositionEx;
|
24
24
|
positionSupplyTa: PublicKey;
|
25
25
|
signerSupplyTa: PublicKey;
|
26
26
|
positionDebtTa: PublicKey;
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"solautoClient.d.ts","sourceRoot":"","sources":["../../../src/services/solauto/solautoClient.ts"],"names":[],"mappings":"AAAA,OAAO,gCAAgC,CAAC;AACxC,OAAO,EAEL,SAAS,EAGV,MAAM,iBAAiB,CAAC;AACzB,OAAO,EACL,kBAAkB,EAMnB,MAAM,0BAA0B,CAAC;AAElC,OAAO,EACL,kBAAkB,EAClB,eAAe,EACf,aAAa,EACb,iBAAiB,EACjB,gCAAgC,EAEhC,sBAAsB,EAGvB,MAAM,iBAAiB,CAAC;AAWzB,OAAO,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAC1D,OAAO,EACL,oBAAoB,EACpB,wBAAwB,EACzB,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAEL,iBAAiB,EAClB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAC/C,OAAO,EAAE,oBAAoB,EAAE,MAAM,oCAAoC,CAAC;AAE1E,MAAM,WAAW,iBAAkB,SAAQ,wBAAwB;IACjE,GAAG,CAAC,EAAE,OAAO,CAAC;IACd,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,UAAU,CAAC,EAAE,SAAS,CAAC;IACvB,QAAQ,CAAC,EAAE,SAAS,CAAC;IACrB,WAAW,CAAC,EAAE,SAAS,CAAC;IACxB,aAAa,CAAC,EAAE,SAAS,CAAC;CAC3B;AAED,8BAAsB,aAAc,SAAQ,oBAAoB;IACvD,eAAe,EAAG,eAAe,CAAC;IAElC,SAAS,EAAG,SAAS,CAAC;IAEtB,UAAU,EAAG,MAAM,CAAC;IACpB,WAAW,EAAG,OAAO,CAAC;IACtB,
|
1
|
+
{"version":3,"file":"solautoClient.d.ts","sourceRoot":"","sources":["../../../src/services/solauto/solautoClient.ts"],"names":[],"mappings":"AAAA,OAAO,gCAAgC,CAAC;AACxC,OAAO,EAEL,SAAS,EAGV,MAAM,iBAAiB,CAAC;AACzB,OAAO,EACL,kBAAkB,EAMnB,MAAM,0BAA0B,CAAC;AAElC,OAAO,EACL,kBAAkB,EAClB,eAAe,EACf,aAAa,EACb,iBAAiB,EACjB,gCAAgC,EAEhC,sBAAsB,EAGvB,MAAM,iBAAiB,CAAC;AAWzB,OAAO,EAAE,cAAc,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,OAAO;IAkB1C,mBAAmB,IAAI,MAAM,EAAE;IAS/B,gBAAgB,IAAI,SAAS,EAAE;IAmBzB,iCAAiC,IAAI,OAAO,CAAC,SAAS,EAAE,CAAC;IAUzD,iBAAiB,IAAI,OAAO,CAC9B;QACE,EAAE,EAAE,kBAAkB,CAAC;QACvB,GAAG,EAAE,OAAO,CAAC;QACb,aAAa,EAAE,SAAS,EAAE,CAAC;KAC5B,GACD,SAAS,CACZ;IA2DK,cAAc,IAAI,OAAO,CAAC;QAC9B,aAAa,EAAE,MAAM,CAAC;QACtB,WAAW,EAAE,MAAM,CAAC;KACrB,CAAC;IA8CF,cAAc,CACZ,QAAQ,CAAC,EAAE,gCAAgC,EAC3C,GAAG,CAAC,EAAE,kBAAkB,GACvB,kBAAkB;IA0BrB,gBAAgB,CAAC,IAAI,EAAE,sBAAsB,GAAG,kBAAkB;IAsDlE,QAAQ,CAAC,eAAe,IAAI,kBAAkB;IAE9C,WAAW,IAAI,kBAAkB;IAgCjC,QAAQ,CAAC,SAAS,IAAI,kBAAkB;IAExC,qBAAqB,CAAC,IAAI,EAAE,iBAAiB,GAAG,kBAAkB;IAsFlE,QAAQ,CAAC,WAAW,CAClB,aAAa,EAAE,aAAa,EAC5B,IAAI,EAAE,gBAAgB,GACrB,kBAAkB;CACtB"}
|
@@ -28,55 +28,54 @@ class SolautoClient extends referralStateManager_1.ReferralStateManager {
|
|
28
28
|
throw new Error("Self managed position is missing arguments");
|
29
29
|
}
|
30
30
|
const positionPk = (0, accountUtils_1.getSolautoPositionAccount)(this.authority, this.positionId, this.programId);
|
31
|
-
this.
|
31
|
+
this.pos = await (0, solautoPosition_1.getOrCreatePositionEx)(this.umi, positionPk, {
|
32
32
|
supplyMint: args.supplyMint,
|
33
33
|
debtMint: args.debtMint,
|
34
34
|
lpUserAccount: args.lpUserAccount,
|
35
35
|
lendingPlatform: this.lendingPlatform,
|
36
36
|
}, this.contextUpdates);
|
37
|
-
this.positionSupplyTa = (0, accountUtils_1.getTokenAccount)(this.
|
38
|
-
this.signerSupplyTa = (0, accountUtils_1.getTokenAccount)((0, umi_web3js_adapters_1.toWeb3JsPublicKey)(this.signer.publicKey), this.
|
39
|
-
this.positionDebtTa = (0, accountUtils_1.getTokenAccount)(this.
|
40
|
-
this.signerDebtTa = (0, accountUtils_1.getTokenAccount)((0, umi_web3js_adapters_1.toWeb3JsPublicKey)(this.signer.publicKey), this.
|
41
|
-
this.solautoFeesSupplyTa = (0, accountUtils_1.getTokenAccount)(generalAccounts_1.SOLAUTO_FEES_WALLET, this.
|
42
|
-
this.solautoFeesDebtTa = (0, accountUtils_1.getTokenAccount)(generalAccounts_1.SOLAUTO_FEES_WALLET, this.
|
37
|
+
this.positionSupplyTa = (0, accountUtils_1.getTokenAccount)(this.pos.publicKey, this.pos.supplyMint());
|
38
|
+
this.signerSupplyTa = (0, accountUtils_1.getTokenAccount)((0, umi_web3js_adapters_1.toWeb3JsPublicKey)(this.signer.publicKey), this.pos.supplyMint());
|
39
|
+
this.positionDebtTa = (0, accountUtils_1.getTokenAccount)(this.pos.publicKey, this.pos.debtMint());
|
40
|
+
this.signerDebtTa = (0, accountUtils_1.getTokenAccount)((0, umi_web3js_adapters_1.toWeb3JsPublicKey)(this.signer.publicKey), this.pos.debtMint());
|
41
|
+
this.solautoFeesSupplyTa = (0, accountUtils_1.getTokenAccount)(generalAccounts_1.SOLAUTO_FEES_WALLET, this.pos.supplyMint());
|
42
|
+
this.solautoFeesDebtTa = (0, accountUtils_1.getTokenAccount)(generalAccounts_1.SOLAUTO_FEES_WALLET, this.pos.debtMint());
|
43
43
|
this.authorityLutAddress =
|
44
44
|
this.referralStateData?.lookupTable &&
|
45
45
|
!(0, umi_web3js_adapters_1.toWeb3JsPublicKey)(this.referralStateData.lookupTable).equals(web3_js_1.PublicKey.default)
|
46
46
|
? (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(this.referralStateData.lookupTable)
|
47
47
|
: undefined;
|
48
|
-
this.flProvider = new flProviderAggregator_1.FlProviderAggregator(this.umi, this.signer, this.
|
48
|
+
this.flProvider = new flProviderAggregator_1.FlProviderAggregator(this.umi, this.signer, this.pos.supplyMint(), this.pos.debtMint());
|
49
49
|
await this.flProvider.initialize();
|
50
50
|
this.otherSigners.push(...this.flProvider.otherSigners());
|
51
|
-
this.log("Position state: ", this.
|
52
|
-
this.log("Position settings: ", this.
|
53
|
-
this.log("Position DCA: ", this.
|
51
|
+
this.log("Position state: ", this.pos.state());
|
52
|
+
this.log("Position settings: ", this.pos.settings());
|
53
|
+
this.log("Position DCA: ", this.pos.dca());
|
54
54
|
}
|
55
55
|
referredBySupplyTa() {
|
56
56
|
if (this.referredByState !== undefined) {
|
57
|
-
return (0, accountUtils_1.getTokenAccount)(this.referredByState, this.
|
57
|
+
return (0, accountUtils_1.getTokenAccount)(this.referredByState, this.pos.supplyMint());
|
58
58
|
}
|
59
59
|
return undefined;
|
60
60
|
}
|
61
61
|
referredByDebtTa() {
|
62
62
|
if (this.referredByState !== undefined) {
|
63
|
-
return (0, accountUtils_1.getTokenAccount)(this.referredByState, this.
|
63
|
+
return (0, accountUtils_1.getTokenAccount)(this.referredByState, this.pos.debtMint());
|
64
64
|
}
|
65
65
|
return undefined;
|
66
66
|
}
|
67
67
|
async resetLiveTxUpdates(success) {
|
68
68
|
this.log("Resetting context updates...");
|
69
69
|
if (success) {
|
70
|
-
if (!this.
|
71
|
-
await this.
|
70
|
+
if (!this.pos.exists()) {
|
71
|
+
await this.pos.refetchPositionData();
|
72
72
|
}
|
73
73
|
else {
|
74
74
|
if (this.contextUpdates.settings) {
|
75
|
-
this.
|
76
|
-
this.contextUpdates.settings;
|
75
|
+
this.pos.data().position.settings = this.contextUpdates.settings;
|
77
76
|
}
|
78
77
|
if (this.contextUpdates.dca) {
|
79
|
-
this.
|
78
|
+
this.pos.data().position.dca = this.contextUpdates.dca;
|
80
79
|
}
|
81
80
|
// All other live position updates can be derived by getting a fresh position state, so we don't need to do anything else form contextUpdates
|
82
81
|
}
|
@@ -100,7 +99,7 @@ class SolautoClient extends referralStateManager_1.ReferralStateManager {
|
|
100
99
|
...((0, umi_web3js_adapters_1.toWeb3JsPublicKey)(this.signer.publicKey).equals(this.authority)
|
101
100
|
? [this.signerDebtTa]
|
102
101
|
: []),
|
103
|
-
this.
|
102
|
+
this.pos.publicKey,
|
104
103
|
this.positionSupplyTa,
|
105
104
|
this.positionDebtTa,
|
106
105
|
this.referralState,
|
@@ -170,7 +169,7 @@ class SolautoClient extends referralStateManager_1.ReferralStateManager {
|
|
170
169
|
(async () => {
|
171
170
|
let data;
|
172
171
|
try {
|
173
|
-
data = await this.connection.getTokenAccountBalance((0, accountUtils_1.getTokenAccount)((0, umi_web3js_adapters_1.toWeb3JsPublicKey)(this.signer.publicKey), this.
|
172
|
+
data = await this.connection.getTokenAccountBalance((0, accountUtils_1.getTokenAccount)((0, umi_web3js_adapters_1.toWeb3JsPublicKey)(this.signer.publicKey), this.pos.supplyMint()), "confirmed");
|
174
173
|
}
|
175
174
|
catch { }
|
176
175
|
return BigInt(parseInt(data?.value.amount ?? "0"));
|
@@ -178,7 +177,7 @@ class SolautoClient extends referralStateManager_1.ReferralStateManager {
|
|
178
177
|
(async () => {
|
179
178
|
let data;
|
180
179
|
try {
|
181
|
-
const data = await this.connection.getTokenAccountBalance((0, accountUtils_1.getTokenAccount)((0, umi_web3js_adapters_1.toWeb3JsPublicKey)(this.signer.publicKey), this.
|
180
|
+
const data = await this.connection.getTokenAccountBalance((0, accountUtils_1.getTokenAccount)((0, umi_web3js_adapters_1.toWeb3JsPublicKey)(this.signer.publicKey), this.pos.debtMint()), "confirmed");
|
182
181
|
}
|
183
182
|
catch { }
|
184
183
|
return BigInt(parseInt(data?.value.amount ?? "0"));
|
@@ -219,12 +218,12 @@ class SolautoClient extends referralStateManager_1.ReferralStateManager {
|
|
219
218
|
let signerDcaTa = undefined;
|
220
219
|
if ((0, umi_1.isOption)(args.dca) && (0, umi_1.isSome)(args.dca)) {
|
221
220
|
if (args.dca.value.tokenType === generated_1.TokenType.Supply) {
|
222
|
-
dcaMint = (0, umi_1.publicKey)(this.
|
221
|
+
dcaMint = (0, umi_1.publicKey)(this.pos.supplyMint());
|
223
222
|
positionDcaTa = (0, umi_1.publicKey)(this.positionSupplyTa);
|
224
223
|
signerDcaTa = (0, umi_1.publicKey)(this.signerSupplyTa);
|
225
224
|
}
|
226
225
|
else {
|
227
|
-
dcaMint = (0, umi_1.publicKey)(this.
|
226
|
+
dcaMint = (0, umi_1.publicKey)(this.pos.debtMint());
|
228
227
|
positionDcaTa = (0, umi_1.publicKey)(this.positionDebtTa);
|
229
228
|
signerDcaTa = (0, umi_1.publicKey)(this.signerDebtTa);
|
230
229
|
}
|
@@ -254,7 +253,7 @@ class SolautoClient extends referralStateManager_1.ReferralStateManager {
|
|
254
253
|
}
|
255
254
|
return (0, generated_1.updatePosition)(this.umi, {
|
256
255
|
signer: this.signer,
|
257
|
-
solautoPosition: (0, umi_1.publicKey)(this.
|
256
|
+
solautoPosition: (0, umi_1.publicKey)(this.pos.publicKey),
|
258
257
|
dcaMint,
|
259
258
|
positionDcaTa,
|
260
259
|
signerDcaTa,
|
@@ -265,26 +264,26 @@ class SolautoClient extends referralStateManager_1.ReferralStateManager {
|
|
265
264
|
let dcaMint = undefined;
|
266
265
|
let positionDcaTa = undefined;
|
267
266
|
let signerDcaTa = undefined;
|
268
|
-
const currDca = this.
|
267
|
+
const currDca = this.pos.dca();
|
269
268
|
if (currDca.dcaInBaseUnit > 0) {
|
270
269
|
if (currDca.tokenType === generated_1.TokenType.Supply) {
|
271
|
-
dcaMint = (0, umi_1.publicKey)(this.
|
270
|
+
dcaMint = (0, umi_1.publicKey)(this.pos.supplyMint());
|
272
271
|
positionDcaTa = (0, umi_1.publicKey)(this.positionSupplyTa);
|
273
272
|
signerDcaTa = (0, umi_1.publicKey)(this.signerSupplyTa);
|
274
273
|
}
|
275
274
|
else {
|
276
|
-
dcaMint = (0, umi_1.publicKey)(this.
|
275
|
+
dcaMint = (0, umi_1.publicKey)(this.pos.debtMint());
|
277
276
|
positionDcaTa = (0, umi_1.publicKey)(this.positionDebtTa);
|
278
277
|
signerDcaTa = (0, umi_1.publicKey)(this.signerDebtTa);
|
279
278
|
}
|
280
279
|
this.contextUpdates.new({
|
281
280
|
type: "cancellingDca",
|
282
|
-
value: this.
|
281
|
+
value: this.pos.dca().tokenType,
|
283
282
|
});
|
284
283
|
}
|
285
284
|
return (0, generated_1.cancelDCA)(this.umi, {
|
286
285
|
signer: this.signer,
|
287
|
-
solautoPosition: (0, umi_1.publicKey)(this.
|
286
|
+
solautoPosition: (0, umi_1.publicKey)(this.pos.publicKey),
|
288
287
|
dcaMint,
|
289
288
|
positionDcaTa,
|
290
289
|
signerDcaTa,
|
@@ -301,7 +300,7 @@ class SolautoClient extends referralStateManager_1.ReferralStateManager {
|
|
301
300
|
tx = tx.add((0, solanaUtils_1.splTokenTransferUmiIx)(this.signer, this.signerDebtTa, this.positionDebtTa, (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(this.signer.publicKey), BigInt(args.fields[0].fields[0])));
|
302
301
|
}
|
303
302
|
else {
|
304
|
-
tx = tx.add((0, solanaUtils_1.splTokenTransferUmiIx)(this.signer, this.signerDebtTa, this.positionDebtTa, (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(this.signer.publicKey), BigInt(Math.round(Number(this.
|
303
|
+
tx = tx.add((0, solanaUtils_1.splTokenTransferUmiIx)(this.signer, this.signerDebtTa, this.positionDebtTa, (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(this.signer.publicKey), BigInt(Math.round(Number(this.pos.state().debt.amountUsed.baseUnit) * 1.01))));
|
305
304
|
}
|
306
305
|
}
|
307
306
|
}
|
@@ -321,8 +320,8 @@ class SolautoClient extends referralStateManager_1.ReferralStateManager {
|
|
321
320
|
else {
|
322
321
|
this.contextUpdates.new({
|
323
322
|
type: "supply",
|
324
|
-
value: (this.
|
325
|
-
BigInt(
|
323
|
+
value: (this.pos.state().supply.amountUsed.baseUnit ?? BigInt(0)) *
|
324
|
+
BigInt(-1),
|
326
325
|
});
|
327
326
|
}
|
328
327
|
}
|
@@ -342,8 +341,8 @@ class SolautoClient extends referralStateManager_1.ReferralStateManager {
|
|
342
341
|
else {
|
343
342
|
this.contextUpdates.new({
|
344
343
|
type: "debt",
|
345
|
-
value: (this.
|
346
|
-
BigInt(
|
344
|
+
value: (this.pos.state().debt.amountUsed.baseUnit ?? BigInt(0)) *
|
345
|
+
BigInt(-1),
|
347
346
|
});
|
348
347
|
}
|
349
348
|
}
|
@@ -18,18 +18,18 @@ class SolautoMarginfiClient extends solautoClient_1.SolautoClient {
|
|
18
18
|
}
|
19
19
|
async initialize(args) {
|
20
20
|
await super.initialize(args);
|
21
|
-
this.marginfiGroup = await this.
|
21
|
+
this.marginfiGroup = await this.pos.lendingPool();
|
22
22
|
if (this.selfManaged) {
|
23
23
|
this.marginfiAccount =
|
24
24
|
args.marginfiAccount ??
|
25
25
|
(0, umi_1.createSignerFromKeypair)(this.umi, this.umi.eddsa.generateKeypair());
|
26
26
|
}
|
27
27
|
else {
|
28
|
-
if (this.
|
29
|
-
this.marginfiAccount = this.
|
28
|
+
if (this.pos.exists()) {
|
29
|
+
this.marginfiAccount = this.pos.lpUserAccount;
|
30
30
|
}
|
31
31
|
else {
|
32
|
-
const accounts = await (0, marginfiUtils_1.getAllMarginfiAccountsByAuthority)(this.umi, this.
|
32
|
+
const accounts = await (0, marginfiUtils_1.getAllMarginfiAccountsByAuthority)(this.umi, this.pos.publicKey, this.marginfiGroup, false);
|
33
33
|
const reusableAccounts = accounts.length > 0
|
34
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
35
|
: [];
|
@@ -47,9 +47,9 @@ class SolautoMarginfiClient extends solautoClient_1.SolautoClient {
|
|
47
47
|
this.otherSigners.push(this.marginfiAccount);
|
48
48
|
}
|
49
49
|
this.marginfiSupplyAccounts =
|
50
|
-
marginfiAccounts_1.MARGINFI_ACCOUNTS[this.marginfiGroup.toString()][this.
|
50
|
+
marginfiAccounts_1.MARGINFI_ACCOUNTS[this.marginfiGroup.toString()][this.pos.supplyMint().toString()];
|
51
51
|
this.marginfiDebtAccounts =
|
52
|
-
marginfiAccounts_1.MARGINFI_ACCOUNTS[this.marginfiGroup.toString()][this.
|
52
|
+
marginfiAccounts_1.MARGINFI_ACCOUNTS[this.marginfiGroup.toString()][this.pos.debtMint().toString()];
|
53
53
|
// TODO: Don't dynamically pull oracle from bank until Marginfi sorts out their price oracle issues.
|
54
54
|
// const [supplyBank, debtBank] = await safeFetchAllBank(this.umi, [
|
55
55
|
// publicKey(this.marginfiSupplyAccounts.bank),
|
@@ -95,15 +95,15 @@ class SolautoMarginfiClient extends solautoClient_1.SolautoClient {
|
|
95
95
|
referredBySupplyTa: this.referredBySupplyTa()
|
96
96
|
? (0, umi_1.publicKey)(this.referredBySupplyTa())
|
97
97
|
: undefined,
|
98
|
-
solautoPosition: (0, umi_1.publicKey)(this.
|
98
|
+
solautoPosition: (0, umi_1.publicKey)(this.pos.publicKey),
|
99
99
|
marginfiGroup: (0, umi_1.publicKey)(this.marginfiGroup),
|
100
100
|
marginfiAccount: "publicKey" in this.marginfiAccount
|
101
101
|
? this.marginfiAccount
|
102
102
|
: (0, umi_1.publicKey)(this.marginfiAccount),
|
103
|
-
supplyMint: (0, umi_1.publicKey)(this.
|
103
|
+
supplyMint: (0, umi_1.publicKey)(this.pos.supplyMint()),
|
104
104
|
supplyBank: (0, umi_1.publicKey)(this.marginfiSupplyAccounts.bank),
|
105
105
|
positionSupplyTa: (0, umi_1.publicKey)(this.positionSupplyTa),
|
106
|
-
debtMint: (0, umi_1.publicKey)(this.
|
106
|
+
debtMint: (0, umi_1.publicKey)(this.pos.debtMint()),
|
107
107
|
debtBank: (0, umi_1.publicKey)(this.marginfiDebtAccounts.bank),
|
108
108
|
positionDebtTa: (0, umi_1.publicKey)(this.positionDebtTa),
|
109
109
|
signerDebtTa: signerDebtTa,
|
@@ -118,7 +118,7 @@ class SolautoMarginfiClient extends solautoClient_1.SolautoClient {
|
|
118
118
|
closePositionIx() {
|
119
119
|
return (0, generated_1.closePosition)(this.umi, {
|
120
120
|
signer: this.signer,
|
121
|
-
solautoPosition: (0, umi_1.publicKey)(this.
|
121
|
+
solautoPosition: (0, umi_1.publicKey)(this.pos.publicKey),
|
122
122
|
signerSupplyTa: (0, umi_1.publicKey)(this.signerSupplyTa),
|
123
123
|
positionSupplyTa: (0, umi_1.publicKey)(this.positionSupplyTa),
|
124
124
|
positionDebtTa: (0, umi_1.publicKey)(this.positionDebtTa),
|
@@ -136,7 +136,7 @@ class SolautoMarginfiClient extends solautoClient_1.SolautoClient {
|
|
136
136
|
supplyPriceOracle: (0, umi_1.publicKey)(this.supplyPriceOracle),
|
137
137
|
debtBank: (0, umi_1.publicKey)(this.marginfiDebtAccounts.bank),
|
138
138
|
debtPriceOracle: (0, umi_1.publicKey)(this.debtPriceOracle),
|
139
|
-
solautoPosition: (0, umi_1.publicKey)(this.
|
139
|
+
solautoPosition: (0, umi_1.publicKey)(this.pos.publicKey),
|
140
140
|
});
|
141
141
|
}
|
142
142
|
protocolInteractionIx(args) {
|
@@ -230,7 +230,7 @@ class SolautoMarginfiClient extends solautoClient_1.SolautoClient {
|
|
230
230
|
return (0, generated_1.marginfiProtocolInteraction)(this.umi, {
|
231
231
|
signer: this.signer,
|
232
232
|
marginfiProgram: (0, umi_1.publicKey)(marginfi_sdk_1.MARGINFI_PROGRAM_ID),
|
233
|
-
solautoPosition: (0, umi_1.publicKey)(this.
|
233
|
+
solautoPosition: (0, umi_1.publicKey)(this.pos.publicKey),
|
234
234
|
marginfiGroup: (0, umi_1.publicKey)(this.marginfiGroup),
|
235
235
|
marginfiAccount: (0, umi_1.publicKey)(this.marginfiAccountPk),
|
236
236
|
supplyBank: (0, umi_1.publicKey)(this.marginfiSupplyAccounts.bank),
|
@@ -247,8 +247,8 @@ class SolautoMarginfiClient extends solautoClient_1.SolautoClient {
|
|
247
247
|
});
|
248
248
|
}
|
249
249
|
rebalanceIx(rebalanceStep, data) {
|
250
|
-
const inputIsSupply = new web3_js_1.PublicKey(data.swapQuote.inputMint).equals(this.
|
251
|
-
const outputIsSupply = new web3_js_1.PublicKey(data.swapQuote.outputMint).equals(this.
|
250
|
+
const inputIsSupply = new web3_js_1.PublicKey(data.swapQuote.inputMint).equals(this.pos.supplyMint());
|
251
|
+
const outputIsSupply = new web3_js_1.PublicKey(data.swapQuote.outputMint).equals(this.pos.supplyMint());
|
252
252
|
const preSwapRebalance = rebalanceStep === generated_1.RebalanceStep.PreSwap;
|
253
253
|
const postSwapRebalance = rebalanceStep === generated_1.RebalanceStep.PostSwap;
|
254
254
|
const needSupplyAccounts = (inputIsSupply && preSwapRebalance) ||
|
@@ -277,7 +277,7 @@ class SolautoMarginfiClient extends solautoClient_1.SolautoClient {
|
|
277
277
|
positionAuthority: data.values.tokenBalanceChange !== undefined
|
278
278
|
? (0, umi_1.publicKey)(this.authority)
|
279
279
|
: undefined,
|
280
|
-
solautoPosition: (0, umi_1.publicKey)(this.
|
280
|
+
solautoPosition: (0, umi_1.publicKey)(this.pos.publicKey),
|
281
281
|
marginfiGroup: (0, umi_1.publicKey)(this.marginfiGroup),
|
282
282
|
marginfiAccount: (0, umi_1.publicKey)(this.marginfiAccountPk),
|
283
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))),
|
@@ -285,7 +285,7 @@ class SolautoMarginfiClient extends solautoClient_1.SolautoClient {
|
|
285
285
|
supplyPriceOracle: (0, umi_1.publicKey)(this.supplyPriceOracle),
|
286
286
|
positionSupplyTa: (0, umi_1.publicKey)(this.positionSupplyTa),
|
287
287
|
authoritySupplyTa: addAuthorityTas
|
288
|
-
? (0, umi_1.publicKey)((0, accountUtils_1.getTokenAccount)(this.authority, this.
|
288
|
+
? (0, umi_1.publicKey)((0, accountUtils_1.getTokenAccount)(this.authority, this.pos.supplyMint()))
|
289
289
|
: undefined,
|
290
290
|
vaultSupplyTa: needSupplyAccounts
|
291
291
|
? (0, umi_1.publicKey)(this.marginfiSupplyAccounts.liquidityVault)
|
@@ -297,7 +297,7 @@ class SolautoMarginfiClient extends solautoClient_1.SolautoClient {
|
|
297
297
|
debtPriceOracle: (0, umi_1.publicKey)(this.debtPriceOracle),
|
298
298
|
positionDebtTa: (0, umi_1.publicKey)(this.positionDebtTa),
|
299
299
|
authorityDebtTa: addAuthorityTas
|
300
|
-
? (0, umi_1.publicKey)((0, accountUtils_1.getTokenAccount)(this.authority, this.
|
300
|
+
? (0, umi_1.publicKey)((0, accountUtils_1.getTokenAccount)(this.authority, this.pos.debtMint()))
|
301
301
|
: undefined,
|
302
302
|
vaultDebtTa: needDebtAccounts
|
303
303
|
? (0, umi_1.publicKey)(this.marginfiDebtAccounts.liquidityVault)
|
@@ -19,8 +19,8 @@ const jupiter_sdk_1 = require("../../jupiter-sdk");
|
|
19
19
|
const utils_1 = require("../../utils");
|
20
20
|
const transactions_1 = require("../../types/transactions");
|
21
21
|
function getWSolUsage(client, solautoActions, initiatingDcaIn, cancellingDcaIn) {
|
22
|
-
const supplyIsWsol = client.
|
23
|
-
const debtIsWsol = client.
|
22
|
+
const supplyIsWsol = client.pos.supplyMint().equals(spl_token_1.NATIVE_MINT);
|
23
|
+
const debtIsWsol = client.pos.debtMint().equals(spl_token_1.NATIVE_MINT);
|
24
24
|
if (!supplyIsWsol && !debtIsWsol) {
|
25
25
|
return undefined;
|
26
26
|
}
|
@@ -61,7 +61,7 @@ async function transactionChoresBefore(client, accountsGettingCreated, solautoAc
|
|
61
61
|
chores = chores.add(client.marginfiAccountInitialize(client.marginfiAccount));
|
62
62
|
}
|
63
63
|
// TODO: PF
|
64
|
-
if (!client.
|
64
|
+
if (!client.pos.exists()) {
|
65
65
|
chores = chores.add(client.openPositionIx());
|
66
66
|
}
|
67
67
|
}
|
@@ -107,8 +107,8 @@ async function transactionChoresBefore(client, accountsGettingCreated, solautoAc
|
|
107
107
|
}
|
108
108
|
if (!(0, generalUtils_1.getSolanaAccountCreated)(client.umi, tokenAccount)) {
|
109
109
|
chores = chores.add((0, solanaUtils_1.createAssociatedTokenAccountUmiIx)(client.signer, (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(client.signer.publicKey), (0, generated_1.isSolautoAction)("Withdraw", solautoAction)
|
110
|
-
? client.
|
111
|
-
: client.
|
110
|
+
? client.pos.supplyMint()
|
111
|
+
: client.pos.debtMint()));
|
112
112
|
accountsGettingCreated.push(tokenAccount.toString());
|
113
113
|
}
|
114
114
|
}
|
@@ -138,24 +138,24 @@ async function rebalanceChoresBefore(client, tx, accountsGettingCreated) {
|
|
138
138
|
let chores = (0, umi_1.transactionBuilder)();
|
139
139
|
if (checkReferralSupplyTa && !(0, generalUtils_1.rpcAccountCreated)(referredBySupplyTa)) {
|
140
140
|
client.log("Creating referred-by supply TA");
|
141
|
-
chores = chores.add((0, solanaUtils_1.createAssociatedTokenAccountUmiIx)(client.signer, client.referredByState, client.
|
141
|
+
chores = chores.add((0, solanaUtils_1.createAssociatedTokenAccountUmiIx)(client.signer, client.referredByState, client.pos.supplyMint()));
|
142
142
|
}
|
143
143
|
if (checkReferralDebtTa && !(0, generalUtils_1.rpcAccountCreated)(referredByDebtTa)) {
|
144
144
|
client.log("Creating referred-by debt TA");
|
145
|
-
chores = chores.add((0, solanaUtils_1.createAssociatedTokenAccountUmiIx)(client.signer, client.referredByState, client.
|
145
|
+
chores = chores.add((0, solanaUtils_1.createAssociatedTokenAccountUmiIx)(client.signer, client.referredByState, client.pos.debtMint()));
|
146
146
|
}
|
147
147
|
if (checkSignerSupplyTa &&
|
148
148
|
!(0, generalUtils_1.rpcAccountCreated)(signerSupplyTa) &&
|
149
149
|
!accountsGettingCreated.includes(signerSupplyTa.publicKey.toString())) {
|
150
150
|
client.log("Creating signer supply token account");
|
151
|
-
chores = chores.add((0, solanaUtils_1.createAssociatedTokenAccountUmiIx)(client.signer, (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(client.signer.publicKey), client.
|
151
|
+
chores = chores.add((0, solanaUtils_1.createAssociatedTokenAccountUmiIx)(client.signer, (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(client.signer.publicKey), client.pos.supplyMint()));
|
152
152
|
accountsGettingCreated.push(signerSupplyTa.publicKey.toString());
|
153
153
|
}
|
154
154
|
if (checkSignerDebtTa &&
|
155
155
|
!(0, generalUtils_1.rpcAccountCreated)(signerDebtTa) &&
|
156
156
|
!accountsGettingCreated.includes(signerDebtTa.publicKey.toString())) {
|
157
157
|
client.log("Creating signer debt token account");
|
158
|
-
chores = chores.add((0, solanaUtils_1.createAssociatedTokenAccountUmiIx)(client.signer, (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(client.signer.publicKey), client.
|
158
|
+
chores = chores.add((0, solanaUtils_1.createAssociatedTokenAccountUmiIx)(client.signer, (0, umi_web3js_adapters_1.toWeb3JsPublicKey)(client.signer.publicKey), client.pos.debtMint()));
|
159
159
|
accountsGettingCreated.push(signerDebtTa.publicKey.toString());
|
160
160
|
}
|
161
161
|
return chores;
|
@@ -62,7 +62,7 @@ export declare abstract class SolautoPositionEx {
|
|
62
62
|
protected canRefreshPositionState(): boolean;
|
63
63
|
abstract refreshPositionState(): Promise<void>;
|
64
64
|
utilizationRateBpsDrift(): Promise<number>;
|
65
|
-
updateWithLatestPrices(supplyPrice?: number, debtPrice?: number): Promise<
|
65
|
+
updateWithLatestPrices(supplyPrice?: number, debtPrice?: number): Promise<PositionState>;
|
66
66
|
refetchPositionData(): Promise<void>;
|
67
67
|
}
|
68
68
|
export {};
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"solautoPositionEx.d.ts","sourceRoot":"","sources":["../../src/solautoPosition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S;IAIT,OAAO;IAIP,6BAA6B;IAI7B,yBAAyB;IAIzB,QAAQ,CAAC,wBAAwB,IAAI,OAAO,CAAC,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC9D,QAAQ,CAAC,0BAA0B,IAAI,MAAM;IAC7C,QAAQ,CAAC,wBAAwB,IAAI,MAAM;IAC3C,QAAQ,CAAC,sBAAsB,IAAI,MAAM;IAEzC,0BAA0B;IA0B1B,oBAAoB,CAAC,oBAAoB,SAAI,GAAG,eAAe,GAAG,SAAS;IAqB3E,kBAAkB,IAAI,OAAO;IAS7B,SAAS,CAAC,uBAAuB;IAWjC,QAAQ,CAAC,oBAAoB,IAAI,OAAO,CAAC,IAAI,CAAC;IAExC,uBAAuB;IAiBvB,sBAAsB,CAAC,WAAW,CAAC,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,aAAa,CAAC;IAUxF,mBAAmB;CAM1B"}
|
@@ -142,7 +142,9 @@ class SolautoPositionEx {
|
|
142
142
|
return newState.liqUtilizationRateBps - oldState.liqUtilizationRateBps;
|
143
143
|
}
|
144
144
|
async updateWithLatestPrices(supplyPrice, debtPrice) {
|
145
|
-
|
145
|
+
const newState = await (0, utils_1.positionStateWithLatestPrices)(this.state(), supplyPrice, debtPrice);
|
146
|
+
this._data.state = newState;
|
147
|
+
return newState;
|
146
148
|
}
|
147
149
|
async refetchPositionData() {
|
148
150
|
this._data = await (0, generated_1.fetchSolautoPosition)(this.umi, (0, umi_web3js_adapters_1.fromWeb3JsPublicKey)(this.publicKey));
|
package/package.json
CHANGED
@@ -40,14 +40,14 @@ export class RebalanceSwapManager {
|
|
40
40
|
}
|
41
41
|
|
42
42
|
private postRebalanceLiqUtilizationRateBps(swapOutputAmount?: bigint) {
|
43
|
-
let supplyUsd = this.client.
|
43
|
+
let supplyUsd = this.client.pos.supplyUsd();
|
44
44
|
// TODO: add token balance change
|
45
|
-
let debtUsd = this.client.
|
45
|
+
let debtUsd = this.client.pos.debtUsd();
|
46
46
|
|
47
47
|
const outputToken = toWeb3JsPublicKey(
|
48
48
|
this.isBoost()
|
49
|
-
? this.client.
|
50
|
-
: this.client.
|
49
|
+
? this.client.pos.state().supply.mint
|
50
|
+
: this.client.pos.state().debt.mint
|
51
51
|
);
|
52
52
|
const swapOutputUsd = swapOutputAmount
|
53
53
|
? fromBaseUnit(swapOutputAmount, tokenInfo(outputToken).decimals) *
|
@@ -64,7 +64,7 @@ export class RebalanceSwapManager {
|
|
64
64
|
return getLiqUtilzationRateBps(
|
65
65
|
supplyUsd,
|
66
66
|
debtUsd,
|
67
|
-
this.client.
|
67
|
+
this.client.pos.state().liqThresholdBps ?? 0
|
68
68
|
);
|
69
69
|
}
|
70
70
|
|
@@ -106,11 +106,11 @@ export class RebalanceSwapManager {
|
|
106
106
|
|
107
107
|
private swapDetails() {
|
108
108
|
const input = this.isBoost()
|
109
|
-
? this.client.
|
110
|
-
: this.client.
|
109
|
+
? this.client.pos.state().debt
|
110
|
+
: this.client.pos.state().supply;
|
111
111
|
const output = this.isBoost()
|
112
|
-
? this.client.
|
113
|
-
: this.client.
|
112
|
+
? this.client.pos.state().supply
|
113
|
+
: this.client.pos.state().debt;
|
114
114
|
|
115
115
|
let inputAmount = toBaseUnit(
|
116
116
|
this.usdToSwap() / safeGetPrice(input.mint)!,
|
@@ -180,8 +180,8 @@ export class RebalanceSwapManager {
|
|
180
180
|
minOutputAmount: rebalanceToZero ? outputAmount : undefined,
|
181
181
|
maxLiqUtilizationRateBps: this.values.repayingCloseToMaxLtv
|
182
182
|
? maxRepayToBps(
|
183
|
-
this.client.
|
184
|
-
this.client.
|
183
|
+
this.client.pos.state().maxLtvBps ?? 0,
|
184
|
+
this.client.pos.state().liqThresholdBps ?? 0
|
185
185
|
) - 15
|
186
186
|
: undefined,
|
187
187
|
});
|
@@ -199,7 +199,7 @@ export class RebalanceSwapManager {
|
|
199
199
|
...swapInput,
|
200
200
|
destinationWallet: flashLoanRepayFromDebt
|
201
201
|
? toWeb3JsPublicKey(this.client.signer.publicKey)
|
202
|
-
: this.client.
|
202
|
+
: this.client.pos.publicKey,
|
203
203
|
slippageIncFactor: 0.2 + attemptNum * 0.25,
|
204
204
|
};
|
205
205
|
}
|
@@ -43,19 +43,19 @@ export class RebalanceTxBuilder {
|
|
43
43
|
|
44
44
|
private async shouldProceedWithRebalance() {
|
45
45
|
return (
|
46
|
-
this.client.
|
46
|
+
this.client.pos.supplyUsd() > 0 &&
|
47
47
|
(this.targetLiqUtilizationRateBps !== undefined ||
|
48
|
-
this.client.
|
48
|
+
this.client.pos.eligibleForRebalance())
|
49
49
|
);
|
50
50
|
}
|
51
51
|
|
52
52
|
private getRebalanceValues(flFee?: number) {
|
53
53
|
return getRebalanceValues(
|
54
|
-
this.client.
|
54
|
+
this.client.pos,
|
55
55
|
new SolautoFeesBps(
|
56
56
|
this.client.isReferred(),
|
57
57
|
this.targetLiqUtilizationRateBps,
|
58
|
-
this.client.
|
58
|
+
this.client.pos.netWorthUsd()
|
59
59
|
),
|
60
60
|
flFee ?? 0,
|
61
61
|
this.targetLiqUtilizationRateBps
|
@@ -84,12 +84,12 @@ export class RebalanceTxBuilder {
|
|
84
84
|
const insufficientSupplyLiquidity = insufficientLiquidity(
|
85
85
|
debtAdjustmentUsd,
|
86
86
|
supplyLiquidityAvailable,
|
87
|
-
this.client.
|
87
|
+
this.client.pos.supplyMint()
|
88
88
|
);
|
89
89
|
const insufficientDebtLiquidity = insufficientLiquidity(
|
90
90
|
debtAdjustmentUsd,
|
91
91
|
debtLiquidityAvailable,
|
92
|
-
this.client.
|
92
|
+
this.client.pos.debtMint()
|
93
93
|
);
|
94
94
|
|
95
95
|
let useDebtLiquidity =
|
@@ -107,8 +107,8 @@ export class RebalanceTxBuilder {
|
|
107
107
|
attemptNum: number
|
108
108
|
): Promise<FlashLoanRequirements | undefined> {
|
109
109
|
const maxLtvRateBps = getMaxLiqUtilizationRateBps(
|
110
|
-
this.client.
|
111
|
-
this.client.
|
110
|
+
this.client.pos.state().maxLtvBps,
|
111
|
+
this.client.pos.state().liqThresholdBps,
|
112
112
|
0.02
|
113
113
|
);
|
114
114
|
|
@@ -156,9 +156,9 @@ export class RebalanceTxBuilder {
|
|
156
156
|
|
157
157
|
let flashLoanToken: PositionTokenState | undefined = undefined;
|
158
158
|
if (boosting || useDebtLiquidity) {
|
159
|
-
flashLoanToken = this.client.
|
159
|
+
flashLoanToken = this.client.pos.state().debt;
|
160
160
|
} else {
|
161
|
-
flashLoanToken = this.client.
|
161
|
+
flashLoanToken = this.client.pos.state().supply;
|
162
162
|
}
|
163
163
|
|
164
164
|
return {
|
@@ -225,14 +225,14 @@ export class RebalanceTxBuilder {
|
|
225
225
|
this.client.selfManaged ||
|
226
226
|
this.client.contextUpdates.supplyAdjustment > BigInt(0) ||
|
227
227
|
this.client.contextUpdates.debtAdjustment > BigInt(0) ||
|
228
|
-
!this.client.
|
228
|
+
!this.client.pos.exists()
|
229
229
|
) {
|
230
230
|
return false;
|
231
231
|
}
|
232
232
|
// Rebalance ix will already refresh internally if position is self managed
|
233
233
|
|
234
234
|
const utilizationRateDiff = Math.abs(
|
235
|
-
await this.client.
|
235
|
+
await this.client.pos.utilizationRateBpsDrift()
|
236
236
|
);
|
237
237
|
consoleLog("Liq utilization rate diff:", utilizationRateDiff);
|
238
238
|
|
@@ -287,7 +287,7 @@ export class RebalanceTxBuilder {
|
|
287
287
|
|
288
288
|
const flashBorrowDest = exactOut
|
289
289
|
? getTokenAccount(
|
290
|
-
this.client.
|
290
|
+
this.client.pos.publicKey,
|
291
291
|
new PublicKey(swapQuote.outputMint)
|
292
292
|
)
|
293
293
|
: getTokenAccount(
|
@@ -314,7 +314,7 @@ export class RebalanceTxBuilder {
|
|
314
314
|
public async buildRebalanceTx(
|
315
315
|
attemptNum: number
|
316
316
|
): Promise<TransactionItemInputs | undefined> {
|
317
|
-
await this.client.
|
317
|
+
await this.client.pos.refreshPositionState();
|
318
318
|
|
319
319
|
if (!this.shouldProceedWithRebalance()) {
|
320
320
|
this.client.log("Not eligible for a rebalance");
|
@@ -63,7 +63,7 @@ export abstract class SolautoClient extends ReferralStateManager {
|
|
63
63
|
|
64
64
|
public positionId!: number;
|
65
65
|
public selfManaged!: boolean;
|
66
|
-
public
|
66
|
+
public pos!: SolautoPositionEx;
|
67
67
|
|
68
68
|
public positionSupplyTa!: PublicKey;
|
69
69
|
public signerSupplyTa!: PublicKey;
|
@@ -99,7 +99,7 @@ export abstract class SolautoClient extends ReferralStateManager {
|
|
99
99
|
this.positionId,
|
100
100
|
this.programId
|
101
101
|
);
|
102
|
-
this.
|
102
|
+
this.pos = await getOrCreatePositionEx(
|
103
103
|
this.umi,
|
104
104
|
positionPk,
|
105
105
|
{
|
@@ -112,30 +112,30 @@ export abstract class SolautoClient extends ReferralStateManager {
|
|
112
112
|
);
|
113
113
|
|
114
114
|
this.positionSupplyTa = getTokenAccount(
|
115
|
-
this.
|
116
|
-
this.
|
115
|
+
this.pos.publicKey,
|
116
|
+
this.pos.supplyMint()
|
117
117
|
);
|
118
118
|
this.signerSupplyTa = getTokenAccount(
|
119
119
|
toWeb3JsPublicKey(this.signer.publicKey),
|
120
|
-
this.
|
120
|
+
this.pos.supplyMint()
|
121
121
|
);
|
122
122
|
|
123
123
|
this.positionDebtTa = getTokenAccount(
|
124
|
-
this.
|
125
|
-
this.
|
124
|
+
this.pos.publicKey,
|
125
|
+
this.pos.debtMint()
|
126
126
|
);
|
127
127
|
this.signerDebtTa = getTokenAccount(
|
128
128
|
toWeb3JsPublicKey(this.signer.publicKey),
|
129
|
-
this.
|
129
|
+
this.pos.debtMint()
|
130
130
|
);
|
131
131
|
|
132
132
|
this.solautoFeesSupplyTa = getTokenAccount(
|
133
133
|
SOLAUTO_FEES_WALLET,
|
134
|
-
this.
|
134
|
+
this.pos.supplyMint()
|
135
135
|
);
|
136
136
|
this.solautoFeesDebtTa = getTokenAccount(
|
137
137
|
SOLAUTO_FEES_WALLET,
|
138
|
-
this.
|
138
|
+
this.pos.debtMint()
|
139
139
|
);
|
140
140
|
|
141
141
|
this.authorityLutAddress =
|
@@ -149,33 +149,27 @@ export abstract class SolautoClient extends ReferralStateManager {
|
|
149
149
|
this.flProvider = new FlProviderAggregator(
|
150
150
|
this.umi,
|
151
151
|
this.signer,
|
152
|
-
this.
|
153
|
-
this.
|
152
|
+
this.pos.supplyMint(),
|
153
|
+
this.pos.debtMint()
|
154
154
|
);
|
155
155
|
await this.flProvider.initialize();
|
156
156
|
this.otherSigners.push(...this.flProvider.otherSigners());
|
157
157
|
|
158
|
-
this.log("Position state: ", this.
|
159
|
-
this.log("Position settings: ", this.
|
160
|
-
this.log("Position DCA: ", this.
|
158
|
+
this.log("Position state: ", this.pos.state());
|
159
|
+
this.log("Position settings: ", this.pos.settings());
|
160
|
+
this.log("Position DCA: ", this.pos.dca());
|
161
161
|
}
|
162
162
|
|
163
163
|
referredBySupplyTa(): PublicKey | undefined {
|
164
164
|
if (this.referredByState !== undefined) {
|
165
|
-
return getTokenAccount(
|
166
|
-
this.referredByState,
|
167
|
-
this.solautoPosition.supplyMint()
|
168
|
-
);
|
165
|
+
return getTokenAccount(this.referredByState, this.pos.supplyMint());
|
169
166
|
}
|
170
167
|
return undefined;
|
171
168
|
}
|
172
169
|
|
173
170
|
referredByDebtTa(): PublicKey | undefined {
|
174
171
|
if (this.referredByState !== undefined) {
|
175
|
-
return getTokenAccount(
|
176
|
-
this.referredByState,
|
177
|
-
this.solautoPosition.debtMint()
|
178
|
-
);
|
172
|
+
return getTokenAccount(this.referredByState, this.pos.debtMint());
|
179
173
|
}
|
180
174
|
return undefined;
|
181
175
|
}
|
@@ -183,15 +177,14 @@ export abstract class SolautoClient extends ReferralStateManager {
|
|
183
177
|
async resetLiveTxUpdates(success?: boolean) {
|
184
178
|
this.log("Resetting context updates...");
|
185
179
|
if (success) {
|
186
|
-
if (!this.
|
187
|
-
await this.
|
180
|
+
if (!this.pos.exists()) {
|
181
|
+
await this.pos.refetchPositionData();
|
188
182
|
} else {
|
189
183
|
if (this.contextUpdates.settings) {
|
190
|
-
this.
|
191
|
-
this.contextUpdates.settings;
|
184
|
+
this.pos.data().position!.settings = this.contextUpdates.settings;
|
192
185
|
}
|
193
186
|
if (this.contextUpdates.dca) {
|
194
|
-
this.
|
187
|
+
this.pos.data().position!.dca = this.contextUpdates.dca;
|
195
188
|
}
|
196
189
|
// All other live position updates can be derived by getting a fresh position state, so we don't need to do anything else form contextUpdates
|
197
190
|
}
|
@@ -217,7 +210,7 @@ export abstract class SolautoClient extends ReferralStateManager {
|
|
217
210
|
...(toWeb3JsPublicKey(this.signer.publicKey).equals(this.authority)
|
218
211
|
? [this.signerDebtTa]
|
219
212
|
: []),
|
220
|
-
this.
|
213
|
+
this.pos.publicKey,
|
221
214
|
this.positionSupplyTa,
|
222
215
|
this.positionDebtTa,
|
223
216
|
this.referralState,
|
@@ -324,7 +317,7 @@ export abstract class SolautoClient extends ReferralStateManager {
|
|
324
317
|
data = await this.connection.getTokenAccountBalance(
|
325
318
|
getTokenAccount(
|
326
319
|
toWeb3JsPublicKey(this.signer.publicKey),
|
327
|
-
this.
|
320
|
+
this.pos.supplyMint()
|
328
321
|
),
|
329
322
|
"confirmed"
|
330
323
|
);
|
@@ -337,7 +330,7 @@ export abstract class SolautoClient extends ReferralStateManager {
|
|
337
330
|
const data = await this.connection.getTokenAccountBalance(
|
338
331
|
getTokenAccount(
|
339
332
|
toWeb3JsPublicKey(this.signer.publicKey),
|
340
|
-
this.
|
333
|
+
this.pos.debtMint()
|
341
334
|
),
|
342
335
|
"confirmed"
|
343
336
|
);
|
@@ -387,11 +380,11 @@ export abstract class SolautoClient extends ReferralStateManager {
|
|
387
380
|
let signerDcaTa: UmiPublicKey | undefined = undefined;
|
388
381
|
if (isOption(args.dca) && isSome(args.dca)) {
|
389
382
|
if (args.dca.value.tokenType === TokenType.Supply) {
|
390
|
-
dcaMint = publicKey(this.
|
383
|
+
dcaMint = publicKey(this.pos.supplyMint());
|
391
384
|
positionDcaTa = publicKey(this.positionSupplyTa);
|
392
385
|
signerDcaTa = publicKey(this.signerSupplyTa);
|
393
386
|
} else {
|
394
|
-
dcaMint = publicKey(this.
|
387
|
+
dcaMint = publicKey(this.pos.debtMint());
|
395
388
|
positionDcaTa = publicKey(this.positionDebtTa);
|
396
389
|
signerDcaTa = publicKey(this.signerDebtTa);
|
397
390
|
}
|
@@ -427,7 +420,7 @@ export abstract class SolautoClient extends ReferralStateManager {
|
|
427
420
|
|
428
421
|
return updatePosition(this.umi, {
|
429
422
|
signer: this.signer,
|
430
|
-
solautoPosition: publicKey(this.
|
423
|
+
solautoPosition: publicKey(this.pos.publicKey),
|
431
424
|
dcaMint,
|
432
425
|
positionDcaTa,
|
433
426
|
signerDcaTa,
|
@@ -442,27 +435,27 @@ export abstract class SolautoClient extends ReferralStateManager {
|
|
442
435
|
let positionDcaTa: UmiPublicKey | undefined = undefined;
|
443
436
|
let signerDcaTa: UmiPublicKey | undefined = undefined;
|
444
437
|
|
445
|
-
const currDca = this.
|
438
|
+
const currDca = this.pos.dca()!;
|
446
439
|
if (currDca.dcaInBaseUnit > 0) {
|
447
440
|
if (currDca.tokenType === TokenType.Supply) {
|
448
|
-
dcaMint = publicKey(this.
|
441
|
+
dcaMint = publicKey(this.pos.supplyMint());
|
449
442
|
positionDcaTa = publicKey(this.positionSupplyTa);
|
450
443
|
signerDcaTa = publicKey(this.signerSupplyTa);
|
451
444
|
} else {
|
452
|
-
dcaMint = publicKey(this.
|
445
|
+
dcaMint = publicKey(this.pos.debtMint());
|
453
446
|
positionDcaTa = publicKey(this.positionDebtTa);
|
454
447
|
signerDcaTa = publicKey(this.signerDebtTa);
|
455
448
|
}
|
456
449
|
|
457
450
|
this.contextUpdates.new({
|
458
451
|
type: "cancellingDca",
|
459
|
-
value: this.
|
452
|
+
value: this.pos.dca()!.tokenType,
|
460
453
|
});
|
461
454
|
}
|
462
455
|
|
463
456
|
return cancelDCA(this.umi, {
|
464
457
|
signer: this.signer,
|
465
|
-
solautoPosition: publicKey(this.
|
458
|
+
solautoPosition: publicKey(this.pos.publicKey),
|
466
459
|
dcaMint,
|
467
460
|
positionDcaTa,
|
468
461
|
signerDcaTa,
|
@@ -505,9 +498,7 @@ export abstract class SolautoClient extends ReferralStateManager {
|
|
505
498
|
toWeb3JsPublicKey(this.signer.publicKey),
|
506
499
|
BigInt(
|
507
500
|
Math.round(
|
508
|
-
Number(
|
509
|
-
this.solautoPosition.state().debt.amountUsed.baseUnit
|
510
|
-
) * 1.01
|
501
|
+
Number(this.pos.state().debt.amountUsed.baseUnit) * 1.01
|
511
502
|
)
|
512
503
|
)
|
513
504
|
)
|
@@ -531,8 +522,8 @@ export abstract class SolautoClient extends ReferralStateManager {
|
|
531
522
|
this.contextUpdates.new({
|
532
523
|
type: "supply",
|
533
524
|
value:
|
534
|
-
(this.
|
535
|
-
|
525
|
+
(this.pos.state().supply.amountUsed.baseUnit ?? BigInt(0)) *
|
526
|
+
BigInt(-1),
|
536
527
|
});
|
537
528
|
}
|
538
529
|
} else if (args.__kind === "Borrow") {
|
@@ -550,8 +541,8 @@ export abstract class SolautoClient extends ReferralStateManager {
|
|
550
541
|
this.contextUpdates.new({
|
551
542
|
type: "debt",
|
552
543
|
value:
|
553
|
-
(this.
|
554
|
-
|
544
|
+
(this.pos.state().debt.amountUsed.baseUnit ?? BigInt(0)) *
|
545
|
+
BigInt(-1),
|
555
546
|
});
|
556
547
|
}
|
557
548
|
}
|
@@ -69,19 +69,19 @@ export class SolautoMarginfiClient extends SolautoClient {
|
|
69
69
|
async initialize(args: SolautoMarginfiClientArgs) {
|
70
70
|
await super.initialize(args);
|
71
71
|
|
72
|
-
this.marginfiGroup = await this.
|
72
|
+
this.marginfiGroup = await this.pos.lendingPool();
|
73
73
|
|
74
74
|
if (this.selfManaged) {
|
75
75
|
this.marginfiAccount =
|
76
76
|
args.marginfiAccount ??
|
77
77
|
createSignerFromKeypair(this.umi, this.umi.eddsa.generateKeypair());
|
78
78
|
} else {
|
79
|
-
if (this.
|
80
|
-
this.marginfiAccount = this.
|
79
|
+
if (this.pos.exists()) {
|
80
|
+
this.marginfiAccount = this.pos.lpUserAccount!;
|
81
81
|
} else {
|
82
82
|
const accounts = await getAllMarginfiAccountsByAuthority(
|
83
83
|
this.umi,
|
84
|
-
this.
|
84
|
+
this.pos.publicKey,
|
85
85
|
this.marginfiGroup,
|
86
86
|
false
|
87
87
|
);
|
@@ -114,11 +114,11 @@ export class SolautoMarginfiClient extends SolautoClient {
|
|
114
114
|
|
115
115
|
this.marginfiSupplyAccounts =
|
116
116
|
MARGINFI_ACCOUNTS[this.marginfiGroup.toString()][
|
117
|
-
this.
|
117
|
+
this.pos.supplyMint().toString()
|
118
118
|
]!;
|
119
119
|
this.marginfiDebtAccounts =
|
120
120
|
MARGINFI_ACCOUNTS[this.marginfiGroup.toString()][
|
121
|
-
this.
|
121
|
+
this.pos.debtMint().toString()
|
122
122
|
]!;
|
123
123
|
|
124
124
|
// TODO: Don't dynamically pull oracle from bank until Marginfi sorts out their price oracle issues.
|
@@ -182,16 +182,16 @@ export class SolautoMarginfiClient extends SolautoClient {
|
|
182
182
|
referredBySupplyTa: this.referredBySupplyTa()
|
183
183
|
? publicKey(this.referredBySupplyTa()!)
|
184
184
|
: undefined,
|
185
|
-
solautoPosition: publicKey(this.
|
185
|
+
solautoPosition: publicKey(this.pos.publicKey),
|
186
186
|
marginfiGroup: publicKey(this.marginfiGroup),
|
187
187
|
marginfiAccount:
|
188
188
|
"publicKey" in this.marginfiAccount
|
189
189
|
? (this.marginfiAccount as Signer)
|
190
190
|
: publicKey(this.marginfiAccount),
|
191
|
-
supplyMint: publicKey(this.
|
191
|
+
supplyMint: publicKey(this.pos.supplyMint()),
|
192
192
|
supplyBank: publicKey(this.marginfiSupplyAccounts.bank),
|
193
193
|
positionSupplyTa: publicKey(this.positionSupplyTa),
|
194
|
-
debtMint: publicKey(this.
|
194
|
+
debtMint: publicKey(this.pos.debtMint()),
|
195
195
|
debtBank: publicKey(this.marginfiDebtAccounts.bank),
|
196
196
|
positionDebtTa: publicKey(this.positionDebtTa),
|
197
197
|
signerDebtTa: signerDebtTa,
|
@@ -207,7 +207,7 @@ export class SolautoMarginfiClient extends SolautoClient {
|
|
207
207
|
closePositionIx(): TransactionBuilder {
|
208
208
|
return closePosition(this.umi, {
|
209
209
|
signer: this.signer,
|
210
|
-
solautoPosition: publicKey(this.
|
210
|
+
solautoPosition: publicKey(this.pos.publicKey),
|
211
211
|
signerSupplyTa: publicKey(this.signerSupplyTa),
|
212
212
|
positionSupplyTa: publicKey(this.positionSupplyTa),
|
213
213
|
positionDebtTa: publicKey(this.positionDebtTa),
|
@@ -226,7 +226,7 @@ export class SolautoMarginfiClient extends SolautoClient {
|
|
226
226
|
supplyPriceOracle: publicKey(this.supplyPriceOracle),
|
227
227
|
debtBank: publicKey(this.marginfiDebtAccounts.bank),
|
228
228
|
debtPriceOracle: publicKey(this.debtPriceOracle),
|
229
|
-
solautoPosition: publicKey(this.
|
229
|
+
solautoPosition: publicKey(this.pos.publicKey),
|
230
230
|
});
|
231
231
|
}
|
232
232
|
|
@@ -348,7 +348,7 @@ export class SolautoMarginfiClient extends SolautoClient {
|
|
348
348
|
return marginfiProtocolInteraction(this.umi, {
|
349
349
|
signer: this.signer,
|
350
350
|
marginfiProgram: publicKey(MARGINFI_PROGRAM_ID),
|
351
|
-
solautoPosition: publicKey(this.
|
351
|
+
solautoPosition: publicKey(this.pos.publicKey),
|
352
352
|
marginfiGroup: publicKey(this.marginfiGroup),
|
353
353
|
marginfiAccount: publicKey(this.marginfiAccountPk),
|
354
354
|
supplyBank: publicKey(this.marginfiSupplyAccounts.bank),
|
@@ -370,10 +370,10 @@ export class SolautoMarginfiClient extends SolautoClient {
|
|
370
370
|
data: RebalanceDetails
|
371
371
|
): TransactionBuilder {
|
372
372
|
const inputIsSupply = new PublicKey(data.swapQuote.inputMint).equals(
|
373
|
-
this.
|
373
|
+
this.pos.supplyMint()
|
374
374
|
);
|
375
375
|
const outputIsSupply = new PublicKey(data.swapQuote.outputMint).equals(
|
376
|
-
this.
|
376
|
+
this.pos.supplyMint()
|
377
377
|
);
|
378
378
|
|
379
379
|
const preSwapRebalance = rebalanceStep === RebalanceStep.PreSwap;
|
@@ -417,7 +417,7 @@ export class SolautoMarginfiClient extends SolautoClient {
|
|
417
417
|
data.values.tokenBalanceChange !== undefined
|
418
418
|
? publicKey(this.authority)
|
419
419
|
: undefined,
|
420
|
-
solautoPosition: publicKey(this.
|
420
|
+
solautoPosition: publicKey(this.pos.publicKey),
|
421
421
|
marginfiGroup: publicKey(this.marginfiGroup),
|
422
422
|
marginfiAccount: publicKey(this.marginfiAccountPk),
|
423
423
|
intermediaryTa: publicKey(
|
@@ -431,7 +431,7 @@ export class SolautoMarginfiClient extends SolautoClient {
|
|
431
431
|
positionSupplyTa: publicKey(this.positionSupplyTa),
|
432
432
|
authoritySupplyTa: addAuthorityTas
|
433
433
|
? publicKey(
|
434
|
-
getTokenAccount(this.authority, this.
|
434
|
+
getTokenAccount(this.authority, this.pos.supplyMint())
|
435
435
|
)
|
436
436
|
: undefined,
|
437
437
|
vaultSupplyTa: needSupplyAccounts
|
@@ -445,7 +445,7 @@ export class SolautoMarginfiClient extends SolautoClient {
|
|
445
445
|
positionDebtTa: publicKey(this.positionDebtTa),
|
446
446
|
authorityDebtTa: addAuthorityTas
|
447
447
|
? publicKey(
|
448
|
-
getTokenAccount(this.authority, this.
|
448
|
+
getTokenAccount(this.authority, this.pos.debtMint())
|
449
449
|
)
|
450
450
|
: undefined,
|
451
451
|
vaultDebtTa: needDebtAccounts
|
@@ -73,8 +73,8 @@ function getWSolUsage(
|
|
73
73
|
},
|
74
74
|
cancellingDcaIn?: TokenType
|
75
75
|
): wSolTokenUsage | undefined {
|
76
|
-
const supplyIsWsol = client.
|
77
|
-
const debtIsWsol = client.
|
76
|
+
const supplyIsWsol = client.pos.supplyMint().equals(NATIVE_MINT);
|
77
|
+
const debtIsWsol = client.pos.debtMint().equals(NATIVE_MINT);
|
78
78
|
if (!supplyIsWsol && !debtIsWsol) {
|
79
79
|
return undefined;
|
80
80
|
}
|
@@ -148,7 +148,7 @@ async function transactionChoresBefore(
|
|
148
148
|
}
|
149
149
|
// TODO: PF
|
150
150
|
|
151
|
-
if (!client.
|
151
|
+
if (!client.pos.exists()) {
|
152
152
|
chores = chores.add(client.openPositionIx());
|
153
153
|
}
|
154
154
|
}
|
@@ -229,8 +229,8 @@ async function transactionChoresBefore(
|
|
229
229
|
client.signer,
|
230
230
|
toWeb3JsPublicKey(client.signer.publicKey),
|
231
231
|
isSolautoAction("Withdraw", solautoAction)
|
232
|
-
? client.
|
233
|
-
: client.
|
232
|
+
? client.pos.supplyMint()
|
233
|
+
: client.pos.debtMint()
|
234
234
|
)
|
235
235
|
);
|
236
236
|
accountsGettingCreated.push(tokenAccount.toString());
|
@@ -284,7 +284,7 @@ export async function rebalanceChoresBefore(
|
|
284
284
|
createAssociatedTokenAccountUmiIx(
|
285
285
|
client.signer,
|
286
286
|
client.referredByState!,
|
287
|
-
client.
|
287
|
+
client.pos.supplyMint()
|
288
288
|
)
|
289
289
|
);
|
290
290
|
}
|
@@ -295,7 +295,7 @@ export async function rebalanceChoresBefore(
|
|
295
295
|
createAssociatedTokenAccountUmiIx(
|
296
296
|
client.signer,
|
297
297
|
client.referredByState!,
|
298
|
-
client.
|
298
|
+
client.pos.debtMint()
|
299
299
|
)
|
300
300
|
);
|
301
301
|
}
|
@@ -310,7 +310,7 @@ export async function rebalanceChoresBefore(
|
|
310
310
|
createAssociatedTokenAccountUmiIx(
|
311
311
|
client.signer,
|
312
312
|
toWeb3JsPublicKey(client.signer.publicKey),
|
313
|
-
client.
|
313
|
+
client.pos.supplyMint()
|
314
314
|
)
|
315
315
|
);
|
316
316
|
accountsGettingCreated.push(signerSupplyTa.publicKey.toString());
|
@@ -326,7 +326,7 @@ export async function rebalanceChoresBefore(
|
|
326
326
|
createAssociatedTokenAccountUmiIx(
|
327
327
|
client.signer,
|
328
328
|
toWeb3JsPublicKey(client.signer.publicKey),
|
329
|
-
client.
|
329
|
+
client.pos.debtMint()
|
330
330
|
)
|
331
331
|
);
|
332
332
|
accountsGettingCreated.push(signerDebtTa.publicKey.toString());
|
@@ -274,12 +274,14 @@ export abstract class SolautoPositionEx {
|
|
274
274
|
return newState.liqUtilizationRateBps - oldState.liqUtilizationRateBps;
|
275
275
|
}
|
276
276
|
|
277
|
-
async updateWithLatestPrices(supplyPrice?: number, debtPrice?: number) {
|
278
|
-
|
277
|
+
async updateWithLatestPrices(supplyPrice?: number, debtPrice?: number): Promise<PositionState> {
|
278
|
+
const newState = await positionStateWithLatestPrices(
|
279
279
|
this.state(),
|
280
280
|
supplyPrice,
|
281
281
|
debtPrice
|
282
282
|
);
|
283
|
+
this._data.state = newState;
|
284
|
+
return newState;
|
283
285
|
}
|
284
286
|
|
285
287
|
async refetchPositionData() {
|
@@ -44,7 +44,7 @@ export async function e2eTransactionTest(
|
|
44
44
|
});
|
45
45
|
|
46
46
|
const [maxLtvBps, liqThresholdBps] =
|
47
|
-
await client.
|
47
|
+
await client.pos.maxLtvAndLiqThresholdBps();
|
48
48
|
const settings: SolautoSettingsParametersInpArgs = {
|
49
49
|
boostToBps: maxBoostToBps(maxLtvBps, liqThresholdBps) - 200,
|
50
50
|
boostGap: 50,
|
@@ -37,11 +37,11 @@ function assertAccurateRebalance(
|
|
37
37
|
targetLiqUtilizationRateBps?: number
|
38
38
|
) {
|
39
39
|
const { endResult } = getRebalanceValues(
|
40
|
-
client.
|
40
|
+
client.pos,
|
41
41
|
new SolautoFeesBps(
|
42
42
|
false,
|
43
43
|
targetLiqUtilizationRateBps,
|
44
|
-
client.
|
44
|
+
client.pos.netWorthUsd()
|
45
45
|
),
|
46
46
|
50,
|
47
47
|
targetLiqUtilizationRateBps
|
@@ -50,7 +50,7 @@ function assertAccurateRebalance(
|
|
50
50
|
const newLiqUtilizationRateBps = getLiqUtilzationRateBps(
|
51
51
|
endResult.supplyUsd,
|
52
52
|
endResult.debtUsd,
|
53
|
-
client.
|
53
|
+
client.pos.state().liqThresholdBps
|
54
54
|
);
|
55
55
|
assert(
|
56
56
|
Math.round(newLiqUtilizationRateBps) ===
|
@@ -98,7 +98,7 @@ async function getFakePosition(
|
|
98
98
|
liqThresholdBps
|
99
99
|
);
|
100
100
|
|
101
|
-
client.
|
101
|
+
client.pos = new MarginfiSolautoPositionEx({
|
102
102
|
umi: client.umi,
|
103
103
|
publicKey: PublicKey.default,
|
104
104
|
data: {
|