@xchainjs/xchain-thorchain 0.21.1 → 0.22.2

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/CHANGELOG.md CHANGED
@@ -1,3 +1,37 @@
1
+ # v0.22.2 (2022-02-17)
2
+
3
+ ## Fix
4
+
5
+ - Request fees from THORChain and use `defaultFees` in case of server errors only
6
+ - Fix `defaultFees` to be 0.02 RUNE
7
+
8
+ # v0.22.1 (2022-02-16)
9
+
10
+ ## Fix
11
+
12
+ - Increase limit for `DEFAULT_GAS_VALUE` from 2000000 to 3000000 to accommodate recent increases in gas used that go above the old limit
13
+
14
+ # v0.22.0 (2022-02-06)
15
+
16
+ ## Add
17
+
18
+ - Option to pass `ChainIds` into constructor
19
+ - getter / setter for `chainId` in `Client`
20
+
21
+ ## Breaking change
22
+
23
+ - `buildDepositTx` needs `chainId` to be passed - all params are set as object
24
+ - Remove `enum ChainId` + `getChainId` + `isChainId` from `utils`
25
+
26
+ # v0.21.2 (2022-02-04)
27
+
28
+ ## Fix
29
+
30
+ - Use latest axios@0.25.0
31
+ - xchain-client@0.11.1
32
+ - @xchainjs/xchain-util@0.5.1
33
+ - @xchainjs/xchain-cosmos@0.16.1
34
+
1
35
  # v.0.21.1 (2022-02-04)
2
36
 
3
37
  ## Fix
package/lib/client.d.ts CHANGED
@@ -3,7 +3,7 @@ import { CosmosSDKClient, RPCTxResult } from '@xchainjs/xchain-cosmos';
3
3
  import { Asset } from '@xchainjs/xchain-util';
4
4
  import { PrivKey, PubKey } from 'cosmos-client';
5
5
  import { StdTx } from 'cosmos-client/x/auth';
6
- import { ClientUrl, DepositParam, ExplorerUrls, NodeUrl, ThorchainClientParams, TxOfflineParams } from './types';
6
+ import { ChainId, ClientUrl, DepositParam, ExplorerUrls, NodeUrl, ThorchainClientParams, TxOfflineParams } from './types';
7
7
  /**
8
8
  * Interface for custom Thorchain client
9
9
  */
@@ -21,6 +21,7 @@ declare class Client implements ThorchainClient, XChainClient {
21
21
  private network;
22
22
  private clientUrl;
23
23
  private explorerUrls;
24
+ private chainIds;
24
25
  private phrase;
25
26
  private rootDerivationPaths;
26
27
  private cosmosClient;
@@ -34,7 +35,7 @@ declare class Client implements ThorchainClient, XChainClient {
34
35
  *
35
36
  * @throws {"Invalid phrase"} Thrown if the given phase is invalid.
36
37
  */
37
- constructor({ network, phrase, clientUrl, explorerUrls, rootDerivationPaths, }: XChainClientParams & ThorchainClientParams);
38
+ constructor({ network, phrase, clientUrl, explorerUrls, rootDerivationPaths, chainIds, }: XChainClientParams & ThorchainClientParams);
38
39
  /**
39
40
  * Purge client.
40
41
  *
@@ -83,6 +84,23 @@ declare class Client implements ThorchainClient, XChainClient {
83
84
  * @returns {string} The explorer url for thorchain based on the current network.
84
85
  */
85
86
  getExplorerUrl(): string;
87
+ /**
88
+ * Sets chain id
89
+ *
90
+ * @param {ChainId} chainId Chain id to update
91
+ * @param {Network} network (optional) Network for given chainId. If `network`not set, current network of the client is used
92
+ *
93
+ * @returns {void}
94
+ */
95
+ setChainId(chainId: ChainId, network?: Network): void;
96
+ /**
97
+ * Gets chain id
98
+ *
99
+ * @param {Network} network (optional) Network to get chain id from. If `network`not set, current network of the client is used
100
+ *
101
+ * @returns {ChainId} Chain id based on the current network.
102
+ */
103
+ getChainId(network?: Network): ChainId;
86
104
  /**
87
105
  * Get cosmos client
88
106
  * @returns {CosmosSDKClient} current cosmos client
@@ -214,7 +232,7 @@ declare class Client implements ThorchainClient, XChainClient {
214
232
  */
215
233
  transferOffline({ walletIndex, asset, amount, recipient, memo, from_rune_balance, from_asset_balance, from_account_number, from_sequence, }: TxOfflineParams): Promise<StdTx>;
216
234
  /**
217
- * Get the fees.
235
+ * Gets fees from Node
218
236
  *
219
237
  * @returns {Fees}
220
238
  */
package/lib/index.esm.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { Network, TxType, singleFee, FeeType } from '@xchainjs/xchain-client';
2
2
  import { CosmosSDKClient } from '@xchainjs/xchain-cosmos';
3
3
  import { validatePhrase } from '@xchainjs/xchain-crypto';
4
- import { assetToString, AssetRuneNative, isSynthAsset, assetFromString, baseAmount } from '@xchainjs/xchain-util';
4
+ import { assetToString, AssetRuneNative, isSynthAsset, assetFromString, baseAmount, assetToBase, assetAmount } from '@xchainjs/xchain-util';
5
5
  import axios from 'axios';
6
6
  import { AccAddress, Msg, codec } from 'cosmos-client';
7
7
  import { StdTx } from 'cosmos-client/x/auth';
@@ -115,7 +115,7 @@ var msgNativeTxFromJson = function (value) {
115
115
  };
116
116
 
117
117
  var DECIMAL = 8;
118
- var DEFAULT_GAS_VALUE = '2000000';
118
+ var DEFAULT_GAS_VALUE = '3000000';
119
119
  var DEPOSIT_GAS_VALUE = '500000000';
120
120
  var MAX_TX_COUNT = 100;
121
121
  /**
@@ -198,36 +198,6 @@ var getPrefix = function (network) {
198
198
  return 'tthor';
199
199
  }
200
200
  };
201
- var ChainId;
202
- (function (ChainId) {
203
- ChainId["Mainnet"] = "thorchain";
204
- ChainId["Stagenet"] = "thorchain-stagenet";
205
- ChainId["Testnet"] = "thorchain-v1";
206
- })(ChainId || (ChainId = {}));
207
- /**
208
- * Type guard to check whether string is a valid `ChainId`
209
- *
210
- * @param {string} id Chain id.
211
- * @returns {boolean} `true` or `false`
212
- */
213
- var isChainId = function (id) { return Object.values(ChainId).includes(id); };
214
- /**
215
- * Get the chain id.
216
- *
217
- * @param {Network} network
218
- * @returns {string} The chain id based on the network.
219
- *
220
- */
221
- var getChainId = function (network) {
222
- switch (network) {
223
- case Network.Mainnet:
224
- return ChainId.Mainnet;
225
- case Network.Stagenet:
226
- return ChainId.Stagenet;
227
- case Network.Testnet:
228
- return ChainId.Testnet;
229
- }
230
- };
231
201
  /**
232
202
  * Register Codecs based on the prefix.
233
203
  *
@@ -289,7 +259,7 @@ var getDepositTxDataFromLogs = function (logs, address) {
289
259
  * @returns {Fees} The default fee.
290
260
  */
291
261
  var getDefaultFees = function () {
292
- var fee = baseAmount(DEFAULT_GAS_VALUE, DECIMAL);
262
+ var fee = assetToBase(assetAmount(0.02 /* 0.02 RUNE */, DECIMAL));
293
263
  return singleFee(FeeType.FlatFee, fee);
294
264
  };
295
265
  /**
@@ -305,46 +275,52 @@ var getTxType = function (txData, encoding) {
305
275
  /**
306
276
  * Structure StdTx from MsgNativeTx.
307
277
  *
308
- * @param {string} txId The transaction id.
278
+ * @param {MsgNativeTx} msgNativeTx Msg of type `MsgNativeTx`.
279
+ * @param {string} nodeUrl Node url
280
+ * @param {chainId} ChainId Chain id of the network
281
+ *
309
282
  * @returns {Tx} The transaction details of the given transaction id.
310
283
  *
311
284
  * @throws {"Invalid client url"} Thrown if the client url is an invalid one.
312
285
  */
313
- var buildDepositTx = function (msgNativeTx, nodeUrl) { return __awaiter(void 0, void 0, void 0, function () {
314
- var data, chainId, response, fee, unsignedStdTx;
315
- var _a, _b;
316
- return __generator(this, function (_c) {
317
- switch (_c.label) {
318
- case 0: return [4 /*yield*/, axios.get(nodeUrl + "/cosmos/base/tendermint/v1beta1/node_info")];
319
- case 1:
320
- data = (_c.sent()).data;
321
- chainId = data.default_node_info.network;
322
- if (!chainId || !isChainId(chainId))
323
- throw new Error('invalid network');
324
- return [4 /*yield*/, axios.post(nodeUrl + "/thorchain/deposit", {
325
- coins: msgNativeTx.coins,
326
- memo: msgNativeTx.memo,
327
- base_req: {
328
- chain_id: chainId,
329
- from: msgNativeTx.signer,
330
- },
331
- })];
332
- case 2:
333
- response = (_c.sent()).data;
334
- if (!response || !response.value)
335
- throw new Error('Invalid client url');
336
- fee = (_b = (_a = response.value) === null || _a === void 0 ? void 0 : _a.fee) !== null && _b !== void 0 ? _b : { amount: [] };
337
- unsignedStdTx = StdTx.fromJSON({
338
- msg: response.value.msg,
339
- // override fee
340
- fee: __assign(__assign({}, fee), { gas: DEPOSIT_GAS_VALUE }),
341
- signatures: [],
342
- memo: '',
343
- });
344
- return [2 /*return*/, unsignedStdTx];
345
- }
286
+ var buildDepositTx = function (_a) {
287
+ var msgNativeTx = _a.msgNativeTx, nodeUrl = _a.nodeUrl, chainId = _a.chainId;
288
+ return __awaiter(void 0, void 0, void 0, function () {
289
+ var data, networkChainId, response, fee, unsignedStdTx;
290
+ var _b, _c;
291
+ return __generator(this, function (_d) {
292
+ switch (_d.label) {
293
+ case 0: return [4 /*yield*/, axios.get(nodeUrl + "/cosmos/base/tendermint/v1beta1/node_info")];
294
+ case 1:
295
+ data = (_d.sent()).data;
296
+ networkChainId = data.default_node_info.network;
297
+ if (!networkChainId || chainId !== networkChainId)
298
+ throw new Error("Invalid network (asked: " + chainId + " / returned: " + networkChainId);
299
+ return [4 /*yield*/, axios.post(nodeUrl + "/thorchain/deposit", {
300
+ coins: msgNativeTx.coins,
301
+ memo: msgNativeTx.memo,
302
+ base_req: {
303
+ chain_id: chainId,
304
+ from: msgNativeTx.signer,
305
+ },
306
+ })];
307
+ case 2:
308
+ response = (_d.sent()).data;
309
+ if (!response || !response.value)
310
+ throw new Error('Invalid client url');
311
+ fee = (_c = (_b = response.value) === null || _b === void 0 ? void 0 : _b.fee) !== null && _c !== void 0 ? _c : { amount: [] };
312
+ unsignedStdTx = StdTx.fromJSON({
313
+ msg: response.value.msg,
314
+ // override fee
315
+ fee: __assign(__assign({}, fee), { gas: DEPOSIT_GAS_VALUE }),
316
+ signatures: [],
317
+ memo: '',
318
+ });
319
+ return [2 /*return*/, unsignedStdTx];
320
+ }
321
+ });
346
322
  });
347
- }); };
323
+ };
348
324
  /**
349
325
  * Get the balance of a given address.
350
326
  *
@@ -493,13 +469,17 @@ var Client = /** @class */ (function () {
493
469
  * @throws {"Invalid phrase"} Thrown if the given phase is invalid.
494
470
  */
495
471
  function Client(_a) {
496
- var _b;
472
+ var _b, _c;
497
473
  var _this = this;
498
- var _c = _a.network, network = _c === void 0 ? Network.Testnet : _c, phrase = _a.phrase, clientUrl = _a.clientUrl, explorerUrls = _a.explorerUrls, _d = _a.rootDerivationPaths, rootDerivationPaths = _d === void 0 ? (_b = {},
474
+ var _d = _a.network, network = _d === void 0 ? Network.Testnet : _d, phrase = _a.phrase, clientUrl = _a.clientUrl, explorerUrls = _a.explorerUrls, _e = _a.rootDerivationPaths, rootDerivationPaths = _e === void 0 ? (_b = {},
499
475
  _b[Network.Mainnet] = "44'/931'/0'/0/",
500
476
  _b[Network.Stagenet] = "44'/931'/0'/0/",
501
477
  _b[Network.Testnet] = "44'/931'/0'/0/",
502
- _b) : _d;
478
+ _b) : _e, _f = _a.chainIds, chainIds = _f === void 0 ? (_c = {},
479
+ _c[Network.Mainnet] = 'thorchain',
480
+ _c[Network.Stagenet] = 'thorchain-stagenet',
481
+ _c[Network.Testnet] = 'thorchain-v1',
482
+ _c) : _f;
503
483
  this.phrase = '';
504
484
  /**
505
485
  * Get transaction history of a given address with pagination options.
@@ -569,10 +549,11 @@ var Client = /** @class */ (function () {
569
549
  this.network = network;
570
550
  this.clientUrl = clientUrl || getDefaultClientUrl();
571
551
  this.explorerUrls = explorerUrls || getDefaultExplorerUrls();
552
+ this.chainIds = chainIds;
572
553
  this.rootDerivationPaths = rootDerivationPaths;
573
554
  this.cosmosClient = new CosmosSDKClient({
574
555
  server: this.getClientUrl().node,
575
- chainId: getChainId(this.network),
556
+ chainId: this.getChainId(this.network),
576
557
  prefix: getPrefix(this.network),
577
558
  });
578
559
  if (phrase)
@@ -644,6 +625,28 @@ var Client = /** @class */ (function () {
644
625
  Client.prototype.getExplorerUrl = function () {
645
626
  return this.explorerUrls.root[this.network];
646
627
  };
628
+ /**
629
+ * Sets chain id
630
+ *
631
+ * @param {ChainId} chainId Chain id to update
632
+ * @param {Network} network (optional) Network for given chainId. If `network`not set, current network of the client is used
633
+ *
634
+ * @returns {void}
635
+ */
636
+ Client.prototype.setChainId = function (chainId, network) {
637
+ var _a;
638
+ this.chainIds = __assign(__assign({}, this.chainIds), (_a = {}, _a[network || this.network] = chainId, _a));
639
+ };
640
+ /**
641
+ * Gets chain id
642
+ *
643
+ * @param {Network} network (optional) Network to get chain id from. If `network`not set, current network of the client is used
644
+ *
645
+ * @returns {ChainId} Chain id based on the current network.
646
+ */
647
+ Client.prototype.getChainId = function (network) {
648
+ return this.chainIds[network || this.network];
649
+ };
647
650
  /**
648
651
  * Get cosmos client
649
652
  * @returns {CosmosSDKClient} current cosmos client
@@ -861,7 +864,9 @@ var Client = /** @class */ (function () {
861
864
  var assetInList = _a.asset;
862
865
  return assetToString(assetInList) === assetToString(asset);
863
866
  })[0]) === null || _d === void 0 ? void 0 : _d.amount) !== null && _e !== void 0 ? _e : baseAmount(0, DECIMAL);
864
- fee = baseAmount(DEFAULT_GAS_VALUE, DECIMAL);
867
+ return [4 /*yield*/, this.getFees()];
868
+ case 2:
869
+ fee = (_k.sent()).average;
865
870
  if (isAssetRuneNative(asset)) {
866
871
  // amount + fee < runeBalance
867
872
  if (runeBalance.lt(amount.plus(fee))) {
@@ -885,13 +890,17 @@ var Client = /** @class */ (function () {
885
890
  memo: memo,
886
891
  signer: signer,
887
892
  });
888
- return [4 /*yield*/, buildDepositTx(msgNativeTx, this.getClientUrl().node)];
889
- case 2:
893
+ return [4 /*yield*/, buildDepositTx({
894
+ msgNativeTx: msgNativeTx,
895
+ nodeUrl: this.getClientUrl().node,
896
+ chainId: this.getChainId(),
897
+ })];
898
+ case 3:
890
899
  unsignedStdTx = _k.sent();
891
900
  privateKey = this.getPrivKey(walletIndex);
892
901
  accAddress = AccAddress.fromBech32(signer);
893
902
  return [4 /*yield*/, this.cosmosClient.signAndBroadcast(unsignedStdTx, privateKey, accAddress)];
894
- case 3: return [2 /*return*/, (_g = (_f = (_k.sent())) === null || _f === void 0 ? void 0 : _f.txhash) !== null && _g !== void 0 ? _g : ''];
903
+ case 4: return [2 /*return*/, (_g = (_f = (_k.sent())) === null || _f === void 0 ? void 0 : _f.txhash) !== null && _g !== void 0 ? _g : ''];
895
904
  }
896
905
  });
897
906
  });
@@ -1010,18 +1019,35 @@ var Client = /** @class */ (function () {
1010
1019
  });
1011
1020
  };
1012
1021
  /**
1013
- * Get the fees.
1022
+ * Gets fees from Node
1014
1023
  *
1015
1024
  * @returns {Fees}
1016
1025
  */
1017
1026
  Client.prototype.getFees = function () {
1018
1027
  return __awaiter(this, void 0, void 0, function () {
1019
- return __generator(this, function (_a) {
1020
- return [2 /*return*/, getDefaultFees()];
1028
+ var fee, _a;
1029
+ return __generator(this, function (_b) {
1030
+ switch (_b.label) {
1031
+ case 0:
1032
+ _b.trys.push([0, 2, , 3]);
1033
+ return [4 /*yield*/, axios.get(this.getClientUrl().node + "/thorchain/constants")
1034
+ // validate data
1035
+ ];
1036
+ case 1:
1037
+ fee = (_b.sent()).data.int_64_values.NativeTransactionFee;
1038
+ // validate data
1039
+ if (!fee || isNaN(fee) || fee < 0)
1040
+ throw Error("Invalid fee: " + fee.toString());
1041
+ return [2 /*return*/, singleFee(FeeType.FlatFee, baseAmount(fee))];
1042
+ case 2:
1043
+ _a = _b.sent();
1044
+ return [2 /*return*/, getDefaultFees()];
1045
+ case 3: return [2 /*return*/];
1046
+ }
1021
1047
  });
1022
1048
  });
1023
1049
  };
1024
1050
  return Client;
1025
1051
  }());
1026
1052
 
1027
- export { ChainId, Client, DECIMAL, DEFAULT_GAS_VALUE, DEPOSIT_GAS_VALUE, MAX_TX_COUNT, MsgNativeTx, assetFromDenom, buildDepositTx, getBalance, getChainId, getDefaultClientUrl, getDefaultExplorerUrls, getDefaultFees, getDenom, getDepositTxDataFromLogs, getExplorerAddressUrl, getExplorerTxUrl, getExplorerUrl, getPrefix, getTxType, isAssetRuneNative, isBroadcastSuccess, isChainId, isMsgMultiSend, isMsgSend, msgNativeTxFromJson, registerCodecs };
1053
+ export { Client, DECIMAL, DEFAULT_GAS_VALUE, DEPOSIT_GAS_VALUE, MAX_TX_COUNT, MsgNativeTx, assetFromDenom, buildDepositTx, getBalance, getDefaultClientUrl, getDefaultExplorerUrls, getDefaultFees, getDenom, getDepositTxDataFromLogs, getExplorerAddressUrl, getExplorerTxUrl, getExplorerUrl, getPrefix, getTxType, isAssetRuneNative, isBroadcastSuccess, isMsgMultiSend, isMsgSend, msgNativeTxFromJson, registerCodecs };
package/lib/index.js CHANGED
@@ -123,7 +123,7 @@ var msgNativeTxFromJson = function (value) {
123
123
  };
124
124
 
125
125
  var DECIMAL = 8;
126
- var DEFAULT_GAS_VALUE = '2000000';
126
+ var DEFAULT_GAS_VALUE = '3000000';
127
127
  var DEPOSIT_GAS_VALUE = '500000000';
128
128
  var MAX_TX_COUNT = 100;
129
129
  /**
@@ -206,35 +206,6 @@ var getPrefix = function (network) {
206
206
  return 'tthor';
207
207
  }
208
208
  };
209
- (function (ChainId) {
210
- ChainId["Mainnet"] = "thorchain";
211
- ChainId["Stagenet"] = "thorchain-stagenet";
212
- ChainId["Testnet"] = "thorchain-v1";
213
- })(exports.ChainId || (exports.ChainId = {}));
214
- /**
215
- * Type guard to check whether string is a valid `ChainId`
216
- *
217
- * @param {string} id Chain id.
218
- * @returns {boolean} `true` or `false`
219
- */
220
- var isChainId = function (id) { return Object.values(exports.ChainId).includes(id); };
221
- /**
222
- * Get the chain id.
223
- *
224
- * @param {Network} network
225
- * @returns {string} The chain id based on the network.
226
- *
227
- */
228
- var getChainId = function (network) {
229
- switch (network) {
230
- case xchainClient.Network.Mainnet:
231
- return exports.ChainId.Mainnet;
232
- case xchainClient.Network.Stagenet:
233
- return exports.ChainId.Stagenet;
234
- case xchainClient.Network.Testnet:
235
- return exports.ChainId.Testnet;
236
- }
237
- };
238
209
  /**
239
210
  * Register Codecs based on the prefix.
240
211
  *
@@ -296,7 +267,7 @@ var getDepositTxDataFromLogs = function (logs, address) {
296
267
  * @returns {Fees} The default fee.
297
268
  */
298
269
  var getDefaultFees = function () {
299
- var fee = xchainUtil.baseAmount(DEFAULT_GAS_VALUE, DECIMAL);
270
+ var fee = xchainUtil.assetToBase(xchainUtil.assetAmount(0.02 /* 0.02 RUNE */, DECIMAL));
300
271
  return xchainClient.singleFee(xchainClient.FeeType.FlatFee, fee);
301
272
  };
302
273
  /**
@@ -312,46 +283,52 @@ var getTxType = function (txData, encoding) {
312
283
  /**
313
284
  * Structure StdTx from MsgNativeTx.
314
285
  *
315
- * @param {string} txId The transaction id.
286
+ * @param {MsgNativeTx} msgNativeTx Msg of type `MsgNativeTx`.
287
+ * @param {string} nodeUrl Node url
288
+ * @param {chainId} ChainId Chain id of the network
289
+ *
316
290
  * @returns {Tx} The transaction details of the given transaction id.
317
291
  *
318
292
  * @throws {"Invalid client url"} Thrown if the client url is an invalid one.
319
293
  */
320
- var buildDepositTx = function (msgNativeTx, nodeUrl) { return __awaiter(void 0, void 0, void 0, function () {
321
- var data, chainId, response, fee, unsignedStdTx;
322
- var _a, _b;
323
- return __generator(this, function (_c) {
324
- switch (_c.label) {
325
- case 0: return [4 /*yield*/, axios__default['default'].get(nodeUrl + "/cosmos/base/tendermint/v1beta1/node_info")];
326
- case 1:
327
- data = (_c.sent()).data;
328
- chainId = data.default_node_info.network;
329
- if (!chainId || !isChainId(chainId))
330
- throw new Error('invalid network');
331
- return [4 /*yield*/, axios__default['default'].post(nodeUrl + "/thorchain/deposit", {
332
- coins: msgNativeTx.coins,
333
- memo: msgNativeTx.memo,
334
- base_req: {
335
- chain_id: chainId,
336
- from: msgNativeTx.signer,
337
- },
338
- })];
339
- case 2:
340
- response = (_c.sent()).data;
341
- if (!response || !response.value)
342
- throw new Error('Invalid client url');
343
- fee = (_b = (_a = response.value) === null || _a === void 0 ? void 0 : _a.fee) !== null && _b !== void 0 ? _b : { amount: [] };
344
- unsignedStdTx = auth.StdTx.fromJSON({
345
- msg: response.value.msg,
346
- // override fee
347
- fee: __assign(__assign({}, fee), { gas: DEPOSIT_GAS_VALUE }),
348
- signatures: [],
349
- memo: '',
350
- });
351
- return [2 /*return*/, unsignedStdTx];
352
- }
294
+ var buildDepositTx = function (_a) {
295
+ var msgNativeTx = _a.msgNativeTx, nodeUrl = _a.nodeUrl, chainId = _a.chainId;
296
+ return __awaiter(void 0, void 0, void 0, function () {
297
+ var data, networkChainId, response, fee, unsignedStdTx;
298
+ var _b, _c;
299
+ return __generator(this, function (_d) {
300
+ switch (_d.label) {
301
+ case 0: return [4 /*yield*/, axios__default['default'].get(nodeUrl + "/cosmos/base/tendermint/v1beta1/node_info")];
302
+ case 1:
303
+ data = (_d.sent()).data;
304
+ networkChainId = data.default_node_info.network;
305
+ if (!networkChainId || chainId !== networkChainId)
306
+ throw new Error("Invalid network (asked: " + chainId + " / returned: " + networkChainId);
307
+ return [4 /*yield*/, axios__default['default'].post(nodeUrl + "/thorchain/deposit", {
308
+ coins: msgNativeTx.coins,
309
+ memo: msgNativeTx.memo,
310
+ base_req: {
311
+ chain_id: chainId,
312
+ from: msgNativeTx.signer,
313
+ },
314
+ })];
315
+ case 2:
316
+ response = (_d.sent()).data;
317
+ if (!response || !response.value)
318
+ throw new Error('Invalid client url');
319
+ fee = (_c = (_b = response.value) === null || _b === void 0 ? void 0 : _b.fee) !== null && _c !== void 0 ? _c : { amount: [] };
320
+ unsignedStdTx = auth.StdTx.fromJSON({
321
+ msg: response.value.msg,
322
+ // override fee
323
+ fee: __assign(__assign({}, fee), { gas: DEPOSIT_GAS_VALUE }),
324
+ signatures: [],
325
+ memo: '',
326
+ });
327
+ return [2 /*return*/, unsignedStdTx];
328
+ }
329
+ });
353
330
  });
354
- }); };
331
+ };
355
332
  /**
356
333
  * Get the balance of a given address.
357
334
  *
@@ -500,13 +477,17 @@ var Client = /** @class */ (function () {
500
477
  * @throws {"Invalid phrase"} Thrown if the given phase is invalid.
501
478
  */
502
479
  function Client(_a) {
503
- var _b;
480
+ var _b, _c;
504
481
  var _this = this;
505
- var _c = _a.network, network = _c === void 0 ? xchainClient.Network.Testnet : _c, phrase = _a.phrase, clientUrl = _a.clientUrl, explorerUrls = _a.explorerUrls, _d = _a.rootDerivationPaths, rootDerivationPaths = _d === void 0 ? (_b = {},
482
+ var _d = _a.network, network = _d === void 0 ? xchainClient.Network.Testnet : _d, phrase = _a.phrase, clientUrl = _a.clientUrl, explorerUrls = _a.explorerUrls, _e = _a.rootDerivationPaths, rootDerivationPaths = _e === void 0 ? (_b = {},
506
483
  _b[xchainClient.Network.Mainnet] = "44'/931'/0'/0/",
507
484
  _b[xchainClient.Network.Stagenet] = "44'/931'/0'/0/",
508
485
  _b[xchainClient.Network.Testnet] = "44'/931'/0'/0/",
509
- _b) : _d;
486
+ _b) : _e, _f = _a.chainIds, chainIds = _f === void 0 ? (_c = {},
487
+ _c[xchainClient.Network.Mainnet] = 'thorchain',
488
+ _c[xchainClient.Network.Stagenet] = 'thorchain-stagenet',
489
+ _c[xchainClient.Network.Testnet] = 'thorchain-v1',
490
+ _c) : _f;
510
491
  this.phrase = '';
511
492
  /**
512
493
  * Get transaction history of a given address with pagination options.
@@ -576,10 +557,11 @@ var Client = /** @class */ (function () {
576
557
  this.network = network;
577
558
  this.clientUrl = clientUrl || getDefaultClientUrl();
578
559
  this.explorerUrls = explorerUrls || getDefaultExplorerUrls();
560
+ this.chainIds = chainIds;
579
561
  this.rootDerivationPaths = rootDerivationPaths;
580
562
  this.cosmosClient = new xchainCosmos.CosmosSDKClient({
581
563
  server: this.getClientUrl().node,
582
- chainId: getChainId(this.network),
564
+ chainId: this.getChainId(this.network),
583
565
  prefix: getPrefix(this.network),
584
566
  });
585
567
  if (phrase)
@@ -651,6 +633,28 @@ var Client = /** @class */ (function () {
651
633
  Client.prototype.getExplorerUrl = function () {
652
634
  return this.explorerUrls.root[this.network];
653
635
  };
636
+ /**
637
+ * Sets chain id
638
+ *
639
+ * @param {ChainId} chainId Chain id to update
640
+ * @param {Network} network (optional) Network for given chainId. If `network`not set, current network of the client is used
641
+ *
642
+ * @returns {void}
643
+ */
644
+ Client.prototype.setChainId = function (chainId, network) {
645
+ var _a;
646
+ this.chainIds = __assign(__assign({}, this.chainIds), (_a = {}, _a[network || this.network] = chainId, _a));
647
+ };
648
+ /**
649
+ * Gets chain id
650
+ *
651
+ * @param {Network} network (optional) Network to get chain id from. If `network`not set, current network of the client is used
652
+ *
653
+ * @returns {ChainId} Chain id based on the current network.
654
+ */
655
+ Client.prototype.getChainId = function (network) {
656
+ return this.chainIds[network || this.network];
657
+ };
654
658
  /**
655
659
  * Get cosmos client
656
660
  * @returns {CosmosSDKClient} current cosmos client
@@ -868,7 +872,9 @@ var Client = /** @class */ (function () {
868
872
  var assetInList = _a.asset;
869
873
  return xchainUtil.assetToString(assetInList) === xchainUtil.assetToString(asset);
870
874
  })[0]) === null || _d === void 0 ? void 0 : _d.amount) !== null && _e !== void 0 ? _e : xchainUtil.baseAmount(0, DECIMAL);
871
- fee = xchainUtil.baseAmount(DEFAULT_GAS_VALUE, DECIMAL);
875
+ return [4 /*yield*/, this.getFees()];
876
+ case 2:
877
+ fee = (_k.sent()).average;
872
878
  if (isAssetRuneNative(asset)) {
873
879
  // amount + fee < runeBalance
874
880
  if (runeBalance.lt(amount.plus(fee))) {
@@ -892,13 +898,17 @@ var Client = /** @class */ (function () {
892
898
  memo: memo,
893
899
  signer: signer,
894
900
  });
895
- return [4 /*yield*/, buildDepositTx(msgNativeTx, this.getClientUrl().node)];
896
- case 2:
901
+ return [4 /*yield*/, buildDepositTx({
902
+ msgNativeTx: msgNativeTx,
903
+ nodeUrl: this.getClientUrl().node,
904
+ chainId: this.getChainId(),
905
+ })];
906
+ case 3:
897
907
  unsignedStdTx = _k.sent();
898
908
  privateKey = this.getPrivKey(walletIndex);
899
909
  accAddress = cosmosClient.AccAddress.fromBech32(signer);
900
910
  return [4 /*yield*/, this.cosmosClient.signAndBroadcast(unsignedStdTx, privateKey, accAddress)];
901
- case 3: return [2 /*return*/, (_g = (_f = (_k.sent())) === null || _f === void 0 ? void 0 : _f.txhash) !== null && _g !== void 0 ? _g : ''];
911
+ case 4: return [2 /*return*/, (_g = (_f = (_k.sent())) === null || _f === void 0 ? void 0 : _f.txhash) !== null && _g !== void 0 ? _g : ''];
902
912
  }
903
913
  });
904
914
  });
@@ -1017,14 +1027,31 @@ var Client = /** @class */ (function () {
1017
1027
  });
1018
1028
  };
1019
1029
  /**
1020
- * Get the fees.
1030
+ * Gets fees from Node
1021
1031
  *
1022
1032
  * @returns {Fees}
1023
1033
  */
1024
1034
  Client.prototype.getFees = function () {
1025
1035
  return __awaiter(this, void 0, void 0, function () {
1026
- return __generator(this, function (_a) {
1027
- return [2 /*return*/, getDefaultFees()];
1036
+ var fee, _a;
1037
+ return __generator(this, function (_b) {
1038
+ switch (_b.label) {
1039
+ case 0:
1040
+ _b.trys.push([0, 2, , 3]);
1041
+ return [4 /*yield*/, axios__default['default'].get(this.getClientUrl().node + "/thorchain/constants")
1042
+ // validate data
1043
+ ];
1044
+ case 1:
1045
+ fee = (_b.sent()).data.int_64_values.NativeTransactionFee;
1046
+ // validate data
1047
+ if (!fee || isNaN(fee) || fee < 0)
1048
+ throw Error("Invalid fee: " + fee.toString());
1049
+ return [2 /*return*/, xchainClient.singleFee(xchainClient.FeeType.FlatFee, xchainUtil.baseAmount(fee))];
1050
+ case 2:
1051
+ _a = _b.sent();
1052
+ return [2 /*return*/, getDefaultFees()];
1053
+ case 3: return [2 /*return*/];
1054
+ }
1028
1055
  });
1029
1056
  });
1030
1057
  };
@@ -1040,7 +1067,6 @@ exports.MsgNativeTx = MsgNativeTx;
1040
1067
  exports.assetFromDenom = assetFromDenom;
1041
1068
  exports.buildDepositTx = buildDepositTx;
1042
1069
  exports.getBalance = getBalance;
1043
- exports.getChainId = getChainId;
1044
1070
  exports.getDefaultClientUrl = getDefaultClientUrl;
1045
1071
  exports.getDefaultExplorerUrls = getDefaultExplorerUrls;
1046
1072
  exports.getDefaultFees = getDefaultFees;
@@ -1053,7 +1079,6 @@ exports.getPrefix = getPrefix;
1053
1079
  exports.getTxType = getTxType;
1054
1080
  exports.isAssetRuneNative = isAssetRuneNative;
1055
1081
  exports.isBroadcastSuccess = isBroadcastSuccess;
1056
- exports.isChainId = isChainId;
1057
1082
  exports.isMsgMultiSend = isMsgMultiSend;
1058
1083
  exports.isMsgSend = isMsgSend;
1059
1084
  exports.msgNativeTxFromJson = msgNativeTxFromJson;
@@ -11,9 +11,12 @@ export declare type ExplorerUrls = {
11
11
  address: ExplorerUrl;
12
12
  };
13
13
  export declare type ExplorerUrl = Record<Network, string>;
14
+ export declare type ChainId = string;
15
+ export declare type ChainIds = Record<Network, ChainId>;
14
16
  export declare type ThorchainClientParams = {
15
17
  clientUrl?: ClientUrl;
16
18
  explorerUrls?: ExplorerUrls;
19
+ chainIds?: ChainIds;
17
20
  };
18
21
  export declare type DepositParam = {
19
22
  walletIndex?: number;
@@ -35,3 +38,11 @@ export declare type TxOfflineParams = TxParams & {
35
38
  from_account_number: string;
36
39
  from_sequence: string;
37
40
  };
41
+ /**
42
+ * Response from `thorchain/constants` endpoint
43
+ */
44
+ export declare type ThorchainConstantsResponse = {
45
+ int_64_values: {
46
+ NativeTransactionFee: number;
47
+ };
48
+ };
package/lib/util.d.ts CHANGED
@@ -4,10 +4,10 @@ import { Asset } from '@xchainjs/xchain-util';
4
4
  import { Msg } from 'cosmos-client';
5
5
  import { StdTx } from 'cosmos-client/x/auth';
6
6
  import { MsgMultiSend, MsgSend } from 'cosmos-client/x/bank';
7
- import { ClientUrl, ExplorerUrls, TxData } from './types';
7
+ import { ChainId, ClientUrl, ExplorerUrls, TxData } from './types';
8
8
  import { MsgNativeTx } from './types/messages';
9
9
  export declare const DECIMAL = 8;
10
- export declare const DEFAULT_GAS_VALUE = "2000000";
10
+ export declare const DEFAULT_GAS_VALUE = "3000000";
11
11
  export declare const DEPOSIT_GAS_VALUE = "500000000";
12
12
  export declare const MAX_TX_COUNT = 100;
13
13
  /**
@@ -60,26 +60,6 @@ export declare const isBroadcastSuccess: (response: unknown) => boolean;
60
60
  *
61
61
  **/
62
62
  export declare const getPrefix: (network: Network) => "thor" | "sthor" | "tthor";
63
- export declare enum ChainId {
64
- Mainnet = "thorchain",
65
- Stagenet = "thorchain-stagenet",
66
- Testnet = "thorchain-v1"
67
- }
68
- /**
69
- * Type guard to check whether string is a valid `ChainId`
70
- *
71
- * @param {string} id Chain id.
72
- * @returns {boolean} `true` or `false`
73
- */
74
- export declare const isChainId: (id: string) => id is ChainId;
75
- /**
76
- * Get the chain id.
77
- *
78
- * @param {Network} network
79
- * @returns {string} The chain id based on the network.
80
- *
81
- */
82
- export declare const getChainId: (network: Network) => ChainId;
83
63
  /**
84
64
  * Register Codecs based on the prefix.
85
65
  *
@@ -111,12 +91,19 @@ export declare const getTxType: (txData: string, encoding: 'base64' | 'hex') =>
111
91
  /**
112
92
  * Structure StdTx from MsgNativeTx.
113
93
  *
114
- * @param {string} txId The transaction id.
94
+ * @param {MsgNativeTx} msgNativeTx Msg of type `MsgNativeTx`.
95
+ * @param {string} nodeUrl Node url
96
+ * @param {chainId} ChainId Chain id of the network
97
+ *
115
98
  * @returns {Tx} The transaction details of the given transaction id.
116
99
  *
117
100
  * @throws {"Invalid client url"} Thrown if the client url is an invalid one.
118
101
  */
119
- export declare const buildDepositTx: (msgNativeTx: MsgNativeTx, nodeUrl: string) => Promise<StdTx>;
102
+ export declare const buildDepositTx: ({ msgNativeTx, nodeUrl, chainId, }: {
103
+ msgNativeTx: MsgNativeTx;
104
+ nodeUrl: string;
105
+ chainId: ChainId;
106
+ }) => Promise<StdTx>;
120
107
  /**
121
108
  * Get the balance of a given address.
122
109
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xchainjs/xchain-thorchain",
3
- "version": "0.21.1",
3
+ "version": "0.22.2",
4
4
  "description": "Custom Thorchain client and utilities used by XChainJS clients",
5
5
  "keywords": [
6
6
  "THORChain",
@@ -33,11 +33,11 @@
33
33
  },
34
34
  "devDependencies": {
35
35
  "@types/big.js": "^6.0.0",
36
- "@xchainjs/xchain-client": "^0.11.0",
37
- "@xchainjs/xchain-cosmos": "^0.16.0",
36
+ "@xchainjs/xchain-client": "^0.11.1",
37
+ "@xchainjs/xchain-cosmos": "^0.16.1",
38
38
  "@xchainjs/xchain-crypto": "^0.2.4",
39
- "@xchainjs/xchain-util": "^0.5.0",
40
- "axios": "^0.21.0",
39
+ "@xchainjs/xchain-util": "^0.5.1",
40
+ "axios": "^0.25.0",
41
41
  "cosmos-client": "0.39.2",
42
42
  "nock": "^13.0.5"
43
43
  },
@@ -45,11 +45,11 @@
45
45
  "access": "public"
46
46
  },
47
47
  "peerDependencies": {
48
- "@xchainjs/xchain-client": "^0.11.0",
49
- "@xchainjs/xchain-cosmos": "^0.16.0",
48
+ "@xchainjs/xchain-client": "^0.11.1",
49
+ "@xchainjs/xchain-cosmos": "^0.16.1",
50
50
  "@xchainjs/xchain-crypto": "^0.2.4",
51
- "@xchainjs/xchain-util": "^0.5.0",
52
- "axios": "^0.21.0",
51
+ "@xchainjs/xchain-util": "^0.5.1",
52
+ "axios": "^0.25.0",
53
53
  "cosmos-client": "0.39.2"
54
54
  }
55
55
  }