carbon-js-sdk 0.2.13 → 0.2.14-dev.1
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/clients/NEOClient.js +1 -1
- package/lib/clients/TokenClient.d.ts +5 -0
- package/lib/clients/TokenClient.js +29 -7
- package/lib/codec/book/genesis.d.ts +1 -1
- package/lib/codec/book/genesis.js +1 -1
- package/lib/codec/book/query.d.ts +1 -1
- package/lib/codec/book/query.js +1 -1
- package/lib/codec/broker/genesis.d.ts +1 -1
- package/lib/codec/broker/genesis.js +1 -1
- package/lib/codec/broker/query.d.ts +3 -3
- package/lib/codec/broker/query.js +3 -3
- package/lib/codec/btcx/denom_cross_chain_info.d.ts +1 -1
- package/lib/codec/btcx/denom_cross_chain_info.js +1 -1
- package/lib/codec/btcx/query.d.ts +1 -1
- package/lib/codec/btcx/query.js +1 -1
- package/lib/codec/ccm/genesis.d.ts +1 -1
- package/lib/codec/ccm/genesis.js +1 -1
- package/lib/codec/cdp/asset_params.d.ts +1 -12
- package/lib/codec/cdp/asset_params.js +14 -81
- package/lib/codec/cdp/debt_info.d.ts +1 -1
- package/lib/codec/cdp/debt_info.js +11 -13
- package/lib/codec/cdp/event.d.ts +321 -22
- package/lib/codec/cdp/event.js +2005 -82
- package/lib/codec/cdp/genesis.d.ts +80 -4
- package/lib/codec/cdp/genesis.js +531 -22
- package/lib/codec/cdp/params.d.ts +35 -0
- package/lib/codec/cdp/params.js +47 -1
- package/lib/codec/cdp/query.d.ts +437 -89
- package/lib/codec/cdp/query.js +1966 -177
- package/lib/codec/cdp/reward_scheme.d.ts +85 -0
- package/lib/codec/cdp/reward_scheme.js +656 -0
- package/lib/codec/cdp/stablecoin_debt_info.d.ts +1 -1
- package/lib/codec/cdp/stablecoin_debt_info.js +11 -13
- package/lib/codec/cdp/tx.d.ts +667 -96
- package/lib/codec/cdp/tx.js +3196 -243
- package/lib/codec/coin/event.d.ts +1 -1
- package/lib/codec/coin/event.js +1 -1
- package/lib/codec/coin/genesis.d.ts +2 -2
- package/lib/codec/coin/genesis.js +2 -2
- package/lib/codec/coin/proposal.d.ts +1 -1
- package/lib/codec/coin/proposal.js +1 -1
- package/lib/codec/coin/query.d.ts +2 -5
- package/lib/codec/coin/query.js +2 -8
- package/lib/codec/coin/token.d.ts +11 -0
- package/lib/codec/coin/token.js +65 -1
- package/lib/codec/coin/tx.d.ts +1 -1
- package/lib/codec/coin/tx.js +1 -1
- package/lib/codec/cosmos/auth/v1beta1/genesis.d.ts +1 -1
- package/lib/codec/cosmos/auth/v1beta1/genesis.js +1 -1
- package/lib/codec/cosmos/auth/v1beta1/query.d.ts +2 -2
- package/lib/codec/cosmos/auth/v1beta1/query.js +2 -2
- package/lib/codec/cosmos/authz/v1beta1/authz.d.ts +19 -0
- package/lib/codec/cosmos/authz/v1beta1/authz.js +89 -1
- package/lib/codec/cosmos/authz/v1beta1/genesis.d.ts +1 -15
- package/lib/codec/cosmos/authz/v1beta1/genesis.js +7 -120
- package/lib/codec/cosmos/authz/v1beta1/query.d.ts +70 -2
- package/lib/codec/cosmos/authz/v1beta1/query.js +275 -3
- package/lib/codec/cosmos/authz/v1beta1/tx.d.ts +1 -1
- package/lib/codec/cosmos/authz/v1beta1/tx.js +1 -1
- package/lib/codec/cosmos/bank/v1beta1/authz.d.ts +1 -1
- package/lib/codec/cosmos/bank/v1beta1/authz.js +1 -1
- package/lib/codec/cosmos/bank/v1beta1/bank.d.ts +1 -1
- package/lib/codec/cosmos/bank/v1beta1/bank.js +1 -1
- package/lib/codec/cosmos/bank/v1beta1/export.d.ts +1 -1
- package/lib/codec/cosmos/bank/v1beta1/export.js +3 -1
- package/lib/codec/cosmos/bank/v1beta1/genesis.d.ts +2 -2
- package/lib/codec/cosmos/bank/v1beta1/genesis.js +2 -2
- package/lib/codec/cosmos/bank/v1beta1/query.d.ts +43 -3
- package/lib/codec/cosmos/bank/v1beta1/query.js +140 -4
- package/lib/codec/cosmos/bank/v1beta1/tx.d.ts +2 -2
- package/lib/codec/cosmos/bank/v1beta1/tx.js +2 -2
- package/lib/codec/cosmos/base/snapshots/v1beta1/snapshot.d.ts +64 -0
- package/lib/codec/cosmos/base/snapshots/v1beta1/snapshot.js +343 -1
- package/lib/codec/cosmos/base/tendermint/v1beta1/query.d.ts +1 -1
- package/lib/codec/cosmos/base/tendermint/v1beta1/query.js +1 -1
- package/lib/codec/cosmos/capability/v1beta1/genesis.d.ts +1 -1
- package/lib/codec/cosmos/capability/v1beta1/genesis.js +1 -1
- package/lib/codec/cosmos/crisis/v1beta1/genesis.d.ts +1 -1
- package/lib/codec/cosmos/crisis/v1beta1/genesis.js +1 -1
- package/lib/codec/cosmos/distribution/v1beta1/distribution.d.ts +1 -1
- package/lib/codec/cosmos/distribution/v1beta1/distribution.js +1 -1
- package/lib/codec/cosmos/distribution/v1beta1/genesis.d.ts +2 -2
- package/lib/codec/cosmos/distribution/v1beta1/genesis.js +2 -2
- package/lib/codec/cosmos/distribution/v1beta1/query.d.ts +3 -3
- package/lib/codec/cosmos/distribution/v1beta1/query.js +3 -3
- package/lib/codec/cosmos/distribution/v1beta1/tx.d.ts +1 -1
- package/lib/codec/cosmos/distribution/v1beta1/tx.js +1 -1
- package/lib/codec/cosmos/evidence/v1beta1/query.d.ts +1 -1
- package/lib/codec/cosmos/evidence/v1beta1/query.js +1 -1
- package/lib/codec/cosmos/feegrant/v1beta1/feegrant.d.ts +1 -1
- package/lib/codec/cosmos/feegrant/v1beta1/feegrant.js +1 -1
- package/lib/codec/cosmos/feegrant/v1beta1/genesis.d.ts +1 -1
- package/lib/codec/cosmos/feegrant/v1beta1/genesis.js +1 -1
- package/lib/codec/cosmos/feegrant/v1beta1/query.d.ts +35 -2
- package/lib/codec/cosmos/feegrant/v1beta1/query.js +139 -3
- package/lib/codec/cosmos/gov/v1beta1/genesis.d.ts +1 -1
- package/lib/codec/cosmos/gov/v1beta1/genesis.js +1 -1
- package/lib/codec/cosmos/gov/v1beta1/gov.d.ts +1 -1
- package/lib/codec/cosmos/gov/v1beta1/gov.js +1 -1
- package/lib/codec/cosmos/gov/v1beta1/query.d.ts +2 -2
- package/lib/codec/cosmos/gov/v1beta1/query.js +2 -2
- package/lib/codec/cosmos/gov/v1beta1/tx.d.ts +2 -2
- package/lib/codec/cosmos/gov/v1beta1/tx.js +2 -2
- package/lib/codec/cosmos/mint/v1beta1/genesis.d.ts +1 -1
- package/lib/codec/cosmos/mint/v1beta1/genesis.js +1 -1
- package/lib/codec/cosmos/mint/v1beta1/query.d.ts +1 -1
- package/lib/codec/cosmos/mint/v1beta1/query.js +1 -1
- package/lib/codec/cosmos/params/v1beta1/query.d.ts +1 -1
- package/lib/codec/cosmos/params/v1beta1/query.js +1 -1
- package/lib/codec/cosmos/slashing/v1beta1/genesis.d.ts +1 -1
- package/lib/codec/cosmos/slashing/v1beta1/genesis.js +1 -1
- package/lib/codec/cosmos/slashing/v1beta1/query.d.ts +2 -2
- package/lib/codec/cosmos/slashing/v1beta1/query.js +2 -2
- package/lib/codec/cosmos/staking/v1beta1/authz.d.ts +1 -1
- package/lib/codec/cosmos/staking/v1beta1/authz.js +1 -1
- package/lib/codec/cosmos/staking/v1beta1/genesis.d.ts +1 -1
- package/lib/codec/cosmos/staking/v1beta1/genesis.js +1 -1
- package/lib/codec/cosmos/staking/v1beta1/query.d.ts +2 -2
- package/lib/codec/cosmos/staking/v1beta1/query.js +2 -2
- package/lib/codec/cosmos/staking/v1beta1/staking.d.ts +1 -1
- package/lib/codec/cosmos/staking/v1beta1/staking.js +1 -1
- package/lib/codec/cosmos/staking/v1beta1/tx.d.ts +2 -2
- package/lib/codec/cosmos/staking/v1beta1/tx.js +2 -2
- package/lib/codec/cosmos/tx/signing/v1beta1/signing.d.ts +14 -1
- package/lib/codec/cosmos/tx/signing/v1beta1/signing.js +19 -1
- package/lib/codec/cosmos/tx/v1beta1/export.d.ts +1 -1
- package/lib/codec/cosmos/tx/v1beta1/export.js +3 -1
- package/lib/codec/cosmos/tx/v1beta1/service.d.ts +53 -5
- package/lib/codec/cosmos/tx/v1beta1/service.js +179 -4
- package/lib/codec/cosmos/tx/v1beta1/tx.d.ts +3 -3
- package/lib/codec/cosmos/tx/v1beta1/tx.js +3 -3
- package/lib/codec/cosmos/upgrade/v1beta1/query.d.ts +1 -1
- package/lib/codec/cosmos/upgrade/v1beta1/query.js +1 -1
- package/lib/codec/cosmos/vesting/v1beta1/tx.d.ts +1 -1
- package/lib/codec/cosmos/vesting/v1beta1/tx.js +1 -1
- package/lib/codec/cosmos/vesting/v1beta1/vesting.d.ts +2 -2
- package/lib/codec/cosmos/vesting/v1beta1/vesting.js +2 -2
- package/lib/codec/fee/genesis.d.ts +1 -1
- package/lib/codec/fee/genesis.js +1 -1
- package/lib/codec/fee/proposal.d.ts +1 -1
- package/lib/codec/fee/proposal.js +1 -1
- package/lib/codec/fee/query.d.ts +1 -1
- package/lib/codec/fee/query.js +1 -1
- package/lib/codec/fee/tx.d.ts +1 -1
- package/lib/codec/fee/tx.js +1 -1
- package/lib/codec/headersync/genesis.d.ts +1 -1
- package/lib/codec/headersync/genesis.js +1 -1
- package/lib/codec/headersync/query.d.ts +1 -1
- package/lib/codec/headersync/query.js +1 -1
- package/lib/codec/index.d.ts +71 -23
- package/lib/codec/index.js +590 -384
- package/lib/codec/inflation/genesis.d.ts +1 -1
- package/lib/codec/inflation/genesis.js +1 -1
- package/lib/codec/inflation/query.d.ts +1 -1
- package/lib/codec/inflation/query.js +1 -1
- package/lib/codec/insurance/genesis.d.ts +1 -1
- package/lib/codec/insurance/genesis.js +1 -1
- package/lib/codec/leverage/genesis.d.ts +1 -1
- package/lib/codec/leverage/genesis.js +1 -1
- package/lib/codec/leverage/query.d.ts +1 -1
- package/lib/codec/leverage/query.js +1 -1
- package/lib/codec/liquidation/genesis.d.ts +1 -1
- package/lib/codec/liquidation/genesis.js +1 -1
- package/lib/codec/liquidation/query.d.ts +1 -1
- package/lib/codec/liquidation/query.js +1 -1
- package/lib/codec/liquidation/quote_changes.d.ts +1 -1
- package/lib/codec/liquidation/quote_changes.js +1 -1
- package/lib/codec/liquiditypool/event.d.ts +2 -2
- package/lib/codec/liquiditypool/event.js +2 -2
- package/lib/codec/liquiditypool/genesis.d.ts +2 -2
- package/lib/codec/liquiditypool/genesis.js +2 -2
- package/lib/codec/liquiditypool/proposal.d.ts +1 -1
- package/lib/codec/liquiditypool/proposal.js +1 -1
- package/lib/codec/liquiditypool/query.d.ts +2 -2
- package/lib/codec/liquiditypool/query.js +2 -2
- package/lib/codec/liquiditypool/tx.d.ts +1 -1
- package/lib/codec/liquiditypool/tx.js +1 -1
- package/lib/codec/lockproxy/genesis.d.ts +1 -1
- package/lib/codec/lockproxy/genesis.js +1 -1
- package/lib/codec/lockproxy/query.d.ts +1 -1
- package/lib/codec/lockproxy/query.js +1 -1
- package/lib/codec/market/event.d.ts +1 -1
- package/lib/codec/market/event.js +1 -1
- package/lib/codec/market/genesis.d.ts +2 -1
- package/lib/codec/market/genesis.js +20 -2
- package/lib/codec/market/proposal.d.ts +1 -1
- package/lib/codec/market/proposal.js +1 -1
- package/lib/codec/market/query.d.ts +1 -1
- package/lib/codec/market/query.js +1 -1
- package/lib/codec/market/tx.d.ts +1 -1
- package/lib/codec/market/tx.js +1 -1
- package/lib/codec/marketstats/genesis.d.ts +1 -1
- package/lib/codec/marketstats/genesis.js +1 -1
- package/lib/codec/marketstats/query.d.ts +2 -2
- package/lib/codec/marketstats/query.js +2 -2
- package/lib/codec/misc/message.d.ts +1 -1
- package/lib/codec/misc/message.js +1 -1
- package/lib/codec/misc/query.d.ts +3 -3
- package/lib/codec/misc/query.js +3 -3
- package/lib/codec/misc/trade.d.ts +1 -0
- package/lib/codec/misc/trade.js +17 -0
- package/lib/codec/misc/transaction.d.ts +1 -1
- package/lib/codec/misc/transaction.js +1 -1
- package/lib/codec/oracle/event.d.ts +1 -1
- package/lib/codec/oracle/event.js +1 -1
- package/lib/codec/oracle/genesis.d.ts +1 -1
- package/lib/codec/oracle/genesis.js +1 -1
- package/lib/codec/oracle/proposal.d.ts +1 -1
- package/lib/codec/oracle/proposal.js +1 -1
- package/lib/codec/oracle/query.d.ts +2 -2
- package/lib/codec/oracle/query.js +2 -2
- package/lib/codec/order/event.d.ts +1 -1
- package/lib/codec/order/event.js +1 -1
- package/lib/codec/order/genesis.d.ts +1 -1
- package/lib/codec/order/genesis.js +1 -1
- package/lib/codec/order/query.d.ts +1 -1
- package/lib/codec/order/query.js +1 -1
- package/lib/codec/position/event.d.ts +1 -1
- package/lib/codec/position/event.js +1 -1
- package/lib/codec/position/genesis.d.ts +1 -1
- package/lib/codec/position/genesis.js +1 -1
- package/lib/codec/position/query.d.ts +1 -1
- package/lib/codec/position/query.js +1 -1
- package/lib/codec/pricing/event.d.ts +11 -1
- package/lib/codec/pricing/event.js +52 -2
- package/lib/codec/pricing/genesis.d.ts +2 -1
- package/lib/codec/pricing/genesis.js +20 -5
- package/lib/codec/pricing/pricing.d.ts +13 -0
- package/lib/codec/pricing/pricing.js +83 -1
- package/lib/codec/pricing/query.d.ts +68 -1
- package/lib/codec/pricing/query.js +321 -2
- package/lib/codec/profile/event.d.ts +1 -1
- package/lib/codec/profile/event.js +1 -1
- package/lib/codec/profile/genesis.d.ts +1 -1
- package/lib/codec/profile/genesis.js +1 -1
- package/lib/codec/profile/query.d.ts +1 -1
- package/lib/codec/profile/query.js +1 -1
- package/lib/codec/subaccount/genesis.d.ts +1 -1
- package/lib/codec/subaccount/genesis.js +1 -1
- package/lib/codec/subaccount/query.d.ts +1 -1
- package/lib/codec/subaccount/query.js +1 -1
- package/lib/codec/tendermint/abci/types.d.ts +4 -4
- package/lib/codec/tendermint/abci/types.js +4 -4
- package/lib/codec/tendermint/types/block.d.ts +2 -2
- package/lib/codec/tendermint/types/block.js +2 -2
- package/lib/codec/tendermint/types/evidence.d.ts +2 -2
- package/lib/codec/tendermint/types/evidence.js +2 -2
- package/lib/codec/tendermint/types/types.d.ts +3 -3
- package/lib/codec/tendermint/types/types.js +3 -3
- package/lib/codec/tendermint/types/validator.d.ts +1 -1
- package/lib/codec/tendermint/types/validator.js +1 -1
- package/lib/modules/admin.d.ts +53 -1
- package/lib/modules/admin.js +175 -43
- package/lib/modules/cdp.d.ts +104 -13
- package/lib/modules/cdp.js +618 -20
- package/lib/provider/amino/types/admin.js +48 -7
- package/lib/provider/amino/types/cdp.js +72 -32
- package/lib/util/number.d.ts +1 -0
- package/lib/util/number.js +2 -1
- package/lib/util/tx.d.ts +58 -14
- package/lib/wallet/CarbonSigningClient.js +27 -2
- package/lib/wallet/CarbonWallet.js +0 -1
- package/lib/websocket/channel.js +60 -0
- package/lib/websocket/models.d.ts +179 -21
- package/lib/websocket/types.d.ts +69 -3
- package/lib/websocket/types.js +27 -0
- package/package.json +1 -1
package/lib/modules/cdp.js
CHANGED
|
@@ -13,67 +13,665 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
13
13
|
};
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
exports.CDPModule = void 0;
|
|
16
|
+
const codec_1 = require("../codec");
|
|
17
|
+
const query_1 = require("../codec/cdp/query");
|
|
16
18
|
const tx_1 = require("../codec/cdp/tx");
|
|
19
|
+
const query_2 = require("../codec/cosmos/bank/v1beta1/query");
|
|
17
20
|
const util_1 = require("../util");
|
|
21
|
+
const number_1 = require("../util/number");
|
|
22
|
+
const bignumber_js_1 = require("bignumber.js");
|
|
23
|
+
const query_3 = require("./../codec/cdp/query");
|
|
18
24
|
const base_1 = __importDefault(require("./base"));
|
|
19
|
-
const long_1 = __importDefault(require("long"));
|
|
20
25
|
class CDPModule extends base_1.default {
|
|
21
|
-
|
|
26
|
+
constructor() {
|
|
27
|
+
super(...arguments);
|
|
28
|
+
this.cdpModuleAddress = "";
|
|
29
|
+
this.collateralPoolAddress = "";
|
|
30
|
+
}
|
|
31
|
+
supplyAsset(params, opts) {
|
|
22
32
|
return __awaiter(this, void 0, void 0, function* () {
|
|
23
33
|
const wallet = this.getWallet();
|
|
24
|
-
const value = tx_1.
|
|
34
|
+
const value = tx_1.MsgSupplyAsset.fromPartial({
|
|
25
35
|
creator: wallet.bech32Address,
|
|
26
|
-
|
|
36
|
+
denom: params.denom,
|
|
27
37
|
amount: params.amount.toString(10),
|
|
28
38
|
});
|
|
29
39
|
return yield wallet.sendTx({
|
|
30
|
-
typeUrl: util_1.CarbonTx.Types.
|
|
31
|
-
value
|
|
40
|
+
typeUrl: util_1.CarbonTx.Types.MsgSupplyAsset,
|
|
41
|
+
value
|
|
32
42
|
}, opts);
|
|
33
43
|
});
|
|
34
44
|
}
|
|
35
|
-
|
|
45
|
+
withdrawAsset(params, opts) {
|
|
36
46
|
return __awaiter(this, void 0, void 0, function* () {
|
|
37
47
|
const wallet = this.getWallet();
|
|
38
|
-
const value = tx_1.
|
|
48
|
+
const value = tx_1.MsgWithdrawAsset.fromPartial({
|
|
39
49
|
creator: wallet.bech32Address,
|
|
40
|
-
|
|
50
|
+
cdpDenom: params.cdpDenom,
|
|
41
51
|
amount: params.amount.toString(10),
|
|
42
52
|
});
|
|
43
53
|
return yield wallet.sendTx({
|
|
44
|
-
typeUrl: util_1.CarbonTx.Types.
|
|
45
|
-
value
|
|
54
|
+
typeUrl: util_1.CarbonTx.Types.MsgWithdrawAsset,
|
|
55
|
+
value
|
|
46
56
|
}, opts);
|
|
47
57
|
});
|
|
48
58
|
}
|
|
49
|
-
|
|
59
|
+
lockCollateral(params, opts) {
|
|
50
60
|
return __awaiter(this, void 0, void 0, function* () {
|
|
51
61
|
const wallet = this.getWallet();
|
|
52
|
-
const value = tx_1.
|
|
62
|
+
const value = tx_1.MsgLockCollateral.fromPartial({
|
|
53
63
|
creator: wallet.bech32Address,
|
|
54
|
-
|
|
64
|
+
cdpDenom: params.cdpDenom,
|
|
55
65
|
amount: params.amount.toString(10),
|
|
56
66
|
});
|
|
57
67
|
return yield wallet.sendTx({
|
|
58
|
-
typeUrl: util_1.CarbonTx.Types.
|
|
59
|
-
value
|
|
68
|
+
typeUrl: util_1.CarbonTx.Types.MsgLockCollateral,
|
|
69
|
+
value
|
|
70
|
+
}, opts);
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
unlockCollateral(params, opts) {
|
|
74
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
75
|
+
const wallet = this.getWallet();
|
|
76
|
+
const value = tx_1.MsgUnlockCollateral.fromPartial({
|
|
77
|
+
creator: wallet.bech32Address,
|
|
78
|
+
cdpDenom: params.cdpDenom,
|
|
79
|
+
amount: params.amount.toString(10),
|
|
80
|
+
});
|
|
81
|
+
return yield wallet.sendTx({
|
|
82
|
+
typeUrl: util_1.CarbonTx.Types.MsgUnlockCollateral,
|
|
83
|
+
value
|
|
84
|
+
}, opts);
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
borrowAsset(params, opts) {
|
|
88
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
89
|
+
const wallet = this.getWallet();
|
|
90
|
+
const value = tx_1.MsgBorrowAsset.fromPartial({
|
|
91
|
+
creator: wallet.bech32Address,
|
|
92
|
+
denom: params.denom,
|
|
93
|
+
amount: params.amount.toString(10),
|
|
94
|
+
});
|
|
95
|
+
return yield wallet.sendTx({
|
|
96
|
+
typeUrl: util_1.CarbonTx.Types.MsgBorrowAsset,
|
|
97
|
+
value
|
|
98
|
+
}, opts);
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
repayAsset(params, opts) {
|
|
102
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
103
|
+
const wallet = this.getWallet();
|
|
104
|
+
const value = tx_1.MsgRepayAsset.fromPartial({
|
|
105
|
+
creator: wallet.bech32Address,
|
|
106
|
+
denom: params.denom,
|
|
107
|
+
amount: params.amount.toString(10),
|
|
108
|
+
});
|
|
109
|
+
return yield wallet.sendTx({
|
|
110
|
+
typeUrl: util_1.CarbonTx.Types.MsgRepayAsset,
|
|
111
|
+
value
|
|
112
|
+
}, opts);
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
supplyAssetAndLockCollateral(params, opts) {
|
|
116
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
117
|
+
const wallet = this.getWallet();
|
|
118
|
+
const value = tx_1.MsgSupplyAssetAndLockCollateral.fromPartial({
|
|
119
|
+
creator: wallet.bech32Address,
|
|
120
|
+
denom: params.denom,
|
|
121
|
+
supplyAmount: params.supplyAmount.toString(10),
|
|
122
|
+
lockAmount: params.lockAmount.toString(10),
|
|
123
|
+
});
|
|
124
|
+
return yield wallet.sendTx({
|
|
125
|
+
typeUrl: util_1.CarbonTx.Types.MsgSupplyAssetAndLockCollateral,
|
|
126
|
+
value
|
|
127
|
+
}, opts);
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
unlockCollateralAndWithdrawAsset(params, opts) {
|
|
131
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
132
|
+
const wallet = this.getWallet();
|
|
133
|
+
const value = tx_1.MsgUnlockCollateralAndWithdrawAsset.fromPartial({
|
|
134
|
+
creator: wallet.bech32Address,
|
|
135
|
+
cdpDenom: params.cdpDenom,
|
|
136
|
+
unlockAmount: params.unlockAmount.toString(10),
|
|
137
|
+
withdrawAmount: params.withdrawAmount.toString(10),
|
|
138
|
+
});
|
|
139
|
+
return yield wallet.sendTx({
|
|
140
|
+
typeUrl: util_1.CarbonTx.Types.MsgUnlockCollateralAndWithdrawAsset,
|
|
141
|
+
value
|
|
142
|
+
}, opts);
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
liquidateCollateral(params, opts) {
|
|
146
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
147
|
+
const wallet = this.getWallet();
|
|
148
|
+
const value = tx_1.MsgLiquidateCollateral.fromPartial({
|
|
149
|
+
creator: wallet.bech32Address,
|
|
150
|
+
debtor: params.debtor,
|
|
151
|
+
collateralDenom: params.collateralDenom,
|
|
152
|
+
debtDenom: params.debtDenom,
|
|
153
|
+
debtAmount: params.debtAmount.toString(10),
|
|
154
|
+
});
|
|
155
|
+
return yield wallet.sendTx({
|
|
156
|
+
typeUrl: util_1.CarbonTx.Types.MsgLiquidateCollateral,
|
|
157
|
+
value
|
|
158
|
+
}, opts);
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
repayAssetWithCdpTokens(params, opts) {
|
|
162
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
163
|
+
const wallet = this.getWallet();
|
|
164
|
+
const debtor = params.debtor ? params.debtor : wallet.bech32Address;
|
|
165
|
+
const value = tx_1.MsgRepayAssetWithCdpTokens.fromPartial({
|
|
166
|
+
creator: wallet.bech32Address,
|
|
167
|
+
debtor: debtor,
|
|
168
|
+
debtDenom: params.debtDenom,
|
|
169
|
+
cdpDenom: params.cdpDenom,
|
|
170
|
+
cdpAmount: params.cdpAmount.toString(10),
|
|
171
|
+
});
|
|
172
|
+
return yield wallet.sendTx({
|
|
173
|
+
typeUrl: util_1.CarbonTx.Types.MsgRepayAssetWithCdpTokens,
|
|
174
|
+
value
|
|
175
|
+
}, opts);
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
repayAssetWithCollateral(params, opts) {
|
|
179
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
180
|
+
const wallet = this.getWallet();
|
|
181
|
+
const debtor = params.debtor ? params.debtor : wallet.bech32Address;
|
|
182
|
+
const value = tx_1.MsgRepayAssetWithCollateral.fromPartial({
|
|
183
|
+
creator: wallet.bech32Address,
|
|
184
|
+
debtor: params.debtor,
|
|
185
|
+
debtDenom: params.debtDenom,
|
|
186
|
+
cdpDenom: params.cdpDenom,
|
|
187
|
+
cdpAmount: params.cdpAmount.toString(10),
|
|
188
|
+
});
|
|
189
|
+
return yield wallet.sendTx({
|
|
190
|
+
typeUrl: util_1.CarbonTx.Types.MsgRepayAssetWithCollateral,
|
|
191
|
+
value
|
|
60
192
|
}, opts);
|
|
61
193
|
});
|
|
62
194
|
}
|
|
63
|
-
|
|
195
|
+
mintStablecoin(params, opts) {
|
|
64
196
|
return __awaiter(this, void 0, void 0, function* () {
|
|
65
197
|
const wallet = this.getWallet();
|
|
66
|
-
const value = tx_1.
|
|
198
|
+
const value = tx_1.MsgMintStablecoin.fromPartial({
|
|
67
199
|
creator: wallet.bech32Address,
|
|
68
|
-
vaultTypeId: new long_1.default(params.vaultTypeId),
|
|
69
200
|
amount: params.amount.toString(10),
|
|
70
201
|
});
|
|
71
202
|
return yield wallet.sendTx({
|
|
72
|
-
typeUrl: util_1.CarbonTx.Types.
|
|
203
|
+
typeUrl: util_1.CarbonTx.Types.MsgMintStablecoin,
|
|
204
|
+
value,
|
|
205
|
+
}, opts);
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
returnStablecoin(params, opts) {
|
|
209
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
210
|
+
const wallet = this.getWallet();
|
|
211
|
+
const value = tx_1.MsgReturnStablecoin.fromPartial({
|
|
212
|
+
creator: wallet.bech32Address,
|
|
213
|
+
principalAmount: params.principalAmount.toString(10),
|
|
214
|
+
interestDenom: params.interestDenom,
|
|
215
|
+
interestAmount: params.interestAmount.toString(10),
|
|
216
|
+
});
|
|
217
|
+
return yield wallet.sendTx({
|
|
218
|
+
typeUrl: util_1.CarbonTx.Types.MsgReturnStablecoin,
|
|
73
219
|
value,
|
|
74
220
|
}, opts);
|
|
75
221
|
});
|
|
76
222
|
}
|
|
223
|
+
updateRateStrategy(params, opts) {
|
|
224
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
225
|
+
const wallet = this.getWallet();
|
|
226
|
+
const value = tx_1.MsgUpdateRateStrategy.fromPartial({
|
|
227
|
+
creator: wallet.bech32Address,
|
|
228
|
+
rateStrategyParams: params.rateStrategyParams
|
|
229
|
+
});
|
|
230
|
+
return yield wallet.sendTx({
|
|
231
|
+
typeUrl: util_1.CarbonTx.Types.MsgUpdateRateStrategy,
|
|
232
|
+
value,
|
|
233
|
+
}, opts);
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
// start of cdp calculations
|
|
237
|
+
getAccountData(account) {
|
|
238
|
+
var _a, _b;
|
|
239
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
240
|
+
const sdk = this.sdkProvider;
|
|
241
|
+
const debtInfoResponse = yield sdk.query.cdp.TokenDebtAll(query_3.QueryTokenDebtAllRequest.fromPartial({}));
|
|
242
|
+
const debtInfos = debtInfoResponse.debtInfosAll;
|
|
243
|
+
const collateralsRsp = yield sdk.query.cdp.AccountCollateralAll(query_3.QueryAccountCollateralAllRequest.fromPartial({ address: account }));
|
|
244
|
+
const collaterals = collateralsRsp.collaterals;
|
|
245
|
+
const assetParamsRsp = yield sdk.query.cdp.AssetAll(query_3.QueryAssetAllRequest.fromPartial({}));
|
|
246
|
+
const assetParams = assetParamsRsp.assetParamsAll;
|
|
247
|
+
let totalCollateralsUsd = number_1.BN_ZERO;
|
|
248
|
+
let availableBorrowsUsd = number_1.BN_ZERO;
|
|
249
|
+
let currLiquidationThreshold = number_1.BN_ZERO;
|
|
250
|
+
for (let i = 0; i < collaterals.length; i++) {
|
|
251
|
+
const amount = (0, number_1.bnOrZero)(collaterals[i].collateralAmount);
|
|
252
|
+
if (amount.isZero()) {
|
|
253
|
+
continue;
|
|
254
|
+
}
|
|
255
|
+
const denom = collaterals[i].denom;
|
|
256
|
+
const debtInfo = debtInfos.find(d => d.denom === denom);
|
|
257
|
+
if (!debtInfo) {
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
const collateralUsdVal = yield this.getCdpTokenUsdVal(collaterals[i].cdpDenom, amount);
|
|
261
|
+
if (!collateralUsdVal) {
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
const assetParam = assetParams.find(a => a.denom === denom);
|
|
265
|
+
if (!assetParam) {
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
const ltv = (0, number_1.bnOrZero)(assetParam.loanToValue).div(number_1.BN_10000);
|
|
269
|
+
const availableBorrowUsd = collateralUsdVal.times(ltv);
|
|
270
|
+
const liquidationThreshold = (0, number_1.bnOrZero)(assetParam.liquidationThreshold).div(number_1.BN_10000);
|
|
271
|
+
const liquidationThresholdVal = collateralUsdVal.times(liquidationThreshold);
|
|
272
|
+
totalCollateralsUsd = totalCollateralsUsd.plus(collateralUsdVal);
|
|
273
|
+
availableBorrowsUsd = availableBorrowsUsd.plus(availableBorrowUsd);
|
|
274
|
+
currLiquidationThreshold = currLiquidationThreshold.plus(liquidationThresholdVal);
|
|
275
|
+
}
|
|
276
|
+
// add token debts
|
|
277
|
+
const debtsRsp = yield sdk.query.cdp.AccountDebtAll(query_1.QueryAccountDebtAllRequest.fromPartial({ address: account }));
|
|
278
|
+
const debts = debtsRsp.debts;
|
|
279
|
+
let totalDebtsUsd = number_1.BN_ZERO;
|
|
280
|
+
for (let i = 0; i < debts.length; i++) {
|
|
281
|
+
const amount = (0, number_1.bnOrZero)(debts[i].principalDebt);
|
|
282
|
+
const denom = debts[i].denom;
|
|
283
|
+
if (amount.isZero()) {
|
|
284
|
+
continue;
|
|
285
|
+
}
|
|
286
|
+
const debtInfo = debtInfos.find(d => d.denom === denom);
|
|
287
|
+
if (!debtInfo) {
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
const tokenDebtUsdVal = yield this.getTotalAccountTokenDebtUsdVal(account, denom, debts[i], debtInfo);
|
|
291
|
+
if (!tokenDebtUsdVal) {
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
totalDebtsUsd = totalDebtsUsd.plus(tokenDebtUsdVal);
|
|
295
|
+
}
|
|
296
|
+
// add stablecoin debt
|
|
297
|
+
const debtInfoRsp = yield sdk.query.cdp.StablecoinDebt(query_1.QueryStablecoinDebtRequest.fromPartial({}));
|
|
298
|
+
const stablecoinDebtInfo = debtInfoRsp.stablecoinDebtInfo;
|
|
299
|
+
if (!stablecoinDebtInfo) {
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
const accountStablecoin = yield sdk.query.cdp.AccountStablecoin({ address: account });
|
|
303
|
+
const usdValue = (_a = yield sdk.getTokenClient().getUSDValue(stablecoinDebtInfo.denom)) !== null && _a !== void 0 ? _a : number_1.BN_ZERO;
|
|
304
|
+
const stablecoinDecimals = (_b = yield this.sdkProvider.getTokenClient().getDecimals(stablecoinDebtInfo.denom)) !== null && _b !== void 0 ? _b : number_1.BN_ZERO;
|
|
305
|
+
const stablecoinDebtAmount = (0, number_1.bnOrZero)(accountStablecoin.principalDebt).plus((0, number_1.bnOrZero)(accountStablecoin.interestDebt));
|
|
306
|
+
const stablecoinDebtUsd = (stablecoinDebtAmount.times(usdValue)).shiftedBy(-stablecoinDecimals);
|
|
307
|
+
totalDebtsUsd = totalDebtsUsd.plus(stablecoinDebtUsd);
|
|
308
|
+
const healthFactor = currLiquidationThreshold.div(totalDebtsUsd);
|
|
309
|
+
return {
|
|
310
|
+
TotalCollateralsUsd: totalCollateralsUsd,
|
|
311
|
+
AvailableBorrowsUsd: availableBorrowsUsd,
|
|
312
|
+
CurrLiquidationThreshold: currLiquidationThreshold,
|
|
313
|
+
TotalDebtsUsd: totalDebtsUsd,
|
|
314
|
+
TotalStablecoinDebtsUsd: stablecoinDebtUsd,
|
|
315
|
+
HealthFactor: healthFactor,
|
|
316
|
+
};
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
getAssetBorrowableSupply(denom) {
|
|
320
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
321
|
+
const sdk = this.sdkProvider;
|
|
322
|
+
if (!this.cdpModuleAddress) {
|
|
323
|
+
const moduleAddressRsp = yield sdk.query.misc.ModuleAddress(codec_1.QueryModuleAddressRequest.fromPartial({ module: "cdp" }));
|
|
324
|
+
this.cdpModuleAddress = moduleAddressRsp.address;
|
|
325
|
+
}
|
|
326
|
+
const balanceRsp = yield sdk.query.bank.Balance(query_2.QueryBalanceRequest.fromPartial({ address: this.cdpModuleAddress, denom }));
|
|
327
|
+
if (!balanceRsp.balance) {
|
|
328
|
+
return;
|
|
329
|
+
}
|
|
330
|
+
return (0, number_1.bnOrZero)(balanceRsp.balance.amount);
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
getCdpToActualRatio(cdpDenom) {
|
|
334
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
335
|
+
const sdk = this.sdkProvider;
|
|
336
|
+
const denom = this.getUnderlyingDenom(cdpDenom);
|
|
337
|
+
if (!denom) {
|
|
338
|
+
return;
|
|
339
|
+
}
|
|
340
|
+
const supplyRsp = yield sdk.query.bank.SupplyOf(query_2.QuerySupplyOfRequest.fromPartial({ denom: cdpDenom }));
|
|
341
|
+
const cdpAmountRsp = supplyRsp.amount;
|
|
342
|
+
if (!cdpAmountRsp) {
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
const cdpAmount = (0, number_1.bnOrZero)(cdpAmountRsp.amount);
|
|
346
|
+
if (!this.cdpModuleAddress) {
|
|
347
|
+
const moduleAddressRsp = yield sdk.query.misc.ModuleAddress(codec_1.QueryModuleAddressRequest.fromPartial({ module: "cdp" }));
|
|
348
|
+
this.cdpModuleAddress = moduleAddressRsp.address;
|
|
349
|
+
}
|
|
350
|
+
const balanceRsp = yield sdk.query.bank.Balance(query_2.QueryBalanceRequest.fromPartial({ address: this.cdpModuleAddress, denom }));
|
|
351
|
+
if (!balanceRsp.balance) {
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
let actualAmount = (0, number_1.bnOrZero)(balanceRsp.balance.amount);
|
|
355
|
+
const owedAmount = yield this.getTotalTokenDebt(denom);
|
|
356
|
+
if (!owedAmount) {
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
actualAmount = actualAmount.plus(owedAmount);
|
|
360
|
+
const ratio = cdpAmount.div(actualAmount);
|
|
361
|
+
return ratio;
|
|
362
|
+
});
|
|
363
|
+
}
|
|
364
|
+
getTotalAccountTokenDebtUsdVal(account, denom, debt, debtInfo) {
|
|
365
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
366
|
+
const amount = yield this.getTotalAccountTokenDebt(account, denom, debt, debtInfo).catch((err) => console.log(err));
|
|
367
|
+
if (!amount) {
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
const tokenDebtUsdVal = yield this.getTokenUsdVal(denom, amount).catch((err) => console.log(err));
|
|
371
|
+
return tokenDebtUsdVal;
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
getModuleTotalDebtUsdVal() {
|
|
375
|
+
var _a;
|
|
376
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
377
|
+
const sdk = this.sdkProvider;
|
|
378
|
+
let totalDebt = new bignumber_js_1.BigNumber(0);
|
|
379
|
+
// get token debts
|
|
380
|
+
const allDebtsRes = yield this.sdkProvider.query.cdp.TokenDebtAll({});
|
|
381
|
+
const allDebts = allDebtsRes.debtInfosAll;
|
|
382
|
+
for (let i = 0; i < allDebts.length; i++) {
|
|
383
|
+
const denom = allDebts[i].denom;
|
|
384
|
+
const interest = (0, number_1.bnOrZero)(allDebts[i].totalAccumulatedInterest);
|
|
385
|
+
const principal = (0, number_1.bnOrZero)(allDebts[i].totalPrincipal);
|
|
386
|
+
const debtAmt = interest.plus(principal);
|
|
387
|
+
const debtUsdVal = yield this.getTokenUsdVal(denom, debtAmt);
|
|
388
|
+
if (!debtUsdVal) {
|
|
389
|
+
return;
|
|
390
|
+
}
|
|
391
|
+
totalDebt = totalDebt.plus(debtUsdVal);
|
|
392
|
+
}
|
|
393
|
+
// get stablecoin debt
|
|
394
|
+
const stablecoinDebtRes = yield this.sdkProvider.query.cdp.StablecoinDebt({});
|
|
395
|
+
const stablecoinDebtInfo = stablecoinDebtRes.stablecoinDebtInfo;
|
|
396
|
+
if (!stablecoinDebtInfo) {
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
const debtAmt = (0, number_1.bnOrZero)(stablecoinDebtInfo.totalPrincipal).plus((0, number_1.bnOrZero)(stablecoinDebtInfo.totalAccumulatedInterest));
|
|
400
|
+
const stablecoinDecimals = (_a = yield sdk.getTokenClient().getDecimals(stablecoinDebtInfo.denom)) !== null && _a !== void 0 ? _a : number_1.BN_ZERO;
|
|
401
|
+
const debtUsdVal = (debtAmt).shiftedBy(-stablecoinDecimals);
|
|
402
|
+
totalDebt = totalDebt.plus(debtUsdVal);
|
|
403
|
+
return totalDebt;
|
|
404
|
+
});
|
|
405
|
+
}
|
|
406
|
+
getModuleTotalCollateralUsdVal() {
|
|
407
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
408
|
+
const moduleAddressRsp = yield this.sdkProvider.query.misc.ModuleAddress(codec_1.QueryModuleAddressRequest.fromPartial({ module: "collateral_pool" }));
|
|
409
|
+
const cdpBalances = yield this.sdkProvider.query.bank.AllBalances({ address: moduleAddressRsp.address });
|
|
410
|
+
let allCollateralsUsdValue = new bignumber_js_1.BigNumber(0);
|
|
411
|
+
for (let i = 0; i < cdpBalances.balances.length; i++) {
|
|
412
|
+
const denom = cdpBalances.balances[i].denom;
|
|
413
|
+
const amount = new bignumber_js_1.BigNumber(cdpBalances.balances[i].amount);
|
|
414
|
+
const collateralUsdValue = yield this.getCdpTokenUsdVal(denom, amount);
|
|
415
|
+
if (!collateralUsdValue) {
|
|
416
|
+
return;
|
|
417
|
+
}
|
|
418
|
+
allCollateralsUsdValue = allCollateralsUsdValue.plus(collateralUsdValue);
|
|
419
|
+
}
|
|
420
|
+
return allCollateralsUsdValue;
|
|
421
|
+
});
|
|
422
|
+
}
|
|
423
|
+
getCdpTokenUsdVal(cdpDenom, amount) {
|
|
424
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
425
|
+
const denom = this.getUnderlyingDenom(cdpDenom);
|
|
426
|
+
if (!denom) {
|
|
427
|
+
return;
|
|
428
|
+
}
|
|
429
|
+
const ratio = yield this.getCdpToActualRatio(cdpDenom);
|
|
430
|
+
if (!ratio) {
|
|
431
|
+
return;
|
|
432
|
+
}
|
|
433
|
+
const actualTokenAmount = amount.div(ratio);
|
|
434
|
+
return yield this.getTokenUsdVal(denom, actualTokenAmount);
|
|
435
|
+
});
|
|
436
|
+
}
|
|
437
|
+
getTokenUsdVal(denom, amount) {
|
|
438
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
439
|
+
const sdk = this.sdkProvider;
|
|
440
|
+
const decimals = yield this.sdkProvider.getTokenClient().getDecimals(denom);
|
|
441
|
+
if (!decimals) {
|
|
442
|
+
return;
|
|
443
|
+
}
|
|
444
|
+
const price = yield sdk.query.pricing.TokenPrice(codec_1.QueryTokenPriceRequest.fromPartial({ denom }));
|
|
445
|
+
if (!price.tokenPrice) {
|
|
446
|
+
return;
|
|
447
|
+
}
|
|
448
|
+
const twap = (0, number_1.bnOrZero)(price.tokenPrice.twap).shiftedBy(18 * (-1));
|
|
449
|
+
return amount.times(twap).shiftedBy(decimals * (-1));
|
|
450
|
+
});
|
|
451
|
+
}
|
|
452
|
+
getTotalTokenDebt(denom, debtInfo) {
|
|
453
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
454
|
+
if (!debtInfo) {
|
|
455
|
+
const debtInfoRsp = yield this.sdkProvider.query.cdp.TokenDebt(query_1.QueryTokenDebtRequest.fromPartial({ denom }));
|
|
456
|
+
debtInfo = debtInfoRsp.debtInfo;
|
|
457
|
+
}
|
|
458
|
+
if (!debtInfo) {
|
|
459
|
+
return;
|
|
460
|
+
}
|
|
461
|
+
const cim = yield this.recalculateCIM(denom, debtInfo);
|
|
462
|
+
if (!cim) {
|
|
463
|
+
return;
|
|
464
|
+
}
|
|
465
|
+
const principal = (0, number_1.bnOrZero)(debtInfo.totalPrincipal);
|
|
466
|
+
const interest = (0, number_1.bnOrZero)(debtInfo.totalAccumulatedInterest);
|
|
467
|
+
return principal.plus(interest);
|
|
468
|
+
});
|
|
469
|
+
}
|
|
470
|
+
getTotalAccountTokenDebt(account, denom, debt, debtInfo) {
|
|
471
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
472
|
+
const sdk = this.sdkProvider;
|
|
473
|
+
if (!debtInfo) {
|
|
474
|
+
const debtInfoRsp = yield sdk.query.cdp.TokenDebt(query_1.QueryTokenDebtRequest.fromPartial({ denom }));
|
|
475
|
+
debtInfo = debtInfoRsp.debtInfo;
|
|
476
|
+
}
|
|
477
|
+
if (!debtInfo) {
|
|
478
|
+
return;
|
|
479
|
+
}
|
|
480
|
+
if (!debt) {
|
|
481
|
+
const debtRes = yield sdk.query.cdp.AccountDebt({ address: account, denom: denom });
|
|
482
|
+
debt = debtRes.debt;
|
|
483
|
+
}
|
|
484
|
+
if (!debt) {
|
|
485
|
+
return;
|
|
486
|
+
}
|
|
487
|
+
const principalAmount = (0, number_1.bnOrZero)(debt.principalDebt);
|
|
488
|
+
const initialCIM = (0, number_1.bnOrZero)(debt.initialCumulativeInterestMultiplier);
|
|
489
|
+
const cim = yield this.recalculateCIM(denom, debtInfo);
|
|
490
|
+
if (!cim) {
|
|
491
|
+
return;
|
|
492
|
+
}
|
|
493
|
+
// TODO: change to round up
|
|
494
|
+
const totalAmountTokenDebt = principalAmount.times(cim).dividedToIntegerBy(initialCIM);
|
|
495
|
+
return totalAmountTokenDebt;
|
|
496
|
+
});
|
|
497
|
+
}
|
|
498
|
+
getTotalAccountStablecoinDebt(account, debt, debtInfo) {
|
|
499
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
500
|
+
const sdk = this.sdkProvider;
|
|
501
|
+
let principalAmount = number_1.BN_ZERO;
|
|
502
|
+
if (!debtInfo) {
|
|
503
|
+
const debtInfoResponse = yield sdk.query.cdp.StablecoinDebt(query_1.QueryStablecoinDebtRequest.fromPartial({}));
|
|
504
|
+
debtInfo = debtInfoResponse.stablecoinDebtInfo;
|
|
505
|
+
}
|
|
506
|
+
if (!debtInfo) {
|
|
507
|
+
return number_1.BN_ZERO;
|
|
508
|
+
}
|
|
509
|
+
if (!debt) {
|
|
510
|
+
const debtResp = yield sdk.query.cdp.AccountStablecoin(query_1.QueryAccountStablecoinRequest.fromPartial({ address: account }));
|
|
511
|
+
debt = debtResp;
|
|
512
|
+
}
|
|
513
|
+
if (!debt) {
|
|
514
|
+
return number_1.BN_ZERO;
|
|
515
|
+
}
|
|
516
|
+
principalAmount = (0, number_1.bnOrZero)(debt.principalDebt);
|
|
517
|
+
const initialCIM = (0, number_1.bnOrZero)(debt.initialCumulativeInterestMultiplier);
|
|
518
|
+
const cim = yield this.recalculateStablecoinCIM(debtInfo);
|
|
519
|
+
if (!cim) {
|
|
520
|
+
return number_1.BN_ZERO;
|
|
521
|
+
}
|
|
522
|
+
const totalStablecoinDebtAmount = principalAmount.times(cim).dividedToIntegerBy(initialCIM);
|
|
523
|
+
return totalStablecoinDebtAmount;
|
|
524
|
+
});
|
|
525
|
+
}
|
|
526
|
+
calculateAPY(denom, debtInfo, assetParams, rateStrategyParams) {
|
|
527
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
528
|
+
const sdk = this.sdkProvider;
|
|
529
|
+
if (!debtInfo) {
|
|
530
|
+
const debtInfoResponse = yield sdk.query.cdp.TokenDebt(query_1.QueryTokenDebtRequest.fromPartial({ denom }));
|
|
531
|
+
debtInfo = debtInfoResponse.debtInfo;
|
|
532
|
+
if (!debtInfo) {
|
|
533
|
+
return;
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
if (!assetParams) {
|
|
537
|
+
const assetResponse = yield sdk.query.cdp.Asset(query_1.QueryAssetRequest.fromPartial({ denom }));
|
|
538
|
+
assetParams = assetResponse.assetParams;
|
|
539
|
+
if (!assetParams) {
|
|
540
|
+
return;
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
if (!rateStrategyParams) {
|
|
544
|
+
const rateStrategyParamsResponse = yield sdk.query.cdp.RateStrategy(query_1.QueryRateStrategyRequest.fromPartial({
|
|
545
|
+
name: assetParams.rateStrategyName
|
|
546
|
+
}));
|
|
547
|
+
rateStrategyParams = rateStrategyParamsResponse.rateStrategyParams;
|
|
548
|
+
if (!rateStrategyParams) {
|
|
549
|
+
return;
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
const utilizationRate = (0, number_1.bnOrZero)(debtInfo.utilizationRate).shiftedBy(-18);
|
|
553
|
+
const optimalUsage = (0, number_1.bnOrZero)(rateStrategyParams.optimalUsage).div(number_1.BN_10000);
|
|
554
|
+
const variableRate1 = (0, number_1.bnOrZero)(rateStrategyParams.variableRateSlope1).div(number_1.BN_10000);
|
|
555
|
+
const variableRate2 = (0, number_1.bnOrZero)(rateStrategyParams.variableRateSlope2).div(number_1.BN_10000);
|
|
556
|
+
const baseVariableBorrowRate = (0, number_1.bnOrZero)(rateStrategyParams.baseVariableBorrowRate).div(number_1.BN_10000);
|
|
557
|
+
if (utilizationRate.lte(optimalUsage)) {
|
|
558
|
+
return utilizationRate.times(variableRate1).div(optimalUsage).plus(baseVariableBorrowRate);
|
|
559
|
+
}
|
|
560
|
+
else {
|
|
561
|
+
const ratio = utilizationRate.minus(optimalUsage).div(number_1.BN_ONE.minus(optimalUsage));
|
|
562
|
+
return ratio.times(variableRate2).plus(variableRate1).plus(baseVariableBorrowRate);
|
|
563
|
+
}
|
|
564
|
+
});
|
|
565
|
+
}
|
|
566
|
+
calculateInterestForTimePeriod(apy, start, end) {
|
|
567
|
+
if (end <= start) {
|
|
568
|
+
return number_1.BN_ZERO;
|
|
569
|
+
}
|
|
570
|
+
const duration = (0, number_1.bnOrZero)(end.valueOf() - start.valueOf());
|
|
571
|
+
const millisecondsAYear = (0, number_1.bnOrZero)(31536000000);
|
|
572
|
+
const interest = duration.div(millisecondsAYear).times(apy);
|
|
573
|
+
return interest;
|
|
574
|
+
}
|
|
575
|
+
recalculateCIM(denom, debtInfo) {
|
|
576
|
+
var _a;
|
|
577
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
578
|
+
const sdk = this.sdkProvider;
|
|
579
|
+
if (!debtInfo) {
|
|
580
|
+
const debtInfoResponse = yield sdk.query.cdp.TokenDebt(query_1.QueryTokenDebtRequest.fromPartial({ denom }));
|
|
581
|
+
debtInfo = debtInfoResponse.debtInfo;
|
|
582
|
+
if (!debtInfo) {
|
|
583
|
+
return number_1.BN_ZERO;
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
const cim = (0, number_1.bnOrZero)(debtInfo.cumulativeInterestMultiplier);
|
|
587
|
+
const apy = yield this.calculateAPY(denom, debtInfo);
|
|
588
|
+
if (!apy) {
|
|
589
|
+
return number_1.BN_ZERO;
|
|
590
|
+
}
|
|
591
|
+
const interest = this.calculateInterestForTimePeriod(apy, (_a = debtInfo.lastUpdatedTime) !== null && _a !== void 0 ? _a : new Date(0), new Date());
|
|
592
|
+
const newCIM = cim.times(interest.plus(1));
|
|
593
|
+
return newCIM;
|
|
594
|
+
});
|
|
595
|
+
}
|
|
596
|
+
recalculateStablecoinCIM(debtInfo) {
|
|
597
|
+
var _a, _b;
|
|
598
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
599
|
+
const sdk = this.sdkProvider;
|
|
600
|
+
if (!debtInfo) {
|
|
601
|
+
const debtInfoResponse = yield sdk.query.cdp.StablecoinDebt(query_1.QueryStablecoinDebtRequest.fromPartial({}));
|
|
602
|
+
debtInfo = debtInfoResponse.stablecoinDebtInfo;
|
|
603
|
+
if (!debtInfo) {
|
|
604
|
+
return number_1.BN_ZERO;
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
const paramsResponse = yield sdk.query.cdp.Params(codec_1.QueryCdpParamsRequest.fromPartial({}));
|
|
608
|
+
const cim = (0, number_1.bnOrZero)(debtInfo.cumulativeInterestMultiplier);
|
|
609
|
+
const apy = (0, number_1.bnOrZero)((_a = paramsResponse.params) === null || _a === void 0 ? void 0 : _a.stablecoinInterestRate);
|
|
610
|
+
if (!apy) {
|
|
611
|
+
return number_1.BN_ZERO;
|
|
612
|
+
}
|
|
613
|
+
const interest = this.calculateInterestForTimePeriod(apy, (_b = debtInfo.lastUpdatedTime) !== null && _b !== void 0 ? _b : new Date(0), new Date());
|
|
614
|
+
const newCIM = cim.times(interest.plus(1));
|
|
615
|
+
return newCIM;
|
|
616
|
+
});
|
|
617
|
+
}
|
|
618
|
+
getMaxCollateralForUnlock(account, cdpDenom) {
|
|
619
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
620
|
+
const sdk = this.sdkProvider;
|
|
621
|
+
const denom = yield this.getUnderlyingDenom(cdpDenom);
|
|
622
|
+
if (!denom)
|
|
623
|
+
return;
|
|
624
|
+
const assetParams = yield sdk.query.cdp.Asset({ denom: denom });
|
|
625
|
+
if (!assetParams.assetParams)
|
|
626
|
+
return;
|
|
627
|
+
const ltv = new bignumber_js_1.BigNumber(assetParams.assetParams.loanToValue);
|
|
628
|
+
const accountData = yield this.getAccountData(account);
|
|
629
|
+
if (!accountData)
|
|
630
|
+
return;
|
|
631
|
+
const availableBorrowsUsd = accountData.AvailableBorrowsUsd.minus(accountData.TotalDebtsUsd);
|
|
632
|
+
const unlockableUsd = availableBorrowsUsd.multipliedBy(10000).div(ltv);
|
|
633
|
+
const cdpTokenPrice = yield this.getCdpTokenPrice(cdpDenom);
|
|
634
|
+
if (!cdpTokenPrice)
|
|
635
|
+
return;
|
|
636
|
+
const tokenDecimals = yield sdk.getTokenClient().getDecimals(denom);
|
|
637
|
+
if (!tokenDecimals)
|
|
638
|
+
return;
|
|
639
|
+
const cdpTokensUnlockableAmt = unlockableUsd.div(cdpTokenPrice).shiftedBy(tokenDecimals);
|
|
640
|
+
// take the min of cdpTokensUnlockableAmt and locked tokens
|
|
641
|
+
const accountCollateral = yield sdk.query.cdp.AccountCollateral({
|
|
642
|
+
address: account,
|
|
643
|
+
cdpDenom: cdpDenom
|
|
644
|
+
});
|
|
645
|
+
if (!accountCollateral.collateral)
|
|
646
|
+
return;
|
|
647
|
+
const lockedAmount = new bignumber_js_1.BigNumber(accountCollateral.collateral.collateralAmount);
|
|
648
|
+
if (lockedAmount.lt(cdpTokensUnlockableAmt)) {
|
|
649
|
+
return lockedAmount;
|
|
650
|
+
}
|
|
651
|
+
return cdpTokensUnlockableAmt;
|
|
652
|
+
});
|
|
653
|
+
}
|
|
654
|
+
getCdpTokenPrice(cdpDenom) {
|
|
655
|
+
var _a;
|
|
656
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
657
|
+
const sdk = this.sdkProvider;
|
|
658
|
+
const denom = yield this.getUnderlyingDenom(cdpDenom);
|
|
659
|
+
if (!denom) {
|
|
660
|
+
return;
|
|
661
|
+
}
|
|
662
|
+
const cdpToActualRatio = (_a = yield this.getCdpToActualRatio(cdpDenom)) !== null && _a !== void 0 ? _a : number_1.BN_ZERO;
|
|
663
|
+
const tokenPrice = yield sdk.query.pricing.TokenPrice({ denom: denom });
|
|
664
|
+
if (!tokenPrice.tokenPrice) {
|
|
665
|
+
return;
|
|
666
|
+
}
|
|
667
|
+
const tokenTwap = new bignumber_js_1.BigNumber(tokenPrice.tokenPrice.twap);
|
|
668
|
+
return tokenTwap.multipliedBy(cdpToActualRatio).shiftedBy(-18);
|
|
669
|
+
});
|
|
670
|
+
}
|
|
671
|
+
getUnderlyingDenom(cdpDenom) {
|
|
672
|
+
var _a;
|
|
673
|
+
return (_a = this.sdkProvider.getTokenClient().getCdpUnderlyingToken(cdpDenom)) === null || _a === void 0 ? void 0 : _a.denom;
|
|
674
|
+
}
|
|
77
675
|
}
|
|
78
676
|
exports.CDPModule = CDPModule;
|
|
79
677
|
;
|