@xchainjs/xchain-thorchain 0.22.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,10 @@
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
+
1
8
  # v0.22.1 (2022-02-16)
2
9
 
3
10
  ## Fix
package/lib/client.d.ts CHANGED
@@ -232,7 +232,7 @@ declare class Client implements ThorchainClient, XChainClient {
232
232
  */
233
233
  transferOffline({ walletIndex, asset, amount, recipient, memo, from_rune_balance, from_asset_balance, from_account_number, from_sequence, }: TxOfflineParams): Promise<StdTx>;
234
234
  /**
235
- * Get the fees.
235
+ * Gets fees from Node
236
236
  *
237
237
  * @returns {Fees}
238
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';
@@ -259,7 +259,7 @@ var getDepositTxDataFromLogs = function (logs, address) {
259
259
  * @returns {Fees} The default fee.
260
260
  */
261
261
  var getDefaultFees = function () {
262
- var fee = baseAmount(DEFAULT_GAS_VALUE, DECIMAL);
262
+ var fee = assetToBase(assetAmount(0.02 /* 0.02 RUNE */, DECIMAL));
263
263
  return singleFee(FeeType.FlatFee, fee);
264
264
  };
265
265
  /**
@@ -864,7 +864,9 @@ var Client = /** @class */ (function () {
864
864
  var assetInList = _a.asset;
865
865
  return assetToString(assetInList) === assetToString(asset);
866
866
  })[0]) === null || _d === void 0 ? void 0 : _d.amount) !== null && _e !== void 0 ? _e : baseAmount(0, DECIMAL);
867
- fee = baseAmount(DEFAULT_GAS_VALUE, DECIMAL);
867
+ return [4 /*yield*/, this.getFees()];
868
+ case 2:
869
+ fee = (_k.sent()).average;
868
870
  if (isAssetRuneNative(asset)) {
869
871
  // amount + fee < runeBalance
870
872
  if (runeBalance.lt(amount.plus(fee))) {
@@ -893,12 +895,12 @@ var Client = /** @class */ (function () {
893
895
  nodeUrl: this.getClientUrl().node,
894
896
  chainId: this.getChainId(),
895
897
  })];
896
- case 2:
898
+ case 3:
897
899
  unsignedStdTx = _k.sent();
898
900
  privateKey = this.getPrivKey(walletIndex);
899
901
  accAddress = AccAddress.fromBech32(signer);
900
902
  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 : ''];
903
+ case 4: return [2 /*return*/, (_g = (_f = (_k.sent())) === null || _f === void 0 ? void 0 : _f.txhash) !== null && _g !== void 0 ? _g : ''];
902
904
  }
903
905
  });
904
906
  });
@@ -1017,14 +1019,31 @@ var Client = /** @class */ (function () {
1017
1019
  });
1018
1020
  };
1019
1021
  /**
1020
- * Get the fees.
1022
+ * Gets fees from Node
1021
1023
  *
1022
1024
  * @returns {Fees}
1023
1025
  */
1024
1026
  Client.prototype.getFees = function () {
1025
1027
  return __awaiter(this, void 0, void 0, function () {
1026
- return __generator(this, function (_a) {
1027
- 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
+ }
1028
1047
  });
1029
1048
  });
1030
1049
  };
package/lib/index.js CHANGED
@@ -267,7 +267,7 @@ var getDepositTxDataFromLogs = function (logs, address) {
267
267
  * @returns {Fees} The default fee.
268
268
  */
269
269
  var getDefaultFees = function () {
270
- var fee = xchainUtil.baseAmount(DEFAULT_GAS_VALUE, DECIMAL);
270
+ var fee = xchainUtil.assetToBase(xchainUtil.assetAmount(0.02 /* 0.02 RUNE */, DECIMAL));
271
271
  return xchainClient.singleFee(xchainClient.FeeType.FlatFee, fee);
272
272
  };
273
273
  /**
@@ -872,7 +872,9 @@ var Client = /** @class */ (function () {
872
872
  var assetInList = _a.asset;
873
873
  return xchainUtil.assetToString(assetInList) === xchainUtil.assetToString(asset);
874
874
  })[0]) === null || _d === void 0 ? void 0 : _d.amount) !== null && _e !== void 0 ? _e : xchainUtil.baseAmount(0, DECIMAL);
875
- fee = xchainUtil.baseAmount(DEFAULT_GAS_VALUE, DECIMAL);
875
+ return [4 /*yield*/, this.getFees()];
876
+ case 2:
877
+ fee = (_k.sent()).average;
876
878
  if (isAssetRuneNative(asset)) {
877
879
  // amount + fee < runeBalance
878
880
  if (runeBalance.lt(amount.plus(fee))) {
@@ -901,12 +903,12 @@ var Client = /** @class */ (function () {
901
903
  nodeUrl: this.getClientUrl().node,
902
904
  chainId: this.getChainId(),
903
905
  })];
904
- case 2:
906
+ case 3:
905
907
  unsignedStdTx = _k.sent();
906
908
  privateKey = this.getPrivKey(walletIndex);
907
909
  accAddress = cosmosClient.AccAddress.fromBech32(signer);
908
910
  return [4 /*yield*/, this.cosmosClient.signAndBroadcast(unsignedStdTx, privateKey, accAddress)];
909
- 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 : ''];
910
912
  }
911
913
  });
912
914
  });
@@ -1025,14 +1027,31 @@ var Client = /** @class */ (function () {
1025
1027
  });
1026
1028
  };
1027
1029
  /**
1028
- * Get the fees.
1030
+ * Gets fees from Node
1029
1031
  *
1030
1032
  * @returns {Fees}
1031
1033
  */
1032
1034
  Client.prototype.getFees = function () {
1033
1035
  return __awaiter(this, void 0, void 0, function () {
1034
- return __generator(this, function (_a) {
1035
- 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
+ }
1036
1055
  });
1037
1056
  });
1038
1057
  };
@@ -38,3 +38,11 @@ export declare type TxOfflineParams = TxParams & {
38
38
  from_account_number: string;
39
39
  from_sequence: string;
40
40
  };
41
+ /**
42
+ * Response from `thorchain/constants` endpoint
43
+ */
44
+ export declare type ThorchainConstantsResponse = {
45
+ int_64_values: {
46
+ NativeTransactionFee: number;
47
+ };
48
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xchainjs/xchain-thorchain",
3
- "version": "0.22.1",
3
+ "version": "0.22.2",
4
4
  "description": "Custom Thorchain client and utilities used by XChainJS clients",
5
5
  "keywords": [
6
6
  "THORChain",