@xchainjs/xchain-thorchain 0.19.5 → 0.21.1
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 +44 -0
- package/lib/client.d.ts +3 -3
- package/lib/index.esm.js +146 -58
- package/lib/index.js +145 -57
- package/lib/types/client-types.d.ts +14 -1
- package/lib/util.d.ts +23 -9
- package/package.json +7 -7
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,47 @@
|
|
|
1
|
+
# v.0.21.1 (2022-02-04)
|
|
2
|
+
|
|
3
|
+
## Fix
|
|
4
|
+
|
|
5
|
+
- Fix chain id for `testnet` #477
|
|
6
|
+
|
|
7
|
+
## Add
|
|
8
|
+
|
|
9
|
+
- Helper `isChainId`
|
|
10
|
+
- `enum ChainId`
|
|
11
|
+
|
|
12
|
+
# v.0.21.0 (2022-02-02)
|
|
13
|
+
|
|
14
|
+
## Breaking change
|
|
15
|
+
|
|
16
|
+
- Remove `getDenomWithChain`
|
|
17
|
+
- Rename `getAsset` -> `assetFromDenom` (incl. fix to get synth asset properly)
|
|
18
|
+
|
|
19
|
+
## Update
|
|
20
|
+
|
|
21
|
+
- xchain-util@0.5.0
|
|
22
|
+
- xchain-cosmos@0.16.0
|
|
23
|
+
|
|
24
|
+
## Add
|
|
25
|
+
|
|
26
|
+
- `isAssetNativeRune` helper
|
|
27
|
+
- Add `TxOfflineParams` type
|
|
28
|
+
|
|
29
|
+
## Fix
|
|
30
|
+
|
|
31
|
+
- Fix synth notation in `transfer|transferOffline|deposit` #473
|
|
32
|
+
|
|
33
|
+
# v0.20.1 (2022-01-11)
|
|
34
|
+
|
|
35
|
+
## Fix
|
|
36
|
+
|
|
37
|
+
- Get chain ID from THORNode before posting to deposit handler.
|
|
38
|
+
|
|
39
|
+
# v.0.20.0 (2021-12-29)
|
|
40
|
+
|
|
41
|
+
## Breaking change
|
|
42
|
+
|
|
43
|
+
- Add stagenet environment handling for `Network` and `BaseXChainClient` to client
|
|
44
|
+
|
|
1
45
|
# v.0.19.5 (2021-11-22)
|
|
2
46
|
|
|
3
47
|
## Add
|
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
|
|
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,
|
|
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
|
|
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,
|
|
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
|
-
*
|
|
122
|
+
* Checks whether an asset is `AssetRuneNative`
|
|
123
123
|
*
|
|
124
124
|
* @param {Asset} asset
|
|
125
|
-
* @returns {
|
|
125
|
+
* @returns {boolean} `true` or `false`
|
|
126
126
|
*/
|
|
127
|
-
var
|
|
128
|
-
|
|
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
|
|
130
|
+
* Get denomination from Asset
|
|
134
131
|
*
|
|
135
132
|
* @param {Asset} asset
|
|
136
|
-
* @returns {string} The denomination
|
|
133
|
+
* @returns {string} The denomination of the given asset.
|
|
137
134
|
*/
|
|
138
|
-
var
|
|
139
|
-
|
|
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
|
|
148
|
-
if (denom ===
|
|
148
|
+
var assetFromDenom = function (denom) {
|
|
149
|
+
if (denom === DENOM_RUNE_NATIVE)
|
|
149
150
|
return AssetRuneNative;
|
|
150
|
-
return assetFromString(
|
|
151
|
+
return assetFromString(denom.toUpperCase());
|
|
151
152
|
};
|
|
152
153
|
/**
|
|
153
154
|
* Type guard for MsgSend
|
|
@@ -191,16 +192,42 @@ var getPrefix = function (network) {
|
|
|
191
192
|
switch (network) {
|
|
192
193
|
case Network.Mainnet:
|
|
193
194
|
return 'thor';
|
|
195
|
+
case Network.Stagenet:
|
|
196
|
+
return 'sthor';
|
|
194
197
|
case Network.Testnet:
|
|
195
198
|
return 'tthor';
|
|
196
199
|
}
|
|
197
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); };
|
|
198
214
|
/**
|
|
199
215
|
* Get the chain id.
|
|
200
216
|
*
|
|
217
|
+
* @param {Network} network
|
|
201
218
|
* @returns {string} The chain id based on the network.
|
|
219
|
+
*
|
|
202
220
|
*/
|
|
203
|
-
var getChainId = function () {
|
|
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
|
+
};
|
|
204
231
|
/**
|
|
205
232
|
* Register Codecs based on the prefix.
|
|
206
233
|
*
|
|
@@ -284,19 +311,25 @@ var getTxType = function (txData, encoding) {
|
|
|
284
311
|
* @throws {"Invalid client url"} Thrown if the client url is an invalid one.
|
|
285
312
|
*/
|
|
286
313
|
var buildDepositTx = function (msgNativeTx, nodeUrl) { return __awaiter(void 0, void 0, void 0, function () {
|
|
287
|
-
var response, fee, unsignedStdTx;
|
|
314
|
+
var data, chainId, response, fee, unsignedStdTx;
|
|
288
315
|
var _a, _b;
|
|
289
316
|
return __generator(this, function (_c) {
|
|
290
317
|
switch (_c.label) {
|
|
291
|
-
case 0: return [4 /*yield*/, axios.
|
|
292
|
-
coins: msgNativeTx.coins,
|
|
293
|
-
memo: msgNativeTx.memo,
|
|
294
|
-
base_req: {
|
|
295
|
-
chain_id: getChainId(),
|
|
296
|
-
from: msgNativeTx.signer,
|
|
297
|
-
},
|
|
298
|
-
})];
|
|
318
|
+
case 0: return [4 /*yield*/, axios.get(nodeUrl + "/cosmos/base/tendermint/v1beta1/node_info")];
|
|
299
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:
|
|
300
333
|
response = (_c.sent()).data;
|
|
301
334
|
if (!response || !response.value)
|
|
302
335
|
throw new Error('Invalid client url');
|
|
@@ -332,7 +365,7 @@ var getBalance = function (_a) {
|
|
|
332
365
|
balances = _b.sent();
|
|
333
366
|
return [2 /*return*/, balances
|
|
334
367
|
.map(function (balance) { return ({
|
|
335
|
-
asset: (balance.denom &&
|
|
368
|
+
asset: (balance.denom && assetFromDenom(balance.denom)) || AssetRuneNative,
|
|
336
369
|
amount: baseAmount(balance.amount, DECIMAL),
|
|
337
370
|
}); })
|
|
338
371
|
.filter(function (balance) { return !assets || assets.filter(function (asset) { return assetToString(balance.asset) === assetToString(asset); }).length; })];
|
|
@@ -352,8 +385,12 @@ var getDefaultClientUrl = function () {
|
|
|
352
385
|
node: 'https://testnet.thornode.thorchain.info',
|
|
353
386
|
rpc: 'https://testnet.rpc.thorchain.info',
|
|
354
387
|
},
|
|
388
|
+
_a[Network.Stagenet] = {
|
|
389
|
+
node: 'https://stagenet-thornode.ninerealms.com',
|
|
390
|
+
rpc: 'https://stagenet-rpc.ninerealms.com',
|
|
391
|
+
},
|
|
355
392
|
_a[Network.Mainnet] = {
|
|
356
|
-
node: 'https://thornode.
|
|
393
|
+
node: 'https://thornode.ninerealms.com',
|
|
357
394
|
rpc: 'https://rpc.thorchain.info',
|
|
358
395
|
},
|
|
359
396
|
_a;
|
|
@@ -368,16 +405,19 @@ var getDefaultExplorerUrls = function () {
|
|
|
368
405
|
var _a, _b, _c;
|
|
369
406
|
var root = (_a = {},
|
|
370
407
|
_a[Network.Testnet] = DEFAULT_EXPLORER_URL + "?network=testnet",
|
|
408
|
+
_a[Network.Stagenet] = DEFAULT_EXPLORER_URL + "?network=stagenet",
|
|
371
409
|
_a[Network.Mainnet] = DEFAULT_EXPLORER_URL,
|
|
372
410
|
_a);
|
|
373
411
|
var txUrl = DEFAULT_EXPLORER_URL + "/tx";
|
|
374
412
|
var tx = (_b = {},
|
|
375
413
|
_b[Network.Testnet] = txUrl,
|
|
414
|
+
_b[Network.Stagenet] = txUrl,
|
|
376
415
|
_b[Network.Mainnet] = txUrl,
|
|
377
416
|
_b);
|
|
378
417
|
var addressUrl = DEFAULT_EXPLORER_URL + "/address";
|
|
379
418
|
var address = (_c = {},
|
|
380
419
|
_c[Network.Testnet] = addressUrl,
|
|
420
|
+
_c[Network.Stagenet] = addressUrl,
|
|
381
421
|
_c[Network.Mainnet] = addressUrl,
|
|
382
422
|
_c);
|
|
383
423
|
return {
|
|
@@ -411,6 +451,8 @@ var getExplorerAddressUrl = function (_a) {
|
|
|
411
451
|
switch (network) {
|
|
412
452
|
case Network.Mainnet:
|
|
413
453
|
return url;
|
|
454
|
+
case Network.Stagenet:
|
|
455
|
+
return url + "?network=stagenet";
|
|
414
456
|
case Network.Testnet:
|
|
415
457
|
return url + "?network=testnet";
|
|
416
458
|
}
|
|
@@ -429,6 +471,8 @@ var getExplorerTxUrl = function (_a) {
|
|
|
429
471
|
switch (network) {
|
|
430
472
|
case Network.Mainnet:
|
|
431
473
|
return url;
|
|
474
|
+
case Network.Stagenet:
|
|
475
|
+
return url + "?network=stagenet";
|
|
432
476
|
case Network.Testnet:
|
|
433
477
|
return url + "?network=testnet";
|
|
434
478
|
}
|
|
@@ -453,6 +497,7 @@ var Client = /** @class */ (function () {
|
|
|
453
497
|
var _this = this;
|
|
454
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 = {},
|
|
455
499
|
_b[Network.Mainnet] = "44'/931'/0'/0/",
|
|
500
|
+
_b[Network.Stagenet] = "44'/931'/0'/0/",
|
|
456
501
|
_b[Network.Testnet] = "44'/931'/0'/0/",
|
|
457
502
|
_b) : _d;
|
|
458
503
|
this.phrase = '';
|
|
@@ -527,7 +572,7 @@ var Client = /** @class */ (function () {
|
|
|
527
572
|
this.rootDerivationPaths = rootDerivationPaths;
|
|
528
573
|
this.cosmosClient = new CosmosSDKClient({
|
|
529
574
|
server: this.getClientUrl().node,
|
|
530
|
-
chainId: getChainId(),
|
|
575
|
+
chainId: getChainId(this.network),
|
|
531
576
|
prefix: getPrefix(this.network),
|
|
532
577
|
});
|
|
533
578
|
if (phrase)
|
|
@@ -799,23 +844,41 @@ var Client = /** @class */ (function () {
|
|
|
799
844
|
* @throws {"failed to broadcast transaction"} Thrown if failed to broadcast transaction.
|
|
800
845
|
*/
|
|
801
846
|
Client.prototype.deposit = function (_a) {
|
|
802
|
-
var _b, _c;
|
|
803
|
-
var
|
|
847
|
+
var _b, _c, _d, _e, _f, _g;
|
|
848
|
+
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;
|
|
804
849
|
return __awaiter(this, void 0, void 0, function () {
|
|
805
|
-
var assetBalance, signer, msgNativeTx, unsignedStdTx, privateKey, accAddress;
|
|
806
|
-
return __generator(this, function (
|
|
807
|
-
switch (
|
|
808
|
-
case 0: return [4 /*yield*/, this.getBalance(this.getAddress(walletIndex)
|
|
850
|
+
var balances, runeBalance, assetBalance, fee, signer, msgNativeTx, unsignedStdTx, privateKey, accAddress;
|
|
851
|
+
return __generator(this, function (_k) {
|
|
852
|
+
switch (_k.label) {
|
|
853
|
+
case 0: return [4 /*yield*/, this.getBalance(this.getAddress(walletIndex))];
|
|
809
854
|
case 1:
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
855
|
+
balances = _k.sent();
|
|
856
|
+
runeBalance = (_c = (_b = balances.filter(function (_a) {
|
|
857
|
+
var asset = _a.asset;
|
|
858
|
+
return isAssetRuneNative(asset);
|
|
859
|
+
})[0]) === null || _b === void 0 ? void 0 : _b.amount) !== null && _c !== void 0 ? _c : baseAmount(0, DECIMAL);
|
|
860
|
+
assetBalance = (_e = (_d = balances.filter(function (_a) {
|
|
861
|
+
var assetInList = _a.asset;
|
|
862
|
+
return assetToString(assetInList) === assetToString(asset);
|
|
863
|
+
})[0]) === null || _d === void 0 ? void 0 : _d.amount) !== null && _e !== void 0 ? _e : baseAmount(0, DECIMAL);
|
|
864
|
+
fee = baseAmount(DEFAULT_GAS_VALUE, DECIMAL);
|
|
865
|
+
if (isAssetRuneNative(asset)) {
|
|
866
|
+
// amount + fee < runeBalance
|
|
867
|
+
if (runeBalance.lt(amount.plus(fee))) {
|
|
868
|
+
throw new Error('insufficient funds');
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
else {
|
|
872
|
+
// amount < assetBalances && runeBalance < fee
|
|
873
|
+
if (assetBalance.lt(amount) || runeBalance.lt(fee)) {
|
|
874
|
+
throw new Error('insufficient funds');
|
|
875
|
+
}
|
|
813
876
|
}
|
|
814
877
|
signer = this.getAddress(walletIndex);
|
|
815
878
|
msgNativeTx = msgNativeTxFromJson({
|
|
816
879
|
coins: [
|
|
817
880
|
{
|
|
818
|
-
asset:
|
|
881
|
+
asset: isAssetRuneNative(asset) ? assetToString(AssetRuneNative) : getDenom(asset),
|
|
819
882
|
amount: amount.amount().toString(),
|
|
820
883
|
},
|
|
821
884
|
],
|
|
@@ -824,11 +887,11 @@ var Client = /** @class */ (function () {
|
|
|
824
887
|
});
|
|
825
888
|
return [4 /*yield*/, buildDepositTx(msgNativeTx, this.getClientUrl().node)];
|
|
826
889
|
case 2:
|
|
827
|
-
unsignedStdTx =
|
|
890
|
+
unsignedStdTx = _k.sent();
|
|
828
891
|
privateKey = this.getPrivKey(walletIndex);
|
|
829
892
|
accAddress = AccAddress.fromBech32(signer);
|
|
830
893
|
return [4 /*yield*/, this.cosmosClient.signAndBroadcast(unsignedStdTx, privateKey, accAddress)];
|
|
831
|
-
case 3: return [2 /*return*/, (
|
|
894
|
+
case 3: return [2 /*return*/, (_g = (_f = (_k.sent())) === null || _f === void 0 ? void 0 : _f.txhash) !== null && _g !== void 0 ? _g : ''];
|
|
832
895
|
}
|
|
833
896
|
});
|
|
834
897
|
});
|
|
@@ -840,22 +903,39 @@ var Client = /** @class */ (function () {
|
|
|
840
903
|
* @returns {TxHash} The transaction hash.
|
|
841
904
|
*/
|
|
842
905
|
Client.prototype.transfer = function (_a) {
|
|
843
|
-
var _b
|
|
906
|
+
var _b, _c, _d, _e;
|
|
907
|
+
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;
|
|
844
908
|
return __awaiter(this, void 0, void 0, function () {
|
|
845
|
-
var assetBalance, fee, transferResult;
|
|
846
|
-
return __generator(this, function (
|
|
847
|
-
switch (
|
|
909
|
+
var balances, runeBalance, assetBalance, fee, transferResult;
|
|
910
|
+
return __generator(this, function (_h) {
|
|
911
|
+
switch (_h.label) {
|
|
848
912
|
case 0:
|
|
849
913
|
registerCodecs(getPrefix(this.network));
|
|
850
|
-
return [4 /*yield*/, this.getBalance(this.getAddress(walletIndex)
|
|
914
|
+
return [4 /*yield*/, this.getBalance(this.getAddress(walletIndex))];
|
|
851
915
|
case 1:
|
|
852
|
-
|
|
916
|
+
balances = _h.sent();
|
|
917
|
+
runeBalance = (_c = (_b = balances.filter(function (_a) {
|
|
918
|
+
var asset = _a.asset;
|
|
919
|
+
return isAssetRuneNative(asset);
|
|
920
|
+
})[0]) === null || _b === void 0 ? void 0 : _b.amount) !== null && _c !== void 0 ? _c : baseAmount(0, DECIMAL);
|
|
921
|
+
assetBalance = (_e = (_d = balances.filter(function (_a) {
|
|
922
|
+
var assetInList = _a.asset;
|
|
923
|
+
return assetToString(assetInList) === assetToString(asset);
|
|
924
|
+
})[0]) === null || _d === void 0 ? void 0 : _d.amount) !== null && _e !== void 0 ? _e : baseAmount(0, DECIMAL);
|
|
853
925
|
return [4 /*yield*/, this.getFees()];
|
|
854
926
|
case 2:
|
|
855
|
-
fee =
|
|
856
|
-
if (
|
|
857
|
-
|
|
858
|
-
|
|
927
|
+
fee = (_h.sent()).average;
|
|
928
|
+
if (isAssetRuneNative(asset)) {
|
|
929
|
+
// amount + fee < runeBalance
|
|
930
|
+
if (runeBalance.lt(amount.plus(fee))) {
|
|
931
|
+
throw new Error('insufficient funds');
|
|
932
|
+
}
|
|
933
|
+
}
|
|
934
|
+
else {
|
|
935
|
+
// amount < assetBalances && runeBalance < fee
|
|
936
|
+
if (assetBalance.lt(amount) || runeBalance.lt(fee)) {
|
|
937
|
+
throw new Error('insufficient funds');
|
|
938
|
+
}
|
|
859
939
|
}
|
|
860
940
|
return [4 /*yield*/, this.cosmosClient.transfer({
|
|
861
941
|
privkey: this.getPrivKey(walletIndex),
|
|
@@ -870,7 +950,7 @@ var Client = /** @class */ (function () {
|
|
|
870
950
|
},
|
|
871
951
|
})];
|
|
872
952
|
case 3:
|
|
873
|
-
transferResult =
|
|
953
|
+
transferResult = _h.sent();
|
|
874
954
|
if (!isBroadcastSuccess(transferResult)) {
|
|
875
955
|
throw new Error("failed to broadcast transaction: " + transferResult.txhash);
|
|
876
956
|
}
|
|
@@ -886,7 +966,7 @@ var Client = /** @class */ (function () {
|
|
|
886
966
|
* @returns {StdTx} The signed transaction.
|
|
887
967
|
*/
|
|
888
968
|
Client.prototype.transferOffline = function (_a) {
|
|
889
|
-
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.
|
|
969
|
+
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;
|
|
890
970
|
return __awaiter(this, void 0, void 0, function () {
|
|
891
971
|
var fee, result;
|
|
892
972
|
return __generator(this, function (_g) {
|
|
@@ -895,10 +975,18 @@ var Client = /** @class */ (function () {
|
|
|
895
975
|
registerCodecs(getPrefix(this.network));
|
|
896
976
|
return [4 /*yield*/, this.getFees()];
|
|
897
977
|
case 1:
|
|
898
|
-
fee = _g.sent();
|
|
899
|
-
if (
|
|
900
|
-
|
|
901
|
-
|
|
978
|
+
fee = (_g.sent()).average;
|
|
979
|
+
if (isAssetRuneNative(asset)) {
|
|
980
|
+
// amount + fee < runeBalance
|
|
981
|
+
if (from_rune_balance.lt(amount.plus(fee))) {
|
|
982
|
+
throw new Error('insufficient funds');
|
|
983
|
+
}
|
|
984
|
+
}
|
|
985
|
+
else {
|
|
986
|
+
// amount < assetBalances && runeBalance < fee
|
|
987
|
+
if (from_asset_balance.lt(amount) || from_rune_balance.lt(fee)) {
|
|
988
|
+
throw new Error('insufficient funds');
|
|
989
|
+
}
|
|
902
990
|
}
|
|
903
991
|
return [4 /*yield*/, this.cosmosClient.transferSignedOffline({
|
|
904
992
|
privkey: this.getPrivKey(walletIndex),
|
|
@@ -936,4 +1024,4 @@ var Client = /** @class */ (function () {
|
|
|
936
1024
|
return Client;
|
|
937
1025
|
}());
|
|
938
1026
|
|
|
939
|
-
export { Client, DECIMAL, DEFAULT_GAS_VALUE, DEPOSIT_GAS_VALUE, MAX_TX_COUNT, MsgNativeTx,
|
|
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 };
|
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
|
-
*
|
|
130
|
+
* Checks whether an asset is `AssetRuneNative`
|
|
131
131
|
*
|
|
132
132
|
* @param {Asset} asset
|
|
133
|
-
* @returns {
|
|
133
|
+
* @returns {boolean} `true` or `false`
|
|
134
134
|
*/
|
|
135
|
-
var
|
|
136
|
-
|
|
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
|
|
138
|
+
* Get denomination from Asset
|
|
142
139
|
*
|
|
143
140
|
* @param {Asset} asset
|
|
144
|
-
* @returns {string} The denomination
|
|
141
|
+
* @returns {string} The denomination of the given asset.
|
|
145
142
|
*/
|
|
146
|
-
var
|
|
147
|
-
|
|
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
|
|
156
|
-
if (denom ===
|
|
156
|
+
var assetFromDenom = function (denom) {
|
|
157
|
+
if (denom === DENOM_RUNE_NATIVE)
|
|
157
158
|
return xchainUtil.AssetRuneNative;
|
|
158
|
-
return xchainUtil.assetFromString(
|
|
159
|
+
return xchainUtil.assetFromString(denom.toUpperCase());
|
|
159
160
|
};
|
|
160
161
|
/**
|
|
161
162
|
* Type guard for MsgSend
|
|
@@ -199,16 +200,41 @@ var getPrefix = function (network) {
|
|
|
199
200
|
switch (network) {
|
|
200
201
|
case xchainClient.Network.Mainnet:
|
|
201
202
|
return 'thor';
|
|
203
|
+
case xchainClient.Network.Stagenet:
|
|
204
|
+
return 'sthor';
|
|
202
205
|
case xchainClient.Network.Testnet:
|
|
203
206
|
return 'tthor';
|
|
204
207
|
}
|
|
205
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); };
|
|
206
221
|
/**
|
|
207
222
|
* Get the chain id.
|
|
208
223
|
*
|
|
224
|
+
* @param {Network} network
|
|
209
225
|
* @returns {string} The chain id based on the network.
|
|
226
|
+
*
|
|
210
227
|
*/
|
|
211
|
-
var getChainId = function () {
|
|
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
|
+
};
|
|
212
238
|
/**
|
|
213
239
|
* Register Codecs based on the prefix.
|
|
214
240
|
*
|
|
@@ -292,19 +318,25 @@ var getTxType = function (txData, encoding) {
|
|
|
292
318
|
* @throws {"Invalid client url"} Thrown if the client url is an invalid one.
|
|
293
319
|
*/
|
|
294
320
|
var buildDepositTx = function (msgNativeTx, nodeUrl) { return __awaiter(void 0, void 0, void 0, function () {
|
|
295
|
-
var response, fee, unsignedStdTx;
|
|
321
|
+
var data, chainId, response, fee, unsignedStdTx;
|
|
296
322
|
var _a, _b;
|
|
297
323
|
return __generator(this, function (_c) {
|
|
298
324
|
switch (_c.label) {
|
|
299
|
-
case 0: return [4 /*yield*/, axios__default['default'].
|
|
300
|
-
coins: msgNativeTx.coins,
|
|
301
|
-
memo: msgNativeTx.memo,
|
|
302
|
-
base_req: {
|
|
303
|
-
chain_id: getChainId(),
|
|
304
|
-
from: msgNativeTx.signer,
|
|
305
|
-
},
|
|
306
|
-
})];
|
|
325
|
+
case 0: return [4 /*yield*/, axios__default['default'].get(nodeUrl + "/cosmos/base/tendermint/v1beta1/node_info")];
|
|
307
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:
|
|
308
340
|
response = (_c.sent()).data;
|
|
309
341
|
if (!response || !response.value)
|
|
310
342
|
throw new Error('Invalid client url');
|
|
@@ -340,7 +372,7 @@ var getBalance = function (_a) {
|
|
|
340
372
|
balances = _b.sent();
|
|
341
373
|
return [2 /*return*/, balances
|
|
342
374
|
.map(function (balance) { return ({
|
|
343
|
-
asset: (balance.denom &&
|
|
375
|
+
asset: (balance.denom && assetFromDenom(balance.denom)) || xchainUtil.AssetRuneNative,
|
|
344
376
|
amount: xchainUtil.baseAmount(balance.amount, DECIMAL),
|
|
345
377
|
}); })
|
|
346
378
|
.filter(function (balance) { return !assets || assets.filter(function (asset) { return xchainUtil.assetToString(balance.asset) === xchainUtil.assetToString(asset); }).length; })];
|
|
@@ -360,8 +392,12 @@ var getDefaultClientUrl = function () {
|
|
|
360
392
|
node: 'https://testnet.thornode.thorchain.info',
|
|
361
393
|
rpc: 'https://testnet.rpc.thorchain.info',
|
|
362
394
|
},
|
|
395
|
+
_a[xchainClient.Network.Stagenet] = {
|
|
396
|
+
node: 'https://stagenet-thornode.ninerealms.com',
|
|
397
|
+
rpc: 'https://stagenet-rpc.ninerealms.com',
|
|
398
|
+
},
|
|
363
399
|
_a[xchainClient.Network.Mainnet] = {
|
|
364
|
-
node: 'https://thornode.
|
|
400
|
+
node: 'https://thornode.ninerealms.com',
|
|
365
401
|
rpc: 'https://rpc.thorchain.info',
|
|
366
402
|
},
|
|
367
403
|
_a;
|
|
@@ -376,16 +412,19 @@ var getDefaultExplorerUrls = function () {
|
|
|
376
412
|
var _a, _b, _c;
|
|
377
413
|
var root = (_a = {},
|
|
378
414
|
_a[xchainClient.Network.Testnet] = DEFAULT_EXPLORER_URL + "?network=testnet",
|
|
415
|
+
_a[xchainClient.Network.Stagenet] = DEFAULT_EXPLORER_URL + "?network=stagenet",
|
|
379
416
|
_a[xchainClient.Network.Mainnet] = DEFAULT_EXPLORER_URL,
|
|
380
417
|
_a);
|
|
381
418
|
var txUrl = DEFAULT_EXPLORER_URL + "/tx";
|
|
382
419
|
var tx = (_b = {},
|
|
383
420
|
_b[xchainClient.Network.Testnet] = txUrl,
|
|
421
|
+
_b[xchainClient.Network.Stagenet] = txUrl,
|
|
384
422
|
_b[xchainClient.Network.Mainnet] = txUrl,
|
|
385
423
|
_b);
|
|
386
424
|
var addressUrl = DEFAULT_EXPLORER_URL + "/address";
|
|
387
425
|
var address = (_c = {},
|
|
388
426
|
_c[xchainClient.Network.Testnet] = addressUrl,
|
|
427
|
+
_c[xchainClient.Network.Stagenet] = addressUrl,
|
|
389
428
|
_c[xchainClient.Network.Mainnet] = addressUrl,
|
|
390
429
|
_c);
|
|
391
430
|
return {
|
|
@@ -419,6 +458,8 @@ var getExplorerAddressUrl = function (_a) {
|
|
|
419
458
|
switch (network) {
|
|
420
459
|
case xchainClient.Network.Mainnet:
|
|
421
460
|
return url;
|
|
461
|
+
case xchainClient.Network.Stagenet:
|
|
462
|
+
return url + "?network=stagenet";
|
|
422
463
|
case xchainClient.Network.Testnet:
|
|
423
464
|
return url + "?network=testnet";
|
|
424
465
|
}
|
|
@@ -437,6 +478,8 @@ var getExplorerTxUrl = function (_a) {
|
|
|
437
478
|
switch (network) {
|
|
438
479
|
case xchainClient.Network.Mainnet:
|
|
439
480
|
return url;
|
|
481
|
+
case xchainClient.Network.Stagenet:
|
|
482
|
+
return url + "?network=stagenet";
|
|
440
483
|
case xchainClient.Network.Testnet:
|
|
441
484
|
return url + "?network=testnet";
|
|
442
485
|
}
|
|
@@ -461,6 +504,7 @@ var Client = /** @class */ (function () {
|
|
|
461
504
|
var _this = this;
|
|
462
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 = {},
|
|
463
506
|
_b[xchainClient.Network.Mainnet] = "44'/931'/0'/0/",
|
|
507
|
+
_b[xchainClient.Network.Stagenet] = "44'/931'/0'/0/",
|
|
464
508
|
_b[xchainClient.Network.Testnet] = "44'/931'/0'/0/",
|
|
465
509
|
_b) : _d;
|
|
466
510
|
this.phrase = '';
|
|
@@ -535,7 +579,7 @@ var Client = /** @class */ (function () {
|
|
|
535
579
|
this.rootDerivationPaths = rootDerivationPaths;
|
|
536
580
|
this.cosmosClient = new xchainCosmos.CosmosSDKClient({
|
|
537
581
|
server: this.getClientUrl().node,
|
|
538
|
-
chainId: getChainId(),
|
|
582
|
+
chainId: getChainId(this.network),
|
|
539
583
|
prefix: getPrefix(this.network),
|
|
540
584
|
});
|
|
541
585
|
if (phrase)
|
|
@@ -807,23 +851,41 @@ var Client = /** @class */ (function () {
|
|
|
807
851
|
* @throws {"failed to broadcast transaction"} Thrown if failed to broadcast transaction.
|
|
808
852
|
*/
|
|
809
853
|
Client.prototype.deposit = function (_a) {
|
|
810
|
-
var _b, _c;
|
|
811
|
-
var
|
|
854
|
+
var _b, _c, _d, _e, _f, _g;
|
|
855
|
+
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;
|
|
812
856
|
return __awaiter(this, void 0, void 0, function () {
|
|
813
|
-
var assetBalance, signer, msgNativeTx, unsignedStdTx, privateKey, accAddress;
|
|
814
|
-
return __generator(this, function (
|
|
815
|
-
switch (
|
|
816
|
-
case 0: return [4 /*yield*/, this.getBalance(this.getAddress(walletIndex)
|
|
857
|
+
var balances, runeBalance, assetBalance, fee, signer, msgNativeTx, unsignedStdTx, privateKey, accAddress;
|
|
858
|
+
return __generator(this, function (_k) {
|
|
859
|
+
switch (_k.label) {
|
|
860
|
+
case 0: return [4 /*yield*/, this.getBalance(this.getAddress(walletIndex))];
|
|
817
861
|
case 1:
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
862
|
+
balances = _k.sent();
|
|
863
|
+
runeBalance = (_c = (_b = balances.filter(function (_a) {
|
|
864
|
+
var asset = _a.asset;
|
|
865
|
+
return isAssetRuneNative(asset);
|
|
866
|
+
})[0]) === null || _b === void 0 ? void 0 : _b.amount) !== null && _c !== void 0 ? _c : xchainUtil.baseAmount(0, DECIMAL);
|
|
867
|
+
assetBalance = (_e = (_d = balances.filter(function (_a) {
|
|
868
|
+
var assetInList = _a.asset;
|
|
869
|
+
return xchainUtil.assetToString(assetInList) === xchainUtil.assetToString(asset);
|
|
870
|
+
})[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);
|
|
872
|
+
if (isAssetRuneNative(asset)) {
|
|
873
|
+
// amount + fee < runeBalance
|
|
874
|
+
if (runeBalance.lt(amount.plus(fee))) {
|
|
875
|
+
throw new Error('insufficient funds');
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
else {
|
|
879
|
+
// amount < assetBalances && runeBalance < fee
|
|
880
|
+
if (assetBalance.lt(amount) || runeBalance.lt(fee)) {
|
|
881
|
+
throw new Error('insufficient funds');
|
|
882
|
+
}
|
|
821
883
|
}
|
|
822
884
|
signer = this.getAddress(walletIndex);
|
|
823
885
|
msgNativeTx = msgNativeTxFromJson({
|
|
824
886
|
coins: [
|
|
825
887
|
{
|
|
826
|
-
asset:
|
|
888
|
+
asset: isAssetRuneNative(asset) ? xchainUtil.assetToString(xchainUtil.AssetRuneNative) : getDenom(asset),
|
|
827
889
|
amount: amount.amount().toString(),
|
|
828
890
|
},
|
|
829
891
|
],
|
|
@@ -832,11 +894,11 @@ var Client = /** @class */ (function () {
|
|
|
832
894
|
});
|
|
833
895
|
return [4 /*yield*/, buildDepositTx(msgNativeTx, this.getClientUrl().node)];
|
|
834
896
|
case 2:
|
|
835
|
-
unsignedStdTx =
|
|
897
|
+
unsignedStdTx = _k.sent();
|
|
836
898
|
privateKey = this.getPrivKey(walletIndex);
|
|
837
899
|
accAddress = cosmosClient.AccAddress.fromBech32(signer);
|
|
838
900
|
return [4 /*yield*/, this.cosmosClient.signAndBroadcast(unsignedStdTx, privateKey, accAddress)];
|
|
839
|
-
case 3: return [2 /*return*/, (
|
|
901
|
+
case 3: return [2 /*return*/, (_g = (_f = (_k.sent())) === null || _f === void 0 ? void 0 : _f.txhash) !== null && _g !== void 0 ? _g : ''];
|
|
840
902
|
}
|
|
841
903
|
});
|
|
842
904
|
});
|
|
@@ -848,22 +910,39 @@ var Client = /** @class */ (function () {
|
|
|
848
910
|
* @returns {TxHash} The transaction hash.
|
|
849
911
|
*/
|
|
850
912
|
Client.prototype.transfer = function (_a) {
|
|
851
|
-
var _b
|
|
913
|
+
var _b, _c, _d, _e;
|
|
914
|
+
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;
|
|
852
915
|
return __awaiter(this, void 0, void 0, function () {
|
|
853
|
-
var assetBalance, fee, transferResult;
|
|
854
|
-
return __generator(this, function (
|
|
855
|
-
switch (
|
|
916
|
+
var balances, runeBalance, assetBalance, fee, transferResult;
|
|
917
|
+
return __generator(this, function (_h) {
|
|
918
|
+
switch (_h.label) {
|
|
856
919
|
case 0:
|
|
857
920
|
registerCodecs(getPrefix(this.network));
|
|
858
|
-
return [4 /*yield*/, this.getBalance(this.getAddress(walletIndex)
|
|
921
|
+
return [4 /*yield*/, this.getBalance(this.getAddress(walletIndex))];
|
|
859
922
|
case 1:
|
|
860
|
-
|
|
923
|
+
balances = _h.sent();
|
|
924
|
+
runeBalance = (_c = (_b = balances.filter(function (_a) {
|
|
925
|
+
var asset = _a.asset;
|
|
926
|
+
return isAssetRuneNative(asset);
|
|
927
|
+
})[0]) === null || _b === void 0 ? void 0 : _b.amount) !== null && _c !== void 0 ? _c : xchainUtil.baseAmount(0, DECIMAL);
|
|
928
|
+
assetBalance = (_e = (_d = balances.filter(function (_a) {
|
|
929
|
+
var assetInList = _a.asset;
|
|
930
|
+
return xchainUtil.assetToString(assetInList) === xchainUtil.assetToString(asset);
|
|
931
|
+
})[0]) === null || _d === void 0 ? void 0 : _d.amount) !== null && _e !== void 0 ? _e : xchainUtil.baseAmount(0, DECIMAL);
|
|
861
932
|
return [4 /*yield*/, this.getFees()];
|
|
862
933
|
case 2:
|
|
863
|
-
fee =
|
|
864
|
-
if (
|
|
865
|
-
|
|
866
|
-
|
|
934
|
+
fee = (_h.sent()).average;
|
|
935
|
+
if (isAssetRuneNative(asset)) {
|
|
936
|
+
// amount + fee < runeBalance
|
|
937
|
+
if (runeBalance.lt(amount.plus(fee))) {
|
|
938
|
+
throw new Error('insufficient funds');
|
|
939
|
+
}
|
|
940
|
+
}
|
|
941
|
+
else {
|
|
942
|
+
// amount < assetBalances && runeBalance < fee
|
|
943
|
+
if (assetBalance.lt(amount) || runeBalance.lt(fee)) {
|
|
944
|
+
throw new Error('insufficient funds');
|
|
945
|
+
}
|
|
867
946
|
}
|
|
868
947
|
return [4 /*yield*/, this.cosmosClient.transfer({
|
|
869
948
|
privkey: this.getPrivKey(walletIndex),
|
|
@@ -878,7 +957,7 @@ var Client = /** @class */ (function () {
|
|
|
878
957
|
},
|
|
879
958
|
})];
|
|
880
959
|
case 3:
|
|
881
|
-
transferResult =
|
|
960
|
+
transferResult = _h.sent();
|
|
882
961
|
if (!isBroadcastSuccess(transferResult)) {
|
|
883
962
|
throw new Error("failed to broadcast transaction: " + transferResult.txhash);
|
|
884
963
|
}
|
|
@@ -894,7 +973,7 @@ var Client = /** @class */ (function () {
|
|
|
894
973
|
* @returns {StdTx} The signed transaction.
|
|
895
974
|
*/
|
|
896
975
|
Client.prototype.transferOffline = function (_a) {
|
|
897
|
-
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.
|
|
976
|
+
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;
|
|
898
977
|
return __awaiter(this, void 0, void 0, function () {
|
|
899
978
|
var fee, result;
|
|
900
979
|
return __generator(this, function (_g) {
|
|
@@ -903,10 +982,18 @@ var Client = /** @class */ (function () {
|
|
|
903
982
|
registerCodecs(getPrefix(this.network));
|
|
904
983
|
return [4 /*yield*/, this.getFees()];
|
|
905
984
|
case 1:
|
|
906
|
-
fee = _g.sent();
|
|
907
|
-
if (
|
|
908
|
-
|
|
909
|
-
|
|
985
|
+
fee = (_g.sent()).average;
|
|
986
|
+
if (isAssetRuneNative(asset)) {
|
|
987
|
+
// amount + fee < runeBalance
|
|
988
|
+
if (from_rune_balance.lt(amount.plus(fee))) {
|
|
989
|
+
throw new Error('insufficient funds');
|
|
990
|
+
}
|
|
991
|
+
}
|
|
992
|
+
else {
|
|
993
|
+
// amount < assetBalances && runeBalance < fee
|
|
994
|
+
if (from_asset_balance.lt(amount) || from_rune_balance.lt(fee)) {
|
|
995
|
+
throw new Error('insufficient funds');
|
|
996
|
+
}
|
|
910
997
|
}
|
|
911
998
|
return [4 /*yield*/, this.cosmosClient.transferSignedOffline({
|
|
912
999
|
privkey: this.getPrivKey(walletIndex),
|
|
@@ -950,22 +1037,23 @@ exports.DEFAULT_GAS_VALUE = DEFAULT_GAS_VALUE;
|
|
|
950
1037
|
exports.DEPOSIT_GAS_VALUE = DEPOSIT_GAS_VALUE;
|
|
951
1038
|
exports.MAX_TX_COUNT = MAX_TX_COUNT;
|
|
952
1039
|
exports.MsgNativeTx = MsgNativeTx;
|
|
1040
|
+
exports.assetFromDenom = assetFromDenom;
|
|
953
1041
|
exports.buildDepositTx = buildDepositTx;
|
|
954
|
-
exports.getAsset = getAsset;
|
|
955
1042
|
exports.getBalance = getBalance;
|
|
956
1043
|
exports.getChainId = getChainId;
|
|
957
1044
|
exports.getDefaultClientUrl = getDefaultClientUrl;
|
|
958
1045
|
exports.getDefaultExplorerUrls = getDefaultExplorerUrls;
|
|
959
1046
|
exports.getDefaultFees = getDefaultFees;
|
|
960
1047
|
exports.getDenom = getDenom;
|
|
961
|
-
exports.getDenomWithChain = getDenomWithChain;
|
|
962
1048
|
exports.getDepositTxDataFromLogs = getDepositTxDataFromLogs;
|
|
963
1049
|
exports.getExplorerAddressUrl = getExplorerAddressUrl;
|
|
964
1050
|
exports.getExplorerTxUrl = getExplorerTxUrl;
|
|
965
1051
|
exports.getExplorerUrl = getExplorerUrl;
|
|
966
1052
|
exports.getPrefix = getPrefix;
|
|
967
1053
|
exports.getTxType = getTxType;
|
|
1054
|
+
exports.isAssetRuneNative = isAssetRuneNative;
|
|
968
1055
|
exports.isBroadcastSuccess = isBroadcastSuccess;
|
|
1056
|
+
exports.isChainId = isChainId;
|
|
969
1057
|
exports.isMsgMultiSend = isMsgMultiSend;
|
|
970
1058
|
exports.isMsgSend = isMsgSend;
|
|
971
1059
|
exports.msgNativeTxFromJson = msgNativeTxFromJson;
|
|
@@ -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
|
-
*
|
|
14
|
+
* Checks whether an asset is `AssetRuneNative`
|
|
15
15
|
*
|
|
16
16
|
* @param {Asset} asset
|
|
17
|
-
* @returns {
|
|
17
|
+
* @returns {boolean} `true` or `false`
|
|
18
18
|
*/
|
|
19
|
-
export declare const
|
|
19
|
+
export declare const isAssetRuneNative: (asset: Asset) => boolean;
|
|
20
20
|
/**
|
|
21
|
-
* Get denomination
|
|
21
|
+
* Get denomination from Asset
|
|
22
22
|
*
|
|
23
23
|
* @param {Asset} asset
|
|
24
|
-
* @returns {string} The denomination
|
|
24
|
+
* @returns {string} The denomination of the given asset.
|
|
25
25
|
*/
|
|
26
|
-
export declare const
|
|
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
|
|
33
|
+
export declare const assetFromDenom: (denom: string) => Asset | null;
|
|
34
34
|
/**
|
|
35
35
|
* Type guard for MsgSend
|
|
36
36
|
*
|
|
@@ -59,13 +59,27 @@ export declare const isBroadcastSuccess: (response: unknown) => boolean;
|
|
|
59
59
|
* @returns {string} The address prefix based on the network.
|
|
60
60
|
*
|
|
61
61
|
**/
|
|
62
|
-
export declare const getPrefix: (network: Network) => "thor" | "tthor";
|
|
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;
|
|
63
75
|
/**
|
|
64
76
|
* Get the chain id.
|
|
65
77
|
*
|
|
78
|
+
* @param {Network} network
|
|
66
79
|
* @returns {string} The chain id based on the network.
|
|
80
|
+
*
|
|
67
81
|
*/
|
|
68
|
-
export declare const getChainId: () =>
|
|
82
|
+
export declare const getChainId: (network: Network) => ChainId;
|
|
69
83
|
/**
|
|
70
84
|
* Register Codecs based on the prefix.
|
|
71
85
|
*
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xchainjs/xchain-thorchain",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.21.1",
|
|
4
4
|
"description": "Custom Thorchain client and utilities used by XChainJS clients",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"THORChain",
|
|
@@ -33,10 +33,10 @@
|
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"@types/big.js": "^6.0.0",
|
|
36
|
-
"@xchainjs/xchain-client": "0.
|
|
37
|
-
"@xchainjs/xchain-cosmos": "0.
|
|
36
|
+
"@xchainjs/xchain-client": "^0.11.0",
|
|
37
|
+
"@xchainjs/xchain-cosmos": "^0.16.0",
|
|
38
38
|
"@xchainjs/xchain-crypto": "^0.2.4",
|
|
39
|
-
"@xchainjs/xchain-util": "^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"
|
|
@@ -45,10 +45,10 @@
|
|
|
45
45
|
"access": "public"
|
|
46
46
|
},
|
|
47
47
|
"peerDependencies": {
|
|
48
|
-
"@xchainjs/xchain-client": "0.
|
|
49
|
-
"@xchainjs/xchain-cosmos": "0.
|
|
48
|
+
"@xchainjs/xchain-client": "^0.11.0",
|
|
49
|
+
"@xchainjs/xchain-cosmos": "^0.16.0",
|
|
50
50
|
"@xchainjs/xchain-crypto": "^0.2.4",
|
|
51
|
-
"@xchainjs/xchain-util": "^0.
|
|
51
|
+
"@xchainjs/xchain-util": "^0.5.0",
|
|
52
52
|
"axios": "^0.21.0",
|
|
53
53
|
"cosmos-client": "0.39.2"
|
|
54
54
|
}
|