@xchainjs/xchain-wallet 0.1.5 → 0.1.7

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/index.esm.js CHANGED
@@ -1,4 +1,4 @@
1
- import { Network } from '@xchainjs/xchain-client';
1
+ import { Network, FeeOption } from '@xchainjs/xchain-client';
2
2
 
3
3
  /******************************************************************************
4
4
  Copyright (c) Microsoft Corporation.
@@ -2911,8 +2911,22 @@ function toFixedPoint(str, e, z) {
2911
2911
  // EXPORT
2912
2912
 
2913
2913
 
2914
- clone();
2914
+ var BigNumber$1 = clone();
2915
2915
 
2916
+ /**
2917
+ * Shortcut to create a BigNumber
2918
+ *
2919
+ * @param {string | number | BigNumber.Instance} value
2920
+ * @returns {BigNumber} The BigNumber interface from the given value.
2921
+ */
2922
+ const bn$1 = (value) => new BigNumber$1(value);
2923
+ /**
2924
+ * Helper to check whether a BigNumber is valid or not
2925
+ *
2926
+ * @param {BigNumber} value
2927
+ * @returns {boolean} `true` or `false`.
2928
+ * */
2929
+ const isValidBN = (value) => !value.isNaN();
2916
2930
  /**
2917
2931
  * The enumuration for symbol position.
2918
2932
  * `before` or `after`
@@ -2922,6 +2936,19 @@ var SymbolPosition;
2922
2936
  SymbolPosition["BEFORE"] = "before";
2923
2937
  SymbolPosition["AFTER"] = "after";
2924
2938
  })(SymbolPosition || (SymbolPosition = {}));
2939
+ /**
2940
+ * Helper to get a fixed `BigNumber`
2941
+ * Returns zero `BigNumber` if `value` is invalid
2942
+ *
2943
+ * @param {number|string|BigNumber|undefined} value
2944
+ * @param {number} decimalPlaces The decimal place. (optional)
2945
+ * @returns {BigNumber} The BigNumber interface from the given value and decimal.
2946
+ * */
2947
+ const fixedBN = (value, decimalPlaces = 2) => {
2948
+ const n = bn$1(value || 0);
2949
+ const fixedBN = isValidBN(n) ? n.toFixed(decimalPlaces) : bn$1(0).toFixed(decimalPlaces);
2950
+ return bn$1(fixedBN);
2951
+ };
2925
2952
 
2926
2953
  var Denomination;
2927
2954
  (function (Denomination) {
@@ -2934,6 +2961,49 @@ var Denomination;
2934
2961
  */
2935
2962
  Denomination["Asset"] = "ASSET";
2936
2963
  })(Denomination || (Denomination = {}));
2964
+
2965
+ /**
2966
+ * Guard to check whether value is a BigNumber.Value or not
2967
+ *
2968
+ * @param {unknown} v
2969
+ * @returns {boolean} `true` or `false`.
2970
+ * */
2971
+ const isBigNumberValue = (v) => typeof v === 'string' || typeof v === 'number' || v instanceof BigNumber$1;
2972
+ /**
2973
+ * Default number of asset decimals
2974
+ * For history reason and by starting the project on Binance chain assets, it's 8 decimal.F
2975
+ *
2976
+ * For example:
2977
+ * ```
2978
+ * RUNE has a maximum of 8 digits of decimal
2979
+ * 0.00000001 RUNE == 1 ð (tor)
2980
+ * ```
2981
+ * */
2982
+ const ASSET_DECIMAL = 8;
2983
+ /**
2984
+ * Factory to create base amounts (e.g. tor)
2985
+ *
2986
+ * @param {string|number|BigNumber|undefined} value - The base amount, If the value is undefined, BaseAmount with value `0` will be returned.
2987
+ * @param {number} decimal The decimal places of its associated AssetAmount. (optional)
2988
+ * @returns {BaseAmount} The base amount from the given value and decimal.
2989
+ **/
2990
+ const baseAmount = (value, decimal = ASSET_DECIMAL) => {
2991
+ const amount = fixedBN(value, 0);
2992
+ return {
2993
+ type: Denomination.Base,
2994
+ amount: () => amount,
2995
+ plus: (v, d = decimal) => baseAmount(amount.plus(isBigNumberValue(v) ? v : v.amount()), d),
2996
+ minus: (v, d = decimal) => baseAmount(amount.minus(isBigNumberValue(v) ? v : v.amount()), d),
2997
+ times: (v, d = decimal) => baseAmount(amount.times(isBigNumberValue(v) ? v : v.amount()), d),
2998
+ div: (v, d = decimal) => baseAmount(amount.div(isBigNumberValue(v) ? v : v.amount()).decimalPlaces(0, BigNumber$1.ROUND_DOWN), d),
2999
+ lt: (v) => amount.lt(isBigNumberValue(v) ? v : v.amount()),
3000
+ lte: (v) => amount.lte(isBigNumberValue(v) ? v : v.amount()),
3001
+ gt: (v) => amount.gt(isBigNumberValue(v) ? v : v.amount()),
3002
+ gte: (v) => amount.gte(isBigNumberValue(v) ? v : v.amount()),
3003
+ eq: (v) => amount.eq(isBigNumberValue(v) ? v : v.amount()),
3004
+ decimal,
3005
+ };
3006
+ };
2937
3007
  /**
2938
3008
  * Currency symbols currently supported
2939
3009
  */
@@ -2944,6 +3014,9 @@ var AssetCurrencySymbol;
2944
3014
  AssetCurrencySymbol["SATOSHI"] = "\u26A1";
2945
3015
  AssetCurrencySymbol["ETH"] = "\u039E";
2946
3016
  AssetCurrencySymbol["USD"] = "$";
3017
+ AssetCurrencySymbol["DASH"] = "\u0110";
3018
+ AssetCurrencySymbol["LTC"] = "\u0141";
3019
+ AssetCurrencySymbol["DOGE"] = "\u00D0";
2947
3020
  })(AssetCurrencySymbol || (AssetCurrencySymbol = {}));
2948
3021
  /**
2949
3022
  * Removes `0x` or `0X` from address
@@ -11975,17 +12048,20 @@ var erc20ABI = [
11975
12048
  }
11976
12049
  ];
11977
12050
 
12051
+ /**
12052
+ * Maximum approval amount possible, set to 2^256 - 1.
12053
+ */
11978
12054
  BigNumber.from(2).pow(256).sub(1);
11979
12055
  /**
11980
12056
  * Check allowance.
11981
12057
  *
11982
- * @param {Provider} provider Provider to interact with the contract.
12058
+ * @param {Provider} provider The provider to interact with the contract.
11983
12059
  * @param {Address} contractAddress The contract (ERC20 token) address.
11984
12060
  * @param {Address} spenderAddress The spender address (router).
11985
12061
  * @param {Address} fromAddress The address a transaction is sent from.
11986
12062
  * @param {BaseAmount} amount The amount to check if it's allowed to spend or not (optional).
11987
12063
  * @param {number} walletIndex (optional) HD wallet index
11988
- * @returns {boolean} `true` or `false`.
12064
+ * @returns {Promise<boolean>} `true` if the spender is allowed to spend the specified amount, `false` otherwise.
11989
12065
  */
11990
12066
  const isApproved = ({ provider, contractAddress, spenderAddress, fromAddress, amount, }) => __awaiter(void 0, void 0, void 0, function* () {
11991
12067
  var _a;
@@ -12236,7 +12312,8 @@ class Wallet {
12236
12312
  });
12237
12313
  }
12238
12314
  /**
12239
- * Get feeRates. Only available for EVM clients
12315
+ * @deprecated
12316
+ * Get feeRates. Only available for EVM clients. Use getFeeRates instead
12240
12317
  * @param {Chain} chain Chain of which return the feeRates
12241
12318
  * @returns {GasPrices} Chain of which return the feeRates
12242
12319
  * @throws {Error} If gas fee rates cannot be returned from the chain
@@ -12244,25 +12321,74 @@ class Wallet {
12244
12321
  getGasFeeRates(chain) {
12245
12322
  return __awaiter$5(this, void 0, void 0, function* () {
12246
12323
  const client = this.getClient(chain);
12247
- if (!('estimateGasPrices' in client))
12324
+ if (!this.isEvmClient(client))
12248
12325
  throw Error(`Can not get gas with ${chain} client`);
12249
12326
  return client.estimateGasPrices();
12250
12327
  });
12251
12328
  }
12329
+ /**
12330
+ * Get chain feeRates.
12331
+ * @param {Chain} chain of which return the feeRates
12332
+ * @param {Protocol} protocol to interact with. If it is not set, fee rates are calculated as chain rules
12333
+ * @returns {Record<FeeOption, BaseAmount>} the fee rates
12334
+ * @throws {Error} if the fee rates can not be returned from the chain
12335
+ */
12336
+ getFeeRates(chain, protocol) {
12337
+ return __awaiter$5(this, void 0, void 0, function* () {
12338
+ const client = this.getClient(chain);
12339
+ if (this.isEvmClient(client)) {
12340
+ return client.estimateGasPrices(protocol);
12341
+ }
12342
+ if (this.isUtxoClient(client)) {
12343
+ const feeRates = yield client.getFeeRates(protocol);
12344
+ const nativeDecimals = client.getAssetInfo().decimal;
12345
+ return {
12346
+ [FeeOption.Average]: baseAmount(feeRates[FeeOption.Average], nativeDecimals),
12347
+ [FeeOption.Fast]: baseAmount(feeRates[FeeOption.Fast], nativeDecimals),
12348
+ [FeeOption.Fastest]: baseAmount(feeRates[FeeOption.Fastest], nativeDecimals),
12349
+ };
12350
+ }
12351
+ throw Error(`getFeeRates method not supported in ${chain} chain`);
12352
+ });
12353
+ }
12252
12354
  /**
12253
12355
  * Make a transaction
12254
12356
  * @param {TxParams} txParams txParams - The parameters to make the transfer
12255
12357
  * @returns The transaction hash
12256
12358
  */
12257
- transfer({ asset, amount, recipient, memo, walletIndex }) {
12359
+ transfer(params) {
12258
12360
  return __awaiter$5(this, void 0, void 0, function* () {
12259
- const client = this.getClient(asset.chain);
12361
+ const client = this.getClient(params.asset.chain);
12362
+ if (this.isEvmClient(client)) {
12363
+ if (!this.isEvmTxParams(params))
12364
+ throw Error(`Invalid params for ${params.asset.chain} transfer`);
12365
+ return client.transfer({
12366
+ walletIndex: params.walletIndex,
12367
+ asset: params.asset,
12368
+ amount: params.amount,
12369
+ recipient: params.recipient,
12370
+ memo: params.memo,
12371
+ gasPrice: params.gasPrice,
12372
+ });
12373
+ }
12374
+ if (this.isUtxoClient(client)) {
12375
+ if (!this.isUtxoTxParams(params))
12376
+ throw Error(`Invalid params for ${params.asset.chain} transfer`);
12377
+ return client.transfer({
12378
+ walletIndex: params.walletIndex,
12379
+ asset: params.asset,
12380
+ amount: params.amount,
12381
+ recipient: params.recipient,
12382
+ memo: params.memo,
12383
+ feeRate: params.feeRate ? params.feeRate.amount().toNumber() : undefined,
12384
+ });
12385
+ }
12260
12386
  return client.transfer({
12261
- walletIndex,
12262
- asset,
12263
- amount,
12264
- recipient,
12265
- memo,
12387
+ walletIndex: params.walletIndex,
12388
+ asset: params.asset,
12389
+ amount: params.amount,
12390
+ recipient: params.recipient,
12391
+ memo: params.memo,
12266
12392
  });
12267
12393
  });
12268
12394
  }
@@ -12292,7 +12418,7 @@ class Wallet {
12292
12418
  approve(asset, amount, spenderAddress) {
12293
12419
  return __awaiter$5(this, void 0, void 0, function* () {
12294
12420
  const client = this.getClient(asset.chain);
12295
- if (!('approve' in client))
12421
+ if (!this.isEvmClient(client))
12296
12422
  throw Error('Can not make approve over non EVM client');
12297
12423
  if (asset.chain === asset.ticker)
12298
12424
  throw Error('Can not make approve over native asset');
@@ -12312,7 +12438,7 @@ class Wallet {
12312
12438
  isApproved(asset, amount, fromAddress, spenderAddress) {
12313
12439
  return __awaiter$5(this, void 0, void 0, function* () {
12314
12440
  const client = this.getClient(asset.chain);
12315
- if (!('isApproved' in client))
12441
+ if (!this.isEvmClient(client))
12316
12442
  throw Error('Can not validate approve over non EVM client');
12317
12443
  if (asset.chain === asset.ticker)
12318
12444
  throw Error('Can not validate approve over native asset');
@@ -12346,7 +12472,7 @@ class Wallet {
12346
12472
  */
12347
12473
  getChainWallet(chain) {
12348
12474
  const client = this.getClient(chain);
12349
- if (!('getWallet' in client))
12475
+ if (!this.isEvmClient(client))
12350
12476
  throw Error(`Can not get wallet of ${chain} client`);
12351
12477
  return client.getWallet();
12352
12478
  }
@@ -12362,6 +12488,30 @@ class Wallet {
12362
12488
  throw Error(`Client not found for ${chain} chain`);
12363
12489
  return client;
12364
12490
  }
12491
+ /**
12492
+ * Check if params type is compatible with EvmTxParams
12493
+ * @param {EvmTxParams | UtxoTxParams} params Params to validate the type of
12494
+ * @returns {boolean} True if params is EvmTxParams
12495
+ */
12496
+ isEvmTxParams(params) {
12497
+ return !('feeRate' in params);
12498
+ }
12499
+ /**
12500
+ * Check if params type is compatible with UtxoTxParams
12501
+ * @param {EvmTxParams | UtxoTxParams} params Params to validate the type of
12502
+ * @returns {boolean} True if params is UtxoTxParams
12503
+ */
12504
+ isUtxoTxParams(params) {
12505
+ return !('gasPrice' in params);
12506
+ }
12507
+ // TEMPORAL APPROACH UNTIL A NEW ONE
12508
+ isEvmClient(client) {
12509
+ return 'estimateGasPrices' in client;
12510
+ }
12511
+ // TEMPORAL APPROACH UNTIL A NEW ONE
12512
+ isUtxoClient(client) {
12513
+ return 'getFeeRates' in client;
12514
+ }
12365
12515
  }
12366
12516
 
12367
12517
  export { Wallet };
package/lib/index.js CHANGED
@@ -2915,8 +2915,22 @@ function toFixedPoint(str, e, z) {
2915
2915
  // EXPORT
2916
2916
 
2917
2917
 
2918
- clone();
2918
+ var BigNumber$1 = clone();
2919
2919
 
2920
+ /**
2921
+ * Shortcut to create a BigNumber
2922
+ *
2923
+ * @param {string | number | BigNumber.Instance} value
2924
+ * @returns {BigNumber} The BigNumber interface from the given value.
2925
+ */
2926
+ const bn$1 = (value) => new BigNumber$1(value);
2927
+ /**
2928
+ * Helper to check whether a BigNumber is valid or not
2929
+ *
2930
+ * @param {BigNumber} value
2931
+ * @returns {boolean} `true` or `false`.
2932
+ * */
2933
+ const isValidBN = (value) => !value.isNaN();
2920
2934
  /**
2921
2935
  * The enumuration for symbol position.
2922
2936
  * `before` or `after`
@@ -2926,6 +2940,19 @@ var SymbolPosition;
2926
2940
  SymbolPosition["BEFORE"] = "before";
2927
2941
  SymbolPosition["AFTER"] = "after";
2928
2942
  })(SymbolPosition || (SymbolPosition = {}));
2943
+ /**
2944
+ * Helper to get a fixed `BigNumber`
2945
+ * Returns zero `BigNumber` if `value` is invalid
2946
+ *
2947
+ * @param {number|string|BigNumber|undefined} value
2948
+ * @param {number} decimalPlaces The decimal place. (optional)
2949
+ * @returns {BigNumber} The BigNumber interface from the given value and decimal.
2950
+ * */
2951
+ const fixedBN = (value, decimalPlaces = 2) => {
2952
+ const n = bn$1(value || 0);
2953
+ const fixedBN = isValidBN(n) ? n.toFixed(decimalPlaces) : bn$1(0).toFixed(decimalPlaces);
2954
+ return bn$1(fixedBN);
2955
+ };
2929
2956
 
2930
2957
  var Denomination;
2931
2958
  (function (Denomination) {
@@ -2938,6 +2965,49 @@ var Denomination;
2938
2965
  */
2939
2966
  Denomination["Asset"] = "ASSET";
2940
2967
  })(Denomination || (Denomination = {}));
2968
+
2969
+ /**
2970
+ * Guard to check whether value is a BigNumber.Value or not
2971
+ *
2972
+ * @param {unknown} v
2973
+ * @returns {boolean} `true` or `false`.
2974
+ * */
2975
+ const isBigNumberValue = (v) => typeof v === 'string' || typeof v === 'number' || v instanceof BigNumber$1;
2976
+ /**
2977
+ * Default number of asset decimals
2978
+ * For history reason and by starting the project on Binance chain assets, it's 8 decimal.F
2979
+ *
2980
+ * For example:
2981
+ * ```
2982
+ * RUNE has a maximum of 8 digits of decimal
2983
+ * 0.00000001 RUNE == 1 ð (tor)
2984
+ * ```
2985
+ * */
2986
+ const ASSET_DECIMAL = 8;
2987
+ /**
2988
+ * Factory to create base amounts (e.g. tor)
2989
+ *
2990
+ * @param {string|number|BigNumber|undefined} value - The base amount, If the value is undefined, BaseAmount with value `0` will be returned.
2991
+ * @param {number} decimal The decimal places of its associated AssetAmount. (optional)
2992
+ * @returns {BaseAmount} The base amount from the given value and decimal.
2993
+ **/
2994
+ const baseAmount = (value, decimal = ASSET_DECIMAL) => {
2995
+ const amount = fixedBN(value, 0);
2996
+ return {
2997
+ type: Denomination.Base,
2998
+ amount: () => amount,
2999
+ plus: (v, d = decimal) => baseAmount(amount.plus(isBigNumberValue(v) ? v : v.amount()), d),
3000
+ minus: (v, d = decimal) => baseAmount(amount.minus(isBigNumberValue(v) ? v : v.amount()), d),
3001
+ times: (v, d = decimal) => baseAmount(amount.times(isBigNumberValue(v) ? v : v.amount()), d),
3002
+ div: (v, d = decimal) => baseAmount(amount.div(isBigNumberValue(v) ? v : v.amount()).decimalPlaces(0, BigNumber$1.ROUND_DOWN), d),
3003
+ lt: (v) => amount.lt(isBigNumberValue(v) ? v : v.amount()),
3004
+ lte: (v) => amount.lte(isBigNumberValue(v) ? v : v.amount()),
3005
+ gt: (v) => amount.gt(isBigNumberValue(v) ? v : v.amount()),
3006
+ gte: (v) => amount.gte(isBigNumberValue(v) ? v : v.amount()),
3007
+ eq: (v) => amount.eq(isBigNumberValue(v) ? v : v.amount()),
3008
+ decimal,
3009
+ };
3010
+ };
2941
3011
  /**
2942
3012
  * Currency symbols currently supported
2943
3013
  */
@@ -2948,6 +3018,9 @@ var AssetCurrencySymbol;
2948
3018
  AssetCurrencySymbol["SATOSHI"] = "\u26A1";
2949
3019
  AssetCurrencySymbol["ETH"] = "\u039E";
2950
3020
  AssetCurrencySymbol["USD"] = "$";
3021
+ AssetCurrencySymbol["DASH"] = "\u0110";
3022
+ AssetCurrencySymbol["LTC"] = "\u0141";
3023
+ AssetCurrencySymbol["DOGE"] = "\u00D0";
2951
3024
  })(AssetCurrencySymbol || (AssetCurrencySymbol = {}));
2952
3025
  /**
2953
3026
  * Removes `0x` or `0X` from address
@@ -11979,17 +12052,20 @@ var erc20ABI = [
11979
12052
  }
11980
12053
  ];
11981
12054
 
12055
+ /**
12056
+ * Maximum approval amount possible, set to 2^256 - 1.
12057
+ */
11982
12058
  BigNumber.from(2).pow(256).sub(1);
11983
12059
  /**
11984
12060
  * Check allowance.
11985
12061
  *
11986
- * @param {Provider} provider Provider to interact with the contract.
12062
+ * @param {Provider} provider The provider to interact with the contract.
11987
12063
  * @param {Address} contractAddress The contract (ERC20 token) address.
11988
12064
  * @param {Address} spenderAddress The spender address (router).
11989
12065
  * @param {Address} fromAddress The address a transaction is sent from.
11990
12066
  * @param {BaseAmount} amount The amount to check if it's allowed to spend or not (optional).
11991
12067
  * @param {number} walletIndex (optional) HD wallet index
11992
- * @returns {boolean} `true` or `false`.
12068
+ * @returns {Promise<boolean>} `true` if the spender is allowed to spend the specified amount, `false` otherwise.
11993
12069
  */
11994
12070
  const isApproved = ({ provider, contractAddress, spenderAddress, fromAddress, amount, }) => __awaiter(void 0, void 0, void 0, function* () {
11995
12071
  var _a;
@@ -12240,7 +12316,8 @@ class Wallet {
12240
12316
  });
12241
12317
  }
12242
12318
  /**
12243
- * Get feeRates. Only available for EVM clients
12319
+ * @deprecated
12320
+ * Get feeRates. Only available for EVM clients. Use getFeeRates instead
12244
12321
  * @param {Chain} chain Chain of which return the feeRates
12245
12322
  * @returns {GasPrices} Chain of which return the feeRates
12246
12323
  * @throws {Error} If gas fee rates cannot be returned from the chain
@@ -12248,25 +12325,74 @@ class Wallet {
12248
12325
  getGasFeeRates(chain) {
12249
12326
  return __awaiter$5(this, void 0, void 0, function* () {
12250
12327
  const client = this.getClient(chain);
12251
- if (!('estimateGasPrices' in client))
12328
+ if (!this.isEvmClient(client))
12252
12329
  throw Error(`Can not get gas with ${chain} client`);
12253
12330
  return client.estimateGasPrices();
12254
12331
  });
12255
12332
  }
12333
+ /**
12334
+ * Get chain feeRates.
12335
+ * @param {Chain} chain of which return the feeRates
12336
+ * @param {Protocol} protocol to interact with. If it is not set, fee rates are calculated as chain rules
12337
+ * @returns {Record<FeeOption, BaseAmount>} the fee rates
12338
+ * @throws {Error} if the fee rates can not be returned from the chain
12339
+ */
12340
+ getFeeRates(chain, protocol) {
12341
+ return __awaiter$5(this, void 0, void 0, function* () {
12342
+ const client = this.getClient(chain);
12343
+ if (this.isEvmClient(client)) {
12344
+ return client.estimateGasPrices(protocol);
12345
+ }
12346
+ if (this.isUtxoClient(client)) {
12347
+ const feeRates = yield client.getFeeRates(protocol);
12348
+ const nativeDecimals = client.getAssetInfo().decimal;
12349
+ return {
12350
+ [xchainClient.FeeOption.Average]: baseAmount(feeRates[xchainClient.FeeOption.Average], nativeDecimals),
12351
+ [xchainClient.FeeOption.Fast]: baseAmount(feeRates[xchainClient.FeeOption.Fast], nativeDecimals),
12352
+ [xchainClient.FeeOption.Fastest]: baseAmount(feeRates[xchainClient.FeeOption.Fastest], nativeDecimals),
12353
+ };
12354
+ }
12355
+ throw Error(`getFeeRates method not supported in ${chain} chain`);
12356
+ });
12357
+ }
12256
12358
  /**
12257
12359
  * Make a transaction
12258
12360
  * @param {TxParams} txParams txParams - The parameters to make the transfer
12259
12361
  * @returns The transaction hash
12260
12362
  */
12261
- transfer({ asset, amount, recipient, memo, walletIndex }) {
12363
+ transfer(params) {
12262
12364
  return __awaiter$5(this, void 0, void 0, function* () {
12263
- const client = this.getClient(asset.chain);
12365
+ const client = this.getClient(params.asset.chain);
12366
+ if (this.isEvmClient(client)) {
12367
+ if (!this.isEvmTxParams(params))
12368
+ throw Error(`Invalid params for ${params.asset.chain} transfer`);
12369
+ return client.transfer({
12370
+ walletIndex: params.walletIndex,
12371
+ asset: params.asset,
12372
+ amount: params.amount,
12373
+ recipient: params.recipient,
12374
+ memo: params.memo,
12375
+ gasPrice: params.gasPrice,
12376
+ });
12377
+ }
12378
+ if (this.isUtxoClient(client)) {
12379
+ if (!this.isUtxoTxParams(params))
12380
+ throw Error(`Invalid params for ${params.asset.chain} transfer`);
12381
+ return client.transfer({
12382
+ walletIndex: params.walletIndex,
12383
+ asset: params.asset,
12384
+ amount: params.amount,
12385
+ recipient: params.recipient,
12386
+ memo: params.memo,
12387
+ feeRate: params.feeRate ? params.feeRate.amount().toNumber() : undefined,
12388
+ });
12389
+ }
12264
12390
  return client.transfer({
12265
- walletIndex,
12266
- asset,
12267
- amount,
12268
- recipient,
12269
- memo,
12391
+ walletIndex: params.walletIndex,
12392
+ asset: params.asset,
12393
+ amount: params.amount,
12394
+ recipient: params.recipient,
12395
+ memo: params.memo,
12270
12396
  });
12271
12397
  });
12272
12398
  }
@@ -12296,7 +12422,7 @@ class Wallet {
12296
12422
  approve(asset, amount, spenderAddress) {
12297
12423
  return __awaiter$5(this, void 0, void 0, function* () {
12298
12424
  const client = this.getClient(asset.chain);
12299
- if (!('approve' in client))
12425
+ if (!this.isEvmClient(client))
12300
12426
  throw Error('Can not make approve over non EVM client');
12301
12427
  if (asset.chain === asset.ticker)
12302
12428
  throw Error('Can not make approve over native asset');
@@ -12316,7 +12442,7 @@ class Wallet {
12316
12442
  isApproved(asset, amount, fromAddress, spenderAddress) {
12317
12443
  return __awaiter$5(this, void 0, void 0, function* () {
12318
12444
  const client = this.getClient(asset.chain);
12319
- if (!('isApproved' in client))
12445
+ if (!this.isEvmClient(client))
12320
12446
  throw Error('Can not validate approve over non EVM client');
12321
12447
  if (asset.chain === asset.ticker)
12322
12448
  throw Error('Can not validate approve over native asset');
@@ -12350,7 +12476,7 @@ class Wallet {
12350
12476
  */
12351
12477
  getChainWallet(chain) {
12352
12478
  const client = this.getClient(chain);
12353
- if (!('getWallet' in client))
12479
+ if (!this.isEvmClient(client))
12354
12480
  throw Error(`Can not get wallet of ${chain} client`);
12355
12481
  return client.getWallet();
12356
12482
  }
@@ -12366,6 +12492,30 @@ class Wallet {
12366
12492
  throw Error(`Client not found for ${chain} chain`);
12367
12493
  return client;
12368
12494
  }
12495
+ /**
12496
+ * Check if params type is compatible with EvmTxParams
12497
+ * @param {EvmTxParams | UtxoTxParams} params Params to validate the type of
12498
+ * @returns {boolean} True if params is EvmTxParams
12499
+ */
12500
+ isEvmTxParams(params) {
12501
+ return !('feeRate' in params);
12502
+ }
12503
+ /**
12504
+ * Check if params type is compatible with UtxoTxParams
12505
+ * @param {EvmTxParams | UtxoTxParams} params Params to validate the type of
12506
+ * @returns {boolean} True if params is UtxoTxParams
12507
+ */
12508
+ isUtxoTxParams(params) {
12509
+ return !('gasPrice' in params);
12510
+ }
12511
+ // TEMPORAL APPROACH UNTIL A NEW ONE
12512
+ isEvmClient(client) {
12513
+ return 'estimateGasPrices' in client;
12514
+ }
12515
+ // TEMPORAL APPROACH UNTIL A NEW ONE
12516
+ isUtxoClient(client) {
12517
+ return 'getFeeRates' in client;
12518
+ }
12369
12519
  }
12370
12520
 
12371
12521
  exports.Wallet = Wallet;
package/lib/types.d.ts ADDED
@@ -0,0 +1,16 @@
1
+ import { TxParams as BaseTxParams } from '@xchainjs/xchain-client';
2
+ import { Asset, BaseAmount } from '@xchainjs/xchain-util';
3
+ /**
4
+ * UTXO transfer params
5
+ */
6
+ export type UtxoTxParams = BaseTxParams & {
7
+ asset: Asset;
8
+ feeRate?: BaseAmount;
9
+ };
10
+ /**
11
+ * EVM transfer params
12
+ */
13
+ export type EvmTxParams = BaseTxParams & {
14
+ asset: Asset;
15
+ gasPrice?: BaseAmount;
16
+ };
package/lib/wallet.d.ts CHANGED
@@ -1,11 +1,12 @@
1
1
  /**
2
2
  * Import necessary modules and libraries
3
3
  */
4
- import { Balance, Network, Tx, TxHash, TxParams, TxsPage, XChainClient } from '@xchainjs/xchain-client';
4
+ import { Balance, FeeOption, Network, Protocol, Tx, TxHash, TxsPage, XChainClient } from '@xchainjs/xchain-client';
5
5
  import { GasPrices } from '@xchainjs/xchain-evm';
6
6
  import { DepositParam } from '@xchainjs/xchain-mayachain';
7
7
  import { Address, Asset, BaseAmount, Chain } from '@xchainjs/xchain-util';
8
8
  import { ethers } from 'ethers';
9
+ import { EvmTxParams, UtxoTxParams } from './types';
9
10
  export type NodeUrls = Record<Network, string>;
10
11
  export declare class Wallet {
11
12
  private clients;
@@ -119,20 +120,27 @@ export declare class Wallet {
119
120
  */
120
121
  getExplorerTxUrl(chain: Chain, hash: string): Promise<string>;
121
122
  /**
122
- * Get feeRates. Only available for EVM clients
123
+ * @deprecated
124
+ * Get feeRates. Only available for EVM clients. Use getFeeRates instead
123
125
  * @param {Chain} chain Chain of which return the feeRates
124
126
  * @returns {GasPrices} Chain of which return the feeRates
125
127
  * @throws {Error} If gas fee rates cannot be returned from the chain
126
128
  */
127
129
  getGasFeeRates(chain: Chain): Promise<GasPrices>;
130
+ /**
131
+ * Get chain feeRates.
132
+ * @param {Chain} chain of which return the feeRates
133
+ * @param {Protocol} protocol to interact with. If it is not set, fee rates are calculated as chain rules
134
+ * @returns {Record<FeeOption, BaseAmount>} the fee rates
135
+ * @throws {Error} if the fee rates can not be returned from the chain
136
+ */
137
+ getFeeRates(chain: Chain, protocol?: Protocol): Promise<Record<FeeOption, BaseAmount>>;
128
138
  /**
129
139
  * Make a transaction
130
140
  * @param {TxParams} txParams txParams - The parameters to make the transfer
131
141
  * @returns The transaction hash
132
142
  */
133
- transfer({ asset, amount, recipient, memo, walletIndex }: TxParams & {
134
- asset: Asset;
135
- }): Promise<string>;
143
+ transfer(params: UtxoTxParams | EvmTxParams): Promise<string>;
136
144
  /**
137
145
  * Make a deposit
138
146
  * @param {DepositParam} depositParams
@@ -183,4 +191,18 @@ export declare class Wallet {
183
191
  * @throws {Error} If the client does not exist
184
192
  */
185
193
  private getClient;
194
+ /**
195
+ * Check if params type is compatible with EvmTxParams
196
+ * @param {EvmTxParams | UtxoTxParams} params Params to validate the type of
197
+ * @returns {boolean} True if params is EvmTxParams
198
+ */
199
+ private isEvmTxParams;
200
+ /**
201
+ * Check if params type is compatible with UtxoTxParams
202
+ * @param {EvmTxParams | UtxoTxParams} params Params to validate the type of
203
+ * @returns {boolean} True if params is UtxoTxParams
204
+ */
205
+ private isUtxoTxParams;
206
+ private isEvmClient;
207
+ private isUtxoClient;
186
208
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@xchainjs/xchain-wallet",
3
3
  "description": "XChainjs clients wrapper to work with several chains at the same time",
4
- "version": "0.1.5",
4
+ "version": "0.1.7",
5
5
  "license": "MIT",
6
6
  "main": "lib/index.js",
7
7
  "module": "lib/index.esm.js",
@@ -28,15 +28,16 @@
28
28
  "access": "public"
29
29
  },
30
30
  "dependencies": {
31
- "@xchainjs/xchain-evm": "0.4.4",
32
- "@xchainjs/xchain-util": "0.13.2",
33
- "@xchainjs/xchain-client": "0.16.1",
34
- "@xchainjs/xchain-thorchain": "1.0.3",
35
- "@xchainjs/xchain-mayachain": "1.0.0",
31
+ "@xchainjs/xchain-evm": "0.4.5",
32
+ "@xchainjs/xchain-util": "0.13.3",
33
+ "@xchainjs/xchain-client": "0.16.2",
34
+ "@xchainjs/xchain-thorchain": "1.0.4",
35
+ "@xchainjs/xchain-mayachain": "1.0.1",
36
+ "@xchainjs/xchain-utxo": "0.1.4",
36
37
  "ethers": "5.6.6"
37
38
  },
38
39
  "devDependencies": {
39
- "@xchainjs/xchain-bitcoin": "0.23.10",
40
- "@xchainjs/xchain-ethereum": "0.31.5"
40
+ "@xchainjs/xchain-bitcoin": "0.23.12",
41
+ "@xchainjs/xchain-ethereum": "0.31.6"
41
42
  }
42
43
  }