@xchainjs/xchain-thorchain 0.28.8 → 0.28.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/client.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import cosmosclient from '@cosmos-client/core';
2
- import { AssetInfo, Balance, BaseXChainClient, Fees, Network, Tx, TxHash, TxHistoryParams, TxParams, TxsPage, XChainClient, XChainClientParams } from '@xchainjs/xchain-client';
2
+ import { AssetInfo, Balance, BaseXChainClient, Fees, Network, PreparedTx, Tx, TxHash, TxHistoryParams, TxParams, TxsPage, XChainClient, XChainClientParams } from '@xchainjs/xchain-client';
3
3
  import { CosmosSDKClient, RPCTxResult } from '@xchainjs/xchain-cosmos';
4
4
  import { Address, Asset } from '@xchainjs/xchain-util';
5
5
  import BigNumber from 'bignumber.js';
@@ -220,6 +220,7 @@ declare class Client extends BaseXChainClient implements ThorchainClient, XChain
220
220
  /**
221
221
  * Transfer without broadcast balances with MsgSend
222
222
  *
223
+ * @deprecated use instead prepareTx
223
224
  * @param {TxOfflineParams} params The transfer offline options.
224
225
  * @returns {string} The signed transaction bytes.
225
226
  */
@@ -230,5 +231,16 @@ declare class Client extends BaseXChainClient implements ThorchainClient, XChain
230
231
  * @returns {Fees}
231
232
  */
232
233
  getFees(): Promise<Fees>;
234
+ /**
235
+ * Prepare transfer.
236
+ *
237
+ * @param {TxParams&Address&BigNumber} params The transfer options.
238
+ * @returns {PreparedTx} The raw unsigned transaction.
239
+ */
240
+ prepareTx({ sender, recipient, amount, memo, asset, gasLimit, sequence, }: TxParams & {
241
+ sender: Address;
242
+ gasLimit?: BigNumber;
243
+ sequence?: number;
244
+ }): Promise<PreparedTx>;
233
245
  }
234
246
  export { Client };
package/lib/index.esm.js CHANGED
@@ -4354,56 +4354,64 @@ var indexMinimal = {};
4354
4354
 
4355
4355
  var minimal$1 = {};
4356
4356
 
4357
- var aspromise = asPromise;
4357
+ var aspromise;
4358
+ var hasRequiredAspromise;
4359
+
4360
+ function requireAspromise () {
4361
+ if (hasRequiredAspromise) return aspromise;
4362
+ hasRequiredAspromise = 1;
4363
+ aspromise = asPromise;
4358
4364
 
4359
- /**
4360
- * Callback as used by {@link util.asPromise}.
4361
- * @typedef asPromiseCallback
4362
- * @type {function}
4363
- * @param {Error|null} error Error, if any
4364
- * @param {...*} params Additional arguments
4365
- * @returns {undefined}
4366
- */
4365
+ /**
4366
+ * Callback as used by {@link util.asPromise}.
4367
+ * @typedef asPromiseCallback
4368
+ * @type {function}
4369
+ * @param {Error|null} error Error, if any
4370
+ * @param {...*} params Additional arguments
4371
+ * @returns {undefined}
4372
+ */
4367
4373
 
4368
- /**
4369
- * Returns a promise from a node-style callback function.
4370
- * @memberof util
4371
- * @param {asPromiseCallback} fn Function to call
4372
- * @param {*} ctx Function context
4373
- * @param {...*} params Function arguments
4374
- * @returns {Promise<*>} Promisified function
4375
- */
4376
- function asPromise(fn, ctx/*, varargs */) {
4377
- var params = new Array(arguments.length - 1),
4378
- offset = 0,
4379
- index = 2,
4380
- pending = true;
4381
- while (index < arguments.length)
4382
- params[offset++] = arguments[index++];
4383
- return new Promise(function executor(resolve, reject) {
4384
- params[offset] = function callback(err/*, varargs */) {
4385
- if (pending) {
4386
- pending = false;
4387
- if (err)
4388
- reject(err);
4389
- else {
4390
- var params = new Array(arguments.length - 1),
4391
- offset = 0;
4392
- while (offset < params.length)
4393
- params[offset++] = arguments[offset];
4394
- resolve.apply(null, params);
4395
- }
4396
- }
4397
- };
4398
- try {
4399
- fn.apply(ctx || null, params);
4400
- } catch (err) {
4401
- if (pending) {
4402
- pending = false;
4403
- reject(err);
4404
- }
4405
- }
4406
- });
4374
+ /**
4375
+ * Returns a promise from a node-style callback function.
4376
+ * @memberof util
4377
+ * @param {asPromiseCallback} fn Function to call
4378
+ * @param {*} ctx Function context
4379
+ * @param {...*} params Function arguments
4380
+ * @returns {Promise<*>} Promisified function
4381
+ */
4382
+ function asPromise(fn, ctx/*, varargs */) {
4383
+ var params = new Array(arguments.length - 1),
4384
+ offset = 0,
4385
+ index = 2,
4386
+ pending = true;
4387
+ while (index < arguments.length)
4388
+ params[offset++] = arguments[index++];
4389
+ return new Promise(function executor(resolve, reject) {
4390
+ params[offset] = function callback(err/*, varargs */) {
4391
+ if (pending) {
4392
+ pending = false;
4393
+ if (err)
4394
+ reject(err);
4395
+ else {
4396
+ var params = new Array(arguments.length - 1),
4397
+ offset = 0;
4398
+ while (offset < params.length)
4399
+ params[offset++] = arguments[offset];
4400
+ resolve.apply(null, params);
4401
+ }
4402
+ }
4403
+ };
4404
+ try {
4405
+ fn.apply(ctx || null, params);
4406
+ } catch (err) {
4407
+ if (pending) {
4408
+ pending = false;
4409
+ reject(err);
4410
+ }
4411
+ }
4412
+ });
4413
+ }
4414
+ return aspromise;
4407
4415
  }
4408
4416
 
4409
4417
  var base64$1 = {};
@@ -4549,81 +4557,89 @@ var base64$1 = {};
4549
4557
  };
4550
4558
  } (base64$1));
4551
4559
 
4552
- var eventemitter = EventEmitter;
4560
+ var eventemitter;
4561
+ var hasRequiredEventemitter;
4562
+
4563
+ function requireEventemitter () {
4564
+ if (hasRequiredEventemitter) return eventemitter;
4565
+ hasRequiredEventemitter = 1;
4566
+ eventemitter = EventEmitter;
4553
4567
 
4554
- /**
4555
- * Constructs a new event emitter instance.
4556
- * @classdesc A minimal event emitter.
4557
- * @memberof util
4558
- * @constructor
4559
- */
4560
- function EventEmitter() {
4568
+ /**
4569
+ * Constructs a new event emitter instance.
4570
+ * @classdesc A minimal event emitter.
4571
+ * @memberof util
4572
+ * @constructor
4573
+ */
4574
+ function EventEmitter() {
4561
4575
 
4562
- /**
4563
- * Registered listeners.
4564
- * @type {Object.<string,*>}
4565
- * @private
4566
- */
4567
- this._listeners = {};
4568
- }
4576
+ /**
4577
+ * Registered listeners.
4578
+ * @type {Object.<string,*>}
4579
+ * @private
4580
+ */
4581
+ this._listeners = {};
4582
+ }
4569
4583
 
4570
- /**
4571
- * Registers an event listener.
4572
- * @param {string} evt Event name
4573
- * @param {function} fn Listener
4574
- * @param {*} [ctx] Listener context
4575
- * @returns {util.EventEmitter} `this`
4576
- */
4577
- EventEmitter.prototype.on = function on(evt, fn, ctx) {
4578
- (this._listeners[evt] || (this._listeners[evt] = [])).push({
4579
- fn : fn,
4580
- ctx : ctx || this
4581
- });
4582
- return this;
4583
- };
4584
+ /**
4585
+ * Registers an event listener.
4586
+ * @param {string} evt Event name
4587
+ * @param {function} fn Listener
4588
+ * @param {*} [ctx] Listener context
4589
+ * @returns {util.EventEmitter} `this`
4590
+ */
4591
+ EventEmitter.prototype.on = function on(evt, fn, ctx) {
4592
+ (this._listeners[evt] || (this._listeners[evt] = [])).push({
4593
+ fn : fn,
4594
+ ctx : ctx || this
4595
+ });
4596
+ return this;
4597
+ };
4584
4598
 
4585
- /**
4586
- * Removes an event listener or any matching listeners if arguments are omitted.
4587
- * @param {string} [evt] Event name. Removes all listeners if omitted.
4588
- * @param {function} [fn] Listener to remove. Removes all listeners of `evt` if omitted.
4589
- * @returns {util.EventEmitter} `this`
4590
- */
4591
- EventEmitter.prototype.off = function off(evt, fn) {
4592
- if (evt === undefined)
4593
- this._listeners = {};
4594
- else {
4595
- if (fn === undefined)
4596
- this._listeners[evt] = [];
4597
- else {
4598
- var listeners = this._listeners[evt];
4599
- for (var i = 0; i < listeners.length;)
4600
- if (listeners[i].fn === fn)
4601
- listeners.splice(i, 1);
4602
- else
4603
- ++i;
4604
- }
4605
- }
4606
- return this;
4607
- };
4599
+ /**
4600
+ * Removes an event listener or any matching listeners if arguments are omitted.
4601
+ * @param {string} [evt] Event name. Removes all listeners if omitted.
4602
+ * @param {function} [fn] Listener to remove. Removes all listeners of `evt` if omitted.
4603
+ * @returns {util.EventEmitter} `this`
4604
+ */
4605
+ EventEmitter.prototype.off = function off(evt, fn) {
4606
+ if (evt === undefined)
4607
+ this._listeners = {};
4608
+ else {
4609
+ if (fn === undefined)
4610
+ this._listeners[evt] = [];
4611
+ else {
4612
+ var listeners = this._listeners[evt];
4613
+ for (var i = 0; i < listeners.length;)
4614
+ if (listeners[i].fn === fn)
4615
+ listeners.splice(i, 1);
4616
+ else
4617
+ ++i;
4618
+ }
4619
+ }
4620
+ return this;
4621
+ };
4608
4622
 
4609
- /**
4610
- * Emits an event by calling its listeners with the specified arguments.
4611
- * @param {string} evt Event name
4612
- * @param {...*} args Arguments
4613
- * @returns {util.EventEmitter} `this`
4614
- */
4615
- EventEmitter.prototype.emit = function emit(evt) {
4616
- var listeners = this._listeners[evt];
4617
- if (listeners) {
4618
- var args = [],
4619
- i = 1;
4620
- for (; i < arguments.length;)
4621
- args.push(arguments[i++]);
4622
- for (i = 0; i < listeners.length;)
4623
- listeners[i].fn.apply(listeners[i++].ctx, args);
4624
- }
4625
- return this;
4626
- };
4623
+ /**
4624
+ * Emits an event by calling its listeners with the specified arguments.
4625
+ * @param {string} evt Event name
4626
+ * @param {...*} args Arguments
4627
+ * @returns {util.EventEmitter} `this`
4628
+ */
4629
+ EventEmitter.prototype.emit = function emit(evt) {
4630
+ var listeners = this._listeners[evt];
4631
+ if (listeners) {
4632
+ var args = [],
4633
+ i = 1;
4634
+ for (; i < arguments.length;)
4635
+ args.push(arguments[i++]);
4636
+ for (i = 0; i < listeners.length;)
4637
+ listeners[i].fn.apply(listeners[i++].ctx, args);
4638
+ }
4639
+ return this;
4640
+ };
4641
+ return eventemitter;
4642
+ }
4627
4643
 
4628
4644
  var float = factory(factory);
4629
4645
 
@@ -4959,29 +4975,21 @@ function readUintBE(buf, pos) {
4959
4975
  | buf[pos + 3]) >>> 0;
4960
4976
  }
4961
4977
 
4962
- var inquire_1;
4963
- var hasRequiredInquire;
4964
-
4965
- function requireInquire () {
4966
- if (hasRequiredInquire) return inquire_1;
4967
- hasRequiredInquire = 1;
4968
- inquire_1 = inquire;
4978
+ var inquire_1 = inquire;
4969
4979
 
4970
- /**
4971
- * Requires a module only if available.
4972
- * @memberof util
4973
- * @param {string} moduleName Module to require
4974
- * @returns {?Object} Required module if available and not empty, otherwise `null`
4975
- */
4976
- function inquire(moduleName) {
4977
- try {
4978
- var mod = eval("quire".replace(/^/,"re"))(moduleName); // eslint-disable-line no-eval
4979
- if (mod && (mod.length || Object.keys(mod).length))
4980
- return mod;
4981
- } catch (e) {} // eslint-disable-line no-empty
4982
- return null;
4983
- }
4984
- return inquire_1;
4980
+ /**
4981
+ * Requires a module only if available.
4982
+ * @memberof util
4983
+ * @param {string} moduleName Module to require
4984
+ * @returns {?Object} Required module if available and not empty, otherwise `null`
4985
+ */
4986
+ function inquire(moduleName) {
4987
+ try {
4988
+ var mod = eval("quire".replace(/^/,"re"))(moduleName); // eslint-disable-line no-eval
4989
+ if (mod && (mod.length || Object.keys(mod).length))
4990
+ return mod;
4991
+ } catch (e) {} // eslint-disable-line no-empty
4992
+ return null;
4985
4993
  }
4986
4994
 
4987
4995
  var utf8$2 = {};
@@ -5373,19 +5381,19 @@ function requireMinimal () {
5373
5381
  var util = exports;
5374
5382
 
5375
5383
  // used to return a Promise where callback is omitted
5376
- util.asPromise = aspromise;
5384
+ util.asPromise = requireAspromise();
5377
5385
 
5378
5386
  // converts to / from base64 encoded strings
5379
5387
  util.base64 = base64$1;
5380
5388
 
5381
5389
  // base class of rpc.Service
5382
- util.EventEmitter = eventemitter;
5390
+ util.EventEmitter = requireEventemitter();
5383
5391
 
5384
5392
  // float handling accross browsers
5385
5393
  util.float = float;
5386
5394
 
5387
5395
  // requires modules optionally and hides the call from bundlers
5388
- util.inquire = requireInquire();
5396
+ util.inquire = inquire_1;
5389
5397
 
5390
5398
  // converts to / from utf8 encoded strings
5391
5399
  util.utf8 = requireUtf8();
@@ -10673,7 +10681,8 @@ class Client extends BaseXChainClient {
10673
10681
  transfer({ walletIndex = 0, asset = AssetRuneNative, amount, recipient, memo, gasLimit = new BigNumber(DEFAULT_GAS_LIMIT_VALUE), sequence, }) {
10674
10682
  var _a, _b, _c, _d;
10675
10683
  return __awaiter(this, void 0, void 0, function* () {
10676
- const balances = yield this.getBalance(this.getAddress(walletIndex));
10684
+ const sender = this.getAddress(walletIndex);
10685
+ const balances = yield this.getBalance(sender);
10677
10686
  const runeBalance = (_b = (_a = balances.filter(({ asset }) => isAssetRuneNative(asset))[0]) === null || _a === void 0 ? void 0 : _a.amount) !== null && _b !== void 0 ? _b : baseAmount(0, RUNE_DECIMAL);
10678
10687
  const assetBalance = (_d = (_c = balances.filter(({ asset: assetInList }) => assetToString(assetInList) === assetToString(asset))[0]) === null || _c === void 0 ? void 0 : _c.amount) !== null && _d !== void 0 ? _d : baseAmount(0, RUNE_DECIMAL);
10679
10688
  const fee = (yield this.getFees()).average;
@@ -10689,35 +10698,25 @@ class Client extends BaseXChainClient {
10689
10698
  throw new Error('insufficient funds');
10690
10699
  }
10691
10700
  }
10692
- const privKey = this.getPrivateKey(walletIndex);
10693
- const from = this.getAddress(walletIndex);
10694
- const signerPubkey = privKey.pubKey();
10695
- const accAddress = cosmosclient.AccAddress.fromString(from);
10696
- const denom = getDenom(asset);
10697
- const txBody = yield buildTransferTx({
10698
- fromAddress: from,
10699
- toAddress: recipient,
10700
- memo: memo,
10701
- assetAmount: amount,
10702
- assetDenom: denom,
10703
- chainId: this.getChainId(),
10704
- nodeUrl: this.getClientUrl().node,
10701
+ const unsignedTxData = yield this.prepareTx({
10702
+ sender,
10703
+ asset,
10704
+ amount,
10705
+ recipient,
10706
+ memo,
10707
+ gasLimit,
10708
+ sequence,
10705
10709
  });
10706
- const account = yield this.getCosmosClient().getAccount(accAddress);
10707
- const { account_number: accountNumber } = account;
10710
+ const decodedTx = cosmosclient.proto.cosmos.tx.v1beta1.TxRaw.decode(Buffer.from(unsignedTxData.rawUnsignedTx, 'base64'));
10711
+ const txBuilder = new cosmosclient.TxBuilder(this.getCosmosClient().sdk, cosmosclient.proto.cosmos.tx.v1beta1.TxBody.decode(decodedTx.body_bytes), cosmosclient.proto.cosmos.tx.v1beta1.AuthInfo.decode(decodedTx.auth_info_bytes));
10712
+ const { account_number: accountNumber } = yield this.getCosmosClient().getAccount(cosmosclient.AccAddress.fromString(sender));
10708
10713
  if (!accountNumber)
10709
- throw Error(`Deposit failed - could not get account number ${accountNumber}`);
10710
- const txBuilder = buildUnsignedTx({
10711
- cosmosSdk: this.getCosmosClient().sdk,
10712
- txBody: txBody,
10713
- gasLimit: Long$1.fromString(gasLimit.toString()),
10714
- signerPubkey: cosmosclient.codec.instanceToProtoAny(signerPubkey),
10715
- sequence: sequence ? Long$1.fromNumber(sequence) : account.sequence || Long$1.ZERO,
10716
- });
10717
- const txHash = yield this.cosmosClient.signAndBroadcast(txBuilder, privKey, accountNumber);
10718
- if (!txHash)
10719
- throw Error(`Invalid transaction hash: ${txHash}`);
10720
- return txHash;
10714
+ throw Error(`Transfer failed - missing account number`);
10715
+ const privKey = this.getCosmosClient().getPrivKeyFromMnemonic(this.phrase, this.getFullDerivationPath(walletIndex));
10716
+ const signDocBytes = txBuilder.signDocBytes(accountNumber);
10717
+ txBuilder.addSignature(privKey.sign(signDocBytes));
10718
+ const signedTx = txBuilder.txBytes();
10719
+ return this.broadcastTx(signedTx);
10721
10720
  });
10722
10721
  }
10723
10722
  broadcastTx(txHex) {
@@ -10728,6 +10727,7 @@ class Client extends BaseXChainClient {
10728
10727
  /**
10729
10728
  * Transfer without broadcast balances with MsgSend
10730
10729
  *
10730
+ * @deprecated use instead prepareTx
10731
10731
  * @param {TxOfflineParams} params The transfer offline options.
10732
10732
  * @returns {string} The signed transaction bytes.
10733
10733
  */
@@ -10787,6 +10787,42 @@ class Client extends BaseXChainClient {
10787
10787
  }
10788
10788
  });
10789
10789
  }
10790
+ /**
10791
+ * Prepare transfer.
10792
+ *
10793
+ * @param {TxParams&Address&BigNumber} params The transfer options.
10794
+ * @returns {PreparedTx} The raw unsigned transaction.
10795
+ */
10796
+ prepareTx({ sender, recipient, amount, memo, asset = AssetRuneNative, gasLimit = new BigNumber(DEFAULT_GAS_LIMIT_VALUE), sequence, }) {
10797
+ return __awaiter(this, void 0, void 0, function* () {
10798
+ if (!this.validateAddress(sender))
10799
+ throw Error('Invalid sender address');
10800
+ if (!this.validateAddress(recipient))
10801
+ throw Error('Invalid recipient address');
10802
+ const denom = getDenom(asset);
10803
+ const txBody = yield buildTransferTx({
10804
+ fromAddress: sender,
10805
+ toAddress: recipient,
10806
+ memo,
10807
+ assetAmount: amount,
10808
+ assetDenom: denom,
10809
+ chainId: this.getChainId(),
10810
+ nodeUrl: this.getClientUrl().node,
10811
+ });
10812
+ const account = yield this.getCosmosClient().getAccount(cosmosclient.AccAddress.fromString(sender));
10813
+ const { pub_key: pubkey } = account;
10814
+ if (!pubkey)
10815
+ throw Error(`Transfer failed - missing pub key`);
10816
+ const txBuilder = buildUnsignedTx({
10817
+ cosmosSdk: this.getCosmosClient().sdk,
10818
+ txBody,
10819
+ gasLimit: Long$1.fromString(gasLimit.toString()),
10820
+ signerPubkey: pubkey,
10821
+ sequence: sequence ? Long$1.fromNumber(sequence) : account.sequence || Long$1.ZERO,
10822
+ });
10823
+ return { rawUnsignedTx: txBuilder.txBytes() };
10824
+ });
10825
+ }
10790
10826
  }
10791
10827
 
10792
10828
  class MsgNativeTx {