@haven-fi/solauto-sdk 1.0.703 → 1.0.704
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.d.ts.map +1 -1
- package/dist/services/rebalance/rebalanceSwapManager.js +20 -6
- package/dist/services/rebalance/rebalanceValues.d.ts.map +1 -1
- package/dist/services/rebalance/rebalanceValues.js +4 -3
- package/dist/services/swap/jupSwapManager.js +1 -1
- package/local/txSandbox.ts +10 -2
- package/package.json +1 -1
- package/src/services/rebalance/rebalanceSwapManager.ts +52 -13
- package/src/services/rebalance/rebalanceValues.ts +4 -3
- package/src/services/swap/jupSwapManager.ts +2 -2
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"rebalanceSwapManager.d.ts","sourceRoot":"","sources":["../../../src/services/rebalance/rebalanceSwapManager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAE5C,OAAO,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AACpD,OAAO,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAC3C,OAAO,EAAkB,UAAU,EAAa,MAAM,SAAS,CAAC;AAChE,OAAO,EAA0B,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAC5E,OAAO,EAAE,SAAS,EAAiC,MAAM,iBAAiB,CAAC;
|
1
|
+
{"version":3,"file":"rebalanceSwapManager.d.ts","sourceRoot":"","sources":["../../../src/services/rebalance/rebalanceSwapManager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAE5C,OAAO,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AACpD,OAAO,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAC3C,OAAO,EAAkB,UAAU,EAAa,MAAM,SAAS,CAAC;AAChE,OAAO,EAA0B,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAC5E,OAAO,EAAE,SAAS,EAAiC,MAAM,iBAAiB,CAAC;AAc3E,qBAAa,oBAAoB;IAS7B,OAAO,CAAC,MAAM;IACd,OAAO,CAAC,MAAM;IACd,OAAO,CAAC,cAAc,CAAC;IACvB,OAAO,CAAC,2BAA2B,CAAC;IACpC,OAAO,CAAC,SAAS,CAAC;IAZb,UAAU,EAAG,UAAU,CAAC;IACxB,SAAS,CAAC,EAAE,aAAa,CAAC;IAC1B,cAAc,CAAC,EAAE,MAAM,CAAC;IAE/B,OAAO,CAAC,cAAc,CAAkB;IACxC,OAAO,CAAC,aAAa,CAAU;gBAGrB,MAAM,EAAE,aAAa,EACrB,MAAM,EAAE,eAAe,EACvB,cAAc,CAAC,EAAE,qBAAqB,YAAA,EACtC,2BAA2B,CAAC,EAAE,MAAM,YAAA,EACpC,SAAS,CAAC,EAAE,SAAS,YAAA;IAU/B,OAAO,CAAC,OAAO;IAIf,OAAO,CAAC,SAAS;IAIjB,OAAO,CAAC,WAAW;IA4CnB,OAAO,CAAC,kCAAkC;YAiF5B,mBAAmB;IA2CjC,OAAO,CAAC,mBAAmB;IAIrB,aAAa,CAAC,UAAU,EAAE,MAAM;IAyEhC,aAAa;;;;;;CAWpB"}
|
@@ -38,6 +38,8 @@ class RebalanceSwapManager {
|
|
38
38
|
const biasedOutputPrice = this.isBoost() ? supplyPrice : debtPrice;
|
39
39
|
// const biasedInputPrice = inputPrice;
|
40
40
|
// const biasedOutputPrice = outputPrice;
|
41
|
+
// const priceDiff = (biasedInputPrice - inputPrice) / 2;
|
42
|
+
// const weightedInputPrice = inputPrice + priceDiff;
|
41
43
|
let inputAmount = (0, utils_1.toBaseUnit)(this.usdToSwap() / biasedInputPrice, input.decimals);
|
42
44
|
return {
|
43
45
|
inputAmount,
|
@@ -49,18 +51,24 @@ class RebalanceSwapManager {
|
|
49
51
|
biasedOutputPrice,
|
50
52
|
};
|
51
53
|
}
|
52
|
-
postRebalanceLiqUtilizationRateBps(
|
54
|
+
postRebalanceLiqUtilizationRateBps(swapOutputAmountBaseUnit, swapInputAmountBaseUnit) {
|
53
55
|
let supplyUsd = this.client.pos.supplyUsd(this.priceType);
|
54
56
|
let debtUsd = this.client.pos.debtUsd(this.priceType);
|
55
57
|
// TODO: add token balance change
|
56
|
-
const { input, biasedInputPrice, output, biasedOutputPrice } = this.swapDetails();
|
58
|
+
const { input, inputPrice, biasedInputPrice, output, outputPrice, biasedOutputPrice, } = this.swapDetails();
|
59
|
+
const swapInputAmount = swapInputAmountBaseUnit
|
60
|
+
? (0, utils_1.fromBaseUnit)(swapInputAmountBaseUnit, (0, utils_1.tokenInfo)((0, umi_web3js_adapters_1.toWeb3JsPublicKey)(input.mint)).decimals)
|
61
|
+
: undefined;
|
62
|
+
const swapOutputAmount = swapOutputAmountBaseUnit
|
63
|
+
? (0, utils_1.fromBaseUnit)(swapOutputAmountBaseUnit, (0, utils_1.tokenInfo)((0, umi_web3js_adapters_1.toWeb3JsPublicKey)(output.mint)).decimals)
|
64
|
+
: undefined;
|
57
65
|
const swapInputUsd = swapInputAmount
|
58
|
-
?
|
66
|
+
? swapInputAmount * biasedInputPrice
|
59
67
|
: this.usdToSwap();
|
60
68
|
const swapOutputUsd = swapOutputAmount
|
61
|
-
?
|
69
|
+
? swapOutputAmount * biasedOutputPrice
|
62
70
|
: this.usdToSwap();
|
63
|
-
console.log(swapInputUsd, swapOutputUsd);
|
71
|
+
console.log((swapInputAmount ?? 0) * inputPrice, swapInputUsd, (swapOutputAmount ?? 0) * outputPrice, swapOutputUsd);
|
64
72
|
const res = (0, rebalanceValues_1.applyDebtAdjustmentUsd)({
|
65
73
|
debtAdjustmentUsd: this.isBoost() ? swapInputUsd : swapInputUsd * -1,
|
66
74
|
debtAdjustmentUsdOutput: swapOutputUsd,
|
@@ -69,6 +77,12 @@ class RebalanceSwapManager {
|
|
69
77
|
flashLoan: this.flRequirements?.flFeeBps ?? 0,
|
70
78
|
lpBorrow: this.client.pos.state.debt.borrowFeeBps,
|
71
79
|
});
|
80
|
+
if ((0, utils_1.isMarginfiPosition)(this.client.pos)) {
|
81
|
+
console.log(res.newPos.supplyUsd, res.newPos.debtUsd);
|
82
|
+
console.log(res.newPos.supplyUsd *
|
83
|
+
(0, utils_1.bytesToI80F48)(this.client.pos.supplyBank.config.assetWeightInit.value), res.newPos.debtUsd *
|
84
|
+
(0, utils_1.bytesToI80F48)(this.client.pos.debtBank.config.liabilityWeightInit.value));
|
85
|
+
}
|
72
86
|
return (0, utils_1.getLiqUtilzationRateBps)(res.newPos.supplyUsd, res.newPos.debtUsd, this.client.pos.state.liqThresholdBps ?? 0);
|
73
87
|
}
|
74
88
|
async findSufficientQuote(swapInput, criteria) {
|
@@ -87,7 +101,7 @@ class RebalanceSwapManager {
|
|
87
101
|
(0, utils_1.consoleLog)(postRebalanceRate, criteria.maxLiqUtilizationRateBps);
|
88
102
|
if (insufficient) {
|
89
103
|
(0, utils_1.consoleLog)("Insufficient swap quote:", swapQuote);
|
90
|
-
const increment = 0.01 + i * 0.
|
104
|
+
const increment = 0.01 + i * 0.01;
|
91
105
|
swapInput.amount = this.bigIntWithIncrement(swapInput.amount, this.isBoost() ? increment * -1 : increment);
|
92
106
|
}
|
93
107
|
else {
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"rebalanceValues.d.ts","sourceRoot":"","sources":["../../../src/services/rebalance/rebalanceValues.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,SAAS,EACT,kBAAkB,EAClB,kBAAkB,EAEnB,MAAM,iBAAiB,CAAC;AAOzB,OAAO,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AAC1D,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAE/C,MAAM,WAAW,cAAc;IAC7B,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,cAAc;IAC7B,iBAAiB,EAAE,MAAM,CAAC;IAC1B,SAAS,EAAE,cAAc,CAAC;IAC1B,iCAAi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,UAAU,yBAAyB;IACjC,MAAM,EAAE,cAAc,CAAC;IACvB,iCAAiC,EAAE,MAAM,CAAC;CAC3C;AAED,wBAAgB,sBAAsB,CACpC,UAAU,EAAE;IAAE,iBAAiB,EAAE,MAAM,CAAC;IAAC,uBAAuB,CAAC,EAAE,MAAM,CAAA;CAAE,EAC3E,GAAG,EAAE,cAAc,EACnB,YAAY,EAAE,MAAM,EACpB,IAAI,CAAC,EAAE,gBAAgB,GACtB,yBAAyB,CAmC3B;AAED,wBAAgB,iBAAiB,CAC/B,eAAe,EAAE,MAAM,EACvB,GAAG,EAAE,cAAc,EACnB,2BAA2B,EAAE,MAAM,EACnC,IAAI,CAAC,EAAE,gBAAgB,GACtB,cAAc,CAiChB;AA2ED,MAAM,WAAW,eAAgB,SAAQ,cAAc;IACrD,kBAAkB,EAAE,kBAAkB,CAAC;IACvC,kBAAkB,CAAC,EAAE,kBAAkB,CAAC;IACxC,qBAAqB,EAAE,OAAO,CAAC;CAChC;AAED,wBAAgB,kBAAkB,CAChC,eAAe,EAAE,iBAAiB,EAClC,SAAS,EAAE,SAAS,EACpB,2BAA2B,CAAC,EAAE,MAAM,EACpC,aAAa,CAAC,EAAE,cAAc,EAC9B,QAAQ,CAAC,EAAE,MAAM,GAChB,eAAe,GAAG,SAAS,CA8C7B"}
|
@@ -44,11 +44,12 @@ function getDebtAdjustment(liqThresholdBps, pos, targetLiqUtilizationRateBps, fe
|
|
44
44
|
targetUtilizationRate * actualizedFee * liqThreshold)
|
45
45
|
: (targetUtilizationRate * liqThreshold * pos.supplyUsd - pos.debtUsd) /
|
46
46
|
(actualizedFee - targetUtilizationRate * liqThreshold * (1.0 + flFee));
|
47
|
-
const
|
47
|
+
const endResult = applyDebtAdjustmentUsd({ debtAdjustmentUsd }, pos, liqThreshold, fees);
|
48
|
+
console.log(debtAdjustmentUsd, pos, endResult.newPos);
|
48
49
|
return {
|
49
50
|
debtAdjustmentUsd,
|
50
|
-
endResult:
|
51
|
-
intermediaryLiqUtilizationRateBps:
|
51
|
+
endResult: endResult.newPos,
|
52
|
+
intermediaryLiqUtilizationRateBps: endResult.intermediaryLiqUtilizationRateBps,
|
52
53
|
};
|
53
54
|
}
|
54
55
|
function getTokenBalanceChange() {
|
package/local/txSandbox.ts
CHANGED
@@ -67,7 +67,15 @@ export async function main() {
|
|
67
67
|
// debtMint: new PublicKey(RETARDIO),
|
68
68
|
});
|
69
69
|
|
70
|
-
await client.pos.refreshPositionState();
|
70
|
+
// await client.pos.refreshPositionState();
|
71
|
+
|
72
|
+
console.log(
|
73
|
+
getMaxLiqUtilizationRateBps(
|
74
|
+
client.pos.state.maxLtvBps,
|
75
|
+
client.pos.state.liqThresholdBps,
|
76
|
+
0
|
77
|
+
)
|
78
|
+
);
|
71
79
|
|
72
80
|
// const transactionItems = [
|
73
81
|
// openSolautoPosition(client, {
|
@@ -84,7 +92,7 @@ export async function main() {
|
|
84
92
|
// repayGap: 100,
|
85
93
|
// repayToBps: client.pos.maxRepayToBps,
|
86
94
|
// }),
|
87
|
-
deposit(client, toBaseUnit(
|
95
|
+
deposit(client, toBaseUnit(5, client.pos.supplyMintInfo.decimals)),
|
88
96
|
rebalance(client, client.pos.maxBoostToBps),
|
89
97
|
// withdraw(client, "All"),
|
90
98
|
// closeSolautoPosition(client)
|
package/package.json
CHANGED
@@ -6,10 +6,12 @@ import { JupSwapManager, SwapParams, SwapInput } from "../swap";
|
|
6
6
|
import { applyDebtAdjustmentUsd, RebalanceValues } from "./rebalanceValues";
|
7
7
|
import { PriceType, RebalanceDirection, TokenType } from "../../generated";
|
8
8
|
import {
|
9
|
+
bytesToI80F48,
|
9
10
|
consoleLog,
|
10
11
|
fromBaseUnit,
|
11
12
|
fromBps,
|
12
13
|
getLiqUtilzationRateBps,
|
14
|
+
isMarginfiPosition,
|
13
15
|
safeGetPrice,
|
14
16
|
toBaseUnit,
|
15
17
|
tokenInfo,
|
@@ -72,6 +74,9 @@ export class RebalanceSwapManager {
|
|
72
74
|
// const biasedInputPrice = inputPrice;
|
73
75
|
// const biasedOutputPrice = outputPrice;
|
74
76
|
|
77
|
+
// const priceDiff = (biasedInputPrice - inputPrice) / 2;
|
78
|
+
// const weightedInputPrice = inputPrice + priceDiff;
|
79
|
+
|
75
80
|
let inputAmount = toBaseUnit(
|
76
81
|
this.usdToSwap() / biasedInputPrice!,
|
77
82
|
input.decimals
|
@@ -89,31 +94,51 @@ export class RebalanceSwapManager {
|
|
89
94
|
}
|
90
95
|
|
91
96
|
private postRebalanceLiqUtilizationRateBps(
|
92
|
-
|
93
|
-
|
97
|
+
swapOutputAmountBaseUnit?: bigint,
|
98
|
+
swapInputAmountBaseUnit?: bigint
|
94
99
|
) {
|
95
100
|
let supplyUsd = this.client.pos.supplyUsd(this.priceType);
|
96
101
|
let debtUsd = this.client.pos.debtUsd(this.priceType);
|
97
102
|
// TODO: add token balance change
|
98
103
|
|
99
|
-
const {
|
100
|
-
|
104
|
+
const {
|
105
|
+
input,
|
106
|
+
inputPrice,
|
107
|
+
biasedInputPrice,
|
108
|
+
output,
|
109
|
+
outputPrice,
|
110
|
+
biasedOutputPrice,
|
111
|
+
} = this.swapDetails();
|
101
112
|
|
102
|
-
const
|
113
|
+
const swapInputAmount = swapInputAmountBaseUnit
|
103
114
|
? fromBaseUnit(
|
104
|
-
|
115
|
+
swapInputAmountBaseUnit,
|
105
116
|
tokenInfo(toWeb3JsPublicKey(input.mint)).decimals
|
106
|
-
)
|
107
|
-
:
|
117
|
+
)
|
118
|
+
: undefined;
|
108
119
|
|
109
|
-
const
|
120
|
+
const swapOutputAmount = swapOutputAmountBaseUnit
|
110
121
|
? fromBaseUnit(
|
111
|
-
|
122
|
+
swapOutputAmountBaseUnit,
|
112
123
|
tokenInfo(toWeb3JsPublicKey(output.mint)).decimals
|
113
|
-
)
|
124
|
+
)
|
125
|
+
: undefined;
|
126
|
+
|
127
|
+
const swapInputUsd = swapInputAmount
|
128
|
+
? swapInputAmount * biasedInputPrice
|
129
|
+
: this.usdToSwap();
|
130
|
+
|
131
|
+
const swapOutputUsd = swapOutputAmount
|
132
|
+
? swapOutputAmount * biasedOutputPrice
|
114
133
|
: this.usdToSwap();
|
115
134
|
|
116
|
-
console.log(
|
135
|
+
console.log(
|
136
|
+
(swapInputAmount ?? 0) * inputPrice,
|
137
|
+
swapInputUsd,
|
138
|
+
(swapOutputAmount ?? 0) * outputPrice,
|
139
|
+
swapOutputUsd
|
140
|
+
);
|
141
|
+
|
117
142
|
const res = applyDebtAdjustmentUsd(
|
118
143
|
{
|
119
144
|
debtAdjustmentUsd: this.isBoost() ? swapInputUsd : swapInputUsd * -1,
|
@@ -128,6 +153,20 @@ export class RebalanceSwapManager {
|
|
128
153
|
}
|
129
154
|
);
|
130
155
|
|
156
|
+
if (isMarginfiPosition(this.client.pos)) {
|
157
|
+
console.log(res.newPos.supplyUsd, res.newPos.debtUsd);
|
158
|
+
console.log(
|
159
|
+
res.newPos.supplyUsd *
|
160
|
+
bytesToI80F48(
|
161
|
+
this.client.pos.supplyBank!.config.assetWeightInit.value
|
162
|
+
),
|
163
|
+
res.newPos.debtUsd *
|
164
|
+
bytesToI80F48(
|
165
|
+
this.client.pos.debtBank!.config.liabilityWeightInit.value
|
166
|
+
)
|
167
|
+
);
|
168
|
+
}
|
169
|
+
|
131
170
|
return getLiqUtilzationRateBps(
|
132
171
|
res.newPos.supplyUsd,
|
133
172
|
res.newPos.debtUsd,
|
@@ -165,7 +204,7 @@ export class RebalanceSwapManager {
|
|
165
204
|
if (insufficient) {
|
166
205
|
consoleLog("Insufficient swap quote:", swapQuote);
|
167
206
|
|
168
|
-
const increment = 0.01 + i * 0.
|
207
|
+
const increment = 0.01 + i * 0.01;
|
169
208
|
swapInput.amount = this.bigIntWithIncrement(
|
170
209
|
swapInput.amount,
|
171
210
|
this.isBoost() ? increment * -1 : increment
|
@@ -104,17 +104,18 @@ export function getDebtAdjustment(
|
|
104
104
|
: (targetUtilizationRate * liqThreshold * pos.supplyUsd - pos.debtUsd) /
|
105
105
|
(actualizedFee - targetUtilizationRate * liqThreshold * (1.0 + flFee));
|
106
106
|
|
107
|
-
const
|
107
|
+
const endResult = applyDebtAdjustmentUsd(
|
108
108
|
{ debtAdjustmentUsd },
|
109
109
|
pos,
|
110
110
|
liqThreshold,
|
111
111
|
fees
|
112
112
|
);
|
113
|
+
console.log(debtAdjustmentUsd, pos, endResult.newPos);
|
113
114
|
|
114
115
|
return {
|
115
116
|
debtAdjustmentUsd,
|
116
|
-
endResult:
|
117
|
-
intermediaryLiqUtilizationRateBps:
|
117
|
+
endResult: endResult.newPos,
|
118
|
+
intermediaryLiqUtilizationRateBps: endResult.intermediaryLiqUtilizationRateBps,
|
118
119
|
};
|
119
120
|
}
|
120
121
|
|