carbon-js-sdk 0.2.14-dev.4 → 0.2.14-dev.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/lib/codec/cdp/asset_params.d.ts +1 -1
- package/lib/codec/cdp/asset_params.js +11 -10
- package/lib/codec/cdp/genesis.d.ts +18 -1
- package/lib/codec/cdp/genesis.js +153 -37
- package/lib/codec/cdp/tx.d.ts +0 -1
- package/lib/codec/cdp/tx.js +6 -20
- package/lib/codec/cosmos/authz/v1beta1/genesis.d.ts +1 -1
- package/lib/codec/cosmos/authz/v1beta1/genesis.js +1 -1
- package/lib/codec/cosmos/authz/v1beta1/query.d.ts +1 -1
- package/lib/codec/cosmos/authz/v1beta1/query.js +1 -1
- package/lib/codec/cosmos/tx/v1beta1/service.d.ts +1 -1
- package/lib/codec/cosmos/tx/v1beta1/service.js +1 -1
- package/lib/codec/index.d.ts +1 -0
- package/lib/codec/index.js +3 -1
- package/lib/codec/oracle/genesis.d.ts +3 -0
- package/lib/codec/oracle/genesis.js +33 -2
- package/lib/codec/pricing/genesis.d.ts +1 -1
- package/lib/codec/pricing/genesis.js +8 -8
- package/lib/codec/pricing/legacy.d.ts +22 -0
- package/lib/codec/pricing/legacy.js +93 -0
- package/lib/modules/admin.d.ts +1 -3
- package/lib/modules/admin.js +2 -3
- package/lib/modules/cdp.d.ts +24 -14
- package/lib/modules/cdp.js +196 -161
- package/lib/util/address.d.ts +1 -0
- package/lib/util/address.js +8 -0
- package/lib/websocket/channel.js +29 -29
- package/lib/websocket/models.d.ts +1 -2
- package/lib/websocket/types.d.ts +0 -1
- package/package.json +1 -1
package/lib/modules/cdp.js
CHANGED
|
@@ -23,12 +23,9 @@ const bignumber_js_1 = require("bignumber.js");
|
|
|
23
23
|
const query_3 = require("./../codec/cdp/query");
|
|
24
24
|
const base_1 = __importDefault(require("./base"));
|
|
25
25
|
const constant_1 = require("../constant");
|
|
26
|
+
const TokenClient_1 = __importDefault(require("../clients/TokenClient"));
|
|
27
|
+
const address_1 = require("../util/address");
|
|
26
28
|
class CDPModule extends base_1.default {
|
|
27
|
-
constructor() {
|
|
28
|
-
super(...arguments);
|
|
29
|
-
this.cdpModuleAddress = "";
|
|
30
|
-
this.collateralPoolAddress = "";
|
|
31
|
-
}
|
|
32
29
|
supplyAsset(params, opts) {
|
|
33
30
|
return __awaiter(this, void 0, void 0, function* () {
|
|
34
31
|
const wallet = this.getWallet();
|
|
@@ -208,7 +205,6 @@ class CDPModule extends base_1.default {
|
|
|
208
205
|
minCollateralAmount: params.minCollateralAmount.toString(10),
|
|
209
206
|
debtDenom: params.debtDenom,
|
|
210
207
|
debtAmount: params.debtAmount.toString(10),
|
|
211
|
-
principalAmount: params.principalAmount.toString(10),
|
|
212
208
|
interestDenom: params.interestDenom,
|
|
213
209
|
interestAmount: params.interestAmount.toString(10),
|
|
214
210
|
});
|
|
@@ -305,6 +301,26 @@ class CDPModule extends base_1.default {
|
|
|
305
301
|
}, opts);
|
|
306
302
|
});
|
|
307
303
|
}
|
|
304
|
+
createRewardScheme(params, opts) {
|
|
305
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
306
|
+
const wallet = this.getWallet();
|
|
307
|
+
const value = tx_1.MsgCreateRewardScheme.fromPartial({
|
|
308
|
+
creator: wallet.bech32Address,
|
|
309
|
+
createRewardSchemeParams: {
|
|
310
|
+
rewardDenom: params.rewardDenom,
|
|
311
|
+
rewardType: params.rewardType,
|
|
312
|
+
assetDenom: params.assetDenom,
|
|
313
|
+
rewardAmountPerSecond: params.rewardAmountPerSecond.toString(10),
|
|
314
|
+
startTime: params.startTime,
|
|
315
|
+
endTime: params.endTime,
|
|
316
|
+
}
|
|
317
|
+
});
|
|
318
|
+
return yield wallet.sendTx({
|
|
319
|
+
typeUrl: util_1.CarbonTx.Types.MsgCreateRewardScheme,
|
|
320
|
+
value,
|
|
321
|
+
}, opts);
|
|
322
|
+
});
|
|
323
|
+
}
|
|
308
324
|
// start of cdp calculations
|
|
309
325
|
getAccountData(account) {
|
|
310
326
|
var _a;
|
|
@@ -322,20 +338,20 @@ class CDPModule extends base_1.default {
|
|
|
322
338
|
for (let i = 0; i < collaterals.length; i++) {
|
|
323
339
|
const amount = (0, number_1.bnOrZero)(collaterals[i].collateralAmount);
|
|
324
340
|
if (amount.isZero()) {
|
|
325
|
-
continue;
|
|
341
|
+
continue; // no collateral for denom
|
|
326
342
|
}
|
|
327
343
|
const denom = collaterals[i].denom;
|
|
328
344
|
const debtInfo = debtInfos.find(d => d.denom === denom);
|
|
329
345
|
if (!debtInfo) {
|
|
330
|
-
|
|
346
|
+
continue; // no debt for denom
|
|
331
347
|
}
|
|
332
348
|
const collateralUsdVal = yield this.getCdpTokenUsdVal(collaterals[i].cdpDenom, amount);
|
|
333
349
|
if (!collateralUsdVal) {
|
|
334
|
-
|
|
350
|
+
continue;
|
|
335
351
|
}
|
|
336
352
|
const assetParam = assetParams.find(a => a.denom === denom);
|
|
337
353
|
if (!assetParam) {
|
|
338
|
-
|
|
354
|
+
continue;
|
|
339
355
|
}
|
|
340
356
|
const ltv = (0, number_1.bnOrZero)(assetParam.loanToValue).div(number_1.BN_10000);
|
|
341
357
|
const availableBorrowUsd = collateralUsdVal.times(ltv);
|
|
@@ -357,25 +373,25 @@ class CDPModule extends base_1.default {
|
|
|
357
373
|
}
|
|
358
374
|
const debtInfo = debtInfos.find(d => d.denom === denom);
|
|
359
375
|
if (!debtInfo) {
|
|
360
|
-
|
|
376
|
+
continue;
|
|
361
377
|
}
|
|
362
378
|
const tokenDebtUsdVal = yield this.getTotalAccountTokenDebtUsdVal(account, denom, debts[i], debtInfo);
|
|
363
379
|
if (!tokenDebtUsdVal) {
|
|
364
|
-
|
|
380
|
+
continue;
|
|
365
381
|
}
|
|
366
382
|
totalDebtsUsd = totalDebtsUsd.plus(tokenDebtUsdVal);
|
|
367
383
|
}
|
|
368
384
|
// add stablecoin debt
|
|
369
385
|
const debtInfoRsp = yield sdk.query.cdp.StablecoinDebt(query_1.QueryStablecoinDebtRequest.fromPartial({}));
|
|
370
386
|
const stablecoinDebtInfo = debtInfoRsp.stablecoinDebtInfo;
|
|
371
|
-
|
|
372
|
-
|
|
387
|
+
let stablecoinDebtUsd = number_1.BN_ZERO;
|
|
388
|
+
if (stablecoinDebtInfo) {
|
|
389
|
+
const accountStablecoin = yield sdk.query.cdp.AccountStablecoin({ address: account });
|
|
390
|
+
const stablecoinDecimals = (_a = yield this.sdkProvider.getTokenClient().getDecimals(stablecoinDebtInfo.denom)) !== null && _a !== void 0 ? _a : number_1.BN_ZERO;
|
|
391
|
+
const stablecoinDebtAmount = (0, number_1.bnOrZero)(accountStablecoin.principalDebt).plus((0, number_1.bnOrZero)(accountStablecoin.interestDebt));
|
|
392
|
+
stablecoinDebtUsd = stablecoinDebtAmount.shiftedBy(-stablecoinDecimals);
|
|
393
|
+
totalDebtsUsd = totalDebtsUsd.plus(stablecoinDebtUsd);
|
|
373
394
|
}
|
|
374
|
-
const accountStablecoin = yield sdk.query.cdp.AccountStablecoin({ address: account });
|
|
375
|
-
const stablecoinDecimals = (_a = yield this.sdkProvider.getTokenClient().getDecimals(stablecoinDebtInfo.denom)) !== null && _a !== void 0 ? _a : number_1.BN_ZERO;
|
|
376
|
-
const stablecoinDebtAmount = (0, number_1.bnOrZero)(accountStablecoin.principalDebt).plus((0, number_1.bnOrZero)(accountStablecoin.interestDebt));
|
|
377
|
-
const stablecoinDebtUsd = stablecoinDebtAmount.shiftedBy(-stablecoinDecimals);
|
|
378
|
-
totalDebtsUsd = totalDebtsUsd.plus(stablecoinDebtUsd);
|
|
379
395
|
const healthFactor = currLiquidationThreshold.div(totalDebtsUsd);
|
|
380
396
|
return {
|
|
381
397
|
TotalCollateralsUsd: totalCollateralsUsd,
|
|
@@ -388,58 +404,38 @@ class CDPModule extends base_1.default {
|
|
|
388
404
|
});
|
|
389
405
|
}
|
|
390
406
|
getAssetBorrowableSupply(denom) {
|
|
407
|
+
var _a;
|
|
391
408
|
return __awaiter(this, void 0, void 0, function* () {
|
|
392
409
|
const sdk = this.sdkProvider;
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
}
|
|
397
|
-
const balanceRsp = yield sdk.query.bank.Balance(query_2.QueryBalanceRequest.fromPartial({ address: this.cdpModuleAddress, denom }));
|
|
398
|
-
if (!balanceRsp.balance) {
|
|
399
|
-
return;
|
|
400
|
-
}
|
|
401
|
-
return (0, number_1.bnOrZero)(balanceRsp.balance.amount);
|
|
410
|
+
const cdpAddress = this.getCdpModuleAddress();
|
|
411
|
+
const balanceRsp = yield sdk.query.bank.Balance(query_2.QueryBalanceRequest.fromPartial({ address: cdpAddress, denom }));
|
|
412
|
+
return (0, number_1.bnOrZero)((_a = balanceRsp.balance) === null || _a === void 0 ? void 0 : _a.amount);
|
|
402
413
|
});
|
|
403
414
|
}
|
|
404
415
|
getCdpToActualRatio(cdpDenom) {
|
|
405
416
|
return __awaiter(this, void 0, void 0, function* () {
|
|
406
417
|
const sdk = this.sdkProvider;
|
|
407
418
|
const denom = this.getUnderlyingDenom(cdpDenom);
|
|
408
|
-
if (!denom) {
|
|
409
|
-
return;
|
|
410
|
-
}
|
|
411
419
|
const supplyRsp = yield sdk.query.bank.SupplyOf(query_2.QuerySupplyOfRequest.fromPartial({ denom: cdpDenom }));
|
|
412
420
|
const cdpAmountRsp = supplyRsp.amount;
|
|
413
|
-
if (!cdpAmountRsp)
|
|
414
|
-
|
|
415
|
-
}
|
|
421
|
+
if (!cdpAmountRsp)
|
|
422
|
+
throw new Error("unable to retrieve cdp token supply");
|
|
416
423
|
const cdpAmount = (0, number_1.bnOrZero)(cdpAmountRsp.amount);
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
const balanceRsp = yield sdk.query.bank.Balance(query_2.QueryBalanceRequest.fromPartial({ address: this.cdpModuleAddress, denom }));
|
|
422
|
-
if (!balanceRsp.balance) {
|
|
423
|
-
return;
|
|
424
|
-
}
|
|
425
|
-
let actualAmount = (0, number_1.bnOrZero)(balanceRsp.balance.amount);
|
|
424
|
+
const cdpAddress = this.getCdpModuleAddress();
|
|
425
|
+
const balanceRsp = yield sdk.query.bank.Balance(query_2.QueryBalanceRequest.fromPartial({ address: cdpAddress, denom }));
|
|
426
|
+
if (!balanceRsp.balance)
|
|
427
|
+
throw new Error("unable to retrieve cdp module balance");
|
|
426
428
|
const owedAmount = yield this.getTotalTokenDebt(denom);
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
const ratio = cdpAmount.div(actualAmount);
|
|
432
|
-
return ratio;
|
|
429
|
+
const actualAmount = (0, number_1.bnOrZero)(balanceRsp.balance.amount).plus(owedAmount);
|
|
430
|
+
if (!owedAmount)
|
|
431
|
+
throw new Error("unable to retrieve total token debt");
|
|
432
|
+
return cdpAmount.div(actualAmount);
|
|
433
433
|
});
|
|
434
434
|
}
|
|
435
435
|
getTotalAccountTokenDebtUsdVal(account, denom, debt, debtInfo) {
|
|
436
436
|
return __awaiter(this, void 0, void 0, function* () {
|
|
437
|
-
const amount = yield this.getTotalAccountTokenDebt(account, denom, debt, debtInfo)
|
|
438
|
-
|
|
439
|
-
return;
|
|
440
|
-
}
|
|
441
|
-
const tokenDebtUsdVal = yield this.getTokenUsdVal(denom, amount).catch((err) => console.log(err));
|
|
442
|
-
return tokenDebtUsdVal;
|
|
437
|
+
const amount = yield this.getTotalAccountTokenDebt(account, denom, debt, debtInfo);
|
|
438
|
+
return yield this.getTokenUsdVal(denom, amount);
|
|
443
439
|
});
|
|
444
440
|
}
|
|
445
441
|
getModuleTotalDebtUsdVal() {
|
|
@@ -463,29 +459,28 @@ class CDPModule extends base_1.default {
|
|
|
463
459
|
}
|
|
464
460
|
// get stablecoin debt
|
|
465
461
|
const stablecoinDebtRes = yield this.sdkProvider.query.cdp.StablecoinDebt({});
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
462
|
+
if (stablecoinDebtRes.stablecoinDebtInfo) {
|
|
463
|
+
const debtInfo = stablecoinDebtRes.stablecoinDebtInfo;
|
|
464
|
+
const debtAmt = (0, number_1.bnOrZero)(debtInfo.totalPrincipal).plus((0, number_1.bnOrZero)(debtInfo.totalAccumulatedInterest));
|
|
465
|
+
const stablecoinDecimals = (_a = yield sdk.getTokenClient().getDecimals(debtInfo.denom)) !== null && _a !== void 0 ? _a : 0;
|
|
466
|
+
const debtUsdVal = (debtAmt).shiftedBy(-stablecoinDecimals);
|
|
467
|
+
totalDebt = totalDebt.plus(debtUsdVal);
|
|
469
468
|
}
|
|
470
|
-
const debtAmt = (0, number_1.bnOrZero)(stablecoinDebtInfo.totalPrincipal).plus((0, number_1.bnOrZero)(stablecoinDebtInfo.totalAccumulatedInterest));
|
|
471
|
-
const stablecoinDecimals = (_a = yield sdk.getTokenClient().getDecimals(stablecoinDebtInfo.denom)) !== null && _a !== void 0 ? _a : number_1.BN_ZERO;
|
|
472
|
-
const debtUsdVal = (debtAmt).shiftedBy(-stablecoinDecimals);
|
|
473
|
-
totalDebt = totalDebt.plus(debtUsdVal);
|
|
474
469
|
return totalDebt;
|
|
475
470
|
});
|
|
476
471
|
}
|
|
477
472
|
getModuleTotalCollateralUsdVal() {
|
|
478
473
|
return __awaiter(this, void 0, void 0, function* () {
|
|
479
|
-
const
|
|
480
|
-
const
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
if (!collateralUsdValue) {
|
|
487
|
-
return;
|
|
474
|
+
const network = this.sdkProvider.getConfig().network;
|
|
475
|
+
const collateralPoolAddress = address_1.SWTHAddress.getModuleAddress("collateral_pool", network);
|
|
476
|
+
const cdpBalances = yield this.sdkProvider.query.bank.AllBalances({ address: collateralPoolAddress });
|
|
477
|
+
let allCollateralsUsdValue = number_1.BN_ZERO;
|
|
478
|
+
for (const balance of cdpBalances.balances) {
|
|
479
|
+
if (!TokenClient_1.default.isCdpToken(balance.denom)) {
|
|
480
|
+
continue;
|
|
488
481
|
}
|
|
482
|
+
const amount = (0, number_1.bnOrZero)(balance.amount);
|
|
483
|
+
const collateralUsdValue = yield this.getCdpTokenUsdVal(balance.denom, amount);
|
|
489
484
|
allCollateralsUsdValue = allCollateralsUsdValue.plus(collateralUsdValue);
|
|
490
485
|
}
|
|
491
486
|
return allCollateralsUsdValue;
|
|
@@ -494,13 +489,7 @@ class CDPModule extends base_1.default {
|
|
|
494
489
|
getCdpTokenUsdVal(cdpDenom, amount) {
|
|
495
490
|
return __awaiter(this, void 0, void 0, function* () {
|
|
496
491
|
const denom = this.getUnderlyingDenom(cdpDenom);
|
|
497
|
-
if (!denom) {
|
|
498
|
-
return;
|
|
499
|
-
}
|
|
500
492
|
const ratio = yield this.getCdpToActualRatio(cdpDenom);
|
|
501
|
-
if (!ratio) {
|
|
502
|
-
return;
|
|
503
|
-
}
|
|
504
493
|
const actualTokenAmount = amount.div(ratio);
|
|
505
494
|
return yield this.getTokenUsdVal(denom, actualTokenAmount);
|
|
506
495
|
});
|
|
@@ -509,39 +498,30 @@ class CDPModule extends base_1.default {
|
|
|
509
498
|
return __awaiter(this, void 0, void 0, function* () {
|
|
510
499
|
const sdk = this.sdkProvider;
|
|
511
500
|
const decimals = yield this.sdkProvider.getTokenClient().getDecimals(denom);
|
|
512
|
-
if (
|
|
513
|
-
|
|
514
|
-
}
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
const twap = (0, number_1.bnOrZero)(price.tokenPrice.twap).shiftedBy(18 * (-1));
|
|
520
|
-
return amount.times(twap).shiftedBy(decimals * (-1));
|
|
501
|
+
if (decimals === undefined)
|
|
502
|
+
throw new Error("unable to retrieve token decimals for " + denom);
|
|
503
|
+
const priceResult = yield sdk.query.pricing.TokenPrice(codec_1.QueryTokenPriceRequest.fromPartial({ denom }));
|
|
504
|
+
if (!priceResult.tokenPrice)
|
|
505
|
+
throw new Error("unable to retrieve token price for " + denom);
|
|
506
|
+
const twap = (0, number_1.bnOrZero)(priceResult.tokenPrice.twap).shiftedBy(-18);
|
|
507
|
+
return amount.multipliedBy(twap).shiftedBy(-decimals);
|
|
521
508
|
});
|
|
522
509
|
}
|
|
523
510
|
getTotalTokenDebt(denom, debtInfo) {
|
|
511
|
+
var _a;
|
|
524
512
|
return __awaiter(this, void 0, void 0, function* () {
|
|
525
513
|
if (!debtInfo) {
|
|
526
514
|
const debtInfoRsp = yield this.sdkProvider.query.cdp.TokenDebt(query_1.QueryTokenDebtRequest.fromPartial({ denom }));
|
|
527
515
|
debtInfo = debtInfoRsp.debtInfo;
|
|
528
516
|
}
|
|
529
|
-
if (!debtInfo)
|
|
530
|
-
|
|
531
|
-
}
|
|
532
|
-
const cim = yield this.recalculateCIM(denom, debtInfo);
|
|
533
|
-
if (!cim) {
|
|
534
|
-
return;
|
|
535
|
-
}
|
|
517
|
+
if (!debtInfo)
|
|
518
|
+
throw new Error("unable to retrieve debt info");
|
|
536
519
|
const principal = (0, number_1.bnOrZero)(debtInfo.totalPrincipal);
|
|
537
|
-
const
|
|
520
|
+
const accumInterest = (0, number_1.bnOrZero)(debtInfo.totalAccumulatedInterest);
|
|
538
521
|
const cdpParamsRsp = yield this.sdkProvider.query.cdp.Params(query_1.QueryParamsRequest.fromPartial({}));
|
|
539
|
-
const
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
}
|
|
543
|
-
const interestFee = (0, number_1.bnOrZero)(cdpParams.interestFee).div(10000);
|
|
544
|
-
return principal.plus(interest.times((0, number_1.bnOrZero)(1).minus(interestFee)));
|
|
522
|
+
const interestFee = (0, number_1.bnOrZero)((_a = cdpParamsRsp.params) === null || _a === void 0 ? void 0 : _a.interestFee);
|
|
523
|
+
const interest = accumInterest.times(number_1.BN_10000.minus(interestFee)).dividedToIntegerBy(number_1.BN_10000);
|
|
524
|
+
return principal.plus(interest);
|
|
545
525
|
});
|
|
546
526
|
}
|
|
547
527
|
getTotalAccountTokenDebt(account, denom, debt, debtInfo) {
|
|
@@ -551,22 +531,19 @@ class CDPModule extends base_1.default {
|
|
|
551
531
|
const debtInfoRsp = yield sdk.query.cdp.TokenDebt(query_1.QueryTokenDebtRequest.fromPartial({ denom }));
|
|
552
532
|
debtInfo = debtInfoRsp.debtInfo;
|
|
553
533
|
}
|
|
554
|
-
if (!debtInfo)
|
|
555
|
-
return;
|
|
556
|
-
}
|
|
534
|
+
if (!debtInfo)
|
|
535
|
+
return number_1.BN_ZERO;
|
|
557
536
|
if (!debt) {
|
|
558
537
|
const debtRes = yield sdk.query.cdp.AccountDebt({ address: account, denom: denom });
|
|
559
538
|
debt = debtRes.debt;
|
|
560
539
|
}
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
const initialCIM = (0, number_1.bnOrZero)(debt.initialCumulativeInterestMultiplier);
|
|
540
|
+
const principalAmount = (0, number_1.bnOrZero)(debt === null || debt === void 0 ? void 0 : debt.principalDebt);
|
|
541
|
+
const initialCIM = (0, number_1.bnOrZero)(debt === null || debt === void 0 ? void 0 : debt.initialCumulativeInterestMultiplier);
|
|
542
|
+
if (principalAmount.isZero() || initialCIM.isZero())
|
|
543
|
+
return number_1.BN_ZERO;
|
|
566
544
|
const cim = yield this.recalculateCIM(denom, debtInfo);
|
|
567
|
-
if (!cim)
|
|
568
|
-
|
|
569
|
-
}
|
|
545
|
+
if (!cim)
|
|
546
|
+
throw new Error("unable to retrieve account debt");
|
|
570
547
|
// TODO: change to round up
|
|
571
548
|
const totalAmountTokenDebt = principalAmount.times(cim).dividedToIntegerBy(initialCIM);
|
|
572
549
|
return totalAmountTokenDebt;
|
|
@@ -580,24 +557,18 @@ class CDPModule extends base_1.default {
|
|
|
580
557
|
const debtInfoResponse = yield sdk.query.cdp.StablecoinDebt(query_1.QueryStablecoinDebtRequest.fromPartial({}));
|
|
581
558
|
debtInfo = debtInfoResponse.stablecoinDebtInfo;
|
|
582
559
|
}
|
|
583
|
-
if (!debtInfo)
|
|
560
|
+
if (!debtInfo)
|
|
584
561
|
return number_1.BN_ZERO;
|
|
585
|
-
}
|
|
586
562
|
if (!debt) {
|
|
587
563
|
const debtResp = yield sdk.query.cdp.AccountStablecoin(query_1.QueryAccountStablecoinRequest.fromPartial({ address: account }));
|
|
588
564
|
debt = debtResp;
|
|
589
565
|
}
|
|
590
|
-
if (!debt) {
|
|
591
|
-
return number_1.BN_ZERO;
|
|
592
|
-
}
|
|
593
566
|
principalAmount = (0, number_1.bnOrZero)(debt.principalDebt);
|
|
594
567
|
const initialCIM = (0, number_1.bnOrZero)(debt.initialCumulativeInterestMultiplier);
|
|
595
568
|
const cim = yield this.recalculateStablecoinCIM(debtInfo);
|
|
596
|
-
if (!cim)
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
const totalStablecoinDebtAmount = principalAmount.times(cim).dividedToIntegerBy(initialCIM);
|
|
600
|
-
return totalStablecoinDebtAmount;
|
|
569
|
+
if (!cim)
|
|
570
|
+
throw new Error("unable to retrieve account debt");
|
|
571
|
+
return principalAmount.times(cim).dividedToIntegerBy(initialCIM);
|
|
601
572
|
});
|
|
602
573
|
}
|
|
603
574
|
calculateAPY(denom, debtInfo, assetParams, rateStrategyParams) {
|
|
@@ -606,15 +577,14 @@ class CDPModule extends base_1.default {
|
|
|
606
577
|
if (!debtInfo) {
|
|
607
578
|
const debtInfoResponse = yield sdk.query.cdp.TokenDebt(query_1.QueryTokenDebtRequest.fromPartial({ denom }));
|
|
608
579
|
debtInfo = debtInfoResponse.debtInfo;
|
|
609
|
-
if (!debtInfo)
|
|
610
|
-
|
|
611
|
-
}
|
|
580
|
+
if (!debtInfo)
|
|
581
|
+
throw new Error("unable to retrieve debt info for " + denom);
|
|
612
582
|
}
|
|
613
583
|
if (!assetParams) {
|
|
614
584
|
const assetResponse = yield sdk.query.cdp.Asset(query_1.QueryAssetRequest.fromPartial({ denom }));
|
|
615
585
|
assetParams = assetResponse.assetParams;
|
|
616
586
|
if (!assetParams) {
|
|
617
|
-
|
|
587
|
+
throw new Error("unable to retrieve asset param for " + denom);
|
|
618
588
|
}
|
|
619
589
|
}
|
|
620
590
|
if (!rateStrategyParams) {
|
|
@@ -623,7 +593,7 @@ class CDPModule extends base_1.default {
|
|
|
623
593
|
}));
|
|
624
594
|
rateStrategyParams = rateStrategyParamsResponse.rateStrategyParams;
|
|
625
595
|
if (!rateStrategyParams) {
|
|
626
|
-
|
|
596
|
+
throw new Error("unable to retrieve rate strategy for " + denom);
|
|
627
597
|
}
|
|
628
598
|
}
|
|
629
599
|
const utilizationRate = (0, number_1.bnOrZero)(debtInfo.utilizationRate).shiftedBy(-18);
|
|
@@ -646,8 +616,7 @@ class CDPModule extends base_1.default {
|
|
|
646
616
|
}
|
|
647
617
|
const duration = (0, number_1.bnOrZero)(end.valueOf() - start.valueOf());
|
|
648
618
|
const millisecondsAYear = (0, number_1.bnOrZero)(31536000000);
|
|
649
|
-
|
|
650
|
-
return interest;
|
|
619
|
+
return duration.div(millisecondsAYear).times(apy);
|
|
651
620
|
}
|
|
652
621
|
recalculateCIM(denom, debtInfo) {
|
|
653
622
|
var _a;
|
|
@@ -693,12 +662,10 @@ class CDPModule extends base_1.default {
|
|
|
693
662
|
});
|
|
694
663
|
}
|
|
695
664
|
getMaxCollateralForUnlock(account, cdpDenom) {
|
|
696
|
-
var _a;
|
|
665
|
+
var _a, _b, _c, _d, _e;
|
|
697
666
|
return __awaiter(this, void 0, void 0, function* () {
|
|
698
667
|
const sdk = this.sdkProvider;
|
|
699
|
-
const denom =
|
|
700
|
-
if (!denom)
|
|
701
|
-
return;
|
|
668
|
+
const denom = this.getUnderlyingDenom(cdpDenom);
|
|
702
669
|
const assetParams = yield sdk.query.cdp.Asset({ denom: denom });
|
|
703
670
|
if (!assetParams.assetParams)
|
|
704
671
|
return;
|
|
@@ -707,54 +674,122 @@ class CDPModule extends base_1.default {
|
|
|
707
674
|
unlockRatio = new bignumber_js_1.BigNumber(assetParams.assetParams.liquidationThreshold);
|
|
708
675
|
}
|
|
709
676
|
const accountData = yield this.getAccountData(account);
|
|
710
|
-
|
|
711
|
-
return;
|
|
712
|
-
const tokenDecimals = yield sdk.getTokenClient().getDecimals(denom);
|
|
713
|
-
if (!tokenDecimals)
|
|
714
|
-
return;
|
|
677
|
+
const tokenDecimals = (_a = yield sdk.getTokenClient().getDecimals(denom)) !== null && _a !== void 0 ? _a : 0;
|
|
715
678
|
const availableBorrowsUsd = accountData.AvailableBorrowsUsd.minus(accountData.TotalDebtsUsd);
|
|
716
|
-
const unlockableUsd = availableBorrowsUsd.multipliedBy(
|
|
717
|
-
const tokenPrice = yield sdk.query.pricing.TokenPrice({ denom
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
679
|
+
const unlockableUsd = availableBorrowsUsd.multipliedBy(number_1.BN_10000).div(unlockRatio);
|
|
680
|
+
const tokenPrice = yield sdk.query.pricing.TokenPrice({ denom });
|
|
681
|
+
const tokenTwap = (0, number_1.bnOrZero)((_b = tokenPrice.tokenPrice) === null || _b === void 0 ? void 0 : _b.twap);
|
|
682
|
+
if (tokenTwap.isZero())
|
|
683
|
+
throw new Error("unable to retrieve token price for " + denom);
|
|
721
684
|
const tokenAmt = unlockableUsd.div(tokenTwap.shiftedBy(-18)).shiftedBy(tokenDecimals);
|
|
722
|
-
const cdpToActualRatio = (
|
|
685
|
+
const cdpToActualRatio = (_c = yield this.getCdpToActualRatio(cdpDenom)) !== null && _c !== void 0 ? _c : number_1.BN_ZERO;
|
|
723
686
|
const cdpTokenAmt = tokenAmt.multipliedBy(cdpToActualRatio);
|
|
724
687
|
// take the min of cdpTokensUnlockableAmt and locked tokens
|
|
725
688
|
const accountCollateral = yield sdk.query.cdp.AccountCollateral({
|
|
726
689
|
address: account,
|
|
727
690
|
cdpDenom: cdpDenom
|
|
728
691
|
});
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
const lockedAmount = new bignumber_js_1.BigNumber(accountCollateral.collateral.collateralAmount);
|
|
732
|
-
if (lockedAmount.lt(cdpTokenAmt)) {
|
|
733
|
-
return lockedAmount;
|
|
734
|
-
}
|
|
735
|
-
return cdpTokenAmt;
|
|
692
|
+
const lockedAmount = (0, number_1.bnOrZero)((_e = (_d = accountCollateral.collateral) === null || _d === void 0 ? void 0 : _d.collateralAmount) !== null && _e !== void 0 ? _e : "0");
|
|
693
|
+
return lockedAmount.lt(cdpTokenAmt) ? lockedAmount : cdpTokenAmt;
|
|
736
694
|
});
|
|
737
695
|
}
|
|
696
|
+
getCdpModuleAddress() {
|
|
697
|
+
if (!this.cdpModuleAddress) {
|
|
698
|
+
const network = this.sdkProvider.getConfig().network;
|
|
699
|
+
this.cdpModuleAddress = address_1.SWTHAddress.getModuleAddress("cdp", network);
|
|
700
|
+
}
|
|
701
|
+
return this.cdpModuleAddress;
|
|
702
|
+
}
|
|
738
703
|
getCdpTokenPrice(cdpDenom) {
|
|
739
|
-
var _a;
|
|
704
|
+
var _a, _b;
|
|
740
705
|
return __awaiter(this, void 0, void 0, function* () {
|
|
741
706
|
const sdk = this.sdkProvider;
|
|
742
|
-
const denom =
|
|
743
|
-
if (!denom) {
|
|
744
|
-
return;
|
|
745
|
-
}
|
|
707
|
+
const denom = this.getUnderlyingDenom(cdpDenom);
|
|
746
708
|
const cdpToActualRatio = (_a = yield this.getCdpToActualRatio(cdpDenom)) !== null && _a !== void 0 ? _a : number_1.BN_ZERO;
|
|
747
709
|
const tokenPrice = yield sdk.query.pricing.TokenPrice({ denom: denom });
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
}
|
|
751
|
-
const tokenTwap = new bignumber_js_1.BigNumber(tokenPrice.tokenPrice.twap);
|
|
752
|
-
return tokenTwap.multipliedBy(cdpToActualRatio).shiftedBy(-18);
|
|
710
|
+
const tokenTwap = (0, number_1.bnOrZero)((_b = tokenPrice.tokenPrice) === null || _b === void 0 ? void 0 : _b.twap).shiftedBy(-18);
|
|
711
|
+
return tokenTwap.multipliedBy(cdpToActualRatio);
|
|
753
712
|
});
|
|
754
713
|
}
|
|
755
714
|
getUnderlyingDenom(cdpDenom) {
|
|
756
715
|
var _a;
|
|
757
|
-
|
|
716
|
+
const denom = (_a = this.sdkProvider.getTokenClient().getCdpUnderlyingToken(cdpDenom)) === null || _a === void 0 ? void 0 : _a.denom;
|
|
717
|
+
if (!denom)
|
|
718
|
+
throw new Error("underlying denom not found for " + cdpDenom);
|
|
719
|
+
return denom;
|
|
720
|
+
}
|
|
721
|
+
// given a debt repayment amount, we get the max collateral that a liquidator can receive
|
|
722
|
+
// this takes into account the liquidation bonus and fees that will be deducted from his profit
|
|
723
|
+
getCollateralReceivableForLiquidation(debtDenom, debtAmount, cdpDenom) {
|
|
724
|
+
var _a, _b;
|
|
725
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
726
|
+
const sdk = this.sdkProvider;
|
|
727
|
+
// get the discounted price for the cdp token
|
|
728
|
+
const cdpActualDenom = this.getUnderlyingDenom(cdpDenom);
|
|
729
|
+
const asset = yield sdk.query.cdp.Asset({
|
|
730
|
+
denom: cdpActualDenom
|
|
731
|
+
});
|
|
732
|
+
if (!asset.assetParams)
|
|
733
|
+
throw new Error("unable to retrieve asset param for " + debtDenom);
|
|
734
|
+
const bonus = (0, number_1.bnOrZero)(asset.assetParams.liquidationDiscount).div(number_1.BN_10000);
|
|
735
|
+
const cdpTokenPrice = yield this.getCdpTokenPrice(cdpDenom);
|
|
736
|
+
const cdpTokenDiscountedPrice = cdpTokenPrice.multipliedBy(number_1.BN_ONE.minus(bonus));
|
|
737
|
+
// get cdp tokens (discounted) that can be gained from the debt amount
|
|
738
|
+
const debtValue = (_a = yield this.getTokenUsdVal(debtDenom, debtAmount)) !== null && _a !== void 0 ? _a : number_1.BN_ZERO;
|
|
739
|
+
const underlyingDenom = this.getUnderlyingDenom(cdpDenom);
|
|
740
|
+
const cdpTokenDecimals = (_b = yield sdk.getTokenClient().getDecimals(underlyingDenom)) !== null && _b !== void 0 ? _b : 0;
|
|
741
|
+
const cdpAmountWithDiscount = debtValue.div(cdpTokenDiscountedPrice).shiftedBy(cdpTokenDecimals);
|
|
742
|
+
// get cdp tokens (not discounted) that can be gained from the debt amount
|
|
743
|
+
const cdpAmountWithoutDiscount = debtValue.div(cdpTokenPrice).shiftedBy(cdpTokenDecimals);
|
|
744
|
+
// get fee amount
|
|
745
|
+
const cdpAmountProfit = cdpAmountWithDiscount.minus(cdpAmountWithoutDiscount);
|
|
746
|
+
const params = yield sdk.query.cdp.Params({});
|
|
747
|
+
if (!params.params)
|
|
748
|
+
throw new Error("unable to retrieve cdp params");
|
|
749
|
+
const feePercentage = (0, number_1.bnOrZero)(params.params.liquidationFee).div(number_1.BN_10000);
|
|
750
|
+
const feeAmount = cdpAmountProfit.multipliedBy(feePercentage);
|
|
751
|
+
// return collateral that can be received by liquidator
|
|
752
|
+
return cdpAmountWithDiscount.minus(feeAmount);
|
|
753
|
+
});
|
|
754
|
+
}
|
|
755
|
+
getCollateralReceivableForStablecoinLiq(cdpDenom, stablecoinRepayAmt, interestDenom, interestRepayAmt) {
|
|
756
|
+
var _a, _b, _c, _d;
|
|
757
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
758
|
+
const sdk = this.sdkProvider;
|
|
759
|
+
// get the discounted price for the cdp token
|
|
760
|
+
const cdpActualDenom = this.getUnderlyingDenom(cdpDenom);
|
|
761
|
+
const asset = yield sdk.query.cdp.Asset({
|
|
762
|
+
denom: cdpActualDenom
|
|
763
|
+
});
|
|
764
|
+
if (!asset.assetParams)
|
|
765
|
+
throw new Error("unable to retrieve asset param for " + cdpActualDenom);
|
|
766
|
+
const bonus = (0, number_1.bnOrZero)(asset.assetParams.liquidationDiscount).div(number_1.BN_10000);
|
|
767
|
+
const cdpTokenPrice = yield this.getCdpTokenPrice(cdpDenom);
|
|
768
|
+
const cdpTokenDiscountedPrice = cdpTokenPrice.multipliedBy(number_1.BN_ONE.minus(bonus));
|
|
769
|
+
// get value of stablecoin repayment + interest repayment
|
|
770
|
+
const debtInfoRsp = (_a = yield sdk.query.cdp.StablecoinDebt({})) !== null && _a !== void 0 ? _a : {};
|
|
771
|
+
if (!debtInfoRsp.stablecoinDebtInfo)
|
|
772
|
+
throw new Error("unable to retrieve stablecoin debt info");
|
|
773
|
+
const stablecoinDebtInfo = debtInfoRsp.stablecoinDebtInfo;
|
|
774
|
+
const stablecoinDecimals = (_b = yield this.sdkProvider.getTokenClient().getDecimals(stablecoinDebtInfo.denom)) !== null && _b !== void 0 ? _b : number_1.BN_ZERO;
|
|
775
|
+
const stablecoinRepaymentValue = stablecoinRepayAmt.shiftedBy(-stablecoinDecimals);
|
|
776
|
+
const interestRepaymentValue = (_c = yield this.getTokenUsdVal(interestDenom, interestRepayAmt)) !== null && _c !== void 0 ? _c : number_1.BN_ZERO;
|
|
777
|
+
const totalRepaymentValue = stablecoinRepaymentValue.plus(interestRepaymentValue);
|
|
778
|
+
// get cdp tokens (discounted) that can be gained from the debt amount
|
|
779
|
+
const cdpTokenDecimals = (_d = yield sdk.getTokenClient().getDecimals(cdpActualDenom)) !== null && _d !== void 0 ? _d : 0;
|
|
780
|
+
const cdpAmountWithDiscount = totalRepaymentValue.div(cdpTokenDiscountedPrice).shiftedBy(cdpTokenDecimals);
|
|
781
|
+
// get cdp tokens (not discounted) that can be gained from the debt amount
|
|
782
|
+
const cdpAmountWithoutDiscount = totalRepaymentValue.div(cdpTokenPrice).shiftedBy(cdpTokenDecimals);
|
|
783
|
+
// get fee amount
|
|
784
|
+
const cdpAmountProfit = cdpAmountWithDiscount.minus(cdpAmountWithoutDiscount);
|
|
785
|
+
const params = yield sdk.query.cdp.Params({});
|
|
786
|
+
if (!params.params)
|
|
787
|
+
throw new Error("unable to retrieve cdp params");
|
|
788
|
+
const feePercentage = (0, number_1.bnOrZero)(params.params.liquidationFee).div(number_1.BN_10000);
|
|
789
|
+
const feeAmount = cdpAmountProfit.multipliedBy(feePercentage);
|
|
790
|
+
// return collateral that can be received by liquidator
|
|
791
|
+
return cdpAmountWithDiscount.minus(feeAmount);
|
|
792
|
+
});
|
|
758
793
|
}
|
|
759
794
|
}
|
|
760
795
|
exports.CDPModule = CDPModule;
|
package/lib/util/address.d.ts
CHANGED
|
@@ -62,6 +62,7 @@ declare type SWTHAddressType = AddressBuilder<SWTHAddressOptions> & {
|
|
|
62
62
|
getAddressBytes(bech32Address: string, networkConfig: Network): Uint8Array;
|
|
63
63
|
keyDerivationPath(index?: number, change?: number, account?: number): number[];
|
|
64
64
|
encode(hash: string | Buffer, opts?: SWTHAddressOptions): string;
|
|
65
|
+
getModuleAddress(moduleKey: string, network?: Network): string;
|
|
65
66
|
};
|
|
66
67
|
export declare const SWTHAddress: SWTHAddressType;
|
|
67
68
|
declare type NEOAddressType = AddressBuilder<AddressOptions> & {
|
package/lib/util/address.js
CHANGED
|
@@ -18,6 +18,9 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
18
18
|
__setModuleDefault(result, mod);
|
|
19
19
|
return result;
|
|
20
20
|
};
|
|
21
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
22
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
23
|
+
};
|
|
21
24
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
25
|
exports.ETHAddress = exports.N3Address = exports.NEOAddress = exports.SWTHAddress = exports.wifEncodePrivateKey = exports.randomMnemonic = exports.BIP44Path = exports.stringOrBufferToBuffer = void 0;
|
|
23
26
|
const constant_1 = require("../constant");
|
|
@@ -26,6 +29,7 @@ const Base58Check = __importStar(require("base58check"));
|
|
|
26
29
|
const bech32 = __importStar(require("bech32"));
|
|
27
30
|
const BIP32 = __importStar(require("bip32"));
|
|
28
31
|
const BIP39 = __importStar(require("bip39"));
|
|
32
|
+
const crypto_js_1 = __importDefault(require("crypto-js"));
|
|
29
33
|
const ethers_1 = require("ethers");
|
|
30
34
|
const secp256k1 = __importStar(require("secp256k1"));
|
|
31
35
|
const secp256r1 = __importStar(require("secp256r1"));
|
|
@@ -189,6 +193,10 @@ exports.SWTHAddress = {
|
|
|
189
193
|
}
|
|
190
194
|
return new Uint8Array(bech32.fromWords(words));
|
|
191
195
|
},
|
|
196
|
+
getModuleAddress: (moduleKey, network = constant_1.Network.MainNet) => {
|
|
197
|
+
const addressHash = crypto_js_1.default.SHA256(moduleKey).toString(crypto_js_1.default.enc.Hex);
|
|
198
|
+
return exports.SWTHAddress.encode(addressHash, { network });
|
|
199
|
+
},
|
|
192
200
|
};
|
|
193
201
|
exports.NEOAddress = {
|
|
194
202
|
coinType: () => {
|