@haven-fi/solauto-sdk 1.0.702 → 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.
@@ -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,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,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;YA6C5B,mBAAmB;IA2CjC,OAAO,CAAC,mBAAmB;IAIrB,aAAa,CAAC,UAAU,EAAE,MAAM;IAyEhC,aAAa;;;;;;CAWpB"}
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"}
@@ -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,17 @@ class RebalanceSwapManager {
29
30
  const output = this.isBoost()
30
31
  ? this.client.pos.state.supply
31
32
  : this.client.pos.state.debt;
32
- // const inputPrice = safeGetPrice(
33
- // toWeb3JsPublicKey(input.mint),
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 inputPrice = biasedInputPrice;
45
- const outputPrice = biasedOutputPrice;
46
- let inputAmount = (0, utils_1.toBaseUnit)(this.usdToSwap() / inputPrice, input.decimals);
39
+ // const biasedInputPrice = inputPrice;
40
+ // const biasedOutputPrice = outputPrice;
41
+ // const priceDiff = (biasedInputPrice - inputPrice) / 2;
42
+ // const weightedInputPrice = inputPrice + priceDiff;
43
+ let inputAmount = (0, utils_1.toBaseUnit)(this.usdToSwap() / biasedInputPrice, input.decimals);
47
44
  return {
48
45
  inputAmount,
49
46
  input,
@@ -54,30 +51,39 @@ class RebalanceSwapManager {
54
51
  biasedOutputPrice,
55
52
  };
56
53
  }
57
- postRebalanceLiqUtilizationRateBps(swapOutputAmount, swapInputAmount) {
54
+ postRebalanceLiqUtilizationRateBps(swapOutputAmountBaseUnit, swapInputAmountBaseUnit) {
58
55
  let supplyUsd = this.client.pos.supplyUsd(this.priceType);
59
56
  let debtUsd = this.client.pos.debtUsd(this.priceType);
60
57
  // TODO: add token balance change
61
- const { input, biasedInputPrice, output, biasedOutputPrice } = this.swapDetails();
62
- let swapInputUsd = swapInputAmount
63
- ? (0, utils_1.fromBaseUnit)(swapInputAmount, (0, utils_1.tokenInfo)((0, umi_web3js_adapters_1.toWeb3JsPublicKey)(input.mint)).decimals) * biasedInputPrice
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;
65
+ const swapInputUsd = swapInputAmount
66
+ ? swapInputAmount * biasedInputPrice
64
67
  : this.usdToSwap();
65
- swapInputUsd = this.flRequirements?.flFeeBps
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
73
- ? (0, utils_1.fromBaseUnit)(swapOutputAmount, (0, utils_1.tokenInfo)((0, umi_web3js_adapters_1.toWeb3JsPublicKey)(output.mint)).decimals) * biasedOutputPrice
68
+ const swapOutputUsd = swapOutputAmount
69
+ ? swapOutputAmount * biasedOutputPrice
74
70
  : this.usdToSwap();
75
- swapOutputUsd = swapOutputUsd - swapOutputUsd * (0, utils_1.fromBps)(this.solautoFeeBps);
76
- supplyUsd = this.isBoost()
77
- ? supplyUsd + swapOutputUsd
78
- : supplyUsd - swapInputUsd;
79
- debtUsd = this.isBoost() ? debtUsd + swapInputUsd : debtUsd - swapOutputUsd;
80
- return (0, utils_1.getLiqUtilzationRateBps)(supplyUsd, debtUsd, this.client.pos.state.liqThresholdBps ?? 0);
71
+ console.log((swapInputAmount ?? 0) * inputPrice, swapInputUsd, (swapOutputAmount ?? 0) * outputPrice, swapOutputUsd);
72
+ const res = (0, rebalanceValues_1.applyDebtAdjustmentUsd)({
73
+ debtAdjustmentUsd: this.isBoost() ? swapInputUsd : swapInputUsd * -1,
74
+ debtAdjustmentUsdOutput: swapOutputUsd,
75
+ }, { supplyUsd, debtUsd }, (0, utils_1.fromBps)(this.client.pos.state.liqThresholdBps), {
76
+ solauto: this.solautoFeeBps,
77
+ flashLoan: this.flRequirements?.flFeeBps ?? 0,
78
+ lpBorrow: this.client.pos.state.debt.borrowFeeBps,
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
+ }
86
+ return (0, utils_1.getLiqUtilzationRateBps)(res.newPos.supplyUsd, res.newPos.debtUsd, this.client.pos.state.liqThresholdBps ?? 0);
81
87
  }
82
88
  async findSufficientQuote(swapInput, criteria) {
83
89
  let swapQuote;
@@ -95,7 +101,7 @@ class RebalanceSwapManager {
95
101
  (0, utils_1.consoleLog)(postRebalanceRate, criteria.maxLiqUtilizationRateBps);
96
102
  if (insufficient) {
97
103
  (0, utils_1.consoleLog)("Insufficient swap quote:", swapQuote);
98
- const increment = 0.01 + i * 0.001;
104
+ const increment = 0.01 + i * 0.01;
99
105
  swapInput.amount = this.bigIntWithIncrement(swapInput.amount, this.isBoost() ? increment * -1 : increment);
100
106
  }
101
107
  else {
@@ -109,13 +115,13 @@ class RebalanceSwapManager {
109
115
  }
110
116
  async setSwapParams(attemptNum) {
111
117
  const rebalanceToZero = this.targetLiqUtilizationRateBps === 0;
112
- let { input, output, outputPrice, inputAmount } = this.swapDetails();
118
+ let { input, output, biasedOutputPrice, inputAmount } = this.swapDetails();
113
119
  let outputAmount = rebalanceToZero
114
120
  ? output.amountUsed.baseUnit +
115
121
  BigInt(Math.round(Number(output.amountUsed.baseUnit) *
116
122
  // Add this small percentage to account for the APR on the debt between now and the transaction
117
123
  0.0001))
118
- : (0, utils_1.toBaseUnit)(this.usdToSwap() / outputPrice, output.decimals);
124
+ : (0, utils_1.toBaseUnit)(this.usdToSwap() / biasedOutputPrice, output.decimals);
119
125
  const flashLoanRepayFromDebt = !this.isBoost() &&
120
126
  this.flRequirements &&
121
127
  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;IA8EpB,gBAAgB,CAC3B,UAAU,EAAE,MAAM,GACjB,OAAO,CAAC,qBAAqB,GAAG,SAAS,CAAC;CAe9C"}
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 * (0, utils_1.bytesToI80F48)(this.client.pos.supplyBank.config.assetWeightInit.value);
205
- const debt = this.values.endResult.debtUsd * (0, utils_1.bytesToI80F48)(this.client.pos.debtBank.config.liabilityWeightInit.value);
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(debtAdjustmentUsd: number, pos: PositionValues, liqThreshold: number, fees?: RebalanceFeesBps): ApplyDebtAdjustmentResult;
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,EACzB,GAAG,EAAE,cAAc,EACnB,YAAY,EAAE,MAAM,EACpB,IAAI,CAAC,EAAE,gBAAgB,GACtB,yBAAyB,CA8B3B;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"}
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,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"}
@@ -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(debtAdjustmentUsd, pos, liqThreshold, fees) {
8
+ function applyDebtAdjustmentUsd(adjustment, pos, liqThreshold, fees) {
9
9
  const newPos = { ...pos };
10
- const isBoost = debtAdjustmentUsd > 0;
11
- const daMinusSolautoFees = debtAdjustmentUsd - debtAdjustmentUsd * (0, utils_1.fromBps)(fees?.solauto ?? 0);
12
- const daWithFlashLoan = debtAdjustmentUsd * (1.0 + (0, utils_1.fromBps)(fees?.flashLoan ?? 0));
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,11 +44,12 @@ 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 endResult = applyDebtAdjustmentUsd({ debtAdjustmentUsd }, pos, liqThreshold, fees);
48
+ console.log(debtAdjustmentUsd, pos, endResult.newPos);
44
49
  return {
45
50
  debtAdjustmentUsd,
46
- endResult: newPos.newPos,
47
- intermediaryLiqUtilizationRateBps: newPos.intermediaryLiqUtilizationRateBps,
51
+ endResult: endResult.newPos,
52
+ intermediaryLiqUtilizationRateBps: endResult.intermediaryLiqUtilizationRateBps,
48
53
  };
49
54
  }
50
55
  function getTokenBalanceChange() {
@@ -25,7 +25,7 @@ class JupSwapManager {
25
25
  : undefined,
26
26
  slippageBps,
27
27
  maxAccounts: !data.exactOut ? (memeSwap ? 25 : 15) + attemptNum * 5 : undefined,
28
- }), 4, 150);
28
+ }), 6, 250);
29
29
  }
30
30
  async getJupInstructions(data) {
31
31
  if (!this.jupQuote) {
@@ -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(50, client.pos.supplyMintInfo.decimals)),
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@haven-fi/solauto-sdk",
3
- "version": "1.0.702",
3
+ "version": "1.0.704",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "description": "Typescript SDK for the Solauto program on the Solana blockchain",
@@ -3,13 +3,15 @@ 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
+ bytesToI80F48,
9
10
  consoleLog,
10
11
  fromBaseUnit,
11
12
  fromBps,
12
13
  getLiqUtilzationRateBps,
14
+ isMarginfiPosition,
13
15
  safeGetPrice,
14
16
  toBaseUnit,
15
17
  tokenInfo,
@@ -55,25 +57,28 @@ export class RebalanceSwapManager {
55
57
  ? this.client.pos.state.supply
56
58
  : this.client.pos.state.debt;
57
59
 
58
- // const inputPrice = safeGetPrice(
59
- // toWeb3JsPublicKey(input.mint),
60
- // this.priceType
61
- // )!;
62
- // const outputPrice = safeGetPrice(
63
- // toWeb3JsPublicKey(output.mint),
64
- // this.priceType
65
- // )!;
60
+ const inputPrice = safeGetPrice(
61
+ toWeb3JsPublicKey(input.mint),
62
+ this.priceType
63
+ )!;
64
+ const outputPrice = safeGetPrice(
65
+ toWeb3JsPublicKey(output.mint),
66
+ this.priceType
67
+ )!;
66
68
 
67
69
  const supplyPrice = this.client.pos.supplyPrice(this.priceType)!;
68
70
  const debtPrice = this.client.pos.debtPrice(this.priceType)!;
69
71
  const biasedInputPrice = this.isBoost() ? debtPrice : supplyPrice;
70
72
  const biasedOutputPrice = this.isBoost() ? supplyPrice : debtPrice;
71
73
 
72
- const inputPrice = biasedInputPrice;
73
- const outputPrice = biasedOutputPrice;
74
+ // const biasedInputPrice = inputPrice;
75
+ // const biasedOutputPrice = outputPrice;
76
+
77
+ // const priceDiff = (biasedInputPrice - inputPrice) / 2;
78
+ // const weightedInputPrice = inputPrice + priceDiff;
74
79
 
75
80
  let inputAmount = toBaseUnit(
76
- this.usdToSwap() / inputPrice!,
81
+ this.usdToSwap() / biasedInputPrice!,
77
82
  input.decimals
78
83
  );
79
84
 
@@ -89,46 +94,82 @@ export class RebalanceSwapManager {
89
94
  }
90
95
 
91
96
  private postRebalanceLiqUtilizationRateBps(
92
- swapOutputAmount?: bigint,
93
- swapInputAmount?: bigint
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 { input, biasedInputPrice, output, biasedOutputPrice } =
100
- this.swapDetails();
104
+ const {
105
+ input,
106
+ inputPrice,
107
+ biasedInputPrice,
108
+ output,
109
+ outputPrice,
110
+ biasedOutputPrice,
111
+ } = this.swapDetails();
101
112
 
102
- let swapInputUsd = swapInputAmount
113
+ const swapInputAmount = swapInputAmountBaseUnit
103
114
  ? fromBaseUnit(
104
- swapInputAmount,
115
+ swapInputAmountBaseUnit,
105
116
  tokenInfo(toWeb3JsPublicKey(input.mint)).decimals
106
- ) * biasedInputPrice
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
- }
117
+ )
118
+ : undefined;
115
119
 
116
- let swapOutputUsd = swapOutputAmount
120
+ const swapOutputAmount = swapOutputAmountBaseUnit
117
121
  ? fromBaseUnit(
118
- swapOutputAmount,
122
+ swapOutputAmountBaseUnit,
119
123
  tokenInfo(toWeb3JsPublicKey(output.mint)).decimals
120
- ) * biasedOutputPrice
124
+ )
125
+ : undefined;
126
+
127
+ const swapInputUsd = swapInputAmount
128
+ ? swapInputAmount * biasedInputPrice
129
+ : this.usdToSwap();
130
+
131
+ const swapOutputUsd = swapOutputAmount
132
+ ? swapOutputAmount * biasedOutputPrice
121
133
  : this.usdToSwap();
122
- swapOutputUsd = swapOutputUsd - swapOutputUsd * fromBps(this.solautoFeeBps);
123
134
 
124
- supplyUsd = this.isBoost()
125
- ? supplyUsd + swapOutputUsd
126
- : supplyUsd - swapInputUsd;
127
- debtUsd = this.isBoost() ? debtUsd + swapInputUsd : debtUsd - swapOutputUsd;
135
+ console.log(
136
+ (swapInputAmount ?? 0) * inputPrice,
137
+ swapInputUsd,
138
+ (swapOutputAmount ?? 0) * outputPrice,
139
+ swapOutputUsd
140
+ );
141
+
142
+ const res = applyDebtAdjustmentUsd(
143
+ {
144
+ debtAdjustmentUsd: this.isBoost() ? swapInputUsd : swapInputUsd * -1,
145
+ debtAdjustmentUsdOutput: swapOutputUsd,
146
+ },
147
+ { supplyUsd, debtUsd },
148
+ fromBps(this.client.pos.state.liqThresholdBps),
149
+ {
150
+ solauto: this.solautoFeeBps,
151
+ flashLoan: this.flRequirements?.flFeeBps ?? 0,
152
+ lpBorrow: this.client.pos.state.debt.borrowFeeBps,
153
+ }
154
+ );
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
+ }
128
169
 
129
170
  return getLiqUtilzationRateBps(
130
- supplyUsd,
131
- debtUsd,
171
+ res.newPos.supplyUsd,
172
+ res.newPos.debtUsd,
132
173
  this.client.pos.state.liqThresholdBps ?? 0
133
174
  );
134
175
  }
@@ -163,7 +204,7 @@ export class RebalanceSwapManager {
163
204
  if (insufficient) {
164
205
  consoleLog("Insufficient swap quote:", swapQuote);
165
206
 
166
- const increment = 0.01 + i * 0.001;
207
+ const increment = 0.01 + i * 0.01;
167
208
  swapInput.amount = this.bigIntWithIncrement(
168
209
  swapInput.amount,
169
210
  this.isBoost() ? increment * -1 : increment
@@ -182,7 +223,7 @@ export class RebalanceSwapManager {
182
223
 
183
224
  async setSwapParams(attemptNum: number) {
184
225
  const rebalanceToZero = this.targetLiqUtilizationRateBps === 0;
185
- let { input, output, outputPrice, inputAmount } = this.swapDetails();
226
+ let { input, output, biasedOutputPrice, inputAmount } = this.swapDetails();
186
227
 
187
228
  let outputAmount = rebalanceToZero
188
229
  ? output.amountUsed.baseUnit +
@@ -193,7 +234,7 @@ export class RebalanceSwapManager {
193
234
  0.0001
194
235
  )
195
236
  )
196
- : toBaseUnit(this.usdToSwap() / outputPrice, output.decimals);
237
+ : toBaseUnit(this.usdToSwap() / biasedOutputPrice, output.decimals);
197
238
 
198
239
  const flashLoanRepayFromDebt =
199
240
  !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 = this.values.endResult.supplyUsd * bytesToI80F48(this.client.pos.supplyBank!.config.assetWeightInit.value);
354
- const debt = this.values.endResult.debtUsd * bytesToI80F48(this.client.pos.debtBank!.config.liabilityWeightInit.value);
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
- debtAdjustmentUsd - debtAdjustmentUsd * fromBps(fees?.solauto ?? 0);
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) {
@@ -99,17 +104,18 @@ export function getDebtAdjustment(
99
104
  : (targetUtilizationRate * liqThreshold * pos.supplyUsd - pos.debtUsd) /
100
105
  (actualizedFee - targetUtilizationRate * liqThreshold * (1.0 + flFee));
101
106
 
102
- const newPos = applyDebtAdjustmentUsd(
103
- debtAdjustmentUsd,
107
+ const endResult = applyDebtAdjustmentUsd(
108
+ { debtAdjustmentUsd },
104
109
  pos,
105
110
  liqThreshold,
106
111
  fees
107
112
  );
113
+ console.log(debtAdjustmentUsd, pos, endResult.newPos);
108
114
 
109
115
  return {
110
116
  debtAdjustmentUsd,
111
- endResult: newPos.newPos,
112
- intermediaryLiqUtilizationRateBps: newPos.intermediaryLiqUtilizationRateBps,
117
+ endResult: endResult.newPos,
118
+ intermediaryLiqUtilizationRateBps: endResult.intermediaryLiqUtilizationRateBps,
113
119
  };
114
120
  }
115
121
 
@@ -71,8 +71,8 @@ export class JupSwapManager {
71
71
  slippageBps,
72
72
  maxAccounts: !data.exactOut ? (memeSwap ? 25 : 15) + attemptNum * 5 : undefined,
73
73
  }),
74
- 4,
75
- 150
74
+ 6,
75
+ 250
76
76
  );
77
77
  }
78
78