carbon-js-sdk 0.3.16-beta.1 → 0.3.17
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/CarbonQueryClient.js +2 -2
- package/lib/clients/ETHClient.js +15 -15
- package/lib/clients/HydrogenClient.js +1 -1
- package/lib/clients/NEOClient.js +7 -7
- package/lib/clients/ZILClient.js +16 -16
- package/lib/codec/cosmos/gov/v1beta1/query.js +2 -2
- package/lib/codec/cosmos/gov/v1beta1/tx.js +2 -2
- package/lib/codec/cosmos/tx/v1beta1/tx.js +2 -2
- package/lib/constant/generic.js +1 -1
- package/lib/modules/cdp.js +43 -43
- package/lib/modules/gov.js +14 -14
- package/lib/modules/liquiditypool.js +1 -1
- package/lib/provider/account/EthLedgerAccount/EthLedgerAccount.js +1 -1
- package/lib/provider/account/NeoLedgerAccount/N3Ledger/ErrorCode.js +1 -1
- package/lib/provider/account/NeoLedgerAccount/N3Ledger/main.js +8 -8
- package/lib/provider/account/NeoLedgerAccount/NeoLedgerAccount.js +3 -3
- package/lib/provider/account/NeoLedgerAccount/NeonLedger.js +1 -1
- package/lib/provider/amino/types/admin.js +30 -30
- package/lib/provider/amino/types/bank.js +1 -1
- package/lib/provider/amino/types/broker.js +1 -1
- package/lib/provider/amino/types/cdp.js +22 -22
- package/lib/provider/amino/types/coin.js +2 -2
- package/lib/provider/amino/types/gov.js +4 -4
- package/lib/provider/amino/types/ibc.js +1 -1
- package/lib/provider/amino/types/leverage.js +1 -1
- package/lib/provider/amino/types/liquidityPool.js +7 -7
- package/lib/provider/amino/types/market.js +1 -1
- package/lib/provider/amino/types/oracle.js +1 -1
- package/lib/provider/amino/types/order.js +4 -4
- package/lib/provider/amino/types/position.js +1 -1
- package/lib/provider/amino/types/profile.js +1 -1
- package/lib/provider/amino/types/staking.js +4 -4
- package/lib/provider/amino/types/subaccount.js +3 -3
- package/lib/provider/amino/utils.js +11 -11
- package/lib/provider/keplr/KeplrStore.js +1 -1
- package/lib/provider/ledger/ledger.js +2 -2
- package/lib/provider/metamask/MetaMask.js +1 -1
- package/lib/provider/o3/O3Wallet.js +2 -2
- package/lib/util/address.js +13 -13
- package/lib/util/api.js +4 -4
- package/lib/util/fetch.js +1 -1
- package/lib/util/generic.js +3 -3
- package/lib/util/number.js +5 -5
- package/lib/wallet/CarbonSigner.js +2 -2
- package/lib/wallet/CarbonSigningClient.js +8 -8
- package/lib/wallet/CarbonWallet.js +6 -6
- package/lib/websocket/connector.js +3 -3
- package/package.json +1 -1
|
@@ -44,7 +44,7 @@ class CarbonQueryClient {
|
|
|
44
44
|
constructor(tmClient) {
|
|
45
45
|
this.tmClient = tmClient;
|
|
46
46
|
this.baseClient = new stargate_1.QueryClient(this.tmClient);
|
|
47
|
-
const rpcClient =
|
|
47
|
+
const rpcClient = stargate_1.createProtobufRpcClient(this.baseClient);
|
|
48
48
|
this.chain = BlockchainClient_1.default.connectWithTm(this.tmClient);
|
|
49
49
|
this.adl = new query_1.QueryClientImpl(rpcClient);
|
|
50
50
|
this.book = new query_2.QueryClientImpl(rpcClient);
|
|
@@ -85,7 +85,7 @@ class CarbonQueryClient {
|
|
|
85
85
|
};
|
|
86
86
|
}
|
|
87
87
|
getProtobufRpcClient() {
|
|
88
|
-
return
|
|
88
|
+
return stargate_1.createProtobufRpcClient(this.baseClient);
|
|
89
89
|
}
|
|
90
90
|
}
|
|
91
91
|
exports.default = CarbonQueryClient;
|
package/lib/clients/ETHClient.js
CHANGED
|
@@ -40,13 +40,13 @@ class ETHClient {
|
|
|
40
40
|
return __awaiter(this, void 0, void 0, function* () {
|
|
41
41
|
const tokenQueryResults = yield api.token.getAllTokens();
|
|
42
42
|
const lockProxyAddress = this.getLockProxyAddress().toLowerCase();
|
|
43
|
-
const tokens = tokenQueryResults.filter((token) =>
|
|
43
|
+
const tokens = tokenQueryResults.filter((token) => blockchain_1.blockchainForChainId(token.chainId.toNumber()) == this.blockchain &&
|
|
44
44
|
token.tokenAddress.length == 40 &&
|
|
45
|
-
token.bridgeAddress.toLowerCase() ==
|
|
45
|
+
token.bridgeAddress.toLowerCase() == generic_1.stripHexPrefix(lockProxyAddress) &&
|
|
46
46
|
(!whitelistDenoms || whitelistDenoms.includes(token.denom)) &&
|
|
47
|
-
this.verifyChecksum(
|
|
47
|
+
this.verifyChecksum(generic_1.appendHexPrefix(token.tokenAddress)));
|
|
48
48
|
const assetIds = tokens.map((token) => {
|
|
49
|
-
return this.verifyChecksum(
|
|
49
|
+
return this.verifyChecksum(generic_1.appendHexPrefix(token.tokenAddress));
|
|
50
50
|
});
|
|
51
51
|
const provider = this.getProvider();
|
|
52
52
|
const contractAddress = this.getBalanceReaderAddress();
|
|
@@ -95,14 +95,14 @@ class ETHClient {
|
|
|
95
95
|
throw new Error("Invalid recovery address");
|
|
96
96
|
}
|
|
97
97
|
const fromTokenId = fromToken.id;
|
|
98
|
-
const fromTokenAddress =
|
|
98
|
+
const fromTokenAddress = generic_1.appendHexPrefix(fromToken.tokenAddress);
|
|
99
99
|
const toTokenDenom = toToken.denom;
|
|
100
100
|
const recoveryAddressHex = ethers_1.ethers.utils.hexlify(util_1.AddressUtils.SWTHAddress.getAddressBytes(recoveryAddress, CarbonSDK_1.default.Network.MainNet));
|
|
101
101
|
const fromAssetHash = ethers_1.ethers.utils.hexlify(ethers_1.ethers.utils.toUtf8Bytes(fromTokenId));
|
|
102
102
|
const toAssetHash = ethers_1.ethers.utils.hexlify(ethers_1.ethers.utils.toUtf8Bytes(toTokenDenom));
|
|
103
103
|
const nonce = yield rpcProvider.getTransactionCount(fromAddress);
|
|
104
104
|
const contract = new ethers_1.ethers.Contract(this.getBridgeEntranceAddr(), eth_1.ABIs.bridgeEntrance, rpcProvider);
|
|
105
|
-
const feeAddress =
|
|
105
|
+
const feeAddress = generic_1.appendHexPrefix(networkConfig.feeAddress);
|
|
106
106
|
const tokenCreator = fromToken.creator;
|
|
107
107
|
const targetAddressBytes = util_1.AddressUtils.SWTHAddress.getAddressBytes(tokenCreator, CarbonSDK_1.default.Network.MainNet);
|
|
108
108
|
const targetProxyHash = ethers_1.ethers.utils.hexlify(targetAddressBytes);
|
|
@@ -137,9 +137,9 @@ class ETHClient {
|
|
|
137
137
|
throw new Error("Minimum gas required: 150,000");
|
|
138
138
|
}
|
|
139
139
|
const networkConfig = this.getNetworkConfig();
|
|
140
|
-
const assetId =
|
|
141
|
-
const targetProxyHash =
|
|
142
|
-
const feeAddress =
|
|
140
|
+
const assetId = generic_1.appendHexPrefix(token.tokenAddress);
|
|
141
|
+
const targetProxyHash = generic_1.appendHexPrefix(this.getTargetProxyHash(token));
|
|
142
|
+
const feeAddress = generic_1.appendHexPrefix(networkConfig.feeAddress);
|
|
143
143
|
const toAssetHash = ethers_1.ethers.utils.hexlify(ethers_1.ethers.utils.toUtf8Bytes(token.id));
|
|
144
144
|
const swthAddress = ethers_1.ethers.utils.hexlify(address);
|
|
145
145
|
const contractAddress = this.getLockProxyAddress();
|
|
@@ -190,16 +190,16 @@ class ETHClient {
|
|
|
190
190
|
return "insufficient balance";
|
|
191
191
|
}
|
|
192
192
|
const networkConfig = this.getNetworkConfig();
|
|
193
|
-
const assetId =
|
|
194
|
-
const targetProxyHash =
|
|
195
|
-
const feeAddress =
|
|
193
|
+
const assetId = generic_1.appendHexPrefix(tokenWithExternalBalances.tokenAddress);
|
|
194
|
+
const targetProxyHash = generic_1.appendHexPrefix(this.getTargetProxyHash(tokenWithExternalBalances));
|
|
195
|
+
const feeAddress = generic_1.appendHexPrefix(networkConfig.feeAddress);
|
|
196
196
|
const toAssetHash = ethers_1.ethers.utils.hexlify(ethers_1.ethers.utils.toUtf8Bytes(tokenWithExternalBalances.id));
|
|
197
197
|
const nonce = Math.floor(Math.random() * 1000000000); // random nonce to prevent replay attacks
|
|
198
198
|
const message = ethers_1.ethers.utils.solidityKeccak256(["string", "address", "bytes", "bytes", "bytes", "uint256", "uint256", "uint256"], ["sendTokens", assetId, targetProxyHash, toAssetHash, feeAddress, amount, feeAmount, nonce]);
|
|
199
199
|
// logger("sendDeposit message", message)
|
|
200
200
|
let signatureResult;
|
|
201
201
|
const { address, signature } = yield getSignatureCallback(message);
|
|
202
|
-
const signatureBytes = ethers_1.ethers.utils.arrayify(
|
|
202
|
+
const signatureBytes = ethers_1.ethers.utils.arrayify(generic_1.appendHexPrefix(signature));
|
|
203
203
|
const rsv = ethers_1.ethers.utils.splitSignature(signatureBytes);
|
|
204
204
|
// logger("sign result", address, signature)
|
|
205
205
|
signatureResult = {
|
|
@@ -238,7 +238,7 @@ class ETHClient {
|
|
|
238
238
|
if (!feeInfo.deposit_fee) {
|
|
239
239
|
throw new Error("unsupported token");
|
|
240
240
|
}
|
|
241
|
-
if (
|
|
241
|
+
if (blockchain_1.blockchainForChainId(token.chainId.toNumber()) !== this.blockchain) {
|
|
242
242
|
throw new Error("unsupported token");
|
|
243
243
|
}
|
|
244
244
|
let feeAmount = ethers_1.ethers.BigNumber.from(feeInfo.deposit_fee);
|
|
@@ -304,7 +304,7 @@ class ETHClient {
|
|
|
304
304
|
getTargetProxyHash(token) {
|
|
305
305
|
const networkConfig = this.getNetworkConfig();
|
|
306
306
|
const addressBytes = address_1.SWTHAddress.getAddressBytes(token.creator, networkConfig.network);
|
|
307
|
-
const addressHex =
|
|
307
|
+
const addressHex = generic_1.stripHexPrefix(ethers_1.ethers.utils.hexlify(addressBytes));
|
|
308
308
|
return addressHex;
|
|
309
309
|
}
|
|
310
310
|
getProvider() {
|
|
@@ -29,7 +29,7 @@ exports.HydrogenEndpoints = {
|
|
|
29
29
|
const formatDateField = (value) => {
|
|
30
30
|
if (typeof value !== "string")
|
|
31
31
|
return null;
|
|
32
|
-
return
|
|
32
|
+
return dayjs_1.default(value);
|
|
33
33
|
};
|
|
34
34
|
const formatCrossChainTransfer = (value) => {
|
|
35
35
|
var _a, _b;
|
package/lib/clients/NEOClient.js
CHANGED
|
@@ -64,13 +64,13 @@ class NEOClient {
|
|
|
64
64
|
return __awaiter(this, void 0, void 0, function* () {
|
|
65
65
|
const tokenQueryResults = yield sdk.token.getAllTokens();
|
|
66
66
|
const account = new Neon.wallet.Account(address);
|
|
67
|
-
const tokens = tokenQueryResults.filter(token =>
|
|
67
|
+
const tokens = tokenQueryResults.filter(token => blockchain_1.blockchainForChainId(token.chainId.toNumber()) == this.blockchain &&
|
|
68
68
|
token.tokenAddress.length == 40 &&
|
|
69
69
|
token.bridgeAddress.length == 40);
|
|
70
70
|
const client = new Neon.rpc.RPCClient(url, "2.5.2"); // TODO: should we change the RPC version??
|
|
71
71
|
// NOTE: fetching of tokens is chunked in sets of 15 as we may hit
|
|
72
72
|
// the gas limit on the RPC node and error out otherwise
|
|
73
|
-
const promises =
|
|
73
|
+
const promises = lodash_1.chunk(tokens, 75).map((partition) => __awaiter(this, void 0, void 0, function* () {
|
|
74
74
|
var _a, _b, _c;
|
|
75
75
|
let acc = {};
|
|
76
76
|
for (const token of partition) {
|
|
@@ -111,8 +111,8 @@ class NEOClient {
|
|
|
111
111
|
const targetProxyHash = this.getTargetProxyHash(token);
|
|
112
112
|
const toAssetHash = Neon.u.str2hexstring(token.id);
|
|
113
113
|
const addressBytes = address_1.SWTHAddress.getAddressBytes(swthAddress, networkConfig.network);
|
|
114
|
-
const toAddress =
|
|
115
|
-
const zeroAddressHex =
|
|
114
|
+
const toAddress = generic_1.stripHexPrefix(ethers_1.ethers.utils.hexlify(addressBytes));
|
|
115
|
+
const zeroAddressHex = generic_1.stripHexPrefix(ethers_1.ethers.utils.hexlify(constant_1.ZeroAddress));
|
|
116
116
|
const amount = ethers_1.ethers.BigNumber.from(token.externalBalance);
|
|
117
117
|
const feeAmount = ethers_1.ethers.BigNumber.from(feeAmountInput !== null && feeAmountInput !== void 0 ? feeAmountInput : "100000000");
|
|
118
118
|
const feeAddress = feeAmount.isZero() ? zeroAddressHex : networkConfig.feeAddress;
|
|
@@ -159,7 +159,7 @@ class NEOClient {
|
|
|
159
159
|
const fromAddress = util_1.AddressUtils.NEOAddress.publicKeyToAddress(publicKeyOutput.publicKey);
|
|
160
160
|
const targetProxyHash = this.getTargetProxyHash(token);
|
|
161
161
|
const toAssetHash = Neon.u.str2hexstring(token.id);
|
|
162
|
-
const toAddress =
|
|
162
|
+
const toAddress = generic_1.stripHexPrefix(ethers_1.ethers.utils.hexlify(address));
|
|
163
163
|
const nonce = Math.floor(Math.random() * 1000000);
|
|
164
164
|
if (amount.lt(feeAmount)) {
|
|
165
165
|
throw new Error("Invalid amount");
|
|
@@ -195,7 +195,7 @@ class NEOClient {
|
|
|
195
195
|
const fromAddress = ledger.scriptHash;
|
|
196
196
|
const targetProxyHash = this.getTargetProxyHash(token);
|
|
197
197
|
const toAssetHash = Neon.u.str2hexstring(token.id);
|
|
198
|
-
const toAddress =
|
|
198
|
+
const toAddress = generic_1.stripHexPrefix(ethers_1.ethers.utils.hexlify(address));
|
|
199
199
|
const feeAddress = networkConfig.feeAddress;
|
|
200
200
|
const nonce = Math.floor(Math.random() * 1000000);
|
|
201
201
|
if (amount.lt(feeAmount)) {
|
|
@@ -315,7 +315,7 @@ class NEOClient {
|
|
|
315
315
|
getTargetProxyHash(token) {
|
|
316
316
|
const networkConfig = this.getNetworkConfig();
|
|
317
317
|
const addressBytes = address_1.SWTHAddress.getAddressBytes(token.creator, networkConfig.network);
|
|
318
|
-
const addressHex =
|
|
318
|
+
const addressHex = generic_1.stripHexPrefix(ethers_1.ethers.utils.hexlify(addressBytes));
|
|
319
319
|
return addressHex;
|
|
320
320
|
}
|
|
321
321
|
getNetworkConfig() {
|
package/lib/clients/ZILClient.js
CHANGED
|
@@ -74,10 +74,10 @@ class ZILClient {
|
|
|
74
74
|
getExternalBalances(sdk, address, whitelistDenoms) {
|
|
75
75
|
return __awaiter(this, void 0, void 0, function* () {
|
|
76
76
|
const tokenQueryResults = yield sdk.token.getAllTokens();
|
|
77
|
-
const tokens = tokenQueryResults.filter(token =>
|
|
77
|
+
const tokens = tokenQueryResults.filter(token => blockchain_1.blockchainForChainId(token.chainId.toNumber()) == this.blockchain &&
|
|
78
78
|
token.tokenAddress.length == 40 &&
|
|
79
79
|
(!whitelistDenoms || whitelistDenoms.includes(token.denom)));
|
|
80
|
-
const requests = tokens.map(token => token.tokenAddress === zeroAddress ?
|
|
80
|
+
const requests = tokens.map(token => token.tokenAddress === zeroAddress ? exports.balanceBatchRequest(address.replace(/^0x/i, "")) : exports.tokenBalanceBatchRequest(token.tokenAddress, address));
|
|
81
81
|
const response = yield fetch(this.getProviderUrl(), {
|
|
82
82
|
method: "post",
|
|
83
83
|
headers: { "content-type": "application/json" },
|
|
@@ -102,7 +102,7 @@ class ZILClient {
|
|
|
102
102
|
// if (!isValidAddress) {
|
|
103
103
|
// throw new Error("invalid address")
|
|
104
104
|
// }
|
|
105
|
-
return
|
|
105
|
+
return zilliqa_1.fromBech32Address(bech32Address).toLowerCase().substr(2);
|
|
106
106
|
});
|
|
107
107
|
}
|
|
108
108
|
// see examplesV2/zil_client.ts on how to confirm the transactions
|
|
@@ -148,7 +148,7 @@ class ZILClient {
|
|
|
148
148
|
zilliqa = new zilliqa_1.Zilliqa(this.getProviderUrl());
|
|
149
149
|
}
|
|
150
150
|
const deployedContract = (this.walletProvider || zilliqa).contracts.at(contractAddress);
|
|
151
|
-
const balanceAndNonceResp = yield zilliqa.blockchain.getBalance(
|
|
151
|
+
const balanceAndNonceResp = yield zilliqa.blockchain.getBalance(generic_1.stripHexPrefix(zilAddress));
|
|
152
152
|
if (balanceAndNonceResp.error !== undefined) {
|
|
153
153
|
throw new Error(balanceAndNonceResp.error.message);
|
|
154
154
|
}
|
|
@@ -166,7 +166,7 @@ class ZILClient {
|
|
|
166
166
|
vname: 'spender',
|
|
167
167
|
type: 'ByStr20',
|
|
168
168
|
// TODO: Check if bridgeAddress corresponds to carbon token lock_proxy_hash
|
|
169
|
-
value: spenderAddress !== null && spenderAddress !== void 0 ? spenderAddress :
|
|
169
|
+
value: spenderAddress !== null && spenderAddress !== void 0 ? spenderAddress : generic_1.appendHexPrefix(token.bridgeAddress),
|
|
170
170
|
},
|
|
171
171
|
{
|
|
172
172
|
vname: 'amount',
|
|
@@ -184,7 +184,7 @@ class ZILClient {
|
|
|
184
184
|
}
|
|
185
185
|
checkAllowanceZRC2(token, owner, spender) {
|
|
186
186
|
return __awaiter(this, void 0, void 0, function* () {
|
|
187
|
-
const contractAddress =
|
|
187
|
+
const contractAddress = generic_1.appendHexPrefix(token.tokenAddress);
|
|
188
188
|
const zilliqa = new zilliqa_1.Zilliqa(this.getProviderUrl());
|
|
189
189
|
const resp = yield zilliqa.blockchain.getSmartContractSubState(contractAddress, "allowances", [owner, spender]);
|
|
190
190
|
if (resp.error !== undefined) {
|
|
@@ -204,13 +204,13 @@ class ZILClient {
|
|
|
204
204
|
throw new Error("Invalid recovery address");
|
|
205
205
|
}
|
|
206
206
|
const fromTokenId = fromToken.id;
|
|
207
|
-
const fromTokenAddr =
|
|
207
|
+
const fromTokenAddr = generic_1.appendHexPrefix(fromToken.tokenAddress);
|
|
208
208
|
const toTokenDenom = toToken.denom;
|
|
209
|
-
const targetProxyHash =
|
|
209
|
+
const targetProxyHash = generic_1.appendHexPrefix(this.getTargetProxyHash(fromToken));
|
|
210
210
|
const recoveryAddressHex = ethers_1.ethers.utils.hexlify(index_1.AddressUtils.SWTHAddress.getAddressBytes(recoveryAddress, CarbonSDK_1.default.Network.MainNet));
|
|
211
211
|
const fromAssetHash = ethers_1.ethers.utils.hexlify(ethers_1.ethers.utils.toUtf8Bytes(fromTokenId));
|
|
212
212
|
const toAssetHash = ethers_1.ethers.utils.hexlify(ethers_1.ethers.utils.toUtf8Bytes(toTokenDenom));
|
|
213
|
-
const feeAddress =
|
|
213
|
+
const feeAddress = generic_1.appendHexPrefix(networkConfig.feeAddress);
|
|
214
214
|
const contractAddress = this.getBridgeEntranceAddr();
|
|
215
215
|
let zilliqa;
|
|
216
216
|
if (typeof signer === 'string') {
|
|
@@ -225,7 +225,7 @@ class ZILClient {
|
|
|
225
225
|
zilliqa = new zilliqa_1.Zilliqa(this.getProviderUrl());
|
|
226
226
|
}
|
|
227
227
|
const deployedContract = (this.walletProvider || zilliqa).contracts.at(contractAddress);
|
|
228
|
-
const balanceAndNonceResp = yield zilliqa.blockchain.getBalance(
|
|
228
|
+
const balanceAndNonceResp = yield zilliqa.blockchain.getBalance(generic_1.stripHexPrefix(fromAddress));
|
|
229
229
|
if (balanceAndNonceResp.error !== undefined) {
|
|
230
230
|
throw new Error(balanceAndNonceResp.error.message);
|
|
231
231
|
}
|
|
@@ -302,13 +302,13 @@ class ZILClient {
|
|
|
302
302
|
return __awaiter(this, void 0, void 0, function* () {
|
|
303
303
|
const { address, amount, token, gasPrice, gasLimit, zilAddress, signer } = params;
|
|
304
304
|
const networkConfig = this.getNetworkConfig();
|
|
305
|
-
const assetId =
|
|
306
|
-
const targetProxyHash =
|
|
307
|
-
const feeAddress =
|
|
305
|
+
const assetId = generic_1.appendHexPrefix(token.tokenAddress);
|
|
306
|
+
const targetProxyHash = generic_1.appendHexPrefix(this.getTargetProxyHash(token));
|
|
307
|
+
const feeAddress = generic_1.appendHexPrefix(networkConfig.feeAddress);
|
|
308
308
|
const toAssetHash = ethers_1.ethers.utils.hexlify(ethers_1.ethers.utils.toUtf8Bytes(token.id));
|
|
309
309
|
const swthAddress = ethers_1.ethers.utils.hexlify(address);
|
|
310
310
|
// TODO: Check if bridgeAddress corresponds to carbon token lock_proxy_hash
|
|
311
|
-
const contractAddress =
|
|
311
|
+
const contractAddress = generic_1.appendHexPrefix(token.bridgeAddress);
|
|
312
312
|
let zilliqa;
|
|
313
313
|
if (typeof signer === 'string') {
|
|
314
314
|
zilliqa = new zilliqa_1.Zilliqa(this.getProviderUrl());
|
|
@@ -322,7 +322,7 @@ class ZILClient {
|
|
|
322
322
|
zilliqa = new zilliqa_1.Zilliqa(this.getProviderUrl());
|
|
323
323
|
}
|
|
324
324
|
const deployedContract = (this.walletProvider || zilliqa).contracts.at(contractAddress);
|
|
325
|
-
const balanceAndNonceResp = yield zilliqa.blockchain.getBalance(
|
|
325
|
+
const balanceAndNonceResp = yield zilliqa.blockchain.getBalance(generic_1.stripHexPrefix(zilAddress));
|
|
326
326
|
if (balanceAndNonceResp.error !== undefined) {
|
|
327
327
|
throw new Error(balanceAndNonceResp.error.message);
|
|
328
328
|
}
|
|
@@ -407,7 +407,7 @@ class ZILClient {
|
|
|
407
407
|
getTargetProxyHash(token) {
|
|
408
408
|
const networkConfig = this.getNetworkConfig();
|
|
409
409
|
const addressBytes = address_1.SWTHAddress.getAddressBytes(token.creator, networkConfig.network);
|
|
410
|
-
const addressHex =
|
|
410
|
+
const addressHex = generic_1.stripHexPrefix(ethers_1.ethers.utils.hexlify(addressBytes));
|
|
411
411
|
return addressHex;
|
|
412
412
|
}
|
|
413
413
|
getNetworkConfig() {
|
|
@@ -159,7 +159,7 @@ exports.QueryProposalsRequest = {
|
|
|
159
159
|
const message = Object.assign({}, baseQueryProposalsRequest);
|
|
160
160
|
message.proposalStatus =
|
|
161
161
|
object.proposalStatus !== undefined && object.proposalStatus !== null
|
|
162
|
-
?
|
|
162
|
+
? gov_1.proposalStatusFromJSON(object.proposalStatus)
|
|
163
163
|
: 0;
|
|
164
164
|
message.voter =
|
|
165
165
|
object.voter !== undefined && object.voter !== null
|
|
@@ -178,7 +178,7 @@ exports.QueryProposalsRequest = {
|
|
|
178
178
|
toJSON(message) {
|
|
179
179
|
const obj = {};
|
|
180
180
|
message.proposalStatus !== undefined &&
|
|
181
|
-
(obj.proposalStatus =
|
|
181
|
+
(obj.proposalStatus = gov_1.proposalStatusToJSON(message.proposalStatus));
|
|
182
182
|
message.voter !== undefined && (obj.voter = message.voter);
|
|
183
183
|
message.depositor !== undefined && (obj.depositor = message.depositor);
|
|
184
184
|
message.pagination !== undefined &&
|
|
@@ -185,7 +185,7 @@ exports.MsgVote = {
|
|
|
185
185
|
: "";
|
|
186
186
|
message.option =
|
|
187
187
|
object.option !== undefined && object.option !== null
|
|
188
|
-
?
|
|
188
|
+
? gov_1.voteOptionFromJSON(object.option)
|
|
189
189
|
: 0;
|
|
190
190
|
return message;
|
|
191
191
|
},
|
|
@@ -195,7 +195,7 @@ exports.MsgVote = {
|
|
|
195
195
|
(obj.proposalId = (message.proposalId || long_1.default.UZERO).toString());
|
|
196
196
|
message.voter !== undefined && (obj.voter = message.voter);
|
|
197
197
|
message.option !== undefined &&
|
|
198
|
-
(obj.option =
|
|
198
|
+
(obj.option = gov_1.voteOptionToJSON(message.option));
|
|
199
199
|
return obj;
|
|
200
200
|
},
|
|
201
201
|
fromPartial(object) {
|
|
@@ -618,13 +618,13 @@ exports.ModeInfo_Single = {
|
|
|
618
618
|
const message = Object.assign({}, baseModeInfo_Single);
|
|
619
619
|
message.mode =
|
|
620
620
|
object.mode !== undefined && object.mode !== null
|
|
621
|
-
?
|
|
621
|
+
? signing_1.signModeFromJSON(object.mode)
|
|
622
622
|
: 0;
|
|
623
623
|
return message;
|
|
624
624
|
},
|
|
625
625
|
toJSON(message) {
|
|
626
626
|
const obj = {};
|
|
627
|
-
message.mode !== undefined && (obj.mode =
|
|
627
|
+
message.mode !== undefined && (obj.mode = signing_1.signModeToJSON(message.mode));
|
|
628
628
|
return obj;
|
|
629
629
|
},
|
|
630
630
|
fromPartial(object) {
|
package/lib/constant/generic.js
CHANGED
|
@@ -12,7 +12,7 @@ exports.DEFAULT_FEE_DENOM = "swth";
|
|
|
12
12
|
exports.DEFAULT_GAS_PRICE = new bignumber_js_1.default(10);
|
|
13
13
|
exports.DEFAULT_GAS = new bignumber_js_1.default(10000000);
|
|
14
14
|
exports.DEFAULT_FEE = {
|
|
15
|
-
amount:
|
|
15
|
+
amount: proto_signing_1.coins(exports.DEFAULT_GAS_PRICE.times(exports.DEFAULT_GAS).dp(0).toString(), "swth"),
|
|
16
16
|
gas: exports.DEFAULT_GAS.toString(10),
|
|
17
17
|
};
|
|
18
18
|
exports.CURRENT_GAS_PRICE = 769.23077;
|
package/lib/modules/cdp.js
CHANGED
|
@@ -429,7 +429,7 @@ class CDPModule extends base_1.default {
|
|
|
429
429
|
let availableBorrowsUsd = number_1.BN_ZERO;
|
|
430
430
|
let currLiquidationThreshold = number_1.BN_ZERO;
|
|
431
431
|
for (let i = 0; i < collaterals.length; i++) {
|
|
432
|
-
const amount =
|
|
432
|
+
const amount = number_1.bnOrZero(collaterals[i].collateralAmount);
|
|
433
433
|
if (amount.isZero()) {
|
|
434
434
|
continue; // no collateral for denom
|
|
435
435
|
}
|
|
@@ -446,9 +446,9 @@ class CDPModule extends base_1.default {
|
|
|
446
446
|
if (!assetParam) {
|
|
447
447
|
continue;
|
|
448
448
|
}
|
|
449
|
-
const ltv =
|
|
449
|
+
const ltv = number_1.bnOrZero(assetParam.loanToValue).div(number_1.BN_10000);
|
|
450
450
|
const availableBorrowUsd = collateralUsdVal.times(ltv);
|
|
451
|
-
const liquidationThreshold =
|
|
451
|
+
const liquidationThreshold = number_1.bnOrZero(assetParam.liquidationThreshold).div(number_1.BN_10000);
|
|
452
452
|
const liquidationThresholdVal = collateralUsdVal.times(liquidationThreshold);
|
|
453
453
|
totalCollateralsUsd = totalCollateralsUsd.plus(collateralUsdVal);
|
|
454
454
|
availableBorrowsUsd = availableBorrowsUsd.plus(availableBorrowUsd);
|
|
@@ -459,7 +459,7 @@ class CDPModule extends base_1.default {
|
|
|
459
459
|
const debts = debtsRsp.debts;
|
|
460
460
|
let totalDebtsUsd = number_1.BN_ZERO;
|
|
461
461
|
for (let i = 0; i < debts.length; i++) {
|
|
462
|
-
const amount =
|
|
462
|
+
const amount = number_1.bnOrZero(debts[i].principalDebt);
|
|
463
463
|
const denom = debts[i].denom;
|
|
464
464
|
if (amount.isZero()) {
|
|
465
465
|
continue;
|
|
@@ -481,7 +481,7 @@ class CDPModule extends base_1.default {
|
|
|
481
481
|
if (stablecoinDebtInfo) {
|
|
482
482
|
const accountStablecoin = yield sdk.query.cdp.AccountStablecoin({ address: account });
|
|
483
483
|
const stablecoinDecimals = (_a = yield this.sdkProvider.getTokenClient().getDecimals(stablecoinDebtInfo.denom)) !== null && _a !== void 0 ? _a : number_1.BN_ZERO;
|
|
484
|
-
const stablecoinDebtAmount =
|
|
484
|
+
const stablecoinDebtAmount = number_1.bnOrZero(accountStablecoin.principalDebt).plus(number_1.bnOrZero(accountStablecoin.interestDebt));
|
|
485
485
|
stablecoinDebtUsd = stablecoinDebtAmount.shiftedBy(-stablecoinDecimals);
|
|
486
486
|
totalDebtsUsd = totalDebtsUsd.plus(stablecoinDebtUsd);
|
|
487
487
|
}
|
|
@@ -502,7 +502,7 @@ class CDPModule extends base_1.default {
|
|
|
502
502
|
const sdk = this.sdkProvider;
|
|
503
503
|
const cdpAddress = this.getCdpModuleAddress();
|
|
504
504
|
const balanceRsp = yield sdk.query.bank.Balance(query_2.QueryBalanceRequest.fromPartial({ address: cdpAddress, denom }));
|
|
505
|
-
return
|
|
505
|
+
return number_1.bnOrZero((_a = balanceRsp.balance) === null || _a === void 0 ? void 0 : _a.amount);
|
|
506
506
|
});
|
|
507
507
|
}
|
|
508
508
|
getCdpToActualRatio(cdpDenom) {
|
|
@@ -513,13 +513,13 @@ class CDPModule extends base_1.default {
|
|
|
513
513
|
const cdpAmountRsp = supplyRsp.amount;
|
|
514
514
|
if (!cdpAmountRsp)
|
|
515
515
|
throw new Error("unable to retrieve cdp token supply");
|
|
516
|
-
const cdpAmount =
|
|
516
|
+
const cdpAmount = number_1.bnOrZero(cdpAmountRsp.amount);
|
|
517
517
|
const cdpAddress = this.getCdpModuleAddress();
|
|
518
518
|
const balanceRsp = yield sdk.query.bank.Balance(query_2.QueryBalanceRequest.fromPartial({ address: cdpAddress, denom }));
|
|
519
519
|
if (!balanceRsp.balance)
|
|
520
520
|
throw new Error("unable to retrieve cdp module balance");
|
|
521
521
|
const owedAmount = yield this.getTotalTokenDebt(denom);
|
|
522
|
-
const actualAmount =
|
|
522
|
+
const actualAmount = number_1.bnOrZero(balanceRsp.balance.amount).plus(owedAmount);
|
|
523
523
|
if (!owedAmount)
|
|
524
524
|
throw new Error("unable to retrieve total token debt");
|
|
525
525
|
return cdpAmount.div(actualAmount);
|
|
@@ -541,8 +541,8 @@ class CDPModule extends base_1.default {
|
|
|
541
541
|
const allDebts = allDebtsRes.debtInfosAll;
|
|
542
542
|
for (let i = 0; i < allDebts.length; i++) {
|
|
543
543
|
const denom = allDebts[i].denom;
|
|
544
|
-
const interest =
|
|
545
|
-
const principal =
|
|
544
|
+
const interest = number_1.bnOrZero(allDebts[i].totalAccumulatedInterest);
|
|
545
|
+
const principal = number_1.bnOrZero(allDebts[i].totalPrincipal);
|
|
546
546
|
const debtAmt = interest.plus(principal);
|
|
547
547
|
const debtUsdVal = yield this.getTokenUsdVal(denom, debtAmt);
|
|
548
548
|
if (!debtUsdVal) {
|
|
@@ -554,7 +554,7 @@ class CDPModule extends base_1.default {
|
|
|
554
554
|
const stablecoinDebtRes = yield this.sdkProvider.query.cdp.StablecoinDebt({});
|
|
555
555
|
if (stablecoinDebtRes.stablecoinDebtInfo) {
|
|
556
556
|
const debtInfo = stablecoinDebtRes.stablecoinDebtInfo;
|
|
557
|
-
const debtAmt =
|
|
557
|
+
const debtAmt = number_1.bnOrZero(debtInfo.totalPrincipal).plus(number_1.bnOrZero(debtInfo.totalAccumulatedInterest));
|
|
558
558
|
const stablecoinDecimals = (_a = yield sdk.getTokenClient().getDecimals(debtInfo.denom)) !== null && _a !== void 0 ? _a : 0;
|
|
559
559
|
const debtUsdVal = (debtAmt).shiftedBy(-stablecoinDecimals);
|
|
560
560
|
totalDebt = totalDebt.plus(debtUsdVal);
|
|
@@ -572,7 +572,7 @@ class CDPModule extends base_1.default {
|
|
|
572
572
|
if (!TokenClient_1.default.isCdpToken(balance.denom)) {
|
|
573
573
|
continue;
|
|
574
574
|
}
|
|
575
|
-
const amount =
|
|
575
|
+
const amount = number_1.bnOrZero(balance.amount);
|
|
576
576
|
const collateralUsdValue = yield this.getCdpTokenUsdVal(balance.denom, amount);
|
|
577
577
|
allCollateralsUsdValue = allCollateralsUsdValue.plus(collateralUsdValue);
|
|
578
578
|
}
|
|
@@ -596,7 +596,7 @@ class CDPModule extends base_1.default {
|
|
|
596
596
|
const priceResult = yield sdk.query.pricing.TokenPrice(codec_1.QueryTokenPriceRequest.fromPartial({ denom }));
|
|
597
597
|
if (!priceResult.tokenPrice)
|
|
598
598
|
throw new Error("unable to retrieve token price for " + denom);
|
|
599
|
-
const twap =
|
|
599
|
+
const twap = number_1.bnOrZero(priceResult.tokenPrice.twap).shiftedBy(-18);
|
|
600
600
|
return amount.multipliedBy(twap).shiftedBy(-decimals);
|
|
601
601
|
});
|
|
602
602
|
}
|
|
@@ -611,11 +611,11 @@ class CDPModule extends base_1.default {
|
|
|
611
611
|
throw new Error("unable to retrieve debt info");
|
|
612
612
|
const cimRsp = yield this.recalculateCIM(denom, debtInfo);
|
|
613
613
|
const newInterestRate = cimRsp.interest;
|
|
614
|
-
const principal =
|
|
615
|
-
const accumInterest =
|
|
614
|
+
const principal = number_1.bnOrZero(debtInfo.totalPrincipal);
|
|
615
|
+
const accumInterest = number_1.bnOrZero(debtInfo.totalAccumulatedInterest);
|
|
616
616
|
const newInterest = principal.times(newInterestRate).plus(accumInterest.times(number_1.BN_ONE.plus(newInterestRate)));
|
|
617
617
|
const cdpParamsRsp = yield this.sdkProvider.query.cdp.Params(query_1.QueryParamsRequest.fromPartial({}));
|
|
618
|
-
const interestFee =
|
|
618
|
+
const interestFee = number_1.bnOrZero((_a = cdpParamsRsp.params) === null || _a === void 0 ? void 0 : _a.interestFee);
|
|
619
619
|
const interest = newInterest.times(number_1.BN_10000.minus(interestFee)).dividedToIntegerBy(number_1.BN_10000);
|
|
620
620
|
return principal.plus(interest);
|
|
621
621
|
});
|
|
@@ -633,8 +633,8 @@ class CDPModule extends base_1.default {
|
|
|
633
633
|
const debtRes = yield sdk.query.cdp.AccountDebt({ address: account, denom: denom });
|
|
634
634
|
debt = debtRes.debt;
|
|
635
635
|
}
|
|
636
|
-
const principalAmount =
|
|
637
|
-
const initialCIM =
|
|
636
|
+
const principalAmount = number_1.bnOrZero(debt === null || debt === void 0 ? void 0 : debt.principalDebt);
|
|
637
|
+
const initialCIM = number_1.bnOrZero(debt === null || debt === void 0 ? void 0 : debt.initialCumulativeInterestMultiplier);
|
|
638
638
|
if (principalAmount.isZero() || initialCIM.isZero())
|
|
639
639
|
return number_1.BN_ZERO;
|
|
640
640
|
const cimRsp = yield this.recalculateCIM(denom, debtInfo);
|
|
@@ -660,8 +660,8 @@ class CDPModule extends base_1.default {
|
|
|
660
660
|
const debtResp = yield sdk.query.cdp.AccountStablecoin(query_1.QueryAccountStablecoinRequest.fromPartial({ address: account }));
|
|
661
661
|
debt = debtResp;
|
|
662
662
|
}
|
|
663
|
-
principalAmount =
|
|
664
|
-
const initialCIM =
|
|
663
|
+
principalAmount = number_1.bnOrZero(debt.principalDebt);
|
|
664
|
+
const initialCIM = number_1.bnOrZero(debt.initialCumulativeInterestMultiplier);
|
|
665
665
|
const cim = yield this.recalculateStablecoinCIM(debtInfo);
|
|
666
666
|
if (!cim)
|
|
667
667
|
throw new Error("unable to retrieve account debt");
|
|
@@ -702,7 +702,7 @@ class CDPModule extends base_1.default {
|
|
|
702
702
|
return number_1.BN_ZERO;
|
|
703
703
|
}
|
|
704
704
|
const diffSeconds = new bignumber_js_1.BigNumber(diffMs).shiftedBy(-3).dp(0, bignumber_js_1.BigNumber.ROUND_CEIL);
|
|
705
|
-
const secondsAYear =
|
|
705
|
+
const secondsAYear = number_1.bnOrZero(31536000);
|
|
706
706
|
const numPeriods = secondsAYear.div(diffSeconds).dp(18);
|
|
707
707
|
return apy.div(numPeriods).dp(18); // carbon backend sdk.dec max 18 dp
|
|
708
708
|
}
|
|
@@ -726,8 +726,8 @@ class CDPModule extends base_1.default {
|
|
|
726
726
|
throw new Error("unable to retrieve cdp params for " + denom);
|
|
727
727
|
}
|
|
728
728
|
}
|
|
729
|
-
const interestFeeRate =
|
|
730
|
-
const utilizationRate =
|
|
729
|
+
const interestFeeRate = number_1.bnOrZero(params.interestFee).div(number_1.BN_10000);
|
|
730
|
+
const utilizationRate = number_1.bnOrZero(debtInfo.utilizationRate).shiftedBy(-18);
|
|
731
731
|
return borrowInterest.times(utilizationRate).times(number_1.BN_ONE.minus(interestFeeRate));
|
|
732
732
|
});
|
|
733
733
|
}
|
|
@@ -742,7 +742,7 @@ class CDPModule extends base_1.default {
|
|
|
742
742
|
return { cim: number_1.BN_ZERO, interest: number_1.BN_ZERO };
|
|
743
743
|
}
|
|
744
744
|
}
|
|
745
|
-
const cim =
|
|
745
|
+
const cim = number_1.bnOrZero(debtInfo.cumulativeInterestMultiplier);
|
|
746
746
|
const apy = yield this.calculateAPY(denom, debtInfo);
|
|
747
747
|
const newInterest = CDPModule.calculateInterestForTimePeriod(apy, (_a = debtInfo.lastUpdatedTime) !== null && _a !== void 0 ? _a : new Date(0), new Date());
|
|
748
748
|
const newCIM = cim.times(newInterest.plus(1));
|
|
@@ -761,8 +761,8 @@ class CDPModule extends base_1.default {
|
|
|
761
761
|
}
|
|
762
762
|
}
|
|
763
763
|
const paramsResponse = yield sdk.query.cdp.Params(codec_1.QueryCdpParamsRequest.fromPartial({}));
|
|
764
|
-
const cim =
|
|
765
|
-
const apy =
|
|
764
|
+
const cim = number_1.bnOrZero(debtInfo.cumulativeInterestMultiplier);
|
|
765
|
+
const apy = number_1.bnOrZero((_a = paramsResponse.params) === null || _a === void 0 ? void 0 : _a.stablecoinInterestRate);
|
|
766
766
|
if (!apy) {
|
|
767
767
|
return number_1.BN_ZERO;
|
|
768
768
|
}
|
|
@@ -788,7 +788,7 @@ class CDPModule extends base_1.default {
|
|
|
788
788
|
const availableBorrowsUsd = accountData.AvailableBorrowsUsd.minus(accountData.TotalDebtsUsd);
|
|
789
789
|
const unlockableUsd = availableBorrowsUsd.multipliedBy(number_1.BN_10000).div(unlockRatio);
|
|
790
790
|
const tokenPrice = yield sdk.query.pricing.TokenPrice({ denom });
|
|
791
|
-
const tokenTwap =
|
|
791
|
+
const tokenTwap = number_1.bnOrZero((_b = tokenPrice.tokenPrice) === null || _b === void 0 ? void 0 : _b.twap);
|
|
792
792
|
if (tokenTwap.isZero())
|
|
793
793
|
throw new Error("unable to retrieve token price for " + denom);
|
|
794
794
|
const tokenAmt = unlockableUsd.div(tokenTwap.shiftedBy(-18)).shiftedBy(tokenDecimals);
|
|
@@ -799,7 +799,7 @@ class CDPModule extends base_1.default {
|
|
|
799
799
|
address: account,
|
|
800
800
|
cdpDenom: cdpDenom
|
|
801
801
|
});
|
|
802
|
-
const lockedAmount =
|
|
802
|
+
const lockedAmount = number_1.bnOrZero((_e = (_d = accountCollateral.collateral) === null || _d === void 0 ? void 0 : _d.collateralAmount) !== null && _e !== void 0 ? _e : "0");
|
|
803
803
|
return lockedAmount.lt(cdpTokenAmt) ? lockedAmount : cdpTokenAmt;
|
|
804
804
|
});
|
|
805
805
|
}
|
|
@@ -817,7 +817,7 @@ class CDPModule extends base_1.default {
|
|
|
817
817
|
const denom = this.getUnderlyingDenom(cdpDenom);
|
|
818
818
|
const cdpToActualRatio = (_a = yield this.getCdpToActualRatio(cdpDenom)) !== null && _a !== void 0 ? _a : number_1.BN_ZERO;
|
|
819
819
|
const tokenPrice = yield sdk.query.pricing.TokenPrice({ denom: denom });
|
|
820
|
-
const tokenTwap =
|
|
820
|
+
const tokenTwap = number_1.bnOrZero((_b = tokenPrice.tokenPrice) === null || _b === void 0 ? void 0 : _b.twap).shiftedBy(-18);
|
|
821
821
|
return tokenTwap.multipliedBy(cdpToActualRatio);
|
|
822
822
|
});
|
|
823
823
|
}
|
|
@@ -838,26 +838,26 @@ class CDPModule extends base_1.default {
|
|
|
838
838
|
});
|
|
839
839
|
if (!asset.assetParams)
|
|
840
840
|
throw new Error("unable to retrieve asset param for " + cdpActualDenom);
|
|
841
|
-
const bonus =
|
|
841
|
+
const bonus = number_1.bnOrZero(asset.assetParams.liquidationDiscount).div(number_1.BN_10000);
|
|
842
842
|
const cdpTokenPrice = yield this.getCdpTokenPrice(cdpDenom);
|
|
843
843
|
const cdpTokenDiscountedPrice = cdpTokenPrice.multipliedBy(number_1.BN_ONE.minus(bonus));
|
|
844
844
|
// get close factor
|
|
845
845
|
const debtorAccountData = yield sdk.query.cdp.AccountData({
|
|
846
846
|
address: debtor
|
|
847
847
|
});
|
|
848
|
-
const debtorTotalCollateralVal =
|
|
849
|
-
const debtorTotalDebtVal =
|
|
850
|
-
const currentLiqThreshold =
|
|
848
|
+
const debtorTotalCollateralVal = number_1.bnOrZero(debtorAccountData.totalCollateralsUsd);
|
|
849
|
+
const debtorTotalDebtVal = number_1.bnOrZero(debtorAccountData.totalDebtsUsd);
|
|
850
|
+
const currentLiqThreshold = number_1.bnOrZero(debtorAccountData.currLiquidationThreshold);
|
|
851
851
|
const params = yield sdk.query.cdp.Params({});
|
|
852
852
|
if (!params.params) {
|
|
853
853
|
throw new Error("unable to retrieve cdp params");
|
|
854
854
|
}
|
|
855
|
-
const smallLiqSize =
|
|
856
|
-
const minCloseFactor =
|
|
857
|
-
const completeLiqThreshold =
|
|
855
|
+
const smallLiqSize = number_1.bnOrZero(params.params.smallLiquidationSize);
|
|
856
|
+
const minCloseFactor = number_1.bnOrZero(params.params.minimumCloseFactor);
|
|
857
|
+
const completeLiqThreshold = number_1.bnOrZero(params.params.completeLiquidationThreshold);
|
|
858
858
|
const closeFactor = this.computeCloseFactor(debtorTotalDebtVal, debtorTotalCollateralVal, currentLiqThreshold, smallLiqSize, minCloseFactor, completeLiqThreshold);
|
|
859
859
|
// get max repayable amount given the debtor's debt and how much liquidator wants to repay
|
|
860
|
-
const debtDecimals =
|
|
860
|
+
const debtDecimals = number_1.bnOrZero(yield sdk.getTokenClient().getDecimals(debtDenom));
|
|
861
861
|
const maxRepayableValue = debtorTotalDebtVal.multipliedBy(closeFactor);
|
|
862
862
|
const maxRepayableAmt = maxRepayableValue.shiftedBy(debtDecimals.toNumber());
|
|
863
863
|
if (debtRepaymentAmount.isGreaterThan(maxRepayableAmt)) {
|
|
@@ -865,7 +865,7 @@ class CDPModule extends base_1.default {
|
|
|
865
865
|
}
|
|
866
866
|
// calculate collateral amount that can be obtained given that debt amount and debtor's collateral balance
|
|
867
867
|
// AND, recalculate debt repay amount if needed
|
|
868
|
-
const cdpTokenDecimals =
|
|
868
|
+
const cdpTokenDecimals = number_1.bnOrZero(yield sdk.getTokenClient().getDecimals(cdpActualDenom));
|
|
869
869
|
let collateralAmtToLiquidate = this.calculateCollateralRequiredForDebt(number_1.BN_ONE, // assumes USC is $1
|
|
870
870
|
cdpTokenDiscountedPrice, debtRepaymentAmount, cdpTokenDecimals, debtDecimals);
|
|
871
871
|
const debtorAccountCollateral = yield sdk.query.cdp.AccountCollateral({
|
|
@@ -938,11 +938,11 @@ class CDPModule extends base_1.default {
|
|
|
938
938
|
}
|
|
939
939
|
exports.CDPModule = CDPModule;
|
|
940
940
|
CDPModule.calculateInterestAPY = (debtInfo, rateStrategy) => {
|
|
941
|
-
const utilizationRate =
|
|
942
|
-
const optimalUsage =
|
|
943
|
-
const variableRate1 =
|
|
944
|
-
const variableRate2 =
|
|
945
|
-
const baseVariableBorrowRate =
|
|
941
|
+
const utilizationRate = number_1.bnOrZero(debtInfo.utilizationRate).shiftedBy(-18);
|
|
942
|
+
const optimalUsage = number_1.bnOrZero(rateStrategy.optimalUsage).shiftedBy(-4);
|
|
943
|
+
const variableRate1 = number_1.bnOrZero(rateStrategy.variableRateSlope1).shiftedBy(-4);
|
|
944
|
+
const variableRate2 = number_1.bnOrZero(rateStrategy.variableRateSlope2).shiftedBy(-4);
|
|
945
|
+
const baseVariableBorrowRate = number_1.bnOrZero(rateStrategy.baseVariableBorrowRate).shiftedBy(-4);
|
|
946
946
|
if (utilizationRate.lte(optimalUsage)) {
|
|
947
947
|
const vRate = utilizationRate.times(variableRate1).div(optimalUsage).dp(4, bignumber_js_1.BigNumber.ROUND_CEIL);
|
|
948
948
|
return vRate.plus(baseVariableBorrowRate);
|