carbon-js-sdk 0.6.8 → 0.6.10
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/modules/cdp.d.ts +2 -4
- package/lib/modules/cdp.js +56 -33
- package/lib/modules/test.d.ts +0 -0
- package/lib/modules/test.js +1 -0
- package/package.json +1 -1
package/lib/modules/cdp.d.ts
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
/// <reference types="long" />
|
|
2
1
|
import { AssetParams, DebtInfo, RateStrategyParams, StablecoinDebtInfo, TokenPrice } from "../codec";
|
|
3
2
|
import { Params } from "../codec/cdp/params";
|
|
4
3
|
import { CarbonTx } from "../util";
|
|
5
4
|
import { BigNumber } from "bignumber.js";
|
|
5
|
+
import Long from "long";
|
|
6
6
|
import { Debt } from "./../codec/cdp/query";
|
|
7
7
|
import BaseModule from "./base";
|
|
8
|
-
import { Coin } from "../codec/cosmos/base/v1beta1/coin";
|
|
9
8
|
export declare class CDPModule extends BaseModule {
|
|
10
9
|
private cdpModuleAddress;
|
|
11
10
|
supplyAsset(params: CDPModule.SupplyAssetParams, opts?: CarbonTx.SignTxOpts): Promise<import("@cosmjs/stargate").DeliverTxResponse>;
|
|
@@ -54,10 +53,9 @@ export declare class CDPModule extends BaseModule {
|
|
|
54
53
|
getTotalAccountTokenDebtUsdVal(account: string, denom: string, debt?: Debt, debtInfo?: DebtInfo): Promise<BigNumber>;
|
|
55
54
|
getModuleTotalDebtUsdVal(): Promise<BigNumber>;
|
|
56
55
|
getModuleTotalCollateralUsdVal(): Promise<BigNumber>;
|
|
57
|
-
getCdpTokensUsdVal(cdpTokens: Coin[]): Promise<BigNumber>;
|
|
58
56
|
getCdpTokenUsdVal(cdpDenom: string, amount: BigNumber): Promise<BigNumber>;
|
|
59
57
|
getTokenUsdVal(denom: string, amount: BigNumber, price?: TokenPrice): Promise<BigNumber>;
|
|
60
|
-
getTotalTokenDebt(denom: string, interestFee?: BigNumber): Promise<BigNumber>;
|
|
58
|
+
getTotalTokenDebt(denom: string, debtInfo?: DebtInfo, interestFee?: BigNumber, newInterestRate?: BigNumber): Promise<BigNumber>;
|
|
61
59
|
getTotalAccountTokenDebt(account: string, denom: string, debt?: Debt, debtInfo?: DebtInfo): Promise<BigNumber>;
|
|
62
60
|
getTotalAccountStablecoinDebt(account: string, debt?: CDPModule.StablecoinDebt, debtInfo?: StablecoinDebtInfo): Promise<BigNumber>;
|
|
63
61
|
static calculateInterestAPY: (debtInfo: DebtInfo, rateStrategy: RateStrategyParams) => BigNumber;
|
package/lib/modules/cdp.js
CHANGED
|
@@ -18,11 +18,13 @@ const codec_1 = require("../codec");
|
|
|
18
18
|
const query_1 = require("../codec/cdp/query");
|
|
19
19
|
const tx_1 = require("../codec/cdp/tx");
|
|
20
20
|
const query_2 = require("../codec/cosmos/bank/v1beta1/query");
|
|
21
|
+
const pagination_1 = require("../codec/cosmos/base/query/v1beta1/pagination");
|
|
21
22
|
const constant_1 = require("../constant");
|
|
22
23
|
const util_1 = require("../util");
|
|
23
24
|
const address_1 = require("../util/address");
|
|
24
25
|
const number_1 = require("../util/number");
|
|
25
26
|
const bignumber_js_1 = require("bignumber.js");
|
|
27
|
+
const long_1 = __importDefault(require("long"));
|
|
26
28
|
const query_3 = require("./../codec/cdp/query");
|
|
27
29
|
const base_1 = __importDefault(require("./base"));
|
|
28
30
|
class CDPModule extends base_1.default {
|
|
@@ -601,14 +603,18 @@ class CDPModule extends base_1.default {
|
|
|
601
603
|
const cdpAddress = this.getCdpModuleAddress();
|
|
602
604
|
const supplyPromise = sdk.query.bank.SupplyOf(query_2.QuerySupplyOfRequest.fromPartial({ denom: cdpDenom }));
|
|
603
605
|
const balancePromise = sdk.query.bank.Balance(query_2.QueryBalanceRequest.fromPartial({ address: cdpAddress, denom }));
|
|
604
|
-
const
|
|
606
|
+
const debtInfoPromise = sdk.query.cdp.TokenDebt(query_1.QueryTokenDebtRequest.fromPartial({ denom }));
|
|
607
|
+
const [supplyRsp, balanceRsp, debtInfoRsp] = yield Promise.all([supplyPromise, balancePromise, debtInfoPromise]);
|
|
605
608
|
const cdpAmountRsp = supplyRsp.amount;
|
|
606
609
|
if (!cdpAmountRsp)
|
|
607
610
|
throw new Error("unable to retrieve cdp token supply");
|
|
608
611
|
const cdpAmount = (0, number_1.bnOrZero)(cdpAmountRsp.amount);
|
|
609
612
|
if (!balanceRsp.balance)
|
|
610
613
|
throw new Error("unable to retrieve cdp module balance");
|
|
611
|
-
const
|
|
614
|
+
const debtInfo = debtInfoRsp.debtInfo;
|
|
615
|
+
if (!debtInfo)
|
|
616
|
+
throw new Error("unable to retrieve debt info");
|
|
617
|
+
const owedAmount = yield this.getTotalTokenDebt(denom, debtInfo, interestFee);
|
|
612
618
|
const actualAmount = (0, number_1.bnOrZero)(balanceRsp.balance.amount).plus(owedAmount);
|
|
613
619
|
if (!owedAmount)
|
|
614
620
|
throw new Error("unable to retrieve total token debt");
|
|
@@ -658,43 +664,56 @@ class CDPModule extends base_1.default {
|
|
|
658
664
|
});
|
|
659
665
|
}
|
|
660
666
|
getModuleTotalCollateralUsdVal() {
|
|
661
|
-
var _a, _b;
|
|
667
|
+
var _a, _b, _c;
|
|
662
668
|
return __awaiter(this, void 0, void 0, function* () {
|
|
663
669
|
const sdk = this.sdkProvider;
|
|
664
670
|
const network = sdk.getConfig().network;
|
|
665
671
|
const collateralPoolAddress = address_1.SWTHAddress.getModuleAddress("collateral_pool", network);
|
|
666
|
-
const
|
|
667
|
-
const
|
|
668
|
-
const
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
}
|
|
672
|
-
getCdpTokensUsdVal(cdpTokens) {
|
|
673
|
-
var _a, _b;
|
|
674
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
675
|
-
const sdk = this.sdkProvider;
|
|
676
|
-
const tokenClient = sdk.getTokenClient();
|
|
672
|
+
const cdpModuleBalancesAddress = this.getCdpModuleAddress();
|
|
673
|
+
const maxPageLimit = { pagination: pagination_1.PageRequest.fromPartial({ limit: new long_1.default(10000) }) };
|
|
674
|
+
const collateralPoolBalancePromise = sdk.query.bank.AllBalances(Object.assign(Object.assign({}, maxPageLimit), { address: collateralPoolAddress }));
|
|
675
|
+
const cdpModuleBalancesPromise = sdk.query.bank.AllBalances(Object.assign(Object.assign({}, maxPageLimit), { address: cdpModuleBalancesAddress }));
|
|
676
|
+
const totalSupplyPromise = sdk.query.bank.TotalSupply(query_2.QueryTotalSupplyRequest.fromPartial(Object.assign({}, maxPageLimit)));
|
|
677
677
|
const cdpParamsPromise = sdk.query.cdp.Params(query_1.QueryParamsRequest.fromPartial({}));
|
|
678
|
-
const tokenPriceAllPromise = sdk.query.pricing.TokenPriceAll(codec_1.QueryTokenPriceAllRequest.fromPartial({}));
|
|
679
|
-
const
|
|
678
|
+
const tokenPriceAllPromise = sdk.query.pricing.TokenPriceAll(codec_1.QueryTokenPriceAllRequest.fromPartial(Object.assign({}, maxPageLimit)));
|
|
679
|
+
const debtInfosPromise = sdk.query.cdp.TokenDebtAll(query_3.QueryTokenDebtAllRequest.fromPartial(Object.assign({}, maxPageLimit)));
|
|
680
|
+
const assetParamsPromise = sdk.query.cdp.AssetAll(query_3.QueryAssetAllRequest.fromPartial(Object.assign({}, maxPageLimit)));
|
|
681
|
+
const rateStrategyPromise = sdk.query.cdp.RateStrategyAll(codec_1.QueryRateStrategyAllRequest.fromPartial(Object.assign({}, maxPageLimit)));
|
|
682
|
+
const [collateralPoolBalances, totalSupply, cdpParams, tokenPriceAll, debtInfosAll, assetParamsAll, rateStrategies, cdpModuleBalances] = yield Promise.all([collateralPoolBalancePromise, totalSupplyPromise, cdpParamsPromise, tokenPriceAllPromise, debtInfosPromise, assetParamsPromise, rateStrategyPromise, cdpModuleBalancesPromise]);
|
|
680
683
|
const interestFee = (0, number_1.bnOrZero)((_a = cdpParams.params) === null || _a === void 0 ? void 0 : _a.interestFee);
|
|
681
684
|
if (!interestFee)
|
|
682
685
|
throw new Error("unable to retrieve interest fee");
|
|
683
686
|
const tokenPrices = tokenPriceAll.tokenPrices;
|
|
684
687
|
if (!tokenPrices)
|
|
685
688
|
throw new Error("unable to retrieve token prices");
|
|
686
|
-
const
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
689
|
+
const moduleBalancesMap = cdpModuleBalances.balances.reduce((prev, moduleBalance) => {
|
|
690
|
+
if (!prev[moduleBalance.denom]) {
|
|
691
|
+
prev[moduleBalance.denom] = moduleBalance;
|
|
692
|
+
}
|
|
693
|
+
return prev;
|
|
694
|
+
}, {});
|
|
695
|
+
const cdpTokenBalances = ((_b = collateralPoolBalances === null || collateralPoolBalances === void 0 ? void 0 : collateralPoolBalances.balances) !== null && _b !== void 0 ? _b : []).filter(balance => TokenClient_1.default.isCdpToken(balance.denom));
|
|
696
|
+
const cdpTokensBalancePromises = cdpTokenBalances.map(token => {
|
|
697
|
+
var _a, _b;
|
|
698
|
+
const underlyingDenom = this.getUnderlyingDenom(token.denom);
|
|
699
|
+
const tokenPrice = tokenPrices.find((price) => price.denom === underlyingDenom);
|
|
700
|
+
const supply = (_a = totalSupply.supply.find((supply) => supply.denom === token.denom)) === null || _a === void 0 ? void 0 : _a.amount;
|
|
701
|
+
const balance = moduleBalancesMap[underlyingDenom].amount;
|
|
702
|
+
const debtInfo = debtInfosAll.debtInfosAll.find((debtInfo) => debtInfo.denom === underlyingDenom);
|
|
703
|
+
const assetParam = assetParamsAll.assetParamsAll.find((assetParam) => assetParam.denom === underlyingDenom);
|
|
704
|
+
const rateStrategy = rateStrategies.rateStrategyParamsAll.find((rateStrategy) => rateStrategy.name === (assetParam === null || assetParam === void 0 ? void 0 : assetParam.rateStrategyName));
|
|
705
|
+
if (!debtInfo || !supply || !tokenPrice || !rateStrategy || !balance)
|
|
706
|
+
throw new Error("unable to retrieve token info");
|
|
707
|
+
const apy = CDPModule.calculateInterestAPY(debtInfo, rateStrategy);
|
|
708
|
+
const newInterestRate = CDPModule.calculateInterestForTimePeriod(apy, (_b = debtInfo.lastUpdatedTime) !== null && _b !== void 0 ? _b : new Date(0), new Date());
|
|
709
|
+
return this.getTotalTokenDebt(underlyingDenom, debtInfo, interestFee, newInterestRate)
|
|
710
|
+
.then((totalDebt) => {
|
|
711
|
+
const ratio = (0, number_1.bnOrZero)(supply).div((0, number_1.bnOrZero)(balance).plus((0, number_1.bnOrZero)(totalDebt)));
|
|
712
|
+
const actualAmount = (0, number_1.bnOrZero)(token.amount).div(ratio);
|
|
713
|
+
return this.getTokenUsdVal(underlyingDenom, actualAmount, tokenPrice);
|
|
714
|
+
});
|
|
715
|
+
});
|
|
716
|
+
const cdpBalances = (_c = (yield Promise.all(cdpTokensBalancePromises))) !== null && _c !== void 0 ? _c : [];
|
|
698
717
|
const totalCdpTokensUsdVal = cdpBalances.reduce((prev, curr) => (prev.plus(curr)), number_1.BN_ZERO);
|
|
699
718
|
return totalCdpTokensUsdVal;
|
|
700
719
|
});
|
|
@@ -723,7 +742,7 @@ class CDPModule extends base_1.default {
|
|
|
723
742
|
return amount.multipliedBy(twap).shiftedBy(-decimals);
|
|
724
743
|
});
|
|
725
744
|
}
|
|
726
|
-
getTotalTokenDebt(denom, interestFee) {
|
|
745
|
+
getTotalTokenDebt(denom, debtInfo, interestFee, newInterestRate) {
|
|
727
746
|
var _a;
|
|
728
747
|
return __awaiter(this, void 0, void 0, function* () {
|
|
729
748
|
if (!interestFee) {
|
|
@@ -732,12 +751,16 @@ class CDPModule extends base_1.default {
|
|
|
732
751
|
}
|
|
733
752
|
if (!interestFee)
|
|
734
753
|
throw new Error("unable to retrieve interest fee");
|
|
735
|
-
|
|
736
|
-
|
|
754
|
+
if (!debtInfo) {
|
|
755
|
+
const debtInfoRsp = yield this.sdkProvider.query.cdp.TokenDebt(query_1.QueryTokenDebtRequest.fromPartial({ denom }));
|
|
756
|
+
debtInfo = debtInfoRsp.debtInfo;
|
|
757
|
+
}
|
|
737
758
|
if (!debtInfo)
|
|
738
759
|
throw new Error("unable to retrieve debt info");
|
|
739
|
-
|
|
740
|
-
|
|
760
|
+
if (!newInterestRate) {
|
|
761
|
+
const cimRsp = yield this.recalculateCIM(denom, debtInfo);
|
|
762
|
+
newInterestRate = cimRsp.interest;
|
|
763
|
+
}
|
|
741
764
|
const principal = (0, number_1.bnOrZero)(debtInfo.totalPrincipal);
|
|
742
765
|
const accumInterest = (0, number_1.bnOrZero)(debtInfo.totalAccumulatedInterest);
|
|
743
766
|
const newInterest = principal.times(newInterestRate).plus(accumInterest.times(number_1.BN_ONE.plus(newInterestRate)));
|
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";
|