@xchainjs/xchain-thorchain 0.19.4 → 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 +39 -0
- package/lib/client.d.ts +9 -1
- package/lib/index.esm.js +170 -53
- package/lib/index.js +169 -52
- package/lib/types/client-types.d.ts +14 -1
- package/lib/util.d.ts +11 -9
- package/package.json +7 -7
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,42 @@
|
|
|
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
|
+
|
|
22
|
+
# v0.20.1 (2022-01-11)
|
|
23
|
+
|
|
24
|
+
## Fix
|
|
25
|
+
|
|
26
|
+
- Get chain ID from THORNode before posting to deposit handler.
|
|
27
|
+
|
|
28
|
+
# v.0.20.0 (2021-12-29)
|
|
29
|
+
|
|
30
|
+
## Breaking change
|
|
31
|
+
|
|
32
|
+
- Add stagenet environment handling for `Network` and `BaseXChainClient` to client
|
|
33
|
+
|
|
34
|
+
# v.0.19.5 (2021-11-22)
|
|
35
|
+
|
|
36
|
+
## Add
|
|
37
|
+
|
|
38
|
+
- Provide `transferOffline` method
|
|
39
|
+
|
|
1
40
|
# v.0.19.4 (2021-11-22)
|
|
2
41
|
|
|
3
42
|
## Add
|
package/lib/client.d.ts
CHANGED
|
@@ -2,7 +2,8 @@ import { Address, Balance, Fees, Network, Tx, TxHash, TxHistoryParams, TxParams,
|
|
|
2
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
|
-
import {
|
|
5
|
+
import { StdTx } from 'cosmos-client/x/auth';
|
|
6
|
+
import { ClientUrl, DepositParam, ExplorerUrls, NodeUrl, ThorchainClientParams, TxOfflineParams } from './types';
|
|
6
7
|
/**
|
|
7
8
|
* Interface for custom Thorchain client
|
|
8
9
|
*/
|
|
@@ -205,6 +206,13 @@ declare class Client implements ThorchainClient, XChainClient {
|
|
|
205
206
|
* @returns {TxHash} The transaction hash.
|
|
206
207
|
*/
|
|
207
208
|
transfer({ walletIndex, asset, amount, recipient, memo }: TxParams): Promise<TxHash>;
|
|
209
|
+
/**
|
|
210
|
+
* Transfer without broadcast balances with MsgSend
|
|
211
|
+
*
|
|
212
|
+
* @param {TxOfflineParams} params The transfer offline options.
|
|
213
|
+
* @returns {StdTx} The signed transaction.
|
|
214
|
+
*/
|
|
215
|
+
transferOffline({ walletIndex, asset, amount, recipient, memo, from_rune_balance, from_asset_balance, from_account_number, from_sequence, }: TxOfflineParams): Promise<StdTx>;
|
|
208
216
|
/**
|
|
209
217
|
* Get the fees.
|
|
210
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,6 +192,8 @@ 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
|
}
|
|
@@ -198,9 +201,20 @@ var getPrefix = function (network) {
|
|
|
198
201
|
/**
|
|
199
202
|
* Get the chain id.
|
|
200
203
|
*
|
|
204
|
+
* @param {Network} network
|
|
201
205
|
* @returns {string} The chain id based on the network.
|
|
206
|
+
*
|
|
202
207
|
*/
|
|
203
|
-
var getChainId = function () {
|
|
208
|
+
var getChainId = function (network) {
|
|
209
|
+
switch (network) {
|
|
210
|
+
case Network.Mainnet:
|
|
211
|
+
return 'thorchain';
|
|
212
|
+
case Network.Stagenet:
|
|
213
|
+
return 'thorchain-stagenet';
|
|
214
|
+
case Network.Testnet:
|
|
215
|
+
return 'thorchain';
|
|
216
|
+
}
|
|
217
|
+
};
|
|
204
218
|
/**
|
|
205
219
|
* Register Codecs based on the prefix.
|
|
206
220
|
*
|
|
@@ -284,19 +298,25 @@ var getTxType = function (txData, encoding) {
|
|
|
284
298
|
* @throws {"Invalid client url"} Thrown if the client url is an invalid one.
|
|
285
299
|
*/
|
|
286
300
|
var buildDepositTx = function (msgNativeTx, nodeUrl) { return __awaiter(void 0, void 0, void 0, function () {
|
|
287
|
-
var response, fee, unsignedStdTx;
|
|
301
|
+
var data, chainId, response, fee, unsignedStdTx;
|
|
288
302
|
var _a, _b;
|
|
289
303
|
return __generator(this, function (_c) {
|
|
290
304
|
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
|
-
})];
|
|
305
|
+
case 0: return [4 /*yield*/, axios.get(nodeUrl + "/cosmos/base/tendermint/v1beta1/node_info")];
|
|
299
306
|
case 1:
|
|
307
|
+
data = (_c.sent()).data;
|
|
308
|
+
chainId = data.default_node_info.network;
|
|
309
|
+
if (!chainId || !(chainId == 'thorchain' || chainId == 'thorchain-stagenet'))
|
|
310
|
+
throw new Error('invalid network');
|
|
311
|
+
return [4 /*yield*/, axios.post(nodeUrl + "/thorchain/deposit", {
|
|
312
|
+
coins: msgNativeTx.coins,
|
|
313
|
+
memo: msgNativeTx.memo,
|
|
314
|
+
base_req: {
|
|
315
|
+
chain_id: chainId,
|
|
316
|
+
from: msgNativeTx.signer,
|
|
317
|
+
},
|
|
318
|
+
})];
|
|
319
|
+
case 2:
|
|
300
320
|
response = (_c.sent()).data;
|
|
301
321
|
if (!response || !response.value)
|
|
302
322
|
throw new Error('Invalid client url');
|
|
@@ -332,7 +352,7 @@ var getBalance = function (_a) {
|
|
|
332
352
|
balances = _b.sent();
|
|
333
353
|
return [2 /*return*/, balances
|
|
334
354
|
.map(function (balance) { return ({
|
|
335
|
-
asset: (balance.denom &&
|
|
355
|
+
asset: (balance.denom && assetFromDenom(balance.denom)) || AssetRuneNative,
|
|
336
356
|
amount: baseAmount(balance.amount, DECIMAL),
|
|
337
357
|
}); })
|
|
338
358
|
.filter(function (balance) { return !assets || assets.filter(function (asset) { return assetToString(balance.asset) === assetToString(asset); }).length; })];
|
|
@@ -352,8 +372,12 @@ var getDefaultClientUrl = function () {
|
|
|
352
372
|
node: 'https://testnet.thornode.thorchain.info',
|
|
353
373
|
rpc: 'https://testnet.rpc.thorchain.info',
|
|
354
374
|
},
|
|
375
|
+
_a[Network.Stagenet] = {
|
|
376
|
+
node: 'https://stagenet-thornode.ninerealms.com',
|
|
377
|
+
rpc: 'https://stagenet-rpc.ninerealms.com',
|
|
378
|
+
},
|
|
355
379
|
_a[Network.Mainnet] = {
|
|
356
|
-
node: 'https://thornode.
|
|
380
|
+
node: 'https://thornode.ninerealms.com',
|
|
357
381
|
rpc: 'https://rpc.thorchain.info',
|
|
358
382
|
},
|
|
359
383
|
_a;
|
|
@@ -368,16 +392,19 @@ var getDefaultExplorerUrls = function () {
|
|
|
368
392
|
var _a, _b, _c;
|
|
369
393
|
var root = (_a = {},
|
|
370
394
|
_a[Network.Testnet] = DEFAULT_EXPLORER_URL + "?network=testnet",
|
|
395
|
+
_a[Network.Stagenet] = DEFAULT_EXPLORER_URL + "?network=stagenet",
|
|
371
396
|
_a[Network.Mainnet] = DEFAULT_EXPLORER_URL,
|
|
372
397
|
_a);
|
|
373
398
|
var txUrl = DEFAULT_EXPLORER_URL + "/tx";
|
|
374
399
|
var tx = (_b = {},
|
|
375
400
|
_b[Network.Testnet] = txUrl,
|
|
401
|
+
_b[Network.Stagenet] = txUrl,
|
|
376
402
|
_b[Network.Mainnet] = txUrl,
|
|
377
403
|
_b);
|
|
378
404
|
var addressUrl = DEFAULT_EXPLORER_URL + "/address";
|
|
379
405
|
var address = (_c = {},
|
|
380
406
|
_c[Network.Testnet] = addressUrl,
|
|
407
|
+
_c[Network.Stagenet] = addressUrl,
|
|
381
408
|
_c[Network.Mainnet] = addressUrl,
|
|
382
409
|
_c);
|
|
383
410
|
return {
|
|
@@ -411,6 +438,8 @@ var getExplorerAddressUrl = function (_a) {
|
|
|
411
438
|
switch (network) {
|
|
412
439
|
case Network.Mainnet:
|
|
413
440
|
return url;
|
|
441
|
+
case Network.Stagenet:
|
|
442
|
+
return url + "?network=stagenet";
|
|
414
443
|
case Network.Testnet:
|
|
415
444
|
return url + "?network=testnet";
|
|
416
445
|
}
|
|
@@ -429,6 +458,8 @@ var getExplorerTxUrl = function (_a) {
|
|
|
429
458
|
switch (network) {
|
|
430
459
|
case Network.Mainnet:
|
|
431
460
|
return url;
|
|
461
|
+
case Network.Stagenet:
|
|
462
|
+
return url + "?network=stagenet";
|
|
432
463
|
case Network.Testnet:
|
|
433
464
|
return url + "?network=testnet";
|
|
434
465
|
}
|
|
@@ -453,6 +484,7 @@ var Client = /** @class */ (function () {
|
|
|
453
484
|
var _this = this;
|
|
454
485
|
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
486
|
_b[Network.Mainnet] = "44'/931'/0'/0/",
|
|
487
|
+
_b[Network.Stagenet] = "44'/931'/0'/0/",
|
|
456
488
|
_b[Network.Testnet] = "44'/931'/0'/0/",
|
|
457
489
|
_b) : _d;
|
|
458
490
|
this.phrase = '';
|
|
@@ -527,7 +559,7 @@ var Client = /** @class */ (function () {
|
|
|
527
559
|
this.rootDerivationPaths = rootDerivationPaths;
|
|
528
560
|
this.cosmosClient = new CosmosSDKClient({
|
|
529
561
|
server: this.getClientUrl().node,
|
|
530
|
-
chainId: getChainId(),
|
|
562
|
+
chainId: getChainId(this.network),
|
|
531
563
|
prefix: getPrefix(this.network),
|
|
532
564
|
});
|
|
533
565
|
if (phrase)
|
|
@@ -799,23 +831,41 @@ var Client = /** @class */ (function () {
|
|
|
799
831
|
* @throws {"failed to broadcast transaction"} Thrown if failed to broadcast transaction.
|
|
800
832
|
*/
|
|
801
833
|
Client.prototype.deposit = function (_a) {
|
|
802
|
-
var _b, _c;
|
|
803
|
-
var
|
|
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;
|
|
804
836
|
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)
|
|
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))];
|
|
809
841
|
case 1:
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
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
|
+
}
|
|
813
863
|
}
|
|
814
864
|
signer = this.getAddress(walletIndex);
|
|
815
865
|
msgNativeTx = msgNativeTxFromJson({
|
|
816
866
|
coins: [
|
|
817
867
|
{
|
|
818
|
-
asset:
|
|
868
|
+
asset: isAssetRuneNative(asset) ? assetToString(AssetRuneNative) : getDenom(asset),
|
|
819
869
|
amount: amount.amount().toString(),
|
|
820
870
|
},
|
|
821
871
|
],
|
|
@@ -824,11 +874,11 @@ var Client = /** @class */ (function () {
|
|
|
824
874
|
});
|
|
825
875
|
return [4 /*yield*/, buildDepositTx(msgNativeTx, this.getClientUrl().node)];
|
|
826
876
|
case 2:
|
|
827
|
-
unsignedStdTx =
|
|
877
|
+
unsignedStdTx = _k.sent();
|
|
828
878
|
privateKey = this.getPrivKey(walletIndex);
|
|
829
879
|
accAddress = AccAddress.fromBech32(signer);
|
|
830
880
|
return [4 /*yield*/, this.cosmosClient.signAndBroadcast(unsignedStdTx, privateKey, accAddress)];
|
|
831
|
-
case 3: return [2 /*return*/, (
|
|
881
|
+
case 3: return [2 /*return*/, (_g = (_f = (_k.sent())) === null || _f === void 0 ? void 0 : _f.txhash) !== null && _g !== void 0 ? _g : ''];
|
|
832
882
|
}
|
|
833
883
|
});
|
|
834
884
|
});
|
|
@@ -840,22 +890,39 @@ var Client = /** @class */ (function () {
|
|
|
840
890
|
* @returns {TxHash} The transaction hash.
|
|
841
891
|
*/
|
|
842
892
|
Client.prototype.transfer = function (_a) {
|
|
843
|
-
var _b
|
|
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;
|
|
844
895
|
return __awaiter(this, void 0, void 0, function () {
|
|
845
|
-
var assetBalance, fee, transferResult;
|
|
846
|
-
return __generator(this, function (
|
|
847
|
-
switch (
|
|
896
|
+
var balances, runeBalance, assetBalance, fee, transferResult;
|
|
897
|
+
return __generator(this, function (_h) {
|
|
898
|
+
switch (_h.label) {
|
|
848
899
|
case 0:
|
|
849
900
|
registerCodecs(getPrefix(this.network));
|
|
850
|
-
return [4 /*yield*/, this.getBalance(this.getAddress(walletIndex)
|
|
901
|
+
return [4 /*yield*/, this.getBalance(this.getAddress(walletIndex))];
|
|
851
902
|
case 1:
|
|
852
|
-
|
|
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);
|
|
853
912
|
return [4 /*yield*/, this.getFees()];
|
|
854
913
|
case 2:
|
|
855
|
-
fee =
|
|
856
|
-
if (
|
|
857
|
-
|
|
858
|
-
|
|
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
|
+
}
|
|
859
926
|
}
|
|
860
927
|
return [4 /*yield*/, this.cosmosClient.transfer({
|
|
861
928
|
privkey: this.getPrivKey(walletIndex),
|
|
@@ -870,7 +937,7 @@ var Client = /** @class */ (function () {
|
|
|
870
937
|
},
|
|
871
938
|
})];
|
|
872
939
|
case 3:
|
|
873
|
-
transferResult =
|
|
940
|
+
transferResult = _h.sent();
|
|
874
941
|
if (!isBroadcastSuccess(transferResult)) {
|
|
875
942
|
throw new Error("failed to broadcast transaction: " + transferResult.txhash);
|
|
876
943
|
}
|
|
@@ -879,6 +946,56 @@ var Client = /** @class */ (function () {
|
|
|
879
946
|
});
|
|
880
947
|
});
|
|
881
948
|
};
|
|
949
|
+
/**
|
|
950
|
+
* Transfer without broadcast balances with MsgSend
|
|
951
|
+
*
|
|
952
|
+
* @param {TxOfflineParams} params The transfer offline options.
|
|
953
|
+
* @returns {StdTx} The signed transaction.
|
|
954
|
+
*/
|
|
955
|
+
Client.prototype.transferOffline = function (_a) {
|
|
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;
|
|
957
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
958
|
+
var fee, result;
|
|
959
|
+
return __generator(this, function (_g) {
|
|
960
|
+
switch (_g.label) {
|
|
961
|
+
case 0:
|
|
962
|
+
registerCodecs(getPrefix(this.network));
|
|
963
|
+
return [4 /*yield*/, this.getFees()];
|
|
964
|
+
case 1:
|
|
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
|
+
}
|
|
977
|
+
}
|
|
978
|
+
return [4 /*yield*/, this.cosmosClient.transferSignedOffline({
|
|
979
|
+
privkey: this.getPrivKey(walletIndex),
|
|
980
|
+
from: this.getAddress(walletIndex),
|
|
981
|
+
from_account_number: from_account_number,
|
|
982
|
+
from_sequence: from_sequence,
|
|
983
|
+
to: recipient,
|
|
984
|
+
amount: amount.amount().toString(),
|
|
985
|
+
asset: getDenom(asset),
|
|
986
|
+
memo: memo,
|
|
987
|
+
fee: {
|
|
988
|
+
amount: [],
|
|
989
|
+
gas: DEFAULT_GAS_VALUE,
|
|
990
|
+
},
|
|
991
|
+
})];
|
|
992
|
+
case 2:
|
|
993
|
+
result = _g.sent();
|
|
994
|
+
return [2 /*return*/, JSON.parse(codec.toJSONString(result)).value];
|
|
995
|
+
}
|
|
996
|
+
});
|
|
997
|
+
});
|
|
998
|
+
};
|
|
882
999
|
/**
|
|
883
1000
|
* Get the fees.
|
|
884
1001
|
*
|
|
@@ -894,4 +1011,4 @@ var Client = /** @class */ (function () {
|
|
|
894
1011
|
return Client;
|
|
895
1012
|
}());
|
|
896
1013
|
|
|
897
|
-
export { Client, DECIMAL, DEFAULT_GAS_VALUE, DEPOSIT_GAS_VALUE, MAX_TX_COUNT, MsgNativeTx,
|
|
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
|
-
*
|
|
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,6 +200,8 @@ 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
|
}
|
|
@@ -206,9 +209,20 @@ var getPrefix = function (network) {
|
|
|
206
209
|
/**
|
|
207
210
|
* Get the chain id.
|
|
208
211
|
*
|
|
212
|
+
* @param {Network} network
|
|
209
213
|
* @returns {string} The chain id based on the network.
|
|
214
|
+
*
|
|
210
215
|
*/
|
|
211
|
-
var getChainId = function () {
|
|
216
|
+
var getChainId = function (network) {
|
|
217
|
+
switch (network) {
|
|
218
|
+
case xchainClient.Network.Mainnet:
|
|
219
|
+
return 'thorchain';
|
|
220
|
+
case xchainClient.Network.Stagenet:
|
|
221
|
+
return 'thorchain-stagenet';
|
|
222
|
+
case xchainClient.Network.Testnet:
|
|
223
|
+
return 'thorchain';
|
|
224
|
+
}
|
|
225
|
+
};
|
|
212
226
|
/**
|
|
213
227
|
* Register Codecs based on the prefix.
|
|
214
228
|
*
|
|
@@ -292,19 +306,25 @@ var getTxType = function (txData, encoding) {
|
|
|
292
306
|
* @throws {"Invalid client url"} Thrown if the client url is an invalid one.
|
|
293
307
|
*/
|
|
294
308
|
var buildDepositTx = function (msgNativeTx, nodeUrl) { return __awaiter(void 0, void 0, void 0, function () {
|
|
295
|
-
var response, fee, unsignedStdTx;
|
|
309
|
+
var data, chainId, response, fee, unsignedStdTx;
|
|
296
310
|
var _a, _b;
|
|
297
311
|
return __generator(this, function (_c) {
|
|
298
312
|
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
|
-
})];
|
|
313
|
+
case 0: return [4 /*yield*/, axios__default['default'].get(nodeUrl + "/cosmos/base/tendermint/v1beta1/node_info")];
|
|
307
314
|
case 1:
|
|
315
|
+
data = (_c.sent()).data;
|
|
316
|
+
chainId = data.default_node_info.network;
|
|
317
|
+
if (!chainId || !(chainId == 'thorchain' || chainId == 'thorchain-stagenet'))
|
|
318
|
+
throw new Error('invalid network');
|
|
319
|
+
return [4 /*yield*/, axios__default['default'].post(nodeUrl + "/thorchain/deposit", {
|
|
320
|
+
coins: msgNativeTx.coins,
|
|
321
|
+
memo: msgNativeTx.memo,
|
|
322
|
+
base_req: {
|
|
323
|
+
chain_id: chainId,
|
|
324
|
+
from: msgNativeTx.signer,
|
|
325
|
+
},
|
|
326
|
+
})];
|
|
327
|
+
case 2:
|
|
308
328
|
response = (_c.sent()).data;
|
|
309
329
|
if (!response || !response.value)
|
|
310
330
|
throw new Error('Invalid client url');
|
|
@@ -340,7 +360,7 @@ var getBalance = function (_a) {
|
|
|
340
360
|
balances = _b.sent();
|
|
341
361
|
return [2 /*return*/, balances
|
|
342
362
|
.map(function (balance) { return ({
|
|
343
|
-
asset: (balance.denom &&
|
|
363
|
+
asset: (balance.denom && assetFromDenom(balance.denom)) || xchainUtil.AssetRuneNative,
|
|
344
364
|
amount: xchainUtil.baseAmount(balance.amount, DECIMAL),
|
|
345
365
|
}); })
|
|
346
366
|
.filter(function (balance) { return !assets || assets.filter(function (asset) { return xchainUtil.assetToString(balance.asset) === xchainUtil.assetToString(asset); }).length; })];
|
|
@@ -360,8 +380,12 @@ var getDefaultClientUrl = function () {
|
|
|
360
380
|
node: 'https://testnet.thornode.thorchain.info',
|
|
361
381
|
rpc: 'https://testnet.rpc.thorchain.info',
|
|
362
382
|
},
|
|
383
|
+
_a[xchainClient.Network.Stagenet] = {
|
|
384
|
+
node: 'https://stagenet-thornode.ninerealms.com',
|
|
385
|
+
rpc: 'https://stagenet-rpc.ninerealms.com',
|
|
386
|
+
},
|
|
363
387
|
_a[xchainClient.Network.Mainnet] = {
|
|
364
|
-
node: 'https://thornode.
|
|
388
|
+
node: 'https://thornode.ninerealms.com',
|
|
365
389
|
rpc: 'https://rpc.thorchain.info',
|
|
366
390
|
},
|
|
367
391
|
_a;
|
|
@@ -376,16 +400,19 @@ var getDefaultExplorerUrls = function () {
|
|
|
376
400
|
var _a, _b, _c;
|
|
377
401
|
var root = (_a = {},
|
|
378
402
|
_a[xchainClient.Network.Testnet] = DEFAULT_EXPLORER_URL + "?network=testnet",
|
|
403
|
+
_a[xchainClient.Network.Stagenet] = DEFAULT_EXPLORER_URL + "?network=stagenet",
|
|
379
404
|
_a[xchainClient.Network.Mainnet] = DEFAULT_EXPLORER_URL,
|
|
380
405
|
_a);
|
|
381
406
|
var txUrl = DEFAULT_EXPLORER_URL + "/tx";
|
|
382
407
|
var tx = (_b = {},
|
|
383
408
|
_b[xchainClient.Network.Testnet] = txUrl,
|
|
409
|
+
_b[xchainClient.Network.Stagenet] = txUrl,
|
|
384
410
|
_b[xchainClient.Network.Mainnet] = txUrl,
|
|
385
411
|
_b);
|
|
386
412
|
var addressUrl = DEFAULT_EXPLORER_URL + "/address";
|
|
387
413
|
var address = (_c = {},
|
|
388
414
|
_c[xchainClient.Network.Testnet] = addressUrl,
|
|
415
|
+
_c[xchainClient.Network.Stagenet] = addressUrl,
|
|
389
416
|
_c[xchainClient.Network.Mainnet] = addressUrl,
|
|
390
417
|
_c);
|
|
391
418
|
return {
|
|
@@ -419,6 +446,8 @@ var getExplorerAddressUrl = function (_a) {
|
|
|
419
446
|
switch (network) {
|
|
420
447
|
case xchainClient.Network.Mainnet:
|
|
421
448
|
return url;
|
|
449
|
+
case xchainClient.Network.Stagenet:
|
|
450
|
+
return url + "?network=stagenet";
|
|
422
451
|
case xchainClient.Network.Testnet:
|
|
423
452
|
return url + "?network=testnet";
|
|
424
453
|
}
|
|
@@ -437,6 +466,8 @@ var getExplorerTxUrl = function (_a) {
|
|
|
437
466
|
switch (network) {
|
|
438
467
|
case xchainClient.Network.Mainnet:
|
|
439
468
|
return url;
|
|
469
|
+
case xchainClient.Network.Stagenet:
|
|
470
|
+
return url + "?network=stagenet";
|
|
440
471
|
case xchainClient.Network.Testnet:
|
|
441
472
|
return url + "?network=testnet";
|
|
442
473
|
}
|
|
@@ -461,6 +492,7 @@ var Client = /** @class */ (function () {
|
|
|
461
492
|
var _this = this;
|
|
462
493
|
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
494
|
_b[xchainClient.Network.Mainnet] = "44'/931'/0'/0/",
|
|
495
|
+
_b[xchainClient.Network.Stagenet] = "44'/931'/0'/0/",
|
|
464
496
|
_b[xchainClient.Network.Testnet] = "44'/931'/0'/0/",
|
|
465
497
|
_b) : _d;
|
|
466
498
|
this.phrase = '';
|
|
@@ -535,7 +567,7 @@ var Client = /** @class */ (function () {
|
|
|
535
567
|
this.rootDerivationPaths = rootDerivationPaths;
|
|
536
568
|
this.cosmosClient = new xchainCosmos.CosmosSDKClient({
|
|
537
569
|
server: this.getClientUrl().node,
|
|
538
|
-
chainId: getChainId(),
|
|
570
|
+
chainId: getChainId(this.network),
|
|
539
571
|
prefix: getPrefix(this.network),
|
|
540
572
|
});
|
|
541
573
|
if (phrase)
|
|
@@ -807,23 +839,41 @@ var Client = /** @class */ (function () {
|
|
|
807
839
|
* @throws {"failed to broadcast transaction"} Thrown if failed to broadcast transaction.
|
|
808
840
|
*/
|
|
809
841
|
Client.prototype.deposit = function (_a) {
|
|
810
|
-
var _b, _c;
|
|
811
|
-
var
|
|
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;
|
|
812
844
|
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)
|
|
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))];
|
|
817
849
|
case 1:
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
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
|
+
}
|
|
821
871
|
}
|
|
822
872
|
signer = this.getAddress(walletIndex);
|
|
823
873
|
msgNativeTx = msgNativeTxFromJson({
|
|
824
874
|
coins: [
|
|
825
875
|
{
|
|
826
|
-
asset:
|
|
876
|
+
asset: isAssetRuneNative(asset) ? xchainUtil.assetToString(xchainUtil.AssetRuneNative) : getDenom(asset),
|
|
827
877
|
amount: amount.amount().toString(),
|
|
828
878
|
},
|
|
829
879
|
],
|
|
@@ -832,11 +882,11 @@ var Client = /** @class */ (function () {
|
|
|
832
882
|
});
|
|
833
883
|
return [4 /*yield*/, buildDepositTx(msgNativeTx, this.getClientUrl().node)];
|
|
834
884
|
case 2:
|
|
835
|
-
unsignedStdTx =
|
|
885
|
+
unsignedStdTx = _k.sent();
|
|
836
886
|
privateKey = this.getPrivKey(walletIndex);
|
|
837
887
|
accAddress = cosmosClient.AccAddress.fromBech32(signer);
|
|
838
888
|
return [4 /*yield*/, this.cosmosClient.signAndBroadcast(unsignedStdTx, privateKey, accAddress)];
|
|
839
|
-
case 3: return [2 /*return*/, (
|
|
889
|
+
case 3: return [2 /*return*/, (_g = (_f = (_k.sent())) === null || _f === void 0 ? void 0 : _f.txhash) !== null && _g !== void 0 ? _g : ''];
|
|
840
890
|
}
|
|
841
891
|
});
|
|
842
892
|
});
|
|
@@ -848,22 +898,39 @@ var Client = /** @class */ (function () {
|
|
|
848
898
|
* @returns {TxHash} The transaction hash.
|
|
849
899
|
*/
|
|
850
900
|
Client.prototype.transfer = function (_a) {
|
|
851
|
-
var _b
|
|
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;
|
|
852
903
|
return __awaiter(this, void 0, void 0, function () {
|
|
853
|
-
var assetBalance, fee, transferResult;
|
|
854
|
-
return __generator(this, function (
|
|
855
|
-
switch (
|
|
904
|
+
var balances, runeBalance, assetBalance, fee, transferResult;
|
|
905
|
+
return __generator(this, function (_h) {
|
|
906
|
+
switch (_h.label) {
|
|
856
907
|
case 0:
|
|
857
908
|
registerCodecs(getPrefix(this.network));
|
|
858
|
-
return [4 /*yield*/, this.getBalance(this.getAddress(walletIndex)
|
|
909
|
+
return [4 /*yield*/, this.getBalance(this.getAddress(walletIndex))];
|
|
859
910
|
case 1:
|
|
860
|
-
|
|
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);
|
|
861
920
|
return [4 /*yield*/, this.getFees()];
|
|
862
921
|
case 2:
|
|
863
|
-
fee =
|
|
864
|
-
if (
|
|
865
|
-
|
|
866
|
-
|
|
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
|
+
}
|
|
867
934
|
}
|
|
868
935
|
return [4 /*yield*/, this.cosmosClient.transfer({
|
|
869
936
|
privkey: this.getPrivKey(walletIndex),
|
|
@@ -878,7 +945,7 @@ var Client = /** @class */ (function () {
|
|
|
878
945
|
},
|
|
879
946
|
})];
|
|
880
947
|
case 3:
|
|
881
|
-
transferResult =
|
|
948
|
+
transferResult = _h.sent();
|
|
882
949
|
if (!isBroadcastSuccess(transferResult)) {
|
|
883
950
|
throw new Error("failed to broadcast transaction: " + transferResult.txhash);
|
|
884
951
|
}
|
|
@@ -887,6 +954,56 @@ var Client = /** @class */ (function () {
|
|
|
887
954
|
});
|
|
888
955
|
});
|
|
889
956
|
};
|
|
957
|
+
/**
|
|
958
|
+
* Transfer without broadcast balances with MsgSend
|
|
959
|
+
*
|
|
960
|
+
* @param {TxOfflineParams} params The transfer offline options.
|
|
961
|
+
* @returns {StdTx} The signed transaction.
|
|
962
|
+
*/
|
|
963
|
+
Client.prototype.transferOffline = function (_a) {
|
|
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;
|
|
965
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
966
|
+
var fee, result;
|
|
967
|
+
return __generator(this, function (_g) {
|
|
968
|
+
switch (_g.label) {
|
|
969
|
+
case 0:
|
|
970
|
+
registerCodecs(getPrefix(this.network));
|
|
971
|
+
return [4 /*yield*/, this.getFees()];
|
|
972
|
+
case 1:
|
|
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
|
+
}
|
|
985
|
+
}
|
|
986
|
+
return [4 /*yield*/, this.cosmosClient.transferSignedOffline({
|
|
987
|
+
privkey: this.getPrivKey(walletIndex),
|
|
988
|
+
from: this.getAddress(walletIndex),
|
|
989
|
+
from_account_number: from_account_number,
|
|
990
|
+
from_sequence: from_sequence,
|
|
991
|
+
to: recipient,
|
|
992
|
+
amount: amount.amount().toString(),
|
|
993
|
+
asset: getDenom(asset),
|
|
994
|
+
memo: memo,
|
|
995
|
+
fee: {
|
|
996
|
+
amount: [],
|
|
997
|
+
gas: DEFAULT_GAS_VALUE,
|
|
998
|
+
},
|
|
999
|
+
})];
|
|
1000
|
+
case 2:
|
|
1001
|
+
result = _g.sent();
|
|
1002
|
+
return [2 /*return*/, JSON.parse(cosmosClient.codec.toJSONString(result)).value];
|
|
1003
|
+
}
|
|
1004
|
+
});
|
|
1005
|
+
});
|
|
1006
|
+
};
|
|
890
1007
|
/**
|
|
891
1008
|
* Get the fees.
|
|
892
1009
|
*
|
|
@@ -908,21 +1025,21 @@ exports.DEFAULT_GAS_VALUE = DEFAULT_GAS_VALUE;
|
|
|
908
1025
|
exports.DEPOSIT_GAS_VALUE = DEPOSIT_GAS_VALUE;
|
|
909
1026
|
exports.MAX_TX_COUNT = MAX_TX_COUNT;
|
|
910
1027
|
exports.MsgNativeTx = MsgNativeTx;
|
|
1028
|
+
exports.assetFromDenom = assetFromDenom;
|
|
911
1029
|
exports.buildDepositTx = buildDepositTx;
|
|
912
|
-
exports.getAsset = getAsset;
|
|
913
1030
|
exports.getBalance = getBalance;
|
|
914
1031
|
exports.getChainId = getChainId;
|
|
915
1032
|
exports.getDefaultClientUrl = getDefaultClientUrl;
|
|
916
1033
|
exports.getDefaultExplorerUrls = getDefaultExplorerUrls;
|
|
917
1034
|
exports.getDefaultFees = getDefaultFees;
|
|
918
1035
|
exports.getDenom = getDenom;
|
|
919
|
-
exports.getDenomWithChain = getDenomWithChain;
|
|
920
1036
|
exports.getDepositTxDataFromLogs = getDepositTxDataFromLogs;
|
|
921
1037
|
exports.getExplorerAddressUrl = getExplorerAddressUrl;
|
|
922
1038
|
exports.getExplorerTxUrl = getExplorerTxUrl;
|
|
923
1039
|
exports.getExplorerUrl = getExplorerUrl;
|
|
924
1040
|
exports.getPrefix = getPrefix;
|
|
925
1041
|
exports.getTxType = getTxType;
|
|
1042
|
+
exports.isAssetRuneNative = isAssetRuneNative;
|
|
926
1043
|
exports.isBroadcastSuccess = isBroadcastSuccess;
|
|
927
1044
|
exports.isMsgMultiSend = isMsgMultiSend;
|
|
928
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
|
-
*
|
|
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,15 @@ 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
63
|
/**
|
|
64
64
|
* Get the chain id.
|
|
65
65
|
*
|
|
66
|
+
* @param {Network} network
|
|
66
67
|
* @returns {string} The chain id based on the network.
|
|
68
|
+
*
|
|
67
69
|
*/
|
|
68
|
-
export declare const getChainId: () =>
|
|
70
|
+
export declare const getChainId: (network: Network) => "thorchain" | "thorchain-stagenet";
|
|
69
71
|
/**
|
|
70
72
|
* Register Codecs based on the prefix.
|
|
71
73
|
*
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xchainjs/xchain-thorchain",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.21.0",
|
|
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
|
}
|