impermax-sdk 1.1.71 → 1.1.72
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/lib/onchain/account/lendingPool/onchainAccountBorrowable.js +37 -11
- package/lib/onchain/account/lendingPool/onchainAccountCollateral.js +36 -10
- package/lib/onchain/account/lendingPool/onchainAccountLendingPool.js +19 -11
- package/lib/onchain/impermaxFactory/lendingPool/onchainLendingPool.d.ts +6 -2
- package/lib/onchain/impermaxFactory/lendingPool/onchainLendingPool.js +42 -64
- package/lib/onchain/interactions/lendingPool/onchainInteractionsLendingPool.d.ts +1 -1
- package/lib/onchain/interactions/lendingPool/onchainInteractionsLendingPool.js +13 -10
- package/lib/utils/lliquidity-math.d.ts +3 -0
- package/lib/utils/lliquidity-math.js +3 -0
- package/package.json +1 -1
|
@@ -12,7 +12,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
12
12
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
13
|
};
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
const onchainTypes_1 = require("../../onchainTypes");
|
|
16
15
|
const types_1 = require("../../../config/types");
|
|
17
16
|
const onchainAccountPoolToken_1 = __importDefault(require("../onchainAccountPoolToken"));
|
|
18
17
|
class OnchainAccountBorrowable extends onchainAccountPoolToken_1.default {
|
|
@@ -71,19 +70,46 @@ class OnchainAccountBorrowable extends onchainAccountPoolToken_1.default {
|
|
|
71
70
|
});
|
|
72
71
|
}
|
|
73
72
|
// Max OffchainBorrowable
|
|
73
|
+
/*public async getMaxBorrowable() : Promise<number> {
|
|
74
|
+
const availableCash = await this.poolToken.getTotalBalance();
|
|
75
|
+
const { valueCollateral, valueA, valueB } = await this.lendingPool.getValues(NO_CHANGES);
|
|
76
|
+
const [valueBorrowed, valueOther] = this.getPoolTokenType() == PoolTokenType.BorrowableA ? [valueA, valueB] : [valueB, valueA];
|
|
77
|
+
const safetyMargin = (await this.lendingPool.getSafetyMargin()) * this.getUiMargin();
|
|
78
|
+
const liquidationPenalty = await this.lendingPool.getLiquidationPenalty();
|
|
79
|
+
const actualCollateral = valueCollateral / liquidationPenalty;
|
|
80
|
+
const totalValueBorrowable1 = (actualCollateral * Math.sqrt(safetyMargin) - valueOther) / safetyMargin;
|
|
81
|
+
const totalValueBorrowable2 = (actualCollateral / Math.sqrt(safetyMargin) - valueOther) * safetyMargin;
|
|
82
|
+
const maxValueBorrowable = Math.min(totalValueBorrowable1, totalValueBorrowable2) - valueBorrowed;
|
|
83
|
+
const price = await this.poolToken.getMarketPriceDenomLP();
|
|
84
|
+
return Math.max(0, Math.min(availableCash, maxValueBorrowable / price));
|
|
85
|
+
}*/
|
|
86
|
+
// binary search approach
|
|
74
87
|
getMaxBorrowable() {
|
|
75
88
|
return __awaiter(this, void 0, void 0, function* () {
|
|
76
89
|
const availableCash = yield this.poolToken.getTotalBalance();
|
|
77
|
-
const
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
90
|
+
const twapPrice = yield this.lendingPool.getLendingPool().getTWAPPrice();
|
|
91
|
+
// start search
|
|
92
|
+
let borrowAmount = availableCash;
|
|
93
|
+
let borrowAmountPrev = 0;
|
|
94
|
+
let bestAmount = 0;
|
|
95
|
+
for (let i = 0; i < 1000; i++) {
|
|
96
|
+
const jump = Math.abs(borrowAmount - borrowAmountPrev) / 2;
|
|
97
|
+
if (jump / availableCash < 0.001)
|
|
98
|
+
break;
|
|
99
|
+
borrowAmountPrev = borrowAmount;
|
|
100
|
+
const changeCollateral = 0;
|
|
101
|
+
const changeBorrowedA = this.getPoolTokenType() == types_1.PoolTokenType.BorrowableA ? borrowAmount * this.getUiMargin() : 0;
|
|
102
|
+
const changeBorrowedB = this.getPoolTokenType() == types_1.PoolTokenType.BorrowableB ? borrowAmount * this.getUiMargin() : 0;
|
|
103
|
+
const changes = { changeCollateral, changeBorrowedA, changeBorrowedB };
|
|
104
|
+
const [liqPriceA, liqPriceB] = yield this.lendingPool.getLiquidationPrices(changes);
|
|
105
|
+
if (liqPriceA > twapPrice || liqPriceB < twapPrice)
|
|
106
|
+
borrowAmount -= jump;
|
|
107
|
+
else {
|
|
108
|
+
bestAmount = borrowAmount;
|
|
109
|
+
borrowAmount += jump;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return bestAmount / this.getDust();
|
|
87
113
|
});
|
|
88
114
|
}
|
|
89
115
|
// Farming Shares
|
|
@@ -12,7 +12,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
12
12
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
13
|
};
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
const onchainTypes_1 = require("../../onchainTypes");
|
|
16
15
|
const onchainAccountPoolToken_1 = __importDefault(require("../onchainAccountPoolToken"));
|
|
17
16
|
class OnchainAccountCollateral extends onchainAccountPoolToken_1.default {
|
|
18
17
|
constructor(lendingPool) {
|
|
@@ -62,17 +61,44 @@ class OnchainAccountCollateral extends onchainAccountPoolToken_1.default {
|
|
|
62
61
|
});
|
|
63
62
|
}
|
|
64
63
|
// Max Withdrawable
|
|
64
|
+
/*public async getMaxWithdrawable() : Promise<number> {
|
|
65
|
+
const deposited = await this.getDeposited();
|
|
66
|
+
const availableCash = await this.poolToken.getTotalBalance();
|
|
67
|
+
const { valueCollateral, valueA, valueB } = await this.lendingPool.getValues(NO_CHANGES);
|
|
68
|
+
const safetyMargin = (await this.lendingPool.getSafetyMargin()) * this.getUiMargin();
|
|
69
|
+
const liquidationPenalty = await this.lendingPool.getLiquidationPenalty();
|
|
70
|
+
const actualCollateral = valueCollateral / liquidationPenalty;
|
|
71
|
+
const maxWithdrawable1 = (actualCollateral - (valueA + valueB * safetyMargin) / Math.sqrt(safetyMargin)) * liquidationPenalty / this.getDust();
|
|
72
|
+
const maxWithdrawable2 = (actualCollateral - (valueB + valueA * safetyMargin) / Math.sqrt(safetyMargin)) * liquidationPenalty / this.getDust();
|
|
73
|
+
return Math.max(0, Math.min(deposited, availableCash, maxWithdrawable1, maxWithdrawable2));
|
|
74
|
+
}*/
|
|
75
|
+
// binary search approach
|
|
65
76
|
getMaxWithdrawable() {
|
|
66
77
|
return __awaiter(this, void 0, void 0, function* () {
|
|
67
|
-
const
|
|
68
|
-
const
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
78
|
+
const collateralAmount = yield this.getDeposited();
|
|
79
|
+
const twapPrice = yield this.lendingPool.getLendingPool().getTWAPPrice();
|
|
80
|
+
// start search
|
|
81
|
+
let withdrawAmount = collateralAmount;
|
|
82
|
+
let withdrawAmountPrev = 0;
|
|
83
|
+
let bestAmount = 0;
|
|
84
|
+
for (let i = 0; i < 1000; i++) {
|
|
85
|
+
const jump = Math.abs(withdrawAmount - withdrawAmountPrev) / 2;
|
|
86
|
+
if (jump / collateralAmount < 0.001)
|
|
87
|
+
break;
|
|
88
|
+
withdrawAmountPrev = withdrawAmount;
|
|
89
|
+
const changeCollateral = -1 * withdrawAmount * this.getUiMargin();
|
|
90
|
+
const changeBorrowedA = 0;
|
|
91
|
+
const changeBorrowedB = 0;
|
|
92
|
+
const changes = { changeCollateral, changeBorrowedA, changeBorrowedB };
|
|
93
|
+
const [liqPriceA, liqPriceB] = yield this.lendingPool.getLiquidationPrices(changes);
|
|
94
|
+
if (liqPriceA > twapPrice || liqPriceB < twapPrice)
|
|
95
|
+
withdrawAmount -= jump;
|
|
96
|
+
else {
|
|
97
|
+
bestAmount = withdrawAmount;
|
|
98
|
+
withdrawAmount += jump;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return bestAmount / this.getDust();
|
|
76
102
|
});
|
|
77
103
|
}
|
|
78
104
|
}
|
|
@@ -197,10 +197,10 @@ class OnchainAccountLendingPool {
|
|
|
197
197
|
const safetyMargin = yield this.getSafetyMargin();
|
|
198
198
|
const liquidationPenalty = yield this.getLiquidationPenalty();
|
|
199
199
|
const actualCollateral = collateralAmount / liquidationPenalty;
|
|
200
|
-
const
|
|
200
|
+
const [reservesWeight0, reservesWeight1] = yield this.lendingPool.getTwapReservesWeights();
|
|
201
201
|
const [priceA, priceB] = yield this.lendingPool.getPriceDenomLP();
|
|
202
|
-
const collateralA = actualCollateral *
|
|
203
|
-
const collateralB = actualCollateral
|
|
202
|
+
const collateralA = actualCollateral * reservesWeight0 / priceA;
|
|
203
|
+
const collateralB = actualCollateral * reservesWeight1 / priceB;
|
|
204
204
|
let priceSwingA, priceSwingB;
|
|
205
205
|
if (factories_1.STABLE_FACTORIES.includes(this.lendingPool.getImpermaxFactory().getFactory())) {
|
|
206
206
|
priceSwingA = (0, lliquidity_math_1.solidlyStable_getLiquidatableXPriceDecrease)(collateralA, collateralB, debtA, debtB) * safetyMargin;
|
|
@@ -245,14 +245,14 @@ class OnchainAccountLendingPool {
|
|
|
245
245
|
const availableCashA = yield this.getBorrowableA().getPoolToken().getTotalBalance();
|
|
246
246
|
const availableCashB = yield this.getBorrowableB().getPoolToken().getTotalBalance();
|
|
247
247
|
const collateralAmount = yield this.getCollateral().getDeposited();
|
|
248
|
-
const
|
|
248
|
+
const [reservesWeight0, reservesWeight1] = yield this.lendingPool.getMarketReservesWeights();
|
|
249
249
|
//const [priceA, priceB] = await this.lendingPool.getPriceDenomLP();
|
|
250
|
-
// come posso adattare l'aggiustamento alle stable pairs?
|
|
250
|
+
//TODO come posso adattare l'aggiustamento alle stable pairs?
|
|
251
251
|
const [priceA, priceB] = yield this.lendingPool.getMarketPriceDenomLP();
|
|
252
252
|
const [priceATWAP,] = yield this.lendingPool.getPriceDenomLP();
|
|
253
253
|
const diff = priceA > priceATWAP ? priceA / priceATWAP : priceATWAP / priceA;
|
|
254
|
-
const collateralA = collateralAmount *
|
|
255
|
-
const collateralB = collateralAmount
|
|
254
|
+
const collateralA = collateralAmount * reservesWeight0 / priceA;
|
|
255
|
+
const collateralB = collateralAmount * reservesWeight1 / priceB;
|
|
256
256
|
const twapPrice = yield this.lendingPool.getTWAPPrice();
|
|
257
257
|
// start search
|
|
258
258
|
let addLev = 100;
|
|
@@ -289,14 +289,22 @@ class OnchainAccountLendingPool {
|
|
|
289
289
|
getMaxDeleverage(slippage) {
|
|
290
290
|
return __awaiter(this, void 0, void 0, function* () {
|
|
291
291
|
const { valueCollateral, valueA, valueB } = yield this.getMarketValues(onchainTypes_1.NO_CHANGES);
|
|
292
|
-
const
|
|
293
|
-
|
|
292
|
+
const [reservesWeight0, reservesWeight1] = yield this.lendingPool.getMarketReservesWeights();
|
|
293
|
+
const adjCollateralValue = valueCollateral / Math.sqrt(slippage);
|
|
294
|
+
const collateralValueA = valueCollateral * reservesWeight0;
|
|
295
|
+
const collateralValueB = valueCollateral * reservesWeight1;
|
|
296
|
+
if (collateralValueA >= valueA && collateralValueB >= valueB) {
|
|
294
297
|
return this.getCollateral().getDeposited();
|
|
295
298
|
}
|
|
296
|
-
if (
|
|
299
|
+
if (adjCollateralValue < valueA + valueB) {
|
|
297
300
|
return 0;
|
|
298
301
|
}
|
|
299
|
-
|
|
302
|
+
if (valueA / collateralValueA < valueB / collateralValueB) {
|
|
303
|
+
return (valueA + valueA / collateralValueA * collateralValueB) * Math.sqrt(slippage);
|
|
304
|
+
}
|
|
305
|
+
else {
|
|
306
|
+
return (valueB + valueB / collateralValueB * collateralValueA) * Math.sqrt(slippage);
|
|
307
|
+
}
|
|
300
308
|
});
|
|
301
309
|
}
|
|
302
310
|
// Available Reward
|
|
@@ -39,8 +39,12 @@ export default class OnchainLendingPool {
|
|
|
39
39
|
getPriceDenomLP(): Promise<[number, number]>;
|
|
40
40
|
getMarketPriceDenomLP(): Promise<[number, number]>;
|
|
41
41
|
getMarketPrice(): Promise<number>;
|
|
42
|
-
private
|
|
43
|
-
|
|
42
|
+
private initializeTwapReservesValueRatio;
|
|
43
|
+
getTwapReservesValueRatio(): Promise<number>;
|
|
44
|
+
getTwapReservesWeights(): Promise<[number, number]>;
|
|
45
|
+
private initializeMarketReservesValueRatio;
|
|
46
|
+
getMarketReservesValueRatio(): Promise<number>;
|
|
47
|
+
getMarketReservesWeights(): Promise<[number, number]>;
|
|
44
48
|
private initializeTWAPPrice;
|
|
45
49
|
getTWAPPrice(): Promise<number>;
|
|
46
50
|
private initializeStakedLPExchangeRate;
|
|
@@ -170,9 +170,7 @@ class OnchainLendingPool {
|
|
|
170
170
|
return __awaiter(this, void 0, void 0, function* () {
|
|
171
171
|
const [reserve0, reserve1] = yield this.getReserves();
|
|
172
172
|
const totalSupply = yield this.getLPTotalSupply();
|
|
173
|
-
const
|
|
174
|
-
const reservesWeight0 = reservesRatio / (1 + reservesRatio);
|
|
175
|
-
const reservesWeight1 = 1 / (1 + reservesRatio);
|
|
173
|
+
const [reservesWeight0, reservesWeight1] = yield this.getMarketReservesWeights();
|
|
176
174
|
return [
|
|
177
175
|
totalSupply / reserve0 * reservesWeight0,
|
|
178
176
|
totalSupply / reserve1 * reservesWeight1,
|
|
@@ -190,64 +188,8 @@ class OnchainLendingPool {
|
|
|
190
188
|
return (0, lliquidity_math_1.uniswapV2_getPriceFromReserves)(reserve0, reserve1, priceInverted);
|
|
191
189
|
});
|
|
192
190
|
}
|
|
193
|
-
/*
|
|
194
|
-
// TWAP Reserves
|
|
195
|
-
// NON VA BENE, voglio tornare le riserve dello stakedLPToken o di tutto? DIPORCO
|
|
196
|
-
private async initializeTWAPReserves() : Promise<[number, number] | null> {
|
|
197
|
-
try {
|
|
198
|
-
const collateral = await this.getCollateral().getPoolToken();
|
|
199
|
-
const decimalsA = await this.getBorrowableA().getDecimals();
|
|
200
|
-
const decimalsB = await this.getBorrowableB().getDecimals();
|
|
201
|
-
|
|
202
|
-
if (STABLE_FACTORIES.includes(this.impermaxFactory.getFactory())) {
|
|
203
|
-
const { reserve0, reserve1 } = await collateral.methods.getReserves().call();
|
|
204
|
-
return [
|
|
205
|
-
reserve0 / Math.pow(10, decimalsA),
|
|
206
|
-
reserve1 / Math.pow(10, decimalsB)
|
|
207
|
-
];
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
const [reserve0, reserve1] = await this.getReserves();
|
|
211
|
-
const currentPrice = uniswapV2_getPriceFromReserves(reserve0, reserve1);
|
|
212
|
-
let TWAPprice;
|
|
213
|
-
if (SOLIDEX_FACTORIES.includes(this.impermaxFactory.getFactory())) {
|
|
214
|
-
TWAPprice = await collateral.methods.getTwapPrice112x112().call();
|
|
215
|
-
} else {
|
|
216
|
-
TWAPprice = (await this.impermaxFactory.getSimpleUniswapOracle().methods.getResult(this.pairAddress).call()).price;
|
|
217
|
-
}
|
|
218
|
-
TWAPprice = TWAPprice / 2**112 * Math.pow(10, decimalsA) / Math.pow(10, decimalsB);
|
|
219
|
-
|
|
220
|
-
const adj = Math.sqrt(currentPrice / TWAPprice);
|
|
221
|
-
return [
|
|
222
|
-
reserve0 * adj,
|
|
223
|
-
reserve1 / adj,
|
|
224
|
-
]
|
|
225
|
-
}
|
|
226
|
-
catch (e) {
|
|
227
|
-
// Oracle is not initialized yet
|
|
228
|
-
return null;
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
public async getTWAPReserves() : Promise<[number, number] | null> {
|
|
232
|
-
if (!this.cache.TWAPReserves) this.cache.TWAPReserves = this.initializeTWAPReserves();
|
|
233
|
-
return this.cache.TWAPReserves;
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
// TWAP Price
|
|
237
|
-
private async initializeTWAPPrice() : Promise<number> {
|
|
238
|
-
const TWAPReserves = await this.getTWAPReserves();
|
|
239
|
-
if (!TWAPReserves) return 0;
|
|
240
|
-
const reserve0 = TWAPReserves[0];
|
|
241
|
-
const reserve1 = TWAPReserves[1];
|
|
242
|
-
|
|
243
|
-
if (STABLE_FACTORIES.includes(this.impermaxFactory.getFactory())) {
|
|
244
|
-
return solidlyStable_getPriceFromReserves(reserve0, reserve1);
|
|
245
|
-
} else {
|
|
246
|
-
return uniswapV2_getPriceFromReserves(reserve0, reserve1);
|
|
247
|
-
}
|
|
248
|
-
}*/
|
|
249
191
|
// Value of reserveA / reserveB
|
|
250
|
-
|
|
192
|
+
initializeTwapReservesValueRatio() {
|
|
251
193
|
return __awaiter(this, void 0, void 0, function* () {
|
|
252
194
|
if (factories_1.STABLE_FACTORIES.includes(this.impermaxFactory.getFactory())) {
|
|
253
195
|
const collateral = yield this.getCollateral().getPoolToken();
|
|
@@ -262,13 +204,49 @@ class OnchainLendingPool {
|
|
|
262
204
|
}
|
|
263
205
|
});
|
|
264
206
|
}
|
|
265
|
-
|
|
207
|
+
getTwapReservesValueRatio() {
|
|
208
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
209
|
+
if (!this.cache.twapReservesValueRatio)
|
|
210
|
+
this.cache.twapReservesValueRatio = this.initializeTwapReservesValueRatio();
|
|
211
|
+
return this.cache.twapReservesValueRatio;
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
getTwapReservesWeights() {
|
|
266
215
|
return __awaiter(this, void 0, void 0, function* () {
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
216
|
+
const reservesRatio = yield this.getTwapReservesValueRatio();
|
|
217
|
+
const reservesWeight0 = reservesRatio / (1 + reservesRatio);
|
|
218
|
+
const reservesWeight1 = 1 / (1 + reservesRatio);
|
|
219
|
+
return [reservesWeight0, reservesWeight1];
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
initializeMarketReservesValueRatio() {
|
|
223
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
224
|
+
if (factories_1.STABLE_FACTORIES.includes(this.impermaxFactory.getFactory())) {
|
|
225
|
+
const [reserve0, reserve1] = yield this.getReserves();
|
|
226
|
+
const price = (0, lliquidity_math_1.solidlyStable_getPriceFromReserves)(reserve0, reserve1);
|
|
227
|
+
return reserve0 * price / reserve1;
|
|
228
|
+
}
|
|
229
|
+
else {
|
|
230
|
+
return 1;
|
|
231
|
+
}
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
getMarketReservesValueRatio() {
|
|
235
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
236
|
+
if (!this.cache.marketReservesValueRatio)
|
|
237
|
+
this.cache.marketReservesValueRatio = this.initializeMarketReservesValueRatio();
|
|
238
|
+
return this.cache.marketReservesValueRatio;
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
getMarketReservesWeights() {
|
|
242
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
243
|
+
const reservesRatio = yield this.getMarketReservesValueRatio();
|
|
244
|
+
const reservesWeight0 = reservesRatio / (1 + reservesRatio);
|
|
245
|
+
const reservesWeight1 = 1 / (1 + reservesRatio);
|
|
246
|
+
return [reservesWeight0, reservesWeight1];
|
|
270
247
|
});
|
|
271
248
|
}
|
|
249
|
+
// TWAP Price
|
|
272
250
|
initializeTWAPPrice() {
|
|
273
251
|
return __awaiter(this, void 0, void 0, function* () {
|
|
274
252
|
try {
|
|
@@ -33,7 +33,7 @@ export default class OnchainInteractionsLendingPool {
|
|
|
33
33
|
cAmountMin: number;
|
|
34
34
|
}>;
|
|
35
35
|
leverage(amountA: BigNumber, amountB: BigNumber, amountAMin: BigNumber, amountBMin: BigNumber, permitDataA: PermitData, permitDataB: PermitData, onTransactionHash: Function): Promise<any>;
|
|
36
|
-
getDeleverageAmounts(
|
|
36
|
+
getDeleverageAmounts(changeCollateralValue: number, slippage: number): Promise<{
|
|
37
37
|
bAmountA: number;
|
|
38
38
|
bAmountB: number;
|
|
39
39
|
cAmount: number;
|
|
@@ -55,10 +55,11 @@ class OnchainInteractionsLendingPool {
|
|
|
55
55
|
const currentLeverage = yield accountLendingPool.getLeverage();
|
|
56
56
|
const collateralValue = yield accountLendingPool.getCollateral().getDeposited();
|
|
57
57
|
const changeCollateralValue = (collateralValue * leverage / currentLeverage - collateralValue) * adjustFactor;
|
|
58
|
-
|
|
59
|
-
const
|
|
60
|
-
const
|
|
61
|
-
const
|
|
58
|
+
const [reservesWeightA, reservesWeightB] = yield this.lendingPool.getMarketReservesWeights();
|
|
59
|
+
const changeValueA = changeCollateralValue * reservesWeightA;
|
|
60
|
+
const changeValueB = changeCollateralValue * reservesWeightB;
|
|
61
|
+
const bAmountA = priceA > 0 ? changeValueA / priceA : 0;
|
|
62
|
+
const bAmountB = priceB > 0 ? changeValueB / priceB : 0;
|
|
62
63
|
const cAmount = changeCollateralValue ? changeCollateralValue : 0;
|
|
63
64
|
return {
|
|
64
65
|
bAmountA: bAmountA,
|
|
@@ -83,17 +84,19 @@ class OnchainInteractionsLendingPool {
|
|
|
83
84
|
return this.send(router.methods.leverage(pairAddress, amountA, amountB, amountAMin, amountBMin, account, deadline, dataA, dataB), onTransactionHash);
|
|
84
85
|
});
|
|
85
86
|
}
|
|
86
|
-
getDeleverageAmounts(
|
|
87
|
+
getDeleverageAmounts(changeCollateralValue, slippage) {
|
|
87
88
|
return __awaiter(this, void 0, void 0, function* () {
|
|
88
|
-
|
|
89
|
+
changeCollateralValue = changeCollateralValue !== null && changeCollateralValue !== void 0 ? changeCollateralValue : 0;
|
|
89
90
|
const [priceA, priceB] = yield this.lendingPool.getMarketPriceDenomLP();
|
|
90
|
-
const
|
|
91
|
-
const
|
|
92
|
-
const
|
|
91
|
+
const [reservesWeightA, reservesWeightB] = yield this.lendingPool.getMarketReservesWeights();
|
|
92
|
+
const changeValueA = changeCollateralValue * reservesWeightA;
|
|
93
|
+
const changeValueB = changeCollateralValue * reservesWeightB;
|
|
94
|
+
const bAmountA = priceA > 0 ? changeValueA / priceA : 0;
|
|
95
|
+
const bAmountB = priceB > 0 ? changeValueB / priceB : 0;
|
|
93
96
|
return {
|
|
94
97
|
bAmountA: bAmountA,
|
|
95
98
|
bAmountB: bAmountB,
|
|
96
|
-
cAmount:
|
|
99
|
+
cAmount: changeCollateralValue,
|
|
97
100
|
bAmountAMin: bAmountA / Math.sqrt(slippage),
|
|
98
101
|
bAmountBMin: bAmountB / Math.sqrt(slippage),
|
|
99
102
|
};
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.solidlyStable_getLiquidatableXPriceDecrease = exports.solidlyStable_getXInMargin = exports.solidlyStable_getEquityValue = exports.solidlyStable_yOut = exports.solidlyStable_k = exports.solidlyStable_getPriceFromReserves = exports.uniswapV2_getLiquidatableXPriceDecrease = exports.uniswapV2_getPriceFromReserves = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Calculate X/Y price
|
|
6
|
+
*/
|
|
4
7
|
function uniswapV2_getPriceFromReserves(x, y, priceInverted = false) {
|
|
5
8
|
const price = y / x;
|
|
6
9
|
return !priceInverted ? price : 1 / price;
|