@haven-fi/solauto-sdk 1.0.702 → 1.0.703
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 -28
- package/dist/services/rebalance/rebalanceTxBuilder.d.ts.map +1 -1
- package/dist/services/rebalance/rebalanceTxBuilder.js +6 -4
- package/dist/services/rebalance/rebalanceValues.d.ts +4 -1
- package/dist/services/rebalance/rebalanceValues.d.ts.map +1 -1
- package/dist/services/rebalance/rebalanceValues.js +9 -5
- package/package.json +1 -1
- package/src/services/rebalance/rebalanceSwapManager.ts +32 -30
- package/src/services/rebalance/rebalanceTxBuilder.ts +11 -3
- package/src/services/rebalance/rebalanceValues.ts +10 -5
@@ -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,
|
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;AAY3E,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;IAyCnB,OAAO,CAAC,kCAAkC;YA+C5B,mBAAmB;IA2CjC,OAAO,CAAC,mBAAmB;IAIrB,aAAa,CAAC,UAAU,EAAE,MAAM;IAyEhC,aAAa;;;;;;CAWpB"}
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.RebalanceSwapManager = void 0;
|
4
4
|
const umi_web3js_adapters_1 = require("@metaplex-foundation/umi-web3js-adapters");
|
5
5
|
const swap_1 = require("../swap");
|
6
|
+
const rebalanceValues_1 = require("./rebalanceValues");
|
6
7
|
const generated_1 = require("../../generated");
|
7
8
|
const utils_1 = require("../../utils");
|
8
9
|
const solautoFees_1 = require("./solautoFees");
|
@@ -29,21 +30,15 @@ class RebalanceSwapManager {
|
|
29
30
|
const output = this.isBoost()
|
30
31
|
? this.client.pos.state.supply
|
31
32
|
: this.client.pos.state.debt;
|
32
|
-
|
33
|
-
|
34
|
-
// this.priceType
|
35
|
-
// )!;
|
36
|
-
// const outputPrice = safeGetPrice(
|
37
|
-
// toWeb3JsPublicKey(output.mint),
|
38
|
-
// this.priceType
|
39
|
-
// )!;
|
33
|
+
const inputPrice = (0, utils_1.safeGetPrice)((0, umi_web3js_adapters_1.toWeb3JsPublicKey)(input.mint), this.priceType);
|
34
|
+
const outputPrice = (0, utils_1.safeGetPrice)((0, umi_web3js_adapters_1.toWeb3JsPublicKey)(output.mint), this.priceType);
|
40
35
|
const supplyPrice = this.client.pos.supplyPrice(this.priceType);
|
41
36
|
const debtPrice = this.client.pos.debtPrice(this.priceType);
|
42
37
|
const biasedInputPrice = this.isBoost() ? debtPrice : supplyPrice;
|
43
38
|
const biasedOutputPrice = this.isBoost() ? supplyPrice : debtPrice;
|
44
|
-
const
|
45
|
-
const
|
46
|
-
let inputAmount = (0, utils_1.toBaseUnit)(this.usdToSwap() /
|
39
|
+
// const biasedInputPrice = inputPrice;
|
40
|
+
// const biasedOutputPrice = outputPrice;
|
41
|
+
let inputAmount = (0, utils_1.toBaseUnit)(this.usdToSwap() / biasedInputPrice, input.decimals);
|
47
42
|
return {
|
48
43
|
inputAmount,
|
49
44
|
input,
|
@@ -59,25 +54,22 @@ class RebalanceSwapManager {
|
|
59
54
|
let debtUsd = this.client.pos.debtUsd(this.priceType);
|
60
55
|
// TODO: add token balance change
|
61
56
|
const { input, biasedInputPrice, output, biasedOutputPrice } = this.swapDetails();
|
62
|
-
|
57
|
+
const swapInputUsd = swapInputAmount
|
63
58
|
? (0, utils_1.fromBaseUnit)(swapInputAmount, (0, utils_1.tokenInfo)((0, umi_web3js_adapters_1.toWeb3JsPublicKey)(input.mint)).decimals) * biasedInputPrice
|
64
59
|
: this.usdToSwap();
|
65
|
-
|
66
|
-
? swapInputUsd + swapInputUsd * (0, utils_1.fromBps)(this.flRequirements.flFeeBps)
|
67
|
-
: swapInputUsd;
|
68
|
-
if (this.isBoost() && this.client.pos.state.debt.borrowFeeBps) {
|
69
|
-
swapInputUsd +=
|
70
|
-
swapInputUsd * (0, utils_1.fromBps)(this.client.pos.state.debt.borrowFeeBps);
|
71
|
-
}
|
72
|
-
let swapOutputUsd = swapOutputAmount
|
60
|
+
const swapOutputUsd = swapOutputAmount
|
73
61
|
? (0, utils_1.fromBaseUnit)(swapOutputAmount, (0, utils_1.tokenInfo)((0, umi_web3js_adapters_1.toWeb3JsPublicKey)(output.mint)).decimals) * biasedOutputPrice
|
74
62
|
: this.usdToSwap();
|
75
|
-
|
76
|
-
|
77
|
-
?
|
78
|
-
:
|
79
|
-
|
80
|
-
|
63
|
+
console.log(swapInputUsd, swapOutputUsd);
|
64
|
+
const res = (0, rebalanceValues_1.applyDebtAdjustmentUsd)({
|
65
|
+
debtAdjustmentUsd: this.isBoost() ? swapInputUsd : swapInputUsd * -1,
|
66
|
+
debtAdjustmentUsdOutput: swapOutputUsd,
|
67
|
+
}, { supplyUsd, debtUsd }, (0, utils_1.fromBps)(this.client.pos.state.liqThresholdBps), {
|
68
|
+
solauto: this.solautoFeeBps,
|
69
|
+
flashLoan: this.flRequirements?.flFeeBps ?? 0,
|
70
|
+
lpBorrow: this.client.pos.state.debt.borrowFeeBps,
|
71
|
+
});
|
72
|
+
return (0, utils_1.getLiqUtilzationRateBps)(res.newPos.supplyUsd, res.newPos.debtUsd, this.client.pos.state.liqThresholdBps ?? 0);
|
81
73
|
}
|
82
74
|
async findSufficientQuote(swapInput, criteria) {
|
83
75
|
let swapQuote;
|
@@ -109,13 +101,13 @@ class RebalanceSwapManager {
|
|
109
101
|
}
|
110
102
|
async setSwapParams(attemptNum) {
|
111
103
|
const rebalanceToZero = this.targetLiqUtilizationRateBps === 0;
|
112
|
-
let { input, output,
|
104
|
+
let { input, output, biasedOutputPrice, inputAmount } = this.swapDetails();
|
113
105
|
let outputAmount = rebalanceToZero
|
114
106
|
? output.amountUsed.baseUnit +
|
115
107
|
BigInt(Math.round(Number(output.amountUsed.baseUnit) *
|
116
108
|
// Add this small percentage to account for the APR on the debt between now and the transaction
|
117
109
|
0.0001))
|
118
|
-
: (0, utils_1.toBaseUnit)(this.usdToSwap() /
|
110
|
+
: (0, utils_1.toBaseUnit)(this.usdToSwap() / biasedOutputPrice, output.decimals);
|
119
111
|
const flashLoanRepayFromDebt = !this.isBoost() &&
|
120
112
|
this.flRequirements &&
|
121
113
|
this.flRequirements.liquiditySource === generated_1.TokenType.Debt;
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"rebalanceTxBuilder.d.ts","sourceRoot":"","sources":["../../../src/services/rebalance/rebalanceTxBuilder.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAC3C,OAAO,EAGL,qBAAqB,EACtB,MAAM,aAAa,CAAC;AAiCrB,qBAAa,kBAAkB;IAQ3B,OAAO,CAAC,MAAM;IACd,OAAO,CAAC,2BAA2B,CAAC;IACpC,OAAO,CAAC,YAAY,CAAC;IATvB,OAAO,CAAC,MAAM,CAAmB;IACjC,OAAO,CAAC,aAAa,CAAwB;IAC7C,OAAO,CAAC,WAAW,CAAwB;IAC3C,OAAO,CAAC,cAAc,CAAC,CAAwB;IAC/C,OAAO,CAAC,SAAS,CAAiC;gBAGxC,MAAM,EAAE,aAAa,EACrB,2BAA2B,CAAC,EAAE,MAAM,YAAA,EACpC,YAAY,CAAC,EAAE,OAAO,YAAA;IAGhC,OAAO,CAAC,0BAA0B;IAclC,OAAO,CAAC,kBAAkB;IAc1B,OAAO,CAAC,oBAAoB;IAwC5B,OAAO,CAAC,iCAAiC;YAgC3B,qBAAqB;IA0CnC,OAAO,CAAC,mBAAmB;IAwB3B,OAAO,CAAC,gBAAgB;IA+BxB,OAAO,CAAC,yBAAyB;YAqBnB,mBAAmB;YAyBnB,sBAAsB;YAyBtB,mBAAmB;
|
1
|
+
{"version":3,"file":"rebalanceTxBuilder.d.ts","sourceRoot":"","sources":["../../../src/services/rebalance/rebalanceTxBuilder.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAC3C,OAAO,EAGL,qBAAqB,EACtB,MAAM,aAAa,CAAC;AAiCrB,qBAAa,kBAAkB;IAQ3B,OAAO,CAAC,MAAM;IACd,OAAO,CAAC,2BAA2B,CAAC;IACpC,OAAO,CAAC,YAAY,CAAC;IATvB,OAAO,CAAC,MAAM,CAAmB;IACjC,OAAO,CAAC,aAAa,CAAwB;IAC7C,OAAO,CAAC,WAAW,CAAwB;IAC3C,OAAO,CAAC,cAAc,CAAC,CAAwB;IAC/C,OAAO,CAAC,SAAS,CAAiC;gBAGxC,MAAM,EAAE,aAAa,EACrB,2BAA2B,CAAC,EAAE,MAAM,YAAA,EACpC,YAAY,CAAC,EAAE,OAAO,YAAA;IAGhC,OAAO,CAAC,0BAA0B;IAclC,OAAO,CAAC,kBAAkB;IAc1B,OAAO,CAAC,oBAAoB;IAwC5B,OAAO,CAAC,iCAAiC;YAgC3B,qBAAqB;IA0CnC,OAAO,CAAC,mBAAmB;IAwB3B,OAAO,CAAC,gBAAgB;IA+BxB,OAAO,CAAC,yBAAyB;YAqBnB,mBAAmB;YAyBnB,sBAAsB;YAyBtB,mBAAmB;IAsFpB,gBAAgB,CAC3B,UAAU,EAAE,MAAM,GACjB,OAAO,CAAC,qBAAqB,GAAG,SAAS,CAAC;CAe9C"}
|
@@ -52,7 +52,7 @@ class RebalanceTxBuilder {
|
|
52
52
|
return this.values.intermediaryLiqUtilizationRateBps;
|
53
53
|
}
|
54
54
|
const fees = new solautoFees_1.SolautoFeesBps(this.client.isReferred, this.targetLiqUtilizationRateBps, this.client.pos.netWorthUsd(generated_1.PriceType.Realtime));
|
55
|
-
const { intermediaryLiqUtilizationRateBps } = (0, rebalanceValues_1.applyDebtAdjustmentUsd)(this.values.debtAdjustmentUsd, {
|
55
|
+
const { intermediaryLiqUtilizationRateBps } = (0, rebalanceValues_1.applyDebtAdjustmentUsd)({ debtAdjustmentUsd: this.values.debtAdjustmentUsd }, {
|
56
56
|
supplyUsd: this.client.pos.supplyUsd(generated_1.PriceType.Ema),
|
57
57
|
debtUsd: this.client.pos.debtUsd(generated_1.PriceType.Ema),
|
58
58
|
}, (0, utils_1.fromBps)(this.client.pos.state.liqThresholdBps), {
|
@@ -199,10 +199,12 @@ class RebalanceTxBuilder {
|
|
199
199
|
priceType: this.priceType,
|
200
200
|
};
|
201
201
|
(0, utils_1.consoleLog)("Rebalance details:", rebalanceDetails);
|
202
|
-
(0, utils_1.consoleLog)("Prices:", this.client.pos.supplyPrice(this.priceType), this.client.pos.debtPrice(this.priceType));
|
202
|
+
(0, utils_1.consoleLog)("Prices:", (0, utils_1.safeGetPrice)(this.client.pos.supplyMint, this.priceType), this.client.pos.supplyPrice(this.priceType), (0, utils_1.safeGetPrice)(this.client.pos.debtMint, this.priceType), this.client.pos.debtPrice(this.priceType));
|
203
203
|
if ((0, utils_1.isMarginfiPosition)(this.client.pos)) {
|
204
|
-
const supply = this.values.endResult.supplyUsd *
|
205
|
-
|
204
|
+
const supply = this.values.endResult.supplyUsd *
|
205
|
+
(0, utils_1.bytesToI80F48)(this.client.pos.supplyBank.config.assetWeightInit.value);
|
206
|
+
const debt = this.values.endResult.debtUsd *
|
207
|
+
(0, utils_1.bytesToI80F48)(this.client.pos.debtBank.config.liabilityWeightInit.value);
|
206
208
|
(0, utils_1.consoleLog)("Weighted values", supply, debt);
|
207
209
|
}
|
208
210
|
const firstRebalance = this.client.rebalanceIx(generated_1.RebalanceStep.PreSwap, rebalanceDetails);
|
@@ -19,7 +19,10 @@ interface ApplyDebtAdjustmentResult {
|
|
19
19
|
newPos: PositionValues;
|
20
20
|
intermediaryLiqUtilizationRateBps: number;
|
21
21
|
}
|
22
|
-
export declare function applyDebtAdjustmentUsd(
|
22
|
+
export declare function applyDebtAdjustmentUsd(adjustment: {
|
23
|
+
debtAdjustmentUsd: number;
|
24
|
+
debtAdjustmentUsdOutput?: number;
|
25
|
+
}, pos: PositionValues, liqThreshold: number, fees?: RebalanceFeesBps): ApplyDebtAdjustmentResult;
|
23
26
|
export declare function getDebtAdjustment(liqThresholdBps: number, pos: PositionValues, targetLiqUtilizationRateBps: number, fees?: RebalanceFeesBps): DebtAdjustment;
|
24
27
|
export interface RebalanceValues extends DebtAdjustment {
|
25
28
|
rebalanceDirection: RebalanceDirection;
|
@@ -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,iCAAiC,EAAE,MAAM,CAAC;CAC3C;AAED,MAAM,WAAW,gBAAgB;IAC/B,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,MAAM,CAAC;IACjB,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,UAAU,yBAAyB;IACjC,MAAM,EAAE,cAAc,CAAC;IACvB,iCAAiC,EAAE,MAAM,CAAC;CAC3C;AAED,wBAAgB,sBAAsB,CACpC,iBAAiB,EAAE,MAAM,
|
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,iCAAiC,EAAE,MAAM,CAAC;CAC3C;AAED,MAAM,WAAW,gBAAgB;IAC/B,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,MAAM,CAAC;IACjB,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,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,CAgChB;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"}
|
@@ -5,11 +5,15 @@ exports.getDebtAdjustment = getDebtAdjustment;
|
|
5
5
|
exports.getRebalanceValues = getRebalanceValues;
|
6
6
|
const generated_1 = require("../../generated");
|
7
7
|
const utils_1 = require("../../utils");
|
8
|
-
function applyDebtAdjustmentUsd(
|
8
|
+
function applyDebtAdjustmentUsd(adjustment, pos, liqThreshold, fees) {
|
9
9
|
const newPos = { ...pos };
|
10
|
-
const isBoost = debtAdjustmentUsd > 0;
|
11
|
-
|
12
|
-
|
10
|
+
const isBoost = adjustment.debtAdjustmentUsd > 0;
|
11
|
+
if (!adjustment.debtAdjustmentUsdOutput) {
|
12
|
+
adjustment.debtAdjustmentUsdOutput = adjustment.debtAdjustmentUsd;
|
13
|
+
}
|
14
|
+
const daMinusSolautoFees = adjustment.debtAdjustmentUsdOutput -
|
15
|
+
adjustment.debtAdjustmentUsdOutput * (0, utils_1.fromBps)(fees?.solauto ?? 0);
|
16
|
+
const daWithFlashLoan = adjustment.debtAdjustmentUsd * (1.0 + (0, utils_1.fromBps)(fees?.flashLoan ?? 0));
|
13
17
|
let intermediaryLiqUtilizationRateBps = 0;
|
14
18
|
if (isBoost) {
|
15
19
|
newPos.debtUsd +=
|
@@ -40,7 +44,7 @@ function getDebtAdjustment(liqThresholdBps, pos, targetLiqUtilizationRateBps, fe
|
|
40
44
|
targetUtilizationRate * actualizedFee * liqThreshold)
|
41
45
|
: (targetUtilizationRate * liqThreshold * pos.supplyUsd - pos.debtUsd) /
|
42
46
|
(actualizedFee - targetUtilizationRate * liqThreshold * (1.0 + flFee));
|
43
|
-
const newPos = applyDebtAdjustmentUsd(debtAdjustmentUsd, pos, liqThreshold, fees);
|
47
|
+
const newPos = applyDebtAdjustmentUsd({ debtAdjustmentUsd }, pos, liqThreshold, fees);
|
44
48
|
return {
|
45
49
|
debtAdjustmentUsd,
|
46
50
|
endResult: newPos.newPos,
|
package/package.json
CHANGED
@@ -3,7 +3,7 @@ import { toWeb3JsPublicKey } from "@metaplex-foundation/umi-web3js-adapters";
|
|
3
3
|
import { FlashLoanRequirements } from "../../types";
|
4
4
|
import { SolautoClient } from "../solauto";
|
5
5
|
import { JupSwapManager, SwapParams, SwapInput } from "../swap";
|
6
|
-
import { RebalanceValues } from "./rebalanceValues";
|
6
|
+
import { applyDebtAdjustmentUsd, RebalanceValues } from "./rebalanceValues";
|
7
7
|
import { PriceType, RebalanceDirection, TokenType } from "../../generated";
|
8
8
|
import {
|
9
9
|
consoleLog,
|
@@ -55,25 +55,25 @@ export class RebalanceSwapManager {
|
|
55
55
|
? this.client.pos.state.supply
|
56
56
|
: this.client.pos.state.debt;
|
57
57
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
58
|
+
const inputPrice = safeGetPrice(
|
59
|
+
toWeb3JsPublicKey(input.mint),
|
60
|
+
this.priceType
|
61
|
+
)!;
|
62
|
+
const outputPrice = safeGetPrice(
|
63
|
+
toWeb3JsPublicKey(output.mint),
|
64
|
+
this.priceType
|
65
|
+
)!;
|
66
66
|
|
67
67
|
const supplyPrice = this.client.pos.supplyPrice(this.priceType)!;
|
68
68
|
const debtPrice = this.client.pos.debtPrice(this.priceType)!;
|
69
69
|
const biasedInputPrice = this.isBoost() ? debtPrice : supplyPrice;
|
70
70
|
const biasedOutputPrice = this.isBoost() ? supplyPrice : debtPrice;
|
71
71
|
|
72
|
-
const
|
73
|
-
const
|
72
|
+
// const biasedInputPrice = inputPrice;
|
73
|
+
// const biasedOutputPrice = outputPrice;
|
74
74
|
|
75
75
|
let inputAmount = toBaseUnit(
|
76
|
-
this.usdToSwap() /
|
76
|
+
this.usdToSwap() / biasedInputPrice!,
|
77
77
|
input.decimals
|
78
78
|
);
|
79
79
|
|
@@ -99,36 +99,38 @@ export class RebalanceSwapManager {
|
|
99
99
|
const { input, biasedInputPrice, output, biasedOutputPrice } =
|
100
100
|
this.swapDetails();
|
101
101
|
|
102
|
-
|
102
|
+
const swapInputUsd = swapInputAmount
|
103
103
|
? fromBaseUnit(
|
104
104
|
swapInputAmount,
|
105
105
|
tokenInfo(toWeb3JsPublicKey(input.mint)).decimals
|
106
106
|
) * biasedInputPrice
|
107
107
|
: this.usdToSwap();
|
108
|
-
swapInputUsd = this.flRequirements?.flFeeBps
|
109
|
-
? swapInputUsd + swapInputUsd * fromBps(this.flRequirements.flFeeBps)
|
110
|
-
: swapInputUsd;
|
111
|
-
if (this.isBoost() && this.client.pos.state.debt.borrowFeeBps) {
|
112
|
-
swapInputUsd +=
|
113
|
-
swapInputUsd * fromBps(this.client.pos.state.debt.borrowFeeBps);
|
114
|
-
}
|
115
108
|
|
116
|
-
|
109
|
+
const swapOutputUsd = swapOutputAmount
|
117
110
|
? fromBaseUnit(
|
118
111
|
swapOutputAmount,
|
119
112
|
tokenInfo(toWeb3JsPublicKey(output.mint)).decimals
|
120
113
|
) * biasedOutputPrice
|
121
114
|
: this.usdToSwap();
|
122
|
-
swapOutputUsd = swapOutputUsd - swapOutputUsd * fromBps(this.solautoFeeBps);
|
123
115
|
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
116
|
+
console.log(swapInputUsd, swapOutputUsd);
|
117
|
+
const res = applyDebtAdjustmentUsd(
|
118
|
+
{
|
119
|
+
debtAdjustmentUsd: this.isBoost() ? swapInputUsd : swapInputUsd * -1,
|
120
|
+
debtAdjustmentUsdOutput: swapOutputUsd,
|
121
|
+
},
|
122
|
+
{ supplyUsd, debtUsd },
|
123
|
+
fromBps(this.client.pos.state.liqThresholdBps),
|
124
|
+
{
|
125
|
+
solauto: this.solautoFeeBps,
|
126
|
+
flashLoan: this.flRequirements?.flFeeBps ?? 0,
|
127
|
+
lpBorrow: this.client.pos.state.debt.borrowFeeBps,
|
128
|
+
}
|
129
|
+
);
|
128
130
|
|
129
131
|
return getLiqUtilzationRateBps(
|
130
|
-
supplyUsd,
|
131
|
-
debtUsd,
|
132
|
+
res.newPos.supplyUsd,
|
133
|
+
res.newPos.debtUsd,
|
132
134
|
this.client.pos.state.liqThresholdBps ?? 0
|
133
135
|
);
|
134
136
|
}
|
@@ -182,7 +184,7 @@ export class RebalanceSwapManager {
|
|
182
184
|
|
183
185
|
async setSwapParams(attemptNum: number) {
|
184
186
|
const rebalanceToZero = this.targetLiqUtilizationRateBps === 0;
|
185
|
-
let { input, output,
|
187
|
+
let { input, output, biasedOutputPrice, inputAmount } = this.swapDetails();
|
186
188
|
|
187
189
|
let outputAmount = rebalanceToZero
|
188
190
|
? output.amountUsed.baseUnit +
|
@@ -193,7 +195,7 @@ export class RebalanceSwapManager {
|
|
193
195
|
0.0001
|
194
196
|
)
|
195
197
|
)
|
196
|
-
: toBaseUnit(this.usdToSwap() /
|
198
|
+
: toBaseUnit(this.usdToSwap() / biasedOutputPrice, output.decimals);
|
197
199
|
|
198
200
|
const flashLoanRepayFromDebt =
|
199
201
|
!this.isBoost() &&
|
@@ -136,7 +136,7 @@ export class RebalanceTxBuilder {
|
|
136
136
|
);
|
137
137
|
|
138
138
|
const { intermediaryLiqUtilizationRateBps } = applyDebtAdjustmentUsd(
|
139
|
-
this.values.debtAdjustmentUsd,
|
139
|
+
{ debtAdjustmentUsd: this.values.debtAdjustmentUsd },
|
140
140
|
{
|
141
141
|
supplyUsd: this.client.pos.supplyUsd(PriceType.Ema),
|
142
142
|
debtUsd: this.client.pos.debtUsd(PriceType.Ema),
|
@@ -345,13 +345,21 @@ export class RebalanceTxBuilder {
|
|
345
345
|
consoleLog("Rebalance details:", rebalanceDetails);
|
346
346
|
consoleLog(
|
347
347
|
"Prices:",
|
348
|
+
safeGetPrice(this.client.pos.supplyMint, this.priceType),
|
348
349
|
this.client.pos.supplyPrice(this.priceType),
|
350
|
+
safeGetPrice(this.client.pos.debtMint, this.priceType),
|
349
351
|
this.client.pos.debtPrice(this.priceType)
|
350
352
|
);
|
351
353
|
|
352
354
|
if (isMarginfiPosition(this.client.pos)) {
|
353
|
-
const supply =
|
354
|
-
|
355
|
+
const supply =
|
356
|
+
this.values.endResult.supplyUsd *
|
357
|
+
bytesToI80F48(this.client.pos.supplyBank!.config.assetWeightInit.value);
|
358
|
+
const debt =
|
359
|
+
this.values.endResult.debtUsd *
|
360
|
+
bytesToI80F48(
|
361
|
+
this.client.pos.debtBank!.config.liabilityWeightInit.value
|
362
|
+
);
|
355
363
|
consoleLog("Weighted values", supply, debt);
|
356
364
|
}
|
357
365
|
|
@@ -38,18 +38,23 @@ interface ApplyDebtAdjustmentResult {
|
|
38
38
|
}
|
39
39
|
|
40
40
|
export function applyDebtAdjustmentUsd(
|
41
|
-
debtAdjustmentUsd: number,
|
41
|
+
adjustment: { debtAdjustmentUsd: number; debtAdjustmentUsdOutput?: number },
|
42
42
|
pos: PositionValues,
|
43
43
|
liqThreshold: number,
|
44
44
|
fees?: RebalanceFeesBps
|
45
45
|
): ApplyDebtAdjustmentResult {
|
46
46
|
const newPos = { ...pos };
|
47
|
-
const isBoost = debtAdjustmentUsd > 0;
|
47
|
+
const isBoost = adjustment.debtAdjustmentUsd > 0;
|
48
48
|
|
49
|
+
if (!adjustment.debtAdjustmentUsdOutput) {
|
50
|
+
adjustment.debtAdjustmentUsdOutput = adjustment.debtAdjustmentUsd;
|
51
|
+
}
|
49
52
|
const daMinusSolautoFees =
|
50
|
-
|
53
|
+
adjustment.debtAdjustmentUsdOutput -
|
54
|
+
adjustment.debtAdjustmentUsdOutput * fromBps(fees?.solauto ?? 0);
|
55
|
+
|
51
56
|
const daWithFlashLoan =
|
52
|
-
debtAdjustmentUsd * (1.0 + fromBps(fees?.flashLoan ?? 0));
|
57
|
+
adjustment.debtAdjustmentUsd * (1.0 + fromBps(fees?.flashLoan ?? 0));
|
53
58
|
|
54
59
|
let intermediaryLiqUtilizationRateBps = 0;
|
55
60
|
if (isBoost) {
|
@@ -100,7 +105,7 @@ export function getDebtAdjustment(
|
|
100
105
|
(actualizedFee - targetUtilizationRate * liqThreshold * (1.0 + flFee));
|
101
106
|
|
102
107
|
const newPos = applyDebtAdjustmentUsd(
|
103
|
-
debtAdjustmentUsd,
|
108
|
+
{ debtAdjustmentUsd },
|
104
109
|
pos,
|
105
110
|
liqThreshold,
|
106
111
|
fees
|