carbon-js-sdk 0.6.8 → 0.6.9
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 +1 -3
- package/lib/modules/cdp.js +44 -32
- package/package.json +1 -1
package/lib/modules/cdp.d.ts
CHANGED
|
@@ -5,7 +5,6 @@ import { CarbonTx } from "../util";
|
|
|
5
5
|
import { BigNumber } from "bignumber.js";
|
|
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
|
@@ -601,14 +601,18 @@ class CDPModule extends base_1.default {
|
|
|
601
601
|
const cdpAddress = this.getCdpModuleAddress();
|
|
602
602
|
const supplyPromise = sdk.query.bank.SupplyOf(query_2.QuerySupplyOfRequest.fromPartial({ denom: cdpDenom }));
|
|
603
603
|
const balancePromise = sdk.query.bank.Balance(query_2.QueryBalanceRequest.fromPartial({ address: cdpAddress, denom }));
|
|
604
|
-
const
|
|
604
|
+
const debtInfoPromise = sdk.query.cdp.TokenDebt(query_1.QueryTokenDebtRequest.fromPartial({ denom }));
|
|
605
|
+
const [supplyRsp, balanceRsp, debtInfoRsp] = yield Promise.all([supplyPromise, balancePromise, debtInfoPromise]);
|
|
605
606
|
const cdpAmountRsp = supplyRsp.amount;
|
|
606
607
|
if (!cdpAmountRsp)
|
|
607
608
|
throw new Error("unable to retrieve cdp token supply");
|
|
608
609
|
const cdpAmount = (0, number_1.bnOrZero)(cdpAmountRsp.amount);
|
|
609
610
|
if (!balanceRsp.balance)
|
|
610
611
|
throw new Error("unable to retrieve cdp module balance");
|
|
611
|
-
const
|
|
612
|
+
const debtInfo = debtInfoRsp.debtInfo;
|
|
613
|
+
if (!debtInfo)
|
|
614
|
+
throw new Error("unable to retrieve debt info");
|
|
615
|
+
const owedAmount = yield this.getTotalTokenDebt(denom, debtInfo, interestFee);
|
|
612
616
|
const actualAmount = (0, number_1.bnOrZero)(balanceRsp.balance.amount).plus(owedAmount);
|
|
613
617
|
if (!owedAmount)
|
|
614
618
|
throw new Error("unable to retrieve total token debt");
|
|
@@ -658,43 +662,47 @@ class CDPModule extends base_1.default {
|
|
|
658
662
|
});
|
|
659
663
|
}
|
|
660
664
|
getModuleTotalCollateralUsdVal() {
|
|
661
|
-
var _a, _b;
|
|
665
|
+
var _a, _b, _c;
|
|
662
666
|
return __awaiter(this, void 0, void 0, function* () {
|
|
663
667
|
const sdk = this.sdkProvider;
|
|
664
668
|
const network = sdk.getConfig().network;
|
|
665
669
|
const collateralPoolAddress = address_1.SWTHAddress.getModuleAddress("collateral_pool", network);
|
|
666
|
-
const
|
|
667
|
-
const
|
|
668
|
-
const totalCollateralsUsdValue = (_b = (yield this.getCdpTokensUsdVal(cdpTokenBalances))) !== null && _b !== void 0 ? _b : [];
|
|
669
|
-
return totalCollateralsUsdValue;
|
|
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();
|
|
670
|
+
const collateralPoolBalancePromise = sdk.query.bank.AllBalances({ address: collateralPoolAddress });
|
|
671
|
+
const totalSupplyPromise = sdk.query.bank.TotalSupply(query_2.QueryTotalSupplyRequest.fromPartial({}));
|
|
677
672
|
const cdpParamsPromise = sdk.query.cdp.Params(query_1.QueryParamsRequest.fromPartial({}));
|
|
678
673
|
const tokenPriceAllPromise = sdk.query.pricing.TokenPriceAll(codec_1.QueryTokenPriceAllRequest.fromPartial({}));
|
|
679
|
-
const
|
|
674
|
+
const debtInfosPromise = sdk.query.cdp.TokenDebtAll(query_3.QueryTokenDebtAllRequest.fromPartial({}));
|
|
675
|
+
const assetParamsPromise = sdk.query.cdp.AssetAll(query_3.QueryAssetAllRequest.fromPartial({}));
|
|
676
|
+
const rateStrategyPromise = sdk.query.cdp.RateStrategyAll(codec_1.QueryRateStrategyAllRequest.fromPartial({}));
|
|
677
|
+
const [collateralPoolBalances, totalSupply, cdpParams, tokenPriceAll, debtInfosAll, assetParamsAll, rateStrategies] = yield Promise.all([collateralPoolBalancePromise, totalSupplyPromise, cdpParamsPromise, tokenPriceAllPromise, debtInfosPromise, assetParamsPromise, rateStrategyPromise]);
|
|
680
678
|
const interestFee = (0, number_1.bnOrZero)((_a = cdpParams.params) === null || _a === void 0 ? void 0 : _a.interestFee);
|
|
681
679
|
if (!interestFee)
|
|
682
680
|
throw new Error("unable to retrieve interest fee");
|
|
683
681
|
const tokenPrices = tokenPriceAll.tokenPrices;
|
|
684
682
|
if (!tokenPrices)
|
|
685
683
|
throw new Error("unable to retrieve token prices");
|
|
686
|
-
const
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
const
|
|
690
|
-
const tokenPrice = tokenPrices.find((price) => price.denom ===
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
.
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
684
|
+
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));
|
|
685
|
+
const cdpTokensBalancePromises = cdpTokenBalances.map(token => {
|
|
686
|
+
var _a, _b;
|
|
687
|
+
const underlyingDenom = this.getUnderlyingDenom(token.denom);
|
|
688
|
+
const tokenPrice = tokenPrices.find((price) => price.denom === underlyingDenom);
|
|
689
|
+
const supply = (_a = totalSupply.supply.find((supply) => supply.denom === token.denom)) === null || _a === void 0 ? void 0 : _a.amount;
|
|
690
|
+
const balance = token.amount;
|
|
691
|
+
const debtInfo = debtInfosAll.debtInfosAll.find((debtInfo) => debtInfo.denom === underlyingDenom);
|
|
692
|
+
const assetParam = assetParamsAll.assetParamsAll.find((assetParam) => assetParam.denom === underlyingDenom);
|
|
693
|
+
const rateStrategy = rateStrategies.rateStrategyParamsAll.find((rateStrategy) => rateStrategy.name === (assetParam === null || assetParam === void 0 ? void 0 : assetParam.rateStrategyName));
|
|
694
|
+
if (!debtInfo || !supply || !tokenPrice || !rateStrategy)
|
|
695
|
+
throw new Error("unable to retrieve token info");
|
|
696
|
+
const apy = CDPModule.calculateInterestAPY(debtInfo, rateStrategy);
|
|
697
|
+
const newInterestRate = CDPModule.calculateInterestForTimePeriod(apy, (_b = debtInfo.lastUpdatedTime) !== null && _b !== void 0 ? _b : new Date(0), new Date());
|
|
698
|
+
return this.getTotalTokenDebt(underlyingDenom, debtInfo, interestFee, newInterestRate)
|
|
699
|
+
.then((totalDebt) => {
|
|
700
|
+
const ratio = (0, number_1.bnOrZero)(supply).div((0, number_1.bnOrZero)(totalDebt));
|
|
701
|
+
const actualAmount = (0, number_1.bnOrZero)(balance).div(ratio);
|
|
702
|
+
return this.getTokenUsdVal(underlyingDenom, actualAmount, tokenPrice);
|
|
703
|
+
});
|
|
704
|
+
});
|
|
705
|
+
const cdpBalances = (_c = (yield Promise.all(cdpTokensBalancePromises))) !== null && _c !== void 0 ? _c : [];
|
|
698
706
|
const totalCdpTokensUsdVal = cdpBalances.reduce((prev, curr) => (prev.plus(curr)), number_1.BN_ZERO);
|
|
699
707
|
return totalCdpTokensUsdVal;
|
|
700
708
|
});
|
|
@@ -723,7 +731,7 @@ class CDPModule extends base_1.default {
|
|
|
723
731
|
return amount.multipliedBy(twap).shiftedBy(-decimals);
|
|
724
732
|
});
|
|
725
733
|
}
|
|
726
|
-
getTotalTokenDebt(denom, interestFee) {
|
|
734
|
+
getTotalTokenDebt(denom, debtInfo, interestFee, newInterestRate) {
|
|
727
735
|
var _a;
|
|
728
736
|
return __awaiter(this, void 0, void 0, function* () {
|
|
729
737
|
if (!interestFee) {
|
|
@@ -732,12 +740,16 @@ class CDPModule extends base_1.default {
|
|
|
732
740
|
}
|
|
733
741
|
if (!interestFee)
|
|
734
742
|
throw new Error("unable to retrieve interest fee");
|
|
735
|
-
|
|
736
|
-
|
|
743
|
+
if (!debtInfo) {
|
|
744
|
+
const debtInfoRsp = yield this.sdkProvider.query.cdp.TokenDebt(query_1.QueryTokenDebtRequest.fromPartial({ denom }));
|
|
745
|
+
debtInfo = debtInfoRsp.debtInfo;
|
|
746
|
+
}
|
|
737
747
|
if (!debtInfo)
|
|
738
748
|
throw new Error("unable to retrieve debt info");
|
|
739
|
-
|
|
740
|
-
|
|
749
|
+
if (!newInterestRate) {
|
|
750
|
+
const cimRsp = yield this.recalculateCIM(denom, debtInfo);
|
|
751
|
+
newInterestRate = cimRsp.interest;
|
|
752
|
+
}
|
|
741
753
|
const principal = (0, number_1.bnOrZero)(debtInfo.totalPrincipal);
|
|
742
754
|
const accumInterest = (0, number_1.bnOrZero)(debtInfo.totalAccumulatedInterest);
|
|
743
755
|
const newInterest = principal.times(newInterestRate).plus(accumInterest.times(number_1.BN_ONE.plus(newInterestRate)));
|