@curvefi/llamalend-api 1.0.21 → 1.0.22-beta.2
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/.github/workflows/publish.yml +1 -0
- package/lib/external-api.d.ts +6 -4
- package/lib/external-api.js +116 -98
- package/lib/index.d.ts +138 -41
- package/lib/index.js +80 -75
- package/lib/lendMarkets/LendMarketTemplate.d.ts +3 -1
- package/lib/lendMarkets/LendMarketTemplate.js +333 -333
- package/lib/lendMarkets/lendMarketConstructor.d.ts +2 -1
- package/lib/lendMarkets/lendMarketConstructor.js +3 -4
- package/lib/llamalend.d.ts +2 -2
- package/lib/llamalend.js +12 -20
- package/lib/mintMarkets/MintMarketTemplate.d.ts +3 -1
- package/lib/mintMarkets/MintMarketTemplate.js +204 -204
- package/lib/mintMarkets/mintMarketConstructor.d.ts +2 -1
- package/lib/mintMarkets/mintMarketConstructor.js +2 -2
- package/lib/st-crvUSD.d.ts +29 -28
- package/lib/st-crvUSD.js +237 -173
- package/lib/utils.d.ts +20 -19
- package/lib/utils.js +290 -256
- package/package.json +14 -14
- package/src/external-api.ts +25 -11
- package/src/index.ts +88 -76
- package/src/lendMarkets/LendMarketTemplate.ts +348 -346
- package/src/lendMarkets/lendMarketConstructor.ts +4 -4
- package/src/llamalend.ts +42 -58
- package/src/mintMarkets/MintMarketTemplate.ts +206 -204
- package/src/mintMarkets/mintMarketConstructor.ts +3 -2
- package/src/st-crvUSD.ts +97 -96
- package/src/utils.ts +85 -82
|
@@ -9,26 +9,25 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
};
|
|
10
10
|
import memoize from "memoizee";
|
|
11
11
|
import BigNumber from "bignumber.js";
|
|
12
|
-
import { llamalend } from "../llamalend.js";
|
|
13
12
|
import { _getAddress, parseUnits, BN, toBN, fromBN, getBalances, ensureAllowance, hasAllowance, ensureAllowanceEstimateGas, isEth, _cutZeros, formatUnits, smartNumber, MAX_ALLOWANCE, MAX_ACTIVE_BAND, _mulBy1_3, DIGas, } from "../utils";
|
|
14
13
|
import { _getUserCollateralCrvUsd } from "../external-api.js";
|
|
15
14
|
export class MintMarketTemplate {
|
|
16
|
-
constructor(id) {
|
|
15
|
+
constructor(id, llamalend) {
|
|
17
16
|
var _a;
|
|
18
17
|
// ---------------- STATS ----------------
|
|
19
18
|
this.statsParameters = memoize(() => __awaiter(this, void 0, void 0, function* () {
|
|
20
|
-
const llammaContract = llamalend.contracts[this.address].multicallContract;
|
|
21
|
-
const controllerContract = llamalend.contracts[this.controller].multicallContract;
|
|
22
|
-
const monetaryPolicyContract = llamalend.contracts[this.monetaryPolicy].multicallContract;
|
|
19
|
+
const llammaContract = this.llamalend.contracts[this.address].multicallContract;
|
|
20
|
+
const controllerContract = this.llamalend.contracts[this.controller].multicallContract;
|
|
21
|
+
const monetaryPolicyContract = this.llamalend.contracts[this.monetaryPolicy].multicallContract;
|
|
23
22
|
const calls = [
|
|
24
23
|
llammaContract.fee(),
|
|
25
24
|
llammaContract.admin_fee(),
|
|
26
25
|
llammaContract.rate(),
|
|
27
|
-
"rate(address)" in llamalend.contracts[this.monetaryPolicy].contract ? monetaryPolicyContract.rate(this.controller) : monetaryPolicyContract.rate(),
|
|
26
|
+
"rate(address)" in this.llamalend.contracts[this.monetaryPolicy].contract ? monetaryPolicyContract.rate(this.controller) : monetaryPolicyContract.rate(),
|
|
28
27
|
controllerContract.liquidation_discount(),
|
|
29
28
|
controllerContract.loan_discount(),
|
|
30
29
|
];
|
|
31
|
-
const [_fee, _admin_fee, _rate, _mp_rate, _liquidation_discount, _loan_discount] = yield llamalend.multicallProvider.all(calls);
|
|
30
|
+
const [_fee, _admin_fee, _rate, _mp_rate, _liquidation_discount, _loan_discount] = yield this.llamalend.multicallProvider.all(calls);
|
|
32
31
|
const [fee, admin_fee, liquidation_discount, loan_discount] = [_fee, _admin_fee, _liquidation_discount, _loan_discount]
|
|
33
32
|
.map((x) => formatUnits(x * BigInt(100)));
|
|
34
33
|
// (1+rate)**(365*86400)-1 ~= (e**(rate*365*86400))-1
|
|
@@ -40,42 +39,42 @@ export class MintMarketTemplate {
|
|
|
40
39
|
maxAge: 5 * 60 * 1000, // 5m
|
|
41
40
|
});
|
|
42
41
|
this.statsMaxMinBands = memoize(() => __awaiter(this, void 0, void 0, function* () {
|
|
43
|
-
const llammaContract = llamalend.contracts[this.address].multicallContract;
|
|
42
|
+
const llammaContract = this.llamalend.contracts[this.address].multicallContract;
|
|
44
43
|
const calls1 = [
|
|
45
44
|
llammaContract.max_band(),
|
|
46
45
|
llammaContract.min_band(),
|
|
47
46
|
];
|
|
48
|
-
return (yield llamalend.multicallProvider.all(calls1)).map((_b) => Number(_b));
|
|
47
|
+
return (yield this.llamalend.multicallProvider.all(calls1)).map((_b) => Number(_b));
|
|
49
48
|
}), {
|
|
50
49
|
promise: true,
|
|
51
50
|
maxAge: 60 * 1000, // 1m
|
|
52
51
|
});
|
|
53
52
|
this.statsActiveBand = memoize(() => __awaiter(this, void 0, void 0, function* () {
|
|
54
|
-
return Number((yield llamalend.contracts[this.address].contract.active_band_with_skip()));
|
|
53
|
+
return Number((yield this.llamalend.contracts[this.address].contract.active_band_with_skip()));
|
|
55
54
|
}), {
|
|
56
55
|
promise: true,
|
|
57
56
|
maxAge: 60 * 1000, // 1m
|
|
58
57
|
});
|
|
59
58
|
this.statsTotalSupply = memoize(() => __awaiter(this, void 0, void 0, function* () {
|
|
60
|
-
const controllerContract = llamalend.contracts[this.controller].multicallContract;
|
|
59
|
+
const controllerContract = this.llamalend.contracts[this.controller].multicallContract;
|
|
61
60
|
const calls = [controllerContract.minted(), controllerContract.redeemed()];
|
|
62
|
-
const [_minted, _redeemed] = yield llamalend.multicallProvider.all(calls);
|
|
61
|
+
const [_minted, _redeemed] = yield this.llamalend.multicallProvider.all(calls);
|
|
63
62
|
return toBN(_minted).minus(toBN(_redeemed)).toString();
|
|
64
63
|
}), {
|
|
65
64
|
promise: true,
|
|
66
65
|
maxAge: 60 * 1000, // 1m
|
|
67
66
|
});
|
|
68
67
|
this.statsTotalDebt = memoize(() => __awaiter(this, void 0, void 0, function* () {
|
|
69
|
-
const debt = yield llamalend.contracts[this.controller].contract.total_debt(llamalend.constantOptions);
|
|
68
|
+
const debt = yield this.llamalend.contracts[this.controller].contract.total_debt(this.llamalend.constantOptions);
|
|
70
69
|
return formatUnits(debt);
|
|
71
70
|
}), {
|
|
72
71
|
promise: true,
|
|
73
72
|
maxAge: 60 * 1000, // 1m
|
|
74
73
|
});
|
|
75
74
|
this.statsTotalStablecoin = memoize(() => __awaiter(this, void 0, void 0, function* () {
|
|
76
|
-
const stablecoinContract = llamalend.contracts[llamalend.crvUsdAddress].multicallContract;
|
|
77
|
-
const ammContract = llamalend.contracts[this.address].multicallContract;
|
|
78
|
-
const [_balance, _fee] = yield llamalend.multicallProvider.all([
|
|
75
|
+
const stablecoinContract = this.llamalend.contracts[this.llamalend.crvUsdAddress].multicallContract;
|
|
76
|
+
const ammContract = this.llamalend.contracts[this.address].multicallContract;
|
|
77
|
+
const [_balance, _fee] = yield this.llamalend.multicallProvider.all([
|
|
79
78
|
stablecoinContract.balanceOf(this.address),
|
|
80
79
|
ammContract.admin_fees_x(),
|
|
81
80
|
]);
|
|
@@ -85,9 +84,9 @@ export class MintMarketTemplate {
|
|
|
85
84
|
maxAge: 60 * 1000, // 1m
|
|
86
85
|
});
|
|
87
86
|
this.statsTotalCollateral = memoize(() => __awaiter(this, void 0, void 0, function* () {
|
|
88
|
-
const collateralContract = llamalend.contracts[isEth(this.collateral) ? llamalend.constants.WETH : this.collateral].multicallContract;
|
|
89
|
-
const ammContract = llamalend.contracts[this.address].multicallContract;
|
|
90
|
-
const [_balance, _fee] = yield llamalend.multicallProvider.all([
|
|
87
|
+
const collateralContract = this.llamalend.contracts[isEth(this.collateral) ? this.llamalend.constants.WETH : this.collateral].multicallContract;
|
|
88
|
+
const ammContract = this.llamalend.contracts[this.address].multicallContract;
|
|
89
|
+
const [_balance, _fee] = yield this.llamalend.multicallProvider.all([
|
|
91
90
|
collateralContract.balanceOf(this.address),
|
|
92
91
|
ammContract.admin_fees_y(),
|
|
93
92
|
]);
|
|
@@ -97,19 +96,19 @@ export class MintMarketTemplate {
|
|
|
97
96
|
maxAge: 60 * 1000, // 1m
|
|
98
97
|
});
|
|
99
98
|
this.statsCapAndAvailable = memoize(() => __awaiter(this, void 0, void 0, function* () {
|
|
100
|
-
const factoryContract = llamalend.contracts[llamalend.constants.FACTORY].multicallContract;
|
|
101
|
-
const crvusdContract = llamalend.contracts[llamalend.crvUsdAddress].multicallContract;
|
|
102
|
-
const [_cap, _available] = yield llamalend.multicallProvider.all([
|
|
99
|
+
const factoryContract = this.llamalend.contracts[this.llamalend.constants.FACTORY].multicallContract;
|
|
100
|
+
const crvusdContract = this.llamalend.contracts[this.llamalend.crvUsdAddress].multicallContract;
|
|
101
|
+
const [_cap, _available] = yield this.llamalend.multicallProvider.all([
|
|
103
102
|
factoryContract.debt_ceiling(this.controller),
|
|
104
103
|
crvusdContract.balanceOf(this.controller),
|
|
105
104
|
]);
|
|
106
|
-
return { "cap": llamalend.formatUnits(_cap), "available": llamalend.formatUnits(_available) };
|
|
105
|
+
return { "cap": this.llamalend.formatUnits(_cap), "available": this.llamalend.formatUnits(_available) };
|
|
107
106
|
}), {
|
|
108
107
|
promise: true,
|
|
109
108
|
maxAge: 60 * 1000, // 1m
|
|
110
109
|
});
|
|
111
110
|
this.basePrice = memoize(() => __awaiter(this, void 0, void 0, function* () {
|
|
112
|
-
const _price = yield llamalend.contracts[this.address].contract.get_base_price(llamalend.constantOptions);
|
|
111
|
+
const _price = yield this.llamalend.contracts[this.address].contract.get_base_price(this.llamalend.constantOptions);
|
|
113
112
|
return formatUnits(_price);
|
|
114
113
|
}), {
|
|
115
114
|
promise: true,
|
|
@@ -119,9 +118,9 @@ export class MintMarketTemplate {
|
|
|
119
118
|
const _collateral = parseUnits(collateral, this.collateralDecimals);
|
|
120
119
|
const calls = [];
|
|
121
120
|
for (let N = this.minBands; N <= this.maxBands; N++) {
|
|
122
|
-
calls.push(llamalend.contracts[this.controller].multicallContract.max_borrowable(_collateral, N));
|
|
121
|
+
calls.push(this.llamalend.contracts[this.controller].multicallContract.max_borrowable(_collateral, N));
|
|
123
122
|
}
|
|
124
|
-
const _amounts = yield llamalend.multicallProvider.all(calls);
|
|
123
|
+
const _amounts = yield this.llamalend.multicallProvider.all(calls);
|
|
125
124
|
const res = {};
|
|
126
125
|
for (let N = this.minBands; N <= this.maxBands; N++) {
|
|
127
126
|
res[N] = formatUnits(_amounts[N - this.minBands]);
|
|
@@ -137,18 +136,18 @@ export class MintMarketTemplate {
|
|
|
137
136
|
const calls = [];
|
|
138
137
|
for (let N = this.minBands; N <= this.maxBands; N++) {
|
|
139
138
|
for (let i = 0; i < 5; i++) {
|
|
140
|
-
calls.push(llamalend.contracts[this.leverageZap].multicallContract.max_borrowable_and_collateral(_collateral, N, i));
|
|
139
|
+
calls.push(this.llamalend.contracts[this.leverageZap].multicallContract.max_borrowable_and_collateral(_collateral, N, i));
|
|
141
140
|
}
|
|
142
141
|
}
|
|
143
|
-
const _rawRes = yield llamalend.multicallProvider.all(calls);
|
|
142
|
+
const _rawRes = yield this.llamalend.multicallProvider.all(calls);
|
|
144
143
|
const res = {};
|
|
145
144
|
for (let N = this.minBands; N <= this.maxBands; N++) {
|
|
146
145
|
const _res = _rawRes.splice(0, 5);
|
|
147
146
|
const _maxBorrowable = _res.map((r) => r[0] * BigInt(999) / BigInt(1000));
|
|
148
147
|
const _maxCollateral = _res.map((r) => r[1] * BigInt(999) / BigInt(1000));
|
|
149
148
|
const routeIdx = this._getBestIdx(_maxCollateral);
|
|
150
|
-
const maxBorrowable = llamalend.formatUnits(_maxBorrowable[routeIdx]);
|
|
151
|
-
const maxCollateral = llamalend.formatUnits(_maxCollateral[routeIdx], this.collateralDecimals);
|
|
149
|
+
const maxBorrowable = this.llamalend.formatUnits(_maxBorrowable[routeIdx]);
|
|
150
|
+
const maxCollateral = this.llamalend.formatUnits(_maxCollateral[routeIdx], this.collateralDecimals);
|
|
152
151
|
res[N] = {
|
|
153
152
|
maxBorrowable,
|
|
154
153
|
maxCollateral,
|
|
@@ -165,13 +164,13 @@ export class MintMarketTemplate {
|
|
|
165
164
|
const _collateral = parseUnits(collateral, this.collateralDecimals);
|
|
166
165
|
const calls = [];
|
|
167
166
|
for (let N = this.minBands; N <= this.maxBands; N++) {
|
|
168
|
-
calls.push(llamalend.contracts[this.leverageZap].multicallContract.max_borrowable_and_collateral(_collateral, N, routeIdx));
|
|
167
|
+
calls.push(this.llamalend.contracts[this.leverageZap].multicallContract.max_borrowable_and_collateral(_collateral, N, routeIdx));
|
|
169
168
|
}
|
|
170
|
-
const _res = yield llamalend.multicallProvider.all(calls);
|
|
169
|
+
const _res = yield this.llamalend.multicallProvider.all(calls);
|
|
171
170
|
const res = {};
|
|
172
171
|
for (let N = this.minBands; N <= this.maxBands; N++) {
|
|
173
|
-
const maxBorrowable = llamalend.formatUnits(_res[N - this.minBands][0] * BigInt(999) / BigInt(1000));
|
|
174
|
-
const maxCollateral = llamalend.formatUnits(_res[N - this.minBands][1] * BigInt(999) / BigInt(1000), this.collateralDecimals);
|
|
172
|
+
const maxBorrowable = this.llamalend.formatUnits(_res[N - this.minBands][0] * BigInt(999) / BigInt(1000));
|
|
173
|
+
const maxCollateral = this.llamalend.formatUnits(_res[N - this.minBands][1] * BigInt(999) / BigInt(1000), this.collateralDecimals);
|
|
175
174
|
res[N] = {
|
|
176
175
|
maxBorrowable,
|
|
177
176
|
maxCollateral,
|
|
@@ -188,9 +187,9 @@ export class MintMarketTemplate {
|
|
|
188
187
|
const _debt = parseUnits(debt);
|
|
189
188
|
const calls = [];
|
|
190
189
|
for (let i = 0; i < 5; i++) {
|
|
191
|
-
calls.push(llamalend.contracts[this.leverageZap].multicallContract.get_collateral(_debt, i));
|
|
190
|
+
calls.push(this.llamalend.contracts[this.leverageZap].multicallContract.get_collateral(_debt, i));
|
|
192
191
|
}
|
|
193
|
-
const _leverageCollateral = yield llamalend.multicallProvider.all(calls);
|
|
192
|
+
const _leverageCollateral = yield this.llamalend.multicallProvider.all(calls);
|
|
194
193
|
const routeIdx = this._getBestIdx(_leverageCollateral);
|
|
195
194
|
return { _collateral: _userCollateral + _leverageCollateral[routeIdx], routeIdx };
|
|
196
195
|
}), {
|
|
@@ -202,18 +201,18 @@ export class MintMarketTemplate {
|
|
|
202
201
|
const _collateral = parseUnits(collateral, this.collateralDecimals);
|
|
203
202
|
const calls = [];
|
|
204
203
|
for (let i = 0; i < 5; i++) {
|
|
205
|
-
calls.push(llamalend.contracts[this.deleverageZap].multicallContract.get_stablecoins(_collateral, i));
|
|
204
|
+
calls.push(this.llamalend.contracts[this.deleverageZap].multicallContract.get_stablecoins(_collateral, i));
|
|
206
205
|
}
|
|
207
|
-
const _stablecoins_arr = yield llamalend.multicallProvider.all(calls);
|
|
206
|
+
const _stablecoins_arr = yield this.llamalend.multicallProvider.all(calls);
|
|
208
207
|
const routeIdx = this._getBestIdx(_stablecoins_arr);
|
|
209
|
-
const stablecoins = llamalend.formatUnits(_stablecoins_arr[routeIdx]);
|
|
208
|
+
const stablecoins = this.llamalend.formatUnits(_stablecoins_arr[routeIdx]);
|
|
210
209
|
return { stablecoins, routeIdx };
|
|
211
210
|
}), {
|
|
212
211
|
promise: true,
|
|
213
212
|
maxAge: 5 * 60 * 1000, // 5m
|
|
214
213
|
});
|
|
215
214
|
this._deleverageRepayBands = memoize((collateral, address) => __awaiter(this, void 0, void 0, function* () {
|
|
216
|
-
address = _getAddress(address);
|
|
215
|
+
address = _getAddress.call(this.llamalend, address);
|
|
217
216
|
if (!(yield this.deleverageIsAvailable(collateral, address)))
|
|
218
217
|
return [parseUnits(0, 0), parseUnits(0, 0)];
|
|
219
218
|
const { routeIdx } = yield this.deleverageRepayStablecoins(collateral);
|
|
@@ -225,7 +224,7 @@ export class MintMarketTemplate {
|
|
|
225
224
|
let _n1 = parseUnits(0, 0);
|
|
226
225
|
let _n2 = parseUnits(0, 0);
|
|
227
226
|
try {
|
|
228
|
-
_n1 = yield llamalend.contracts[this.deleverageZap].contract.calculate_debt_n1(_collateral, routeIdx, address);
|
|
227
|
+
_n1 = yield this.llamalend.contracts[this.deleverageZap].contract.calculate_debt_n1(_collateral, routeIdx, address);
|
|
229
228
|
_n2 = _n1 + BigInt(N - 1);
|
|
230
229
|
}
|
|
231
230
|
catch (_a) {
|
|
@@ -236,7 +235,8 @@ export class MintMarketTemplate {
|
|
|
236
235
|
promise: true,
|
|
237
236
|
maxAge: 5 * 60 * 1000, // 5m
|
|
238
237
|
});
|
|
239
|
-
|
|
238
|
+
this.llamalend = llamalend;
|
|
239
|
+
const llammaData = this.llamalend.constants.LLAMMAS[id];
|
|
240
240
|
this.id = id;
|
|
241
241
|
this.address = llammaData.amm_address;
|
|
242
242
|
this.controller = llammaData.controller_address;
|
|
@@ -248,7 +248,7 @@ export class MintMarketTemplate {
|
|
|
248
248
|
this.collateralSymbol = llammaData.collateral_symbol;
|
|
249
249
|
this.collateralDecimals = Number(llammaData.collateral_decimals);
|
|
250
250
|
this.coins = ["crvUSD", llammaData.collateral_symbol];
|
|
251
|
-
this.coinAddresses = [llamalend.crvUsdAddress, llammaData.collateral_address];
|
|
251
|
+
this.coinAddresses = [this.llamalend.crvUsdAddress, llammaData.collateral_address];
|
|
252
252
|
this.coinDecimals = [18, Number(llammaData.collateral_decimals)];
|
|
253
253
|
this.minBands = llammaData.min_bands;
|
|
254
254
|
this.maxBands = llammaData.max_bands;
|
|
@@ -329,16 +329,16 @@ export class MintMarketTemplate {
|
|
|
329
329
|
}
|
|
330
330
|
statsBalances() {
|
|
331
331
|
return __awaiter(this, void 0, void 0, function* () {
|
|
332
|
-
const crvusdContract = llamalend.contracts[llamalend.crvUsdAddress].multicallContract;
|
|
333
|
-
const collateralContract = llamalend.contracts[isEth(this.collateral) ? llamalend.constants.WETH : this.collateral].multicallContract;
|
|
334
|
-
const contract = llamalend.contracts[this.address].multicallContract;
|
|
332
|
+
const crvusdContract = this.llamalend.contracts[this.llamalend.crvUsdAddress].multicallContract;
|
|
333
|
+
const collateralContract = this.llamalend.contracts[isEth(this.collateral) ? this.llamalend.constants.WETH : this.collateral].multicallContract;
|
|
334
|
+
const contract = this.llamalend.contracts[this.address].multicallContract;
|
|
335
335
|
const calls = [
|
|
336
336
|
crvusdContract.balanceOf(this.address),
|
|
337
337
|
collateralContract.balanceOf(this.address),
|
|
338
338
|
contract.admin_fees_x(),
|
|
339
339
|
contract.admin_fees_y(),
|
|
340
340
|
];
|
|
341
|
-
const [_crvusdBalance, _collateralBalance, _crvusdAdminFees, _collateralAdminFees] = yield llamalend.multicallProvider.all(calls);
|
|
341
|
+
const [_crvusdBalance, _collateralBalance, _crvusdAdminFees, _collateralAdminFees] = yield this.llamalend.multicallProvider.all(calls);
|
|
342
342
|
return [
|
|
343
343
|
formatUnits(_crvusdBalance - _crvusdAdminFees),
|
|
344
344
|
formatUnits(_collateralBalance - _collateralAdminFees, this.collateralDecimals),
|
|
@@ -356,10 +356,10 @@ export class MintMarketTemplate {
|
|
|
356
356
|
}
|
|
357
357
|
statsBandBalances(n) {
|
|
358
358
|
return __awaiter(this, void 0, void 0, function* () {
|
|
359
|
-
const llammaContract = llamalend.contracts[this.address].multicallContract;
|
|
359
|
+
const llammaContract = this.llamalend.contracts[this.address].multicallContract;
|
|
360
360
|
const calls = [];
|
|
361
361
|
calls.push(llammaContract.bands_x(n), llammaContract.bands_y(n));
|
|
362
|
-
const _balances = yield llamalend.multicallProvider.all(calls);
|
|
362
|
+
const _balances = yield this.llamalend.multicallProvider.all(calls);
|
|
363
363
|
return {
|
|
364
364
|
stablecoin: formatUnits(_balances[0]),
|
|
365
365
|
collateral: formatUnits(_balances[1], this.collateralDecimals),
|
|
@@ -369,12 +369,12 @@ export class MintMarketTemplate {
|
|
|
369
369
|
statsBandsBalances() {
|
|
370
370
|
return __awaiter(this, void 0, void 0, function* () {
|
|
371
371
|
const [max_band, min_band] = yield this.statsMaxMinBands();
|
|
372
|
-
const llammaContract = llamalend.contracts[this.address].multicallContract;
|
|
372
|
+
const llammaContract = this.llamalend.contracts[this.address].multicallContract;
|
|
373
373
|
const calls = [];
|
|
374
374
|
for (let i = min_band; i <= max_band; i++) {
|
|
375
375
|
calls.push(llammaContract.bands_x(i), llammaContract.bands_y(i));
|
|
376
376
|
}
|
|
377
|
-
const _bands = yield llamalend.multicallProvider.all(calls);
|
|
377
|
+
const _bands = yield this.llamalend.multicallProvider.all(calls);
|
|
378
378
|
const bands = {};
|
|
379
379
|
for (let i = min_band; i <= max_band; i++) {
|
|
380
380
|
const _i = i - min_band;
|
|
@@ -392,29 +392,29 @@ export class MintMarketTemplate {
|
|
|
392
392
|
// ---------------------------------------
|
|
393
393
|
loanExists() {
|
|
394
394
|
return __awaiter(this, arguments, void 0, function* (address = "") {
|
|
395
|
-
address = _getAddress(address);
|
|
396
|
-
return yield llamalend.contracts[this.controller].contract.loan_exists(address, llamalend.constantOptions);
|
|
395
|
+
address = _getAddress.call(this.llamalend, address);
|
|
396
|
+
return yield this.llamalend.contracts[this.controller].contract.loan_exists(address, this.llamalend.constantOptions);
|
|
397
397
|
});
|
|
398
398
|
}
|
|
399
399
|
userDebt() {
|
|
400
400
|
return __awaiter(this, arguments, void 0, function* (address = "") {
|
|
401
|
-
address = _getAddress(address);
|
|
402
|
-
const debt = yield llamalend.contracts[this.controller].contract.debt(address, llamalend.constantOptions);
|
|
401
|
+
address = _getAddress.call(this.llamalend, address);
|
|
402
|
+
const debt = yield this.llamalend.contracts[this.controller].contract.debt(address, this.llamalend.constantOptions);
|
|
403
403
|
return formatUnits(debt);
|
|
404
404
|
});
|
|
405
405
|
}
|
|
406
406
|
userHealth() {
|
|
407
407
|
return __awaiter(this, arguments, void 0, function* (full = true, address = "") {
|
|
408
|
-
address = _getAddress(address);
|
|
409
|
-
let _health = yield llamalend.contracts[this.controller].contract.health(address, full, llamalend.constantOptions);
|
|
408
|
+
address = _getAddress.call(this.llamalend, address);
|
|
409
|
+
let _health = yield this.llamalend.contracts[this.controller].contract.health(address, full, this.llamalend.constantOptions);
|
|
410
410
|
_health = _health * BigInt(100);
|
|
411
411
|
return formatUnits(_health);
|
|
412
412
|
});
|
|
413
413
|
}
|
|
414
414
|
userBands() {
|
|
415
415
|
return __awaiter(this, arguments, void 0, function* (address = "") {
|
|
416
|
-
address = _getAddress(address);
|
|
417
|
-
const _bands = yield llamalend.contracts[this.address].contract.read_user_tick_numbers(address, llamalend.constantOptions);
|
|
416
|
+
address = _getAddress.call(this.llamalend, address);
|
|
417
|
+
const _bands = yield this.llamalend.contracts[this.address].contract.read_user_tick_numbers(address, this.llamalend.constantOptions);
|
|
418
418
|
return _bands.map((_t) => Number(_t)).reverse();
|
|
419
419
|
});
|
|
420
420
|
}
|
|
@@ -428,16 +428,16 @@ export class MintMarketTemplate {
|
|
|
428
428
|
}
|
|
429
429
|
userPrices() {
|
|
430
430
|
return __awaiter(this, arguments, void 0, function* (address = "") {
|
|
431
|
-
address = _getAddress(address);
|
|
432
|
-
const _prices = yield llamalend.contracts[this.controller].contract.user_prices(address, llamalend.constantOptions);
|
|
431
|
+
address = _getAddress.call(this.llamalend, address);
|
|
432
|
+
const _prices = yield this.llamalend.contracts[this.controller].contract.user_prices(address, this.llamalend.constantOptions);
|
|
433
433
|
return _prices.map((_p) => formatUnits(_p)).reverse();
|
|
434
434
|
});
|
|
435
435
|
}
|
|
436
436
|
_userState() {
|
|
437
437
|
return __awaiter(this, arguments, void 0, function* (address = "") {
|
|
438
|
-
address = _getAddress(address);
|
|
439
|
-
const contract = llamalend.contracts[this.controller].contract;
|
|
440
|
-
const [_collateral, _stablecoin, _debt] = yield contract.user_state(address, llamalend.constantOptions);
|
|
438
|
+
address = _getAddress.call(this.llamalend, address);
|
|
439
|
+
const contract = this.llamalend.contracts[this.controller].contract;
|
|
440
|
+
const [_collateral, _stablecoin, _debt] = yield contract.user_state(address, this.llamalend.constantOptions);
|
|
441
441
|
return { _collateral, _stablecoin, _debt };
|
|
442
442
|
});
|
|
443
443
|
}
|
|
@@ -453,12 +453,12 @@ export class MintMarketTemplate {
|
|
|
453
453
|
}
|
|
454
454
|
userLoss() {
|
|
455
455
|
return __awaiter(this, arguments, void 0, function* (userAddress = "") {
|
|
456
|
-
userAddress = _getAddress(userAddress);
|
|
456
|
+
userAddress = _getAddress.call(this.llamalend, userAddress);
|
|
457
457
|
const [deposited_collateral, _current_collateral_estimation] = yield Promise.all([
|
|
458
|
-
_getUserCollateralCrvUsd(llamalend.constants.NETWORK_NAME, this.controller, userAddress),
|
|
459
|
-
llamalend.contracts[this.address].contract.get_y_up(userAddress),
|
|
458
|
+
_getUserCollateralCrvUsd(this.llamalend.constants.NETWORK_NAME, this.controller, userAddress),
|
|
459
|
+
this.llamalend.contracts[this.address].contract.get_y_up(userAddress),
|
|
460
460
|
]);
|
|
461
|
-
const current_collateral_estimation = llamalend.formatUnits(_current_collateral_estimation, this.collateralDecimals);
|
|
461
|
+
const current_collateral_estimation = this.llamalend.formatUnits(_current_collateral_estimation, this.collateralDecimals);
|
|
462
462
|
if (BN(deposited_collateral).lte(0)) {
|
|
463
463
|
return {
|
|
464
464
|
deposited_collateral,
|
|
@@ -482,9 +482,9 @@ export class MintMarketTemplate {
|
|
|
482
482
|
const [n2, n1] = yield this.userBands(address);
|
|
483
483
|
if (n1 == 0 && n2 == 0)
|
|
484
484
|
return {};
|
|
485
|
-
address = _getAddress(address);
|
|
486
|
-
const contract = llamalend.contracts[this.address].contract;
|
|
487
|
-
const [_stablecoins, _collaterals] = yield contract.get_xy(address, llamalend.constantOptions);
|
|
485
|
+
address = _getAddress.call(this.llamalend, address);
|
|
486
|
+
const contract = this.llamalend.contracts[this.address].contract;
|
|
487
|
+
const [_stablecoins, _collaterals] = yield contract.get_xy(address, this.llamalend.constantOptions);
|
|
488
488
|
const res = {};
|
|
489
489
|
for (let i = n1; i <= n2; i++) {
|
|
490
490
|
res[i] = {
|
|
@@ -497,7 +497,7 @@ export class MintMarketTemplate {
|
|
|
497
497
|
}
|
|
498
498
|
oraclePrice() {
|
|
499
499
|
return __awaiter(this, void 0, void 0, function* () {
|
|
500
|
-
const _price = yield llamalend.contracts[this.address].contract.price_oracle(llamalend.constantOptions);
|
|
500
|
+
const _price = yield this.llamalend.contracts[this.address].contract.price_oracle(this.llamalend.constantOptions);
|
|
501
501
|
return formatUnits(_price);
|
|
502
502
|
});
|
|
503
503
|
}
|
|
@@ -522,7 +522,7 @@ export class MintMarketTemplate {
|
|
|
522
522
|
}
|
|
523
523
|
price() {
|
|
524
524
|
return __awaiter(this, void 0, void 0, function* () {
|
|
525
|
-
const _price = yield llamalend.contracts[this.address].contract.get_p(llamalend.constantOptions);
|
|
525
|
+
const _price = yield this.llamalend.contracts[this.address].contract.get_p(this.llamalend.constantOptions);
|
|
526
526
|
return formatUnits(_price);
|
|
527
527
|
});
|
|
528
528
|
}
|
|
@@ -553,7 +553,7 @@ export class MintMarketTemplate {
|
|
|
553
553
|
// ---------------- WALLET BALANCES ----------------
|
|
554
554
|
walletBalances() {
|
|
555
555
|
return __awaiter(this, arguments, void 0, function* (address = "") {
|
|
556
|
-
const [collateral, stablecoin] = yield getBalances([this.collateral, llamalend.crvUsdAddress], address);
|
|
556
|
+
const [collateral, stablecoin] = yield getBalances.call(this.llamalend, [this.collateral, this.llamalend.crvUsdAddress], address);
|
|
557
557
|
return { stablecoin, collateral };
|
|
558
558
|
});
|
|
559
559
|
}
|
|
@@ -568,7 +568,7 @@ export class MintMarketTemplate {
|
|
|
568
568
|
return __awaiter(this, void 0, void 0, function* () {
|
|
569
569
|
this._checkRange(range);
|
|
570
570
|
const _collateral = parseUnits(collateral, this.collateralDecimals);
|
|
571
|
-
return formatUnits(yield llamalend.contracts[this.controller].contract.max_borrowable(_collateral, range, llamalend.constantOptions));
|
|
571
|
+
return formatUnits(yield this.llamalend.contracts[this.controller].contract.max_borrowable(_collateral, range, this.llamalend.constantOptions));
|
|
572
572
|
});
|
|
573
573
|
}
|
|
574
574
|
getMaxRange(collateral, debt) {
|
|
@@ -584,22 +584,22 @@ export class MintMarketTemplate {
|
|
|
584
584
|
_calcN1(_collateral, _debt, range) {
|
|
585
585
|
return __awaiter(this, void 0, void 0, function* () {
|
|
586
586
|
this._checkRange(range);
|
|
587
|
-
return yield llamalend.contracts[this.controller].contract.calculate_debt_n1(_collateral, _debt, range, llamalend.constantOptions);
|
|
587
|
+
return yield this.llamalend.contracts[this.controller].contract.calculate_debt_n1(_collateral, _debt, range, this.llamalend.constantOptions);
|
|
588
588
|
});
|
|
589
589
|
}
|
|
590
590
|
_calcN1AllRanges(_collateral, _debt, maxN) {
|
|
591
591
|
return __awaiter(this, void 0, void 0, function* () {
|
|
592
592
|
const calls = [];
|
|
593
593
|
for (let N = this.minBands; N <= maxN; N++) {
|
|
594
|
-
calls.push(llamalend.contracts[this.controller].multicallContract.calculate_debt_n1(_collateral, _debt, N));
|
|
594
|
+
calls.push(this.llamalend.contracts[this.controller].multicallContract.calculate_debt_n1(_collateral, _debt, N));
|
|
595
595
|
}
|
|
596
|
-
return yield llamalend.multicallProvider.all(calls);
|
|
596
|
+
return yield this.llamalend.multicallProvider.all(calls);
|
|
597
597
|
});
|
|
598
598
|
}
|
|
599
599
|
_getPrices(_n2, _n1) {
|
|
600
600
|
return __awaiter(this, void 0, void 0, function* () {
|
|
601
|
-
const contract = llamalend.contracts[this.address].multicallContract;
|
|
602
|
-
return (yield llamalend.multicallProvider.all([
|
|
601
|
+
const contract = this.llamalend.contracts[this.address].multicallContract;
|
|
602
|
+
return (yield this.llamalend.multicallProvider.all([
|
|
603
603
|
contract.p_oracle_down(_n2),
|
|
604
604
|
contract.p_oracle_up(_n1),
|
|
605
605
|
])).map((_p) => formatUnits(_p));
|
|
@@ -677,28 +677,28 @@ export class MintMarketTemplate {
|
|
|
677
677
|
createLoanHealth(collateral_1, debt_1, range_1) {
|
|
678
678
|
return __awaiter(this, arguments, void 0, function* (collateral, debt, range, full = true, address = "") {
|
|
679
679
|
var _a;
|
|
680
|
-
address = _getAddress(address);
|
|
680
|
+
address = _getAddress.call(this.llamalend, address);
|
|
681
681
|
const _collateral = parseUnits(collateral, this.collateralDecimals);
|
|
682
682
|
const _debt = parseUnits(debt);
|
|
683
|
-
const contract = llamalend.contracts[(_a = this.healthCalculator) !== null && _a !== void 0 ? _a : this.controller].contract;
|
|
684
|
-
let _health = yield contract.health_calculator(address, _collateral, _debt, full, range, llamalend.constantOptions);
|
|
683
|
+
const contract = this.llamalend.contracts[(_a = this.healthCalculator) !== null && _a !== void 0 ? _a : this.controller].contract;
|
|
684
|
+
let _health = yield contract.health_calculator(address, _collateral, _debt, full, range, this.llamalend.constantOptions);
|
|
685
685
|
_health = _health * BigInt(100);
|
|
686
686
|
return formatUnits(_health);
|
|
687
687
|
});
|
|
688
688
|
}
|
|
689
689
|
createLoanIsApproved(collateral) {
|
|
690
690
|
return __awaiter(this, void 0, void 0, function* () {
|
|
691
|
-
return yield hasAllowance([this.collateral], [collateral], llamalend.signerAddress, this.controller);
|
|
691
|
+
return yield hasAllowance.call(this.llamalend, [this.collateral], [collateral], this.llamalend.signerAddress, this.controller);
|
|
692
692
|
});
|
|
693
693
|
}
|
|
694
694
|
createLoanApproveEstimateGas(collateral) {
|
|
695
695
|
return __awaiter(this, void 0, void 0, function* () {
|
|
696
|
-
return yield ensureAllowanceEstimateGas([this.collateral], [collateral], this.controller);
|
|
696
|
+
return yield ensureAllowanceEstimateGas.call(this.llamalend, [this.collateral], [collateral], this.controller);
|
|
697
697
|
});
|
|
698
698
|
}
|
|
699
699
|
createLoanApprove(collateral) {
|
|
700
700
|
return __awaiter(this, void 0, void 0, function* () {
|
|
701
|
-
return yield ensureAllowance([this.collateral], [collateral], this.controller);
|
|
701
|
+
return yield ensureAllowance.call(this.llamalend, [this.collateral], [collateral], this.controller);
|
|
702
702
|
});
|
|
703
703
|
}
|
|
704
704
|
_createLoan(collateral, debt, range, estimateGas) {
|
|
@@ -708,14 +708,14 @@ export class MintMarketTemplate {
|
|
|
708
708
|
this._checkRange(range);
|
|
709
709
|
const _collateral = parseUnits(collateral, this.collateralDecimals);
|
|
710
710
|
const _debt = parseUnits(debt);
|
|
711
|
-
const contract = llamalend.contracts[this.controller].contract;
|
|
712
|
-
const value = isEth(this.collateral) ? _collateral : llamalend.parseUnits("0");
|
|
713
|
-
const gas = yield contract.create_loan.estimateGas(_collateral, _debt, range, Object.assign(Object.assign({}, llamalend.constantOptions), { value }));
|
|
711
|
+
const contract = this.llamalend.contracts[this.controller].contract;
|
|
712
|
+
const value = isEth(this.collateral) ? _collateral : this.llamalend.parseUnits("0");
|
|
713
|
+
const gas = yield contract.create_loan.estimateGas(_collateral, _debt, range, Object.assign(Object.assign({}, this.llamalend.constantOptions), { value }));
|
|
714
714
|
if (estimateGas)
|
|
715
715
|
return smartNumber(gas);
|
|
716
|
-
yield llamalend.updateFeeData();
|
|
716
|
+
yield this.llamalend.updateFeeData();
|
|
717
717
|
const gasLimit = _mulBy1_3(DIGas(gas));
|
|
718
|
-
return (yield contract.create_loan(_collateral, _debt, range, Object.assign(Object.assign({}, llamalend.options), { gasLimit, value }))).hash;
|
|
718
|
+
return (yield contract.create_loan(_collateral, _debt, range, Object.assign(Object.assign({}, this.llamalend.options), { gasLimit, value }))).hash;
|
|
719
719
|
});
|
|
720
720
|
}
|
|
721
721
|
createLoanEstimateGas(collateral, debt, range) {
|
|
@@ -737,8 +737,8 @@ export class MintMarketTemplate {
|
|
|
737
737
|
const { _collateral: _currentCollateral, _debt: _currentDebt } = yield this._userState();
|
|
738
738
|
const N = yield this.userRange();
|
|
739
739
|
const _collateral = _currentCollateral + parseUnits(collateralAmount, this.collateralDecimals);
|
|
740
|
-
const contract = llamalend.contracts[this.controller].contract;
|
|
741
|
-
const _debt = yield contract.max_borrowable(_collateral, N, llamalend.constantOptions);
|
|
740
|
+
const contract = this.llamalend.contracts[this.controller].contract;
|
|
741
|
+
const _debt = yield contract.max_borrowable(_collateral, N, this.llamalend.constantOptions);
|
|
742
742
|
return formatUnits(_debt - _currentDebt);
|
|
743
743
|
});
|
|
744
744
|
}
|
|
@@ -746,7 +746,7 @@ export class MintMarketTemplate {
|
|
|
746
746
|
return __awaiter(this, void 0, void 0, function* () {
|
|
747
747
|
const { _collateral: _currentCollateral, _debt: _currentDebt } = yield this._userState();
|
|
748
748
|
if (_currentDebt === BigInt(0))
|
|
749
|
-
throw Error(`Loan for ${llamalend.signerAddress} does not exist`);
|
|
749
|
+
throw Error(`Loan for ${this.llamalend.signerAddress} does not exist`);
|
|
750
750
|
const N = yield this.userRange();
|
|
751
751
|
const _collateral = _currentCollateral + parseUnits(collateral, this.collateralDecimals);
|
|
752
752
|
const _debt = _currentDebt + parseUnits(debt);
|
|
@@ -770,47 +770,47 @@ export class MintMarketTemplate {
|
|
|
770
770
|
borrowMoreHealth(collateral_1, debt_1) {
|
|
771
771
|
return __awaiter(this, arguments, void 0, function* (collateral, debt, full = true, address = "") {
|
|
772
772
|
var _a;
|
|
773
|
-
address = _getAddress(address);
|
|
773
|
+
address = _getAddress.call(this.llamalend, address);
|
|
774
774
|
const _collateral = parseUnits(collateral, this.collateralDecimals);
|
|
775
775
|
const _debt = parseUnits(debt);
|
|
776
|
-
const contract = llamalend.contracts[(_a = this.healthCalculator) !== null && _a !== void 0 ? _a : this.controller].contract;
|
|
777
|
-
let _health = yield contract.health_calculator(address, _collateral, _debt, full, 0, llamalend.constantOptions);
|
|
776
|
+
const contract = this.llamalend.contracts[(_a = this.healthCalculator) !== null && _a !== void 0 ? _a : this.controller].contract;
|
|
777
|
+
let _health = yield contract.health_calculator(address, _collateral, _debt, full, 0, this.llamalend.constantOptions);
|
|
778
778
|
_health = _health * BigInt(100);
|
|
779
779
|
return formatUnits(_health);
|
|
780
780
|
});
|
|
781
781
|
}
|
|
782
782
|
borrowMoreIsApproved(collateral) {
|
|
783
783
|
return __awaiter(this, void 0, void 0, function* () {
|
|
784
|
-
return yield hasAllowance([this.collateral], [collateral], llamalend.signerAddress, this.controller);
|
|
784
|
+
return yield hasAllowance.call(this.llamalend, [this.collateral], [collateral], this.llamalend.signerAddress, this.controller);
|
|
785
785
|
});
|
|
786
786
|
}
|
|
787
787
|
borrowMoreApproveEstimateGas(collateral) {
|
|
788
788
|
return __awaiter(this, void 0, void 0, function* () {
|
|
789
|
-
return yield ensureAllowanceEstimateGas([this.collateral], [collateral], this.controller);
|
|
789
|
+
return yield ensureAllowanceEstimateGas.call(this.llamalend, [this.collateral], [collateral], this.controller);
|
|
790
790
|
});
|
|
791
791
|
}
|
|
792
792
|
borrowMoreApprove(collateral) {
|
|
793
793
|
return __awaiter(this, void 0, void 0, function* () {
|
|
794
|
-
return yield ensureAllowance([this.collateral], [collateral], this.controller);
|
|
794
|
+
return yield ensureAllowance.call(this.llamalend, [this.collateral], [collateral], this.controller);
|
|
795
795
|
});
|
|
796
796
|
}
|
|
797
797
|
_borrowMore(collateral, debt, estimateGas) {
|
|
798
798
|
return __awaiter(this, void 0, void 0, function* () {
|
|
799
799
|
const { stablecoin, debt: currentDebt } = yield this.userState();
|
|
800
800
|
if (Number(currentDebt) === 0)
|
|
801
|
-
throw Error(`Loan for ${llamalend.signerAddress} does not exist`);
|
|
801
|
+
throw Error(`Loan for ${this.llamalend.signerAddress} does not exist`);
|
|
802
802
|
if (Number(stablecoin) > 0)
|
|
803
|
-
throw Error(`User ${llamalend.signerAddress} is already in liquidation mode`);
|
|
803
|
+
throw Error(`User ${this.llamalend.signerAddress} is already in liquidation mode`);
|
|
804
804
|
const _collateral = parseUnits(collateral, this.collateralDecimals);
|
|
805
805
|
const _debt = parseUnits(debt);
|
|
806
|
-
const contract = llamalend.contracts[this.controller].contract;
|
|
807
|
-
const value = isEth(this.collateral) ? _collateral : llamalend.parseUnits("0");
|
|
808
|
-
const gas = yield contract.borrow_more.estimateGas(_collateral, _debt, Object.assign(Object.assign({}, llamalend.constantOptions), { value }));
|
|
806
|
+
const contract = this.llamalend.contracts[this.controller].contract;
|
|
807
|
+
const value = isEth(this.collateral) ? _collateral : this.llamalend.parseUnits("0");
|
|
808
|
+
const gas = yield contract.borrow_more.estimateGas(_collateral, _debt, Object.assign(Object.assign({}, this.llamalend.constantOptions), { value }));
|
|
809
809
|
if (estimateGas)
|
|
810
810
|
return smartNumber(gas);
|
|
811
|
-
yield llamalend.updateFeeData();
|
|
811
|
+
yield this.llamalend.updateFeeData();
|
|
812
812
|
const gasLimit = _mulBy1_3(DIGas(gas));
|
|
813
|
-
return (yield contract.borrow_more(_collateral, _debt, Object.assign(Object.assign({}, llamalend.options), { gasLimit, value }))).hash;
|
|
813
|
+
return (yield contract.borrow_more(_collateral, _debt, Object.assign(Object.assign({}, this.llamalend.options), { gasLimit, value }))).hash;
|
|
814
814
|
});
|
|
815
815
|
}
|
|
816
816
|
borrowMoreEstimateGas(collateral, debt) {
|
|
@@ -829,7 +829,7 @@ export class MintMarketTemplate {
|
|
|
829
829
|
// ---------------- ADD COLLATERAL ----------------
|
|
830
830
|
_addCollateralBands(collateral_1) {
|
|
831
831
|
return __awaiter(this, arguments, void 0, function* (collateral, address = "") {
|
|
832
|
-
address = _getAddress(address);
|
|
832
|
+
address = _getAddress.call(this.llamalend, address);
|
|
833
833
|
const { _collateral: _currentCollateral, _debt: _currentDebt } = yield this._userState(address);
|
|
834
834
|
if (_currentDebt === BigInt(0))
|
|
835
835
|
throw Error(`Loan for ${address} does not exist`);
|
|
@@ -855,27 +855,27 @@ export class MintMarketTemplate {
|
|
|
855
855
|
addCollateralHealth(collateral_1) {
|
|
856
856
|
return __awaiter(this, arguments, void 0, function* (collateral, full = true, address = "") {
|
|
857
857
|
var _a;
|
|
858
|
-
address = _getAddress(address);
|
|
858
|
+
address = _getAddress.call(this.llamalend, address);
|
|
859
859
|
const _collateral = parseUnits(collateral, this.collateralDecimals);
|
|
860
|
-
const contract = llamalend.contracts[(_a = this.healthCalculator) !== null && _a !== void 0 ? _a : this.controller].contract;
|
|
861
|
-
let _health = yield contract.health_calculator(address, _collateral, 0, full, 0, llamalend.constantOptions);
|
|
860
|
+
const contract = this.llamalend.contracts[(_a = this.healthCalculator) !== null && _a !== void 0 ? _a : this.controller].contract;
|
|
861
|
+
let _health = yield contract.health_calculator(address, _collateral, 0, full, 0, this.llamalend.constantOptions);
|
|
862
862
|
_health = _health * BigInt(100);
|
|
863
863
|
return formatUnits(_health);
|
|
864
864
|
});
|
|
865
865
|
}
|
|
866
866
|
addCollateralIsApproved(collateral) {
|
|
867
867
|
return __awaiter(this, void 0, void 0, function* () {
|
|
868
|
-
return yield hasAllowance([this.collateral], [collateral], llamalend.signerAddress, this.controller);
|
|
868
|
+
return yield hasAllowance.call(this.llamalend, [this.collateral], [collateral], this.llamalend.signerAddress, this.controller);
|
|
869
869
|
});
|
|
870
870
|
}
|
|
871
871
|
addCollateralApproveEstimateGas(collateral) {
|
|
872
872
|
return __awaiter(this, void 0, void 0, function* () {
|
|
873
|
-
return yield ensureAllowanceEstimateGas([this.collateral], [collateral], this.controller);
|
|
873
|
+
return yield ensureAllowanceEstimateGas.call(this.llamalend, [this.collateral], [collateral], this.controller);
|
|
874
874
|
});
|
|
875
875
|
}
|
|
876
876
|
addCollateralApprove(collateral) {
|
|
877
877
|
return __awaiter(this, void 0, void 0, function* () {
|
|
878
|
-
return yield ensureAllowance([this.collateral], [collateral], this.controller);
|
|
878
|
+
return yield ensureAllowance.call(this.llamalend, [this.collateral], [collateral], this.controller);
|
|
879
879
|
});
|
|
880
880
|
}
|
|
881
881
|
_addCollateral(collateral, address, estimateGas) {
|
|
@@ -886,19 +886,19 @@ export class MintMarketTemplate {
|
|
|
886
886
|
if (Number(stablecoin) > 0)
|
|
887
887
|
throw Error(`User ${address} is already in liquidation mode`);
|
|
888
888
|
const _collateral = parseUnits(collateral, this.collateralDecimals);
|
|
889
|
-
const contract = llamalend.contracts[this.controller].contract;
|
|
890
|
-
const value = isEth(this.collateral) ? _collateral : llamalend.parseUnits("0");
|
|
891
|
-
const gas = yield contract.add_collateral.estimateGas(_collateral, address, Object.assign(Object.assign({}, llamalend.constantOptions), { value }));
|
|
889
|
+
const contract = this.llamalend.contracts[this.controller].contract;
|
|
890
|
+
const value = isEth(this.collateral) ? _collateral : this.llamalend.parseUnits("0");
|
|
891
|
+
const gas = yield contract.add_collateral.estimateGas(_collateral, address, Object.assign(Object.assign({}, this.llamalend.constantOptions), { value }));
|
|
892
892
|
if (estimateGas)
|
|
893
893
|
return smartNumber(gas);
|
|
894
|
-
yield llamalend.updateFeeData();
|
|
894
|
+
yield this.llamalend.updateFeeData();
|
|
895
895
|
const gasLimit = _mulBy1_3(DIGas(gas));
|
|
896
|
-
return (yield contract.add_collateral(_collateral, address, Object.assign(Object.assign({}, llamalend.options), { gasLimit, value }))).hash;
|
|
896
|
+
return (yield contract.add_collateral(_collateral, address, Object.assign(Object.assign({}, this.llamalend.options), { gasLimit, value }))).hash;
|
|
897
897
|
});
|
|
898
898
|
}
|
|
899
899
|
addCollateralEstimateGas(collateral_1) {
|
|
900
900
|
return __awaiter(this, arguments, void 0, function* (collateral, address = "") {
|
|
901
|
-
address = _getAddress(address);
|
|
901
|
+
address = _getAddress.call(this.llamalend, address);
|
|
902
902
|
if (!(yield this.addCollateralIsApproved(collateral)))
|
|
903
903
|
throw Error("Approval is needed for gas estimation");
|
|
904
904
|
return yield this._addCollateral(collateral, address, true);
|
|
@@ -906,7 +906,7 @@ export class MintMarketTemplate {
|
|
|
906
906
|
}
|
|
907
907
|
addCollateral(collateral_1) {
|
|
908
908
|
return __awaiter(this, arguments, void 0, function* (collateral, address = "") {
|
|
909
|
-
address = _getAddress(address);
|
|
909
|
+
address = _getAddress.call(this.llamalend, address);
|
|
910
910
|
yield this.addCollateralApprove(collateral);
|
|
911
911
|
return yield this._addCollateral(collateral, address, false);
|
|
912
912
|
});
|
|
@@ -916,7 +916,7 @@ export class MintMarketTemplate {
|
|
|
916
916
|
return __awaiter(this, void 0, void 0, function* () {
|
|
917
917
|
const { _collateral: _currentCollateral, _debt: _currentDebt } = yield this._userState();
|
|
918
918
|
const N = yield this.userRange();
|
|
919
|
-
const _requiredCollateral = yield llamalend.contracts[this.controller].contract.min_collateral(_currentDebt, N, llamalend.constantOptions);
|
|
919
|
+
const _requiredCollateral = yield this.llamalend.contracts[this.controller].contract.min_collateral(_currentDebt, N, this.llamalend.constantOptions);
|
|
920
920
|
return formatUnits(_currentCollateral - _requiredCollateral, this.collateralDecimals);
|
|
921
921
|
});
|
|
922
922
|
}
|
|
@@ -924,7 +924,7 @@ export class MintMarketTemplate {
|
|
|
924
924
|
return __awaiter(this, void 0, void 0, function* () {
|
|
925
925
|
const { _collateral: _currentCollateral, _debt: _currentDebt } = yield this._userState();
|
|
926
926
|
if (_currentDebt === BigInt(0))
|
|
927
|
-
throw Error(`Loan for ${llamalend.signerAddress} does not exist`);
|
|
927
|
+
throw Error(`Loan for ${this.llamalend.signerAddress} does not exist`);
|
|
928
928
|
const N = yield this.userRange();
|
|
929
929
|
const _collateral = _currentCollateral - parseUnits(collateral, this.collateralDecimals);
|
|
930
930
|
const _n1 = yield this._calcN1(_collateral, _currentDebt, N);
|
|
@@ -947,10 +947,10 @@ export class MintMarketTemplate {
|
|
|
947
947
|
removeCollateralHealth(collateral_1) {
|
|
948
948
|
return __awaiter(this, arguments, void 0, function* (collateral, full = true, address = "") {
|
|
949
949
|
var _a;
|
|
950
|
-
address = _getAddress(address);
|
|
950
|
+
address = _getAddress.call(this.llamalend, address);
|
|
951
951
|
const _collateral = parseUnits(collateral, this.collateralDecimals) * BigInt(-1);
|
|
952
|
-
const contract = llamalend.contracts[(_a = this.healthCalculator) !== null && _a !== void 0 ? _a : this.controller].contract;
|
|
953
|
-
let _health = yield contract.health_calculator(address, _collateral, 0, full, 0, llamalend.constantOptions);
|
|
952
|
+
const contract = this.llamalend.contracts[(_a = this.healthCalculator) !== null && _a !== void 0 ? _a : this.controller].contract;
|
|
953
|
+
let _health = yield contract.health_calculator(address, _collateral, 0, full, 0, this.llamalend.constantOptions);
|
|
954
954
|
_health = _health * BigInt(100);
|
|
955
955
|
return formatUnits(_health);
|
|
956
956
|
});
|
|
@@ -959,17 +959,17 @@ export class MintMarketTemplate {
|
|
|
959
959
|
return __awaiter(this, void 0, void 0, function* () {
|
|
960
960
|
const { stablecoin, debt: currentDebt } = yield this.userState();
|
|
961
961
|
if (Number(currentDebt) === 0)
|
|
962
|
-
throw Error(`Loan for ${llamalend.signerAddress} does not exist`);
|
|
962
|
+
throw Error(`Loan for ${this.llamalend.signerAddress} does not exist`);
|
|
963
963
|
if (Number(stablecoin) > 0)
|
|
964
|
-
throw Error(`User ${llamalend.signerAddress} is already in liquidation mode`);
|
|
964
|
+
throw Error(`User ${this.llamalend.signerAddress} is already in liquidation mode`);
|
|
965
965
|
const _collateral = parseUnits(collateral, this.collateralDecimals);
|
|
966
|
-
const contract = llamalend.contracts[this.controller].contract;
|
|
967
|
-
const gas = this.isDeleverageSupported ? yield contract.remove_collateral.estimateGas(_collateral, llamalend.constantOptions) : yield contract.remove_collateral.estimateGas(_collateral, isEth(this.collateral), llamalend.constantOptions);
|
|
966
|
+
const contract = this.llamalend.contracts[this.controller].contract;
|
|
967
|
+
const gas = this.isDeleverageSupported ? yield contract.remove_collateral.estimateGas(_collateral, this.llamalend.constantOptions) : yield contract.remove_collateral.estimateGas(_collateral, isEth(this.collateral), this.llamalend.constantOptions);
|
|
968
968
|
if (estimateGas)
|
|
969
969
|
return smartNumber(gas);
|
|
970
|
-
yield llamalend.updateFeeData();
|
|
970
|
+
yield this.llamalend.updateFeeData();
|
|
971
971
|
const gasLimit = _mulBy1_3(DIGas(gas));
|
|
972
|
-
return (this.isDeleverageSupported ? yield contract.remove_collateral(_collateral, Object.assign(Object.assign({}, llamalend.options), { gasLimit })) : yield contract.remove_collateral(_collateral, isEth(this.collateral), Object.assign(Object.assign({}, llamalend.options), { gasLimit }))).hash;
|
|
972
|
+
return (this.isDeleverageSupported ? yield contract.remove_collateral(_collateral, Object.assign(Object.assign({}, this.llamalend.options), { gasLimit })) : yield contract.remove_collateral(_collateral, isEth(this.collateral), Object.assign(Object.assign({}, this.llamalend.options), { gasLimit }))).hash;
|
|
973
973
|
});
|
|
974
974
|
}
|
|
975
975
|
removeCollateralEstimateGas(collateral) {
|
|
@@ -990,7 +990,7 @@ export class MintMarketTemplate {
|
|
|
990
990
|
throw Error(`Loan for ${address} does not exist`);
|
|
991
991
|
const N = yield this.userRange(address);
|
|
992
992
|
const _debt = _currentDebt - parseUnits(debt);
|
|
993
|
-
const _n1 = _currentStablecoin === BigInt(0) ? yield this._calcN1(_currentCollateral, _debt, N) : (yield llamalend.contracts[this.address].contract.read_user_tick_numbers(address, llamalend.constantOptions))[0];
|
|
993
|
+
const _n1 = _currentStablecoin === BigInt(0) ? yield this._calcN1(_currentCollateral, _debt, N) : (yield this.llamalend.contracts[this.address].contract.read_user_tick_numbers(address, this.llamalend.constantOptions))[0];
|
|
994
994
|
const _n2 = _n1 + BigInt(N - 1);
|
|
995
995
|
return [_n2, _n1];
|
|
996
996
|
});
|
|
@@ -1009,47 +1009,47 @@ export class MintMarketTemplate {
|
|
|
1009
1009
|
}
|
|
1010
1010
|
repayIsApproved(debt) {
|
|
1011
1011
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1012
|
-
return yield hasAllowance([llamalend.crvUsdAddress], [debt], llamalend.signerAddress, this.controller);
|
|
1012
|
+
return yield hasAllowance.call(this.llamalend, [this.llamalend.crvUsdAddress], [debt], this.llamalend.signerAddress, this.controller);
|
|
1013
1013
|
});
|
|
1014
1014
|
}
|
|
1015
1015
|
repayApproveEstimateGas(debt) {
|
|
1016
1016
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1017
|
-
return yield ensureAllowanceEstimateGas([llamalend.crvUsdAddress], [debt], this.controller);
|
|
1017
|
+
return yield ensureAllowanceEstimateGas.call(this.llamalend, [this.llamalend.crvUsdAddress], [debt], this.controller);
|
|
1018
1018
|
});
|
|
1019
1019
|
}
|
|
1020
1020
|
repayApprove(debt) {
|
|
1021
1021
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1022
|
-
return yield ensureAllowance([llamalend.crvUsdAddress], [debt], this.controller);
|
|
1022
|
+
return yield ensureAllowance.call(this.llamalend, [this.llamalend.crvUsdAddress], [debt], this.controller);
|
|
1023
1023
|
});
|
|
1024
1024
|
}
|
|
1025
1025
|
repayHealth(debt_1) {
|
|
1026
1026
|
return __awaiter(this, arguments, void 0, function* (debt, full = true, address = "") {
|
|
1027
1027
|
var _a;
|
|
1028
|
-
address = _getAddress(address);
|
|
1028
|
+
address = _getAddress.call(this.llamalend, address);
|
|
1029
1029
|
const _debt = parseUnits(debt) * BigInt(-1);
|
|
1030
|
-
const contract = llamalend.contracts[(_a = this.healthCalculator) !== null && _a !== void 0 ? _a : this.controller].contract;
|
|
1031
|
-
let _health = yield contract.health_calculator(address, 0, _debt, full, 0, llamalend.constantOptions);
|
|
1030
|
+
const contract = this.llamalend.contracts[(_a = this.healthCalculator) !== null && _a !== void 0 ? _a : this.controller].contract;
|
|
1031
|
+
let _health = yield contract.health_calculator(address, 0, _debt, full, 0, this.llamalend.constantOptions);
|
|
1032
1032
|
_health = _health * BigInt(100);
|
|
1033
1033
|
return formatUnits(_health);
|
|
1034
1034
|
});
|
|
1035
1035
|
}
|
|
1036
1036
|
_repay(debt, address, estimateGas) {
|
|
1037
1037
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1038
|
-
address = _getAddress(address);
|
|
1038
|
+
address = _getAddress.call(this.llamalend, address);
|
|
1039
1039
|
const { debt: currentDebt } = yield this.userState(address);
|
|
1040
1040
|
if (Number(currentDebt) === 0)
|
|
1041
1041
|
throw Error(`Loan for ${address} does not exist`);
|
|
1042
1042
|
const _debt = parseUnits(debt);
|
|
1043
|
-
const contract = llamalend.contracts[this.controller].contract;
|
|
1043
|
+
const contract = this.llamalend.contracts[this.controller].contract;
|
|
1044
1044
|
const [, n1] = yield this.userBands(address);
|
|
1045
1045
|
const { stablecoin } = yield this.userState(address);
|
|
1046
1046
|
const n = (BN(stablecoin).gt(0)) ? MAX_ACTIVE_BAND : n1 - 1; // In liquidation mode it doesn't matter if active band moves
|
|
1047
|
-
const gas = this.isDeleverageSupported ? yield contract.repay.estimateGas(_debt, address, n, llamalend.constantOptions) : yield contract.repay.estimateGas(_debt, address, n, isEth(this.collateral), llamalend.constantOptions);
|
|
1047
|
+
const gas = this.isDeleverageSupported ? yield contract.repay.estimateGas(_debt, address, n, this.llamalend.constantOptions) : yield contract.repay.estimateGas(_debt, address, n, isEth(this.collateral), this.llamalend.constantOptions);
|
|
1048
1048
|
if (estimateGas)
|
|
1049
1049
|
return smartNumber(gas);
|
|
1050
|
-
yield llamalend.updateFeeData();
|
|
1050
|
+
yield this.llamalend.updateFeeData();
|
|
1051
1051
|
const gasLimit = _mulBy1_3(DIGas(gas));
|
|
1052
|
-
return (this.isDeleverageSupported ? yield contract.repay(_debt, address, n, Object.assign(Object.assign({}, llamalend.options), { gasLimit })) : yield contract.repay(_debt, address, n, isEth(this.collateral), Object.assign(Object.assign({}, llamalend.options), { gasLimit }))).hash;
|
|
1052
|
+
return (this.isDeleverageSupported ? yield contract.repay(_debt, address, n, Object.assign(Object.assign({}, this.llamalend.options), { gasLimit })) : yield contract.repay(_debt, address, n, isEth(this.collateral), Object.assign(Object.assign({}, this.llamalend.options), { gasLimit }))).hash;
|
|
1053
1053
|
});
|
|
1054
1054
|
}
|
|
1055
1055
|
repayEstimateGas(debt_1) {
|
|
@@ -1068,35 +1068,35 @@ export class MintMarketTemplate {
|
|
|
1068
1068
|
// ---------------- FULL REPAY ----------------
|
|
1069
1069
|
_fullRepayAmount() {
|
|
1070
1070
|
return __awaiter(this, arguments, void 0, function* (address = "") {
|
|
1071
|
-
address = _getAddress(address);
|
|
1071
|
+
address = _getAddress.call(this.llamalend, address);
|
|
1072
1072
|
const debt = yield this.userDebt(address);
|
|
1073
1073
|
return BN(debt).times(1.0001).toString();
|
|
1074
1074
|
});
|
|
1075
1075
|
}
|
|
1076
1076
|
fullRepayIsApproved() {
|
|
1077
1077
|
return __awaiter(this, arguments, void 0, function* (address = "") {
|
|
1078
|
-
address = _getAddress(address);
|
|
1078
|
+
address = _getAddress.call(this.llamalend, address);
|
|
1079
1079
|
const fullRepayAmount = yield this._fullRepayAmount(address);
|
|
1080
1080
|
return yield this.repayIsApproved(fullRepayAmount);
|
|
1081
1081
|
});
|
|
1082
1082
|
}
|
|
1083
1083
|
fullRepayApproveEstimateGas() {
|
|
1084
1084
|
return __awaiter(this, arguments, void 0, function* (address = "") {
|
|
1085
|
-
address = _getAddress(address);
|
|
1085
|
+
address = _getAddress.call(this.llamalend, address);
|
|
1086
1086
|
const fullRepayAmount = yield this._fullRepayAmount(address);
|
|
1087
1087
|
return yield this.repayApproveEstimateGas(fullRepayAmount);
|
|
1088
1088
|
});
|
|
1089
1089
|
}
|
|
1090
1090
|
fullRepayApprove() {
|
|
1091
1091
|
return __awaiter(this, arguments, void 0, function* (address = "") {
|
|
1092
|
-
address = _getAddress(address);
|
|
1092
|
+
address = _getAddress.call(this.llamalend, address);
|
|
1093
1093
|
const fullRepayAmount = yield this._fullRepayAmount(address);
|
|
1094
1094
|
return yield this.repayApprove(fullRepayAmount);
|
|
1095
1095
|
});
|
|
1096
1096
|
}
|
|
1097
1097
|
fullRepayEstimateGas() {
|
|
1098
1098
|
return __awaiter(this, arguments, void 0, function* (address = "") {
|
|
1099
|
-
address = _getAddress(address);
|
|
1099
|
+
address = _getAddress.call(this.llamalend, address);
|
|
1100
1100
|
const fullRepayAmount = yield this._fullRepayAmount(address);
|
|
1101
1101
|
if (!(yield this.repayIsApproved(fullRepayAmount)))
|
|
1102
1102
|
throw Error("Approval is needed for gas estimation");
|
|
@@ -1105,7 +1105,7 @@ export class MintMarketTemplate {
|
|
|
1105
1105
|
}
|
|
1106
1106
|
fullRepay() {
|
|
1107
1107
|
return __awaiter(this, arguments, void 0, function* (address = "") {
|
|
1108
|
-
address = _getAddress(address);
|
|
1108
|
+
address = _getAddress.call(this.llamalend, address);
|
|
1109
1109
|
const fullRepayAmount = yield this._fullRepayAmount(address);
|
|
1110
1110
|
yield this.repayApprove(fullRepayAmount);
|
|
1111
1111
|
return yield this._repay(fullRepayAmount, address, false);
|
|
@@ -1117,8 +1117,8 @@ export class MintMarketTemplate {
|
|
|
1117
1117
|
if (!(i === 0 && j === 1) && !(i === 1 && j === 0))
|
|
1118
1118
|
throw Error("Wrong index");
|
|
1119
1119
|
const inDecimals = this.coinDecimals[i];
|
|
1120
|
-
const contract = llamalend.contracts[this.address].contract;
|
|
1121
|
-
const [_inAmount, _outAmount] = yield contract.get_dxdy(i, j, MAX_ALLOWANCE, llamalend.constantOptions);
|
|
1120
|
+
const contract = this.llamalend.contracts[this.address].contract;
|
|
1121
|
+
const [_inAmount, _outAmount] = yield contract.get_dxdy(i, j, MAX_ALLOWANCE, this.llamalend.constantOptions);
|
|
1122
1122
|
if (_outAmount === BigInt(0))
|
|
1123
1123
|
return "0";
|
|
1124
1124
|
return formatUnits(_inAmount, inDecimals);
|
|
@@ -1126,7 +1126,7 @@ export class MintMarketTemplate {
|
|
|
1126
1126
|
}
|
|
1127
1127
|
_swapExpected(i, j, _amount) {
|
|
1128
1128
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1129
|
-
return yield llamalend.contracts[this.address].contract.get_dy(i, j, _amount, llamalend.constantOptions);
|
|
1129
|
+
return yield this.llamalend.contracts[this.address].contract.get_dy(i, j, _amount, this.llamalend.constantOptions);
|
|
1130
1130
|
});
|
|
1131
1131
|
}
|
|
1132
1132
|
swapExpected(i, j, amount) {
|
|
@@ -1145,7 +1145,7 @@ export class MintMarketTemplate {
|
|
|
1145
1145
|
throw Error("Wrong index");
|
|
1146
1146
|
const [inDecimals, outDecimals] = this.coinDecimals;
|
|
1147
1147
|
const _amount = parseUnits(outAmount, outDecimals);
|
|
1148
|
-
const _expected = yield llamalend.contracts[this.address].contract.get_dx(i, j, _amount, llamalend.constantOptions);
|
|
1148
|
+
const _expected = yield this.llamalend.contracts[this.address].contract.get_dx(i, j, _amount, this.llamalend.constantOptions);
|
|
1149
1149
|
return formatUnits(_expected, inDecimals);
|
|
1150
1150
|
});
|
|
1151
1151
|
}
|
|
@@ -1184,21 +1184,21 @@ export class MintMarketTemplate {
|
|
|
1184
1184
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1185
1185
|
if (i !== 0 && i !== 1)
|
|
1186
1186
|
throw Error("Wrong index");
|
|
1187
|
-
return yield hasAllowance([this.coinAddresses[i]], [amount], llamalend.signerAddress, this.address);
|
|
1187
|
+
return yield hasAllowance.call(this.llamalend, [this.coinAddresses[i]], [amount], this.llamalend.signerAddress, this.address);
|
|
1188
1188
|
});
|
|
1189
1189
|
}
|
|
1190
1190
|
swapApproveEstimateGas(i, amount) {
|
|
1191
1191
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1192
1192
|
if (i !== 0 && i !== 1)
|
|
1193
1193
|
throw Error("Wrong index");
|
|
1194
|
-
return yield ensureAllowanceEstimateGas([this.coinAddresses[i]], [amount], this.address);
|
|
1194
|
+
return yield ensureAllowanceEstimateGas.call(this.llamalend, [this.coinAddresses[i]], [amount], this.address);
|
|
1195
1195
|
});
|
|
1196
1196
|
}
|
|
1197
1197
|
swapApprove(i, amount) {
|
|
1198
1198
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1199
1199
|
if (i !== 0 && i !== 1)
|
|
1200
1200
|
throw Error("Wrong index");
|
|
1201
|
-
return yield ensureAllowance([this.coinAddresses[i]], [amount], this.address);
|
|
1201
|
+
return yield ensureAllowance.call(this.llamalend, [this.coinAddresses[i]], [amount], this.address);
|
|
1202
1202
|
});
|
|
1203
1203
|
}
|
|
1204
1204
|
_swap(i, j, amount, slippage, estimateGas) {
|
|
@@ -1210,13 +1210,13 @@ export class MintMarketTemplate {
|
|
|
1210
1210
|
const _expected = yield this._swapExpected(i, j, _amount);
|
|
1211
1211
|
const minRecvAmountBN = toBN(_expected, outDecimals).times(100 - slippage).div(100);
|
|
1212
1212
|
const _minRecvAmount = fromBN(minRecvAmountBN, outDecimals);
|
|
1213
|
-
const contract = llamalend.contracts[this.address].contract;
|
|
1214
|
-
const gas = yield contract.exchange.estimateGas(i, j, _amount, _minRecvAmount, llamalend.constantOptions);
|
|
1213
|
+
const contract = this.llamalend.contracts[this.address].contract;
|
|
1214
|
+
const gas = yield contract.exchange.estimateGas(i, j, _amount, _minRecvAmount, this.llamalend.constantOptions);
|
|
1215
1215
|
if (estimateGas)
|
|
1216
1216
|
return smartNumber(gas);
|
|
1217
|
-
yield llamalend.updateFeeData();
|
|
1217
|
+
yield this.llamalend.updateFeeData();
|
|
1218
1218
|
const gasLimit = _mulBy1_3(DIGas(gas));
|
|
1219
|
-
return (yield contract.exchange(i, j, _amount, _minRecvAmount, Object.assign(Object.assign({}, llamalend.options), { gasLimit }))).hash;
|
|
1219
|
+
return (yield contract.exchange(i, j, _amount, _minRecvAmount, Object.assign(Object.assign({}, this.llamalend.options), { gasLimit }))).hash;
|
|
1220
1220
|
});
|
|
1221
1221
|
}
|
|
1222
1222
|
swapEstimateGas(i_1, j_1, amount_1) {
|
|
@@ -1235,27 +1235,27 @@ export class MintMarketTemplate {
|
|
|
1235
1235
|
// ---------------- LIQUIDATE ----------------
|
|
1236
1236
|
tokensToLiquidate() {
|
|
1237
1237
|
return __awaiter(this, arguments, void 0, function* (address = "") {
|
|
1238
|
-
address = _getAddress(address);
|
|
1239
|
-
const _tokens = yield llamalend.contracts[this.controller].contract.tokens_to_liquidate(address, llamalend.constantOptions);
|
|
1238
|
+
address = _getAddress.call(this.llamalend, address);
|
|
1239
|
+
const _tokens = yield this.llamalend.contracts[this.controller].contract.tokens_to_liquidate(address, this.llamalend.constantOptions);
|
|
1240
1240
|
return formatUnits(_tokens);
|
|
1241
1241
|
});
|
|
1242
1242
|
}
|
|
1243
1243
|
liquidateIsApproved() {
|
|
1244
1244
|
return __awaiter(this, arguments, void 0, function* (address = "") {
|
|
1245
1245
|
const tokensToLiquidate = yield this.tokensToLiquidate(address);
|
|
1246
|
-
return yield hasAllowance([llamalend.crvUsdAddress], [tokensToLiquidate], llamalend.signerAddress, this.controller);
|
|
1246
|
+
return yield hasAllowance.call(this.llamalend, [this.llamalend.crvUsdAddress], [tokensToLiquidate], this.llamalend.signerAddress, this.controller);
|
|
1247
1247
|
});
|
|
1248
1248
|
}
|
|
1249
1249
|
liquidateApproveEstimateGas() {
|
|
1250
1250
|
return __awaiter(this, arguments, void 0, function* (address = "") {
|
|
1251
1251
|
const tokensToLiquidate = yield this.tokensToLiquidate(address);
|
|
1252
|
-
return yield ensureAllowanceEstimateGas([llamalend.crvUsdAddress], [tokensToLiquidate], this.controller);
|
|
1252
|
+
return yield ensureAllowanceEstimateGas.call(this.llamalend, [this.llamalend.crvUsdAddress], [tokensToLiquidate], this.controller);
|
|
1253
1253
|
});
|
|
1254
1254
|
}
|
|
1255
1255
|
liquidateApprove() {
|
|
1256
1256
|
return __awaiter(this, arguments, void 0, function* (address = "") {
|
|
1257
1257
|
const tokensToLiquidate = yield this.tokensToLiquidate(address);
|
|
1258
|
-
return yield ensureAllowance([llamalend.crvUsdAddress], [tokensToLiquidate], this.controller);
|
|
1258
|
+
return yield ensureAllowance.call(this.llamalend, [this.llamalend.crvUsdAddress], [tokensToLiquidate], this.controller);
|
|
1259
1259
|
});
|
|
1260
1260
|
}
|
|
1261
1261
|
_liquidate(address, slippage, estimateGas) {
|
|
@@ -1271,13 +1271,13 @@ export class MintMarketTemplate {
|
|
|
1271
1271
|
throw Error(`User ${address} is not in liquidation mode`);
|
|
1272
1272
|
const minAmountBN = BN(stablecoin).times(100 - slippage).div(100);
|
|
1273
1273
|
const _minAmount = fromBN(minAmountBN);
|
|
1274
|
-
const contract = llamalend.contracts[this.controller].contract;
|
|
1275
|
-
const gas = this.isDeleverageSupported ? (yield contract.liquidate.estimateGas(address, _minAmount, llamalend.constantOptions)) : (yield contract.liquidate.estimateGas(address, _minAmount, isEth(this.collateral), llamalend.constantOptions));
|
|
1274
|
+
const contract = this.llamalend.contracts[this.controller].contract;
|
|
1275
|
+
const gas = this.isDeleverageSupported ? (yield contract.liquidate.estimateGas(address, _minAmount, this.llamalend.constantOptions)) : (yield contract.liquidate.estimateGas(address, _minAmount, isEth(this.collateral), this.llamalend.constantOptions));
|
|
1276
1276
|
if (estimateGas)
|
|
1277
1277
|
return smartNumber(gas);
|
|
1278
|
-
yield llamalend.updateFeeData();
|
|
1278
|
+
yield this.llamalend.updateFeeData();
|
|
1279
1279
|
const gasLimit = _mulBy1_3(DIGas(gas));
|
|
1280
|
-
return (this.isDeleverageSupported ? yield contract.liquidate(address, _minAmount, Object.assign(Object.assign({}, llamalend.options), { gasLimit })) : yield contract.liquidate(address, _minAmount, isEth(this.collateral), Object.assign(Object.assign({}, llamalend.options), { gasLimit }))).hash;
|
|
1280
|
+
return (this.isDeleverageSupported ? yield contract.liquidate(address, _minAmount, Object.assign(Object.assign({}, this.llamalend.options), { gasLimit })) : yield contract.liquidate(address, _minAmount, isEth(this.collateral), Object.assign(Object.assign({}, this.llamalend.options), { gasLimit }))).hash;
|
|
1281
1281
|
});
|
|
1282
1282
|
}
|
|
1283
1283
|
liquidateEstimateGas(address_1) {
|
|
@@ -1313,13 +1313,13 @@ export class MintMarketTemplate {
|
|
|
1313
1313
|
return __awaiter(this, arguments, void 0, function* (slippage = 0.1) {
|
|
1314
1314
|
if (!(yield this.selfLiquidateIsApproved()))
|
|
1315
1315
|
throw Error("Approval is needed for gas estimation");
|
|
1316
|
-
return yield this._liquidate(llamalend.signerAddress, slippage, true);
|
|
1316
|
+
return yield this._liquidate(this.llamalend.signerAddress, slippage, true);
|
|
1317
1317
|
});
|
|
1318
1318
|
}
|
|
1319
1319
|
selfLiquidate() {
|
|
1320
1320
|
return __awaiter(this, arguments, void 0, function* (slippage = 0.1) {
|
|
1321
1321
|
yield this.selfLiquidateApprove();
|
|
1322
|
-
return yield this._liquidate(llamalend.signerAddress, slippage, false);
|
|
1322
|
+
return yield this._liquidate(this.llamalend.signerAddress, slippage, false);
|
|
1323
1323
|
});
|
|
1324
1324
|
}
|
|
1325
1325
|
// ---------------- CREATE LOAN WITH LEVERAGE ----------------
|
|
@@ -1342,14 +1342,14 @@ export class MintMarketTemplate {
|
|
|
1342
1342
|
const _collateral = parseUnits(collateral, this.collateralDecimals);
|
|
1343
1343
|
const calls = [];
|
|
1344
1344
|
for (let i = 0; i < 5; i++) {
|
|
1345
|
-
calls.push(llamalend.contracts[this.leverageZap].multicallContract.max_borrowable_and_collateral(_collateral, range, i));
|
|
1345
|
+
calls.push(this.llamalend.contracts[this.leverageZap].multicallContract.max_borrowable_and_collateral(_collateral, range, i));
|
|
1346
1346
|
}
|
|
1347
|
-
const _res = yield llamalend.multicallProvider.all(calls);
|
|
1347
|
+
const _res = yield this.llamalend.multicallProvider.all(calls);
|
|
1348
1348
|
const _maxBorrowable = _res.map((r) => r[0] * BigInt(999) / BigInt(1000));
|
|
1349
1349
|
const _maxCollateral = _res.map((r) => r[1] * BigInt(999) / BigInt(1000));
|
|
1350
1350
|
const routeIdx = this._getBestIdx(_maxCollateral);
|
|
1351
|
-
const maxBorrowable = llamalend.formatUnits(_maxBorrowable[routeIdx]);
|
|
1352
|
-
const maxCollateral = llamalend.formatUnits(_maxCollateral[routeIdx], this.collateralDecimals);
|
|
1351
|
+
const maxBorrowable = this.llamalend.formatUnits(_maxBorrowable[routeIdx]);
|
|
1352
|
+
const maxCollateral = this.llamalend.formatUnits(_maxCollateral[routeIdx], this.collateralDecimals);
|
|
1353
1353
|
return {
|
|
1354
1354
|
maxBorrowable,
|
|
1355
1355
|
maxCollateral,
|
|
@@ -1368,14 +1368,14 @@ export class MintMarketTemplate {
|
|
|
1368
1368
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1369
1369
|
this._checkLeverageZap();
|
|
1370
1370
|
const { _collateral, routeIdx } = yield this._leverageCreateLoanCollateral(userCollateral, debt);
|
|
1371
|
-
const collateral = llamalend.formatUnits(_collateral, this.collateralDecimals);
|
|
1371
|
+
const collateral = this.llamalend.formatUnits(_collateral, this.collateralDecimals);
|
|
1372
1372
|
return { collateral, leverage: BN(collateral).div(userCollateral).toFixed(4), routeIdx };
|
|
1373
1373
|
});
|
|
1374
1374
|
}
|
|
1375
1375
|
leverageGetRouteName(routeIdx) {
|
|
1376
1376
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1377
1377
|
this._checkLeverageZap();
|
|
1378
|
-
return yield llamalend.contracts[this.leverageZap].contract.route_names(routeIdx);
|
|
1378
|
+
return yield this.llamalend.contracts[this.leverageZap].contract.route_names(routeIdx);
|
|
1379
1379
|
});
|
|
1380
1380
|
}
|
|
1381
1381
|
leverageGetMaxRange(collateral, debt) {
|
|
@@ -1396,7 +1396,7 @@ export class MintMarketTemplate {
|
|
|
1396
1396
|
const routeIdx = yield this._getRouteIdx(collateral, debt);
|
|
1397
1397
|
const _collateral = parseUnits(collateral, this.collateralDecimals);
|
|
1398
1398
|
const _debt = parseUnits(debt);
|
|
1399
|
-
return yield llamalend.contracts[this.leverageZap].contract.calculate_debt_n1(_collateral, _debt, range, routeIdx, llamalend.constantOptions);
|
|
1399
|
+
return yield this.llamalend.contracts[this.leverageZap].contract.calculate_debt_n1(_collateral, _debt, range, routeIdx, this.llamalend.constantOptions);
|
|
1400
1400
|
});
|
|
1401
1401
|
}
|
|
1402
1402
|
_leverageCalcN1AllRanges(collateral, debt, maxN) {
|
|
@@ -1406,9 +1406,9 @@ export class MintMarketTemplate {
|
|
|
1406
1406
|
const _debt = parseUnits(debt);
|
|
1407
1407
|
const calls = [];
|
|
1408
1408
|
for (let N = this.minBands; N <= maxN; N++) {
|
|
1409
|
-
calls.push(llamalend.contracts[this.leverageZap].multicallContract.calculate_debt_n1(_collateral, _debt, N, routeIdx));
|
|
1409
|
+
calls.push(this.llamalend.contracts[this.leverageZap].multicallContract.calculate_debt_n1(_collateral, _debt, N, routeIdx));
|
|
1410
1410
|
}
|
|
1411
|
-
return yield llamalend.multicallProvider.all(calls);
|
|
1411
|
+
return yield this.llamalend.multicallProvider.all(calls);
|
|
1412
1412
|
});
|
|
1413
1413
|
}
|
|
1414
1414
|
_leverageCreateLoanBands(collateral, debt, range) {
|
|
@@ -1486,8 +1486,8 @@ export class MintMarketTemplate {
|
|
|
1486
1486
|
const address = "0x0000000000000000000000000000000000000000";
|
|
1487
1487
|
const { _collateral } = yield this._leverageCreateLoanCollateral(collateral, debt);
|
|
1488
1488
|
const _debt = parseUnits(debt);
|
|
1489
|
-
const contract = llamalend.contracts[(_a = this.healthCalculator) !== null && _a !== void 0 ? _a : this.controller].contract;
|
|
1490
|
-
let _health = yield contract.health_calculator(address, _collateral, _debt, full, range, llamalend.constantOptions);
|
|
1489
|
+
const contract = this.llamalend.contracts[(_a = this.healthCalculator) !== null && _a !== void 0 ? _a : this.controller].contract;
|
|
1490
|
+
let _health = yield contract.health_calculator(address, _collateral, _debt, full, range, this.llamalend.constantOptions);
|
|
1491
1491
|
_health = _health * BigInt(100);
|
|
1492
1492
|
return formatUnits(_health);
|
|
1493
1493
|
});
|
|
@@ -1498,7 +1498,7 @@ export class MintMarketTemplate {
|
|
|
1498
1498
|
const small_x_BN = BN(100);
|
|
1499
1499
|
const { _collateral, routeIdx } = yield this._leverageCreateLoanCollateral(collateral, debt);
|
|
1500
1500
|
const _y = _collateral - parseUnits(collateral, this.collateralDecimals);
|
|
1501
|
-
const _small_y = yield llamalend.contracts[this.leverageZap].contract.get_collateral(fromBN(small_x_BN), routeIdx);
|
|
1501
|
+
const _small_y = yield this.llamalend.contracts[this.leverageZap].contract.get_collateral(fromBN(small_x_BN), routeIdx);
|
|
1502
1502
|
const y_BN = toBN(_y, this.collateralDecimals);
|
|
1503
1503
|
const small_y_BN = toBN(_small_y, this.collateralDecimals);
|
|
1504
1504
|
const rateBN = y_BN.div(x_BN);
|
|
@@ -1515,19 +1515,19 @@ export class MintMarketTemplate {
|
|
|
1515
1515
|
this._checkRange(range);
|
|
1516
1516
|
const _collateral = parseUnits(collateral, this.collateralDecimals);
|
|
1517
1517
|
const _debt = parseUnits(debt);
|
|
1518
|
-
const leverageContract = llamalend.contracts[this.leverageZap].contract;
|
|
1518
|
+
const leverageContract = this.llamalend.contracts[this.leverageZap].contract;
|
|
1519
1519
|
const routeIdx = yield this._getRouteIdx(collateral, debt);
|
|
1520
|
-
const _expected = yield leverageContract.get_collateral_underlying(_debt, routeIdx, llamalend.constantOptions);
|
|
1520
|
+
const _expected = yield leverageContract.get_collateral_underlying(_debt, routeIdx, this.llamalend.constantOptions);
|
|
1521
1521
|
const minRecvBN = toBN(_expected, this.collateralDecimals).times(100 - slippage).div(100);
|
|
1522
1522
|
const _minRecv = fromBN(minRecvBN, this.collateralDecimals);
|
|
1523
|
-
const contract = llamalend.contracts[this.controller].contract;
|
|
1524
|
-
const value = isEth(this.collateral) ? _collateral : llamalend.parseUnits("0");
|
|
1525
|
-
const gas = yield contract.create_loan_extended.estimateGas(_collateral, _debt, range, this.leverageZap, [routeIdx, _minRecv], Object.assign(Object.assign({}, llamalend.constantOptions), { value }));
|
|
1523
|
+
const contract = this.llamalend.contracts[this.controller].contract;
|
|
1524
|
+
const value = isEth(this.collateral) ? _collateral : this.llamalend.parseUnits("0");
|
|
1525
|
+
const gas = yield contract.create_loan_extended.estimateGas(_collateral, _debt, range, this.leverageZap, [routeIdx, _minRecv], Object.assign(Object.assign({}, this.llamalend.constantOptions), { value }));
|
|
1526
1526
|
if (estimateGas)
|
|
1527
1527
|
return smartNumber(gas);
|
|
1528
|
-
yield llamalend.updateFeeData();
|
|
1528
|
+
yield this.llamalend.updateFeeData();
|
|
1529
1529
|
const gasLimit = _mulBy1_3(DIGas(gas));
|
|
1530
|
-
return (yield contract.create_loan_extended(_collateral, _debt, range, this.leverageZap, [routeIdx, _minRecv], Object.assign(Object.assign({}, llamalend.options), { gasLimit, value }))).hash;
|
|
1530
|
+
return (yield contract.create_loan_extended(_collateral, _debt, range, this.leverageZap, [routeIdx, _minRecv], Object.assign(Object.assign({}, this.llamalend.options), { gasLimit, value }))).hash;
|
|
1531
1531
|
});
|
|
1532
1532
|
}
|
|
1533
1533
|
leverageCreateLoanEstimateGas(collateral_1, debt_1, range_1) {
|
|
@@ -1553,12 +1553,12 @@ export class MintMarketTemplate {
|
|
|
1553
1553
|
deleverageGetRouteName(routeIdx) {
|
|
1554
1554
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1555
1555
|
this._checkDeleverageZap();
|
|
1556
|
-
return yield llamalend.contracts[this.deleverageZap].contract.route_names(routeIdx);
|
|
1556
|
+
return yield this.llamalend.contracts[this.deleverageZap].contract.route_names(routeIdx);
|
|
1557
1557
|
});
|
|
1558
1558
|
}
|
|
1559
1559
|
deleverageIsFullRepayment(deleverageCollateral_1) {
|
|
1560
1560
|
return __awaiter(this, arguments, void 0, function* (deleverageCollateral, address = "") {
|
|
1561
|
-
address = _getAddress(address);
|
|
1561
|
+
address = _getAddress.call(this.llamalend, address);
|
|
1562
1562
|
const { stablecoin, debt } = yield this.userState(address);
|
|
1563
1563
|
const { stablecoins: deleverageStablecoins } = yield this.deleverageRepayStablecoins(deleverageCollateral);
|
|
1564
1564
|
return BN(stablecoin).plus(deleverageStablecoins).gt(debt);
|
|
@@ -1573,7 +1573,7 @@ export class MintMarketTemplate {
|
|
|
1573
1573
|
// There is no deleverage zap
|
|
1574
1574
|
if (this.deleverageZap === "0x0000000000000000000000000000000000000000")
|
|
1575
1575
|
return false;
|
|
1576
|
-
address = _getAddress(address);
|
|
1576
|
+
address = _getAddress.call(this.llamalend, address);
|
|
1577
1577
|
const { collateral, stablecoin, debt } = yield this.userState(address);
|
|
1578
1578
|
// Loan does not exist
|
|
1579
1579
|
if (BN(debt).eq(0))
|
|
@@ -1605,7 +1605,7 @@ export class MintMarketTemplate {
|
|
|
1605
1605
|
return __awaiter(this, arguments, void 0, function* (collateral, full = true, address = "") {
|
|
1606
1606
|
var _a;
|
|
1607
1607
|
this._checkDeleverageZap();
|
|
1608
|
-
address = _getAddress(address);
|
|
1608
|
+
address = _getAddress.call(this.llamalend, address);
|
|
1609
1609
|
if (!(yield this.deleverageIsAvailable(collateral, address)))
|
|
1610
1610
|
return "0.0";
|
|
1611
1611
|
const { _stablecoin, _debt } = yield this._userState(address);
|
|
@@ -1615,8 +1615,8 @@ export class MintMarketTemplate {
|
|
|
1615
1615
|
const N = yield this.userRange(address);
|
|
1616
1616
|
if ((_debt + _d_debt) < 0)
|
|
1617
1617
|
return "0.0";
|
|
1618
|
-
const contract = llamalend.contracts[(_a = this.healthCalculator) !== null && _a !== void 0 ? _a : this.controller].contract;
|
|
1619
|
-
let _health = yield contract.health_calculator(address, _d_collateral, _d_debt, full, N, llamalend.constantOptions);
|
|
1618
|
+
const contract = this.llamalend.contracts[(_a = this.healthCalculator) !== null && _a !== void 0 ? _a : this.controller].contract;
|
|
1619
|
+
let _health = yield contract.health_calculator(address, _d_collateral, _d_debt, full, N, this.llamalend.constantOptions);
|
|
1620
1620
|
_health = _health * BigInt(100);
|
|
1621
1621
|
return formatUnits(_health);
|
|
1622
1622
|
});
|
|
@@ -1627,7 +1627,7 @@ export class MintMarketTemplate {
|
|
|
1627
1627
|
const small_x_BN = BN(0.001);
|
|
1628
1628
|
const { stablecoins, routeIdx } = yield this.deleverageRepayStablecoins(collateral);
|
|
1629
1629
|
const _y = parseUnits(stablecoins);
|
|
1630
|
-
const _small_y = yield llamalend.contracts[this.deleverageZap].contract.get_stablecoins(fromBN(small_x_BN, this.collateralDecimals), routeIdx);
|
|
1630
|
+
const _small_y = yield this.llamalend.contracts[this.deleverageZap].contract.get_stablecoins(fromBN(small_x_BN, this.collateralDecimals), routeIdx);
|
|
1631
1631
|
const y_BN = toBN(_y);
|
|
1632
1632
|
const small_y_BN = toBN(_small_y);
|
|
1633
1633
|
const rateBN = y_BN.div(x_BN);
|
|
@@ -1639,21 +1639,21 @@ export class MintMarketTemplate {
|
|
|
1639
1639
|
}
|
|
1640
1640
|
_deleverageRepay(collateral, slippage, estimateGas) {
|
|
1641
1641
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1642
|
-
const { debt: currentDebt } = yield this.userState(llamalend.signerAddress);
|
|
1642
|
+
const { debt: currentDebt } = yield this.userState(this.llamalend.signerAddress);
|
|
1643
1643
|
if (Number(currentDebt) === 0)
|
|
1644
|
-
throw Error(`Loan for ${llamalend.signerAddress} does not exist`);
|
|
1644
|
+
throw Error(`Loan for ${this.llamalend.signerAddress} does not exist`);
|
|
1645
1645
|
const { stablecoins, routeIdx } = yield this.deleverageRepayStablecoins(collateral);
|
|
1646
1646
|
const _collateral = parseUnits(collateral, this.collateralDecimals);
|
|
1647
1647
|
const _debt = parseUnits(stablecoins);
|
|
1648
1648
|
const minRecvBN = toBN(_debt).times(100 - slippage).div(100);
|
|
1649
1649
|
const _minRecv = fromBN(minRecvBN);
|
|
1650
|
-
const contract = llamalend.contracts[this.controller].contract;
|
|
1651
|
-
const gas = yield contract.repay_extended.estimateGas(this.deleverageZap, [routeIdx, _collateral, _minRecv], llamalend.constantOptions);
|
|
1650
|
+
const contract = this.llamalend.contracts[this.controller].contract;
|
|
1651
|
+
const gas = yield contract.repay_extended.estimateGas(this.deleverageZap, [routeIdx, _collateral, _minRecv], this.llamalend.constantOptions);
|
|
1652
1652
|
if (estimateGas)
|
|
1653
1653
|
return smartNumber(gas);
|
|
1654
|
-
yield llamalend.updateFeeData();
|
|
1654
|
+
yield this.llamalend.updateFeeData();
|
|
1655
1655
|
const gasLimit = _mulBy1_3(DIGas(gas));
|
|
1656
|
-
return (yield contract.repay_extended(this.deleverageZap, [routeIdx, _collateral, _minRecv], Object.assign(Object.assign({}, llamalend.options), { gasLimit }))).hash;
|
|
1656
|
+
return (yield contract.repay_extended(this.deleverageZap, [routeIdx, _collateral, _minRecv], Object.assign(Object.assign({}, this.llamalend.options), { gasLimit }))).hash;
|
|
1657
1657
|
});
|
|
1658
1658
|
}
|
|
1659
1659
|
deleverageRepayEstimateGas(collateral_1) {
|