@curvefi/llamalend-api 1.0.3 → 1.0.6
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/lint.yml +2 -2
- package/.github/workflows/publish.yml +11 -7
- package/eslint.config.mjs +87 -0
- package/lib/cache/index.js +18 -19
- package/lib/constants/L2Networks.js +1 -1
- package/lib/constants/abis/crvUSD/controller_v2.json +979 -0
- package/lib/constants/aliases.js +34 -34
- package/lib/constants/coins.js +16 -16
- package/lib/constants/llammas.js +1 -1
- package/lib/constants/utils.js +8 -10
- package/lib/external-api.d.ts +8 -6
- package/lib/external-api.js +136 -397
- package/lib/index.d.ts +9 -10
- package/lib/index.js +46 -81
- package/lib/interfaces.d.ts +2 -20
- package/lib/lendMarkets/LendMarketTemplate.js +2438 -4419
- package/lib/lendMarkets/lendMarketConstructor.js +2 -2
- package/lib/llamalend.js +511 -696
- package/lib/mintMarkets/MintMarketTemplate.js +1469 -2799
- package/lib/mintMarkets/mintMarketConstructor.js +1 -1
- package/lib/st-crvUSD.js +172 -488
- package/lib/utils.d.ts +2 -2
- package/lib/utils.js +318 -548
- package/package.json +21 -16
- package/src/cache/index.ts +1 -0
- package/src/constants/abis/crvUSD/controller_v2.json +979 -0
- package/src/constants/aliases.ts +7 -7
- package/src/external-api.ts +25 -148
- package/src/interfaces.ts +2 -21
- package/src/lendMarkets/LendMarketTemplate.ts +31 -6
- package/src/llamalend.ts +36 -6
- package/src/mintMarkets/MintMarketTemplate.ts +2 -2
- package/src/utils.ts +8 -8
- package/tsconfig.json +1 -1
- package/.eslintrc.json +0 -40
|
@@ -1,14 +1,3 @@
|
|
|
1
|
-
var __assign = (this && this.__assign) || function () {
|
|
2
|
-
__assign = Object.assign || function(t) {
|
|
3
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
-
s = arguments[i];
|
|
5
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
-
t[p] = s[p];
|
|
7
|
-
}
|
|
8
|
-
return t;
|
|
9
|
-
};
|
|
10
|
-
return __assign.apply(this, arguments);
|
|
11
|
-
};
|
|
12
1
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
13
2
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
14
3
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -18,387 +7,235 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
18
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
19
8
|
});
|
|
20
9
|
};
|
|
21
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
22
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
23
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
24
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
25
|
-
function step(op) {
|
|
26
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
27
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
28
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
29
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
30
|
-
switch (op[0]) {
|
|
31
|
-
case 0: case 1: t = op; break;
|
|
32
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
33
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
34
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
35
|
-
default:
|
|
36
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
37
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
38
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
39
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
40
|
-
if (t[2]) _.ops.pop();
|
|
41
|
-
_.trys.pop(); continue;
|
|
42
|
-
}
|
|
43
|
-
op = body.call(thisArg, _);
|
|
44
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
45
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
10
|
import memoize from "memoizee";
|
|
49
11
|
import BigNumber from "bignumber.js";
|
|
50
12
|
import { llamalend } from "../llamalend.js";
|
|
51
13
|
import { _getAddress, parseUnits, BN, toBN, fromBN, getBalances, ensureAllowance, hasAllowance, ensureAllowanceEstimateGas, isEth, _cutZeros, formatUnits, smartNumber, MAX_ALLOWANCE, MAX_ACTIVE_BAND, _mulBy1_3, DIGas, } from "../utils";
|
|
52
14
|
import { _getUserCollateralCrvUsd } from "../external-api.js";
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
var _this = this;
|
|
15
|
+
export class MintMarketTemplate {
|
|
16
|
+
constructor(id) {
|
|
56
17
|
// ---------------- STATS ----------------
|
|
57
|
-
this.statsParameters = memoize(
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
.map(function (x) { return formatUnits(x * BigInt(100)); }), fee = _c[0], admin_fee = _c[1], liquidation_discount = _c[2], loan_discount = _c[3];
|
|
78
|
-
rate = String(((Math.pow(2.718281828459, (toBN(_rate).times(365).times(86400)).toNumber())) - 1) * 100);
|
|
79
|
-
future_rate = String(((Math.pow(2.718281828459, (toBN(_mp_rate).times(365).times(86400)).toNumber())) - 1) * 100);
|
|
80
|
-
return [2 /*return*/, { fee: fee, admin_fee: admin_fee, rate: rate, future_rate: future_rate, liquidation_discount: liquidation_discount, loan_discount: loan_discount }];
|
|
81
|
-
}
|
|
82
|
-
});
|
|
83
|
-
}); }, {
|
|
18
|
+
this.statsParameters = memoize(() => __awaiter(this, void 0, void 0, function* () {
|
|
19
|
+
const llammaContract = llamalend.contracts[this.address].multicallContract;
|
|
20
|
+
const controllerContract = llamalend.contracts[this.controller].multicallContract;
|
|
21
|
+
const monetaryPolicyContract = llamalend.contracts[this.monetaryPolicy].multicallContract;
|
|
22
|
+
const calls = [
|
|
23
|
+
llammaContract.fee(),
|
|
24
|
+
llammaContract.admin_fee(),
|
|
25
|
+
llammaContract.rate(),
|
|
26
|
+
"rate(address)" in llamalend.contracts[this.monetaryPolicy].contract ? monetaryPolicyContract.rate(this.controller) : monetaryPolicyContract.rate(),
|
|
27
|
+
controllerContract.liquidation_discount(),
|
|
28
|
+
controllerContract.loan_discount(),
|
|
29
|
+
];
|
|
30
|
+
const [_fee, _admin_fee, _rate, _mp_rate, _liquidation_discount, _loan_discount] = yield llamalend.multicallProvider.all(calls);
|
|
31
|
+
const [fee, admin_fee, liquidation_discount, loan_discount] = [_fee, _admin_fee, _liquidation_discount, _loan_discount]
|
|
32
|
+
.map((x) => formatUnits(x * BigInt(100)));
|
|
33
|
+
// (1+rate)**(365*86400)-1 ~= (e**(rate*365*86400))-1
|
|
34
|
+
const rate = String(((Math.pow(2.718281828459, (toBN(_rate).times(365).times(86400)).toNumber())) - 1) * 100);
|
|
35
|
+
const future_rate = String(((Math.pow(2.718281828459, (toBN(_mp_rate).times(365).times(86400)).toNumber())) - 1) * 100);
|
|
36
|
+
return { fee, admin_fee, rate, future_rate, liquidation_discount, loan_discount };
|
|
37
|
+
}), {
|
|
84
38
|
promise: true,
|
|
85
39
|
maxAge: 5 * 60 * 1000, // 5m
|
|
86
40
|
});
|
|
87
|
-
this.statsMaxMinBands = memoize(
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
llammaContract.min_band(),
|
|
96
|
-
];
|
|
97
|
-
return [4 /*yield*/, llamalend.multicallProvider.all(calls1)];
|
|
98
|
-
case 1: return [2 /*return*/, (_a.sent()).map(function (_b) { return _b.toNumber(); })];
|
|
99
|
-
}
|
|
100
|
-
});
|
|
101
|
-
}); }, {
|
|
41
|
+
this.statsMaxMinBands = memoize(() => __awaiter(this, void 0, void 0, function* () {
|
|
42
|
+
const llammaContract = llamalend.contracts[this.address].multicallContract;
|
|
43
|
+
const calls1 = [
|
|
44
|
+
llammaContract.max_band(),
|
|
45
|
+
llammaContract.min_band(),
|
|
46
|
+
];
|
|
47
|
+
return (yield llamalend.multicallProvider.all(calls1)).map((_b) => _b.toNumber());
|
|
48
|
+
}), {
|
|
102
49
|
promise: true,
|
|
103
50
|
maxAge: 60 * 1000, // 1m
|
|
104
51
|
});
|
|
105
|
-
this.statsActiveBand = memoize(
|
|
106
|
-
return
|
|
107
|
-
|
|
108
|
-
case 0: return [4 /*yield*/, llamalend.contracts[this.address].contract.active_band_with_skip()];
|
|
109
|
-
case 1: return [2 /*return*/, (_a.sent()).toNumber()];
|
|
110
|
-
}
|
|
111
|
-
});
|
|
112
|
-
}); }, {
|
|
52
|
+
this.statsActiveBand = memoize(() => __awaiter(this, void 0, void 0, function* () {
|
|
53
|
+
return (yield llamalend.contracts[this.address].contract.active_band_with_skip()).toNumber();
|
|
54
|
+
}), {
|
|
113
55
|
promise: true,
|
|
114
56
|
maxAge: 60 * 1000, // 1m
|
|
115
57
|
});
|
|
116
|
-
this.statsTotalSupply = memoize(
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
calls = [controllerContract.minted(), controllerContract.redeemed()];
|
|
123
|
-
return [4 /*yield*/, llamalend.multicallProvider.all(calls)];
|
|
124
|
-
case 1:
|
|
125
|
-
_a = _c.sent(), _minted = _a[0], _redeemed = _a[1];
|
|
126
|
-
return [2 /*return*/, toBN(_minted).minus(toBN(_redeemed)).toString()];
|
|
127
|
-
}
|
|
128
|
-
});
|
|
129
|
-
}); }, {
|
|
58
|
+
this.statsTotalSupply = memoize(() => __awaiter(this, void 0, void 0, function* () {
|
|
59
|
+
const controllerContract = llamalend.contracts[this.controller].multicallContract;
|
|
60
|
+
const calls = [controllerContract.minted(), controllerContract.redeemed()];
|
|
61
|
+
const [_minted, _redeemed] = yield llamalend.multicallProvider.all(calls);
|
|
62
|
+
return toBN(_minted).minus(toBN(_redeemed)).toString();
|
|
63
|
+
}), {
|
|
130
64
|
promise: true,
|
|
131
65
|
maxAge: 60 * 1000, // 1m
|
|
132
66
|
});
|
|
133
|
-
this.statsTotalDebt = memoize(
|
|
134
|
-
|
|
135
|
-
return
|
|
136
|
-
|
|
137
|
-
case 0: return [4 /*yield*/, llamalend.contracts[this.controller].contract.total_debt(llamalend.constantOptions)];
|
|
138
|
-
case 1:
|
|
139
|
-
debt = _a.sent();
|
|
140
|
-
return [2 /*return*/, formatUnits(debt)];
|
|
141
|
-
}
|
|
142
|
-
});
|
|
143
|
-
}); }, {
|
|
67
|
+
this.statsTotalDebt = memoize(() => __awaiter(this, void 0, void 0, function* () {
|
|
68
|
+
const debt = yield llamalend.contracts[this.controller].contract.total_debt(llamalend.constantOptions);
|
|
69
|
+
return formatUnits(debt);
|
|
70
|
+
}), {
|
|
144
71
|
promise: true,
|
|
145
72
|
maxAge: 60 * 1000, // 1m
|
|
146
73
|
});
|
|
147
|
-
this.statsTotalStablecoin = memoize(
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
ammContract.admin_fees_x(),
|
|
157
|
-
])];
|
|
158
|
-
case 1:
|
|
159
|
-
_a = _c.sent(), _balance = _a[0], _fee = _a[1];
|
|
160
|
-
return [2 /*return*/, toBN(_balance).minus(toBN(_fee)).toString()];
|
|
161
|
-
}
|
|
162
|
-
});
|
|
163
|
-
}); }, {
|
|
74
|
+
this.statsTotalStablecoin = memoize(() => __awaiter(this, void 0, void 0, function* () {
|
|
75
|
+
const stablecoinContract = llamalend.contracts[llamalend.address].multicallContract;
|
|
76
|
+
const ammContract = llamalend.contracts[this.address].multicallContract;
|
|
77
|
+
const [_balance, _fee] = yield llamalend.multicallProvider.all([
|
|
78
|
+
stablecoinContract.balanceOf(this.address),
|
|
79
|
+
ammContract.admin_fees_x(),
|
|
80
|
+
]);
|
|
81
|
+
return toBN(_balance).minus(toBN(_fee)).toString();
|
|
82
|
+
}), {
|
|
164
83
|
promise: true,
|
|
165
84
|
maxAge: 60 * 1000, // 1m
|
|
166
85
|
});
|
|
167
|
-
this.statsTotalCollateral = memoize(
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
ammContract.admin_fees_y(),
|
|
177
|
-
])];
|
|
178
|
-
case 1:
|
|
179
|
-
_a = _c.sent(), _balance = _a[0], _fee = _a[1];
|
|
180
|
-
return [2 /*return*/, toBN(_balance, this.collateralDecimals).minus(toBN(_fee, this.collateralDecimals)).toString()];
|
|
181
|
-
}
|
|
182
|
-
});
|
|
183
|
-
}); }, {
|
|
86
|
+
this.statsTotalCollateral = memoize(() => __awaiter(this, void 0, void 0, function* () {
|
|
87
|
+
const collateralContract = llamalend.contracts[isEth(this.collateral) ? llamalend.constants.WETH : this.collateral].multicallContract;
|
|
88
|
+
const ammContract = llamalend.contracts[this.address].multicallContract;
|
|
89
|
+
const [_balance, _fee] = yield llamalend.multicallProvider.all([
|
|
90
|
+
collateralContract.balanceOf(this.address),
|
|
91
|
+
ammContract.admin_fees_y(),
|
|
92
|
+
]);
|
|
93
|
+
return toBN(_balance, this.collateralDecimals).minus(toBN(_fee, this.collateralDecimals)).toString();
|
|
94
|
+
}), {
|
|
184
95
|
promise: true,
|
|
185
96
|
maxAge: 60 * 1000, // 1m
|
|
186
97
|
});
|
|
187
|
-
this.statsCapAndAvailable = memoize(
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
crvusdContract.balanceOf(this.controller),
|
|
197
|
-
])];
|
|
198
|
-
case 1:
|
|
199
|
-
_a = _c.sent(), _cap = _a[0], _available = _a[1];
|
|
200
|
-
return [2 /*return*/, { "cap": llamalend.formatUnits(_cap), "available": llamalend.formatUnits(_available) }];
|
|
201
|
-
}
|
|
202
|
-
});
|
|
203
|
-
}); }, {
|
|
98
|
+
this.statsCapAndAvailable = memoize(() => __awaiter(this, void 0, void 0, function* () {
|
|
99
|
+
const factoryContract = llamalend.contracts[llamalend.constants.FACTORY].multicallContract;
|
|
100
|
+
const crvusdContract = llamalend.contracts[llamalend.address].multicallContract;
|
|
101
|
+
const [_cap, _available] = yield llamalend.multicallProvider.all([
|
|
102
|
+
factoryContract.debt_ceiling(this.controller),
|
|
103
|
+
crvusdContract.balanceOf(this.controller),
|
|
104
|
+
]);
|
|
105
|
+
return { "cap": llamalend.formatUnits(_cap), "available": llamalend.formatUnits(_available) };
|
|
106
|
+
}), {
|
|
204
107
|
promise: true,
|
|
205
108
|
maxAge: 60 * 1000, // 1m
|
|
206
109
|
});
|
|
207
|
-
this.basePrice = memoize(
|
|
208
|
-
|
|
209
|
-
return
|
|
210
|
-
|
|
211
|
-
case 0: return [4 /*yield*/, llamalend.contracts[this.address].contract.get_base_price(llamalend.constantOptions)];
|
|
212
|
-
case 1:
|
|
213
|
-
_price = _a.sent();
|
|
214
|
-
return [2 /*return*/, formatUnits(_price)];
|
|
215
|
-
}
|
|
216
|
-
});
|
|
217
|
-
}); }, {
|
|
110
|
+
this.basePrice = memoize(() => __awaiter(this, void 0, void 0, function* () {
|
|
111
|
+
const _price = yield llamalend.contracts[this.address].contract.get_base_price(llamalend.constantOptions);
|
|
112
|
+
return formatUnits(_price);
|
|
113
|
+
}), {
|
|
218
114
|
promise: true,
|
|
219
115
|
maxAge: 60 * 1000, // 1m
|
|
220
116
|
});
|
|
221
|
-
this.createLoanMaxRecvAllRanges = memoize(
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
res = {};
|
|
235
|
-
for (N = this.minBands; N <= this.maxBands; N++) {
|
|
236
|
-
res[N] = formatUnits(_amounts[N - this.minBands]);
|
|
237
|
-
}
|
|
238
|
-
return [2 /*return*/, res];
|
|
239
|
-
}
|
|
240
|
-
});
|
|
241
|
-
}); }, {
|
|
117
|
+
this.createLoanMaxRecvAllRanges = memoize((collateral) => __awaiter(this, void 0, void 0, function* () {
|
|
118
|
+
const _collateral = parseUnits(collateral, this.collateralDecimals);
|
|
119
|
+
const calls = [];
|
|
120
|
+
for (let N = this.minBands; N <= this.maxBands; N++) {
|
|
121
|
+
calls.push(llamalend.contracts[this.controller].multicallContract.max_borrowable(_collateral, N));
|
|
122
|
+
}
|
|
123
|
+
const _amounts = yield llamalend.multicallProvider.all(calls);
|
|
124
|
+
const res = {};
|
|
125
|
+
for (let N = this.minBands; N <= this.maxBands; N++) {
|
|
126
|
+
res[N] = formatUnits(_amounts[N - this.minBands]);
|
|
127
|
+
}
|
|
128
|
+
return res;
|
|
129
|
+
}), {
|
|
242
130
|
promise: true,
|
|
243
131
|
maxAge: 5 * 60 * 1000, // 5m
|
|
244
132
|
});
|
|
245
|
-
this.leverageCreateLoanMaxRecvAllRanges = memoize(
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
calls = [];
|
|
253
|
-
for (N = this.minBands; N <= this.maxBands; N++) {
|
|
254
|
-
for (i = 0; i < 5; i++) {
|
|
255
|
-
calls.push(llamalend.contracts[this.leverageZap].multicallContract.max_borrowable_and_collateral(_collateral, N, i));
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
return [4 /*yield*/, llamalend.multicallProvider.all(calls)];
|
|
259
|
-
case 1:
|
|
260
|
-
_rawRes = _a.sent();
|
|
261
|
-
res = {};
|
|
262
|
-
for (N = this.minBands; N <= this.maxBands; N++) {
|
|
263
|
-
_res = _rawRes.splice(0, 5);
|
|
264
|
-
_maxBorrowable = _res.map(function (r) { return r[0] * BigInt(999) / BigInt(1000); });
|
|
265
|
-
_maxCollateral = _res.map(function (r) { return r[1] * BigInt(999) / BigInt(1000); });
|
|
266
|
-
routeIdx = this._getBestIdx(_maxCollateral);
|
|
267
|
-
maxBorrowable = llamalend.formatUnits(_maxBorrowable[routeIdx]);
|
|
268
|
-
maxCollateral = llamalend.formatUnits(_maxCollateral[routeIdx], this.collateralDecimals);
|
|
269
|
-
res[N] = {
|
|
270
|
-
maxBorrowable: maxBorrowable,
|
|
271
|
-
maxCollateral: maxCollateral,
|
|
272
|
-
leverage: BN(maxCollateral).div(collateral).toFixed(4),
|
|
273
|
-
routeIdx: routeIdx,
|
|
274
|
-
};
|
|
275
|
-
}
|
|
276
|
-
return [2 /*return*/, res];
|
|
133
|
+
this.leverageCreateLoanMaxRecvAllRanges = memoize((collateral) => __awaiter(this, void 0, void 0, function* () {
|
|
134
|
+
this._checkLeverageZap();
|
|
135
|
+
const _collateral = parseUnits(collateral, this.collateralDecimals);
|
|
136
|
+
const calls = [];
|
|
137
|
+
for (let N = this.minBands; N <= this.maxBands; N++) {
|
|
138
|
+
for (let i = 0; i < 5; i++) {
|
|
139
|
+
calls.push(llamalend.contracts[this.leverageZap].multicallContract.max_borrowable_and_collateral(_collateral, N, i));
|
|
277
140
|
}
|
|
278
|
-
}
|
|
279
|
-
|
|
141
|
+
}
|
|
142
|
+
const _rawRes = yield llamalend.multicallProvider.all(calls);
|
|
143
|
+
const res = {};
|
|
144
|
+
for (let N = this.minBands; N <= this.maxBands; N++) {
|
|
145
|
+
const _res = _rawRes.splice(0, 5);
|
|
146
|
+
const _maxBorrowable = _res.map((r) => r[0] * BigInt(999) / BigInt(1000));
|
|
147
|
+
const _maxCollateral = _res.map((r) => r[1] * BigInt(999) / BigInt(1000));
|
|
148
|
+
const routeIdx = this._getBestIdx(_maxCollateral);
|
|
149
|
+
const maxBorrowable = llamalend.formatUnits(_maxBorrowable[routeIdx]);
|
|
150
|
+
const maxCollateral = llamalend.formatUnits(_maxCollateral[routeIdx], this.collateralDecimals);
|
|
151
|
+
res[N] = {
|
|
152
|
+
maxBorrowable,
|
|
153
|
+
maxCollateral,
|
|
154
|
+
leverage: BN(maxCollateral).div(collateral).toFixed(4),
|
|
155
|
+
routeIdx,
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
return res;
|
|
159
|
+
}), {
|
|
280
160
|
promise: true,
|
|
281
161
|
maxAge: 5 * 60 * 1000, // 5m
|
|
282
162
|
});
|
|
283
|
-
this._leverageCreateLoanMaxRecvAllRanges2 = memoize(
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
maxCollateral: maxCollateral,
|
|
303
|
-
leverage: BN(maxCollateral).div(collateral).toFixed(4),
|
|
304
|
-
};
|
|
305
|
-
}
|
|
306
|
-
return [2 /*return*/, res];
|
|
307
|
-
}
|
|
308
|
-
});
|
|
309
|
-
}); }, {
|
|
163
|
+
this._leverageCreateLoanMaxRecvAllRanges2 = memoize((collateral, routeIdx) => __awaiter(this, void 0, void 0, function* () {
|
|
164
|
+
const _collateral = parseUnits(collateral, this.collateralDecimals);
|
|
165
|
+
const calls = [];
|
|
166
|
+
for (let N = this.minBands; N <= this.maxBands; N++) {
|
|
167
|
+
calls.push(llamalend.contracts[this.leverageZap].multicallContract.max_borrowable_and_collateral(_collateral, N, routeIdx));
|
|
168
|
+
}
|
|
169
|
+
const _res = yield llamalend.multicallProvider.all(calls);
|
|
170
|
+
const res = {};
|
|
171
|
+
for (let N = this.minBands; N <= this.maxBands; N++) {
|
|
172
|
+
const maxBorrowable = llamalend.formatUnits(_res[N - this.minBands][0] * BigInt(999) / BigInt(1000));
|
|
173
|
+
const maxCollateral = llamalend.formatUnits(_res[N - this.minBands][1] * BigInt(999) / BigInt(1000), this.collateralDecimals);
|
|
174
|
+
res[N] = {
|
|
175
|
+
maxBorrowable,
|
|
176
|
+
maxCollateral,
|
|
177
|
+
leverage: BN(maxCollateral).div(collateral).toFixed(4),
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
return res;
|
|
181
|
+
}), {
|
|
310
182
|
promise: true,
|
|
311
183
|
maxAge: 5 * 60 * 1000, // 5m
|
|
312
184
|
});
|
|
313
|
-
this._leverageCreateLoanCollateral = memoize(
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
return [4 /*yield*/, llamalend.multicallProvider.all(calls)];
|
|
325
|
-
case 1:
|
|
326
|
-
_leverageCollateral = _a.sent();
|
|
327
|
-
routeIdx = this._getBestIdx(_leverageCollateral);
|
|
328
|
-
return [2 /*return*/, { _collateral: _userCollateral + _leverageCollateral[routeIdx], routeIdx: routeIdx }];
|
|
329
|
-
}
|
|
330
|
-
});
|
|
331
|
-
}); }, {
|
|
185
|
+
this._leverageCreateLoanCollateral = memoize((userCollateral, debt) => __awaiter(this, void 0, void 0, function* () {
|
|
186
|
+
const _userCollateral = parseUnits(userCollateral, this.collateralDecimals);
|
|
187
|
+
const _debt = parseUnits(debt);
|
|
188
|
+
const calls = [];
|
|
189
|
+
for (let i = 0; i < 5; i++) {
|
|
190
|
+
calls.push(llamalend.contracts[this.leverageZap].multicallContract.get_collateral(_debt, i));
|
|
191
|
+
}
|
|
192
|
+
const _leverageCollateral = yield llamalend.multicallProvider.all(calls);
|
|
193
|
+
const routeIdx = this._getBestIdx(_leverageCollateral);
|
|
194
|
+
return { _collateral: _userCollateral + _leverageCollateral[routeIdx], routeIdx };
|
|
195
|
+
}), {
|
|
332
196
|
promise: true,
|
|
333
197
|
maxAge: 5 * 60 * 1000, // 5m
|
|
334
198
|
});
|
|
335
|
-
this.deleverageRepayStablecoins = memoize(
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
case 1:
|
|
348
|
-
_stablecoins_arr = _a.sent();
|
|
349
|
-
routeIdx = this._getBestIdx(_stablecoins_arr);
|
|
350
|
-
stablecoins = llamalend.formatUnits(_stablecoins_arr[routeIdx]);
|
|
351
|
-
return [2 /*return*/, { stablecoins: stablecoins, routeIdx: routeIdx }];
|
|
352
|
-
}
|
|
353
|
-
});
|
|
354
|
-
}); }, {
|
|
199
|
+
this.deleverageRepayStablecoins = memoize((collateral) => __awaiter(this, void 0, void 0, function* () {
|
|
200
|
+
this._checkDeleverageZap();
|
|
201
|
+
const _collateral = parseUnits(collateral, this.collateralDecimals);
|
|
202
|
+
const calls = [];
|
|
203
|
+
for (let i = 0; i < 5; i++) {
|
|
204
|
+
calls.push(llamalend.contracts[this.deleverageZap].multicallContract.get_stablecoins(_collateral, i));
|
|
205
|
+
}
|
|
206
|
+
const _stablecoins_arr = yield llamalend.multicallProvider.all(calls);
|
|
207
|
+
const routeIdx = this._getBestIdx(_stablecoins_arr);
|
|
208
|
+
const stablecoins = llamalend.formatUnits(_stablecoins_arr[routeIdx]);
|
|
209
|
+
return { stablecoins, routeIdx };
|
|
210
|
+
}), {
|
|
355
211
|
promise: true,
|
|
356
212
|
maxAge: 5 * 60 * 1000, // 5m
|
|
357
213
|
});
|
|
358
|
-
this._deleverageRepayBands = memoize(
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
_collateral = parseUnits(collateral, this.collateralDecimals);
|
|
380
|
-
_n1 = parseUnits(0, 0);
|
|
381
|
-
_n2 = parseUnits(0, 0);
|
|
382
|
-
_a.label = 5;
|
|
383
|
-
case 5:
|
|
384
|
-
_a.trys.push([5, 7, , 8]);
|
|
385
|
-
return [4 /*yield*/, llamalend.contracts[this.deleverageZap].contract.calculate_debt_n1(_collateral, routeIdx, address)];
|
|
386
|
-
case 6:
|
|
387
|
-
_n1 = _a.sent();
|
|
388
|
-
_n2 = _n1 + BigInt(N - 1);
|
|
389
|
-
return [3 /*break*/, 8];
|
|
390
|
-
case 7:
|
|
391
|
-
e_1 = _a.sent();
|
|
392
|
-
console.log("Full repayment");
|
|
393
|
-
return [3 /*break*/, 8];
|
|
394
|
-
case 8: return [2 /*return*/, [_n2, _n1]];
|
|
395
|
-
}
|
|
396
|
-
});
|
|
397
|
-
}); }, {
|
|
214
|
+
this._deleverageRepayBands = memoize((collateral, address) => __awaiter(this, void 0, void 0, function* () {
|
|
215
|
+
address = _getAddress(address);
|
|
216
|
+
if (!(yield this.deleverageIsAvailable(collateral, address)))
|
|
217
|
+
return [parseUnits(0, 0), parseUnits(0, 0)];
|
|
218
|
+
const { routeIdx } = yield this.deleverageRepayStablecoins(collateral);
|
|
219
|
+
const { _debt: _currentDebt } = yield this._userState(address);
|
|
220
|
+
if (_currentDebt === BigInt(0))
|
|
221
|
+
throw Error(`Loan for ${address} does not exist`);
|
|
222
|
+
const N = yield this.userRange(address);
|
|
223
|
+
const _collateral = parseUnits(collateral, this.collateralDecimals);
|
|
224
|
+
let _n1 = parseUnits(0, 0);
|
|
225
|
+
let _n2 = parseUnits(0, 0);
|
|
226
|
+
try {
|
|
227
|
+
_n1 = yield llamalend.contracts[this.deleverageZap].contract.calculate_debt_n1(_collateral, routeIdx, address);
|
|
228
|
+
_n2 = _n1 + BigInt(N - 1);
|
|
229
|
+
}
|
|
230
|
+
catch (_a) {
|
|
231
|
+
console.log("Full repayment");
|
|
232
|
+
}
|
|
233
|
+
return [_n2, _n1];
|
|
234
|
+
}), {
|
|
398
235
|
promise: true,
|
|
399
236
|
maxAge: 5 * 60 * 1000, // 5m
|
|
400
237
|
});
|
|
401
|
-
|
|
238
|
+
const llammaData = llamalend.constants.LLAMMAS[id];
|
|
402
239
|
this.id = id;
|
|
403
240
|
this.address = llammaData.amm_address;
|
|
404
241
|
this.controller = llammaData.controller_address;
|
|
@@ -487,2512 +324,1345 @@ var MintMarketTemplate = /** @class */ (function () {
|
|
|
487
324
|
},
|
|
488
325
|
};
|
|
489
326
|
}
|
|
490
|
-
|
|
491
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
};
|
|
516
|
-
MintMarketTemplate.prototype.statsLiquidatingBand = function () {
|
|
517
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
518
|
-
var activeBand, _a, stablecoin, collateral;
|
|
519
|
-
return __generator(this, function (_c) {
|
|
520
|
-
switch (_c.label) {
|
|
521
|
-
case 0: return [4 /*yield*/, this.statsActiveBand()];
|
|
522
|
-
case 1:
|
|
523
|
-
activeBand = _c.sent();
|
|
524
|
-
return [4 /*yield*/, this.statsBandBalances(activeBand)];
|
|
525
|
-
case 2:
|
|
526
|
-
_a = _c.sent(), stablecoin = _a.stablecoin, collateral = _a.collateral;
|
|
527
|
-
if (Number(stablecoin) > 0 && Number(collateral) > 0)
|
|
528
|
-
return [2 /*return*/, activeBand];
|
|
529
|
-
return [2 /*return*/, null];
|
|
530
|
-
}
|
|
531
|
-
});
|
|
532
|
-
});
|
|
533
|
-
};
|
|
534
|
-
MintMarketTemplate.prototype.statsBandBalances = function (n) {
|
|
535
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
536
|
-
var llammaContract, calls, _balances;
|
|
537
|
-
return __generator(this, function (_a) {
|
|
538
|
-
switch (_a.label) {
|
|
539
|
-
case 0:
|
|
540
|
-
llammaContract = llamalend.contracts[this.address].multicallContract;
|
|
541
|
-
calls = [];
|
|
542
|
-
calls.push(llammaContract.bands_x(n), llammaContract.bands_y(n));
|
|
543
|
-
return [4 /*yield*/, llamalend.multicallProvider.all(calls)];
|
|
544
|
-
case 1:
|
|
545
|
-
_balances = _a.sent();
|
|
546
|
-
return [2 /*return*/, {
|
|
547
|
-
stablecoin: formatUnits(_balances[0]),
|
|
548
|
-
collateral: formatUnits(_balances[1], this.collateralDecimals),
|
|
549
|
-
}];
|
|
550
|
-
}
|
|
551
|
-
});
|
|
552
|
-
});
|
|
553
|
-
};
|
|
554
|
-
MintMarketTemplate.prototype.statsBandsBalances = function () {
|
|
555
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
556
|
-
var _a, max_band, min_band, llammaContract, calls, i, _bands, bands, i, _i, collateral;
|
|
557
|
-
return __generator(this, function (_c) {
|
|
558
|
-
switch (_c.label) {
|
|
559
|
-
case 0: return [4 /*yield*/, this.statsMaxMinBands()];
|
|
560
|
-
case 1:
|
|
561
|
-
_a = _c.sent(), max_band = _a[0], min_band = _a[1];
|
|
562
|
-
llammaContract = llamalend.contracts[this.address].multicallContract;
|
|
563
|
-
calls = [];
|
|
564
|
-
for (i = min_band; i <= max_band; i++) {
|
|
565
|
-
calls.push(llammaContract.bands_x(i), llammaContract.bands_y(i));
|
|
566
|
-
}
|
|
567
|
-
return [4 /*yield*/, llamalend.multicallProvider.all(calls)];
|
|
568
|
-
case 2:
|
|
569
|
-
_bands = _c.sent();
|
|
570
|
-
bands = {};
|
|
571
|
-
for (i = min_band; i <= max_band; i++) {
|
|
572
|
-
_i = i - min_band;
|
|
573
|
-
collateral = formatUnits(_bands[(2 * _i) + 1]);
|
|
574
|
-
collateral = collateral.split(".")[0] + "." +
|
|
575
|
-
(collateral.split(".")[1] || "0").slice(0, this.collateralDecimals);
|
|
576
|
-
bands[i] = {
|
|
577
|
-
stablecoin: formatUnits(_bands[2 * _i]),
|
|
578
|
-
collateral: collateral,
|
|
579
|
-
};
|
|
580
|
-
}
|
|
581
|
-
return [2 /*return*/, bands];
|
|
582
|
-
}
|
|
583
|
-
});
|
|
327
|
+
statsBalances() {
|
|
328
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
329
|
+
const crvusdContract = llamalend.contracts[llamalend.address].multicallContract;
|
|
330
|
+
const collateralContract = llamalend.contracts[isEth(this.collateral) ? llamalend.constants.WETH : this.collateral].multicallContract;
|
|
331
|
+
const contract = llamalend.contracts[this.address].multicallContract;
|
|
332
|
+
const calls = [
|
|
333
|
+
crvusdContract.balanceOf(this.address),
|
|
334
|
+
collateralContract.balanceOf(this.address),
|
|
335
|
+
contract.admin_fees_x(),
|
|
336
|
+
contract.admin_fees_y(),
|
|
337
|
+
];
|
|
338
|
+
const [_crvusdBalance, _collateralBalance, _crvusdAdminFees, _collateralAdminFees] = yield llamalend.multicallProvider.all(calls);
|
|
339
|
+
return [
|
|
340
|
+
formatUnits(_crvusdBalance - _crvusdAdminFees),
|
|
341
|
+
formatUnits(_collateralBalance - _collateralAdminFees, this.collateralDecimals),
|
|
342
|
+
];
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
statsLiquidatingBand() {
|
|
346
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
347
|
+
const activeBand = yield this.statsActiveBand();
|
|
348
|
+
const { stablecoin, collateral } = yield this.statsBandBalances(activeBand);
|
|
349
|
+
if (Number(stablecoin) > 0 && Number(collateral) > 0)
|
|
350
|
+
return activeBand;
|
|
351
|
+
return null;
|
|
584
352
|
});
|
|
585
|
-
}
|
|
353
|
+
}
|
|
354
|
+
statsBandBalances(n) {
|
|
355
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
356
|
+
const llammaContract = llamalend.contracts[this.address].multicallContract;
|
|
357
|
+
const calls = [];
|
|
358
|
+
calls.push(llammaContract.bands_x(n), llammaContract.bands_y(n));
|
|
359
|
+
const _balances = yield llamalend.multicallProvider.all(calls);
|
|
360
|
+
return {
|
|
361
|
+
stablecoin: formatUnits(_balances[0]),
|
|
362
|
+
collateral: formatUnits(_balances[1], this.collateralDecimals),
|
|
363
|
+
};
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
statsBandsBalances() {
|
|
367
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
368
|
+
const [max_band, min_band] = yield this.statsMaxMinBands();
|
|
369
|
+
const llammaContract = llamalend.contracts[this.address].multicallContract;
|
|
370
|
+
const calls = [];
|
|
371
|
+
for (let i = min_band; i <= max_band; i++) {
|
|
372
|
+
calls.push(llammaContract.bands_x(i), llammaContract.bands_y(i));
|
|
373
|
+
}
|
|
374
|
+
const _bands = yield llamalend.multicallProvider.all(calls);
|
|
375
|
+
const bands = {};
|
|
376
|
+
for (let i = min_band; i <= max_band; i++) {
|
|
377
|
+
const _i = i - min_band;
|
|
378
|
+
let collateral = formatUnits(_bands[(2 * _i) + 1]);
|
|
379
|
+
collateral = collateral.split(".")[0] + "." +
|
|
380
|
+
(collateral.split(".")[1] || "0").slice(0, this.collateralDecimals);
|
|
381
|
+
bands[i] = {
|
|
382
|
+
stablecoin: formatUnits(_bands[2 * _i]),
|
|
383
|
+
collateral,
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
return bands;
|
|
387
|
+
});
|
|
388
|
+
}
|
|
586
389
|
// ---------------------------------------
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
return
|
|
591
|
-
switch (_a.label) {
|
|
592
|
-
case 0:
|
|
593
|
-
address = _getAddress(address);
|
|
594
|
-
return [4 /*yield*/, llamalend.contracts[this.controller].contract.loan_exists(address, llamalend.constantOptions)];
|
|
595
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
596
|
-
}
|
|
597
|
-
});
|
|
598
|
-
});
|
|
599
|
-
};
|
|
600
|
-
MintMarketTemplate.prototype.userDebt = function (address) {
|
|
601
|
-
if (address === void 0) { address = ""; }
|
|
602
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
603
|
-
var debt;
|
|
604
|
-
return __generator(this, function (_a) {
|
|
605
|
-
switch (_a.label) {
|
|
606
|
-
case 0:
|
|
607
|
-
address = _getAddress(address);
|
|
608
|
-
return [4 /*yield*/, llamalend.contracts[this.controller].contract.debt(address, llamalend.constantOptions)];
|
|
609
|
-
case 1:
|
|
610
|
-
debt = _a.sent();
|
|
611
|
-
return [2 /*return*/, formatUnits(debt)];
|
|
612
|
-
}
|
|
613
|
-
});
|
|
614
|
-
});
|
|
615
|
-
};
|
|
616
|
-
MintMarketTemplate.prototype.userHealth = function (full, address) {
|
|
617
|
-
if (full === void 0) { full = true; }
|
|
618
|
-
if (address === void 0) { address = ""; }
|
|
619
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
620
|
-
var _health;
|
|
621
|
-
return __generator(this, function (_a) {
|
|
622
|
-
switch (_a.label) {
|
|
623
|
-
case 0:
|
|
624
|
-
address = _getAddress(address);
|
|
625
|
-
return [4 /*yield*/, llamalend.contracts[this.controller].contract.health(address, full, llamalend.constantOptions)];
|
|
626
|
-
case 1:
|
|
627
|
-
_health = _a.sent();
|
|
628
|
-
_health = _health * BigInt(100);
|
|
629
|
-
return [2 /*return*/, formatUnits(_health)];
|
|
630
|
-
}
|
|
631
|
-
});
|
|
632
|
-
});
|
|
633
|
-
};
|
|
634
|
-
MintMarketTemplate.prototype.userBands = function (address) {
|
|
635
|
-
if (address === void 0) { address = ""; }
|
|
636
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
637
|
-
var _bands;
|
|
638
|
-
return __generator(this, function (_a) {
|
|
639
|
-
switch (_a.label) {
|
|
640
|
-
case 0:
|
|
641
|
-
address = _getAddress(address);
|
|
642
|
-
return [4 /*yield*/, llamalend.contracts[this.address].contract.read_user_tick_numbers(address, llamalend.constantOptions)];
|
|
643
|
-
case 1:
|
|
644
|
-
_bands = _a.sent();
|
|
645
|
-
return [2 /*return*/, _bands.map(function (_t) { return _t.toNumber(); }).reverse()];
|
|
646
|
-
}
|
|
647
|
-
});
|
|
648
|
-
});
|
|
649
|
-
};
|
|
650
|
-
MintMarketTemplate.prototype.userRange = function (address) {
|
|
651
|
-
if (address === void 0) { address = ""; }
|
|
652
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
653
|
-
var _a, n2, n1;
|
|
654
|
-
return __generator(this, function (_c) {
|
|
655
|
-
switch (_c.label) {
|
|
656
|
-
case 0: return [4 /*yield*/, this.userBands(address)];
|
|
657
|
-
case 1:
|
|
658
|
-
_a = _c.sent(), n2 = _a[0], n1 = _a[1];
|
|
659
|
-
if (n1 == n2)
|
|
660
|
-
return [2 /*return*/, 0];
|
|
661
|
-
return [2 /*return*/, n2 - n1 + 1];
|
|
662
|
-
}
|
|
663
|
-
});
|
|
664
|
-
});
|
|
665
|
-
};
|
|
666
|
-
MintMarketTemplate.prototype.userPrices = function (address) {
|
|
667
|
-
if (address === void 0) { address = ""; }
|
|
668
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
669
|
-
var _prices;
|
|
670
|
-
return __generator(this, function (_a) {
|
|
671
|
-
switch (_a.label) {
|
|
672
|
-
case 0:
|
|
673
|
-
address = _getAddress(address);
|
|
674
|
-
return [4 /*yield*/, llamalend.contracts[this.controller].contract.user_prices(address, llamalend.constantOptions)];
|
|
675
|
-
case 1:
|
|
676
|
-
_prices = _a.sent();
|
|
677
|
-
return [2 /*return*/, _prices.map(function (_p) { return formatUnits(_p); }).reverse()];
|
|
678
|
-
}
|
|
679
|
-
});
|
|
680
|
-
});
|
|
681
|
-
};
|
|
682
|
-
MintMarketTemplate.prototype._userState = function (address) {
|
|
683
|
-
if (address === void 0) { address = ""; }
|
|
684
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
685
|
-
var contract, _a, _collateral, _stablecoin, _debt;
|
|
686
|
-
return __generator(this, function (_c) {
|
|
687
|
-
switch (_c.label) {
|
|
688
|
-
case 0:
|
|
689
|
-
address = _getAddress(address);
|
|
690
|
-
contract = llamalend.contracts[this.controller].contract;
|
|
691
|
-
return [4 /*yield*/, contract.user_state(address, llamalend.constantOptions)];
|
|
692
|
-
case 1:
|
|
693
|
-
_a = _c.sent(), _collateral = _a[0], _stablecoin = _a[1], _debt = _a[2];
|
|
694
|
-
return [2 /*return*/, { _collateral: _collateral, _stablecoin: _stablecoin, _debt: _debt }];
|
|
695
|
-
}
|
|
696
|
-
});
|
|
697
|
-
});
|
|
698
|
-
};
|
|
699
|
-
MintMarketTemplate.prototype.userState = function (address) {
|
|
700
|
-
if (address === void 0) { address = ""; }
|
|
701
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
702
|
-
var _a, _collateral, _stablecoin, _debt;
|
|
703
|
-
return __generator(this, function (_c) {
|
|
704
|
-
switch (_c.label) {
|
|
705
|
-
case 0: return [4 /*yield*/, this._userState(address)];
|
|
706
|
-
case 1:
|
|
707
|
-
_a = _c.sent(), _collateral = _a._collateral, _stablecoin = _a._stablecoin, _debt = _a._debt;
|
|
708
|
-
return [2 /*return*/, {
|
|
709
|
-
collateral: formatUnits(_collateral, this.collateralDecimals),
|
|
710
|
-
stablecoin: formatUnits(_stablecoin),
|
|
711
|
-
debt: formatUnits(_debt),
|
|
712
|
-
}];
|
|
713
|
-
}
|
|
714
|
-
});
|
|
715
|
-
});
|
|
716
|
-
};
|
|
717
|
-
MintMarketTemplate.prototype.userLoss = function (userAddress) {
|
|
718
|
-
if (userAddress === void 0) { userAddress = ""; }
|
|
719
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
720
|
-
var _a, deposited_collateral, _current_collateral_estimation, current_collateral_estimation, loss, loss_pct;
|
|
721
|
-
return __generator(this, function (_c) {
|
|
722
|
-
switch (_c.label) {
|
|
723
|
-
case 0:
|
|
724
|
-
userAddress = _getAddress(userAddress);
|
|
725
|
-
return [4 /*yield*/, Promise.all([
|
|
726
|
-
_getUserCollateralCrvUsd(llamalend.constants.NETWORK_NAME, this.controller, userAddress),
|
|
727
|
-
llamalend.contracts[this.address].contract.get_y_up(userAddress),
|
|
728
|
-
])];
|
|
729
|
-
case 1:
|
|
730
|
-
_a = _c.sent(), deposited_collateral = _a[0], _current_collateral_estimation = _a[1];
|
|
731
|
-
current_collateral_estimation = llamalend.formatUnits(_current_collateral_estimation, this.collateralDecimals);
|
|
732
|
-
if (BN(deposited_collateral).lte(0)) {
|
|
733
|
-
return [2 /*return*/, {
|
|
734
|
-
deposited_collateral: deposited_collateral,
|
|
735
|
-
current_collateral_estimation: current_collateral_estimation,
|
|
736
|
-
loss: "0.0",
|
|
737
|
-
loss_pct: "0.0",
|
|
738
|
-
}];
|
|
739
|
-
}
|
|
740
|
-
loss = BN(deposited_collateral).minus(current_collateral_estimation).toString();
|
|
741
|
-
loss_pct = BN(loss).div(deposited_collateral).times(100).toString();
|
|
742
|
-
return [2 /*return*/, {
|
|
743
|
-
deposited_collateral: deposited_collateral,
|
|
744
|
-
current_collateral_estimation: current_collateral_estimation,
|
|
745
|
-
loss: loss,
|
|
746
|
-
loss_pct: loss_pct,
|
|
747
|
-
}];
|
|
748
|
-
}
|
|
749
|
-
});
|
|
750
|
-
});
|
|
751
|
-
};
|
|
752
|
-
MintMarketTemplate.prototype.userBandsBalances = function (address) {
|
|
753
|
-
if (address === void 0) { address = ""; }
|
|
754
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
755
|
-
var _a, n2, n1, contract, _c, _stablecoins, _collaterals, res, i;
|
|
756
|
-
return __generator(this, function (_d) {
|
|
757
|
-
switch (_d.label) {
|
|
758
|
-
case 0: return [4 /*yield*/, this.userBands(address)];
|
|
759
|
-
case 1:
|
|
760
|
-
_a = _d.sent(), n2 = _a[0], n1 = _a[1];
|
|
761
|
-
if (n1 == 0 && n2 == 0)
|
|
762
|
-
return [2 /*return*/, {}];
|
|
763
|
-
address = _getAddress(address);
|
|
764
|
-
contract = llamalend.contracts[this.address].contract;
|
|
765
|
-
return [4 /*yield*/, contract.get_xy(address, llamalend.constantOptions)];
|
|
766
|
-
case 2:
|
|
767
|
-
_c = _d.sent(), _stablecoins = _c[0], _collaterals = _c[1];
|
|
768
|
-
res = {};
|
|
769
|
-
for (i = n1; i <= n2; i++) {
|
|
770
|
-
res[i] = {
|
|
771
|
-
stablecoin: formatUnits(_stablecoins[i - n1], 18),
|
|
772
|
-
collateral: formatUnits(_collaterals[i - n1], this.collateralDecimals),
|
|
773
|
-
};
|
|
774
|
-
}
|
|
775
|
-
return [2 /*return*/, res];
|
|
776
|
-
}
|
|
777
|
-
});
|
|
778
|
-
});
|
|
779
|
-
};
|
|
780
|
-
MintMarketTemplate.prototype.oraclePrice = function () {
|
|
781
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
782
|
-
var _price;
|
|
783
|
-
return __generator(this, function (_a) {
|
|
784
|
-
switch (_a.label) {
|
|
785
|
-
case 0: return [4 /*yield*/, llamalend.contracts[this.address].contract.price_oracle(llamalend.constantOptions)];
|
|
786
|
-
case 1:
|
|
787
|
-
_price = _a.sent();
|
|
788
|
-
return [2 /*return*/, formatUnits(_price)];
|
|
789
|
-
}
|
|
790
|
-
});
|
|
791
|
-
});
|
|
792
|
-
};
|
|
793
|
-
MintMarketTemplate.prototype.oraclePriceBand = function () {
|
|
794
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
795
|
-
var oraclePriceBN, _a, basePriceBN, _c, A_BN, multiplier, term, compareFunc, band, currentTickPriceBN;
|
|
796
|
-
return __generator(this, function (_d) {
|
|
797
|
-
switch (_d.label) {
|
|
798
|
-
case 0:
|
|
799
|
-
_a = BN;
|
|
800
|
-
return [4 /*yield*/, this.oraclePrice()];
|
|
801
|
-
case 1:
|
|
802
|
-
oraclePriceBN = _a.apply(void 0, [_d.sent()]);
|
|
803
|
-
_c = BN;
|
|
804
|
-
return [4 /*yield*/, this.basePrice()];
|
|
805
|
-
case 2:
|
|
806
|
-
basePriceBN = _c.apply(void 0, [_d.sent()]);
|
|
807
|
-
A_BN = BN(this.A);
|
|
808
|
-
multiplier = oraclePriceBN.lte(basePriceBN) ? A_BN.minus(1).div(A_BN) : A_BN.div(A_BN.minus(1));
|
|
809
|
-
term = oraclePriceBN.lte(basePriceBN) ? 1 : -1;
|
|
810
|
-
compareFunc = oraclePriceBN.lte(basePriceBN) ?
|
|
811
|
-
function (oraclePriceBN, currentTickPriceBN) { return oraclePriceBN.lte(currentTickPriceBN); } :
|
|
812
|
-
function (oraclePriceBN, currentTickPriceBN) { return oraclePriceBN.gt(currentTickPriceBN); };
|
|
813
|
-
band = 0;
|
|
814
|
-
currentTickPriceBN = oraclePriceBN.lte(basePriceBN) ? basePriceBN.times(multiplier) : basePriceBN;
|
|
815
|
-
while (compareFunc(oraclePriceBN, currentTickPriceBN)) {
|
|
816
|
-
currentTickPriceBN = currentTickPriceBN.times(multiplier);
|
|
817
|
-
band += term;
|
|
818
|
-
}
|
|
819
|
-
return [2 /*return*/, band];
|
|
820
|
-
}
|
|
821
|
-
});
|
|
822
|
-
});
|
|
823
|
-
};
|
|
824
|
-
MintMarketTemplate.prototype.price = function () {
|
|
825
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
826
|
-
var _price;
|
|
827
|
-
return __generator(this, function (_a) {
|
|
828
|
-
switch (_a.label) {
|
|
829
|
-
case 0: return [4 /*yield*/, llamalend.contracts[this.address].contract.get_p(llamalend.constantOptions)];
|
|
830
|
-
case 1:
|
|
831
|
-
_price = _a.sent();
|
|
832
|
-
return [2 /*return*/, formatUnits(_price)];
|
|
833
|
-
}
|
|
834
|
-
});
|
|
835
|
-
});
|
|
836
|
-
};
|
|
837
|
-
MintMarketTemplate.prototype.calcTickPrice = function (n) {
|
|
838
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
839
|
-
var basePrice, basePriceBN, A_BN;
|
|
840
|
-
return __generator(this, function (_a) {
|
|
841
|
-
switch (_a.label) {
|
|
842
|
-
case 0: return [4 /*yield*/, this.basePrice()];
|
|
843
|
-
case 1:
|
|
844
|
-
basePrice = _a.sent();
|
|
845
|
-
basePriceBN = BN(basePrice);
|
|
846
|
-
A_BN = BN(this.A);
|
|
847
|
-
return [2 /*return*/, _cutZeros(basePriceBN.times(A_BN.minus(1).div(A_BN).pow(n)).toFixed(18))];
|
|
848
|
-
}
|
|
849
|
-
});
|
|
390
|
+
loanExists() {
|
|
391
|
+
return __awaiter(this, arguments, void 0, function* (address = "") {
|
|
392
|
+
address = _getAddress(address);
|
|
393
|
+
return yield llamalend.contracts[this.controller].contract.loan_exists(address, llamalend.constantOptions);
|
|
850
394
|
});
|
|
851
|
-
}
|
|
852
|
-
|
|
853
|
-
return __awaiter(this,
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
395
|
+
}
|
|
396
|
+
userDebt() {
|
|
397
|
+
return __awaiter(this, arguments, void 0, function* (address = "") {
|
|
398
|
+
address = _getAddress(address);
|
|
399
|
+
const debt = yield llamalend.contracts[this.controller].contract.debt(address, llamalend.constantOptions);
|
|
400
|
+
return formatUnits(debt);
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
userHealth() {
|
|
404
|
+
return __awaiter(this, arguments, void 0, function* (full = true, address = "") {
|
|
405
|
+
address = _getAddress(address);
|
|
406
|
+
let _health = yield llamalend.contracts[this.controller].contract.health(address, full, llamalend.constantOptions);
|
|
407
|
+
_health = _health * BigInt(100);
|
|
408
|
+
return formatUnits(_health);
|
|
409
|
+
});
|
|
410
|
+
}
|
|
411
|
+
userBands() {
|
|
412
|
+
return __awaiter(this, arguments, void 0, function* (address = "") {
|
|
413
|
+
address = _getAddress(address);
|
|
414
|
+
const _bands = yield llamalend.contracts[this.address].contract.read_user_tick_numbers(address, llamalend.constantOptions);
|
|
415
|
+
return _bands.map((_t) => _t.toNumber()).reverse();
|
|
416
|
+
});
|
|
417
|
+
}
|
|
418
|
+
userRange() {
|
|
419
|
+
return __awaiter(this, arguments, void 0, function* (address = "") {
|
|
420
|
+
const [n2, n1] = yield this.userBands(address);
|
|
421
|
+
if (n1 == n2)
|
|
422
|
+
return 0;
|
|
423
|
+
return n2 - n1 + 1;
|
|
424
|
+
});
|
|
425
|
+
}
|
|
426
|
+
userPrices() {
|
|
427
|
+
return __awaiter(this, arguments, void 0, function* (address = "") {
|
|
428
|
+
address = _getAddress(address);
|
|
429
|
+
const _prices = yield llamalend.contracts[this.controller].contract.user_prices(address, llamalend.constantOptions);
|
|
430
|
+
return _prices.map((_p) => formatUnits(_p)).reverse();
|
|
431
|
+
});
|
|
432
|
+
}
|
|
433
|
+
_userState() {
|
|
434
|
+
return __awaiter(this, arguments, void 0, function* (address = "") {
|
|
435
|
+
address = _getAddress(address);
|
|
436
|
+
const contract = llamalend.contracts[this.controller].contract;
|
|
437
|
+
const [_collateral, _stablecoin, _debt] = yield contract.user_state(address, llamalend.constantOptions);
|
|
438
|
+
return { _collateral, _stablecoin, _debt };
|
|
439
|
+
});
|
|
440
|
+
}
|
|
441
|
+
userState() {
|
|
442
|
+
return __awaiter(this, arguments, void 0, function* (address = "") {
|
|
443
|
+
const { _collateral, _stablecoin, _debt } = yield this._userState(address);
|
|
444
|
+
return {
|
|
445
|
+
collateral: formatUnits(_collateral, this.collateralDecimals),
|
|
446
|
+
stablecoin: formatUnits(_stablecoin),
|
|
447
|
+
debt: formatUnits(_debt),
|
|
448
|
+
};
|
|
449
|
+
});
|
|
450
|
+
}
|
|
451
|
+
userLoss() {
|
|
452
|
+
return __awaiter(this, arguments, void 0, function* (userAddress = "") {
|
|
453
|
+
userAddress = _getAddress(userAddress);
|
|
454
|
+
const [deposited_collateral, _current_collateral_estimation] = yield Promise.all([
|
|
455
|
+
_getUserCollateralCrvUsd(llamalend.constants.NETWORK_NAME, this.controller, userAddress),
|
|
456
|
+
llamalend.contracts[this.address].contract.get_y_up(userAddress),
|
|
457
|
+
]);
|
|
458
|
+
const current_collateral_estimation = llamalend.formatUnits(_current_collateral_estimation, this.collateralDecimals);
|
|
459
|
+
if (BN(deposited_collateral).lte(0)) {
|
|
460
|
+
return {
|
|
461
|
+
deposited_collateral,
|
|
462
|
+
current_collateral_estimation,
|
|
463
|
+
loss: "0.0",
|
|
464
|
+
loss_pct: "0.0",
|
|
465
|
+
};
|
|
466
|
+
}
|
|
467
|
+
const loss = BN(deposited_collateral).minus(current_collateral_estimation).toString();
|
|
468
|
+
const loss_pct = BN(loss).div(deposited_collateral).times(100).toString();
|
|
469
|
+
return {
|
|
470
|
+
deposited_collateral,
|
|
471
|
+
current_collateral_estimation,
|
|
472
|
+
loss,
|
|
473
|
+
loss_pct,
|
|
474
|
+
};
|
|
475
|
+
});
|
|
476
|
+
}
|
|
477
|
+
userBandsBalances() {
|
|
478
|
+
return __awaiter(this, arguments, void 0, function* (address = "") {
|
|
479
|
+
const [n2, n1] = yield this.userBands(address);
|
|
480
|
+
if (n1 == 0 && n2 == 0)
|
|
481
|
+
return {};
|
|
482
|
+
address = _getAddress(address);
|
|
483
|
+
const contract = llamalend.contracts[this.address].contract;
|
|
484
|
+
const [_stablecoins, _collaterals] = yield contract.get_xy(address, llamalend.constantOptions);
|
|
485
|
+
const res = {};
|
|
486
|
+
for (let i = n1; i <= n2; i++) {
|
|
487
|
+
res[i] = {
|
|
488
|
+
stablecoin: formatUnits(_stablecoins[i - n1], 18),
|
|
489
|
+
collateral: formatUnits(_collaterals[i - n1], this.collateralDecimals),
|
|
490
|
+
};
|
|
491
|
+
}
|
|
492
|
+
return res;
|
|
493
|
+
});
|
|
494
|
+
}
|
|
495
|
+
oraclePrice() {
|
|
496
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
497
|
+
const _price = yield llamalend.contracts[this.address].contract.price_oracle(llamalend.constantOptions);
|
|
498
|
+
return formatUnits(_price);
|
|
499
|
+
});
|
|
500
|
+
}
|
|
501
|
+
oraclePriceBand() {
|
|
502
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
503
|
+
const oraclePriceBN = BN(yield this.oraclePrice());
|
|
504
|
+
const basePriceBN = BN(yield this.basePrice());
|
|
505
|
+
const A_BN = BN(this.A);
|
|
506
|
+
const multiplier = oraclePriceBN.lte(basePriceBN) ? A_BN.minus(1).div(A_BN) : A_BN.div(A_BN.minus(1));
|
|
507
|
+
const term = oraclePriceBN.lte(basePriceBN) ? 1 : -1;
|
|
508
|
+
const compareFunc = oraclePriceBN.lte(basePriceBN) ?
|
|
509
|
+
(oraclePriceBN, currentTickPriceBN) => oraclePriceBN.lte(currentTickPriceBN) :
|
|
510
|
+
(oraclePriceBN, currentTickPriceBN) => oraclePriceBN.gt(currentTickPriceBN);
|
|
511
|
+
let band = 0;
|
|
512
|
+
let currentTickPriceBN = oraclePriceBN.lte(basePriceBN) ? basePriceBN.times(multiplier) : basePriceBN;
|
|
513
|
+
while (compareFunc(oraclePriceBN, currentTickPriceBN)) {
|
|
514
|
+
currentTickPriceBN = currentTickPriceBN.times(multiplier);
|
|
515
|
+
band += term;
|
|
516
|
+
}
|
|
517
|
+
return band;
|
|
518
|
+
});
|
|
519
|
+
}
|
|
520
|
+
price() {
|
|
521
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
522
|
+
const _price = yield llamalend.contracts[this.address].contract.get_p(llamalend.constantOptions);
|
|
523
|
+
return formatUnits(_price);
|
|
864
524
|
});
|
|
865
|
-
}
|
|
866
|
-
|
|
525
|
+
}
|
|
526
|
+
calcTickPrice(n) {
|
|
527
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
528
|
+
const basePrice = yield this.basePrice();
|
|
529
|
+
const basePriceBN = BN(basePrice);
|
|
530
|
+
const A_BN = BN(this.A);
|
|
531
|
+
return _cutZeros(basePriceBN.times(A_BN.minus(1).div(A_BN).pow(n)).toFixed(18));
|
|
532
|
+
});
|
|
533
|
+
}
|
|
534
|
+
calcBandPrices(n) {
|
|
535
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
536
|
+
return [yield this.calcTickPrice(n + 1), yield this.calcTickPrice(n)];
|
|
537
|
+
});
|
|
538
|
+
}
|
|
539
|
+
calcRangePct(range) {
|
|
867
540
|
/**
|
|
868
541
|
* Calculates range in terms of price difference %
|
|
869
542
|
* @param {number} range Number of bands in range
|
|
870
543
|
* @return {string} Range in %
|
|
871
544
|
*/
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
545
|
+
const A_BN = BN(this.A);
|
|
546
|
+
const startBN = BN(1);
|
|
547
|
+
const endBN = A_BN.minus(1).div(A_BN).pow(range);
|
|
875
548
|
return startBN.minus(endBN).times(100).toString();
|
|
876
|
-
}
|
|
549
|
+
}
|
|
877
550
|
// ---------------- WALLET BALANCES ----------------
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
return __generator(this, function (_c) {
|
|
883
|
-
switch (_c.label) {
|
|
884
|
-
case 0: return [4 /*yield*/, getBalances([this.collateral, llamalend.address], address)];
|
|
885
|
-
case 1:
|
|
886
|
-
_a = _c.sent(), collateral = _a[0], stablecoin = _a[1];
|
|
887
|
-
return [2 /*return*/, { stablecoin: stablecoin, collateral: collateral }];
|
|
888
|
-
}
|
|
889
|
-
});
|
|
551
|
+
walletBalances() {
|
|
552
|
+
return __awaiter(this, arguments, void 0, function* (address = "") {
|
|
553
|
+
const [collateral, stablecoin] = yield getBalances([this.collateral, llamalend.address], address);
|
|
554
|
+
return { stablecoin, collateral };
|
|
890
555
|
});
|
|
891
|
-
}
|
|
556
|
+
}
|
|
892
557
|
// ---------------- CREATE LOAN ----------------
|
|
893
|
-
|
|
558
|
+
_checkRange(range) {
|
|
894
559
|
if (range < this.minBands)
|
|
895
|
-
throw Error(
|
|
560
|
+
throw Error(`range must be >= ${this.minBands}`);
|
|
896
561
|
if (range > this.maxBands)
|
|
897
|
-
throw Error(
|
|
898
|
-
}
|
|
899
|
-
|
|
900
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
562
|
+
throw Error(`range must be <= ${this.maxBands}`);
|
|
563
|
+
}
|
|
564
|
+
createLoanMaxRecv(collateral, range) {
|
|
565
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
566
|
+
this._checkRange(range);
|
|
567
|
+
const _collateral = parseUnits(collateral, this.collateralDecimals);
|
|
568
|
+
return formatUnits(yield llamalend.contracts[this.controller].contract.max_borrowable(_collateral, range, llamalend.constantOptions));
|
|
569
|
+
});
|
|
570
|
+
}
|
|
571
|
+
getMaxRange(collateral, debt) {
|
|
572
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
573
|
+
const maxRecv = yield this.createLoanMaxRecvAllRanges(collateral);
|
|
574
|
+
for (let N = this.minBands; N <= this.maxBands; N++) {
|
|
575
|
+
if (BN(debt).gt(BN(maxRecv[N])))
|
|
576
|
+
return N - 1;
|
|
577
|
+
}
|
|
578
|
+
return this.maxBands;
|
|
579
|
+
});
|
|
580
|
+
}
|
|
581
|
+
_calcN1(_collateral, _debt, range) {
|
|
582
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
583
|
+
this._checkRange(range);
|
|
584
|
+
return yield llamalend.contracts[this.controller].contract.calculate_debt_n1(_collateral, _debt, range, llamalend.constantOptions);
|
|
585
|
+
});
|
|
586
|
+
}
|
|
587
|
+
_calcN1AllRanges(_collateral, _debt, maxN) {
|
|
588
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
589
|
+
const calls = [];
|
|
590
|
+
for (let N = this.minBands; N <= maxN; N++) {
|
|
591
|
+
calls.push(llamalend.contracts[this.controller].multicallContract.calculate_debt_n1(_collateral, _debt, N));
|
|
592
|
+
}
|
|
593
|
+
return yield llamalend.multicallProvider.all(calls);
|
|
594
|
+
});
|
|
595
|
+
}
|
|
596
|
+
_getPrices(_n2, _n1) {
|
|
597
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
598
|
+
const contract = llamalend.contracts[this.address].multicallContract;
|
|
599
|
+
return (yield llamalend.multicallProvider.all([
|
|
600
|
+
contract.p_oracle_down(_n2),
|
|
601
|
+
contract.p_oracle_up(_n1),
|
|
602
|
+
])).map((_p) => formatUnits(_p));
|
|
603
|
+
});
|
|
604
|
+
}
|
|
605
|
+
_calcPrices(_n2, _n1) {
|
|
606
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
607
|
+
return [yield this.calcTickPrice(Number(_n2) + 1), yield this.calcTickPrice(Number(_n1))];
|
|
608
|
+
});
|
|
609
|
+
}
|
|
610
|
+
_createLoanBands(collateral, debt, range) {
|
|
611
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
612
|
+
const _n1 = yield this._calcN1(parseUnits(collateral, this.collateralDecimals), parseUnits(debt), range);
|
|
613
|
+
const _n2 = _n1 + BigInt(range - 1);
|
|
614
|
+
return [_n2, _n1];
|
|
615
|
+
});
|
|
616
|
+
}
|
|
617
|
+
_createLoanBandsAllRanges(collateral, debt) {
|
|
618
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
619
|
+
const maxN = yield this.getMaxRange(collateral, debt);
|
|
620
|
+
const _n1_arr = yield this._calcN1AllRanges(parseUnits(collateral, this.collateralDecimals), parseUnits(debt), maxN);
|
|
621
|
+
const _n2_arr = [];
|
|
622
|
+
for (let N = this.minBands; N <= maxN; N++) {
|
|
623
|
+
_n2_arr.push(_n1_arr[N - this.minBands] + BigInt(N - 1));
|
|
624
|
+
}
|
|
625
|
+
const res = {};
|
|
626
|
+
for (let N = this.minBands; N <= maxN; N++) {
|
|
627
|
+
res[N] = [_n2_arr[N - this.minBands], _n1_arr[N - this.minBands]];
|
|
628
|
+
}
|
|
629
|
+
return res;
|
|
630
|
+
});
|
|
631
|
+
}
|
|
632
|
+
createLoanBands(collateral, debt, range) {
|
|
633
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
634
|
+
const [_n2, _n1] = yield this._createLoanBands(collateral, debt, range);
|
|
635
|
+
return [Number(_n2), Number(_n1)];
|
|
636
|
+
});
|
|
637
|
+
}
|
|
638
|
+
createLoanBandsAllRanges(collateral, debt) {
|
|
639
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
640
|
+
const _bandsAllRanges = yield this._createLoanBandsAllRanges(collateral, debt);
|
|
641
|
+
const bandsAllRanges = {};
|
|
642
|
+
for (let N = this.minBands; N <= this.maxBands; N++) {
|
|
643
|
+
if (_bandsAllRanges[N]) {
|
|
644
|
+
bandsAllRanges[N] = _bandsAllRanges[N].map(Number);
|
|
927
645
|
}
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
};
|
|
931
|
-
MintMarketTemplate.prototype._calcN1 = function (_collateral, _debt, range) {
|
|
932
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
933
|
-
return __generator(this, function (_a) {
|
|
934
|
-
switch (_a.label) {
|
|
935
|
-
case 0:
|
|
936
|
-
this._checkRange(range);
|
|
937
|
-
return [4 /*yield*/, llamalend.contracts[this.controller].contract.calculate_debt_n1(_collateral, _debt, range, llamalend.constantOptions)];
|
|
938
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
646
|
+
else {
|
|
647
|
+
bandsAllRanges[N] = null;
|
|
939
648
|
}
|
|
940
|
-
}
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
649
|
+
}
|
|
650
|
+
return bandsAllRanges;
|
|
651
|
+
});
|
|
652
|
+
}
|
|
653
|
+
createLoanPrices(collateral, debt, range) {
|
|
654
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
655
|
+
const [_n2, _n1] = yield this._createLoanBands(collateral, debt, range);
|
|
656
|
+
return yield this._getPrices(_n2, _n1);
|
|
657
|
+
});
|
|
658
|
+
}
|
|
659
|
+
createLoanPricesAllRanges(collateral, debt) {
|
|
660
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
661
|
+
const _bandsAllRanges = yield this._createLoanBandsAllRanges(collateral, debt);
|
|
662
|
+
const pricesAllRanges = {};
|
|
663
|
+
for (let N = this.minBands; N <= this.maxBands; N++) {
|
|
664
|
+
if (_bandsAllRanges[N]) {
|
|
665
|
+
pricesAllRanges[N] = yield this._calcPrices(..._bandsAllRanges[N]);
|
|
955
666
|
}
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
};
|
|
959
|
-
MintMarketTemplate.prototype._getPrices = function (_n2, _n1) {
|
|
960
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
961
|
-
var contract;
|
|
962
|
-
return __generator(this, function (_a) {
|
|
963
|
-
switch (_a.label) {
|
|
964
|
-
case 0:
|
|
965
|
-
contract = llamalend.contracts[this.address].multicallContract;
|
|
966
|
-
return [4 /*yield*/, llamalend.multicallProvider.all([
|
|
967
|
-
contract.p_oracle_down(_n2),
|
|
968
|
-
contract.p_oracle_up(_n1),
|
|
969
|
-
])];
|
|
970
|
-
case 1: return [2 /*return*/, (_a.sent()).map(function (_p) { return formatUnits(_p); })];
|
|
667
|
+
else {
|
|
668
|
+
pricesAllRanges[N] = null;
|
|
971
669
|
}
|
|
972
|
-
}
|
|
670
|
+
}
|
|
671
|
+
return pricesAllRanges;
|
|
973
672
|
});
|
|
974
|
-
}
|
|
975
|
-
|
|
976
|
-
return __awaiter(this,
|
|
673
|
+
}
|
|
674
|
+
createLoanHealth(collateral_1, debt_1, range_1) {
|
|
675
|
+
return __awaiter(this, arguments, void 0, function* (collateral, debt, range, full = true, address = "") {
|
|
977
676
|
var _a;
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
991
|
-
var _n1, _n2;
|
|
992
|
-
return __generator(this, function (_a) {
|
|
993
|
-
switch (_a.label) {
|
|
994
|
-
case 0: return [4 /*yield*/, this._calcN1(parseUnits(collateral, this.collateralDecimals), parseUnits(debt), range)];
|
|
995
|
-
case 1:
|
|
996
|
-
_n1 = _a.sent();
|
|
997
|
-
_n2 = _n1 + BigInt(range - 1);
|
|
998
|
-
return [2 /*return*/, [_n2, _n1]];
|
|
999
|
-
}
|
|
1000
|
-
});
|
|
1001
|
-
});
|
|
1002
|
-
};
|
|
1003
|
-
MintMarketTemplate.prototype._createLoanBandsAllRanges = function (collateral, debt) {
|
|
1004
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1005
|
-
var maxN, _n1_arr, _n2_arr, N, res, N;
|
|
1006
|
-
return __generator(this, function (_a) {
|
|
1007
|
-
switch (_a.label) {
|
|
1008
|
-
case 0: return [4 /*yield*/, this.getMaxRange(collateral, debt)];
|
|
1009
|
-
case 1:
|
|
1010
|
-
maxN = _a.sent();
|
|
1011
|
-
return [4 /*yield*/, this._calcN1AllRanges(parseUnits(collateral, this.collateralDecimals), parseUnits(debt), maxN)];
|
|
1012
|
-
case 2:
|
|
1013
|
-
_n1_arr = _a.sent();
|
|
1014
|
-
_n2_arr = [];
|
|
1015
|
-
for (N = this.minBands; N <= maxN; N++) {
|
|
1016
|
-
_n2_arr.push(_n1_arr[N - this.minBands] + BigInt(N - 1));
|
|
1017
|
-
}
|
|
1018
|
-
res = {};
|
|
1019
|
-
for (N = this.minBands; N <= maxN; N++) {
|
|
1020
|
-
res[N] = [_n2_arr[N - this.minBands], _n1_arr[N - this.minBands]];
|
|
1021
|
-
}
|
|
1022
|
-
return [2 /*return*/, res];
|
|
1023
|
-
}
|
|
1024
|
-
});
|
|
1025
|
-
});
|
|
1026
|
-
};
|
|
1027
|
-
MintMarketTemplate.prototype.createLoanBands = function (collateral, debt, range) {
|
|
1028
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1029
|
-
var _a, _n2, _n1;
|
|
1030
|
-
return __generator(this, function (_c) {
|
|
1031
|
-
switch (_c.label) {
|
|
1032
|
-
case 0: return [4 /*yield*/, this._createLoanBands(collateral, debt, range)];
|
|
1033
|
-
case 1:
|
|
1034
|
-
_a = _c.sent(), _n2 = _a[0], _n1 = _a[1];
|
|
1035
|
-
return [2 /*return*/, [Number(_n2), Number(_n1)]];
|
|
1036
|
-
}
|
|
1037
|
-
});
|
|
1038
|
-
});
|
|
1039
|
-
};
|
|
1040
|
-
MintMarketTemplate.prototype.createLoanBandsAllRanges = function (collateral, debt) {
|
|
1041
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1042
|
-
var _bandsAllRanges, bandsAllRanges, N;
|
|
1043
|
-
return __generator(this, function (_a) {
|
|
1044
|
-
switch (_a.label) {
|
|
1045
|
-
case 0: return [4 /*yield*/, this._createLoanBandsAllRanges(collateral, debt)];
|
|
1046
|
-
case 1:
|
|
1047
|
-
_bandsAllRanges = _a.sent();
|
|
1048
|
-
bandsAllRanges = {};
|
|
1049
|
-
for (N = this.minBands; N <= this.maxBands; N++) {
|
|
1050
|
-
if (_bandsAllRanges[N]) {
|
|
1051
|
-
bandsAllRanges[N] = _bandsAllRanges[N].map(Number);
|
|
1052
|
-
}
|
|
1053
|
-
else {
|
|
1054
|
-
bandsAllRanges[N] = null;
|
|
1055
|
-
}
|
|
1056
|
-
}
|
|
1057
|
-
return [2 /*return*/, bandsAllRanges];
|
|
1058
|
-
}
|
|
1059
|
-
});
|
|
1060
|
-
});
|
|
1061
|
-
};
|
|
1062
|
-
MintMarketTemplate.prototype.createLoanPrices = function (collateral, debt, range) {
|
|
1063
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1064
|
-
var _a, _n2, _n1;
|
|
1065
|
-
return __generator(this, function (_c) {
|
|
1066
|
-
switch (_c.label) {
|
|
1067
|
-
case 0: return [4 /*yield*/, this._createLoanBands(collateral, debt, range)];
|
|
1068
|
-
case 1:
|
|
1069
|
-
_a = _c.sent(), _n2 = _a[0], _n1 = _a[1];
|
|
1070
|
-
return [4 /*yield*/, this._getPrices(_n2, _n1)];
|
|
1071
|
-
case 2: return [2 /*return*/, _c.sent()];
|
|
1072
|
-
}
|
|
1073
|
-
});
|
|
1074
|
-
});
|
|
1075
|
-
};
|
|
1076
|
-
MintMarketTemplate.prototype.createLoanPricesAllRanges = function (collateral, debt) {
|
|
1077
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1078
|
-
var _bandsAllRanges, pricesAllRanges, N, _a, _c;
|
|
1079
|
-
return __generator(this, function (_d) {
|
|
1080
|
-
switch (_d.label) {
|
|
1081
|
-
case 0: return [4 /*yield*/, this._createLoanBandsAllRanges(collateral, debt)];
|
|
1082
|
-
case 1:
|
|
1083
|
-
_bandsAllRanges = _d.sent();
|
|
1084
|
-
pricesAllRanges = {};
|
|
1085
|
-
N = this.minBands;
|
|
1086
|
-
_d.label = 2;
|
|
1087
|
-
case 2:
|
|
1088
|
-
if (!(N <= this.maxBands)) return [3 /*break*/, 6];
|
|
1089
|
-
if (!_bandsAllRanges[N]) return [3 /*break*/, 4];
|
|
1090
|
-
_a = pricesAllRanges;
|
|
1091
|
-
_c = N;
|
|
1092
|
-
return [4 /*yield*/, this._calcPrices.apply(this, _bandsAllRanges[N])];
|
|
1093
|
-
case 3:
|
|
1094
|
-
_a[_c] = _d.sent();
|
|
1095
|
-
return [3 /*break*/, 5];
|
|
1096
|
-
case 4:
|
|
1097
|
-
pricesAllRanges[N] = null;
|
|
1098
|
-
_d.label = 5;
|
|
1099
|
-
case 5:
|
|
1100
|
-
N++;
|
|
1101
|
-
return [3 /*break*/, 2];
|
|
1102
|
-
case 6: return [2 /*return*/, pricesAllRanges];
|
|
1103
|
-
}
|
|
1104
|
-
});
|
|
1105
|
-
});
|
|
1106
|
-
};
|
|
1107
|
-
MintMarketTemplate.prototype.createLoanHealth = function (collateral, debt, range, full, address) {
|
|
1108
|
-
var _a;
|
|
1109
|
-
if (full === void 0) { full = true; }
|
|
1110
|
-
if (address === void 0) { address = ""; }
|
|
1111
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1112
|
-
var _collateral, _debt, contract, _health;
|
|
1113
|
-
return __generator(this, function (_c) {
|
|
1114
|
-
switch (_c.label) {
|
|
1115
|
-
case 0:
|
|
1116
|
-
address = _getAddress(address);
|
|
1117
|
-
_collateral = parseUnits(collateral, this.collateralDecimals);
|
|
1118
|
-
_debt = parseUnits(debt);
|
|
1119
|
-
contract = llamalend.contracts[(_a = this.healthCalculator) !== null && _a !== void 0 ? _a : this.controller].contract;
|
|
1120
|
-
return [4 /*yield*/, contract.health_calculator(address, _collateral, _debt, full, range, llamalend.constantOptions)];
|
|
1121
|
-
case 1:
|
|
1122
|
-
_health = _c.sent();
|
|
1123
|
-
_health = _health * BigInt(100);
|
|
1124
|
-
return [2 /*return*/, formatUnits(_health)];
|
|
1125
|
-
}
|
|
1126
|
-
});
|
|
1127
|
-
});
|
|
1128
|
-
};
|
|
1129
|
-
MintMarketTemplate.prototype.createLoanIsApproved = function (collateral) {
|
|
1130
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1131
|
-
return __generator(this, function (_a) {
|
|
1132
|
-
switch (_a.label) {
|
|
1133
|
-
case 0: return [4 /*yield*/, hasAllowance([this.collateral], [collateral], llamalend.signerAddress, this.controller)];
|
|
1134
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
1135
|
-
}
|
|
1136
|
-
});
|
|
1137
|
-
});
|
|
1138
|
-
};
|
|
1139
|
-
MintMarketTemplate.prototype.createLoanApproveEstimateGas = function (collateral) {
|
|
1140
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1141
|
-
return __generator(this, function (_a) {
|
|
1142
|
-
switch (_a.label) {
|
|
1143
|
-
case 0: return [4 /*yield*/, ensureAllowanceEstimateGas([this.collateral], [collateral], this.controller)];
|
|
1144
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
1145
|
-
}
|
|
1146
|
-
});
|
|
1147
|
-
});
|
|
1148
|
-
};
|
|
1149
|
-
MintMarketTemplate.prototype.createLoanApprove = function (collateral) {
|
|
1150
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1151
|
-
return __generator(this, function (_a) {
|
|
1152
|
-
switch (_a.label) {
|
|
1153
|
-
case 0: return [4 /*yield*/, ensureAllowance([this.collateral], [collateral], this.controller)];
|
|
1154
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
1155
|
-
}
|
|
1156
|
-
});
|
|
1157
|
-
});
|
|
1158
|
-
};
|
|
1159
|
-
MintMarketTemplate.prototype._createLoan = function (collateral, debt, range, estimateGas) {
|
|
1160
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1161
|
-
var _collateral, _debt, contract, value, gas, gasLimit;
|
|
1162
|
-
return __generator(this, function (_a) {
|
|
1163
|
-
switch (_a.label) {
|
|
1164
|
-
case 0: return [4 /*yield*/, this.loanExists()];
|
|
1165
|
-
case 1:
|
|
1166
|
-
if (_a.sent())
|
|
1167
|
-
throw Error("Loan already created");
|
|
1168
|
-
this._checkRange(range);
|
|
1169
|
-
_collateral = parseUnits(collateral, this.collateralDecimals);
|
|
1170
|
-
_debt = parseUnits(debt);
|
|
1171
|
-
contract = llamalend.contracts[this.controller].contract;
|
|
1172
|
-
value = isEth(this.collateral) ? _collateral : llamalend.parseUnits("0");
|
|
1173
|
-
return [4 /*yield*/, contract.create_loan.estimateGas(_collateral, _debt, range, __assign(__assign({}, llamalend.constantOptions), { value: value }))];
|
|
1174
|
-
case 2:
|
|
1175
|
-
gas = _a.sent();
|
|
1176
|
-
if (estimateGas)
|
|
1177
|
-
return [2 /*return*/, smartNumber(gas)];
|
|
1178
|
-
return [4 /*yield*/, llamalend.updateFeeData()];
|
|
1179
|
-
case 3:
|
|
1180
|
-
_a.sent();
|
|
1181
|
-
gasLimit = _mulBy1_3(DIGas(gas));
|
|
1182
|
-
return [4 /*yield*/, contract.create_loan(_collateral, _debt, range, __assign(__assign({}, llamalend.options), { gasLimit: gasLimit, value: value }))];
|
|
1183
|
-
case 4: return [2 /*return*/, (_a.sent()).hash];
|
|
1184
|
-
}
|
|
1185
|
-
});
|
|
1186
|
-
});
|
|
1187
|
-
};
|
|
1188
|
-
MintMarketTemplate.prototype.createLoanEstimateGas = function (collateral, debt, range) {
|
|
1189
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1190
|
-
return __generator(this, function (_a) {
|
|
1191
|
-
switch (_a.label) {
|
|
1192
|
-
case 0: return [4 /*yield*/, this.createLoanIsApproved(collateral)];
|
|
1193
|
-
case 1:
|
|
1194
|
-
if (!(_a.sent()))
|
|
1195
|
-
throw Error("Approval is needed for gas estimation");
|
|
1196
|
-
return [4 /*yield*/, this._createLoan(collateral, debt, range, true)];
|
|
1197
|
-
case 2: return [2 /*return*/, _a.sent()];
|
|
1198
|
-
}
|
|
1199
|
-
});
|
|
1200
|
-
});
|
|
1201
|
-
};
|
|
1202
|
-
MintMarketTemplate.prototype.createLoan = function (collateral, debt, range) {
|
|
1203
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1204
|
-
return __generator(this, function (_a) {
|
|
1205
|
-
switch (_a.label) {
|
|
1206
|
-
case 0: return [4 /*yield*/, this.createLoanApprove(collateral)];
|
|
1207
|
-
case 1:
|
|
1208
|
-
_a.sent();
|
|
1209
|
-
return [4 /*yield*/, this._createLoan(collateral, debt, range, false)];
|
|
1210
|
-
case 2: return [2 /*return*/, _a.sent()];
|
|
1211
|
-
}
|
|
1212
|
-
});
|
|
677
|
+
address = _getAddress(address);
|
|
678
|
+
const _collateral = parseUnits(collateral, this.collateralDecimals);
|
|
679
|
+
const _debt = parseUnits(debt);
|
|
680
|
+
const contract = llamalend.contracts[(_a = this.healthCalculator) !== null && _a !== void 0 ? _a : this.controller].contract;
|
|
681
|
+
let _health = yield contract.health_calculator(address, _collateral, _debt, full, range, llamalend.constantOptions);
|
|
682
|
+
_health = _health * BigInt(100);
|
|
683
|
+
return formatUnits(_health);
|
|
684
|
+
});
|
|
685
|
+
}
|
|
686
|
+
createLoanIsApproved(collateral) {
|
|
687
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
688
|
+
return yield hasAllowance([this.collateral], [collateral], llamalend.signerAddress, this.controller);
|
|
1213
689
|
});
|
|
1214
|
-
}
|
|
690
|
+
}
|
|
691
|
+
createLoanApproveEstimateGas(collateral) {
|
|
692
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
693
|
+
return yield ensureAllowanceEstimateGas([this.collateral], [collateral], this.controller);
|
|
694
|
+
});
|
|
695
|
+
}
|
|
696
|
+
createLoanApprove(collateral) {
|
|
697
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
698
|
+
return yield ensureAllowance([this.collateral], [collateral], this.controller);
|
|
699
|
+
});
|
|
700
|
+
}
|
|
701
|
+
_createLoan(collateral, debt, range, estimateGas) {
|
|
702
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
703
|
+
if (yield this.loanExists())
|
|
704
|
+
throw Error("Loan already created");
|
|
705
|
+
this._checkRange(range);
|
|
706
|
+
const _collateral = parseUnits(collateral, this.collateralDecimals);
|
|
707
|
+
const _debt = parseUnits(debt);
|
|
708
|
+
const contract = llamalend.contracts[this.controller].contract;
|
|
709
|
+
const value = isEth(this.collateral) ? _collateral : llamalend.parseUnits("0");
|
|
710
|
+
const gas = yield contract.create_loan.estimateGas(_collateral, _debt, range, Object.assign(Object.assign({}, llamalend.constantOptions), { value }));
|
|
711
|
+
if (estimateGas)
|
|
712
|
+
return smartNumber(gas);
|
|
713
|
+
yield llamalend.updateFeeData();
|
|
714
|
+
const gasLimit = _mulBy1_3(DIGas(gas));
|
|
715
|
+
return (yield contract.create_loan(_collateral, _debt, range, Object.assign(Object.assign({}, llamalend.options), { gasLimit, value }))).hash;
|
|
716
|
+
});
|
|
717
|
+
}
|
|
718
|
+
createLoanEstimateGas(collateral, debt, range) {
|
|
719
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
720
|
+
if (!(yield this.createLoanIsApproved(collateral)))
|
|
721
|
+
throw Error("Approval is needed for gas estimation");
|
|
722
|
+
return yield this._createLoan(collateral, debt, range, true);
|
|
723
|
+
});
|
|
724
|
+
}
|
|
725
|
+
createLoan(collateral, debt, range) {
|
|
726
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
727
|
+
yield this.createLoanApprove(collateral);
|
|
728
|
+
return yield this._createLoan(collateral, debt, range, false);
|
|
729
|
+
});
|
|
730
|
+
}
|
|
1215
731
|
// ---------------- BORROW MORE ----------------
|
|
1216
|
-
|
|
1217
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
}
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
return
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
case 1:
|
|
1303
|
-
_health = _c.sent();
|
|
1304
|
-
_health = _health * BigInt(100);
|
|
1305
|
-
return [2 /*return*/, formatUnits(_health)];
|
|
1306
|
-
}
|
|
1307
|
-
});
|
|
1308
|
-
});
|
|
1309
|
-
};
|
|
1310
|
-
MintMarketTemplate.prototype.borrowMoreIsApproved = function (collateral) {
|
|
1311
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1312
|
-
return __generator(this, function (_a) {
|
|
1313
|
-
switch (_a.label) {
|
|
1314
|
-
case 0: return [4 /*yield*/, hasAllowance([this.collateral], [collateral], llamalend.signerAddress, this.controller)];
|
|
1315
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
1316
|
-
}
|
|
1317
|
-
});
|
|
1318
|
-
});
|
|
1319
|
-
};
|
|
1320
|
-
MintMarketTemplate.prototype.borrowMoreApproveEstimateGas = function (collateral) {
|
|
1321
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1322
|
-
return __generator(this, function (_a) {
|
|
1323
|
-
switch (_a.label) {
|
|
1324
|
-
case 0: return [4 /*yield*/, ensureAllowanceEstimateGas([this.collateral], [collateral], this.controller)];
|
|
1325
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
1326
|
-
}
|
|
1327
|
-
});
|
|
1328
|
-
});
|
|
1329
|
-
};
|
|
1330
|
-
MintMarketTemplate.prototype.borrowMoreApprove = function (collateral) {
|
|
1331
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1332
|
-
return __generator(this, function (_a) {
|
|
1333
|
-
switch (_a.label) {
|
|
1334
|
-
case 0: return [4 /*yield*/, ensureAllowance([this.collateral], [collateral], this.controller)];
|
|
1335
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
1336
|
-
}
|
|
1337
|
-
});
|
|
1338
|
-
});
|
|
1339
|
-
};
|
|
1340
|
-
MintMarketTemplate.prototype._borrowMore = function (collateral, debt, estimateGas) {
|
|
1341
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1342
|
-
var _a, stablecoin, currentDebt, _collateral, _debt, contract, value, gas, gasLimit;
|
|
1343
|
-
return __generator(this, function (_c) {
|
|
1344
|
-
switch (_c.label) {
|
|
1345
|
-
case 0: return [4 /*yield*/, this.userState()];
|
|
1346
|
-
case 1:
|
|
1347
|
-
_a = _c.sent(), stablecoin = _a.stablecoin, currentDebt = _a.debt;
|
|
1348
|
-
if (Number(currentDebt) === 0)
|
|
1349
|
-
throw Error("Loan for ".concat(llamalend.signerAddress, " does not exist"));
|
|
1350
|
-
if (Number(stablecoin) > 0)
|
|
1351
|
-
throw Error("User ".concat(llamalend.signerAddress, " is already in liquidation mode"));
|
|
1352
|
-
_collateral = parseUnits(collateral, this.collateralDecimals);
|
|
1353
|
-
_debt = parseUnits(debt);
|
|
1354
|
-
contract = llamalend.contracts[this.controller].contract;
|
|
1355
|
-
value = isEth(this.collateral) ? _collateral : llamalend.parseUnits("0");
|
|
1356
|
-
return [4 /*yield*/, contract.borrow_more.estimateGas(_collateral, _debt, __assign(__assign({}, llamalend.constantOptions), { value: value }))];
|
|
1357
|
-
case 2:
|
|
1358
|
-
gas = _c.sent();
|
|
1359
|
-
if (estimateGas)
|
|
1360
|
-
return [2 /*return*/, smartNumber(gas)];
|
|
1361
|
-
return [4 /*yield*/, llamalend.updateFeeData()];
|
|
1362
|
-
case 3:
|
|
1363
|
-
_c.sent();
|
|
1364
|
-
gasLimit = _mulBy1_3(DIGas(gas));
|
|
1365
|
-
return [4 /*yield*/, contract.borrow_more(_collateral, _debt, __assign(__assign({}, llamalend.options), { gasLimit: gasLimit, value: value }))];
|
|
1366
|
-
case 4: return [2 /*return*/, (_c.sent()).hash];
|
|
1367
|
-
}
|
|
1368
|
-
});
|
|
1369
|
-
});
|
|
1370
|
-
};
|
|
1371
|
-
MintMarketTemplate.prototype.borrowMoreEstimateGas = function (collateral, debt) {
|
|
1372
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1373
|
-
return __generator(this, function (_a) {
|
|
1374
|
-
switch (_a.label) {
|
|
1375
|
-
case 0: return [4 /*yield*/, this.borrowMoreIsApproved(collateral)];
|
|
1376
|
-
case 1:
|
|
1377
|
-
if (!(_a.sent()))
|
|
1378
|
-
throw Error("Approval is needed for gas estimation");
|
|
1379
|
-
return [4 /*yield*/, this._borrowMore(collateral, debt, true)];
|
|
1380
|
-
case 2: return [2 /*return*/, _a.sent()];
|
|
1381
|
-
}
|
|
1382
|
-
});
|
|
1383
|
-
});
|
|
1384
|
-
};
|
|
1385
|
-
MintMarketTemplate.prototype.borrowMore = function (collateral, debt) {
|
|
1386
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1387
|
-
return __generator(this, function (_a) {
|
|
1388
|
-
switch (_a.label) {
|
|
1389
|
-
case 0: return [4 /*yield*/, this.borrowMoreApprove(collateral)];
|
|
1390
|
-
case 1:
|
|
1391
|
-
_a.sent();
|
|
1392
|
-
return [4 /*yield*/, this._borrowMore(collateral, debt, false)];
|
|
1393
|
-
case 2: return [2 /*return*/, _a.sent()];
|
|
1394
|
-
}
|
|
1395
|
-
});
|
|
732
|
+
borrowMoreMaxRecv(collateralAmount) {
|
|
733
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
734
|
+
const { _collateral: _currentCollateral, _debt: _currentDebt } = yield this._userState();
|
|
735
|
+
const N = yield this.userRange();
|
|
736
|
+
const _collateral = _currentCollateral + parseUnits(collateralAmount, this.collateralDecimals);
|
|
737
|
+
const contract = llamalend.contracts[this.controller].contract;
|
|
738
|
+
const _debt = yield contract.max_borrowable(_collateral, N, llamalend.constantOptions);
|
|
739
|
+
return formatUnits(_debt - _currentDebt);
|
|
740
|
+
});
|
|
741
|
+
}
|
|
742
|
+
_borrowMoreBands(collateral, debt) {
|
|
743
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
744
|
+
const { _collateral: _currentCollateral, _debt: _currentDebt } = yield this._userState();
|
|
745
|
+
if (_currentDebt === BigInt(0))
|
|
746
|
+
throw Error(`Loan for ${llamalend.signerAddress} does not exist`);
|
|
747
|
+
const N = yield this.userRange();
|
|
748
|
+
const _collateral = _currentCollateral + parseUnits(collateral, this.collateralDecimals);
|
|
749
|
+
const _debt = _currentDebt + parseUnits(debt);
|
|
750
|
+
const _n1 = yield this._calcN1(_collateral, _debt, N);
|
|
751
|
+
const _n2 = _n1 + BigInt(N - 1);
|
|
752
|
+
return [_n2, _n1];
|
|
753
|
+
});
|
|
754
|
+
}
|
|
755
|
+
borrowMoreBands(collateral, debt) {
|
|
756
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
757
|
+
const [_n2, _n1] = yield this._borrowMoreBands(collateral, debt);
|
|
758
|
+
return [Number(_n2), Number(_n1)];
|
|
759
|
+
});
|
|
760
|
+
}
|
|
761
|
+
borrowMorePrices(collateral, debt) {
|
|
762
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
763
|
+
const [_n2, _n1] = yield this._borrowMoreBands(collateral, debt);
|
|
764
|
+
return yield this._getPrices(_n2, _n1);
|
|
765
|
+
});
|
|
766
|
+
}
|
|
767
|
+
borrowMoreHealth(collateral_1, debt_1) {
|
|
768
|
+
return __awaiter(this, arguments, void 0, function* (collateral, debt, full = true, address = "") {
|
|
769
|
+
var _a;
|
|
770
|
+
address = _getAddress(address);
|
|
771
|
+
const _collateral = parseUnits(collateral, this.collateralDecimals);
|
|
772
|
+
const _debt = parseUnits(debt);
|
|
773
|
+
const contract = llamalend.contracts[(_a = this.healthCalculator) !== null && _a !== void 0 ? _a : this.controller].contract;
|
|
774
|
+
let _health = yield contract.health_calculator(address, _collateral, _debt, full, 0, llamalend.constantOptions);
|
|
775
|
+
_health = _health * BigInt(100);
|
|
776
|
+
return formatUnits(_health);
|
|
777
|
+
});
|
|
778
|
+
}
|
|
779
|
+
borrowMoreIsApproved(collateral) {
|
|
780
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
781
|
+
return yield hasAllowance([this.collateral], [collateral], llamalend.signerAddress, this.controller);
|
|
782
|
+
});
|
|
783
|
+
}
|
|
784
|
+
borrowMoreApproveEstimateGas(collateral) {
|
|
785
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
786
|
+
return yield ensureAllowanceEstimateGas([this.collateral], [collateral], this.controller);
|
|
787
|
+
});
|
|
788
|
+
}
|
|
789
|
+
borrowMoreApprove(collateral) {
|
|
790
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
791
|
+
return yield ensureAllowance([this.collateral], [collateral], this.controller);
|
|
792
|
+
});
|
|
793
|
+
}
|
|
794
|
+
_borrowMore(collateral, debt, estimateGas) {
|
|
795
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
796
|
+
const { stablecoin, debt: currentDebt } = yield this.userState();
|
|
797
|
+
if (Number(currentDebt) === 0)
|
|
798
|
+
throw Error(`Loan for ${llamalend.signerAddress} does not exist`);
|
|
799
|
+
if (Number(stablecoin) > 0)
|
|
800
|
+
throw Error(`User ${llamalend.signerAddress} is already in liquidation mode`);
|
|
801
|
+
const _collateral = parseUnits(collateral, this.collateralDecimals);
|
|
802
|
+
const _debt = parseUnits(debt);
|
|
803
|
+
const contract = llamalend.contracts[this.controller].contract;
|
|
804
|
+
const value = isEth(this.collateral) ? _collateral : llamalend.parseUnits("0");
|
|
805
|
+
const gas = yield contract.borrow_more.estimateGas(_collateral, _debt, Object.assign(Object.assign({}, llamalend.constantOptions), { value }));
|
|
806
|
+
if (estimateGas)
|
|
807
|
+
return smartNumber(gas);
|
|
808
|
+
yield llamalend.updateFeeData();
|
|
809
|
+
const gasLimit = _mulBy1_3(DIGas(gas));
|
|
810
|
+
return (yield contract.borrow_more(_collateral, _debt, Object.assign(Object.assign({}, llamalend.options), { gasLimit, value }))).hash;
|
|
811
|
+
});
|
|
812
|
+
}
|
|
813
|
+
borrowMoreEstimateGas(collateral, debt) {
|
|
814
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
815
|
+
if (!(yield this.borrowMoreIsApproved(collateral)))
|
|
816
|
+
throw Error("Approval is needed for gas estimation");
|
|
817
|
+
return yield this._borrowMore(collateral, debt, true);
|
|
1396
818
|
});
|
|
1397
|
-
}
|
|
819
|
+
}
|
|
820
|
+
borrowMore(collateral, debt) {
|
|
821
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
822
|
+
yield this.borrowMoreApprove(collateral);
|
|
823
|
+
return yield this._borrowMore(collateral, debt, false);
|
|
824
|
+
});
|
|
825
|
+
}
|
|
1398
826
|
// ---------------- ADD COLLATERAL ----------------
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
if (_currentDebt === BigInt(0))
|
|
1411
|
-
throw Error("Loan for ".concat(address, " does not exist"));
|
|
1412
|
-
return [4 /*yield*/, this.userRange(address)];
|
|
1413
|
-
case 2:
|
|
1414
|
-
N = _c.sent();
|
|
1415
|
-
_collateral = _currentCollateral + parseUnits(collateral, this.collateralDecimals);
|
|
1416
|
-
return [4 /*yield*/, this._calcN1(_collateral, _currentDebt, N)];
|
|
1417
|
-
case 3:
|
|
1418
|
-
_n1 = _c.sent();
|
|
1419
|
-
_n2 = _n1 + BigInt(N - 1);
|
|
1420
|
-
return [2 /*return*/, [_n2, _n1]];
|
|
1421
|
-
}
|
|
1422
|
-
});
|
|
1423
|
-
});
|
|
1424
|
-
};
|
|
1425
|
-
MintMarketTemplate.prototype.addCollateralBands = function (collateral, address) {
|
|
1426
|
-
if (address === void 0) { address = ""; }
|
|
1427
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1428
|
-
var _a, _n2, _n1;
|
|
1429
|
-
return __generator(this, function (_c) {
|
|
1430
|
-
switch (_c.label) {
|
|
1431
|
-
case 0: return [4 /*yield*/, this._addCollateralBands(collateral, address)];
|
|
1432
|
-
case 1:
|
|
1433
|
-
_a = _c.sent(), _n2 = _a[0], _n1 = _a[1];
|
|
1434
|
-
return [2 /*return*/, [Number(_n2), Number(_n1)]];
|
|
1435
|
-
}
|
|
1436
|
-
});
|
|
1437
|
-
});
|
|
1438
|
-
};
|
|
1439
|
-
MintMarketTemplate.prototype.addCollateralPrices = function (collateral, address) {
|
|
1440
|
-
if (address === void 0) { address = ""; }
|
|
1441
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1442
|
-
var _a, _n2, _n1;
|
|
1443
|
-
return __generator(this, function (_c) {
|
|
1444
|
-
switch (_c.label) {
|
|
1445
|
-
case 0: return [4 /*yield*/, this._addCollateralBands(collateral, address)];
|
|
1446
|
-
case 1:
|
|
1447
|
-
_a = _c.sent(), _n2 = _a[0], _n1 = _a[1];
|
|
1448
|
-
return [4 /*yield*/, this._getPrices(_n2, _n1)];
|
|
1449
|
-
case 2: return [2 /*return*/, _c.sent()];
|
|
1450
|
-
}
|
|
1451
|
-
});
|
|
1452
|
-
});
|
|
1453
|
-
};
|
|
1454
|
-
MintMarketTemplate.prototype.addCollateralHealth = function (collateral, full, address) {
|
|
1455
|
-
var _a;
|
|
1456
|
-
if (full === void 0) { full = true; }
|
|
1457
|
-
if (address === void 0) { address = ""; }
|
|
1458
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1459
|
-
var _collateral, contract, _health;
|
|
1460
|
-
return __generator(this, function (_c) {
|
|
1461
|
-
switch (_c.label) {
|
|
1462
|
-
case 0:
|
|
1463
|
-
address = _getAddress(address);
|
|
1464
|
-
_collateral = parseUnits(collateral, this.collateralDecimals);
|
|
1465
|
-
contract = llamalend.contracts[(_a = this.healthCalculator) !== null && _a !== void 0 ? _a : this.controller].contract;
|
|
1466
|
-
return [4 /*yield*/, contract.health_calculator(address, _collateral, 0, full, 0, llamalend.constantOptions)];
|
|
1467
|
-
case 1:
|
|
1468
|
-
_health = _c.sent();
|
|
1469
|
-
_health = _health * BigInt(100);
|
|
1470
|
-
return [2 /*return*/, formatUnits(_health)];
|
|
1471
|
-
}
|
|
1472
|
-
});
|
|
1473
|
-
});
|
|
1474
|
-
};
|
|
1475
|
-
MintMarketTemplate.prototype.addCollateralIsApproved = function (collateral) {
|
|
1476
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1477
|
-
return __generator(this, function (_a) {
|
|
1478
|
-
switch (_a.label) {
|
|
1479
|
-
case 0: return [4 /*yield*/, hasAllowance([this.collateral], [collateral], llamalend.signerAddress, this.controller)];
|
|
1480
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
1481
|
-
}
|
|
1482
|
-
});
|
|
1483
|
-
});
|
|
1484
|
-
};
|
|
1485
|
-
MintMarketTemplate.prototype.addCollateralApproveEstimateGas = function (collateral) {
|
|
1486
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1487
|
-
return __generator(this, function (_a) {
|
|
1488
|
-
switch (_a.label) {
|
|
1489
|
-
case 0: return [4 /*yield*/, ensureAllowanceEstimateGas([this.collateral], [collateral], this.controller)];
|
|
1490
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
1491
|
-
}
|
|
1492
|
-
});
|
|
1493
|
-
});
|
|
1494
|
-
};
|
|
1495
|
-
MintMarketTemplate.prototype.addCollateralApprove = function (collateral) {
|
|
1496
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1497
|
-
return __generator(this, function (_a) {
|
|
1498
|
-
switch (_a.label) {
|
|
1499
|
-
case 0: return [4 /*yield*/, ensureAllowance([this.collateral], [collateral], this.controller)];
|
|
1500
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
1501
|
-
}
|
|
1502
|
-
});
|
|
1503
|
-
});
|
|
1504
|
-
};
|
|
1505
|
-
MintMarketTemplate.prototype._addCollateral = function (collateral, address, estimateGas) {
|
|
1506
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1507
|
-
var _a, stablecoin, currentDebt, _collateral, contract, value, gas, gasLimit;
|
|
1508
|
-
return __generator(this, function (_c) {
|
|
1509
|
-
switch (_c.label) {
|
|
1510
|
-
case 0: return [4 /*yield*/, this.userState(address)];
|
|
1511
|
-
case 1:
|
|
1512
|
-
_a = _c.sent(), stablecoin = _a.stablecoin, currentDebt = _a.debt;
|
|
1513
|
-
if (Number(currentDebt) === 0)
|
|
1514
|
-
throw Error("Loan for ".concat(address, " does not exist"));
|
|
1515
|
-
if (Number(stablecoin) > 0)
|
|
1516
|
-
throw Error("User ".concat(address, " is already in liquidation mode"));
|
|
1517
|
-
_collateral = parseUnits(collateral, this.collateralDecimals);
|
|
1518
|
-
contract = llamalend.contracts[this.controller].contract;
|
|
1519
|
-
value = isEth(this.collateral) ? _collateral : llamalend.parseUnits("0");
|
|
1520
|
-
return [4 /*yield*/, contract.add_collateral.estimateGas(_collateral, address, __assign(__assign({}, llamalend.constantOptions), { value: value }))];
|
|
1521
|
-
case 2:
|
|
1522
|
-
gas = _c.sent();
|
|
1523
|
-
if (estimateGas)
|
|
1524
|
-
return [2 /*return*/, smartNumber(gas)];
|
|
1525
|
-
return [4 /*yield*/, llamalend.updateFeeData()];
|
|
1526
|
-
case 3:
|
|
1527
|
-
_c.sent();
|
|
1528
|
-
gasLimit = _mulBy1_3(DIGas(gas));
|
|
1529
|
-
return [4 /*yield*/, contract.add_collateral(_collateral, address, __assign(__assign({}, llamalend.options), { gasLimit: gasLimit, value: value }))];
|
|
1530
|
-
case 4: return [2 /*return*/, (_c.sent()).hash];
|
|
1531
|
-
}
|
|
1532
|
-
});
|
|
1533
|
-
});
|
|
1534
|
-
};
|
|
1535
|
-
MintMarketTemplate.prototype.addCollateralEstimateGas = function (collateral, address) {
|
|
1536
|
-
if (address === void 0) { address = ""; }
|
|
1537
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1538
|
-
return __generator(this, function (_a) {
|
|
1539
|
-
switch (_a.label) {
|
|
1540
|
-
case 0:
|
|
1541
|
-
address = _getAddress(address);
|
|
1542
|
-
return [4 /*yield*/, this.addCollateralIsApproved(collateral)];
|
|
1543
|
-
case 1:
|
|
1544
|
-
if (!(_a.sent()))
|
|
1545
|
-
throw Error("Approval is needed for gas estimation");
|
|
1546
|
-
return [4 /*yield*/, this._addCollateral(collateral, address, true)];
|
|
1547
|
-
case 2: return [2 /*return*/, _a.sent()];
|
|
1548
|
-
}
|
|
1549
|
-
});
|
|
1550
|
-
});
|
|
1551
|
-
};
|
|
1552
|
-
MintMarketTemplate.prototype.addCollateral = function (collateral, address) {
|
|
1553
|
-
if (address === void 0) { address = ""; }
|
|
1554
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1555
|
-
return __generator(this, function (_a) {
|
|
1556
|
-
switch (_a.label) {
|
|
1557
|
-
case 0:
|
|
1558
|
-
address = _getAddress(address);
|
|
1559
|
-
return [4 /*yield*/, this.addCollateralApprove(collateral)];
|
|
1560
|
-
case 1:
|
|
1561
|
-
_a.sent();
|
|
1562
|
-
return [4 /*yield*/, this._addCollateral(collateral, address, false)];
|
|
1563
|
-
case 2: return [2 /*return*/, _a.sent()];
|
|
1564
|
-
}
|
|
1565
|
-
});
|
|
827
|
+
_addCollateralBands(collateral_1) {
|
|
828
|
+
return __awaiter(this, arguments, void 0, function* (collateral, address = "") {
|
|
829
|
+
address = _getAddress(address);
|
|
830
|
+
const { _collateral: _currentCollateral, _debt: _currentDebt } = yield this._userState(address);
|
|
831
|
+
if (_currentDebt === BigInt(0))
|
|
832
|
+
throw Error(`Loan for ${address} does not exist`);
|
|
833
|
+
const N = yield this.userRange(address);
|
|
834
|
+
const _collateral = _currentCollateral + parseUnits(collateral, this.collateralDecimals);
|
|
835
|
+
const _n1 = yield this._calcN1(_collateral, _currentDebt, N);
|
|
836
|
+
const _n2 = _n1 + BigInt(N - 1);
|
|
837
|
+
return [_n2, _n1];
|
|
1566
838
|
});
|
|
1567
|
-
}
|
|
839
|
+
}
|
|
840
|
+
addCollateralBands(collateral_1) {
|
|
841
|
+
return __awaiter(this, arguments, void 0, function* (collateral, address = "") {
|
|
842
|
+
const [_n2, _n1] = yield this._addCollateralBands(collateral, address);
|
|
843
|
+
return [Number(_n2), Number(_n1)];
|
|
844
|
+
});
|
|
845
|
+
}
|
|
846
|
+
addCollateralPrices(collateral_1) {
|
|
847
|
+
return __awaiter(this, arguments, void 0, function* (collateral, address = "") {
|
|
848
|
+
const [_n2, _n1] = yield this._addCollateralBands(collateral, address);
|
|
849
|
+
return yield this._getPrices(_n2, _n1);
|
|
850
|
+
});
|
|
851
|
+
}
|
|
852
|
+
addCollateralHealth(collateral_1) {
|
|
853
|
+
return __awaiter(this, arguments, void 0, function* (collateral, full = true, address = "") {
|
|
854
|
+
var _a;
|
|
855
|
+
address = _getAddress(address);
|
|
856
|
+
const _collateral = parseUnits(collateral, this.collateralDecimals);
|
|
857
|
+
const contract = llamalend.contracts[(_a = this.healthCalculator) !== null && _a !== void 0 ? _a : this.controller].contract;
|
|
858
|
+
let _health = yield contract.health_calculator(address, _collateral, 0, full, 0, llamalend.constantOptions);
|
|
859
|
+
_health = _health * BigInt(100);
|
|
860
|
+
return formatUnits(_health);
|
|
861
|
+
});
|
|
862
|
+
}
|
|
863
|
+
addCollateralIsApproved(collateral) {
|
|
864
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
865
|
+
return yield hasAllowance([this.collateral], [collateral], llamalend.signerAddress, this.controller);
|
|
866
|
+
});
|
|
867
|
+
}
|
|
868
|
+
addCollateralApproveEstimateGas(collateral) {
|
|
869
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
870
|
+
return yield ensureAllowanceEstimateGas([this.collateral], [collateral], this.controller);
|
|
871
|
+
});
|
|
872
|
+
}
|
|
873
|
+
addCollateralApprove(collateral) {
|
|
874
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
875
|
+
return yield ensureAllowance([this.collateral], [collateral], this.controller);
|
|
876
|
+
});
|
|
877
|
+
}
|
|
878
|
+
_addCollateral(collateral, address, estimateGas) {
|
|
879
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
880
|
+
const { stablecoin, debt: currentDebt } = yield this.userState(address);
|
|
881
|
+
if (Number(currentDebt) === 0)
|
|
882
|
+
throw Error(`Loan for ${address} does not exist`);
|
|
883
|
+
if (Number(stablecoin) > 0)
|
|
884
|
+
throw Error(`User ${address} is already in liquidation mode`);
|
|
885
|
+
const _collateral = parseUnits(collateral, this.collateralDecimals);
|
|
886
|
+
const contract = llamalend.contracts[this.controller].contract;
|
|
887
|
+
const value = isEth(this.collateral) ? _collateral : llamalend.parseUnits("0");
|
|
888
|
+
const gas = yield contract.add_collateral.estimateGas(_collateral, address, Object.assign(Object.assign({}, llamalend.constantOptions), { value }));
|
|
889
|
+
if (estimateGas)
|
|
890
|
+
return smartNumber(gas);
|
|
891
|
+
yield llamalend.updateFeeData();
|
|
892
|
+
const gasLimit = _mulBy1_3(DIGas(gas));
|
|
893
|
+
return (yield contract.add_collateral(_collateral, address, Object.assign(Object.assign({}, llamalend.options), { gasLimit, value }))).hash;
|
|
894
|
+
});
|
|
895
|
+
}
|
|
896
|
+
addCollateralEstimateGas(collateral_1) {
|
|
897
|
+
return __awaiter(this, arguments, void 0, function* (collateral, address = "") {
|
|
898
|
+
address = _getAddress(address);
|
|
899
|
+
if (!(yield this.addCollateralIsApproved(collateral)))
|
|
900
|
+
throw Error("Approval is needed for gas estimation");
|
|
901
|
+
return yield this._addCollateral(collateral, address, true);
|
|
902
|
+
});
|
|
903
|
+
}
|
|
904
|
+
addCollateral(collateral_1) {
|
|
905
|
+
return __awaiter(this, arguments, void 0, function* (collateral, address = "") {
|
|
906
|
+
address = _getAddress(address);
|
|
907
|
+
yield this.addCollateralApprove(collateral);
|
|
908
|
+
return yield this._addCollateral(collateral, address, false);
|
|
909
|
+
});
|
|
910
|
+
}
|
|
1568
911
|
// ---------------- REMOVE COLLATERAL ----------------
|
|
1569
|
-
|
|
1570
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
case 1:
|
|
1576
|
-
_a = _c.sent(), _currentCollateral = _a._collateral, _currentDebt = _a._debt;
|
|
1577
|
-
return [4 /*yield*/, this.userRange()];
|
|
1578
|
-
case 2:
|
|
1579
|
-
N = _c.sent();
|
|
1580
|
-
return [4 /*yield*/, llamalend.contracts[this.controller].contract.min_collateral(_currentDebt, N, llamalend.constantOptions)];
|
|
1581
|
-
case 3:
|
|
1582
|
-
_requiredCollateral = _c.sent();
|
|
1583
|
-
return [2 /*return*/, formatUnits(_currentCollateral - _requiredCollateral, this.collateralDecimals)];
|
|
1584
|
-
}
|
|
1585
|
-
});
|
|
1586
|
-
});
|
|
1587
|
-
};
|
|
1588
|
-
MintMarketTemplate.prototype._removeCollateralBands = function (collateral) {
|
|
1589
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1590
|
-
var _a, _currentCollateral, _currentDebt, N, _collateral, _n1, _n2;
|
|
1591
|
-
return __generator(this, function (_c) {
|
|
1592
|
-
switch (_c.label) {
|
|
1593
|
-
case 0: return [4 /*yield*/, this._userState()];
|
|
1594
|
-
case 1:
|
|
1595
|
-
_a = _c.sent(), _currentCollateral = _a._collateral, _currentDebt = _a._debt;
|
|
1596
|
-
if (_currentDebt === BigInt(0))
|
|
1597
|
-
throw Error("Loan for ".concat(llamalend.signerAddress, " does not exist"));
|
|
1598
|
-
return [4 /*yield*/, this.userRange()];
|
|
1599
|
-
case 2:
|
|
1600
|
-
N = _c.sent();
|
|
1601
|
-
_collateral = _currentCollateral - parseUnits(collateral, this.collateralDecimals);
|
|
1602
|
-
return [4 /*yield*/, this._calcN1(_collateral, _currentDebt, N)];
|
|
1603
|
-
case 3:
|
|
1604
|
-
_n1 = _c.sent();
|
|
1605
|
-
_n2 = _n1 + BigInt(N - 1);
|
|
1606
|
-
return [2 /*return*/, [_n2, _n1]];
|
|
1607
|
-
}
|
|
1608
|
-
});
|
|
1609
|
-
});
|
|
1610
|
-
};
|
|
1611
|
-
MintMarketTemplate.prototype.removeCollateralBands = function (collateral) {
|
|
1612
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1613
|
-
var _a, _n2, _n1;
|
|
1614
|
-
return __generator(this, function (_c) {
|
|
1615
|
-
switch (_c.label) {
|
|
1616
|
-
case 0: return [4 /*yield*/, this._removeCollateralBands(collateral)];
|
|
1617
|
-
case 1:
|
|
1618
|
-
_a = _c.sent(), _n2 = _a[0], _n1 = _a[1];
|
|
1619
|
-
return [2 /*return*/, [Number(_n2), Number(_n1)]];
|
|
1620
|
-
}
|
|
1621
|
-
});
|
|
1622
|
-
});
|
|
1623
|
-
};
|
|
1624
|
-
MintMarketTemplate.prototype.removeCollateralPrices = function (collateral) {
|
|
1625
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1626
|
-
var _a, _n2, _n1;
|
|
1627
|
-
return __generator(this, function (_c) {
|
|
1628
|
-
switch (_c.label) {
|
|
1629
|
-
case 0: return [4 /*yield*/, this._removeCollateralBands(collateral)];
|
|
1630
|
-
case 1:
|
|
1631
|
-
_a = _c.sent(), _n2 = _a[0], _n1 = _a[1];
|
|
1632
|
-
return [4 /*yield*/, this._getPrices(_n2, _n1)];
|
|
1633
|
-
case 2: return [2 /*return*/, _c.sent()];
|
|
1634
|
-
}
|
|
1635
|
-
});
|
|
1636
|
-
});
|
|
1637
|
-
};
|
|
1638
|
-
MintMarketTemplate.prototype.removeCollateralHealth = function (collateral, full, address) {
|
|
1639
|
-
var _a;
|
|
1640
|
-
if (full === void 0) { full = true; }
|
|
1641
|
-
if (address === void 0) { address = ""; }
|
|
1642
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1643
|
-
var _collateral, contract, _health;
|
|
1644
|
-
return __generator(this, function (_c) {
|
|
1645
|
-
switch (_c.label) {
|
|
1646
|
-
case 0:
|
|
1647
|
-
address = _getAddress(address);
|
|
1648
|
-
_collateral = parseUnits(collateral, this.collateralDecimals) * BigInt(-1);
|
|
1649
|
-
contract = llamalend.contracts[(_a = this.healthCalculator) !== null && _a !== void 0 ? _a : this.controller].contract;
|
|
1650
|
-
return [4 /*yield*/, contract.health_calculator(address, _collateral, 0, full, 0, llamalend.constantOptions)];
|
|
1651
|
-
case 1:
|
|
1652
|
-
_health = _c.sent();
|
|
1653
|
-
_health = _health * BigInt(100);
|
|
1654
|
-
return [2 /*return*/, formatUnits(_health)];
|
|
1655
|
-
}
|
|
1656
|
-
});
|
|
1657
|
-
});
|
|
1658
|
-
};
|
|
1659
|
-
MintMarketTemplate.prototype._removeCollateral = function (collateral, estimateGas) {
|
|
1660
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1661
|
-
var _a, stablecoin, currentDebt, _collateral, contract, gas, gasLimit;
|
|
1662
|
-
return __generator(this, function (_c) {
|
|
1663
|
-
switch (_c.label) {
|
|
1664
|
-
case 0: return [4 /*yield*/, this.userState()];
|
|
1665
|
-
case 1:
|
|
1666
|
-
_a = _c.sent(), stablecoin = _a.stablecoin, currentDebt = _a.debt;
|
|
1667
|
-
if (Number(currentDebt) === 0)
|
|
1668
|
-
throw Error("Loan for ".concat(llamalend.signerAddress, " does not exist"));
|
|
1669
|
-
if (Number(stablecoin) > 0)
|
|
1670
|
-
throw Error("User ".concat(llamalend.signerAddress, " is already in liquidation mode"));
|
|
1671
|
-
_collateral = parseUnits(collateral, this.collateralDecimals);
|
|
1672
|
-
contract = llamalend.contracts[this.controller].contract;
|
|
1673
|
-
return [4 /*yield*/, contract.remove_collateral.estimateGas(_collateral, isEth(this.collateral), llamalend.constantOptions)];
|
|
1674
|
-
case 2:
|
|
1675
|
-
gas = _c.sent();
|
|
1676
|
-
if (estimateGas)
|
|
1677
|
-
return [2 /*return*/, smartNumber(gas)];
|
|
1678
|
-
return [4 /*yield*/, llamalend.updateFeeData()];
|
|
1679
|
-
case 3:
|
|
1680
|
-
_c.sent();
|
|
1681
|
-
gasLimit = _mulBy1_3(DIGas(gas));
|
|
1682
|
-
return [4 /*yield*/, contract.remove_collateral(_collateral, isEth(this.collateral), __assign(__assign({}, llamalend.options), { gasLimit: gasLimit }))];
|
|
1683
|
-
case 4: return [2 /*return*/, (_c.sent()).hash];
|
|
1684
|
-
}
|
|
1685
|
-
});
|
|
1686
|
-
});
|
|
1687
|
-
};
|
|
1688
|
-
MintMarketTemplate.prototype.removeCollateralEstimateGas = function (collateral) {
|
|
1689
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1690
|
-
return __generator(this, function (_a) {
|
|
1691
|
-
switch (_a.label) {
|
|
1692
|
-
case 0: return [4 /*yield*/, this._removeCollateral(collateral, true)];
|
|
1693
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
1694
|
-
}
|
|
1695
|
-
});
|
|
1696
|
-
});
|
|
1697
|
-
};
|
|
1698
|
-
MintMarketTemplate.prototype.removeCollateral = function (collateral) {
|
|
1699
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1700
|
-
return __generator(this, function (_a) {
|
|
1701
|
-
switch (_a.label) {
|
|
1702
|
-
case 0: return [4 /*yield*/, this._removeCollateral(collateral, false)];
|
|
1703
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
1704
|
-
}
|
|
1705
|
-
});
|
|
912
|
+
maxRemovable() {
|
|
913
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
914
|
+
const { _collateral: _currentCollateral, _debt: _currentDebt } = yield this._userState();
|
|
915
|
+
const N = yield this.userRange();
|
|
916
|
+
const _requiredCollateral = yield llamalend.contracts[this.controller].contract.min_collateral(_currentDebt, N, llamalend.constantOptions);
|
|
917
|
+
return formatUnits(_currentCollateral - _requiredCollateral, this.collateralDecimals);
|
|
1706
918
|
});
|
|
1707
|
-
}
|
|
919
|
+
}
|
|
920
|
+
_removeCollateralBands(collateral) {
|
|
921
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
922
|
+
const { _collateral: _currentCollateral, _debt: _currentDebt } = yield this._userState();
|
|
923
|
+
if (_currentDebt === BigInt(0))
|
|
924
|
+
throw Error(`Loan for ${llamalend.signerAddress} does not exist`);
|
|
925
|
+
const N = yield this.userRange();
|
|
926
|
+
const _collateral = _currentCollateral - parseUnits(collateral, this.collateralDecimals);
|
|
927
|
+
const _n1 = yield this._calcN1(_collateral, _currentDebt, N);
|
|
928
|
+
const _n2 = _n1 + BigInt(N - 1);
|
|
929
|
+
return [_n2, _n1];
|
|
930
|
+
});
|
|
931
|
+
}
|
|
932
|
+
removeCollateralBands(collateral) {
|
|
933
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
934
|
+
const [_n2, _n1] = yield this._removeCollateralBands(collateral);
|
|
935
|
+
return [Number(_n2), Number(_n1)];
|
|
936
|
+
});
|
|
937
|
+
}
|
|
938
|
+
removeCollateralPrices(collateral) {
|
|
939
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
940
|
+
const [_n2, _n1] = yield this._removeCollateralBands(collateral);
|
|
941
|
+
return yield this._getPrices(_n2, _n1);
|
|
942
|
+
});
|
|
943
|
+
}
|
|
944
|
+
removeCollateralHealth(collateral_1) {
|
|
945
|
+
return __awaiter(this, arguments, void 0, function* (collateral, full = true, address = "") {
|
|
946
|
+
var _a;
|
|
947
|
+
address = _getAddress(address);
|
|
948
|
+
const _collateral = parseUnits(collateral, this.collateralDecimals) * BigInt(-1);
|
|
949
|
+
const contract = llamalend.contracts[(_a = this.healthCalculator) !== null && _a !== void 0 ? _a : this.controller].contract;
|
|
950
|
+
let _health = yield contract.health_calculator(address, _collateral, 0, full, 0, llamalend.constantOptions);
|
|
951
|
+
_health = _health * BigInt(100);
|
|
952
|
+
return formatUnits(_health);
|
|
953
|
+
});
|
|
954
|
+
}
|
|
955
|
+
_removeCollateral(collateral, estimateGas) {
|
|
956
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
957
|
+
const { stablecoin, debt: currentDebt } = yield this.userState();
|
|
958
|
+
if (Number(currentDebt) === 0)
|
|
959
|
+
throw Error(`Loan for ${llamalend.signerAddress} does not exist`);
|
|
960
|
+
if (Number(stablecoin) > 0)
|
|
961
|
+
throw Error(`User ${llamalend.signerAddress} is already in liquidation mode`);
|
|
962
|
+
const _collateral = parseUnits(collateral, this.collateralDecimals);
|
|
963
|
+
const contract = llamalend.contracts[this.controller].contract;
|
|
964
|
+
const gas = yield contract.remove_collateral.estimateGas(_collateral, isEth(this.collateral), llamalend.constantOptions);
|
|
965
|
+
if (estimateGas)
|
|
966
|
+
return smartNumber(gas);
|
|
967
|
+
yield llamalend.updateFeeData();
|
|
968
|
+
const gasLimit = _mulBy1_3(DIGas(gas));
|
|
969
|
+
return (yield contract.remove_collateral(_collateral, isEth(this.collateral), Object.assign(Object.assign({}, llamalend.options), { gasLimit }))).hash;
|
|
970
|
+
});
|
|
971
|
+
}
|
|
972
|
+
removeCollateralEstimateGas(collateral) {
|
|
973
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
974
|
+
return yield this._removeCollateral(collateral, true);
|
|
975
|
+
});
|
|
976
|
+
}
|
|
977
|
+
removeCollateral(collateral) {
|
|
978
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
979
|
+
return yield this._removeCollateral(collateral, false);
|
|
980
|
+
});
|
|
981
|
+
}
|
|
1708
982
|
// ---------------- REPAY ----------------
|
|
1709
|
-
|
|
1710
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
case 3:
|
|
1726
|
-
_c = _d.sent();
|
|
1727
|
-
return [3 /*break*/, 6];
|
|
1728
|
-
case 4: return [4 /*yield*/, llamalend.contracts[this.address].contract.read_user_tick_numbers(address, llamalend.constantOptions)];
|
|
1729
|
-
case 5:
|
|
1730
|
-
_c = (_d.sent())[0];
|
|
1731
|
-
_d.label = 6;
|
|
1732
|
-
case 6:
|
|
1733
|
-
_n1 = _c;
|
|
1734
|
-
_n2 = _n1 + BigInt(N - 1);
|
|
1735
|
-
return [2 /*return*/, [_n2, _n1]];
|
|
1736
|
-
}
|
|
1737
|
-
});
|
|
1738
|
-
});
|
|
1739
|
-
};
|
|
1740
|
-
MintMarketTemplate.prototype.repayBands = function (debt, address) {
|
|
1741
|
-
if (address === void 0) { address = ""; }
|
|
1742
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1743
|
-
var _a, _n2, _n1;
|
|
1744
|
-
return __generator(this, function (_c) {
|
|
1745
|
-
switch (_c.label) {
|
|
1746
|
-
case 0: return [4 /*yield*/, this._repayBands(debt, address)];
|
|
1747
|
-
case 1:
|
|
1748
|
-
_a = _c.sent(), _n2 = _a[0], _n1 = _a[1];
|
|
1749
|
-
return [2 /*return*/, [Number(_n2), Number(_n1)]];
|
|
1750
|
-
}
|
|
1751
|
-
});
|
|
1752
|
-
});
|
|
1753
|
-
};
|
|
1754
|
-
MintMarketTemplate.prototype.repayPrices = function (debt, address) {
|
|
1755
|
-
if (address === void 0) { address = ""; }
|
|
1756
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1757
|
-
var _a, _n2, _n1;
|
|
1758
|
-
return __generator(this, function (_c) {
|
|
1759
|
-
switch (_c.label) {
|
|
1760
|
-
case 0: return [4 /*yield*/, this._repayBands(debt, address)];
|
|
1761
|
-
case 1:
|
|
1762
|
-
_a = _c.sent(), _n2 = _a[0], _n1 = _a[1];
|
|
1763
|
-
return [4 /*yield*/, this._getPrices(_n2, _n1)];
|
|
1764
|
-
case 2: return [2 /*return*/, _c.sent()];
|
|
1765
|
-
}
|
|
1766
|
-
});
|
|
1767
|
-
});
|
|
1768
|
-
};
|
|
1769
|
-
MintMarketTemplate.prototype.repayIsApproved = function (debt) {
|
|
1770
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1771
|
-
return __generator(this, function (_a) {
|
|
1772
|
-
switch (_a.label) {
|
|
1773
|
-
case 0: return [4 /*yield*/, hasAllowance([llamalend.address], [debt], llamalend.signerAddress, this.controller)];
|
|
1774
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
1775
|
-
}
|
|
1776
|
-
});
|
|
1777
|
-
});
|
|
1778
|
-
};
|
|
1779
|
-
MintMarketTemplate.prototype.repayApproveEstimateGas = function (debt) {
|
|
1780
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1781
|
-
return __generator(this, function (_a) {
|
|
1782
|
-
switch (_a.label) {
|
|
1783
|
-
case 0: return [4 /*yield*/, ensureAllowanceEstimateGas([llamalend.address], [debt], this.controller)];
|
|
1784
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
1785
|
-
}
|
|
1786
|
-
});
|
|
1787
|
-
});
|
|
1788
|
-
};
|
|
1789
|
-
MintMarketTemplate.prototype.repayApprove = function (debt) {
|
|
1790
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1791
|
-
return __generator(this, function (_a) {
|
|
1792
|
-
switch (_a.label) {
|
|
1793
|
-
case 0: return [4 /*yield*/, ensureAllowance([llamalend.address], [debt], this.controller)];
|
|
1794
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
1795
|
-
}
|
|
1796
|
-
});
|
|
1797
|
-
});
|
|
1798
|
-
};
|
|
1799
|
-
MintMarketTemplate.prototype.repayHealth = function (debt, full, address) {
|
|
1800
|
-
var _a;
|
|
1801
|
-
if (full === void 0) { full = true; }
|
|
1802
|
-
if (address === void 0) { address = ""; }
|
|
1803
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1804
|
-
var _debt, contract, _health;
|
|
1805
|
-
return __generator(this, function (_c) {
|
|
1806
|
-
switch (_c.label) {
|
|
1807
|
-
case 0:
|
|
1808
|
-
address = _getAddress(address);
|
|
1809
|
-
_debt = parseUnits(debt) * BigInt(-1);
|
|
1810
|
-
contract = llamalend.contracts[(_a = this.healthCalculator) !== null && _a !== void 0 ? _a : this.controller].contract;
|
|
1811
|
-
return [4 /*yield*/, contract.health_calculator(address, 0, _debt, full, 0, llamalend.constantOptions)];
|
|
1812
|
-
case 1:
|
|
1813
|
-
_health = _c.sent();
|
|
1814
|
-
_health = _health * BigInt(100);
|
|
1815
|
-
return [2 /*return*/, formatUnits(_health)];
|
|
1816
|
-
}
|
|
1817
|
-
});
|
|
1818
|
-
});
|
|
1819
|
-
};
|
|
1820
|
-
MintMarketTemplate.prototype._repay = function (debt, address, estimateGas) {
|
|
1821
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1822
|
-
var currentDebt, _debt, contract, _a, _, n1, stablecoin, n, gas, gasLimit;
|
|
1823
|
-
return __generator(this, function (_c) {
|
|
1824
|
-
switch (_c.label) {
|
|
1825
|
-
case 0:
|
|
1826
|
-
address = _getAddress(address);
|
|
1827
|
-
return [4 /*yield*/, this.userState(address)];
|
|
1828
|
-
case 1:
|
|
1829
|
-
currentDebt = (_c.sent()).debt;
|
|
1830
|
-
if (Number(currentDebt) === 0)
|
|
1831
|
-
throw Error("Loan for ".concat(address, " does not exist"));
|
|
1832
|
-
_debt = parseUnits(debt);
|
|
1833
|
-
contract = llamalend.contracts[this.controller].contract;
|
|
1834
|
-
return [4 /*yield*/, this.userBands(address)];
|
|
1835
|
-
case 2:
|
|
1836
|
-
_a = _c.sent(), _ = _a[0], n1 = _a[1];
|
|
1837
|
-
return [4 /*yield*/, this.userState(address)];
|
|
1838
|
-
case 3:
|
|
1839
|
-
stablecoin = (_c.sent()).stablecoin;
|
|
1840
|
-
n = (BN(stablecoin).gt(0)) ? MAX_ACTIVE_BAND : n1 - 1;
|
|
1841
|
-
return [4 /*yield*/, contract.repay.estimateGas(_debt, address, n, isEth(this.collateral), llamalend.constantOptions)];
|
|
1842
|
-
case 4:
|
|
1843
|
-
gas = _c.sent();
|
|
1844
|
-
if (estimateGas)
|
|
1845
|
-
return [2 /*return*/, smartNumber(gas)];
|
|
1846
|
-
return [4 /*yield*/, llamalend.updateFeeData()];
|
|
1847
|
-
case 5:
|
|
1848
|
-
_c.sent();
|
|
1849
|
-
gasLimit = _mulBy1_3(DIGas(gas));
|
|
1850
|
-
return [4 /*yield*/, contract.repay(_debt, address, n, isEth(this.collateral), __assign(__assign({}, llamalend.options), { gasLimit: gasLimit }))];
|
|
1851
|
-
case 6: return [2 /*return*/, (_c.sent()).hash];
|
|
1852
|
-
}
|
|
1853
|
-
});
|
|
1854
|
-
});
|
|
1855
|
-
};
|
|
1856
|
-
MintMarketTemplate.prototype.repayEstimateGas = function (debt, address) {
|
|
1857
|
-
if (address === void 0) { address = ""; }
|
|
1858
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1859
|
-
return __generator(this, function (_a) {
|
|
1860
|
-
switch (_a.label) {
|
|
1861
|
-
case 0: return [4 /*yield*/, this.repayIsApproved(debt)];
|
|
1862
|
-
case 1:
|
|
1863
|
-
if (!(_a.sent()))
|
|
1864
|
-
throw Error("Approval is needed for gas estimation");
|
|
1865
|
-
return [4 /*yield*/, this._repay(debt, address, true)];
|
|
1866
|
-
case 2: return [2 /*return*/, _a.sent()];
|
|
1867
|
-
}
|
|
1868
|
-
});
|
|
1869
|
-
});
|
|
1870
|
-
};
|
|
1871
|
-
MintMarketTemplate.prototype.repay = function (debt, address) {
|
|
1872
|
-
if (address === void 0) { address = ""; }
|
|
1873
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1874
|
-
return __generator(this, function (_a) {
|
|
1875
|
-
switch (_a.label) {
|
|
1876
|
-
case 0: return [4 /*yield*/, this.repayApprove(debt)];
|
|
1877
|
-
case 1:
|
|
1878
|
-
_a.sent();
|
|
1879
|
-
return [4 /*yield*/, this._repay(debt, address, false)];
|
|
1880
|
-
case 2: return [2 /*return*/, _a.sent()];
|
|
1881
|
-
}
|
|
1882
|
-
});
|
|
983
|
+
_repayBands(debt, address) {
|
|
984
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
985
|
+
const { _collateral: _currentCollateral, _debt: _currentDebt, _stablecoin: _currentStablecoin } = yield this._userState(address);
|
|
986
|
+
if (_currentDebt === BigInt(0))
|
|
987
|
+
throw Error(`Loan for ${address} does not exist`);
|
|
988
|
+
const N = yield this.userRange(address);
|
|
989
|
+
const _debt = _currentDebt - parseUnits(debt);
|
|
990
|
+
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];
|
|
991
|
+
const _n2 = _n1 + BigInt(N - 1);
|
|
992
|
+
return [_n2, _n1];
|
|
993
|
+
});
|
|
994
|
+
}
|
|
995
|
+
repayBands(debt_1) {
|
|
996
|
+
return __awaiter(this, arguments, void 0, function* (debt, address = "") {
|
|
997
|
+
const [_n2, _n1] = yield this._repayBands(debt, address);
|
|
998
|
+
return [Number(_n2), Number(_n1)];
|
|
1883
999
|
});
|
|
1884
|
-
}
|
|
1000
|
+
}
|
|
1001
|
+
repayPrices(debt_1) {
|
|
1002
|
+
return __awaiter(this, arguments, void 0, function* (debt, address = "") {
|
|
1003
|
+
const [_n2, _n1] = yield this._repayBands(debt, address);
|
|
1004
|
+
return yield this._getPrices(_n2, _n1);
|
|
1005
|
+
});
|
|
1006
|
+
}
|
|
1007
|
+
repayIsApproved(debt) {
|
|
1008
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1009
|
+
return yield hasAllowance([llamalend.address], [debt], llamalend.signerAddress, this.controller);
|
|
1010
|
+
});
|
|
1011
|
+
}
|
|
1012
|
+
repayApproveEstimateGas(debt) {
|
|
1013
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1014
|
+
return yield ensureAllowanceEstimateGas([llamalend.address], [debt], this.controller);
|
|
1015
|
+
});
|
|
1016
|
+
}
|
|
1017
|
+
repayApprove(debt) {
|
|
1018
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1019
|
+
return yield ensureAllowance([llamalend.address], [debt], this.controller);
|
|
1020
|
+
});
|
|
1021
|
+
}
|
|
1022
|
+
repayHealth(debt_1) {
|
|
1023
|
+
return __awaiter(this, arguments, void 0, function* (debt, full = true, address = "") {
|
|
1024
|
+
var _a;
|
|
1025
|
+
address = _getAddress(address);
|
|
1026
|
+
const _debt = parseUnits(debt) * BigInt(-1);
|
|
1027
|
+
const contract = llamalend.contracts[(_a = this.healthCalculator) !== null && _a !== void 0 ? _a : this.controller].contract;
|
|
1028
|
+
let _health = yield contract.health_calculator(address, 0, _debt, full, 0, llamalend.constantOptions);
|
|
1029
|
+
_health = _health * BigInt(100);
|
|
1030
|
+
return formatUnits(_health);
|
|
1031
|
+
});
|
|
1032
|
+
}
|
|
1033
|
+
_repay(debt, address, estimateGas) {
|
|
1034
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1035
|
+
address = _getAddress(address);
|
|
1036
|
+
const { debt: currentDebt } = yield this.userState(address);
|
|
1037
|
+
if (Number(currentDebt) === 0)
|
|
1038
|
+
throw Error(`Loan for ${address} does not exist`);
|
|
1039
|
+
const _debt = parseUnits(debt);
|
|
1040
|
+
const contract = llamalend.contracts[this.controller].contract;
|
|
1041
|
+
const [, n1] = yield this.userBands(address);
|
|
1042
|
+
const { stablecoin } = yield this.userState(address);
|
|
1043
|
+
const n = (BN(stablecoin).gt(0)) ? MAX_ACTIVE_BAND : n1 - 1; // In liquidation mode it doesn't matter if active band moves
|
|
1044
|
+
const gas = yield contract.repay.estimateGas(_debt, address, n, isEth(this.collateral), llamalend.constantOptions);
|
|
1045
|
+
if (estimateGas)
|
|
1046
|
+
return smartNumber(gas);
|
|
1047
|
+
yield llamalend.updateFeeData();
|
|
1048
|
+
const gasLimit = _mulBy1_3(DIGas(gas));
|
|
1049
|
+
return (yield contract.repay(_debt, address, n, isEth(this.collateral), Object.assign(Object.assign({}, llamalend.options), { gasLimit }))).hash;
|
|
1050
|
+
});
|
|
1051
|
+
}
|
|
1052
|
+
repayEstimateGas(debt_1) {
|
|
1053
|
+
return __awaiter(this, arguments, void 0, function* (debt, address = "") {
|
|
1054
|
+
if (!(yield this.repayIsApproved(debt)))
|
|
1055
|
+
throw Error("Approval is needed for gas estimation");
|
|
1056
|
+
return yield this._repay(debt, address, true);
|
|
1057
|
+
});
|
|
1058
|
+
}
|
|
1059
|
+
repay(debt_1) {
|
|
1060
|
+
return __awaiter(this, arguments, void 0, function* (debt, address = "") {
|
|
1061
|
+
yield this.repayApprove(debt);
|
|
1062
|
+
return yield this._repay(debt, address, false);
|
|
1063
|
+
});
|
|
1064
|
+
}
|
|
1885
1065
|
// ---------------- FULL REPAY ----------------
|
|
1886
|
-
|
|
1887
|
-
|
|
1888
|
-
|
|
1889
|
-
|
|
1890
|
-
return
|
|
1891
|
-
|
|
1892
|
-
|
|
1893
|
-
|
|
1894
|
-
|
|
1895
|
-
|
|
1896
|
-
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
|
|
1906
|
-
|
|
1907
|
-
|
|
1908
|
-
|
|
1909
|
-
|
|
1910
|
-
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
fullRepayAmount = _a.sent();
|
|
1930
|
-
return [4 /*yield*/, this.repayApproveEstimateGas(fullRepayAmount)];
|
|
1931
|
-
case 2: return [2 /*return*/, _a.sent()];
|
|
1932
|
-
}
|
|
1933
|
-
});
|
|
1934
|
-
});
|
|
1935
|
-
};
|
|
1936
|
-
MintMarketTemplate.prototype.fullRepayApprove = function (address) {
|
|
1937
|
-
if (address === void 0) { address = ""; }
|
|
1938
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1939
|
-
var fullRepayAmount;
|
|
1940
|
-
return __generator(this, function (_a) {
|
|
1941
|
-
switch (_a.label) {
|
|
1942
|
-
case 0:
|
|
1943
|
-
address = _getAddress(address);
|
|
1944
|
-
return [4 /*yield*/, this._fullRepayAmount(address)];
|
|
1945
|
-
case 1:
|
|
1946
|
-
fullRepayAmount = _a.sent();
|
|
1947
|
-
return [4 /*yield*/, this.repayApprove(fullRepayAmount)];
|
|
1948
|
-
case 2: return [2 /*return*/, _a.sent()];
|
|
1949
|
-
}
|
|
1950
|
-
});
|
|
1951
|
-
});
|
|
1952
|
-
};
|
|
1953
|
-
MintMarketTemplate.prototype.fullRepayEstimateGas = function (address) {
|
|
1954
|
-
if (address === void 0) { address = ""; }
|
|
1955
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1956
|
-
var fullRepayAmount;
|
|
1957
|
-
return __generator(this, function (_a) {
|
|
1958
|
-
switch (_a.label) {
|
|
1959
|
-
case 0:
|
|
1960
|
-
address = _getAddress(address);
|
|
1961
|
-
return [4 /*yield*/, this._fullRepayAmount(address)];
|
|
1962
|
-
case 1:
|
|
1963
|
-
fullRepayAmount = _a.sent();
|
|
1964
|
-
return [4 /*yield*/, this.repayIsApproved(fullRepayAmount)];
|
|
1965
|
-
case 2:
|
|
1966
|
-
if (!(_a.sent()))
|
|
1967
|
-
throw Error("Approval is needed for gas estimation");
|
|
1968
|
-
return [4 /*yield*/, this._repay(fullRepayAmount, address, true)];
|
|
1969
|
-
case 3: return [2 /*return*/, _a.sent()];
|
|
1970
|
-
}
|
|
1971
|
-
});
|
|
1972
|
-
});
|
|
1973
|
-
};
|
|
1974
|
-
MintMarketTemplate.prototype.fullRepay = function (address) {
|
|
1975
|
-
if (address === void 0) { address = ""; }
|
|
1976
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1977
|
-
var fullRepayAmount;
|
|
1978
|
-
return __generator(this, function (_a) {
|
|
1979
|
-
switch (_a.label) {
|
|
1980
|
-
case 0:
|
|
1981
|
-
address = _getAddress(address);
|
|
1982
|
-
return [4 /*yield*/, this._fullRepayAmount(address)];
|
|
1983
|
-
case 1:
|
|
1984
|
-
fullRepayAmount = _a.sent();
|
|
1985
|
-
return [4 /*yield*/, this.repayApprove(fullRepayAmount)];
|
|
1986
|
-
case 2:
|
|
1987
|
-
_a.sent();
|
|
1988
|
-
return [4 /*yield*/, this._repay(fullRepayAmount, address, false)];
|
|
1989
|
-
case 3: return [2 /*return*/, _a.sent()];
|
|
1990
|
-
}
|
|
1991
|
-
});
|
|
1066
|
+
_fullRepayAmount() {
|
|
1067
|
+
return __awaiter(this, arguments, void 0, function* (address = "") {
|
|
1068
|
+
address = _getAddress(address);
|
|
1069
|
+
const debt = yield this.userDebt(address);
|
|
1070
|
+
return BN(debt).times(1.0001).toString();
|
|
1071
|
+
});
|
|
1072
|
+
}
|
|
1073
|
+
fullRepayIsApproved() {
|
|
1074
|
+
return __awaiter(this, arguments, void 0, function* (address = "") {
|
|
1075
|
+
address = _getAddress(address);
|
|
1076
|
+
const fullRepayAmount = yield this._fullRepayAmount(address);
|
|
1077
|
+
return yield this.repayIsApproved(fullRepayAmount);
|
|
1078
|
+
});
|
|
1079
|
+
}
|
|
1080
|
+
fullRepayApproveEstimateGas() {
|
|
1081
|
+
return __awaiter(this, arguments, void 0, function* (address = "") {
|
|
1082
|
+
address = _getAddress(address);
|
|
1083
|
+
const fullRepayAmount = yield this._fullRepayAmount(address);
|
|
1084
|
+
return yield this.repayApproveEstimateGas(fullRepayAmount);
|
|
1085
|
+
});
|
|
1086
|
+
}
|
|
1087
|
+
fullRepayApprove() {
|
|
1088
|
+
return __awaiter(this, arguments, void 0, function* (address = "") {
|
|
1089
|
+
address = _getAddress(address);
|
|
1090
|
+
const fullRepayAmount = yield this._fullRepayAmount(address);
|
|
1091
|
+
return yield this.repayApprove(fullRepayAmount);
|
|
1092
|
+
});
|
|
1093
|
+
}
|
|
1094
|
+
fullRepayEstimateGas() {
|
|
1095
|
+
return __awaiter(this, arguments, void 0, function* (address = "") {
|
|
1096
|
+
address = _getAddress(address);
|
|
1097
|
+
const fullRepayAmount = yield this._fullRepayAmount(address);
|
|
1098
|
+
if (!(yield this.repayIsApproved(fullRepayAmount)))
|
|
1099
|
+
throw Error("Approval is needed for gas estimation");
|
|
1100
|
+
return yield this._repay(fullRepayAmount, address, true);
|
|
1101
|
+
});
|
|
1102
|
+
}
|
|
1103
|
+
fullRepay() {
|
|
1104
|
+
return __awaiter(this, arguments, void 0, function* (address = "") {
|
|
1105
|
+
address = _getAddress(address);
|
|
1106
|
+
const fullRepayAmount = yield this._fullRepayAmount(address);
|
|
1107
|
+
yield this.repayApprove(fullRepayAmount);
|
|
1108
|
+
return yield this._repay(fullRepayAmount, address, false);
|
|
1992
1109
|
});
|
|
1993
|
-
}
|
|
1110
|
+
}
|
|
1994
1111
|
// ---------------- SWAP ----------------
|
|
1995
|
-
|
|
1996
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
1997
|
-
|
|
1998
|
-
|
|
1999
|
-
|
|
2000
|
-
|
|
2001
|
-
|
|
2002
|
-
|
|
2003
|
-
|
|
2004
|
-
|
|
2005
|
-
|
|
2006
|
-
|
|
2007
|
-
|
|
2008
|
-
|
|
2009
|
-
|
|
2010
|
-
return [2 /*return*/, formatUnits(_inAmount, inDecimals)];
|
|
2011
|
-
}
|
|
2012
|
-
});
|
|
2013
|
-
});
|
|
2014
|
-
};
|
|
2015
|
-
MintMarketTemplate.prototype._swapExpected = function (i, j, _amount) {
|
|
2016
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2017
|
-
return __generator(this, function (_a) {
|
|
2018
|
-
switch (_a.label) {
|
|
2019
|
-
case 0: return [4 /*yield*/, llamalend.contracts[this.address].contract.get_dy(i, j, _amount, llamalend.constantOptions)];
|
|
2020
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
2021
|
-
}
|
|
2022
|
-
});
|
|
2023
|
-
});
|
|
2024
|
-
};
|
|
2025
|
-
MintMarketTemplate.prototype.swapExpected = function (i, j, amount) {
|
|
2026
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2027
|
-
var _a, inDecimals, outDecimals, _amount, _expected;
|
|
2028
|
-
return __generator(this, function (_c) {
|
|
2029
|
-
switch (_c.label) {
|
|
2030
|
-
case 0:
|
|
2031
|
-
if (!(i === 0 && j === 1) && !(i === 1 && j === 0))
|
|
2032
|
-
throw Error("Wrong index");
|
|
2033
|
-
_a = this.coinDecimals, inDecimals = _a[0], outDecimals = _a[1];
|
|
2034
|
-
_amount = parseUnits(amount, inDecimals);
|
|
2035
|
-
return [4 /*yield*/, this._swapExpected(i, j, _amount)];
|
|
2036
|
-
case 1:
|
|
2037
|
-
_expected = _c.sent();
|
|
2038
|
-
return [2 /*return*/, formatUnits(_expected, outDecimals)];
|
|
2039
|
-
}
|
|
2040
|
-
});
|
|
2041
|
-
});
|
|
2042
|
-
};
|
|
2043
|
-
MintMarketTemplate.prototype.swapRequired = function (i, j, outAmount) {
|
|
2044
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2045
|
-
var _a, inDecimals, outDecimals, _amount, _expected;
|
|
2046
|
-
return __generator(this, function (_c) {
|
|
2047
|
-
switch (_c.label) {
|
|
2048
|
-
case 0:
|
|
2049
|
-
if (!(i === 0 && j === 1) && !(i === 1 && j === 0))
|
|
2050
|
-
throw Error("Wrong index");
|
|
2051
|
-
_a = this.coinDecimals, inDecimals = _a[0], outDecimals = _a[1];
|
|
2052
|
-
_amount = parseUnits(outAmount, outDecimals);
|
|
2053
|
-
return [4 /*yield*/, llamalend.contracts[this.address].contract.get_dx(i, j, _amount, llamalend.constantOptions)];
|
|
2054
|
-
case 1:
|
|
2055
|
-
_expected = _c.sent();
|
|
2056
|
-
return [2 /*return*/, formatUnits(_expected, inDecimals)];
|
|
2057
|
-
}
|
|
2058
|
-
});
|
|
2059
|
-
});
|
|
2060
|
-
};
|
|
2061
|
-
MintMarketTemplate.prototype.swapPriceImpact = function (i, j, amount) {
|
|
2062
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2063
|
-
var _a, inDecimals, outDecimals, _amount, _output, target, amountIntBN, outputIntBN, k, smallAmountIntBN, _smallAmount, _smallOutput, amountBN, outputBN, smallAmountBN, smallOutputBN, rateBN, smallRateBN, slippageBN;
|
|
2064
|
-
return __generator(this, function (_c) {
|
|
2065
|
-
switch (_c.label) {
|
|
2066
|
-
case 0:
|
|
2067
|
-
if (!(i === 0 && j === 1) && !(i === 1 && j === 0))
|
|
2068
|
-
throw Error("Wrong index");
|
|
2069
|
-
_a = this.coinDecimals, inDecimals = _a[0], outDecimals = _a[1];
|
|
2070
|
-
_amount = parseUnits(amount, inDecimals);
|
|
2071
|
-
return [4 /*yield*/, this._swapExpected(i, j, _amount)];
|
|
2072
|
-
case 1:
|
|
2073
|
-
_output = _c.sent();
|
|
2074
|
-
target = BN(Math.pow(10, 15));
|
|
2075
|
-
amountIntBN = BN(amount).times(Math.pow(10, inDecimals));
|
|
2076
|
-
outputIntBN = toBN(_output, 0);
|
|
2077
|
-
k = BigNumber.min(BigNumber.max(target.div(amountIntBN), target.div(outputIntBN)), 0.2);
|
|
2078
|
-
smallAmountIntBN = BigNumber.min(amountIntBN.times(k), BN(Math.pow(10, inDecimals)));
|
|
2079
|
-
if (smallAmountIntBN.toFixed(0) === '0')
|
|
2080
|
-
return [2 /*return*/, '0'];
|
|
2081
|
-
_smallAmount = fromBN(smallAmountIntBN.div(Math.pow(10, inDecimals)), inDecimals);
|
|
2082
|
-
return [4 /*yield*/, this._swapExpected(i, j, _smallAmount)];
|
|
2083
|
-
case 2:
|
|
2084
|
-
_smallOutput = _c.sent();
|
|
2085
|
-
amountBN = BN(amount);
|
|
2086
|
-
outputBN = toBN(_output, outDecimals);
|
|
2087
|
-
smallAmountBN = toBN(_smallAmount, inDecimals);
|
|
2088
|
-
smallOutputBN = toBN(_smallOutput, outDecimals);
|
|
2089
|
-
rateBN = outputBN.div(amountBN);
|
|
2090
|
-
smallRateBN = smallOutputBN.div(smallAmountBN);
|
|
2091
|
-
if (rateBN.gt(smallRateBN))
|
|
2092
|
-
return [2 /*return*/, "0"];
|
|
2093
|
-
slippageBN = BN(1).minus(rateBN.div(smallRateBN)).times(100);
|
|
2094
|
-
return [2 /*return*/, _cutZeros(slippageBN.toFixed(6))];
|
|
2095
|
-
}
|
|
2096
|
-
});
|
|
2097
|
-
});
|
|
2098
|
-
};
|
|
2099
|
-
MintMarketTemplate.prototype.swapIsApproved = function (i, amount) {
|
|
2100
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2101
|
-
return __generator(this, function (_a) {
|
|
2102
|
-
switch (_a.label) {
|
|
2103
|
-
case 0:
|
|
2104
|
-
if (i !== 0 && i !== 1)
|
|
2105
|
-
throw Error("Wrong index");
|
|
2106
|
-
return [4 /*yield*/, hasAllowance([this.coinAddresses[i]], [amount], llamalend.signerAddress, this.address)];
|
|
2107
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
2108
|
-
}
|
|
2109
|
-
});
|
|
2110
|
-
});
|
|
2111
|
-
};
|
|
2112
|
-
MintMarketTemplate.prototype.swapApproveEstimateGas = function (i, amount) {
|
|
2113
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2114
|
-
return __generator(this, function (_a) {
|
|
2115
|
-
switch (_a.label) {
|
|
2116
|
-
case 0:
|
|
2117
|
-
if (i !== 0 && i !== 1)
|
|
2118
|
-
throw Error("Wrong index");
|
|
2119
|
-
return [4 /*yield*/, ensureAllowanceEstimateGas([this.coinAddresses[i]], [amount], this.address)];
|
|
2120
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
2121
|
-
}
|
|
2122
|
-
});
|
|
2123
|
-
});
|
|
2124
|
-
};
|
|
2125
|
-
MintMarketTemplate.prototype.swapApprove = function (i, amount) {
|
|
2126
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2127
|
-
return __generator(this, function (_a) {
|
|
2128
|
-
switch (_a.label) {
|
|
2129
|
-
case 0:
|
|
2130
|
-
if (i !== 0 && i !== 1)
|
|
2131
|
-
throw Error("Wrong index");
|
|
2132
|
-
return [4 /*yield*/, ensureAllowance([this.coinAddresses[i]], [amount], this.address)];
|
|
2133
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
2134
|
-
}
|
|
2135
|
-
});
|
|
2136
|
-
});
|
|
2137
|
-
};
|
|
2138
|
-
MintMarketTemplate.prototype._swap = function (i, j, amount, slippage, estimateGas) {
|
|
2139
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2140
|
-
var _a, inDecimals, outDecimals, _amount, _expected, minRecvAmountBN, _minRecvAmount, contract, gas, gasLimit;
|
|
2141
|
-
return __generator(this, function (_c) {
|
|
2142
|
-
switch (_c.label) {
|
|
2143
|
-
case 0:
|
|
2144
|
-
if (!(i === 0 && j === 1) && !(i === 1 && j === 0))
|
|
2145
|
-
throw Error("Wrong index");
|
|
2146
|
-
_a = [this.coinDecimals[i], this.coinDecimals[j]], inDecimals = _a[0], outDecimals = _a[1];
|
|
2147
|
-
_amount = parseUnits(amount, inDecimals);
|
|
2148
|
-
return [4 /*yield*/, this._swapExpected(i, j, _amount)];
|
|
2149
|
-
case 1:
|
|
2150
|
-
_expected = _c.sent();
|
|
2151
|
-
minRecvAmountBN = toBN(_expected, outDecimals).times(100 - slippage).div(100);
|
|
2152
|
-
_minRecvAmount = fromBN(minRecvAmountBN, outDecimals);
|
|
2153
|
-
contract = llamalend.contracts[this.address].contract;
|
|
2154
|
-
return [4 /*yield*/, contract.exchange.estimateGas(i, j, _amount, _minRecvAmount, llamalend.constantOptions)];
|
|
2155
|
-
case 2:
|
|
2156
|
-
gas = _c.sent();
|
|
2157
|
-
if (estimateGas)
|
|
2158
|
-
return [2 /*return*/, smartNumber(gas)];
|
|
2159
|
-
return [4 /*yield*/, llamalend.updateFeeData()];
|
|
2160
|
-
case 3:
|
|
2161
|
-
_c.sent();
|
|
2162
|
-
gasLimit = _mulBy1_3(DIGas(gas));
|
|
2163
|
-
return [4 /*yield*/, contract.exchange(i, j, _amount, _minRecvAmount, __assign(__assign({}, llamalend.options), { gasLimit: gasLimit }))];
|
|
2164
|
-
case 4: return [2 /*return*/, (_c.sent()).hash];
|
|
2165
|
-
}
|
|
2166
|
-
});
|
|
2167
|
-
});
|
|
2168
|
-
};
|
|
2169
|
-
MintMarketTemplate.prototype.swapEstimateGas = function (i, j, amount, slippage) {
|
|
2170
|
-
if (slippage === void 0) { slippage = 0.1; }
|
|
2171
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2172
|
-
return __generator(this, function (_a) {
|
|
2173
|
-
switch (_a.label) {
|
|
2174
|
-
case 0: return [4 /*yield*/, this.swapIsApproved(i, amount)];
|
|
2175
|
-
case 1:
|
|
2176
|
-
if (!(_a.sent()))
|
|
2177
|
-
throw Error("Approval is needed for gas estimation");
|
|
2178
|
-
return [4 /*yield*/, this._swap(i, j, amount, slippage, true)];
|
|
2179
|
-
case 2: return [2 /*return*/, _a.sent()];
|
|
2180
|
-
}
|
|
2181
|
-
});
|
|
2182
|
-
});
|
|
2183
|
-
};
|
|
2184
|
-
MintMarketTemplate.prototype.swap = function (i, j, amount, slippage) {
|
|
2185
|
-
if (slippage === void 0) { slippage = 0.1; }
|
|
2186
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2187
|
-
return __generator(this, function (_a) {
|
|
2188
|
-
switch (_a.label) {
|
|
2189
|
-
case 0: return [4 /*yield*/, this.swapApprove(i, amount)];
|
|
2190
|
-
case 1:
|
|
2191
|
-
_a.sent();
|
|
2192
|
-
return [4 /*yield*/, this._swap(i, j, amount, slippage, false)];
|
|
2193
|
-
case 2: return [2 /*return*/, _a.sent()];
|
|
2194
|
-
}
|
|
2195
|
-
});
|
|
1112
|
+
maxSwappable(i, j) {
|
|
1113
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1114
|
+
if (!(i === 0 && j === 1) && !(i === 1 && j === 0))
|
|
1115
|
+
throw Error("Wrong index");
|
|
1116
|
+
const inDecimals = this.coinDecimals[i];
|
|
1117
|
+
const contract = llamalend.contracts[this.address].contract;
|
|
1118
|
+
const [_inAmount, _outAmount] = yield contract.get_dxdy(i, j, MAX_ALLOWANCE, llamalend.constantOptions);
|
|
1119
|
+
if (_outAmount === BigInt(0))
|
|
1120
|
+
return "0";
|
|
1121
|
+
return formatUnits(_inAmount, inDecimals);
|
|
1122
|
+
});
|
|
1123
|
+
}
|
|
1124
|
+
_swapExpected(i, j, _amount) {
|
|
1125
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1126
|
+
return yield llamalend.contracts[this.address].contract.get_dy(i, j, _amount, llamalend.constantOptions);
|
|
2196
1127
|
});
|
|
2197
|
-
}
|
|
1128
|
+
}
|
|
1129
|
+
swapExpected(i, j, amount) {
|
|
1130
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1131
|
+
if (!(i === 0 && j === 1) && !(i === 1 && j === 0))
|
|
1132
|
+
throw Error("Wrong index");
|
|
1133
|
+
const [inDecimals, outDecimals] = this.coinDecimals;
|
|
1134
|
+
const _amount = parseUnits(amount, inDecimals);
|
|
1135
|
+
const _expected = yield this._swapExpected(i, j, _amount);
|
|
1136
|
+
return formatUnits(_expected, outDecimals);
|
|
1137
|
+
});
|
|
1138
|
+
}
|
|
1139
|
+
swapRequired(i, j, outAmount) {
|
|
1140
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1141
|
+
if (!(i === 0 && j === 1) && !(i === 1 && j === 0))
|
|
1142
|
+
throw Error("Wrong index");
|
|
1143
|
+
const [inDecimals, outDecimals] = this.coinDecimals;
|
|
1144
|
+
const _amount = parseUnits(outAmount, outDecimals);
|
|
1145
|
+
const _expected = yield llamalend.contracts[this.address].contract.get_dx(i, j, _amount, llamalend.constantOptions);
|
|
1146
|
+
return formatUnits(_expected, inDecimals);
|
|
1147
|
+
});
|
|
1148
|
+
}
|
|
1149
|
+
swapPriceImpact(i, j, amount) {
|
|
1150
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1151
|
+
if (!(i === 0 && j === 1) && !(i === 1 && j === 0))
|
|
1152
|
+
throw Error("Wrong index");
|
|
1153
|
+
const [inDecimals, outDecimals] = this.coinDecimals;
|
|
1154
|
+
const _amount = parseUnits(amount, inDecimals);
|
|
1155
|
+
const _output = yield this._swapExpected(i, j, _amount);
|
|
1156
|
+
// Find k for which x * k = 10^15 or y * k = 10^15: k = max(10^15 / x, 10^15 / y)
|
|
1157
|
+
// For coins with d (decimals) <= 15: k = min(k, 0.2), and x0 = min(x * k, 10^d)
|
|
1158
|
+
// x0 = min(x * min(max(10^15 / x, 10^15 / y), 0.2), 10^d), if x0 == 0 then priceImpact = 0
|
|
1159
|
+
const target = BN(Math.pow(10, 15));
|
|
1160
|
+
const amountIntBN = BN(amount).times(Math.pow(10, inDecimals));
|
|
1161
|
+
const outputIntBN = toBN(_output, 0);
|
|
1162
|
+
const k = BigNumber.min(BigNumber.max(target.div(amountIntBN), target.div(outputIntBN)), 0.2);
|
|
1163
|
+
const smallAmountIntBN = BigNumber.min(amountIntBN.times(k), BN(Math.pow(10, inDecimals)));
|
|
1164
|
+
if (smallAmountIntBN.toFixed(0) === '0')
|
|
1165
|
+
return '0';
|
|
1166
|
+
const _smallAmount = fromBN(smallAmountIntBN.div(Math.pow(10, inDecimals)), inDecimals);
|
|
1167
|
+
const _smallOutput = yield this._swapExpected(i, j, _smallAmount);
|
|
1168
|
+
const amountBN = BN(amount);
|
|
1169
|
+
const outputBN = toBN(_output, outDecimals);
|
|
1170
|
+
const smallAmountBN = toBN(_smallAmount, inDecimals);
|
|
1171
|
+
const smallOutputBN = toBN(_smallOutput, outDecimals);
|
|
1172
|
+
const rateBN = outputBN.div(amountBN);
|
|
1173
|
+
const smallRateBN = smallOutputBN.div(smallAmountBN);
|
|
1174
|
+
if (rateBN.gt(smallRateBN))
|
|
1175
|
+
return "0";
|
|
1176
|
+
const slippageBN = BN(1).minus(rateBN.div(smallRateBN)).times(100);
|
|
1177
|
+
return _cutZeros(slippageBN.toFixed(6));
|
|
1178
|
+
});
|
|
1179
|
+
}
|
|
1180
|
+
swapIsApproved(i, amount) {
|
|
1181
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1182
|
+
if (i !== 0 && i !== 1)
|
|
1183
|
+
throw Error("Wrong index");
|
|
1184
|
+
return yield hasAllowance([this.coinAddresses[i]], [amount], llamalend.signerAddress, this.address);
|
|
1185
|
+
});
|
|
1186
|
+
}
|
|
1187
|
+
swapApproveEstimateGas(i, amount) {
|
|
1188
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1189
|
+
if (i !== 0 && i !== 1)
|
|
1190
|
+
throw Error("Wrong index");
|
|
1191
|
+
return yield ensureAllowanceEstimateGas([this.coinAddresses[i]], [amount], this.address);
|
|
1192
|
+
});
|
|
1193
|
+
}
|
|
1194
|
+
swapApprove(i, amount) {
|
|
1195
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1196
|
+
if (i !== 0 && i !== 1)
|
|
1197
|
+
throw Error("Wrong index");
|
|
1198
|
+
return yield ensureAllowance([this.coinAddresses[i]], [amount], this.address);
|
|
1199
|
+
});
|
|
1200
|
+
}
|
|
1201
|
+
_swap(i, j, amount, slippage, estimateGas) {
|
|
1202
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1203
|
+
if (!(i === 0 && j === 1) && !(i === 1 && j === 0))
|
|
1204
|
+
throw Error("Wrong index");
|
|
1205
|
+
const [inDecimals, outDecimals] = [this.coinDecimals[i], this.coinDecimals[j]];
|
|
1206
|
+
const _amount = parseUnits(amount, inDecimals);
|
|
1207
|
+
const _expected = yield this._swapExpected(i, j, _amount);
|
|
1208
|
+
const minRecvAmountBN = toBN(_expected, outDecimals).times(100 - slippage).div(100);
|
|
1209
|
+
const _minRecvAmount = fromBN(minRecvAmountBN, outDecimals);
|
|
1210
|
+
const contract = llamalend.contracts[this.address].contract;
|
|
1211
|
+
const gas = yield contract.exchange.estimateGas(i, j, _amount, _minRecvAmount, llamalend.constantOptions);
|
|
1212
|
+
if (estimateGas)
|
|
1213
|
+
return smartNumber(gas);
|
|
1214
|
+
yield llamalend.updateFeeData();
|
|
1215
|
+
const gasLimit = _mulBy1_3(DIGas(gas));
|
|
1216
|
+
return (yield contract.exchange(i, j, _amount, _minRecvAmount, Object.assign(Object.assign({}, llamalend.options), { gasLimit }))).hash;
|
|
1217
|
+
});
|
|
1218
|
+
}
|
|
1219
|
+
swapEstimateGas(i_1, j_1, amount_1) {
|
|
1220
|
+
return __awaiter(this, arguments, void 0, function* (i, j, amount, slippage = 0.1) {
|
|
1221
|
+
if (!(yield this.swapIsApproved(i, amount)))
|
|
1222
|
+
throw Error("Approval is needed for gas estimation");
|
|
1223
|
+
return yield this._swap(i, j, amount, slippage, true);
|
|
1224
|
+
});
|
|
1225
|
+
}
|
|
1226
|
+
swap(i_1, j_1, amount_1) {
|
|
1227
|
+
return __awaiter(this, arguments, void 0, function* (i, j, amount, slippage = 0.1) {
|
|
1228
|
+
yield this.swapApprove(i, amount);
|
|
1229
|
+
return yield this._swap(i, j, amount, slippage, false);
|
|
1230
|
+
});
|
|
1231
|
+
}
|
|
2198
1232
|
// ---------------- LIQUIDATE ----------------
|
|
2199
|
-
|
|
2200
|
-
|
|
2201
|
-
|
|
2202
|
-
|
|
2203
|
-
return
|
|
2204
|
-
|
|
2205
|
-
|
|
2206
|
-
|
|
2207
|
-
|
|
2208
|
-
|
|
2209
|
-
|
|
2210
|
-
|
|
2211
|
-
|
|
2212
|
-
|
|
2213
|
-
|
|
2214
|
-
|
|
2215
|
-
|
|
2216
|
-
|
|
2217
|
-
|
|
2218
|
-
|
|
2219
|
-
|
|
2220
|
-
|
|
2221
|
-
|
|
2222
|
-
|
|
2223
|
-
|
|
2224
|
-
|
|
2225
|
-
|
|
2226
|
-
|
|
2227
|
-
|
|
2228
|
-
|
|
2229
|
-
|
|
2230
|
-
|
|
2231
|
-
|
|
2232
|
-
|
|
2233
|
-
|
|
2234
|
-
|
|
2235
|
-
|
|
2236
|
-
|
|
2237
|
-
|
|
2238
|
-
|
|
2239
|
-
|
|
2240
|
-
|
|
2241
|
-
|
|
2242
|
-
|
|
2243
|
-
|
|
2244
|
-
};
|
|
2245
|
-
MintMarketTemplate.prototype.liquidateApprove = function (address) {
|
|
2246
|
-
if (address === void 0) { address = ""; }
|
|
2247
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2248
|
-
var tokensToLiquidate;
|
|
2249
|
-
return __generator(this, function (_a) {
|
|
2250
|
-
switch (_a.label) {
|
|
2251
|
-
case 0: return [4 /*yield*/, this.tokensToLiquidate(address)];
|
|
2252
|
-
case 1:
|
|
2253
|
-
tokensToLiquidate = _a.sent();
|
|
2254
|
-
return [4 /*yield*/, ensureAllowance([llamalend.address], [tokensToLiquidate], this.controller)];
|
|
2255
|
-
case 2: return [2 /*return*/, _a.sent()];
|
|
2256
|
-
}
|
|
2257
|
-
});
|
|
2258
|
-
});
|
|
2259
|
-
};
|
|
2260
|
-
MintMarketTemplate.prototype._liquidate = function (address, slippage, estimateGas) {
|
|
2261
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2262
|
-
var _a, stablecoin, currentDebt, minAmountBN, _minAmount, contract, gas, gasLimit;
|
|
2263
|
-
return __generator(this, function (_c) {
|
|
2264
|
-
switch (_c.label) {
|
|
2265
|
-
case 0: return [4 /*yield*/, this.userState(address)];
|
|
2266
|
-
case 1:
|
|
2267
|
-
_a = _c.sent(), stablecoin = _a.stablecoin, currentDebt = _a.debt;
|
|
2268
|
-
if (slippage <= 0)
|
|
2269
|
-
throw Error("Slippage must be > 0");
|
|
2270
|
-
if (slippage > 100)
|
|
2271
|
-
throw Error("Slippage must be <= 100");
|
|
2272
|
-
if (Number(currentDebt) === 0)
|
|
2273
|
-
throw Error("Loan for ".concat(address, " does not exist"));
|
|
2274
|
-
if (Number(stablecoin) === 0)
|
|
2275
|
-
throw Error("User ".concat(address, " is not in liquidation mode"));
|
|
2276
|
-
minAmountBN = BN(stablecoin).times(100 - slippage).div(100);
|
|
2277
|
-
_minAmount = fromBN(minAmountBN);
|
|
2278
|
-
contract = llamalend.contracts[this.controller].contract;
|
|
2279
|
-
return [4 /*yield*/, contract.liquidate.estimateGas(address, _minAmount, isEth(this.collateral), llamalend.constantOptions)];
|
|
2280
|
-
case 2:
|
|
2281
|
-
gas = (_c.sent());
|
|
2282
|
-
if (estimateGas)
|
|
2283
|
-
return [2 /*return*/, smartNumber(gas)];
|
|
2284
|
-
return [4 /*yield*/, llamalend.updateFeeData()];
|
|
2285
|
-
case 3:
|
|
2286
|
-
_c.sent();
|
|
2287
|
-
gasLimit = _mulBy1_3(DIGas(gas));
|
|
2288
|
-
return [4 /*yield*/, contract.liquidate(address, _minAmount, isEth(this.collateral), __assign(__assign({}, llamalend.options), { gasLimit: gasLimit }))];
|
|
2289
|
-
case 4: return [2 /*return*/, (_c.sent()).hash];
|
|
2290
|
-
}
|
|
2291
|
-
});
|
|
2292
|
-
});
|
|
2293
|
-
};
|
|
2294
|
-
MintMarketTemplate.prototype.liquidateEstimateGas = function (address, slippage) {
|
|
2295
|
-
if (slippage === void 0) { slippage = 0.1; }
|
|
2296
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2297
|
-
return __generator(this, function (_a) {
|
|
2298
|
-
switch (_a.label) {
|
|
2299
|
-
case 0: return [4 /*yield*/, this.liquidateIsApproved(address)];
|
|
2300
|
-
case 1:
|
|
2301
|
-
if (!(_a.sent()))
|
|
2302
|
-
throw Error("Approval is needed for gas estimation");
|
|
2303
|
-
return [4 /*yield*/, this._liquidate(address, slippage, true)];
|
|
2304
|
-
case 2: return [2 /*return*/, _a.sent()];
|
|
2305
|
-
}
|
|
2306
|
-
});
|
|
2307
|
-
});
|
|
2308
|
-
};
|
|
2309
|
-
MintMarketTemplate.prototype.liquidate = function (address, slippage) {
|
|
2310
|
-
if (slippage === void 0) { slippage = 0.1; }
|
|
2311
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2312
|
-
return __generator(this, function (_a) {
|
|
2313
|
-
switch (_a.label) {
|
|
2314
|
-
case 0: return [4 /*yield*/, this.liquidateApprove(address)];
|
|
2315
|
-
case 1:
|
|
2316
|
-
_a.sent();
|
|
2317
|
-
return [4 /*yield*/, this._liquidate(address, slippage, false)];
|
|
2318
|
-
case 2: return [2 /*return*/, _a.sent()];
|
|
2319
|
-
}
|
|
2320
|
-
});
|
|
1233
|
+
tokensToLiquidate() {
|
|
1234
|
+
return __awaiter(this, arguments, void 0, function* (address = "") {
|
|
1235
|
+
address = _getAddress(address);
|
|
1236
|
+
const _tokens = yield llamalend.contracts[this.controller].contract.tokens_to_liquidate(address, llamalend.constantOptions);
|
|
1237
|
+
return formatUnits(_tokens);
|
|
1238
|
+
});
|
|
1239
|
+
}
|
|
1240
|
+
liquidateIsApproved() {
|
|
1241
|
+
return __awaiter(this, arguments, void 0, function* (address = "") {
|
|
1242
|
+
const tokensToLiquidate = yield this.tokensToLiquidate(address);
|
|
1243
|
+
return yield hasAllowance([llamalend.address], [tokensToLiquidate], llamalend.signerAddress, this.controller);
|
|
1244
|
+
});
|
|
1245
|
+
}
|
|
1246
|
+
liquidateApproveEstimateGas() {
|
|
1247
|
+
return __awaiter(this, arguments, void 0, function* (address = "") {
|
|
1248
|
+
const tokensToLiquidate = yield this.tokensToLiquidate(address);
|
|
1249
|
+
return yield ensureAllowanceEstimateGas([llamalend.address], [tokensToLiquidate], this.controller);
|
|
1250
|
+
});
|
|
1251
|
+
}
|
|
1252
|
+
liquidateApprove() {
|
|
1253
|
+
return __awaiter(this, arguments, void 0, function* (address = "") {
|
|
1254
|
+
const tokensToLiquidate = yield this.tokensToLiquidate(address);
|
|
1255
|
+
return yield ensureAllowance([llamalend.address], [tokensToLiquidate], this.controller);
|
|
1256
|
+
});
|
|
1257
|
+
}
|
|
1258
|
+
_liquidate(address, slippage, estimateGas) {
|
|
1259
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1260
|
+
const { stablecoin, debt: currentDebt } = yield this.userState(address);
|
|
1261
|
+
if (slippage <= 0)
|
|
1262
|
+
throw Error("Slippage must be > 0");
|
|
1263
|
+
if (slippage > 100)
|
|
1264
|
+
throw Error("Slippage must be <= 100");
|
|
1265
|
+
if (Number(currentDebt) === 0)
|
|
1266
|
+
throw Error(`Loan for ${address} does not exist`);
|
|
1267
|
+
if (Number(stablecoin) === 0)
|
|
1268
|
+
throw Error(`User ${address} is not in liquidation mode`);
|
|
1269
|
+
const minAmountBN = BN(stablecoin).times(100 - slippage).div(100);
|
|
1270
|
+
const _minAmount = fromBN(minAmountBN);
|
|
1271
|
+
const contract = llamalend.contracts[this.controller].contract;
|
|
1272
|
+
const gas = (yield contract.liquidate.estimateGas(address, _minAmount, isEth(this.collateral), llamalend.constantOptions));
|
|
1273
|
+
if (estimateGas)
|
|
1274
|
+
return smartNumber(gas);
|
|
1275
|
+
yield llamalend.updateFeeData();
|
|
1276
|
+
const gasLimit = _mulBy1_3(DIGas(gas));
|
|
1277
|
+
return (yield contract.liquidate(address, _minAmount, isEth(this.collateral), Object.assign(Object.assign({}, llamalend.options), { gasLimit }))).hash;
|
|
2321
1278
|
});
|
|
2322
|
-
}
|
|
1279
|
+
}
|
|
1280
|
+
liquidateEstimateGas(address_1) {
|
|
1281
|
+
return __awaiter(this, arguments, void 0, function* (address, slippage = 0.1) {
|
|
1282
|
+
if (!(yield this.liquidateIsApproved(address)))
|
|
1283
|
+
throw Error("Approval is needed for gas estimation");
|
|
1284
|
+
return yield this._liquidate(address, slippage, true);
|
|
1285
|
+
});
|
|
1286
|
+
}
|
|
1287
|
+
liquidate(address_1) {
|
|
1288
|
+
return __awaiter(this, arguments, void 0, function* (address, slippage = 0.1) {
|
|
1289
|
+
yield this.liquidateApprove(address);
|
|
1290
|
+
return yield this._liquidate(address, slippage, false);
|
|
1291
|
+
});
|
|
1292
|
+
}
|
|
2323
1293
|
// ---------------- SELF-LIQUIDATE ----------------
|
|
2324
|
-
|
|
2325
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2326
|
-
return
|
|
2327
|
-
|
|
2328
|
-
|
|
2329
|
-
|
|
2330
|
-
|
|
2331
|
-
|
|
2332
|
-
});
|
|
2333
|
-
}
|
|
2334
|
-
|
|
2335
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2336
|
-
return
|
|
2337
|
-
|
|
2338
|
-
|
|
2339
|
-
|
|
2340
|
-
|
|
2341
|
-
|
|
2342
|
-
|
|
2343
|
-
return
|
|
2344
|
-
|
|
2345
|
-
|
|
2346
|
-
|
|
2347
|
-
|
|
2348
|
-
|
|
2349
|
-
|
|
2350
|
-
};
|
|
2351
|
-
MintMarketTemplate.prototype.selfLiquidateEstimateGas = function (slippage) {
|
|
2352
|
-
if (slippage === void 0) { slippage = 0.1; }
|
|
2353
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2354
|
-
return __generator(this, function (_a) {
|
|
2355
|
-
switch (_a.label) {
|
|
2356
|
-
case 0: return [4 /*yield*/, this.selfLiquidateIsApproved()];
|
|
2357
|
-
case 1:
|
|
2358
|
-
if (!(_a.sent()))
|
|
2359
|
-
throw Error("Approval is needed for gas estimation");
|
|
2360
|
-
return [4 /*yield*/, this._liquidate(llamalend.signerAddress, slippage, true)];
|
|
2361
|
-
case 2: return [2 /*return*/, _a.sent()];
|
|
2362
|
-
}
|
|
2363
|
-
});
|
|
2364
|
-
});
|
|
2365
|
-
};
|
|
2366
|
-
MintMarketTemplate.prototype.selfLiquidate = function (slippage) {
|
|
2367
|
-
if (slippage === void 0) { slippage = 0.1; }
|
|
2368
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2369
|
-
return __generator(this, function (_a) {
|
|
2370
|
-
switch (_a.label) {
|
|
2371
|
-
case 0: return [4 /*yield*/, this.selfLiquidateApprove()];
|
|
2372
|
-
case 1:
|
|
2373
|
-
_a.sent();
|
|
2374
|
-
return [4 /*yield*/, this._liquidate(llamalend.signerAddress, slippage, false)];
|
|
2375
|
-
case 2: return [2 /*return*/, _a.sent()];
|
|
2376
|
-
}
|
|
2377
|
-
});
|
|
1294
|
+
selfLiquidateIsApproved() {
|
|
1295
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1296
|
+
return yield this.liquidateIsApproved();
|
|
1297
|
+
});
|
|
1298
|
+
}
|
|
1299
|
+
selfLiquidateApproveEstimateGas() {
|
|
1300
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1301
|
+
return this.liquidateApproveEstimateGas();
|
|
1302
|
+
});
|
|
1303
|
+
}
|
|
1304
|
+
selfLiquidateApprove() {
|
|
1305
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1306
|
+
return yield this.liquidateApprove();
|
|
1307
|
+
});
|
|
1308
|
+
}
|
|
1309
|
+
selfLiquidateEstimateGas() {
|
|
1310
|
+
return __awaiter(this, arguments, void 0, function* (slippage = 0.1) {
|
|
1311
|
+
if (!(yield this.selfLiquidateIsApproved()))
|
|
1312
|
+
throw Error("Approval is needed for gas estimation");
|
|
1313
|
+
return yield this._liquidate(llamalend.signerAddress, slippage, true);
|
|
1314
|
+
});
|
|
1315
|
+
}
|
|
1316
|
+
selfLiquidate() {
|
|
1317
|
+
return __awaiter(this, arguments, void 0, function* (slippage = 0.1) {
|
|
1318
|
+
yield this.selfLiquidateApprove();
|
|
1319
|
+
return yield this._liquidate(llamalend.signerAddress, slippage, false);
|
|
2378
1320
|
});
|
|
2379
|
-
}
|
|
1321
|
+
}
|
|
2380
1322
|
// ---------------- CREATE LOAN WITH LEVERAGE ----------------
|
|
2381
|
-
|
|
2382
|
-
|
|
2383
|
-
for (
|
|
1323
|
+
_getBestIdx(_amounts) {
|
|
1324
|
+
let bestIdx = 0;
|
|
1325
|
+
for (let i = 1; i < 5; i++) {
|
|
2384
1326
|
if (_amounts[i] > _amounts[bestIdx])
|
|
2385
1327
|
bestIdx = i;
|
|
2386
1328
|
}
|
|
2387
1329
|
return bestIdx;
|
|
2388
|
-
}
|
|
2389
|
-
|
|
1330
|
+
}
|
|
1331
|
+
_checkLeverageZap() {
|
|
2390
1332
|
if (this.leverageZap === "0x0000000000000000000000000000000000000000")
|
|
2391
|
-
throw Error(
|
|
2392
|
-
}
|
|
2393
|
-
|
|
2394
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2395
|
-
|
|
2396
|
-
|
|
2397
|
-
|
|
2398
|
-
|
|
2399
|
-
|
|
2400
|
-
|
|
2401
|
-
|
|
2402
|
-
|
|
2403
|
-
|
|
2404
|
-
|
|
2405
|
-
|
|
2406
|
-
|
|
2407
|
-
|
|
2408
|
-
|
|
2409
|
-
|
|
2410
|
-
|
|
2411
|
-
|
|
2412
|
-
|
|
2413
|
-
|
|
2414
|
-
|
|
2415
|
-
|
|
2416
|
-
|
|
2417
|
-
|
|
2418
|
-
|
|
2419
|
-
|
|
2420
|
-
|
|
2421
|
-
|
|
2422
|
-
|
|
2423
|
-
|
|
2424
|
-
|
|
2425
|
-
|
|
2426
|
-
|
|
2427
|
-
return
|
|
2428
|
-
|
|
2429
|
-
|
|
2430
|
-
|
|
2431
|
-
|
|
2432
|
-
|
|
2433
|
-
|
|
2434
|
-
|
|
2435
|
-
|
|
2436
|
-
|
|
2437
|
-
|
|
2438
|
-
|
|
2439
|
-
|
|
2440
|
-
|
|
2441
|
-
|
|
2442
|
-
|
|
2443
|
-
|
|
2444
|
-
|
|
2445
|
-
|
|
2446
|
-
|
|
2447
|
-
|
|
2448
|
-
|
|
2449
|
-
|
|
2450
|
-
|
|
2451
|
-
|
|
2452
|
-
|
|
2453
|
-
|
|
2454
|
-
|
|
2455
|
-
|
|
2456
|
-
|
|
2457
|
-
|
|
2458
|
-
|
|
2459
|
-
|
|
2460
|
-
|
|
2461
|
-
|
|
2462
|
-
|
|
2463
|
-
|
|
2464
|
-
|
|
2465
|
-
|
|
2466
|
-
|
|
2467
|
-
|
|
2468
|
-
|
|
2469
|
-
|
|
2470
|
-
|
|
2471
|
-
|
|
2472
|
-
|
|
2473
|
-
|
|
2474
|
-
|
|
2475
|
-
|
|
2476
|
-
|
|
2477
|
-
|
|
2478
|
-
|
|
2479
|
-
|
|
2480
|
-
|
|
2481
|
-
|
|
2482
|
-
|
|
2483
|
-
|
|
2484
|
-
}
|
|
2485
|
-
|
|
2486
|
-
|
|
2487
|
-
|
|
2488
|
-
|
|
2489
|
-
|
|
2490
|
-
|
|
2491
|
-
|
|
2492
|
-
|
|
2493
|
-
|
|
2494
|
-
|
|
2495
|
-
|
|
2496
|
-
|
|
2497
|
-
|
|
2498
|
-
|
|
2499
|
-
|
|
2500
|
-
|
|
2501
|
-
|
|
2502
|
-
}
|
|
2503
|
-
|
|
2504
|
-
|
|
2505
|
-
|
|
2506
|
-
|
|
2507
|
-
|
|
2508
|
-
|
|
2509
|
-
switch (_a.label) {
|
|
2510
|
-
case 0: return [4 /*yield*/, this._getRouteIdx(collateral, debt)];
|
|
2511
|
-
case 1:
|
|
2512
|
-
routeIdx = _a.sent();
|
|
2513
|
-
_collateral = parseUnits(collateral, this.collateralDecimals);
|
|
2514
|
-
_debt = parseUnits(debt);
|
|
2515
|
-
calls = [];
|
|
2516
|
-
for (N = this.minBands; N <= maxN; N++) {
|
|
2517
|
-
calls.push(llamalend.contracts[this.leverageZap].multicallContract.calculate_debt_n1(_collateral, _debt, N, routeIdx));
|
|
2518
|
-
}
|
|
2519
|
-
return [4 /*yield*/, llamalend.multicallProvider.all(calls)];
|
|
2520
|
-
case 2: return [2 /*return*/, _a.sent()];
|
|
2521
|
-
}
|
|
2522
|
-
});
|
|
2523
|
-
});
|
|
2524
|
-
};
|
|
2525
|
-
MintMarketTemplate.prototype._leverageCreateLoanBands = function (collateral, debt, range) {
|
|
2526
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2527
|
-
var _n1, _n2;
|
|
2528
|
-
return __generator(this, function (_a) {
|
|
2529
|
-
switch (_a.label) {
|
|
2530
|
-
case 0: return [4 /*yield*/, this._leverageCalcN1(collateral, debt, range)];
|
|
2531
|
-
case 1:
|
|
2532
|
-
_n1 = _a.sent();
|
|
2533
|
-
_n2 = _n1 + BigInt(range - 1);
|
|
2534
|
-
return [2 /*return*/, [_n2, _n1]];
|
|
2535
|
-
}
|
|
2536
|
-
});
|
|
2537
|
-
});
|
|
2538
|
-
};
|
|
2539
|
-
MintMarketTemplate.prototype._leverageCreateLoanBandsAllRanges = function (collateral, debt) {
|
|
2540
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2541
|
-
var maxN, _n1_arr, _n2_arr, N, _bands, N;
|
|
2542
|
-
return __generator(this, function (_a) {
|
|
2543
|
-
switch (_a.label) {
|
|
2544
|
-
case 0: return [4 /*yield*/, this.leverageGetMaxRange(collateral, debt)];
|
|
2545
|
-
case 1:
|
|
2546
|
-
maxN = _a.sent();
|
|
2547
|
-
return [4 /*yield*/, this._leverageCalcN1AllRanges(collateral, debt, maxN)];
|
|
2548
|
-
case 2:
|
|
2549
|
-
_n1_arr = _a.sent();
|
|
2550
|
-
_n2_arr = [];
|
|
2551
|
-
for (N = this.minBands; N <= maxN; N++) {
|
|
2552
|
-
_n2_arr.push(_n1_arr[N - this.minBands] + BigInt(N - 1));
|
|
2553
|
-
}
|
|
2554
|
-
_bands = {};
|
|
2555
|
-
for (N = this.minBands; N <= maxN; N++) {
|
|
2556
|
-
_bands[N] = [_n2_arr[N - this.minBands], _n1_arr[N - this.minBands]];
|
|
2557
|
-
}
|
|
2558
|
-
return [2 /*return*/, _bands];
|
|
2559
|
-
}
|
|
2560
|
-
});
|
|
2561
|
-
});
|
|
2562
|
-
};
|
|
2563
|
-
MintMarketTemplate.prototype.leverageCreateLoanBands = function (collateral, debt, range) {
|
|
2564
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2565
|
-
var _a, _n2, _n1;
|
|
2566
|
-
return __generator(this, function (_c) {
|
|
2567
|
-
switch (_c.label) {
|
|
2568
|
-
case 0:
|
|
2569
|
-
this._checkLeverageZap();
|
|
2570
|
-
return [4 /*yield*/, this._leverageCreateLoanBands(collateral, debt, range)];
|
|
2571
|
-
case 1:
|
|
2572
|
-
_a = _c.sent(), _n2 = _a[0], _n1 = _a[1];
|
|
2573
|
-
return [2 /*return*/, [Number(_n2), Number(_n1)]];
|
|
2574
|
-
}
|
|
2575
|
-
});
|
|
2576
|
-
});
|
|
2577
|
-
};
|
|
2578
|
-
MintMarketTemplate.prototype.leverageCreateLoanBandsAllRanges = function (collateral, debt) {
|
|
2579
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2580
|
-
var _bands, bands, N;
|
|
2581
|
-
return __generator(this, function (_a) {
|
|
2582
|
-
switch (_a.label) {
|
|
2583
|
-
case 0:
|
|
2584
|
-
this._checkLeverageZap();
|
|
2585
|
-
return [4 /*yield*/, this._leverageCreateLoanBandsAllRanges(collateral, debt)];
|
|
2586
|
-
case 1:
|
|
2587
|
-
_bands = _a.sent();
|
|
2588
|
-
bands = {};
|
|
2589
|
-
for (N = this.minBands; N <= this.maxBands; N++) {
|
|
2590
|
-
if (_bands[N]) {
|
|
2591
|
-
bands[N] = _bands[N].map(Number);
|
|
2592
|
-
}
|
|
2593
|
-
else {
|
|
2594
|
-
bands[N] = null;
|
|
2595
|
-
}
|
|
2596
|
-
}
|
|
2597
|
-
return [2 /*return*/, bands];
|
|
2598
|
-
}
|
|
2599
|
-
});
|
|
2600
|
-
});
|
|
2601
|
-
};
|
|
2602
|
-
MintMarketTemplate.prototype.leverageCreateLoanPrices = function (collateral, debt, range) {
|
|
2603
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2604
|
-
var _a, _n2, _n1;
|
|
2605
|
-
return __generator(this, function (_c) {
|
|
2606
|
-
switch (_c.label) {
|
|
2607
|
-
case 0:
|
|
2608
|
-
this._checkLeverageZap();
|
|
2609
|
-
return [4 /*yield*/, this._leverageCreateLoanBands(collateral, debt, range)];
|
|
2610
|
-
case 1:
|
|
2611
|
-
_a = _c.sent(), _n2 = _a[0], _n1 = _a[1];
|
|
2612
|
-
return [4 /*yield*/, this._getPrices(_n2, _n1)];
|
|
2613
|
-
case 2: return [2 /*return*/, _c.sent()];
|
|
2614
|
-
}
|
|
2615
|
-
});
|
|
2616
|
-
});
|
|
2617
|
-
};
|
|
2618
|
-
MintMarketTemplate.prototype.leverageCreateLoanPricesAllRanges = function (collateral, debt) {
|
|
2619
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2620
|
-
var _bands, prices, N, _a, _c;
|
|
2621
|
-
return __generator(this, function (_d) {
|
|
2622
|
-
switch (_d.label) {
|
|
2623
|
-
case 0:
|
|
2624
|
-
this._checkLeverageZap();
|
|
2625
|
-
return [4 /*yield*/, this._leverageCreateLoanBandsAllRanges(collateral, debt)];
|
|
2626
|
-
case 1:
|
|
2627
|
-
_bands = _d.sent();
|
|
2628
|
-
prices = {};
|
|
2629
|
-
N = this.minBands;
|
|
2630
|
-
_d.label = 2;
|
|
2631
|
-
case 2:
|
|
2632
|
-
if (!(N <= this.maxBands)) return [3 /*break*/, 6];
|
|
2633
|
-
if (!_bands[N]) return [3 /*break*/, 4];
|
|
2634
|
-
_a = prices;
|
|
2635
|
-
_c = N;
|
|
2636
|
-
return [4 /*yield*/, this._calcPrices.apply(this, _bands[N])];
|
|
2637
|
-
case 3:
|
|
2638
|
-
_a[_c] = _d.sent();
|
|
2639
|
-
return [3 /*break*/, 5];
|
|
2640
|
-
case 4:
|
|
2641
|
-
prices[N] = null;
|
|
2642
|
-
_d.label = 5;
|
|
2643
|
-
case 5:
|
|
2644
|
-
N++;
|
|
2645
|
-
return [3 /*break*/, 2];
|
|
2646
|
-
case 6: return [2 /*return*/, prices];
|
|
2647
|
-
}
|
|
2648
|
-
});
|
|
2649
|
-
});
|
|
2650
|
-
};
|
|
2651
|
-
MintMarketTemplate.prototype.leverageCreateLoanHealth = function (collateral, debt, range, full) {
|
|
2652
|
-
var _a;
|
|
2653
|
-
if (full === void 0) { full = true; }
|
|
2654
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2655
|
-
var address, _collateral, _debt, contract, _health;
|
|
2656
|
-
return __generator(this, function (_c) {
|
|
2657
|
-
switch (_c.label) {
|
|
2658
|
-
case 0:
|
|
2659
|
-
this._checkLeverageZap();
|
|
2660
|
-
address = "0x0000000000000000000000000000000000000000";
|
|
2661
|
-
return [4 /*yield*/, this._leverageCreateLoanCollateral(collateral, debt)];
|
|
2662
|
-
case 1:
|
|
2663
|
-
_collateral = (_c.sent())._collateral;
|
|
2664
|
-
_debt = parseUnits(debt);
|
|
2665
|
-
contract = llamalend.contracts[(_a = this.healthCalculator) !== null && _a !== void 0 ? _a : this.controller].contract;
|
|
2666
|
-
return [4 /*yield*/, contract.health_calculator(address, _collateral, _debt, full, range, llamalend.constantOptions)];
|
|
2667
|
-
case 2:
|
|
2668
|
-
_health = _c.sent();
|
|
2669
|
-
_health = _health * BigInt(100);
|
|
2670
|
-
return [2 /*return*/, formatUnits(_health)];
|
|
2671
|
-
}
|
|
2672
|
-
});
|
|
2673
|
-
});
|
|
2674
|
-
};
|
|
2675
|
-
MintMarketTemplate.prototype.leveragePriceImpact = function (collateral, debt) {
|
|
2676
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2677
|
-
var x_BN, small_x_BN, _a, _collateral, routeIdx, _y, _small_y, y_BN, small_y_BN, rateBN, smallRateBN;
|
|
2678
|
-
return __generator(this, function (_c) {
|
|
2679
|
-
switch (_c.label) {
|
|
2680
|
-
case 0:
|
|
2681
|
-
x_BN = BN(debt);
|
|
2682
|
-
small_x_BN = BN(100);
|
|
2683
|
-
return [4 /*yield*/, this._leverageCreateLoanCollateral(collateral, debt)];
|
|
2684
|
-
case 1:
|
|
2685
|
-
_a = _c.sent(), _collateral = _a._collateral, routeIdx = _a.routeIdx;
|
|
2686
|
-
_y = _collateral - parseUnits(collateral, this.collateralDecimals);
|
|
2687
|
-
return [4 /*yield*/, llamalend.contracts[this.leverageZap].contract.get_collateral(fromBN(small_x_BN), routeIdx)];
|
|
2688
|
-
case 2:
|
|
2689
|
-
_small_y = _c.sent();
|
|
2690
|
-
y_BN = toBN(_y, this.collateralDecimals);
|
|
2691
|
-
small_y_BN = toBN(_small_y, this.collateralDecimals);
|
|
2692
|
-
rateBN = y_BN.div(x_BN);
|
|
2693
|
-
smallRateBN = small_y_BN.div(small_x_BN);
|
|
2694
|
-
if (rateBN.gt(smallRateBN))
|
|
2695
|
-
return [2 /*return*/, "0.0"];
|
|
2696
|
-
return [2 /*return*/, BN(1).minus(rateBN.div(smallRateBN)).times(100).toFixed(4)];
|
|
2697
|
-
}
|
|
2698
|
-
});
|
|
2699
|
-
});
|
|
2700
|
-
};
|
|
2701
|
-
MintMarketTemplate.prototype._leverageCreateLoan = function (collateral, debt, range, slippage, estimateGas) {
|
|
2702
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2703
|
-
var _collateral, _debt, leverageContract, routeIdx, _expected, minRecvBN, _minRecv, contract, value, gas, gasLimit;
|
|
2704
|
-
return __generator(this, function (_a) {
|
|
2705
|
-
switch (_a.label) {
|
|
2706
|
-
case 0: return [4 /*yield*/, this.loanExists()];
|
|
2707
|
-
case 1:
|
|
2708
|
-
if (_a.sent())
|
|
2709
|
-
throw Error("Loan already created");
|
|
2710
|
-
this._checkRange(range);
|
|
2711
|
-
_collateral = parseUnits(collateral, this.collateralDecimals);
|
|
2712
|
-
_debt = parseUnits(debt);
|
|
2713
|
-
leverageContract = llamalend.contracts[this.leverageZap].contract;
|
|
2714
|
-
return [4 /*yield*/, this._getRouteIdx(collateral, debt)];
|
|
2715
|
-
case 2:
|
|
2716
|
-
routeIdx = _a.sent();
|
|
2717
|
-
return [4 /*yield*/, leverageContract.get_collateral_underlying(_debt, routeIdx, llamalend.constantOptions)];
|
|
2718
|
-
case 3:
|
|
2719
|
-
_expected = _a.sent();
|
|
2720
|
-
minRecvBN = toBN(_expected, this.collateralDecimals).times(100 - slippage).div(100);
|
|
2721
|
-
_minRecv = fromBN(minRecvBN, this.collateralDecimals);
|
|
2722
|
-
contract = llamalend.contracts[this.controller].contract;
|
|
2723
|
-
value = isEth(this.collateral) ? _collateral : llamalend.parseUnits("0");
|
|
2724
|
-
return [4 /*yield*/, contract.create_loan_extended.estimateGas(_collateral, _debt, range, this.leverageZap, [routeIdx, _minRecv], __assign(__assign({}, llamalend.constantOptions), { value: value }))];
|
|
2725
|
-
case 4:
|
|
2726
|
-
gas = _a.sent();
|
|
2727
|
-
if (estimateGas)
|
|
2728
|
-
return [2 /*return*/, smartNumber(gas)];
|
|
2729
|
-
return [4 /*yield*/, llamalend.updateFeeData()];
|
|
2730
|
-
case 5:
|
|
2731
|
-
_a.sent();
|
|
2732
|
-
gasLimit = _mulBy1_3(DIGas(gas));
|
|
2733
|
-
return [4 /*yield*/, contract.create_loan_extended(_collateral, _debt, range, this.leverageZap, [routeIdx, _minRecv], __assign(__assign({}, llamalend.options), { gasLimit: gasLimit, value: value }))];
|
|
2734
|
-
case 6: return [2 /*return*/, (_a.sent()).hash];
|
|
2735
|
-
}
|
|
2736
|
-
});
|
|
2737
|
-
});
|
|
2738
|
-
};
|
|
2739
|
-
MintMarketTemplate.prototype.leverageCreateLoanEstimateGas = function (collateral, debt, range, slippage) {
|
|
2740
|
-
if (slippage === void 0) { slippage = 0.1; }
|
|
2741
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2742
|
-
return __generator(this, function (_a) {
|
|
2743
|
-
switch (_a.label) {
|
|
2744
|
-
case 0:
|
|
2745
|
-
this._checkLeverageZap();
|
|
2746
|
-
return [4 /*yield*/, this.createLoanIsApproved(collateral)];
|
|
2747
|
-
case 1:
|
|
2748
|
-
if (!(_a.sent()))
|
|
2749
|
-
throw Error("Approval is needed for gas estimation");
|
|
2750
|
-
return [4 /*yield*/, this._leverageCreateLoan(collateral, debt, range, slippage, true)];
|
|
2751
|
-
case 2: return [2 /*return*/, _a.sent()];
|
|
1333
|
+
throw Error(`There is no leverage for ${this.id} market`);
|
|
1334
|
+
}
|
|
1335
|
+
leverageCreateLoanMaxRecv(collateral, range) {
|
|
1336
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1337
|
+
this._checkLeverageZap();
|
|
1338
|
+
this._checkRange(range);
|
|
1339
|
+
const _collateral = parseUnits(collateral, this.collateralDecimals);
|
|
1340
|
+
const calls = [];
|
|
1341
|
+
for (let i = 0; i < 5; i++) {
|
|
1342
|
+
calls.push(llamalend.contracts[this.leverageZap].multicallContract.max_borrowable_and_collateral(_collateral, range, i));
|
|
1343
|
+
}
|
|
1344
|
+
const _res = yield llamalend.multicallProvider.all(calls);
|
|
1345
|
+
const _maxBorrowable = _res.map((r) => r[0] * BigInt(999) / BigInt(1000));
|
|
1346
|
+
const _maxCollateral = _res.map((r) => r[1] * BigInt(999) / BigInt(1000));
|
|
1347
|
+
const routeIdx = this._getBestIdx(_maxCollateral);
|
|
1348
|
+
const maxBorrowable = llamalend.formatUnits(_maxBorrowable[routeIdx]);
|
|
1349
|
+
const maxCollateral = llamalend.formatUnits(_maxCollateral[routeIdx], this.collateralDecimals);
|
|
1350
|
+
return {
|
|
1351
|
+
maxBorrowable,
|
|
1352
|
+
maxCollateral,
|
|
1353
|
+
leverage: BN(maxCollateral).div(collateral).toFixed(4),
|
|
1354
|
+
routeIdx,
|
|
1355
|
+
};
|
|
1356
|
+
});
|
|
1357
|
+
}
|
|
1358
|
+
_getRouteIdx(userCollateral, debt) {
|
|
1359
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1360
|
+
const { routeIdx } = yield this._leverageCreateLoanCollateral(userCollateral, debt);
|
|
1361
|
+
return routeIdx;
|
|
1362
|
+
});
|
|
1363
|
+
}
|
|
1364
|
+
leverageCreateLoanCollateral(userCollateral, debt) {
|
|
1365
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1366
|
+
this._checkLeverageZap();
|
|
1367
|
+
const { _collateral, routeIdx } = yield this._leverageCreateLoanCollateral(userCollateral, debt);
|
|
1368
|
+
const collateral = llamalend.formatUnits(_collateral, this.collateralDecimals);
|
|
1369
|
+
return { collateral, leverage: BN(collateral).div(userCollateral).toFixed(4), routeIdx };
|
|
1370
|
+
});
|
|
1371
|
+
}
|
|
1372
|
+
leverageGetRouteName(routeIdx) {
|
|
1373
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1374
|
+
this._checkLeverageZap();
|
|
1375
|
+
return yield llamalend.contracts[this.leverageZap].contract.route_names(routeIdx);
|
|
1376
|
+
});
|
|
1377
|
+
}
|
|
1378
|
+
leverageGetMaxRange(collateral, debt) {
|
|
1379
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1380
|
+
this._checkLeverageZap();
|
|
1381
|
+
const routeIdx = yield this._getRouteIdx(collateral, debt);
|
|
1382
|
+
const maxRecv = yield this._leverageCreateLoanMaxRecvAllRanges2(collateral, routeIdx);
|
|
1383
|
+
for (let N = this.minBands; N <= this.maxBands; N++) {
|
|
1384
|
+
if (BN(debt).gt(BN(maxRecv[N].maxBorrowable)))
|
|
1385
|
+
return N - 1;
|
|
1386
|
+
}
|
|
1387
|
+
return this.maxBands;
|
|
1388
|
+
});
|
|
1389
|
+
}
|
|
1390
|
+
_leverageCalcN1(collateral, debt, range) {
|
|
1391
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1392
|
+
this._checkRange(range);
|
|
1393
|
+
const routeIdx = yield this._getRouteIdx(collateral, debt);
|
|
1394
|
+
const _collateral = parseUnits(collateral, this.collateralDecimals);
|
|
1395
|
+
const _debt = parseUnits(debt);
|
|
1396
|
+
return yield llamalend.contracts[this.leverageZap].contract.calculate_debt_n1(_collateral, _debt, range, routeIdx, llamalend.constantOptions);
|
|
1397
|
+
});
|
|
1398
|
+
}
|
|
1399
|
+
_leverageCalcN1AllRanges(collateral, debt, maxN) {
|
|
1400
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1401
|
+
const routeIdx = yield this._getRouteIdx(collateral, debt);
|
|
1402
|
+
const _collateral = parseUnits(collateral, this.collateralDecimals);
|
|
1403
|
+
const _debt = parseUnits(debt);
|
|
1404
|
+
const calls = [];
|
|
1405
|
+
for (let N = this.minBands; N <= maxN; N++) {
|
|
1406
|
+
calls.push(llamalend.contracts[this.leverageZap].multicallContract.calculate_debt_n1(_collateral, _debt, N, routeIdx));
|
|
1407
|
+
}
|
|
1408
|
+
return yield llamalend.multicallProvider.all(calls);
|
|
1409
|
+
});
|
|
1410
|
+
}
|
|
1411
|
+
_leverageCreateLoanBands(collateral, debt, range) {
|
|
1412
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1413
|
+
const _n1 = yield this._leverageCalcN1(collateral, debt, range);
|
|
1414
|
+
const _n2 = _n1 + BigInt(range - 1);
|
|
1415
|
+
return [_n2, _n1];
|
|
1416
|
+
});
|
|
1417
|
+
}
|
|
1418
|
+
_leverageCreateLoanBandsAllRanges(collateral, debt) {
|
|
1419
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1420
|
+
const maxN = yield this.leverageGetMaxRange(collateral, debt);
|
|
1421
|
+
const _n1_arr = yield this._leverageCalcN1AllRanges(collateral, debt, maxN);
|
|
1422
|
+
const _n2_arr = [];
|
|
1423
|
+
for (let N = this.minBands; N <= maxN; N++) {
|
|
1424
|
+
_n2_arr.push(_n1_arr[N - this.minBands] + BigInt(N - 1));
|
|
1425
|
+
}
|
|
1426
|
+
const _bands = {};
|
|
1427
|
+
for (let N = this.minBands; N <= maxN; N++) {
|
|
1428
|
+
_bands[N] = [_n2_arr[N - this.minBands], _n1_arr[N - this.minBands]];
|
|
1429
|
+
}
|
|
1430
|
+
return _bands;
|
|
1431
|
+
});
|
|
1432
|
+
}
|
|
1433
|
+
leverageCreateLoanBands(collateral, debt, range) {
|
|
1434
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1435
|
+
this._checkLeverageZap();
|
|
1436
|
+
const [_n2, _n1] = yield this._leverageCreateLoanBands(collateral, debt, range);
|
|
1437
|
+
return [Number(_n2), Number(_n1)];
|
|
1438
|
+
});
|
|
1439
|
+
}
|
|
1440
|
+
leverageCreateLoanBandsAllRanges(collateral, debt) {
|
|
1441
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1442
|
+
this._checkLeverageZap();
|
|
1443
|
+
const _bands = yield this._leverageCreateLoanBandsAllRanges(collateral, debt);
|
|
1444
|
+
const bands = {};
|
|
1445
|
+
for (let N = this.minBands; N <= this.maxBands; N++) {
|
|
1446
|
+
if (_bands[N]) {
|
|
1447
|
+
bands[N] = _bands[N].map(Number);
|
|
1448
|
+
}
|
|
1449
|
+
else {
|
|
1450
|
+
bands[N] = null;
|
|
2752
1451
|
}
|
|
2753
|
-
}
|
|
2754
|
-
|
|
2755
|
-
|
|
2756
|
-
|
|
2757
|
-
|
|
2758
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2759
|
-
|
|
2760
|
-
|
|
2761
|
-
|
|
2762
|
-
|
|
2763
|
-
|
|
2764
|
-
|
|
2765
|
-
|
|
2766
|
-
|
|
2767
|
-
|
|
1452
|
+
}
|
|
1453
|
+
return bands;
|
|
1454
|
+
});
|
|
1455
|
+
}
|
|
1456
|
+
leverageCreateLoanPrices(collateral, debt, range) {
|
|
1457
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1458
|
+
this._checkLeverageZap();
|
|
1459
|
+
const [_n2, _n1] = yield this._leverageCreateLoanBands(collateral, debt, range);
|
|
1460
|
+
return yield this._getPrices(_n2, _n1);
|
|
1461
|
+
});
|
|
1462
|
+
}
|
|
1463
|
+
leverageCreateLoanPricesAllRanges(collateral, debt) {
|
|
1464
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1465
|
+
this._checkLeverageZap();
|
|
1466
|
+
const _bands = yield this._leverageCreateLoanBandsAllRanges(collateral, debt);
|
|
1467
|
+
const prices = {};
|
|
1468
|
+
for (let N = this.minBands; N <= this.maxBands; N++) {
|
|
1469
|
+
if (_bands[N]) {
|
|
1470
|
+
prices[N] = yield this._calcPrices(..._bands[N]);
|
|
1471
|
+
}
|
|
1472
|
+
else {
|
|
1473
|
+
prices[N] = null;
|
|
2768
1474
|
}
|
|
2769
|
-
}
|
|
1475
|
+
}
|
|
1476
|
+
return prices;
|
|
1477
|
+
});
|
|
1478
|
+
}
|
|
1479
|
+
leverageCreateLoanHealth(collateral_1, debt_1, range_1) {
|
|
1480
|
+
return __awaiter(this, arguments, void 0, function* (collateral, debt, range, full = true) {
|
|
1481
|
+
var _a;
|
|
1482
|
+
this._checkLeverageZap();
|
|
1483
|
+
const address = "0x0000000000000000000000000000000000000000";
|
|
1484
|
+
const { _collateral } = yield this._leverageCreateLoanCollateral(collateral, debt);
|
|
1485
|
+
const _debt = parseUnits(debt);
|
|
1486
|
+
const contract = llamalend.contracts[(_a = this.healthCalculator) !== null && _a !== void 0 ? _a : this.controller].contract;
|
|
1487
|
+
let _health = yield contract.health_calculator(address, _collateral, _debt, full, range, llamalend.constantOptions);
|
|
1488
|
+
_health = _health * BigInt(100);
|
|
1489
|
+
return formatUnits(_health);
|
|
1490
|
+
});
|
|
1491
|
+
}
|
|
1492
|
+
leveragePriceImpact(collateral, debt) {
|
|
1493
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1494
|
+
const x_BN = BN(debt);
|
|
1495
|
+
const small_x_BN = BN(100);
|
|
1496
|
+
const { _collateral, routeIdx } = yield this._leverageCreateLoanCollateral(collateral, debt);
|
|
1497
|
+
const _y = _collateral - parseUnits(collateral, this.collateralDecimals);
|
|
1498
|
+
const _small_y = yield llamalend.contracts[this.leverageZap].contract.get_collateral(fromBN(small_x_BN), routeIdx);
|
|
1499
|
+
const y_BN = toBN(_y, this.collateralDecimals);
|
|
1500
|
+
const small_y_BN = toBN(_small_y, this.collateralDecimals);
|
|
1501
|
+
const rateBN = y_BN.div(x_BN);
|
|
1502
|
+
const smallRateBN = small_y_BN.div(small_x_BN);
|
|
1503
|
+
if (rateBN.gt(smallRateBN))
|
|
1504
|
+
return "0.0";
|
|
1505
|
+
return BN(1).minus(rateBN.div(smallRateBN)).times(100).toFixed(4);
|
|
1506
|
+
});
|
|
1507
|
+
}
|
|
1508
|
+
_leverageCreateLoan(collateral, debt, range, slippage, estimateGas) {
|
|
1509
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1510
|
+
if (yield this.loanExists())
|
|
1511
|
+
throw Error("Loan already created");
|
|
1512
|
+
this._checkRange(range);
|
|
1513
|
+
const _collateral = parseUnits(collateral, this.collateralDecimals);
|
|
1514
|
+
const _debt = parseUnits(debt);
|
|
1515
|
+
const leverageContract = llamalend.contracts[this.leverageZap].contract;
|
|
1516
|
+
const routeIdx = yield this._getRouteIdx(collateral, debt);
|
|
1517
|
+
const _expected = yield leverageContract.get_collateral_underlying(_debt, routeIdx, llamalend.constantOptions);
|
|
1518
|
+
const minRecvBN = toBN(_expected, this.collateralDecimals).times(100 - slippage).div(100);
|
|
1519
|
+
const _minRecv = fromBN(minRecvBN, this.collateralDecimals);
|
|
1520
|
+
const contract = llamalend.contracts[this.controller].contract;
|
|
1521
|
+
const value = isEth(this.collateral) ? _collateral : llamalend.parseUnits("0");
|
|
1522
|
+
const gas = yield contract.create_loan_extended.estimateGas(_collateral, _debt, range, this.leverageZap, [routeIdx, _minRecv], Object.assign(Object.assign({}, llamalend.constantOptions), { value }));
|
|
1523
|
+
if (estimateGas)
|
|
1524
|
+
return smartNumber(gas);
|
|
1525
|
+
yield llamalend.updateFeeData();
|
|
1526
|
+
const gasLimit = _mulBy1_3(DIGas(gas));
|
|
1527
|
+
return (yield contract.create_loan_extended(_collateral, _debt, range, this.leverageZap, [routeIdx, _minRecv], Object.assign(Object.assign({}, llamalend.options), { gasLimit, value }))).hash;
|
|
1528
|
+
});
|
|
1529
|
+
}
|
|
1530
|
+
leverageCreateLoanEstimateGas(collateral_1, debt_1, range_1) {
|
|
1531
|
+
return __awaiter(this, arguments, void 0, function* (collateral, debt, range, slippage = 0.1) {
|
|
1532
|
+
this._checkLeverageZap();
|
|
1533
|
+
if (!(yield this.createLoanIsApproved(collateral)))
|
|
1534
|
+
throw Error("Approval is needed for gas estimation");
|
|
1535
|
+
return yield this._leverageCreateLoan(collateral, debt, range, slippage, true);
|
|
2770
1536
|
});
|
|
2771
|
-
}
|
|
1537
|
+
}
|
|
1538
|
+
leverageCreateLoan(collateral_1, debt_1, range_1) {
|
|
1539
|
+
return __awaiter(this, arguments, void 0, function* (collateral, debt, range, slippage = 0.1) {
|
|
1540
|
+
this._checkLeverageZap();
|
|
1541
|
+
yield this.createLoanApprove(collateral);
|
|
1542
|
+
return yield this._leverageCreateLoan(collateral, debt, range, slippage, false);
|
|
1543
|
+
});
|
|
1544
|
+
}
|
|
2772
1545
|
// ---------------- DELEVERAGE REPAY ----------------
|
|
2773
|
-
|
|
1546
|
+
_checkDeleverageZap() {
|
|
2774
1547
|
if (this.deleverageZap === "0x0000000000000000000000000000000000000000")
|
|
2775
|
-
throw Error(
|
|
2776
|
-
}
|
|
2777
|
-
|
|
2778
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2779
|
-
|
|
2780
|
-
|
|
2781
|
-
case 0:
|
|
2782
|
-
this._checkDeleverageZap();
|
|
2783
|
-
return [4 /*yield*/, llamalend.contracts[this.deleverageZap].contract.route_names(routeIdx)];
|
|
2784
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
2785
|
-
}
|
|
2786
|
-
});
|
|
2787
|
-
});
|
|
2788
|
-
};
|
|
2789
|
-
MintMarketTemplate.prototype.deleverageIsFullRepayment = function (deleverageCollateral, address) {
|
|
2790
|
-
if (address === void 0) { address = ""; }
|
|
2791
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2792
|
-
var _a, stablecoin, debt, deleverageStablecoins;
|
|
2793
|
-
return __generator(this, function (_c) {
|
|
2794
|
-
switch (_c.label) {
|
|
2795
|
-
case 0:
|
|
2796
|
-
address = _getAddress(address);
|
|
2797
|
-
return [4 /*yield*/, this.userState(address)];
|
|
2798
|
-
case 1:
|
|
2799
|
-
_a = _c.sent(), stablecoin = _a.stablecoin, debt = _a.debt;
|
|
2800
|
-
return [4 /*yield*/, this.deleverageRepayStablecoins(deleverageCollateral)];
|
|
2801
|
-
case 2:
|
|
2802
|
-
deleverageStablecoins = (_c.sent()).stablecoins;
|
|
2803
|
-
return [2 /*return*/, BN(stablecoin).plus(deleverageStablecoins).gt(debt)];
|
|
2804
|
-
}
|
|
2805
|
-
});
|
|
2806
|
-
});
|
|
2807
|
-
};
|
|
2808
|
-
MintMarketTemplate.prototype.deleverageIsAvailable = function (deleverageCollateral, address) {
|
|
2809
|
-
if (address === void 0) { address = ""; }
|
|
2810
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2811
|
-
var _a, collateral, stablecoin, debt;
|
|
2812
|
-
return __generator(this, function (_c) {
|
|
2813
|
-
switch (_c.label) {
|
|
2814
|
-
case 0:
|
|
2815
|
-
// 0. const { collateral, stablecoin, debt } = await this.userState(address);
|
|
2816
|
-
// 1. maxCollateral for deleverage is collateral from line above (0).
|
|
2817
|
-
// 2. If user is underwater (stablecoin > 0), only full repayment is available:
|
|
2818
|
-
// await this.deleverageRepayStablecoins(deleverageCollateral) + stablecoin > debt
|
|
2819
|
-
// There is no deleverage zap
|
|
2820
|
-
if (this.deleverageZap === "0x0000000000000000000000000000000000000000")
|
|
2821
|
-
return [2 /*return*/, false];
|
|
2822
|
-
address = _getAddress(address);
|
|
2823
|
-
return [4 /*yield*/, this.userState(address)];
|
|
2824
|
-
case 1:
|
|
2825
|
-
_a = _c.sent(), collateral = _a.collateral, stablecoin = _a.stablecoin, debt = _a.debt;
|
|
2826
|
-
// Loan does not exist
|
|
2827
|
-
if (BN(debt).eq(0))
|
|
2828
|
-
return [2 /*return*/, false];
|
|
2829
|
-
// Can't spend more than user has
|
|
2830
|
-
if (BN(deleverageCollateral).gt(collateral))
|
|
2831
|
-
return [2 /*return*/, false];
|
|
2832
|
-
if (!BN(stablecoin).gt(0)) return [3 /*break*/, 3];
|
|
2833
|
-
return [4 /*yield*/, this.deleverageIsFullRepayment(deleverageCollateral, address)];
|
|
2834
|
-
case 2: return [2 /*return*/, _c.sent()];
|
|
2835
|
-
case 3: return [2 /*return*/, true];
|
|
2836
|
-
}
|
|
2837
|
-
});
|
|
2838
|
-
});
|
|
2839
|
-
};
|
|
2840
|
-
MintMarketTemplate.prototype.deleverageRepayBands = function (collateral, address) {
|
|
2841
|
-
if (address === void 0) { address = ""; }
|
|
2842
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2843
|
-
var _a, _n2, _n1;
|
|
2844
|
-
return __generator(this, function (_c) {
|
|
2845
|
-
switch (_c.label) {
|
|
2846
|
-
case 0:
|
|
2847
|
-
this._checkDeleverageZap();
|
|
2848
|
-
return [4 /*yield*/, this._deleverageRepayBands(collateral, address)];
|
|
2849
|
-
case 1:
|
|
2850
|
-
_a = _c.sent(), _n2 = _a[0], _n1 = _a[1];
|
|
2851
|
-
return [2 /*return*/, [Number(_n2), Number(_n1)]];
|
|
2852
|
-
}
|
|
2853
|
-
});
|
|
2854
|
-
});
|
|
2855
|
-
};
|
|
2856
|
-
MintMarketTemplate.prototype.deleverageRepayPrices = function (debt, address) {
|
|
2857
|
-
if (address === void 0) { address = ""; }
|
|
2858
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2859
|
-
var _a, _n2, _n1;
|
|
2860
|
-
return __generator(this, function (_c) {
|
|
2861
|
-
switch (_c.label) {
|
|
2862
|
-
case 0:
|
|
2863
|
-
this._checkDeleverageZap();
|
|
2864
|
-
return [4 /*yield*/, this._deleverageRepayBands(debt, address)];
|
|
2865
|
-
case 1:
|
|
2866
|
-
_a = _c.sent(), _n2 = _a[0], _n1 = _a[1];
|
|
2867
|
-
return [4 /*yield*/, this._getPrices(_n2, _n1)];
|
|
2868
|
-
case 2: return [2 /*return*/, _c.sent()];
|
|
2869
|
-
}
|
|
2870
|
-
});
|
|
2871
|
-
});
|
|
2872
|
-
};
|
|
2873
|
-
MintMarketTemplate.prototype.deleverageRepayHealth = function (collateral, full, address) {
|
|
2874
|
-
var _a;
|
|
2875
|
-
if (full === void 0) { full = true; }
|
|
2876
|
-
if (address === void 0) { address = ""; }
|
|
2877
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2878
|
-
var _c, _stablecoin, _debt, deleverageStablecoins, _d_collateral, _d_debt, N, contract, _health;
|
|
2879
|
-
return __generator(this, function (_d) {
|
|
2880
|
-
switch (_d.label) {
|
|
2881
|
-
case 0:
|
|
2882
|
-
this._checkDeleverageZap();
|
|
2883
|
-
address = _getAddress(address);
|
|
2884
|
-
return [4 /*yield*/, this.deleverageIsAvailable(collateral, address)];
|
|
2885
|
-
case 1:
|
|
2886
|
-
if (!(_d.sent()))
|
|
2887
|
-
return [2 /*return*/, "0.0"];
|
|
2888
|
-
return [4 /*yield*/, this._userState(address)];
|
|
2889
|
-
case 2:
|
|
2890
|
-
_c = _d.sent(), _stablecoin = _c._stablecoin, _debt = _c._debt;
|
|
2891
|
-
return [4 /*yield*/, this.deleverageRepayStablecoins(collateral)];
|
|
2892
|
-
case 3:
|
|
2893
|
-
deleverageStablecoins = (_d.sent()).stablecoins;
|
|
2894
|
-
_d_collateral = parseUnits(collateral, this.collateralDecimals) * BigInt(-1);
|
|
2895
|
-
_d_debt = (parseUnits(deleverageStablecoins) + _stablecoin) * BigInt(-1);
|
|
2896
|
-
return [4 /*yield*/, this.userRange(address)];
|
|
2897
|
-
case 4:
|
|
2898
|
-
N = _d.sent();
|
|
2899
|
-
if ((_debt + _d_debt) < 0)
|
|
2900
|
-
return [2 /*return*/, "0.0"];
|
|
2901
|
-
contract = llamalend.contracts[(_a = this.healthCalculator) !== null && _a !== void 0 ? _a : this.controller].contract;
|
|
2902
|
-
return [4 /*yield*/, contract.health_calculator(address, _d_collateral, _d_debt, full, N, llamalend.constantOptions)];
|
|
2903
|
-
case 5:
|
|
2904
|
-
_health = _d.sent();
|
|
2905
|
-
_health = _health * BigInt(100);
|
|
2906
|
-
return [2 /*return*/, formatUnits(_health)];
|
|
2907
|
-
}
|
|
2908
|
-
});
|
|
2909
|
-
});
|
|
2910
|
-
};
|
|
2911
|
-
MintMarketTemplate.prototype.deleveragePriceImpact = function (collateral) {
|
|
2912
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2913
|
-
var x_BN, small_x_BN, _a, stablecoins, routeIdx, _y, _small_y, y_BN, small_y_BN, rateBN, smallRateBN;
|
|
2914
|
-
return __generator(this, function (_c) {
|
|
2915
|
-
switch (_c.label) {
|
|
2916
|
-
case 0:
|
|
2917
|
-
x_BN = BN(collateral);
|
|
2918
|
-
small_x_BN = BN(0.001);
|
|
2919
|
-
return [4 /*yield*/, this.deleverageRepayStablecoins(collateral)];
|
|
2920
|
-
case 1:
|
|
2921
|
-
_a = _c.sent(), stablecoins = _a.stablecoins, routeIdx = _a.routeIdx;
|
|
2922
|
-
_y = parseUnits(stablecoins);
|
|
2923
|
-
return [4 /*yield*/, llamalend.contracts[this.deleverageZap].contract.get_stablecoins(fromBN(small_x_BN, this.collateralDecimals), routeIdx)];
|
|
2924
|
-
case 2:
|
|
2925
|
-
_small_y = _c.sent();
|
|
2926
|
-
y_BN = toBN(_y);
|
|
2927
|
-
small_y_BN = toBN(_small_y);
|
|
2928
|
-
rateBN = y_BN.div(x_BN);
|
|
2929
|
-
smallRateBN = small_y_BN.div(small_x_BN);
|
|
2930
|
-
if (rateBN.gt(smallRateBN))
|
|
2931
|
-
return [2 /*return*/, "0.0"];
|
|
2932
|
-
return [2 /*return*/, BN(1).minus(rateBN.div(smallRateBN)).times(100).toFixed(4)];
|
|
2933
|
-
}
|
|
2934
|
-
});
|
|
2935
|
-
});
|
|
2936
|
-
};
|
|
2937
|
-
MintMarketTemplate.prototype._deleverageRepay = function (collateral, slippage, estimateGas) {
|
|
2938
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2939
|
-
var currentDebt, _a, stablecoins, routeIdx, _collateral, _debt, minRecvBN, _minRecv, contract, gas, gasLimit;
|
|
2940
|
-
return __generator(this, function (_c) {
|
|
2941
|
-
switch (_c.label) {
|
|
2942
|
-
case 0: return [4 /*yield*/, this.userState(llamalend.signerAddress)];
|
|
2943
|
-
case 1:
|
|
2944
|
-
currentDebt = (_c.sent()).debt;
|
|
2945
|
-
if (Number(currentDebt) === 0)
|
|
2946
|
-
throw Error("Loan for ".concat(llamalend.signerAddress, " does not exist"));
|
|
2947
|
-
return [4 /*yield*/, this.deleverageRepayStablecoins(collateral)];
|
|
2948
|
-
case 2:
|
|
2949
|
-
_a = _c.sent(), stablecoins = _a.stablecoins, routeIdx = _a.routeIdx;
|
|
2950
|
-
_collateral = parseUnits(collateral, this.collateralDecimals);
|
|
2951
|
-
_debt = parseUnits(stablecoins);
|
|
2952
|
-
minRecvBN = toBN(_debt).times(100 - slippage).div(100);
|
|
2953
|
-
_minRecv = fromBN(minRecvBN);
|
|
2954
|
-
contract = llamalend.contracts[this.controller].contract;
|
|
2955
|
-
return [4 /*yield*/, contract.repay_extended.estimateGas(this.deleverageZap, [routeIdx, _collateral, _minRecv], llamalend.constantOptions)];
|
|
2956
|
-
case 3:
|
|
2957
|
-
gas = _c.sent();
|
|
2958
|
-
if (estimateGas)
|
|
2959
|
-
return [2 /*return*/, smartNumber(gas)];
|
|
2960
|
-
return [4 /*yield*/, llamalend.updateFeeData()];
|
|
2961
|
-
case 4:
|
|
2962
|
-
_c.sent();
|
|
2963
|
-
gasLimit = _mulBy1_3(DIGas(gas));
|
|
2964
|
-
return [4 /*yield*/, contract.repay_extended(this.deleverageZap, [routeIdx, _collateral, _minRecv], __assign(__assign({}, llamalend.options), { gasLimit: gasLimit }))];
|
|
2965
|
-
case 5: return [2 /*return*/, (_c.sent()).hash];
|
|
2966
|
-
}
|
|
2967
|
-
});
|
|
2968
|
-
});
|
|
2969
|
-
};
|
|
2970
|
-
MintMarketTemplate.prototype.deleverageRepayEstimateGas = function (collateral, slippage) {
|
|
2971
|
-
if (slippage === void 0) { slippage = 0.1; }
|
|
2972
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2973
|
-
return __generator(this, function (_a) {
|
|
2974
|
-
switch (_a.label) {
|
|
2975
|
-
case 0:
|
|
2976
|
-
this._checkDeleverageZap();
|
|
2977
|
-
return [4 /*yield*/, this._deleverageRepay(collateral, slippage, true)];
|
|
2978
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
2979
|
-
}
|
|
2980
|
-
});
|
|
2981
|
-
});
|
|
2982
|
-
};
|
|
2983
|
-
MintMarketTemplate.prototype.deleverageRepay = function (collateral, slippage) {
|
|
2984
|
-
if (slippage === void 0) { slippage = 0.1; }
|
|
2985
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
2986
|
-
return __generator(this, function (_a) {
|
|
2987
|
-
switch (_a.label) {
|
|
2988
|
-
case 0:
|
|
2989
|
-
this._checkDeleverageZap();
|
|
2990
|
-
return [4 /*yield*/, this._deleverageRepay(collateral, slippage, false)];
|
|
2991
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
2992
|
-
}
|
|
2993
|
-
});
|
|
1548
|
+
throw Error(`There is no deleverage for ${this.id} market`);
|
|
1549
|
+
}
|
|
1550
|
+
deleverageGetRouteName(routeIdx) {
|
|
1551
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1552
|
+
this._checkDeleverageZap();
|
|
1553
|
+
return yield llamalend.contracts[this.deleverageZap].contract.route_names(routeIdx);
|
|
2994
1554
|
});
|
|
2995
|
-
}
|
|
2996
|
-
|
|
2997
|
-
|
|
2998
|
-
|
|
1555
|
+
}
|
|
1556
|
+
deleverageIsFullRepayment(deleverageCollateral_1) {
|
|
1557
|
+
return __awaiter(this, arguments, void 0, function* (deleverageCollateral, address = "") {
|
|
1558
|
+
address = _getAddress(address);
|
|
1559
|
+
const { stablecoin, debt } = yield this.userState(address);
|
|
1560
|
+
const { stablecoins: deleverageStablecoins } = yield this.deleverageRepayStablecoins(deleverageCollateral);
|
|
1561
|
+
return BN(stablecoin).plus(deleverageStablecoins).gt(debt);
|
|
1562
|
+
});
|
|
1563
|
+
}
|
|
1564
|
+
deleverageIsAvailable(deleverageCollateral_1) {
|
|
1565
|
+
return __awaiter(this, arguments, void 0, function* (deleverageCollateral, address = "") {
|
|
1566
|
+
// 0. const { collateral, stablecoin, debt } = await this.userState(address);
|
|
1567
|
+
// 1. maxCollateral for deleverage is collateral from line above (0).
|
|
1568
|
+
// 2. If user is underwater (stablecoin > 0), only full repayment is available:
|
|
1569
|
+
// await this.deleverageRepayStablecoins(deleverageCollateral) + stablecoin > debt
|
|
1570
|
+
// There is no deleverage zap
|
|
1571
|
+
if (this.deleverageZap === "0x0000000000000000000000000000000000000000")
|
|
1572
|
+
return false;
|
|
1573
|
+
address = _getAddress(address);
|
|
1574
|
+
const { collateral, stablecoin, debt } = yield this.userState(address);
|
|
1575
|
+
// Loan does not exist
|
|
1576
|
+
if (BN(debt).eq(0))
|
|
1577
|
+
return false;
|
|
1578
|
+
// Can't spend more than user has
|
|
1579
|
+
if (BN(deleverageCollateral).gt(collateral))
|
|
1580
|
+
return false;
|
|
1581
|
+
// Only full repayment and closing the position is available if user is underwater+
|
|
1582
|
+
if (BN(stablecoin).gt(0))
|
|
1583
|
+
return yield this.deleverageIsFullRepayment(deleverageCollateral, address);
|
|
1584
|
+
return true;
|
|
1585
|
+
});
|
|
1586
|
+
}
|
|
1587
|
+
deleverageRepayBands(collateral_1) {
|
|
1588
|
+
return __awaiter(this, arguments, void 0, function* (collateral, address = "") {
|
|
1589
|
+
this._checkDeleverageZap();
|
|
1590
|
+
const [_n2, _n1] = yield this._deleverageRepayBands(collateral, address);
|
|
1591
|
+
return [Number(_n2), Number(_n1)];
|
|
1592
|
+
});
|
|
1593
|
+
}
|
|
1594
|
+
deleverageRepayPrices(debt_1) {
|
|
1595
|
+
return __awaiter(this, arguments, void 0, function* (debt, address = "") {
|
|
1596
|
+
this._checkDeleverageZap();
|
|
1597
|
+
const [_n2, _n1] = yield this._deleverageRepayBands(debt, address);
|
|
1598
|
+
return yield this._getPrices(_n2, _n1);
|
|
1599
|
+
});
|
|
1600
|
+
}
|
|
1601
|
+
deleverageRepayHealth(collateral_1) {
|
|
1602
|
+
return __awaiter(this, arguments, void 0, function* (collateral, full = true, address = "") {
|
|
1603
|
+
var _a;
|
|
1604
|
+
this._checkDeleverageZap();
|
|
1605
|
+
address = _getAddress(address);
|
|
1606
|
+
if (!(yield this.deleverageIsAvailable(collateral, address)))
|
|
1607
|
+
return "0.0";
|
|
1608
|
+
const { _stablecoin, _debt } = yield this._userState(address);
|
|
1609
|
+
const { stablecoins: deleverageStablecoins } = yield this.deleverageRepayStablecoins(collateral);
|
|
1610
|
+
const _d_collateral = parseUnits(collateral, this.collateralDecimals) * BigInt(-1);
|
|
1611
|
+
const _d_debt = (parseUnits(deleverageStablecoins) + _stablecoin) * BigInt(-1);
|
|
1612
|
+
const N = yield this.userRange(address);
|
|
1613
|
+
if ((_debt + _d_debt) < 0)
|
|
1614
|
+
return "0.0";
|
|
1615
|
+
const contract = llamalend.contracts[(_a = this.healthCalculator) !== null && _a !== void 0 ? _a : this.controller].contract;
|
|
1616
|
+
let _health = yield contract.health_calculator(address, _d_collateral, _d_debt, full, N, llamalend.constantOptions);
|
|
1617
|
+
_health = _health * BigInt(100);
|
|
1618
|
+
return formatUnits(_health);
|
|
1619
|
+
});
|
|
1620
|
+
}
|
|
1621
|
+
deleveragePriceImpact(collateral) {
|
|
1622
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1623
|
+
const x_BN = BN(collateral);
|
|
1624
|
+
const small_x_BN = BN(0.001);
|
|
1625
|
+
const { stablecoins, routeIdx } = yield this.deleverageRepayStablecoins(collateral);
|
|
1626
|
+
const _y = parseUnits(stablecoins);
|
|
1627
|
+
const _small_y = yield llamalend.contracts[this.deleverageZap].contract.get_stablecoins(fromBN(small_x_BN, this.collateralDecimals), routeIdx);
|
|
1628
|
+
const y_BN = toBN(_y);
|
|
1629
|
+
const small_y_BN = toBN(_small_y);
|
|
1630
|
+
const rateBN = y_BN.div(x_BN);
|
|
1631
|
+
const smallRateBN = small_y_BN.div(small_x_BN);
|
|
1632
|
+
if (rateBN.gt(smallRateBN))
|
|
1633
|
+
return "0.0";
|
|
1634
|
+
return BN(1).minus(rateBN.div(smallRateBN)).times(100).toFixed(4);
|
|
1635
|
+
});
|
|
1636
|
+
}
|
|
1637
|
+
_deleverageRepay(collateral, slippage, estimateGas) {
|
|
1638
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1639
|
+
const { debt: currentDebt } = yield this.userState(llamalend.signerAddress);
|
|
1640
|
+
if (Number(currentDebt) === 0)
|
|
1641
|
+
throw Error(`Loan for ${llamalend.signerAddress} does not exist`);
|
|
1642
|
+
const { stablecoins, routeIdx } = yield this.deleverageRepayStablecoins(collateral);
|
|
1643
|
+
const _collateral = parseUnits(collateral, this.collateralDecimals);
|
|
1644
|
+
const _debt = parseUnits(stablecoins);
|
|
1645
|
+
const minRecvBN = toBN(_debt).times(100 - slippage).div(100);
|
|
1646
|
+
const _minRecv = fromBN(minRecvBN);
|
|
1647
|
+
const contract = llamalend.contracts[this.controller].contract;
|
|
1648
|
+
const gas = yield contract.repay_extended.estimateGas(this.deleverageZap, [routeIdx, _collateral, _minRecv], llamalend.constantOptions);
|
|
1649
|
+
if (estimateGas)
|
|
1650
|
+
return smartNumber(gas);
|
|
1651
|
+
yield llamalend.updateFeeData();
|
|
1652
|
+
const gasLimit = _mulBy1_3(DIGas(gas));
|
|
1653
|
+
return (yield contract.repay_extended(this.deleverageZap, [routeIdx, _collateral, _minRecv], Object.assign(Object.assign({}, llamalend.options), { gasLimit }))).hash;
|
|
1654
|
+
});
|
|
1655
|
+
}
|
|
1656
|
+
deleverageRepayEstimateGas(collateral_1) {
|
|
1657
|
+
return __awaiter(this, arguments, void 0, function* (collateral, slippage = 0.1) {
|
|
1658
|
+
this._checkDeleverageZap();
|
|
1659
|
+
return yield this._deleverageRepay(collateral, slippage, true);
|
|
1660
|
+
});
|
|
1661
|
+
}
|
|
1662
|
+
deleverageRepay(collateral_1) {
|
|
1663
|
+
return __awaiter(this, arguments, void 0, function* (collateral, slippage = 0.1) {
|
|
1664
|
+
this._checkDeleverageZap();
|
|
1665
|
+
return yield this._deleverageRepay(collateral, slippage, false);
|
|
1666
|
+
});
|
|
1667
|
+
}
|
|
1668
|
+
}
|