carbon-js-sdk 0.2.17 → 0.2.18-dev.0

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.
Files changed (296) hide show
  1. package/lib/CarbonSDK.d.ts +13 -1
  2. package/lib/CarbonSDK.js +31 -2
  3. package/lib/clients/CarbonQueryClient.js +2 -2
  4. package/lib/clients/ETHClient.d.ts +1 -1
  5. package/lib/clients/ETHClient.js +15 -15
  6. package/lib/clients/HydrogenClient.js +1 -1
  7. package/lib/clients/N3Client.js +3 -2
  8. package/lib/clients/NEOClient.d.ts +1 -1
  9. package/lib/clients/NEOClient.js +7 -7
  10. package/lib/clients/TokenClient.d.ts +5 -0
  11. package/lib/clients/TokenClient.js +38 -10
  12. package/lib/clients/ZILClient.d.ts +1 -1
  13. package/lib/clients/ZILClient.js +16 -16
  14. package/lib/codec/book/genesis.d.ts +1 -1
  15. package/lib/codec/book/genesis.js +1 -1
  16. package/lib/codec/book/query.d.ts +1 -1
  17. package/lib/codec/book/query.js +1 -1
  18. package/lib/codec/broker/genesis.d.ts +1 -1
  19. package/lib/codec/broker/genesis.js +1 -1
  20. package/lib/codec/broker/query.d.ts +3 -3
  21. package/lib/codec/broker/query.js +3 -3
  22. package/lib/codec/btcx/denom_cross_chain_info.d.ts +1 -1
  23. package/lib/codec/btcx/denom_cross_chain_info.js +1 -1
  24. package/lib/codec/btcx/query.d.ts +1 -1
  25. package/lib/codec/btcx/query.js +1 -1
  26. package/lib/codec/ccm/genesis.d.ts +1 -1
  27. package/lib/codec/ccm/genesis.js +1 -1
  28. package/lib/codec/cdp/event.d.ts +389 -22
  29. package/lib/codec/cdp/event.js +2494 -83
  30. package/lib/codec/cdp/genesis.d.ts +97 -4
  31. package/lib/codec/cdp/genesis.js +648 -23
  32. package/lib/codec/cdp/query.d.ts +439 -89
  33. package/lib/codec/cdp/query.js +1995 -177
  34. package/lib/codec/cdp/tx.d.ts +760 -96
  35. package/lib/codec/cdp/tx.js +3645 -235
  36. package/lib/codec/coin/event.d.ts +1 -1
  37. package/lib/codec/coin/event.js +1 -1
  38. package/lib/codec/coin/genesis.d.ts +2 -2
  39. package/lib/codec/coin/genesis.js +2 -2
  40. package/lib/codec/coin/proposal.d.ts +1 -1
  41. package/lib/codec/coin/proposal.js +1 -1
  42. package/lib/codec/coin/query.d.ts +2 -2
  43. package/lib/codec/coin/query.js +2 -2
  44. package/lib/codec/coin/tx.d.ts +1 -1
  45. package/lib/codec/coin/tx.js +1 -1
  46. package/lib/codec/cosmos/auth/v1beta1/genesis.d.ts +1 -1
  47. package/lib/codec/cosmos/auth/v1beta1/genesis.js +1 -1
  48. package/lib/codec/cosmos/auth/v1beta1/query.d.ts +2 -2
  49. package/lib/codec/cosmos/auth/v1beta1/query.js +2 -2
  50. package/lib/codec/cosmos/authz/v1beta1/genesis.d.ts +1 -1
  51. package/lib/codec/cosmos/authz/v1beta1/genesis.js +1 -1
  52. package/lib/codec/cosmos/authz/v1beta1/query.d.ts +2 -2
  53. package/lib/codec/cosmos/authz/v1beta1/query.js +2 -2
  54. package/lib/codec/cosmos/authz/v1beta1/tx.d.ts +1 -1
  55. package/lib/codec/cosmos/authz/v1beta1/tx.js +1 -1
  56. package/lib/codec/cosmos/bank/v1beta1/authz.d.ts +1 -1
  57. package/lib/codec/cosmos/bank/v1beta1/authz.js +1 -1
  58. package/lib/codec/cosmos/bank/v1beta1/bank.d.ts +1 -1
  59. package/lib/codec/cosmos/bank/v1beta1/bank.js +1 -1
  60. package/lib/codec/cosmos/bank/v1beta1/genesis.d.ts +2 -2
  61. package/lib/codec/cosmos/bank/v1beta1/genesis.js +2 -2
  62. package/lib/codec/cosmos/bank/v1beta1/query.d.ts +3 -3
  63. package/lib/codec/cosmos/bank/v1beta1/query.js +3 -3
  64. package/lib/codec/cosmos/bank/v1beta1/tx.d.ts +2 -2
  65. package/lib/codec/cosmos/bank/v1beta1/tx.js +2 -2
  66. package/lib/codec/cosmos/base/tendermint/v1beta1/query.d.ts +1 -1
  67. package/lib/codec/cosmos/base/tendermint/v1beta1/query.js +1 -1
  68. package/lib/codec/cosmos/capability/v1beta1/genesis.d.ts +1 -1
  69. package/lib/codec/cosmos/capability/v1beta1/genesis.js +1 -1
  70. package/lib/codec/cosmos/crisis/v1beta1/genesis.d.ts +1 -1
  71. package/lib/codec/cosmos/crisis/v1beta1/genesis.js +1 -1
  72. package/lib/codec/cosmos/distribution/v1beta1/distribution.d.ts +1 -1
  73. package/lib/codec/cosmos/distribution/v1beta1/distribution.js +1 -1
  74. package/lib/codec/cosmos/distribution/v1beta1/genesis.d.ts +2 -2
  75. package/lib/codec/cosmos/distribution/v1beta1/genesis.js +2 -2
  76. package/lib/codec/cosmos/distribution/v1beta1/query.d.ts +3 -3
  77. package/lib/codec/cosmos/distribution/v1beta1/query.js +3 -3
  78. package/lib/codec/cosmos/distribution/v1beta1/tx.d.ts +1 -1
  79. package/lib/codec/cosmos/distribution/v1beta1/tx.js +1 -1
  80. package/lib/codec/cosmos/evidence/v1beta1/query.d.ts +1 -1
  81. package/lib/codec/cosmos/evidence/v1beta1/query.js +1 -1
  82. package/lib/codec/cosmos/feegrant/v1beta1/feegrant.d.ts +1 -1
  83. package/lib/codec/cosmos/feegrant/v1beta1/feegrant.js +1 -1
  84. package/lib/codec/cosmos/feegrant/v1beta1/genesis.d.ts +1 -1
  85. package/lib/codec/cosmos/feegrant/v1beta1/genesis.js +1 -1
  86. package/lib/codec/cosmos/feegrant/v1beta1/query.d.ts +2 -2
  87. package/lib/codec/cosmos/feegrant/v1beta1/query.js +2 -2
  88. package/lib/codec/cosmos/gov/v1beta1/genesis.d.ts +1 -1
  89. package/lib/codec/cosmos/gov/v1beta1/genesis.js +1 -1
  90. package/lib/codec/cosmos/gov/v1beta1/gov.d.ts +1 -1
  91. package/lib/codec/cosmos/gov/v1beta1/gov.js +1 -1
  92. package/lib/codec/cosmos/gov/v1beta1/query.d.ts +2 -2
  93. package/lib/codec/cosmos/gov/v1beta1/query.js +4 -4
  94. package/lib/codec/cosmos/gov/v1beta1/tx.d.ts +2 -2
  95. package/lib/codec/cosmos/gov/v1beta1/tx.js +4 -4
  96. package/lib/codec/cosmos/mint/v1beta1/genesis.d.ts +1 -1
  97. package/lib/codec/cosmos/mint/v1beta1/genesis.js +1 -1
  98. package/lib/codec/cosmos/mint/v1beta1/query.d.ts +1 -1
  99. package/lib/codec/cosmos/mint/v1beta1/query.js +1 -1
  100. package/lib/codec/cosmos/params/v1beta1/query.d.ts +1 -1
  101. package/lib/codec/cosmos/params/v1beta1/query.js +1 -1
  102. package/lib/codec/cosmos/slashing/v1beta1/genesis.d.ts +1 -1
  103. package/lib/codec/cosmos/slashing/v1beta1/genesis.js +1 -1
  104. package/lib/codec/cosmos/slashing/v1beta1/query.d.ts +2 -2
  105. package/lib/codec/cosmos/slashing/v1beta1/query.js +2 -2
  106. package/lib/codec/cosmos/staking/v1beta1/authz.d.ts +1 -1
  107. package/lib/codec/cosmos/staking/v1beta1/authz.js +1 -1
  108. package/lib/codec/cosmos/staking/v1beta1/genesis.d.ts +1 -1
  109. package/lib/codec/cosmos/staking/v1beta1/genesis.js +1 -1
  110. package/lib/codec/cosmos/staking/v1beta1/query.d.ts +2 -2
  111. package/lib/codec/cosmos/staking/v1beta1/query.js +2 -2
  112. package/lib/codec/cosmos/staking/v1beta1/staking.d.ts +1 -1
  113. package/lib/codec/cosmos/staking/v1beta1/staking.js +1 -1
  114. package/lib/codec/cosmos/staking/v1beta1/tx.d.ts +2 -2
  115. package/lib/codec/cosmos/staking/v1beta1/tx.js +2 -2
  116. package/lib/codec/cosmos/tx/signing/v1beta1/signing.d.ts +1 -1
  117. package/lib/codec/cosmos/tx/signing/v1beta1/signing.js +1 -1
  118. package/lib/codec/cosmos/tx/v1beta1/service.d.ts +3 -3
  119. package/lib/codec/cosmos/tx/v1beta1/service.js +3 -3
  120. package/lib/codec/cosmos/tx/v1beta1/tx.d.ts +3 -3
  121. package/lib/codec/cosmos/tx/v1beta1/tx.js +5 -5
  122. package/lib/codec/cosmos/upgrade/v1beta1/query.d.ts +1 -1
  123. package/lib/codec/cosmos/upgrade/v1beta1/query.js +1 -1
  124. package/lib/codec/cosmos/vesting/v1beta1/tx.d.ts +1 -1
  125. package/lib/codec/cosmos/vesting/v1beta1/tx.js +1 -1
  126. package/lib/codec/cosmos/vesting/v1beta1/vesting.d.ts +2 -2
  127. package/lib/codec/cosmos/vesting/v1beta1/vesting.js +2 -2
  128. package/lib/codec/fee/genesis.d.ts +1 -1
  129. package/lib/codec/fee/genesis.js +1 -1
  130. package/lib/codec/fee/proposal.d.ts +1 -1
  131. package/lib/codec/fee/proposal.js +1 -1
  132. package/lib/codec/fee/query.d.ts +1 -1
  133. package/lib/codec/fee/query.js +1 -1
  134. package/lib/codec/fee/tx.d.ts +1 -1
  135. package/lib/codec/fee/tx.js +1 -1
  136. package/lib/codec/headersync/genesis.d.ts +1 -1
  137. package/lib/codec/headersync/genesis.js +1 -1
  138. package/lib/codec/headersync/query.d.ts +1 -1
  139. package/lib/codec/headersync/query.js +1 -1
  140. package/lib/codec/index.d.ts +89 -18
  141. package/lib/codec/index.js +463 -180
  142. package/lib/codec/inflation/genesis.d.ts +1 -1
  143. package/lib/codec/inflation/genesis.js +1 -1
  144. package/lib/codec/inflation/query.d.ts +1 -1
  145. package/lib/codec/inflation/query.js +1 -1
  146. package/lib/codec/insurance/genesis.d.ts +1 -1
  147. package/lib/codec/insurance/genesis.js +1 -1
  148. package/lib/codec/leverage/genesis.d.ts +1 -1
  149. package/lib/codec/leverage/genesis.js +1 -1
  150. package/lib/codec/leverage/query.d.ts +1 -1
  151. package/lib/codec/leverage/query.js +1 -1
  152. package/lib/codec/liquidation/genesis.d.ts +1 -1
  153. package/lib/codec/liquidation/genesis.js +1 -1
  154. package/lib/codec/liquidation/query.d.ts +1 -1
  155. package/lib/codec/liquidation/query.js +1 -1
  156. package/lib/codec/liquidation/quote_changes.d.ts +1 -1
  157. package/lib/codec/liquidation/quote_changes.js +1 -1
  158. package/lib/codec/liquiditypool/event.d.ts +2 -2
  159. package/lib/codec/liquiditypool/event.js +2 -2
  160. package/lib/codec/liquiditypool/genesis.d.ts +2 -2
  161. package/lib/codec/liquiditypool/genesis.js +2 -2
  162. package/lib/codec/liquiditypool/liquiditypool.d.ts +3 -0
  163. package/lib/codec/liquiditypool/liquiditypool.js +44 -1
  164. package/lib/codec/liquiditypool/proposal.d.ts +1 -1
  165. package/lib/codec/liquiditypool/proposal.js +1 -1
  166. package/lib/codec/liquiditypool/query.d.ts +39 -2
  167. package/lib/codec/liquiditypool/query.js +195 -3
  168. package/lib/codec/liquiditypool/tx.d.ts +3 -1
  169. package/lib/codec/liquiditypool/tx.js +35 -1
  170. package/lib/codec/lockproxy/genesis.d.ts +1 -1
  171. package/lib/codec/lockproxy/genesis.js +1 -1
  172. package/lib/codec/lockproxy/query.d.ts +1 -1
  173. package/lib/codec/lockproxy/query.js +1 -1
  174. package/lib/codec/market/event.d.ts +1 -1
  175. package/lib/codec/market/event.js +1 -1
  176. package/lib/codec/market/genesis.d.ts +1 -1
  177. package/lib/codec/market/genesis.js +1 -1
  178. package/lib/codec/market/proposal.d.ts +1 -1
  179. package/lib/codec/market/proposal.js +1 -1
  180. package/lib/codec/market/query.d.ts +1 -1
  181. package/lib/codec/market/query.js +1 -1
  182. package/lib/codec/market/tx.d.ts +1 -1
  183. package/lib/codec/market/tx.js +1 -1
  184. package/lib/codec/marketstats/genesis.d.ts +1 -1
  185. package/lib/codec/marketstats/genesis.js +1 -1
  186. package/lib/codec/marketstats/query.d.ts +2 -2
  187. package/lib/codec/marketstats/query.js +2 -2
  188. package/lib/codec/misc/message.d.ts +1 -1
  189. package/lib/codec/misc/message.js +1 -1
  190. package/lib/codec/misc/query.d.ts +3 -3
  191. package/lib/codec/misc/query.js +3 -3
  192. package/lib/codec/misc/transaction.d.ts +1 -1
  193. package/lib/codec/misc/transaction.js +1 -1
  194. package/lib/codec/oracle/event.d.ts +1 -1
  195. package/lib/codec/oracle/event.js +1 -1
  196. package/lib/codec/oracle/genesis.d.ts +4 -1
  197. package/lib/codec/oracle/genesis.js +34 -3
  198. package/lib/codec/oracle/proposal.d.ts +1 -1
  199. package/lib/codec/oracle/proposal.js +1 -1
  200. package/lib/codec/oracle/query.d.ts +2 -2
  201. package/lib/codec/oracle/query.js +2 -2
  202. package/lib/codec/order/event.d.ts +1 -1
  203. package/lib/codec/order/event.js +1 -1
  204. package/lib/codec/order/genesis.d.ts +1 -1
  205. package/lib/codec/order/genesis.js +1 -1
  206. package/lib/codec/order/query.d.ts +1 -1
  207. package/lib/codec/order/query.js +1 -1
  208. package/lib/codec/position/event.d.ts +1 -1
  209. package/lib/codec/position/event.js +1 -1
  210. package/lib/codec/position/genesis.d.ts +1 -1
  211. package/lib/codec/position/genesis.js +1 -1
  212. package/lib/codec/position/query.d.ts +1 -1
  213. package/lib/codec/position/query.js +1 -1
  214. package/lib/codec/pricing/event.d.ts +56 -1
  215. package/lib/codec/pricing/event.js +298 -2
  216. package/lib/codec/pricing/genesis.d.ts +2 -1
  217. package/lib/codec/pricing/genesis.js +20 -5
  218. package/lib/codec/pricing/pricing.d.ts +14 -0
  219. package/lib/codec/pricing/pricing.js +103 -1
  220. package/lib/codec/pricing/query.d.ts +68 -1
  221. package/lib/codec/pricing/query.js +321 -2
  222. package/lib/codec/pricing/tx.d.ts +96 -0
  223. package/lib/codec/pricing/tx.js +399 -1
  224. package/lib/codec/profile/event.d.ts +1 -1
  225. package/lib/codec/profile/event.js +1 -1
  226. package/lib/codec/profile/genesis.d.ts +1 -1
  227. package/lib/codec/profile/genesis.js +1 -1
  228. package/lib/codec/profile/query.d.ts +1 -1
  229. package/lib/codec/profile/query.js +1 -1
  230. package/lib/codec/subaccount/genesis.d.ts +1 -1
  231. package/lib/codec/subaccount/genesis.js +1 -1
  232. package/lib/codec/subaccount/query.d.ts +1 -1
  233. package/lib/codec/subaccount/query.js +1 -1
  234. package/lib/codec/tendermint/abci/types.d.ts +4 -4
  235. package/lib/codec/tendermint/abci/types.js +4 -4
  236. package/lib/codec/tendermint/types/block.d.ts +2 -2
  237. package/lib/codec/tendermint/types/block.js +2 -2
  238. package/lib/codec/tendermint/types/evidence.d.ts +2 -2
  239. package/lib/codec/tendermint/types/evidence.js +2 -2
  240. package/lib/codec/tendermint/types/types.d.ts +3 -3
  241. package/lib/codec/tendermint/types/types.js +3 -3
  242. package/lib/codec/tendermint/types/validator.d.ts +1 -1
  243. package/lib/codec/tendermint/types/validator.js +1 -1
  244. package/lib/constant/generic.js +1 -1
  245. package/lib/constant/token.d.ts +4 -0
  246. package/lib/constant/token.js +5 -1
  247. package/lib/modules/admin.d.ts +114 -7
  248. package/lib/modules/admin.js +275 -53
  249. package/lib/modules/cdp.d.ts +152 -13
  250. package/lib/modules/cdp.js +800 -20
  251. package/lib/modules/gov.js +14 -14
  252. package/lib/modules/liquiditypool.d.ts +3 -0
  253. package/lib/modules/liquiditypool.js +3 -1
  254. package/lib/provider/account/EthLedgerAccount/EthLedgerAccount.js +1 -1
  255. package/lib/provider/account/NeoLedgerAccount/N3Ledger/ErrorCode.js +1 -1
  256. package/lib/provider/account/NeoLedgerAccount/N3Ledger/main.js +8 -8
  257. package/lib/provider/account/NeoLedgerAccount/NeoLedgerAccount.js +3 -3
  258. package/lib/provider/account/NeoLedgerAccount/NeonLedger.js +1 -1
  259. package/lib/provider/amino/types/admin.js +91 -26
  260. package/lib/provider/amino/types/bank.js +1 -1
  261. package/lib/provider/amino/types/broker.js +1 -1
  262. package/lib/provider/amino/types/cdp.js +135 -24
  263. package/lib/provider/amino/types/coin.js +2 -2
  264. package/lib/provider/amino/types/gov.js +4 -4
  265. package/lib/provider/amino/types/ibc.js +1 -1
  266. package/lib/provider/amino/types/leverage.js +1 -1
  267. package/lib/provider/amino/types/liquidityPool.js +9 -7
  268. package/lib/provider/amino/types/market.js +1 -1
  269. package/lib/provider/amino/types/oracle.js +1 -1
  270. package/lib/provider/amino/types/order.js +4 -4
  271. package/lib/provider/amino/types/position.js +1 -1
  272. package/lib/provider/amino/types/profile.js +1 -1
  273. package/lib/provider/amino/types/staking.js +4 -4
  274. package/lib/provider/amino/types/subaccount.js +3 -3
  275. package/lib/provider/amino/utils.js +11 -11
  276. package/lib/provider/keplr/KeplrStore.js +1 -1
  277. package/lib/provider/ledger/ledger.js +2 -2
  278. package/lib/provider/metamask/MetaMask.js +1 -1
  279. package/lib/provider/o3/O3Wallet.js +2 -2
  280. package/lib/util/address.d.ts +1 -0
  281. package/lib/util/address.js +21 -13
  282. package/lib/util/api.js +4 -4
  283. package/lib/util/fetch.js +1 -1
  284. package/lib/util/generic.js +3 -3
  285. package/lib/util/number.d.ts +1 -0
  286. package/lib/util/number.js +7 -6
  287. package/lib/util/tx.d.ts +74 -10
  288. package/lib/wallet/CarbonSigner.js +2 -2
  289. package/lib/wallet/CarbonSigningClient.js +8 -8
  290. package/lib/wallet/CarbonWallet.js +6 -7
  291. package/lib/websocket/channel.js +115 -21
  292. package/lib/websocket/connector.js +3 -3
  293. package/lib/websocket/models.d.ts +156 -0
  294. package/lib/websocket/types.d.ts +87 -3
  295. package/lib/websocket/types.js +31 -0
  296. package/package.json +1 -1
@@ -13,67 +13,847 @@ 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"));
25
+ const constant_1 = require("../constant");
26
+ const TokenClient_1 = __importDefault(require("../clients/TokenClient"));
27
+ const address_1 = require("../util/address");
20
28
  class CDPModule extends base_1.default {
21
- addCollateral(params, opts) {
29
+ supplyAsset(params, opts) {
22
30
  return __awaiter(this, void 0, void 0, function* () {
23
31
  const wallet = this.getWallet();
24
- const value = tx_1.MsgAddCollateral.fromPartial({
32
+ const value = tx_1.MsgSupplyAsset.fromPartial({
25
33
  creator: wallet.bech32Address,
26
- vaultTypeId: new long_1.default(params.vaultTypeId),
34
+ denom: params.denom,
27
35
  amount: params.amount.toString(10),
28
36
  });
29
37
  return yield wallet.sendTx({
30
- typeUrl: util_1.CarbonTx.Types.MsgAddCollateral,
31
- value,
38
+ typeUrl: util_1.CarbonTx.Types.MsgSupplyAsset,
39
+ value
32
40
  }, opts);
33
41
  });
34
42
  }
35
- removeCollateral(params, opts) {
43
+ withdrawAsset(params, opts) {
36
44
  return __awaiter(this, void 0, void 0, function* () {
37
45
  const wallet = this.getWallet();
38
- const value = tx_1.MsgRemoveCollateral.fromPartial({
46
+ const value = tx_1.MsgWithdrawAsset.fromPartial({
39
47
  creator: wallet.bech32Address,
40
- vaultTypeId: new long_1.default(params.vaultTypeId),
48
+ cdpDenom: params.cdpDenom,
41
49
  amount: params.amount.toString(10),
42
50
  });
43
51
  return yield wallet.sendTx({
44
- typeUrl: util_1.CarbonTx.Types.MsgRemoveCollateral,
45
- value,
52
+ typeUrl: util_1.CarbonTx.Types.MsgWithdrawAsset,
53
+ value
46
54
  }, opts);
47
55
  });
48
56
  }
49
- addDebt(params, opts) {
57
+ lockCollateral(params, opts) {
50
58
  return __awaiter(this, void 0, void 0, function* () {
51
59
  const wallet = this.getWallet();
52
- const value = tx_1.MsgAddDebt.fromPartial({
60
+ const value = tx_1.MsgLockCollateral.fromPartial({
53
61
  creator: wallet.bech32Address,
54
- vaultTypeId: new long_1.default(params.vaultTypeId),
62
+ cdpDenom: params.cdpDenom,
55
63
  amount: params.amount.toString(10),
56
64
  });
57
65
  return yield wallet.sendTx({
58
- typeUrl: util_1.CarbonTx.Types.MsgAddDebt,
59
- value,
66
+ typeUrl: util_1.CarbonTx.Types.MsgLockCollateral,
67
+ value
60
68
  }, opts);
61
69
  });
62
70
  }
63
- removeDebt(params, opts) {
71
+ unlockCollateral(params, opts) {
64
72
  return __awaiter(this, void 0, void 0, function* () {
65
73
  const wallet = this.getWallet();
66
- const value = tx_1.MsgRemoveDebt.fromPartial({
74
+ const value = tx_1.MsgUnlockCollateral.fromPartial({
67
75
  creator: wallet.bech32Address,
68
- vaultTypeId: new long_1.default(params.vaultTypeId),
76
+ cdpDenom: params.cdpDenom,
69
77
  amount: params.amount.toString(10),
70
78
  });
71
79
  return yield wallet.sendTx({
72
- typeUrl: util_1.CarbonTx.Types.MsgRemoveDebt,
80
+ typeUrl: util_1.CarbonTx.Types.MsgUnlockCollateral,
81
+ value
82
+ }, opts);
83
+ });
84
+ }
85
+ borrowAsset(params, opts) {
86
+ return __awaiter(this, void 0, void 0, function* () {
87
+ const wallet = this.getWallet();
88
+ const value = tx_1.MsgBorrowAsset.fromPartial({
89
+ creator: wallet.bech32Address,
90
+ denom: params.denom,
91
+ amount: params.amount.toString(10),
92
+ });
93
+ return yield wallet.sendTx({
94
+ typeUrl: util_1.CarbonTx.Types.MsgBorrowAsset,
95
+ value
96
+ }, opts);
97
+ });
98
+ }
99
+ repayAsset(params, opts) {
100
+ return __awaiter(this, void 0, void 0, function* () {
101
+ const wallet = this.getWallet();
102
+ const value = tx_1.MsgRepayAsset.fromPartial({
103
+ creator: wallet.bech32Address,
104
+ denom: params.denom,
105
+ amount: params.amount.toString(10),
106
+ });
107
+ return yield wallet.sendTx({
108
+ typeUrl: util_1.CarbonTx.Types.MsgRepayAsset,
109
+ value
110
+ }, opts);
111
+ });
112
+ }
113
+ supplyAssetAndLockCollateral(params, opts) {
114
+ return __awaiter(this, void 0, void 0, function* () {
115
+ const wallet = this.getWallet();
116
+ const value = tx_1.MsgSupplyAssetAndLockCollateral.fromPartial({
117
+ creator: wallet.bech32Address,
118
+ denom: params.denom,
119
+ supplyAmount: params.supplyAmount.toString(10),
120
+ lockAmount: params.lockAmount.toString(10),
121
+ });
122
+ return yield wallet.sendTx({
123
+ typeUrl: util_1.CarbonTx.Types.MsgSupplyAssetAndLockCollateral,
124
+ value
125
+ }, opts);
126
+ });
127
+ }
128
+ unlockCollateralAndWithdrawAsset(params, opts) {
129
+ return __awaiter(this, void 0, void 0, function* () {
130
+ const wallet = this.getWallet();
131
+ const value = tx_1.MsgUnlockCollateralAndWithdrawAsset.fromPartial({
132
+ creator: wallet.bech32Address,
133
+ cdpDenom: params.cdpDenom,
134
+ unlockAmount: params.unlockAmount.toString(10),
135
+ withdrawAmount: params.withdrawAmount.toString(10),
136
+ });
137
+ return yield wallet.sendTx({
138
+ typeUrl: util_1.CarbonTx.Types.MsgUnlockCollateralAndWithdrawAsset,
139
+ value
140
+ }, opts);
141
+ });
142
+ }
143
+ liquidateCollateral(params, opts) {
144
+ return __awaiter(this, void 0, void 0, function* () {
145
+ const wallet = this.getWallet();
146
+ const value = tx_1.MsgLiquidateCollateral.fromPartial({
147
+ creator: wallet.bech32Address,
148
+ debtor: params.debtor,
149
+ collateralDenom: params.collateralDenom,
150
+ minCollateralAmount: params.minCollateralAmount.toString(10),
151
+ debtDenom: params.debtDenom,
152
+ debtAmount: params.debtAmount.toString(10),
153
+ });
154
+ return yield wallet.sendTx({
155
+ typeUrl: util_1.CarbonTx.Types.MsgLiquidateCollateral,
156
+ value
157
+ }, opts);
158
+ });
159
+ }
160
+ liquidateCollateralWithCdpTokens(params, opts) {
161
+ return __awaiter(this, void 0, void 0, function* () {
162
+ const wallet = this.getWallet();
163
+ const value = tx_1.MsgLiquidateCollateralWithCdpTokens.fromPartial({
164
+ creator: wallet.bech32Address,
165
+ debtor: params.debtor,
166
+ collateralDenom: params.collateralDenom,
167
+ minCollateralAmount: params.minCollateralAmount.toString(10),
168
+ debtDenom: params.debtDenom,
169
+ debtAmount: params.debtAmount.toString(10),
170
+ debtCollateralDenom: params.debtCollateralDenom,
171
+ debtCollateralAmount: params.debtCollateralAmount.toString(10)
172
+ });
173
+ return yield wallet.sendTx({
174
+ typeUrl: util_1.CarbonTx.Types.MsgLiquidateCollateralWithCdpTokens,
175
+ value
176
+ }, opts);
177
+ });
178
+ }
179
+ liquidateCollateralWithCollateral(params, opts) {
180
+ return __awaiter(this, void 0, void 0, function* () {
181
+ const wallet = this.getWallet();
182
+ const value = tx_1.MsgLiquidateCollateralWithCollateral.fromPartial({
183
+ creator: wallet.bech32Address,
184
+ debtor: params.debtor,
185
+ collateralDenom: params.collateralDenom,
186
+ minCollateralAmount: params.minCollateralAmount.toString(10),
187
+ debtDenom: params.debtDenom,
188
+ debtAmount: params.debtAmount.toString(10),
189
+ debtCollateralDenom: params.debtCollateralDenom,
190
+ debtCollateralAmount: params.debtCollateralAmount.toString(10)
191
+ });
192
+ return yield wallet.sendTx({
193
+ typeUrl: util_1.CarbonTx.Types.MsgLiquidateCollateralWithCollateral,
194
+ value
195
+ }, opts);
196
+ });
197
+ }
198
+ liquidateCollateralWithStablecoin(params, opts) {
199
+ return __awaiter(this, void 0, void 0, function* () {
200
+ const wallet = this.getWallet();
201
+ const value = tx_1.MsgLiquidateCollateralWithStablecoin.fromPartial({
202
+ creator: wallet.bech32Address,
203
+ debtor: params.debtor,
204
+ collateralDenom: params.collateralDenom,
205
+ minCollateralAmount: params.minCollateralAmount.toString(10),
206
+ debtDenom: params.debtDenom,
207
+ debtAmount: params.debtAmount.toString(10),
208
+ interestDenom: params.interestDenom,
209
+ interestAmount: params.interestAmount.toString(10),
210
+ });
211
+ return yield wallet.sendTx({
212
+ typeUrl: util_1.CarbonTx.Types.MsgLiquidateCollateralWithStablecoin,
213
+ value
214
+ }, opts);
215
+ });
216
+ }
217
+ repayAssetWithCdpTokens(params, opts) {
218
+ return __awaiter(this, void 0, void 0, function* () {
219
+ const wallet = this.getWallet();
220
+ const debtor = params.debtor ? params.debtor : wallet.bech32Address;
221
+ const value = tx_1.MsgRepayAssetWithCdpTokens.fromPartial({
222
+ creator: wallet.bech32Address,
223
+ debtor: debtor,
224
+ debtDenom: params.debtDenom,
225
+ cdpDenom: params.cdpDenom,
226
+ cdpAmount: params.cdpAmount.toString(10),
227
+ });
228
+ return yield wallet.sendTx({
229
+ typeUrl: util_1.CarbonTx.Types.MsgRepayAssetWithCdpTokens,
230
+ value
231
+ }, opts);
232
+ });
233
+ }
234
+ repayAssetWithCollateral(params, opts) {
235
+ return __awaiter(this, void 0, void 0, function* () {
236
+ const wallet = this.getWallet();
237
+ const debtor = params.debtor ? params.debtor : wallet.bech32Address;
238
+ const value = tx_1.MsgRepayAssetWithCollateral.fromPartial({
239
+ creator: wallet.bech32Address,
240
+ debtor: debtor,
241
+ debtDenom: params.debtDenom,
242
+ cdpDenom: params.cdpDenom,
243
+ cdpAmount: params.cdpAmount.toString(10),
244
+ });
245
+ return yield wallet.sendTx({
246
+ typeUrl: util_1.CarbonTx.Types.MsgRepayAssetWithCollateral,
247
+ value
248
+ }, opts);
249
+ });
250
+ }
251
+ mintStablecoin(params, opts) {
252
+ return __awaiter(this, void 0, void 0, function* () {
253
+ const wallet = this.getWallet();
254
+ const value = tx_1.MsgMintStablecoin.fromPartial({
255
+ creator: wallet.bech32Address,
256
+ amount: params.amount.toString(10),
257
+ });
258
+ return yield wallet.sendTx({
259
+ typeUrl: util_1.CarbonTx.Types.MsgMintStablecoin,
260
+ value,
261
+ }, opts);
262
+ });
263
+ }
264
+ returnStablecoin(params, opts) {
265
+ return __awaiter(this, void 0, void 0, function* () {
266
+ const wallet = this.getWallet();
267
+ const value = tx_1.MsgReturnStablecoin.fromPartial({
268
+ creator: wallet.bech32Address,
269
+ principalAmount: params.principalAmount.toString(10),
270
+ interestDenom: params.interestDenom,
271
+ interestAmount: params.interestAmount.toString(10),
272
+ });
273
+ return yield wallet.sendTx({
274
+ typeUrl: util_1.CarbonTx.Types.MsgReturnStablecoin,
275
+ value,
276
+ }, opts);
277
+ });
278
+ }
279
+ updateRateStrategy(params, opts) {
280
+ return __awaiter(this, void 0, void 0, function* () {
281
+ const wallet = this.getWallet();
282
+ const value = tx_1.MsgUpdateRateStrategy.fromPartial({
283
+ creator: wallet.bech32Address,
284
+ rateStrategyParams: params.rateStrategyParams
285
+ });
286
+ return yield wallet.sendTx({
287
+ typeUrl: util_1.CarbonTx.Types.MsgUpdateRateStrategy,
288
+ value,
289
+ }, opts);
290
+ });
291
+ }
292
+ claimRewards(opts) {
293
+ return __awaiter(this, void 0, void 0, function* () {
294
+ const wallet = this.getWallet();
295
+ const value = tx_1.MsgClaimRewards.fromPartial({
296
+ creator: wallet.bech32Address,
297
+ });
298
+ return yield wallet.sendTx({
299
+ typeUrl: util_1.CarbonTx.Types.MsgClaimRewards,
300
+ value,
301
+ }, opts);
302
+ });
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,
73
320
  value,
74
321
  }, opts);
75
322
  });
76
323
  }
324
+ // start of cdp calculations
325
+ getAccountData(account) {
326
+ var _a;
327
+ return __awaiter(this, void 0, void 0, function* () {
328
+ const sdk = this.sdkProvider;
329
+ const debtInfoResponse = yield sdk.query.cdp.TokenDebtAll(query_3.QueryTokenDebtAllRequest.fromPartial({}));
330
+ const debtInfos = debtInfoResponse.debtInfosAll;
331
+ const collateralsRsp = yield sdk.query.cdp.AccountCollateralAll(query_3.QueryAccountCollateralAllRequest.fromPartial({ address: account }));
332
+ const collaterals = collateralsRsp.collaterals;
333
+ const assetParamsRsp = yield sdk.query.cdp.AssetAll(query_3.QueryAssetAllRequest.fromPartial({}));
334
+ const assetParams = assetParamsRsp.assetParamsAll;
335
+ let totalCollateralsUsd = number_1.BN_ZERO;
336
+ let availableBorrowsUsd = number_1.BN_ZERO;
337
+ let currLiquidationThreshold = number_1.BN_ZERO;
338
+ for (let i = 0; i < collaterals.length; i++) {
339
+ const amount = number_1.bnOrZero(collaterals[i].collateralAmount);
340
+ if (amount.isZero()) {
341
+ continue; // no collateral for denom
342
+ }
343
+ const denom = collaterals[i].denom;
344
+ const debtInfo = debtInfos.find(d => d.denom === denom);
345
+ if (!debtInfo) {
346
+ continue; // no debt for denom
347
+ }
348
+ const collateralUsdVal = yield this.getCdpTokenUsdVal(collaterals[i].cdpDenom, amount);
349
+ if (!collateralUsdVal) {
350
+ continue;
351
+ }
352
+ const assetParam = assetParams.find(a => a.denom === denom);
353
+ if (!assetParam) {
354
+ continue;
355
+ }
356
+ const ltv = number_1.bnOrZero(assetParam.loanToValue).div(number_1.BN_10000);
357
+ const availableBorrowUsd = collateralUsdVal.times(ltv);
358
+ const liquidationThreshold = number_1.bnOrZero(assetParam.liquidationThreshold).div(number_1.BN_10000);
359
+ const liquidationThresholdVal = collateralUsdVal.times(liquidationThreshold);
360
+ totalCollateralsUsd = totalCollateralsUsd.plus(collateralUsdVal);
361
+ availableBorrowsUsd = availableBorrowsUsd.plus(availableBorrowUsd);
362
+ currLiquidationThreshold = currLiquidationThreshold.plus(liquidationThresholdVal);
363
+ }
364
+ // add token debts
365
+ const debtsRsp = yield sdk.query.cdp.AccountDebtAll(query_1.QueryAccountDebtAllRequest.fromPartial({ address: account }));
366
+ const debts = debtsRsp.debts;
367
+ let totalDebtsUsd = number_1.BN_ZERO;
368
+ for (let i = 0; i < debts.length; i++) {
369
+ const amount = number_1.bnOrZero(debts[i].principalDebt);
370
+ const denom = debts[i].denom;
371
+ if (amount.isZero()) {
372
+ continue;
373
+ }
374
+ const debtInfo = debtInfos.find(d => d.denom === denom);
375
+ if (!debtInfo) {
376
+ continue;
377
+ }
378
+ const tokenDebtUsdVal = yield this.getTotalAccountTokenDebtUsdVal(account, denom, debts[i], debtInfo);
379
+ if (!tokenDebtUsdVal) {
380
+ continue;
381
+ }
382
+ totalDebtsUsd = totalDebtsUsd.plus(tokenDebtUsdVal);
383
+ }
384
+ // add stablecoin debt
385
+ const debtInfoRsp = yield sdk.query.cdp.StablecoinDebt(query_1.QueryStablecoinDebtRequest.fromPartial({}));
386
+ const stablecoinDebtInfo = debtInfoRsp.stablecoinDebtInfo;
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 = number_1.bnOrZero(accountStablecoin.principalDebt).plus(number_1.bnOrZero(accountStablecoin.interestDebt));
392
+ stablecoinDebtUsd = stablecoinDebtAmount.shiftedBy(-stablecoinDecimals);
393
+ totalDebtsUsd = totalDebtsUsd.plus(stablecoinDebtUsd);
394
+ }
395
+ const healthFactor = currLiquidationThreshold.div(totalDebtsUsd);
396
+ return {
397
+ TotalCollateralsUsd: totalCollateralsUsd,
398
+ AvailableBorrowsUsd: availableBorrowsUsd,
399
+ CurrLiquidationThreshold: currLiquidationThreshold,
400
+ TotalDebtsUsd: totalDebtsUsd,
401
+ TotalStablecoinDebtsUsd: stablecoinDebtUsd,
402
+ HealthFactor: healthFactor,
403
+ };
404
+ });
405
+ }
406
+ getAssetBorrowableSupply(denom) {
407
+ var _a;
408
+ return __awaiter(this, void 0, void 0, function* () {
409
+ const sdk = this.sdkProvider;
410
+ const cdpAddress = this.getCdpModuleAddress();
411
+ const balanceRsp = yield sdk.query.bank.Balance(query_2.QueryBalanceRequest.fromPartial({ address: cdpAddress, denom }));
412
+ return number_1.bnOrZero((_a = balanceRsp.balance) === null || _a === void 0 ? void 0 : _a.amount);
413
+ });
414
+ }
415
+ getCdpToActualRatio(cdpDenom) {
416
+ return __awaiter(this, void 0, void 0, function* () {
417
+ const sdk = this.sdkProvider;
418
+ const denom = this.getUnderlyingDenom(cdpDenom);
419
+ const supplyRsp = yield sdk.query.bank.SupplyOf(query_2.QuerySupplyOfRequest.fromPartial({ denom: cdpDenom }));
420
+ const cdpAmountRsp = supplyRsp.amount;
421
+ if (!cdpAmountRsp)
422
+ throw new Error("unable to retrieve cdp token supply");
423
+ const cdpAmount = number_1.bnOrZero(cdpAmountRsp.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");
428
+ const owedAmount = yield this.getTotalTokenDebt(denom);
429
+ const actualAmount = 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
+ });
434
+ }
435
+ getTotalAccountTokenDebtUsdVal(account, denom, debt, debtInfo) {
436
+ return __awaiter(this, void 0, void 0, function* () {
437
+ const amount = yield this.getTotalAccountTokenDebt(account, denom, debt, debtInfo);
438
+ return yield this.getTokenUsdVal(denom, amount);
439
+ });
440
+ }
441
+ getModuleTotalDebtUsdVal() {
442
+ var _a;
443
+ return __awaiter(this, void 0, void 0, function* () {
444
+ const sdk = this.sdkProvider;
445
+ let totalDebt = new bignumber_js_1.BigNumber(0);
446
+ // get token debts
447
+ const allDebtsRes = yield this.sdkProvider.query.cdp.TokenDebtAll({});
448
+ const allDebts = allDebtsRes.debtInfosAll;
449
+ for (let i = 0; i < allDebts.length; i++) {
450
+ const denom = allDebts[i].denom;
451
+ const interest = number_1.bnOrZero(allDebts[i].totalAccumulatedInterest);
452
+ const principal = number_1.bnOrZero(allDebts[i].totalPrincipal);
453
+ const debtAmt = interest.plus(principal);
454
+ const debtUsdVal = yield this.getTokenUsdVal(denom, debtAmt);
455
+ if (!debtUsdVal) {
456
+ return;
457
+ }
458
+ totalDebt = totalDebt.plus(debtUsdVal);
459
+ }
460
+ // get stablecoin debt
461
+ const stablecoinDebtRes = yield this.sdkProvider.query.cdp.StablecoinDebt({});
462
+ if (stablecoinDebtRes.stablecoinDebtInfo) {
463
+ const debtInfo = stablecoinDebtRes.stablecoinDebtInfo;
464
+ const debtAmt = number_1.bnOrZero(debtInfo.totalPrincipal).plus(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);
468
+ }
469
+ return totalDebt;
470
+ });
471
+ }
472
+ getModuleTotalCollateralUsdVal() {
473
+ return __awaiter(this, void 0, void 0, function* () {
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;
481
+ }
482
+ const amount = number_1.bnOrZero(balance.amount);
483
+ const collateralUsdValue = yield this.getCdpTokenUsdVal(balance.denom, amount);
484
+ allCollateralsUsdValue = allCollateralsUsdValue.plus(collateralUsdValue);
485
+ }
486
+ return allCollateralsUsdValue;
487
+ });
488
+ }
489
+ getCdpTokenUsdVal(cdpDenom, amount) {
490
+ return __awaiter(this, void 0, void 0, function* () {
491
+ const denom = this.getUnderlyingDenom(cdpDenom);
492
+ const ratio = yield this.getCdpToActualRatio(cdpDenom);
493
+ const actualTokenAmount = amount.div(ratio);
494
+ return yield this.getTokenUsdVal(denom, actualTokenAmount);
495
+ });
496
+ }
497
+ getTokenUsdVal(denom, amount) {
498
+ return __awaiter(this, void 0, void 0, function* () {
499
+ const sdk = this.sdkProvider;
500
+ const decimals = yield this.sdkProvider.getTokenClient().getDecimals(denom);
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 = number_1.bnOrZero(priceResult.tokenPrice.twap).shiftedBy(-18);
507
+ return amount.multipliedBy(twap).shiftedBy(-decimals);
508
+ });
509
+ }
510
+ getTotalTokenDebt(denom, debtInfo) {
511
+ var _a;
512
+ return __awaiter(this, void 0, void 0, function* () {
513
+ if (!debtInfo) {
514
+ const debtInfoRsp = yield this.sdkProvider.query.cdp.TokenDebt(query_1.QueryTokenDebtRequest.fromPartial({ denom }));
515
+ debtInfo = debtInfoRsp.debtInfo;
516
+ }
517
+ if (!debtInfo)
518
+ throw new Error("unable to retrieve debt info");
519
+ const principal = number_1.bnOrZero(debtInfo.totalPrincipal);
520
+ const accumInterest = number_1.bnOrZero(debtInfo.totalAccumulatedInterest);
521
+ const cdpParamsRsp = yield this.sdkProvider.query.cdp.Params(query_1.QueryParamsRequest.fromPartial({}));
522
+ const interestFee = 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);
525
+ });
526
+ }
527
+ getTotalAccountTokenDebt(account, denom, debt, debtInfo) {
528
+ return __awaiter(this, void 0, void 0, function* () {
529
+ const sdk = this.sdkProvider;
530
+ if (!debtInfo) {
531
+ const debtInfoRsp = yield sdk.query.cdp.TokenDebt(query_1.QueryTokenDebtRequest.fromPartial({ denom }));
532
+ debtInfo = debtInfoRsp.debtInfo;
533
+ }
534
+ if (!debtInfo)
535
+ return number_1.BN_ZERO;
536
+ if (!debt) {
537
+ const debtRes = yield sdk.query.cdp.AccountDebt({ address: account, denom: denom });
538
+ debt = debtRes.debt;
539
+ }
540
+ const principalAmount = number_1.bnOrZero(debt === null || debt === void 0 ? void 0 : debt.principalDebt);
541
+ const initialCIM = number_1.bnOrZero(debt === null || debt === void 0 ? void 0 : debt.initialCumulativeInterestMultiplier);
542
+ if (principalAmount.isZero() || initialCIM.isZero())
543
+ return number_1.BN_ZERO;
544
+ const cim = yield this.recalculateCIM(denom, debtInfo);
545
+ if (!cim)
546
+ throw new Error("unable to retrieve account debt");
547
+ // TODO: change to round up
548
+ const totalAmountTokenDebt = principalAmount.times(cim).dividedToIntegerBy(initialCIM);
549
+ return totalAmountTokenDebt;
550
+ });
551
+ }
552
+ getTotalAccountStablecoinDebt(account, debt, debtInfo) {
553
+ return __awaiter(this, void 0, void 0, function* () {
554
+ const sdk = this.sdkProvider;
555
+ let principalAmount = number_1.BN_ZERO;
556
+ if (!debtInfo) {
557
+ const debtInfoResponse = yield sdk.query.cdp.StablecoinDebt(query_1.QueryStablecoinDebtRequest.fromPartial({}));
558
+ debtInfo = debtInfoResponse.stablecoinDebtInfo;
559
+ }
560
+ if (!debtInfo)
561
+ return number_1.BN_ZERO;
562
+ if (!debt) {
563
+ const debtResp = yield sdk.query.cdp.AccountStablecoin(query_1.QueryAccountStablecoinRequest.fromPartial({ address: account }));
564
+ debt = debtResp;
565
+ }
566
+ principalAmount = number_1.bnOrZero(debt.principalDebt);
567
+ const initialCIM = number_1.bnOrZero(debt.initialCumulativeInterestMultiplier);
568
+ const cim = yield this.recalculateStablecoinCIM(debtInfo);
569
+ if (!cim)
570
+ throw new Error("unable to retrieve account debt");
571
+ return principalAmount.times(cim).dividedToIntegerBy(initialCIM);
572
+ });
573
+ }
574
+ calculateAPY(denom, debtInfo, assetParams, rateStrategyParams) {
575
+ return __awaiter(this, void 0, void 0, function* () {
576
+ const sdk = this.sdkProvider;
577
+ if (!debtInfo) {
578
+ const debtInfoResponse = yield sdk.query.cdp.TokenDebt(query_1.QueryTokenDebtRequest.fromPartial({ denom }));
579
+ debtInfo = debtInfoResponse.debtInfo;
580
+ if (!debtInfo)
581
+ throw new Error("unable to retrieve debt info for " + denom);
582
+ }
583
+ if (!rateStrategyParams) {
584
+ if (!assetParams) {
585
+ const assetResponse = yield sdk.query.cdp.Asset(query_1.QueryAssetRequest.fromPartial({ denom }));
586
+ assetParams = assetResponse.assetParams;
587
+ if (!assetParams) {
588
+ throw new Error("unable to retrieve asset param for " + denom);
589
+ }
590
+ }
591
+ const rateStrategyParamsResponse = yield sdk.query.cdp.RateStrategy(query_1.QueryRateStrategyRequest.fromPartial({
592
+ name: assetParams.rateStrategyName
593
+ }));
594
+ rateStrategyParams = rateStrategyParamsResponse.rateStrategyParams;
595
+ if (!rateStrategyParams) {
596
+ throw new Error("unable to retrieve rate strategy for " + denom);
597
+ }
598
+ }
599
+ return CDPModule.calculateInterestAPY(debtInfo, rateStrategyParams);
600
+ });
601
+ }
602
+ static calculateInterestForTimePeriod(apy, start, end) {
603
+ const diffMs = end.getTime() - start.getTime();
604
+ if (diffMs <= 0) {
605
+ return number_1.BN_ZERO;
606
+ }
607
+ const diffSeconds = new bignumber_js_1.BigNumber(diffMs).shiftedBy(-3).dp(0, bignumber_js_1.BigNumber.ROUND_CEIL);
608
+ const secondsAYear = number_1.bnOrZero(31536000);
609
+ const numPeriods = secondsAYear.div(diffSeconds).dp(18);
610
+ return apy.div(numPeriods).dp(18); // carbon backend sdk.dec max 18 dp
611
+ }
612
+ calculateLendAPY(denom, borrowInterest, debtInfo, params) {
613
+ return __awaiter(this, void 0, void 0, function* () {
614
+ const sdk = this.sdkProvider;
615
+ if (!debtInfo) {
616
+ const debtInfoResponse = yield sdk.query.cdp.TokenDebt(query_1.QueryTokenDebtRequest.fromPartial({ denom }));
617
+ debtInfo = debtInfoResponse.debtInfo;
618
+ if (!debtInfo) {
619
+ throw new Error("unable to retrieve debt info for " + denom);
620
+ }
621
+ }
622
+ if (!borrowInterest) {
623
+ borrowInterest = yield this.calculateAPY(denom, debtInfo);
624
+ }
625
+ if (!params) {
626
+ const paramsResponse = yield sdk.query.cdp.Params(codec_1.QueryCdpParamsRequest.fromPartial({}));
627
+ params = paramsResponse.params;
628
+ if (!params) {
629
+ throw new Error("unable to retrieve cdp params for " + denom);
630
+ }
631
+ }
632
+ const interestFeeRate = number_1.bnOrZero(params.interestFee).div(number_1.BN_10000);
633
+ const utilizationRate = number_1.bnOrZero(debtInfo.utilizationRate).shiftedBy(-18);
634
+ return borrowInterest.times(utilizationRate).times(number_1.BN_ONE.minus(interestFeeRate));
635
+ });
636
+ }
637
+ recalculateCIM(denom, debtInfo) {
638
+ var _a;
639
+ return __awaiter(this, void 0, void 0, function* () {
640
+ const sdk = this.sdkProvider;
641
+ if (!debtInfo) {
642
+ const debtInfoResponse = yield sdk.query.cdp.TokenDebt(query_1.QueryTokenDebtRequest.fromPartial({ denom }));
643
+ debtInfo = debtInfoResponse.debtInfo;
644
+ if (!debtInfo) {
645
+ return number_1.BN_ZERO;
646
+ }
647
+ }
648
+ const cim = number_1.bnOrZero(debtInfo.cumulativeInterestMultiplier);
649
+ const apy = yield this.calculateAPY(denom, debtInfo);
650
+ const interest = CDPModule.calculateInterestForTimePeriod(apy, (_a = debtInfo.lastUpdatedTime) !== null && _a !== void 0 ? _a : new Date(0), new Date());
651
+ const newCIM = cim.times(interest.plus(1));
652
+ return newCIM;
653
+ });
654
+ }
655
+ recalculateStablecoinCIM(debtInfo) {
656
+ var _a, _b;
657
+ return __awaiter(this, void 0, void 0, function* () {
658
+ const sdk = this.sdkProvider;
659
+ if (!debtInfo) {
660
+ const debtInfoResponse = yield sdk.query.cdp.StablecoinDebt(query_1.QueryStablecoinDebtRequest.fromPartial({}));
661
+ debtInfo = debtInfoResponse.stablecoinDebtInfo;
662
+ if (!debtInfo) {
663
+ return number_1.BN_ZERO;
664
+ }
665
+ }
666
+ const paramsResponse = yield sdk.query.cdp.Params(codec_1.QueryCdpParamsRequest.fromPartial({}));
667
+ const cim = number_1.bnOrZero(debtInfo.cumulativeInterestMultiplier);
668
+ const apy = number_1.bnOrZero((_a = paramsResponse.params) === null || _a === void 0 ? void 0 : _a.stablecoinInterestRate);
669
+ if (!apy) {
670
+ return number_1.BN_ZERO;
671
+ }
672
+ const interest = CDPModule.calculateInterestForTimePeriod(apy, (_b = debtInfo.lastUpdatedTime) !== null && _b !== void 0 ? _b : new Date(0), new Date());
673
+ const newCIM = cim.times(interest.plus(1));
674
+ return newCIM;
675
+ });
676
+ }
677
+ getMaxCollateralForUnlock(account, cdpDenom) {
678
+ var _a, _b, _c, _d, _e;
679
+ return __awaiter(this, void 0, void 0, function* () {
680
+ const sdk = this.sdkProvider;
681
+ const denom = this.getUnderlyingDenom(cdpDenom);
682
+ const assetParams = yield sdk.query.cdp.Asset({ denom: denom });
683
+ if (!assetParams.assetParams)
684
+ return;
685
+ let unlockRatio = new bignumber_js_1.BigNumber(assetParams.assetParams.loanToValue);
686
+ if (sdk.getConfig().network === constant_1.Network.LocalHost || sdk.getConfig().network === constant_1.Network.DevNet) {
687
+ unlockRatio = new bignumber_js_1.BigNumber(assetParams.assetParams.liquidationThreshold);
688
+ }
689
+ const accountData = yield this.getAccountData(account);
690
+ const tokenDecimals = (_a = yield sdk.getTokenClient().getDecimals(denom)) !== null && _a !== void 0 ? _a : 0;
691
+ const availableBorrowsUsd = accountData.AvailableBorrowsUsd.minus(accountData.TotalDebtsUsd);
692
+ const unlockableUsd = availableBorrowsUsd.multipliedBy(number_1.BN_10000).div(unlockRatio);
693
+ const tokenPrice = yield sdk.query.pricing.TokenPrice({ denom });
694
+ const tokenTwap = number_1.bnOrZero((_b = tokenPrice.tokenPrice) === null || _b === void 0 ? void 0 : _b.twap);
695
+ if (tokenTwap.isZero())
696
+ throw new Error("unable to retrieve token price for " + denom);
697
+ const tokenAmt = unlockableUsd.div(tokenTwap.shiftedBy(-18)).shiftedBy(tokenDecimals);
698
+ const cdpToActualRatio = (_c = yield this.getCdpToActualRatio(cdpDenom)) !== null && _c !== void 0 ? _c : number_1.BN_ZERO;
699
+ const cdpTokenAmt = tokenAmt.multipliedBy(cdpToActualRatio);
700
+ // take the min of cdpTokensUnlockableAmt and locked tokens
701
+ const accountCollateral = yield sdk.query.cdp.AccountCollateral({
702
+ address: account,
703
+ cdpDenom: cdpDenom
704
+ });
705
+ const lockedAmount = number_1.bnOrZero((_e = (_d = accountCollateral.collateral) === null || _d === void 0 ? void 0 : _d.collateralAmount) !== null && _e !== void 0 ? _e : "0");
706
+ return lockedAmount.lt(cdpTokenAmt) ? lockedAmount : cdpTokenAmt;
707
+ });
708
+ }
709
+ getCdpModuleAddress() {
710
+ if (!this.cdpModuleAddress) {
711
+ const network = this.sdkProvider.getConfig().network;
712
+ this.cdpModuleAddress = address_1.SWTHAddress.getModuleAddress("cdp", network);
713
+ }
714
+ return this.cdpModuleAddress;
715
+ }
716
+ getCdpTokenPrice(cdpDenom) {
717
+ var _a, _b;
718
+ return __awaiter(this, void 0, void 0, function* () {
719
+ const sdk = this.sdkProvider;
720
+ const denom = this.getUnderlyingDenom(cdpDenom);
721
+ const cdpToActualRatio = (_a = yield this.getCdpToActualRatio(cdpDenom)) !== null && _a !== void 0 ? _a : number_1.BN_ZERO;
722
+ const tokenPrice = yield sdk.query.pricing.TokenPrice({ denom: denom });
723
+ const tokenTwap = number_1.bnOrZero((_b = tokenPrice.tokenPrice) === null || _b === void 0 ? void 0 : _b.twap).shiftedBy(-18);
724
+ return tokenTwap.multipliedBy(cdpToActualRatio);
725
+ });
726
+ }
727
+ getUnderlyingDenom(cdpDenom) {
728
+ var _a;
729
+ const denom = (_a = this.sdkProvider.getTokenClient().getCdpUnderlyingToken(cdpDenom)) === null || _a === void 0 ? void 0 : _a.denom;
730
+ if (!denom)
731
+ throw new Error("underlying denom not found for " + cdpDenom);
732
+ return denom;
733
+ }
734
+ getMaxCollateralForLiquidator(debtor, cdpDenom, debtDenom, debtRepaymentAmount) {
735
+ return __awaiter(this, void 0, void 0, function* () {
736
+ const sdk = this.sdkProvider;
737
+ // get the discounted price for the cdp token
738
+ const cdpActualDenom = this.getUnderlyingDenom(cdpDenom);
739
+ const asset = yield sdk.query.cdp.Asset({
740
+ denom: cdpActualDenom
741
+ });
742
+ if (!asset.assetParams)
743
+ throw new Error("unable to retrieve asset param for " + cdpActualDenom);
744
+ const bonus = number_1.bnOrZero(asset.assetParams.liquidationDiscount).div(number_1.BN_10000);
745
+ const cdpTokenPrice = yield this.getCdpTokenPrice(cdpDenom);
746
+ const cdpTokenDiscountedPrice = cdpTokenPrice.multipliedBy(number_1.BN_ONE.minus(bonus));
747
+ // get close factor
748
+ const debtorAccountData = yield sdk.query.cdp.AccountData({
749
+ address: debtor
750
+ });
751
+ const debtorTotalCollateralVal = number_1.bnOrZero(debtorAccountData.totalCollateralsUsd);
752
+ const debtorTotalDebtVal = number_1.bnOrZero(debtorAccountData.totalDebtsUsd);
753
+ const currentLiqThreshold = number_1.bnOrZero(debtorAccountData.currLiquidationThreshold);
754
+ const params = yield sdk.query.cdp.Params({});
755
+ if (!params.params) {
756
+ throw new Error("unable to retrieve cdp params");
757
+ }
758
+ const smallLiqSize = number_1.bnOrZero(params.params.smallLiquidationSize);
759
+ const minCloseFactor = number_1.bnOrZero(params.params.minimumCloseFactor);
760
+ const completeLiqThreshold = number_1.bnOrZero(params.params.completeLiquidationThreshold);
761
+ const closeFactor = this.computeCloseFactor(debtorTotalDebtVal, debtorTotalCollateralVal, currentLiqThreshold, smallLiqSize, minCloseFactor, completeLiqThreshold);
762
+ // get max repayable amount given the debtor's debt and how much liquidator wants to repay
763
+ const debtDecimals = number_1.bnOrZero(yield sdk.getTokenClient().getDecimals(debtDenom));
764
+ const maxRepayableValue = debtorTotalDebtVal.multipliedBy(closeFactor);
765
+ const maxRepayableAmt = maxRepayableValue.shiftedBy(debtDecimals.toNumber());
766
+ if (debtRepaymentAmount.isGreaterThan(maxRepayableAmt)) {
767
+ debtRepaymentAmount = maxRepayableAmt;
768
+ }
769
+ // calculate collateral amount that can be obtained given that debt amount and debtor's collateral balance
770
+ // AND, recalculate debt repay amount if needed
771
+ const cdpTokenDecimals = number_1.bnOrZero(yield sdk.getTokenClient().getDecimals(cdpActualDenom));
772
+ let collateralAmtToLiquidate = this.calculateCollateralRequiredForDebt(number_1.BN_ONE, // assumes USC is $1
773
+ cdpTokenDiscountedPrice, debtRepaymentAmount, cdpTokenDecimals, debtDecimals);
774
+ const debtorAccountCollateral = yield sdk.query.cdp.AccountCollateral({
775
+ address: debtor,
776
+ cdpDenom: cdpDenom,
777
+ });
778
+ if (!debtorAccountCollateral.collateral) {
779
+ throw Error("unable to retrieve debtor's collateral amount");
780
+ }
781
+ const debtorCollateralAmt = new bignumber_js_1.BigNumber(debtorAccountCollateral.collateral.collateralAmount);
782
+ if (collateralAmtToLiquidate.isGreaterThan(debtorCollateralAmt)) {
783
+ collateralAmtToLiquidate = debtorCollateralAmt;
784
+ debtRepaymentAmount = this.calculateDebtCoveredByCollateral(number_1.BN_ONE, cdpTokenDiscountedPrice, collateralAmtToLiquidate, cdpTokenDecimals, debtDecimals);
785
+ }
786
+ // get collateral amt without discount
787
+ const collateralAmountWithoutDiscount = this.calculateCollateralRequiredForDebt(number_1.BN_ONE, cdpTokenPrice, debtRepaymentAmount, cdpTokenDecimals, debtDecimals);
788
+ // get liquidation profit
789
+ const liquidatorProfit = collateralAmtToLiquidate.minus(collateralAmountWithoutDiscount);
790
+ if (liquidatorProfit.isNegative()) {
791
+ throw Error("liquidator's profit is negative");
792
+ }
793
+ // get fee amount
794
+ const liquidationFee = params.params.liquidationFee;
795
+ const liquidationFeeAmount = liquidatorProfit.multipliedBy(liquidationFee).div(10000);
796
+ // return the collateral amount left for the liquidator once fees have been deducted
797
+ return collateralAmtToLiquidate.minus(liquidationFeeAmount);
798
+ });
799
+ }
800
+ computeCloseFactor(borrowedValue, collateralValue, liquidationThreshold, smallLiquidationSize, minimumCloseFactor, completeLiquidationThreshold) {
801
+ if (borrowedValue.isLessThan(liquidationThreshold) ||
802
+ borrowedValue.isLessThan(smallLiquidationSize)) {
803
+ return number_1.BN_ZERO;
804
+ }
805
+ if (completeLiquidationThreshold.isZero()) {
806
+ return number_1.BN_ONE;
807
+ }
808
+ const criticalVal = liquidationThreshold.plus(completeLiquidationThreshold.multipliedBy(collateralValue.minus(liquidationThreshold)));
809
+ const slope = number_1.BN_ONE.minus(minimumCloseFactor).div(criticalVal.minus(liquidationThreshold));
810
+ let closeFactor = minimumCloseFactor.plus(borrowedValue.minus(liquidationThreshold).multipliedBy(slope));
811
+ if (liquidationThreshold.isEqualTo(criticalVal)) {
812
+ closeFactor = minimumCloseFactor;
813
+ }
814
+ if (closeFactor.isGreaterThan(1)) {
815
+ return number_1.BN_ONE;
816
+ }
817
+ if (closeFactor.isLessThan(0)) {
818
+ return number_1.BN_ZERO;
819
+ }
820
+ return closeFactor;
821
+ }
822
+ calculateCollateralRequiredForDebt(debtPrice, collateralPrice, debtAmount, collateralTokenDecimals, debtTokenDecimals) {
823
+ const decimalPower = collateralTokenDecimals.minus(debtTokenDecimals);
824
+ const decimalMultiplier = this.getDecimalMultiplier(decimalPower);
825
+ const res = debtPrice.multipliedBy(debtAmount).multipliedBy(decimalMultiplier).div(collateralPrice);
826
+ return res.decimalPlaces(0, bignumber_js_1.BigNumber.ROUND_CEIL);
827
+ }
828
+ calculateDebtCoveredByCollateral(debtPrice, collateralPrice, collateralAmount, collateralTokenDecimals, debtTokenDecimals) {
829
+ const decimalPower = debtTokenDecimals.minus(collateralTokenDecimals);
830
+ const decimalMultiplier = this.getDecimalMultiplier(decimalPower);
831
+ const res = collateralPrice.multipliedBy(collateralAmount).multipliedBy(decimalMultiplier).div(debtPrice);
832
+ return res.decimalPlaces(0);
833
+ }
834
+ getDecimalMultiplier(n) {
835
+ const ten = new bignumber_js_1.BigNumber(10);
836
+ if (n.isLessThan(0)) {
837
+ return number_1.BN_ONE.div(ten.pow(n.negated()));
838
+ }
839
+ return ten.pow(n);
840
+ }
77
841
  }
78
842
  exports.CDPModule = CDPModule;
843
+ CDPModule.calculateInterestAPY = (debtInfo, rateStrategy) => {
844
+ const utilizationRate = number_1.bnOrZero(debtInfo.utilizationRate).shiftedBy(-18);
845
+ const optimalUsage = number_1.bnOrZero(rateStrategy.optimalUsage).shiftedBy(-4);
846
+ const variableRate1 = number_1.bnOrZero(rateStrategy.variableRateSlope1).shiftedBy(-4);
847
+ const variableRate2 = number_1.bnOrZero(rateStrategy.variableRateSlope2).shiftedBy(-4);
848
+ const baseVariableBorrowRate = number_1.bnOrZero(rateStrategy.baseVariableBorrowRate).shiftedBy(-4);
849
+ if (utilizationRate.lte(optimalUsage)) {
850
+ const vRate = utilizationRate.times(variableRate1).div(optimalUsage).dp(4, bignumber_js_1.BigNumber.ROUND_CEIL);
851
+ return vRate.plus(baseVariableBorrowRate);
852
+ }
853
+ else {
854
+ const ratio = utilizationRate.minus(optimalUsage).div(number_1.BN_ONE.minus(optimalUsage));
855
+ const vRate = ratio.times(variableRate2).plus(variableRate1).dp(4, bignumber_js_1.BigNumber.ROUND_CEIL);
856
+ return vRate.plus(baseVariableBorrowRate);
857
+ }
858
+ };
79
859
  ;