@xchainjs/xchain-thorchain 0.20.1 → 0.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,24 @@
1
+ # v.0.21.0 (2022-02-02)
2
+
3
+ ## Breaking change
4
+
5
+ - Remove `getDenomWithChain`
6
+ - Rename `getAsset` -> `assetFromDenom` (incl. fix to get synth asset properly)
7
+
8
+ ## Update
9
+
10
+ - xchain-util@0.5.0
11
+ - xchain-cosmos@0.16.0
12
+
13
+ ## Add
14
+
15
+ - `isAssetNativeRune` helper
16
+ - Add `TxOfflineParams` type
17
+
18
+ ## Fix
19
+
20
+ - Fix synth notation in `transfer|transferOffline|deposit` #473
21
+
1
22
  # v0.20.1 (2022-01-11)
2
23
 
3
24
  ## Fix
package/lib/client.d.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  import { Address, Balance, Fees, Network, Tx, TxHash, TxHistoryParams, TxParams, TxsPage, XChainClient, XChainClientParams } from '@xchainjs/xchain-client';
2
- import { CosmosSDKClient, RPCTxResult, TxOfflineParams } from '@xchainjs/xchain-cosmos';
2
+ 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 } from './types';
6
+ import { ClientUrl, DepositParam, ExplorerUrls, NodeUrl, ThorchainClientParams, TxOfflineParams } from './types';
7
7
  /**
8
8
  * Interface for custom Thorchain client
9
9
  */
@@ -212,7 +212,7 @@ declare class Client implements ThorchainClient, XChainClient {
212
212
  * @param {TxOfflineParams} params The transfer offline options.
213
213
  * @returns {StdTx} The signed transaction.
214
214
  */
215
- transferOffline({ walletIndex, asset, amount, recipient, memo, from_balance, from_account_number, from_sequence, }: TxOfflineParams): Promise<StdTx>;
215
+ transferOffline({ walletIndex, asset, amount, recipient, memo, from_rune_balance, from_asset_balance, from_account_number, from_sequence, }: TxOfflineParams): Promise<StdTx>;
216
216
  /**
217
217
  * Get the fees.
218
218
  *
package/lib/index.esm.js CHANGED
@@ -1,7 +1,7 @@
1
- import { Network, TxType, singleFee, FeeType, FeeOption } from '@xchainjs/xchain-client';
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, Chain, assetFromString, baseAmount } from '@xchainjs/xchain-util';
4
+ import { assetToString, AssetRuneNative, isSynthAsset, assetFromString, baseAmount } 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';
@@ -119,24 +119,25 @@ var DEFAULT_GAS_VALUE = '2000000';
119
119
  var DEPOSIT_GAS_VALUE = '500000000';
120
120
  var MAX_TX_COUNT = 100;
121
121
  /**
122
- * Get denomination from Asset
122
+ * Checks whether an asset is `AssetRuneNative`
123
123
  *
124
124
  * @param {Asset} asset
125
- * @returns {string} The denomination of the given asset.
125
+ * @returns {boolean} `true` or `false`
126
126
  */
127
- var getDenom = function (asset) {
128
- if (assetToString(asset) === assetToString(AssetRuneNative))
129
- return 'rune';
130
- return asset.symbol;
131
- };
127
+ var isAssetRuneNative = function (asset) { return assetToString(asset) === assetToString(AssetRuneNative); };
128
+ var DENOM_RUNE_NATIVE = 'rune';
132
129
  /**
133
- * Get denomination with chainname from Asset
130
+ * Get denomination from Asset
134
131
  *
135
132
  * @param {Asset} asset
136
- * @returns {string} The denomination with chainname of the given asset.
133
+ * @returns {string} The denomination of the given asset.
137
134
  */
138
- var getDenomWithChain = function (asset) {
139
- return Chain.THORChain + "." + asset.symbol.toUpperCase();
135
+ var getDenom = function (asset) {
136
+ if (isAssetRuneNative(asset))
137
+ return DENOM_RUNE_NATIVE;
138
+ if (isSynthAsset(asset))
139
+ return assetToString(asset).toLowerCase();
140
+ return asset.symbol.toLowerCase();
140
141
  };
141
142
  /**
142
143
  * Get Asset from denomination
@@ -144,10 +145,10 @@ var getDenomWithChain = function (asset) {
144
145
  * @param {string} denom
145
146
  * @returns {Asset|null} The asset of the given denomination.
146
147
  */
147
- var getAsset = function (denom) {
148
- if (denom === getDenom(AssetRuneNative))
148
+ var assetFromDenom = function (denom) {
149
+ if (denom === DENOM_RUNE_NATIVE)
149
150
  return AssetRuneNative;
150
- return assetFromString(Chain.THORChain + "." + denom.toUpperCase());
151
+ return assetFromString(denom.toUpperCase());
151
152
  };
152
153
  /**
153
154
  * Type guard for MsgSend
@@ -351,7 +352,7 @@ var getBalance = function (_a) {
351
352
  balances = _b.sent();
352
353
  return [2 /*return*/, balances
353
354
  .map(function (balance) { return ({
354
- asset: (balance.denom && getAsset(balance.denom)) || AssetRuneNative,
355
+ asset: (balance.denom && assetFromDenom(balance.denom)) || AssetRuneNative,
355
356
  amount: baseAmount(balance.amount, DECIMAL),
356
357
  }); })
357
358
  .filter(function (balance) { return !assets || assets.filter(function (asset) { return assetToString(balance.asset) === assetToString(asset); }).length; })];
@@ -830,23 +831,41 @@ var Client = /** @class */ (function () {
830
831
  * @throws {"failed to broadcast transaction"} Thrown if failed to broadcast transaction.
831
832
  */
832
833
  Client.prototype.deposit = function (_a) {
833
- var _b, _c;
834
- var _d = _a.walletIndex, walletIndex = _d === void 0 ? 0 : _d, _e = _a.asset, asset = _e === void 0 ? AssetRuneNative : _e, amount = _a.amount, memo = _a.memo;
834
+ var _b, _c, _d, _e, _f, _g;
835
+ var _h = _a.walletIndex, walletIndex = _h === void 0 ? 0 : _h, _j = _a.asset, asset = _j === void 0 ? AssetRuneNative : _j, amount = _a.amount, memo = _a.memo;
835
836
  return __awaiter(this, void 0, void 0, function () {
836
- var assetBalance, signer, msgNativeTx, unsignedStdTx, privateKey, accAddress;
837
- return __generator(this, function (_f) {
838
- switch (_f.label) {
839
- case 0: return [4 /*yield*/, this.getBalance(this.getAddress(walletIndex), [asset])];
837
+ var balances, runeBalance, assetBalance, fee, signer, msgNativeTx, unsignedStdTx, privateKey, accAddress;
838
+ return __generator(this, function (_k) {
839
+ switch (_k.label) {
840
+ case 0: return [4 /*yield*/, this.getBalance(this.getAddress(walletIndex))];
840
841
  case 1:
841
- assetBalance = _f.sent();
842
- if (assetBalance.length === 0 || assetBalance[0].amount.amount().lt(amount.amount().plus(DEFAULT_GAS_VALUE))) {
843
- throw new Error('insufficient funds');
842
+ balances = _k.sent();
843
+ runeBalance = (_c = (_b = balances.filter(function (_a) {
844
+ var asset = _a.asset;
845
+ return isAssetRuneNative(asset);
846
+ })[0]) === null || _b === void 0 ? void 0 : _b.amount) !== null && _c !== void 0 ? _c : baseAmount(0, DECIMAL);
847
+ assetBalance = (_e = (_d = balances.filter(function (_a) {
848
+ var assetInList = _a.asset;
849
+ return assetToString(assetInList) === assetToString(asset);
850
+ })[0]) === null || _d === void 0 ? void 0 : _d.amount) !== null && _e !== void 0 ? _e : baseAmount(0, DECIMAL);
851
+ fee = baseAmount(DEFAULT_GAS_VALUE, DECIMAL);
852
+ if (isAssetRuneNative(asset)) {
853
+ // amount + fee < runeBalance
854
+ if (runeBalance.lt(amount.plus(fee))) {
855
+ throw new Error('insufficient funds');
856
+ }
857
+ }
858
+ else {
859
+ // amount < assetBalances && runeBalance < fee
860
+ if (assetBalance.lt(amount) || runeBalance.lt(fee)) {
861
+ throw new Error('insufficient funds');
862
+ }
844
863
  }
845
864
  signer = this.getAddress(walletIndex);
846
865
  msgNativeTx = msgNativeTxFromJson({
847
866
  coins: [
848
867
  {
849
- asset: getDenomWithChain(asset),
868
+ asset: isAssetRuneNative(asset) ? assetToString(AssetRuneNative) : getDenom(asset),
850
869
  amount: amount.amount().toString(),
851
870
  },
852
871
  ],
@@ -855,11 +874,11 @@ var Client = /** @class */ (function () {
855
874
  });
856
875
  return [4 /*yield*/, buildDepositTx(msgNativeTx, this.getClientUrl().node)];
857
876
  case 2:
858
- unsignedStdTx = _f.sent();
877
+ unsignedStdTx = _k.sent();
859
878
  privateKey = this.getPrivKey(walletIndex);
860
879
  accAddress = AccAddress.fromBech32(signer);
861
880
  return [4 /*yield*/, this.cosmosClient.signAndBroadcast(unsignedStdTx, privateKey, accAddress)];
862
- case 3: return [2 /*return*/, (_c = (_b = (_f.sent())) === null || _b === void 0 ? void 0 : _b.txhash) !== null && _c !== void 0 ? _c : ''];
881
+ case 3: return [2 /*return*/, (_g = (_f = (_k.sent())) === null || _f === void 0 ? void 0 : _f.txhash) !== null && _g !== void 0 ? _g : ''];
863
882
  }
864
883
  });
865
884
  });
@@ -871,22 +890,39 @@ var Client = /** @class */ (function () {
871
890
  * @returns {TxHash} The transaction hash.
872
891
  */
873
892
  Client.prototype.transfer = function (_a) {
874
- var _b = _a.walletIndex, walletIndex = _b === void 0 ? 0 : _b, _c = _a.asset, asset = _c === void 0 ? AssetRuneNative : _c, amount = _a.amount, recipient = _a.recipient, memo = _a.memo;
893
+ var _b, _c, _d, _e;
894
+ var _f = _a.walletIndex, walletIndex = _f === void 0 ? 0 : _f, _g = _a.asset, asset = _g === void 0 ? AssetRuneNative : _g, amount = _a.amount, recipient = _a.recipient, memo = _a.memo;
875
895
  return __awaiter(this, void 0, void 0, function () {
876
- var assetBalance, fee, transferResult;
877
- return __generator(this, function (_d) {
878
- switch (_d.label) {
896
+ var balances, runeBalance, assetBalance, fee, transferResult;
897
+ return __generator(this, function (_h) {
898
+ switch (_h.label) {
879
899
  case 0:
880
900
  registerCodecs(getPrefix(this.network));
881
- return [4 /*yield*/, this.getBalance(this.getAddress(walletIndex), [asset])];
901
+ return [4 /*yield*/, this.getBalance(this.getAddress(walletIndex))];
882
902
  case 1:
883
- assetBalance = _d.sent();
903
+ balances = _h.sent();
904
+ runeBalance = (_c = (_b = balances.filter(function (_a) {
905
+ var asset = _a.asset;
906
+ return isAssetRuneNative(asset);
907
+ })[0]) === null || _b === void 0 ? void 0 : _b.amount) !== null && _c !== void 0 ? _c : baseAmount(0, DECIMAL);
908
+ assetBalance = (_e = (_d = balances.filter(function (_a) {
909
+ var assetInList = _a.asset;
910
+ return assetToString(assetInList) === assetToString(asset);
911
+ })[0]) === null || _d === void 0 ? void 0 : _d.amount) !== null && _e !== void 0 ? _e : baseAmount(0, DECIMAL);
884
912
  return [4 /*yield*/, this.getFees()];
885
913
  case 2:
886
- fee = _d.sent();
887
- if (assetBalance.length === 0 ||
888
- assetBalance[0].amount.amount().lt(amount.amount().plus(fee[FeeOption.Average].amount()))) {
889
- throw new Error('insufficient funds');
914
+ fee = (_h.sent()).average;
915
+ if (isAssetRuneNative(asset)) {
916
+ // amount + fee < runeBalance
917
+ if (runeBalance.lt(amount.plus(fee))) {
918
+ throw new Error('insufficient funds');
919
+ }
920
+ }
921
+ else {
922
+ // amount < assetBalances && runeBalance < fee
923
+ if (assetBalance.lt(amount) || runeBalance.lt(fee)) {
924
+ throw new Error('insufficient funds');
925
+ }
890
926
  }
891
927
  return [4 /*yield*/, this.cosmosClient.transfer({
892
928
  privkey: this.getPrivKey(walletIndex),
@@ -901,7 +937,7 @@ var Client = /** @class */ (function () {
901
937
  },
902
938
  })];
903
939
  case 3:
904
- transferResult = _d.sent();
940
+ transferResult = _h.sent();
905
941
  if (!isBroadcastSuccess(transferResult)) {
906
942
  throw new Error("failed to broadcast transaction: " + transferResult.txhash);
907
943
  }
@@ -917,7 +953,7 @@ var Client = /** @class */ (function () {
917
953
  * @returns {StdTx} The signed transaction.
918
954
  */
919
955
  Client.prototype.transferOffline = function (_a) {
920
- var _b = _a.walletIndex, walletIndex = _b === void 0 ? 0 : _b, _c = _a.asset, asset = _c === void 0 ? AssetRuneNative : _c, amount = _a.amount, recipient = _a.recipient, memo = _a.memo, _d = _a.from_balance, from_balance = _d === void 0 ? baseAmount('0', DECIMAL) : _d, _e = _a.from_account_number, from_account_number = _e === void 0 ? '0' : _e, _f = _a.from_sequence, from_sequence = _f === void 0 ? '0' : _f;
956
+ var _b = _a.walletIndex, walletIndex = _b === void 0 ? 0 : _b, _c = _a.asset, asset = _c === void 0 ? AssetRuneNative : _c, amount = _a.amount, recipient = _a.recipient, memo = _a.memo, from_rune_balance = _a.from_rune_balance, _d = _a.from_asset_balance, from_asset_balance = _d === void 0 ? baseAmount(0, DECIMAL) : _d, _e = _a.from_account_number, from_account_number = _e === void 0 ? '0' : _e, _f = _a.from_sequence, from_sequence = _f === void 0 ? '0' : _f;
921
957
  return __awaiter(this, void 0, void 0, function () {
922
958
  var fee, result;
923
959
  return __generator(this, function (_g) {
@@ -926,10 +962,18 @@ var Client = /** @class */ (function () {
926
962
  registerCodecs(getPrefix(this.network));
927
963
  return [4 /*yield*/, this.getFees()];
928
964
  case 1:
929
- fee = _g.sent();
930
- if (from_balance === baseAmount('0', DECIMAL) ||
931
- from_balance.amount().lt(amount.amount().plus(fee[FeeOption.Average].amount()))) {
932
- throw new Error('insufficient funds');
965
+ fee = (_g.sent()).average;
966
+ if (isAssetRuneNative(asset)) {
967
+ // amount + fee < runeBalance
968
+ if (from_rune_balance.lt(amount.plus(fee))) {
969
+ throw new Error('insufficient funds');
970
+ }
971
+ }
972
+ else {
973
+ // amount < assetBalances && runeBalance < fee
974
+ if (from_asset_balance.lt(amount) || from_rune_balance.lt(fee)) {
975
+ throw new Error('insufficient funds');
976
+ }
933
977
  }
934
978
  return [4 /*yield*/, this.cosmosClient.transferSignedOffline({
935
979
  privkey: this.getPrivKey(walletIndex),
@@ -967,4 +1011,4 @@ var Client = /** @class */ (function () {
967
1011
  return Client;
968
1012
  }());
969
1013
 
970
- export { Client, DECIMAL, DEFAULT_GAS_VALUE, DEPOSIT_GAS_VALUE, MAX_TX_COUNT, MsgNativeTx, buildDepositTx, getAsset, getBalance, getChainId, getDefaultClientUrl, getDefaultExplorerUrls, getDefaultFees, getDenom, getDenomWithChain, getDepositTxDataFromLogs, getExplorerAddressUrl, getExplorerTxUrl, getExplorerUrl, getPrefix, getTxType, isBroadcastSuccess, isMsgMultiSend, isMsgSend, msgNativeTxFromJson, registerCodecs };
1014
+ export { 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, isMsgMultiSend, isMsgSend, msgNativeTxFromJson, registerCodecs };
package/lib/index.js CHANGED
@@ -127,24 +127,25 @@ var DEFAULT_GAS_VALUE = '2000000';
127
127
  var DEPOSIT_GAS_VALUE = '500000000';
128
128
  var MAX_TX_COUNT = 100;
129
129
  /**
130
- * Get denomination from Asset
130
+ * Checks whether an asset is `AssetRuneNative`
131
131
  *
132
132
  * @param {Asset} asset
133
- * @returns {string} The denomination of the given asset.
133
+ * @returns {boolean} `true` or `false`
134
134
  */
135
- var getDenom = function (asset) {
136
- if (xchainUtil.assetToString(asset) === xchainUtil.assetToString(xchainUtil.AssetRuneNative))
137
- return 'rune';
138
- return asset.symbol;
139
- };
135
+ var isAssetRuneNative = function (asset) { return xchainUtil.assetToString(asset) === xchainUtil.assetToString(xchainUtil.AssetRuneNative); };
136
+ var DENOM_RUNE_NATIVE = 'rune';
140
137
  /**
141
- * Get denomination with chainname from Asset
138
+ * Get denomination from Asset
142
139
  *
143
140
  * @param {Asset} asset
144
- * @returns {string} The denomination with chainname of the given asset.
141
+ * @returns {string} The denomination of the given asset.
145
142
  */
146
- var getDenomWithChain = function (asset) {
147
- return xchainUtil.Chain.THORChain + "." + asset.symbol.toUpperCase();
143
+ var getDenom = function (asset) {
144
+ if (isAssetRuneNative(asset))
145
+ return DENOM_RUNE_NATIVE;
146
+ if (xchainUtil.isSynthAsset(asset))
147
+ return xchainUtil.assetToString(asset).toLowerCase();
148
+ return asset.symbol.toLowerCase();
148
149
  };
149
150
  /**
150
151
  * Get Asset from denomination
@@ -152,10 +153,10 @@ var getDenomWithChain = function (asset) {
152
153
  * @param {string} denom
153
154
  * @returns {Asset|null} The asset of the given denomination.
154
155
  */
155
- var getAsset = function (denom) {
156
- if (denom === getDenom(xchainUtil.AssetRuneNative))
156
+ var assetFromDenom = function (denom) {
157
+ if (denom === DENOM_RUNE_NATIVE)
157
158
  return xchainUtil.AssetRuneNative;
158
- return xchainUtil.assetFromString(xchainUtil.Chain.THORChain + "." + denom.toUpperCase());
159
+ return xchainUtil.assetFromString(denom.toUpperCase());
159
160
  };
160
161
  /**
161
162
  * Type guard for MsgSend
@@ -359,7 +360,7 @@ var getBalance = function (_a) {
359
360
  balances = _b.sent();
360
361
  return [2 /*return*/, balances
361
362
  .map(function (balance) { return ({
362
- asset: (balance.denom && getAsset(balance.denom)) || xchainUtil.AssetRuneNative,
363
+ asset: (balance.denom && assetFromDenom(balance.denom)) || xchainUtil.AssetRuneNative,
363
364
  amount: xchainUtil.baseAmount(balance.amount, DECIMAL),
364
365
  }); })
365
366
  .filter(function (balance) { return !assets || assets.filter(function (asset) { return xchainUtil.assetToString(balance.asset) === xchainUtil.assetToString(asset); }).length; })];
@@ -838,23 +839,41 @@ var Client = /** @class */ (function () {
838
839
  * @throws {"failed to broadcast transaction"} Thrown if failed to broadcast transaction.
839
840
  */
840
841
  Client.prototype.deposit = function (_a) {
841
- var _b, _c;
842
- var _d = _a.walletIndex, walletIndex = _d === void 0 ? 0 : _d, _e = _a.asset, asset = _e === void 0 ? xchainUtil.AssetRuneNative : _e, amount = _a.amount, memo = _a.memo;
842
+ var _b, _c, _d, _e, _f, _g;
843
+ var _h = _a.walletIndex, walletIndex = _h === void 0 ? 0 : _h, _j = _a.asset, asset = _j === void 0 ? xchainUtil.AssetRuneNative : _j, amount = _a.amount, memo = _a.memo;
843
844
  return __awaiter(this, void 0, void 0, function () {
844
- var assetBalance, signer, msgNativeTx, unsignedStdTx, privateKey, accAddress;
845
- return __generator(this, function (_f) {
846
- switch (_f.label) {
847
- case 0: return [4 /*yield*/, this.getBalance(this.getAddress(walletIndex), [asset])];
845
+ var balances, runeBalance, assetBalance, fee, signer, msgNativeTx, unsignedStdTx, privateKey, accAddress;
846
+ return __generator(this, function (_k) {
847
+ switch (_k.label) {
848
+ case 0: return [4 /*yield*/, this.getBalance(this.getAddress(walletIndex))];
848
849
  case 1:
849
- assetBalance = _f.sent();
850
- if (assetBalance.length === 0 || assetBalance[0].amount.amount().lt(amount.amount().plus(DEFAULT_GAS_VALUE))) {
851
- throw new Error('insufficient funds');
850
+ balances = _k.sent();
851
+ runeBalance = (_c = (_b = balances.filter(function (_a) {
852
+ var asset = _a.asset;
853
+ return isAssetRuneNative(asset);
854
+ })[0]) === null || _b === void 0 ? void 0 : _b.amount) !== null && _c !== void 0 ? _c : xchainUtil.baseAmount(0, DECIMAL);
855
+ assetBalance = (_e = (_d = balances.filter(function (_a) {
856
+ var assetInList = _a.asset;
857
+ return xchainUtil.assetToString(assetInList) === xchainUtil.assetToString(asset);
858
+ })[0]) === null || _d === void 0 ? void 0 : _d.amount) !== null && _e !== void 0 ? _e : xchainUtil.baseAmount(0, DECIMAL);
859
+ fee = xchainUtil.baseAmount(DEFAULT_GAS_VALUE, DECIMAL);
860
+ if (isAssetRuneNative(asset)) {
861
+ // amount + fee < runeBalance
862
+ if (runeBalance.lt(amount.plus(fee))) {
863
+ throw new Error('insufficient funds');
864
+ }
865
+ }
866
+ else {
867
+ // amount < assetBalances && runeBalance < fee
868
+ if (assetBalance.lt(amount) || runeBalance.lt(fee)) {
869
+ throw new Error('insufficient funds');
870
+ }
852
871
  }
853
872
  signer = this.getAddress(walletIndex);
854
873
  msgNativeTx = msgNativeTxFromJson({
855
874
  coins: [
856
875
  {
857
- asset: getDenomWithChain(asset),
876
+ asset: isAssetRuneNative(asset) ? xchainUtil.assetToString(xchainUtil.AssetRuneNative) : getDenom(asset),
858
877
  amount: amount.amount().toString(),
859
878
  },
860
879
  ],
@@ -863,11 +882,11 @@ var Client = /** @class */ (function () {
863
882
  });
864
883
  return [4 /*yield*/, buildDepositTx(msgNativeTx, this.getClientUrl().node)];
865
884
  case 2:
866
- unsignedStdTx = _f.sent();
885
+ unsignedStdTx = _k.sent();
867
886
  privateKey = this.getPrivKey(walletIndex);
868
887
  accAddress = cosmosClient.AccAddress.fromBech32(signer);
869
888
  return [4 /*yield*/, this.cosmosClient.signAndBroadcast(unsignedStdTx, privateKey, accAddress)];
870
- case 3: return [2 /*return*/, (_c = (_b = (_f.sent())) === null || _b === void 0 ? void 0 : _b.txhash) !== null && _c !== void 0 ? _c : ''];
889
+ case 3: return [2 /*return*/, (_g = (_f = (_k.sent())) === null || _f === void 0 ? void 0 : _f.txhash) !== null && _g !== void 0 ? _g : ''];
871
890
  }
872
891
  });
873
892
  });
@@ -879,22 +898,39 @@ var Client = /** @class */ (function () {
879
898
  * @returns {TxHash} The transaction hash.
880
899
  */
881
900
  Client.prototype.transfer = function (_a) {
882
- var _b = _a.walletIndex, walletIndex = _b === void 0 ? 0 : _b, _c = _a.asset, asset = _c === void 0 ? xchainUtil.AssetRuneNative : _c, amount = _a.amount, recipient = _a.recipient, memo = _a.memo;
901
+ var _b, _c, _d, _e;
902
+ var _f = _a.walletIndex, walletIndex = _f === void 0 ? 0 : _f, _g = _a.asset, asset = _g === void 0 ? xchainUtil.AssetRuneNative : _g, amount = _a.amount, recipient = _a.recipient, memo = _a.memo;
883
903
  return __awaiter(this, void 0, void 0, function () {
884
- var assetBalance, fee, transferResult;
885
- return __generator(this, function (_d) {
886
- switch (_d.label) {
904
+ var balances, runeBalance, assetBalance, fee, transferResult;
905
+ return __generator(this, function (_h) {
906
+ switch (_h.label) {
887
907
  case 0:
888
908
  registerCodecs(getPrefix(this.network));
889
- return [4 /*yield*/, this.getBalance(this.getAddress(walletIndex), [asset])];
909
+ return [4 /*yield*/, this.getBalance(this.getAddress(walletIndex))];
890
910
  case 1:
891
- assetBalance = _d.sent();
911
+ balances = _h.sent();
912
+ runeBalance = (_c = (_b = balances.filter(function (_a) {
913
+ var asset = _a.asset;
914
+ return isAssetRuneNative(asset);
915
+ })[0]) === null || _b === void 0 ? void 0 : _b.amount) !== null && _c !== void 0 ? _c : xchainUtil.baseAmount(0, DECIMAL);
916
+ assetBalance = (_e = (_d = balances.filter(function (_a) {
917
+ var assetInList = _a.asset;
918
+ return xchainUtil.assetToString(assetInList) === xchainUtil.assetToString(asset);
919
+ })[0]) === null || _d === void 0 ? void 0 : _d.amount) !== null && _e !== void 0 ? _e : xchainUtil.baseAmount(0, DECIMAL);
892
920
  return [4 /*yield*/, this.getFees()];
893
921
  case 2:
894
- fee = _d.sent();
895
- if (assetBalance.length === 0 ||
896
- assetBalance[0].amount.amount().lt(amount.amount().plus(fee[xchainClient.FeeOption.Average].amount()))) {
897
- throw new Error('insufficient funds');
922
+ fee = (_h.sent()).average;
923
+ if (isAssetRuneNative(asset)) {
924
+ // amount + fee < runeBalance
925
+ if (runeBalance.lt(amount.plus(fee))) {
926
+ throw new Error('insufficient funds');
927
+ }
928
+ }
929
+ else {
930
+ // amount < assetBalances && runeBalance < fee
931
+ if (assetBalance.lt(amount) || runeBalance.lt(fee)) {
932
+ throw new Error('insufficient funds');
933
+ }
898
934
  }
899
935
  return [4 /*yield*/, this.cosmosClient.transfer({
900
936
  privkey: this.getPrivKey(walletIndex),
@@ -909,7 +945,7 @@ var Client = /** @class */ (function () {
909
945
  },
910
946
  })];
911
947
  case 3:
912
- transferResult = _d.sent();
948
+ transferResult = _h.sent();
913
949
  if (!isBroadcastSuccess(transferResult)) {
914
950
  throw new Error("failed to broadcast transaction: " + transferResult.txhash);
915
951
  }
@@ -925,7 +961,7 @@ var Client = /** @class */ (function () {
925
961
  * @returns {StdTx} The signed transaction.
926
962
  */
927
963
  Client.prototype.transferOffline = function (_a) {
928
- var _b = _a.walletIndex, walletIndex = _b === void 0 ? 0 : _b, _c = _a.asset, asset = _c === void 0 ? xchainUtil.AssetRuneNative : _c, amount = _a.amount, recipient = _a.recipient, memo = _a.memo, _d = _a.from_balance, from_balance = _d === void 0 ? xchainUtil.baseAmount('0', DECIMAL) : _d, _e = _a.from_account_number, from_account_number = _e === void 0 ? '0' : _e, _f = _a.from_sequence, from_sequence = _f === void 0 ? '0' : _f;
964
+ var _b = _a.walletIndex, walletIndex = _b === void 0 ? 0 : _b, _c = _a.asset, asset = _c === void 0 ? xchainUtil.AssetRuneNative : _c, amount = _a.amount, recipient = _a.recipient, memo = _a.memo, from_rune_balance = _a.from_rune_balance, _d = _a.from_asset_balance, from_asset_balance = _d === void 0 ? xchainUtil.baseAmount(0, DECIMAL) : _d, _e = _a.from_account_number, from_account_number = _e === void 0 ? '0' : _e, _f = _a.from_sequence, from_sequence = _f === void 0 ? '0' : _f;
929
965
  return __awaiter(this, void 0, void 0, function () {
930
966
  var fee, result;
931
967
  return __generator(this, function (_g) {
@@ -934,10 +970,18 @@ var Client = /** @class */ (function () {
934
970
  registerCodecs(getPrefix(this.network));
935
971
  return [4 /*yield*/, this.getFees()];
936
972
  case 1:
937
- fee = _g.sent();
938
- if (from_balance === xchainUtil.baseAmount('0', DECIMAL) ||
939
- from_balance.amount().lt(amount.amount().plus(fee[xchainClient.FeeOption.Average].amount()))) {
940
- throw new Error('insufficient funds');
973
+ fee = (_g.sent()).average;
974
+ if (isAssetRuneNative(asset)) {
975
+ // amount + fee < runeBalance
976
+ if (from_rune_balance.lt(amount.plus(fee))) {
977
+ throw new Error('insufficient funds');
978
+ }
979
+ }
980
+ else {
981
+ // amount < assetBalances && runeBalance < fee
982
+ if (from_asset_balance.lt(amount) || from_rune_balance.lt(fee)) {
983
+ throw new Error('insufficient funds');
984
+ }
941
985
  }
942
986
  return [4 /*yield*/, this.cosmosClient.transferSignedOffline({
943
987
  privkey: this.getPrivKey(walletIndex),
@@ -981,21 +1025,21 @@ exports.DEFAULT_GAS_VALUE = DEFAULT_GAS_VALUE;
981
1025
  exports.DEPOSIT_GAS_VALUE = DEPOSIT_GAS_VALUE;
982
1026
  exports.MAX_TX_COUNT = MAX_TX_COUNT;
983
1027
  exports.MsgNativeTx = MsgNativeTx;
1028
+ exports.assetFromDenom = assetFromDenom;
984
1029
  exports.buildDepositTx = buildDepositTx;
985
- exports.getAsset = getAsset;
986
1030
  exports.getBalance = getBalance;
987
1031
  exports.getChainId = getChainId;
988
1032
  exports.getDefaultClientUrl = getDefaultClientUrl;
989
1033
  exports.getDefaultExplorerUrls = getDefaultExplorerUrls;
990
1034
  exports.getDefaultFees = getDefaultFees;
991
1035
  exports.getDenom = getDenom;
992
- exports.getDenomWithChain = getDenomWithChain;
993
1036
  exports.getDepositTxDataFromLogs = getDepositTxDataFromLogs;
994
1037
  exports.getExplorerAddressUrl = getExplorerAddressUrl;
995
1038
  exports.getExplorerTxUrl = getExplorerTxUrl;
996
1039
  exports.getExplorerUrl = getExplorerUrl;
997
1040
  exports.getPrefix = getPrefix;
998
1041
  exports.getTxType = getTxType;
1042
+ exports.isAssetRuneNative = isAssetRuneNative;
999
1043
  exports.isBroadcastSuccess = isBroadcastSuccess;
1000
1044
  exports.isMsgMultiSend = isMsgMultiSend;
1001
1045
  exports.isMsgSend = isMsgSend;
@@ -1,4 +1,4 @@
1
- import { Network, Tx } from '@xchainjs/xchain-client';
1
+ import { Network, Tx, TxParams } from '@xchainjs/xchain-client';
2
2
  import { Asset, BaseAmount } from '@xchainjs/xchain-util';
3
3
  export declare type NodeUrl = {
4
4
  node: string;
@@ -22,3 +22,16 @@ export declare type DepositParam = {
22
22
  memo: string;
23
23
  };
24
24
  export declare type TxData = Pick<Tx, 'from' | 'to' | 'type'>;
25
+ export declare type TxOfflineParams = TxParams & {
26
+ /**
27
+ * Balance of Rune to send from
28
+ */
29
+ from_rune_balance: BaseAmount;
30
+ /**
31
+ * Balance of asset to send from
32
+ * Optional: It can be ignored if asset to send from is RUNE
33
+ */
34
+ from_asset_balance?: BaseAmount;
35
+ from_account_number: string;
36
+ from_sequence: string;
37
+ };
package/lib/util.d.ts CHANGED
@@ -11,26 +11,26 @@ export declare const DEFAULT_GAS_VALUE = "2000000";
11
11
  export declare const DEPOSIT_GAS_VALUE = "500000000";
12
12
  export declare const MAX_TX_COUNT = 100;
13
13
  /**
14
- * Get denomination from Asset
14
+ * Checks whether an asset is `AssetRuneNative`
15
15
  *
16
16
  * @param {Asset} asset
17
- * @returns {string} The denomination of the given asset.
17
+ * @returns {boolean} `true` or `false`
18
18
  */
19
- export declare const getDenom: (asset: Asset) => string;
19
+ export declare const isAssetRuneNative: (asset: Asset) => boolean;
20
20
  /**
21
- * Get denomination with chainname from Asset
21
+ * Get denomination from Asset
22
22
  *
23
23
  * @param {Asset} asset
24
- * @returns {string} The denomination with chainname of the given asset.
24
+ * @returns {string} The denomination of the given asset.
25
25
  */
26
- export declare const getDenomWithChain: (asset: Asset) => string;
26
+ export declare const getDenom: (asset: Asset) => string;
27
27
  /**
28
28
  * Get Asset from denomination
29
29
  *
30
30
  * @param {string} denom
31
31
  * @returns {Asset|null} The asset of the given denomination.
32
32
  */
33
- export declare const getAsset: (denom: string) => Asset | null;
33
+ export declare const assetFromDenom: (denom: string) => Asset | null;
34
34
  /**
35
35
  * Type guard for MsgSend
36
36
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xchainjs/xchain-thorchain",
3
- "version": "0.20.1",
3
+ "version": "0.21.0",
4
4
  "description": "Custom Thorchain client and utilities used by XChainJS clients",
5
5
  "keywords": [
6
6
  "THORChain",
@@ -34,9 +34,9 @@
34
34
  "devDependencies": {
35
35
  "@types/big.js": "^6.0.0",
36
36
  "@xchainjs/xchain-client": "^0.11.0",
37
- "@xchainjs/xchain-cosmos": "^0.15.0",
37
+ "@xchainjs/xchain-cosmos": "^0.16.0",
38
38
  "@xchainjs/xchain-crypto": "^0.2.4",
39
- "@xchainjs/xchain-util": "^0.3.0",
39
+ "@xchainjs/xchain-util": "^0.5.0",
40
40
  "axios": "^0.21.0",
41
41
  "cosmos-client": "0.39.2",
42
42
  "nock": "^13.0.5"
@@ -46,10 +46,10 @@
46
46
  },
47
47
  "peerDependencies": {
48
48
  "@xchainjs/xchain-client": "^0.11.0",
49
- "@xchainjs/xchain-cosmos": "^0.15.0",
49
+ "@xchainjs/xchain-cosmos": "^0.16.0",
50
50
  "@xchainjs/xchain-crypto": "^0.2.4",
51
- "@xchainjs/xchain-util": "^0.3.0",
51
+ "@xchainjs/xchain-util": "^0.5.0",
52
52
  "axios": "^0.21.0",
53
53
  "cosmos-client": "0.39.2"
54
54
  }
55
- }
55
+ }