@haven-fi/solauto-sdk 1.0.695 → 1.0.697
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 +1 -1
- package/dist/services/rebalance/rebalanceSwapManager.d.ts.map +1 -1
- package/dist/services/rebalance/rebalanceSwapManager.js +24 -21
- package/dist/utils/priceUtils.d.ts.map +1 -1
- package/dist/utils/priceUtils.js +5 -3
- package/package.json +1 -1
- package/src/services/rebalance/rebalanceSwapManager.ts +35 -31
- package/src/utils/priceUtils.ts +6 -3
@@ -17,9 +17,9 @@ export declare class RebalanceSwapManager {
|
|
17
17
|
constructor(client: SolautoClient, values: RebalanceValues, flRequirements?: FlashLoanRequirements | undefined, targetLiqUtilizationRateBps?: number | undefined, priceType?: PriceType | undefined);
|
18
18
|
private isBoost;
|
19
19
|
private usdToSwap;
|
20
|
+
private swapDetails;
|
20
21
|
private postRebalanceLiqUtilizationRateBps;
|
21
22
|
private findSufficientQuote;
|
22
|
-
private swapDetails;
|
23
23
|
private bigIntWithIncrement;
|
24
24
|
setSwapParams(attemptNum: number): Promise<void>;
|
25
25
|
getSwapTxData(): Promise<{
|
@@ -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;
|
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;AAS3E,qBAAa,oBAAoB;IAQ7B,OAAO,CAAC,MAAM;IACd,OAAO,CAAC,MAAM;IACd,OAAO,CAAC,cAAc,CAAC;IACvB,OAAO,CAAC,2BAA2B,CAAC;IACpC,OAAO,CAAC,SAAS,CAAC;IAXb,UAAU,EAAG,UAAU,CAAC;IACxB,SAAS,CAAC,EAAE,aAAa,CAAC;IAC1B,cAAc,CAAC,EAAE,MAAM,CAAC;IAE/B,OAAO,CAAC,cAAc,CAAkB;gBAG9B,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;IAK/B,OAAO,CAAC,OAAO;IAIf,OAAO,CAAC,SAAS;IAIjB,OAAO,CAAC,WAAW;IA2BnB,OAAO,CAAC,kCAAkC;YA4B5B,mBAAmB;IAoCjC,OAAO,CAAC,mBAAmB;IAIrB,aAAa,CAAC,UAAU,EAAE,MAAM;IAyEhC,aAAa;;;;;;CAWpB"}
|
@@ -20,16 +20,33 @@ class RebalanceSwapManager {
|
|
20
20
|
usdToSwap() {
|
21
21
|
return Math.abs(this.values.debtAdjustmentUsd);
|
22
22
|
}
|
23
|
+
swapDetails() {
|
24
|
+
const input = this.isBoost()
|
25
|
+
? this.client.pos.state.debt
|
26
|
+
: this.client.pos.state.supply;
|
27
|
+
const output = this.isBoost()
|
28
|
+
? this.client.pos.state.supply
|
29
|
+
: this.client.pos.state.debt;
|
30
|
+
const supplyPrice = this.client.pos.supplyPrice(this.priceType);
|
31
|
+
const debtPrice = this.client.pos.debtPrice(this.priceType);
|
32
|
+
const inputPrice = this.isBoost() ? debtPrice : supplyPrice;
|
33
|
+
const outputPrice = this.isBoost() ? supplyPrice : debtPrice;
|
34
|
+
let inputAmount = (0, utils_1.toBaseUnit)(this.usdToSwap() / inputPrice, input.decimals);
|
35
|
+
return {
|
36
|
+
inputAmount,
|
37
|
+
input,
|
38
|
+
inputPrice,
|
39
|
+
output,
|
40
|
+
outputPrice,
|
41
|
+
};
|
42
|
+
}
|
23
43
|
postRebalanceLiqUtilizationRateBps(swapOutputAmount) {
|
24
44
|
let supplyUsd = this.client.pos.supplyUsd(this.priceType);
|
25
45
|
let debtUsd = this.client.pos.debtUsd(this.priceType);
|
26
46
|
// TODO: add token balance change
|
27
|
-
const
|
28
|
-
? this.client.pos.supplyMint
|
29
|
-
: this.client.pos.debtMint;
|
47
|
+
const { output, outputPrice } = this.swapDetails();
|
30
48
|
const swapOutputUsd = swapOutputAmount
|
31
|
-
? (0, utils_1.fromBaseUnit)(swapOutputAmount, (0, utils_1.tokenInfo)(
|
32
|
-
(0, utils_1.safeGetPrice)(outputToken, this.priceType)
|
49
|
+
? (0, utils_1.fromBaseUnit)(swapOutputAmount, (0, utils_1.tokenInfo)((0, umi_web3js_adapters_1.toWeb3JsPublicKey)(output.mint)).decimals) * outputPrice
|
33
50
|
: this.usdToSwap();
|
34
51
|
supplyUsd = this.isBoost()
|
35
52
|
? supplyUsd + swapOutputUsd
|
@@ -62,32 +79,18 @@ class RebalanceSwapManager {
|
|
62
79
|
}
|
63
80
|
return swapQuote;
|
64
81
|
}
|
65
|
-
swapDetails() {
|
66
|
-
const input = this.isBoost()
|
67
|
-
? this.client.pos.state.debt
|
68
|
-
: this.client.pos.state.supply;
|
69
|
-
const output = this.isBoost()
|
70
|
-
? this.client.pos.state.supply
|
71
|
-
: this.client.pos.state.debt;
|
72
|
-
let inputAmount = (0, utils_1.toBaseUnit)(this.usdToSwap() / (0, utils_1.safeGetPrice)(input.mint, this.priceType), input.decimals);
|
73
|
-
return {
|
74
|
-
input,
|
75
|
-
output,
|
76
|
-
inputAmount,
|
77
|
-
};
|
78
|
-
}
|
79
82
|
bigIntWithIncrement(num, inc) {
|
80
83
|
return num + BigInt(Math.round(Number(num) * inc));
|
81
84
|
}
|
82
85
|
async setSwapParams(attemptNum) {
|
83
86
|
const rebalanceToZero = this.targetLiqUtilizationRateBps === 0;
|
84
|
-
let { input, output, inputAmount } = this.swapDetails();
|
87
|
+
let { input, output, outputPrice, inputAmount } = this.swapDetails();
|
85
88
|
let outputAmount = rebalanceToZero
|
86
89
|
? output.amountUsed.baseUnit +
|
87
90
|
BigInt(Math.round(Number(output.amountUsed.baseUnit) *
|
88
91
|
// Add this small percentage to account for the APR on the debt between now and the transaction
|
89
92
|
0.0001))
|
90
|
-
: (0, utils_1.toBaseUnit)(this.usdToSwap() /
|
93
|
+
: (0, utils_1.toBaseUnit)(this.usdToSwap() / outputPrice, output.decimals);
|
91
94
|
const flashLoanRepayFromDebt = !this.isBoost() &&
|
92
95
|
this.flRequirements &&
|
93
96
|
this.flRequirements.liquiditySource === generated_1.TokenType.Debt;
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"priceUtils.d.ts","sourceRoot":"","sources":["../../src/utils/priceUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,SAAS,IAAI,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAerE,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzC,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAE5C,UAAU,WAAW;IACnB,aAAa,EAAE,MAAM,CAAC;IACtB,YAAY,CAAC,EAAE,MAAM,CAAC;I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AAgB,CACpC,KAAK,EAAE,SAAS,EAAE,EAClB,SAAS,GAAE,SAA8B,EACzC,SAAS,CAAC,EAAE,SAAS,GACpB,OAAO,CAAC,MAAM,EAAE,CAAC,CA0CnB;AAED,wBAAsB,aAAa,CACjC,KAAK,EAAE,SAAS,EAAE,GACjB,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC,CAoDtC;AAkBD,wBAAsB,oBAAoB,CACxC,KAAK,EAAE,SAAS,EAAE,GACjB,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC,CA+CtC;AAED,wBAAsB,iBAAiB,CACrC,KAAK,EAAE,SAAS,EAAE,GACjB,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC,CAiBtC;AAED,wBAAgB,YAAY,CAC1B,IAAI,EAAE,SAAS,GAAG,YAAY,GAAG,MAAM,GAAG,SAAS,EACnD,SAAS,GAAE,SAA8B,EACzC,SAAS,CAAC,EAAE,SAAS,GACpB,MAAM,GAAG,SAAS,CAyBpB"}
|
package/dist/utils/priceUtils.js
CHANGED
@@ -91,9 +91,9 @@ async function getPythPrices(mints) {
|
|
91
91
|
const prices = json.parsed.map((x) => {
|
92
92
|
return {
|
93
93
|
realtimePrice: deriveValue(x.price.price, x.price.expo),
|
94
|
-
confInterval: deriveValue(x.price.conf, x.price.expo),
|
94
|
+
confInterval: deriveValue(x.price.conf, x.price.expo) * 2.12,
|
95
95
|
emaPrice: deriveValue(x.ema_price.price, x.ema_price.expo),
|
96
|
-
emaConfInterval: deriveValue(x.ema_price.conf, x.ema_price.expo),
|
96
|
+
emaConfInterval: deriveValue(x.ema_price.conf, x.ema_price.expo) * 2.12,
|
97
97
|
};
|
98
98
|
});
|
99
99
|
return prices;
|
@@ -164,13 +164,15 @@ function safeGetPrice(mint, priceType = generated_1.PriceType.Realtime, priceBia
|
|
164
164
|
: priceData.realtimePrice;
|
165
165
|
if (priceBias !== undefined) {
|
166
166
|
const confInterval = priceType === generated_1.PriceType.Ema ? priceData.confInterval : priceData.emaConfInterval;
|
167
|
-
const conf = Math.min(confInterval
|
167
|
+
const conf = Math.min(confInterval, price * 0.05);
|
168
|
+
(0, generalUtils_1.consoleLog)("before", price);
|
168
169
|
if (priceBias === marginfi_sdk_1.PriceBias.Low) {
|
169
170
|
price = price - conf;
|
170
171
|
}
|
171
172
|
else {
|
172
173
|
price = price + conf;
|
173
174
|
}
|
175
|
+
(0, generalUtils_1.consoleLog)("after", price);
|
174
176
|
}
|
175
177
|
return price;
|
176
178
|
}
|
package/package.json
CHANGED
@@ -9,7 +9,6 @@ import {
|
|
9
9
|
consoleLog,
|
10
10
|
fromBaseUnit,
|
11
11
|
getLiqUtilzationRateBps,
|
12
|
-
safeGetPrice,
|
13
12
|
toBaseUnit,
|
14
13
|
tokenInfo,
|
15
14
|
} from "../../utils";
|
@@ -39,17 +38,45 @@ export class RebalanceSwapManager {
|
|
39
38
|
return Math.abs(this.values.debtAdjustmentUsd);
|
40
39
|
}
|
41
40
|
|
41
|
+
private swapDetails() {
|
42
|
+
const input = this.isBoost()
|
43
|
+
? this.client.pos.state.debt
|
44
|
+
: this.client.pos.state.supply;
|
45
|
+
const output = this.isBoost()
|
46
|
+
? this.client.pos.state.supply
|
47
|
+
: this.client.pos.state.debt;
|
48
|
+
|
49
|
+
const supplyPrice = this.client.pos.supplyPrice(this.priceType)!;
|
50
|
+
const debtPrice = this.client.pos.debtPrice(this.priceType)!;
|
51
|
+
const inputPrice = this.isBoost() ? debtPrice : supplyPrice;
|
52
|
+
const outputPrice = this.isBoost() ? supplyPrice : debtPrice;
|
53
|
+
|
54
|
+
let inputAmount = toBaseUnit(
|
55
|
+
this.usdToSwap() / inputPrice!,
|
56
|
+
input.decimals
|
57
|
+
);
|
58
|
+
|
59
|
+
return {
|
60
|
+
inputAmount,
|
61
|
+
input,
|
62
|
+
inputPrice,
|
63
|
+
output,
|
64
|
+
outputPrice,
|
65
|
+
};
|
66
|
+
}
|
67
|
+
|
42
68
|
private postRebalanceLiqUtilizationRateBps(swapOutputAmount?: bigint) {
|
43
69
|
let supplyUsd = this.client.pos.supplyUsd(this.priceType);
|
44
70
|
let debtUsd = this.client.pos.debtUsd(this.priceType);
|
45
71
|
// TODO: add token balance change
|
46
72
|
|
47
|
-
const
|
48
|
-
|
49
|
-
: this.client.pos.debtMint;
|
73
|
+
const { output, outputPrice } = this.swapDetails();
|
74
|
+
|
50
75
|
const swapOutputUsd = swapOutputAmount
|
51
|
-
? fromBaseUnit(
|
52
|
-
|
76
|
+
? fromBaseUnit(
|
77
|
+
swapOutputAmount,
|
78
|
+
tokenInfo(toWeb3JsPublicKey(output.mint)).decimals
|
79
|
+
) * outputPrice
|
53
80
|
: this.usdToSwap();
|
54
81
|
|
55
82
|
supplyUsd = this.isBoost()
|
@@ -102,33 +129,13 @@ export class RebalanceSwapManager {
|
|
102
129
|
return swapQuote!;
|
103
130
|
}
|
104
131
|
|
105
|
-
private swapDetails() {
|
106
|
-
const input = this.isBoost()
|
107
|
-
? this.client.pos.state.debt
|
108
|
-
: this.client.pos.state.supply;
|
109
|
-
const output = this.isBoost()
|
110
|
-
? this.client.pos.state.supply
|
111
|
-
: this.client.pos.state.debt;
|
112
|
-
|
113
|
-
let inputAmount = toBaseUnit(
|
114
|
-
this.usdToSwap() / safeGetPrice(input.mint, this.priceType)!,
|
115
|
-
input.decimals
|
116
|
-
);
|
117
|
-
|
118
|
-
return {
|
119
|
-
input,
|
120
|
-
output,
|
121
|
-
inputAmount,
|
122
|
-
};
|
123
|
-
}
|
124
|
-
|
125
132
|
private bigIntWithIncrement(num: bigint, inc: number) {
|
126
133
|
return num + BigInt(Math.round(Number(num) * inc));
|
127
134
|
}
|
128
135
|
|
129
136
|
async setSwapParams(attemptNum: number) {
|
130
137
|
const rebalanceToZero = this.targetLiqUtilizationRateBps === 0;
|
131
|
-
let { input, output, inputAmount } = this.swapDetails();
|
138
|
+
let { input, output, outputPrice, inputAmount } = this.swapDetails();
|
132
139
|
|
133
140
|
let outputAmount = rebalanceToZero
|
134
141
|
? output.amountUsed.baseUnit +
|
@@ -139,10 +146,7 @@ export class RebalanceSwapManager {
|
|
139
146
|
0.0001
|
140
147
|
)
|
141
148
|
)
|
142
|
-
: toBaseUnit(
|
143
|
-
this.usdToSwap() / safeGetPrice(output.mint, this.priceType)!,
|
144
|
-
output.decimals
|
145
|
-
);
|
149
|
+
: toBaseUnit(this.usdToSwap() / outputPrice, output.decimals);
|
146
150
|
|
147
151
|
const flashLoanRepayFromDebt =
|
148
152
|
!this.isBoost() &&
|
package/src/utils/priceUtils.ts
CHANGED
@@ -110,9 +110,9 @@ export async function getPythPrices(
|
|
110
110
|
const prices = json.parsed.map((x: any) => {
|
111
111
|
return {
|
112
112
|
realtimePrice: deriveValue(x.price.price, x.price.expo),
|
113
|
-
confInterval: deriveValue(x.price.conf, x.price.expo),
|
113
|
+
confInterval: deriveValue(x.price.conf, x.price.expo) * 2.12,
|
114
114
|
emaPrice: deriveValue(x.ema_price.price, x.ema_price.expo),
|
115
|
-
emaConfInterval: deriveValue(x.ema_price.conf, x.ema_price.expo),
|
115
|
+
emaConfInterval: deriveValue(x.ema_price.conf, x.ema_price.expo) * 2.12,
|
116
116
|
};
|
117
117
|
});
|
118
118
|
|
@@ -230,13 +230,16 @@ export function safeGetPrice(
|
|
230
230
|
|
231
231
|
if (priceBias !== undefined) {
|
232
232
|
const confInterval = priceType === PriceType.Ema ? priceData.confInterval : priceData.emaConfInterval;
|
233
|
-
const conf = Math.min(confInterval
|
233
|
+
const conf = Math.min(confInterval, price * 0.05);
|
234
234
|
|
235
|
+
consoleLog("before", price);
|
235
236
|
if (priceBias === PriceBias.Low) {
|
236
237
|
price = price - conf;
|
237
238
|
} else {
|
238
239
|
price = price + conf;
|
239
240
|
}
|
241
|
+
consoleLog("after", price);
|
242
|
+
|
240
243
|
}
|
241
244
|
|
242
245
|
return price;
|