@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/index.js CHANGED
@@ -4382,56 +4382,64 @@ var indexMinimal = {};
4382
4382
 
4383
4383
  var minimal$1 = {};
4384
4384
 
4385
- var aspromise = asPromise;
4385
+ var aspromise;
4386
+ var hasRequiredAspromise;
4387
+
4388
+ function requireAspromise () {
4389
+ if (hasRequiredAspromise) return aspromise;
4390
+ hasRequiredAspromise = 1;
4391
+ aspromise = asPromise;
4386
4392
 
4387
- /**
4388
- * Callback as used by {@link util.asPromise}.
4389
- * @typedef asPromiseCallback
4390
- * @type {function}
4391
- * @param {Error|null} error Error, if any
4392
- * @param {...*} params Additional arguments
4393
- * @returns {undefined}
4394
- */
4393
+ /**
4394
+ * Callback as used by {@link util.asPromise}.
4395
+ * @typedef asPromiseCallback
4396
+ * @type {function}
4397
+ * @param {Error|null} error Error, if any
4398
+ * @param {...*} params Additional arguments
4399
+ * @returns {undefined}
4400
+ */
4395
4401
 
4396
- /**
4397
- * Returns a promise from a node-style callback function.
4398
- * @memberof util
4399
- * @param {asPromiseCallback} fn Function to call
4400
- * @param {*} ctx Function context
4401
- * @param {...*} params Function arguments
4402
- * @returns {Promise<*>} Promisified function
4403
- */
4404
- function asPromise(fn, ctx/*, varargs */) {
4405
- var params = new Array(arguments.length - 1),
4406
- offset = 0,
4407
- index = 2,
4408
- pending = true;
4409
- while (index < arguments.length)
4410
- params[offset++] = arguments[index++];
4411
- return new Promise(function executor(resolve, reject) {
4412
- params[offset] = function callback(err/*, varargs */) {
4413
- if (pending) {
4414
- pending = false;
4415
- if (err)
4416
- reject(err);
4417
- else {
4418
- var params = new Array(arguments.length - 1),
4419
- offset = 0;
4420
- while (offset < params.length)
4421
- params[offset++] = arguments[offset];
4422
- resolve.apply(null, params);
4423
- }
4424
- }
4425
- };
4426
- try {
4427
- fn.apply(ctx || null, params);
4428
- } catch (err) {
4429
- if (pending) {
4430
- pending = false;
4431
- reject(err);
4432
- }
4433
- }
4434
- });
4402
+ /**
4403
+ * Returns a promise from a node-style callback function.
4404
+ * @memberof util
4405
+ * @param {asPromiseCallback} fn Function to call
4406
+ * @param {*} ctx Function context
4407
+ * @param {...*} params Function arguments
4408
+ * @returns {Promise<*>} Promisified function
4409
+ */
4410
+ function asPromise(fn, ctx/*, varargs */) {
4411
+ var params = new Array(arguments.length - 1),
4412
+ offset = 0,
4413
+ index = 2,
4414
+ pending = true;
4415
+ while (index < arguments.length)
4416
+ params[offset++] = arguments[index++];
4417
+ return new Promise(function executor(resolve, reject) {
4418
+ params[offset] = function callback(err/*, varargs */) {
4419
+ if (pending) {
4420
+ pending = false;
4421
+ if (err)
4422
+ reject(err);
4423
+ else {
4424
+ var params = new Array(arguments.length - 1),
4425
+ offset = 0;
4426
+ while (offset < params.length)
4427
+ params[offset++] = arguments[offset];
4428
+ resolve.apply(null, params);
4429
+ }
4430
+ }
4431
+ };
4432
+ try {
4433
+ fn.apply(ctx || null, params);
4434
+ } catch (err) {
4435
+ if (pending) {
4436
+ pending = false;
4437
+ reject(err);
4438
+ }
4439
+ }
4440
+ });
4441
+ }
4442
+ return aspromise;
4435
4443
  }
4436
4444
 
4437
4445
  var base64$1 = {};
@@ -4577,81 +4585,89 @@ var base64$1 = {};
4577
4585
  };
4578
4586
  } (base64$1));
4579
4587
 
4580
- var eventemitter = EventEmitter;
4588
+ var eventemitter;
4589
+ var hasRequiredEventemitter;
4590
+
4591
+ function requireEventemitter () {
4592
+ if (hasRequiredEventemitter) return eventemitter;
4593
+ hasRequiredEventemitter = 1;
4594
+ eventemitter = EventEmitter;
4581
4595
 
4582
- /**
4583
- * Constructs a new event emitter instance.
4584
- * @classdesc A minimal event emitter.
4585
- * @memberof util
4586
- * @constructor
4587
- */
4588
- function EventEmitter() {
4596
+ /**
4597
+ * Constructs a new event emitter instance.
4598
+ * @classdesc A minimal event emitter.
4599
+ * @memberof util
4600
+ * @constructor
4601
+ */
4602
+ function EventEmitter() {
4589
4603
 
4590
- /**
4591
- * Registered listeners.
4592
- * @type {Object.<string,*>}
4593
- * @private
4594
- */
4595
- this._listeners = {};
4596
- }
4604
+ /**
4605
+ * Registered listeners.
4606
+ * @type {Object.<string,*>}
4607
+ * @private
4608
+ */
4609
+ this._listeners = {};
4610
+ }
4597
4611
 
4598
- /**
4599
- * Registers an event listener.
4600
- * @param {string} evt Event name
4601
- * @param {function} fn Listener
4602
- * @param {*} [ctx] Listener context
4603
- * @returns {util.EventEmitter} `this`
4604
- */
4605
- EventEmitter.prototype.on = function on(evt, fn, ctx) {
4606
- (this._listeners[evt] || (this._listeners[evt] = [])).push({
4607
- fn : fn,
4608
- ctx : ctx || this
4609
- });
4610
- return this;
4611
- };
4612
+ /**
4613
+ * Registers an event listener.
4614
+ * @param {string} evt Event name
4615
+ * @param {function} fn Listener
4616
+ * @param {*} [ctx] Listener context
4617
+ * @returns {util.EventEmitter} `this`
4618
+ */
4619
+ EventEmitter.prototype.on = function on(evt, fn, ctx) {
4620
+ (this._listeners[evt] || (this._listeners[evt] = [])).push({
4621
+ fn : fn,
4622
+ ctx : ctx || this
4623
+ });
4624
+ return this;
4625
+ };
4612
4626
 
4613
- /**
4614
- * Removes an event listener or any matching listeners if arguments are omitted.
4615
- * @param {string} [evt] Event name. Removes all listeners if omitted.
4616
- * @param {function} [fn] Listener to remove. Removes all listeners of `evt` if omitted.
4617
- * @returns {util.EventEmitter} `this`
4618
- */
4619
- EventEmitter.prototype.off = function off(evt, fn) {
4620
- if (evt === undefined)
4621
- this._listeners = {};
4622
- else {
4623
- if (fn === undefined)
4624
- this._listeners[evt] = [];
4625
- else {
4626
- var listeners = this._listeners[evt];
4627
- for (var i = 0; i < listeners.length;)
4628
- if (listeners[i].fn === fn)
4629
- listeners.splice(i, 1);
4630
- else
4631
- ++i;
4632
- }
4633
- }
4634
- return this;
4635
- };
4627
+ /**
4628
+ * Removes an event listener or any matching listeners if arguments are omitted.
4629
+ * @param {string} [evt] Event name. Removes all listeners if omitted.
4630
+ * @param {function} [fn] Listener to remove. Removes all listeners of `evt` if omitted.
4631
+ * @returns {util.EventEmitter} `this`
4632
+ */
4633
+ EventEmitter.prototype.off = function off(evt, fn) {
4634
+ if (evt === undefined)
4635
+ this._listeners = {};
4636
+ else {
4637
+ if (fn === undefined)
4638
+ this._listeners[evt] = [];
4639
+ else {
4640
+ var listeners = this._listeners[evt];
4641
+ for (var i = 0; i < listeners.length;)
4642
+ if (listeners[i].fn === fn)
4643
+ listeners.splice(i, 1);
4644
+ else
4645
+ ++i;
4646
+ }
4647
+ }
4648
+ return this;
4649
+ };
4636
4650
 
4637
- /**
4638
- * Emits an event by calling its listeners with the specified arguments.
4639
- * @param {string} evt Event name
4640
- * @param {...*} args Arguments
4641
- * @returns {util.EventEmitter} `this`
4642
- */
4643
- EventEmitter.prototype.emit = function emit(evt) {
4644
- var listeners = this._listeners[evt];
4645
- if (listeners) {
4646
- var args = [],
4647
- i = 1;
4648
- for (; i < arguments.length;)
4649
- args.push(arguments[i++]);
4650
- for (i = 0; i < listeners.length;)
4651
- listeners[i].fn.apply(listeners[i++].ctx, args);
4652
- }
4653
- return this;
4654
- };
4651
+ /**
4652
+ * Emits an event by calling its listeners with the specified arguments.
4653
+ * @param {string} evt Event name
4654
+ * @param {...*} args Arguments
4655
+ * @returns {util.EventEmitter} `this`
4656
+ */
4657
+ EventEmitter.prototype.emit = function emit(evt) {
4658
+ var listeners = this._listeners[evt];
4659
+ if (listeners) {
4660
+ var args = [],
4661
+ i = 1;
4662
+ for (; i < arguments.length;)
4663
+ args.push(arguments[i++]);
4664
+ for (i = 0; i < listeners.length;)
4665
+ listeners[i].fn.apply(listeners[i++].ctx, args);
4666
+ }
4667
+ return this;
4668
+ };
4669
+ return eventemitter;
4670
+ }
4655
4671
 
4656
4672
  var float = factory(factory);
4657
4673
 
@@ -4987,29 +5003,21 @@ function readUintBE(buf, pos) {
4987
5003
  | buf[pos + 3]) >>> 0;
4988
5004
  }
4989
5005
 
4990
- var inquire_1;
4991
- var hasRequiredInquire;
4992
-
4993
- function requireInquire () {
4994
- if (hasRequiredInquire) return inquire_1;
4995
- hasRequiredInquire = 1;
4996
- inquire_1 = inquire;
5006
+ var inquire_1 = inquire;
4997
5007
 
4998
- /**
4999
- * Requires a module only if available.
5000
- * @memberof util
5001
- * @param {string} moduleName Module to require
5002
- * @returns {?Object} Required module if available and not empty, otherwise `null`
5003
- */
5004
- function inquire(moduleName) {
5005
- try {
5006
- var mod = eval("quire".replace(/^/,"re"))(moduleName); // eslint-disable-line no-eval
5007
- if (mod && (mod.length || Object.keys(mod).length))
5008
- return mod;
5009
- } catch (e) {} // eslint-disable-line no-empty
5010
- return null;
5011
- }
5012
- return inquire_1;
5008
+ /**
5009
+ * Requires a module only if available.
5010
+ * @memberof util
5011
+ * @param {string} moduleName Module to require
5012
+ * @returns {?Object} Required module if available and not empty, otherwise `null`
5013
+ */
5014
+ function inquire(moduleName) {
5015
+ try {
5016
+ var mod = eval("quire".replace(/^/,"re"))(moduleName); // eslint-disable-line no-eval
5017
+ if (mod && (mod.length || Object.keys(mod).length))
5018
+ return mod;
5019
+ } catch (e) {} // eslint-disable-line no-empty
5020
+ return null;
5013
5021
  }
5014
5022
 
5015
5023
  var utf8$2 = {};
@@ -5401,19 +5409,19 @@ function requireMinimal () {
5401
5409
  var util = exports;
5402
5410
 
5403
5411
  // used to return a Promise where callback is omitted
5404
- util.asPromise = aspromise;
5412
+ util.asPromise = requireAspromise();
5405
5413
 
5406
5414
  // converts to / from base64 encoded strings
5407
5415
  util.base64 = base64$1;
5408
5416
 
5409
5417
  // base class of rpc.Service
5410
- util.EventEmitter = eventemitter;
5418
+ util.EventEmitter = requireEventemitter();
5411
5419
 
5412
5420
  // float handling accross browsers
5413
5421
  util.float = float;
5414
5422
 
5415
5423
  // requires modules optionally and hides the call from bundlers
5416
- util.inquire = requireInquire();
5424
+ util.inquire = inquire_1;
5417
5425
 
5418
5426
  // converts to / from utf8 encoded strings
5419
5427
  util.utf8 = requireUtf8();
@@ -10701,7 +10709,8 @@ class Client extends xchainClient.BaseXChainClient {
10701
10709
  transfer({ walletIndex = 0, asset = AssetRuneNative, amount, recipient, memo, gasLimit = new BigNumber(DEFAULT_GAS_LIMIT_VALUE), sequence, }) {
10702
10710
  var _a, _b, _c, _d;
10703
10711
  return __awaiter(this, void 0, void 0, function* () {
10704
- const balances = yield this.getBalance(this.getAddress(walletIndex));
10712
+ const sender = this.getAddress(walletIndex);
10713
+ const balances = yield this.getBalance(sender);
10705
10714
  const runeBalance = (_b = (_a = balances.filter(({ asset }) => isAssetRuneNative(asset))[0]) === null || _a === void 0 ? void 0 : _a.amount) !== null && _b !== void 0 ? _b : xchainUtil.baseAmount(0, RUNE_DECIMAL);
10706
10715
  const assetBalance = (_d = (_c = balances.filter(({ asset: assetInList }) => xchainUtil.assetToString(assetInList) === xchainUtil.assetToString(asset))[0]) === null || _c === void 0 ? void 0 : _c.amount) !== null && _d !== void 0 ? _d : xchainUtil.baseAmount(0, RUNE_DECIMAL);
10707
10716
  const fee = (yield this.getFees()).average;
@@ -10717,35 +10726,25 @@ class Client extends xchainClient.BaseXChainClient {
10717
10726
  throw new Error('insufficient funds');
10718
10727
  }
10719
10728
  }
10720
- const privKey = this.getPrivateKey(walletIndex);
10721
- const from = this.getAddress(walletIndex);
10722
- const signerPubkey = privKey.pubKey();
10723
- const accAddress = cosmosclient__default["default"].AccAddress.fromString(from);
10724
- const denom = getDenom(asset);
10725
- const txBody = yield buildTransferTx({
10726
- fromAddress: from,
10727
- toAddress: recipient,
10728
- memo: memo,
10729
- assetAmount: amount,
10730
- assetDenom: denom,
10731
- chainId: this.getChainId(),
10732
- nodeUrl: this.getClientUrl().node,
10729
+ const unsignedTxData = yield this.prepareTx({
10730
+ sender,
10731
+ asset,
10732
+ amount,
10733
+ recipient,
10734
+ memo,
10735
+ gasLimit,
10736
+ sequence,
10733
10737
  });
10734
- const account = yield this.getCosmosClient().getAccount(accAddress);
10735
- const { account_number: accountNumber } = account;
10738
+ const decodedTx = cosmosclient__default["default"].proto.cosmos.tx.v1beta1.TxRaw.decode(Buffer.from(unsignedTxData.rawUnsignedTx, 'base64'));
10739
+ const txBuilder = new cosmosclient__default["default"].TxBuilder(this.getCosmosClient().sdk, cosmosclient__default["default"].proto.cosmos.tx.v1beta1.TxBody.decode(decodedTx.body_bytes), cosmosclient__default["default"].proto.cosmos.tx.v1beta1.AuthInfo.decode(decodedTx.auth_info_bytes));
10740
+ const { account_number: accountNumber } = yield this.getCosmosClient().getAccount(cosmosclient__default["default"].AccAddress.fromString(sender));
10736
10741
  if (!accountNumber)
10737
- throw Error(`Deposit failed - could not get account number ${accountNumber}`);
10738
- const txBuilder = buildUnsignedTx({
10739
- cosmosSdk: this.getCosmosClient().sdk,
10740
- txBody: txBody,
10741
- gasLimit: Long$1.fromString(gasLimit.toString()),
10742
- signerPubkey: cosmosclient__default["default"].codec.instanceToProtoAny(signerPubkey),
10743
- sequence: sequence ? Long$1.fromNumber(sequence) : account.sequence || Long$1.ZERO,
10744
- });
10745
- const txHash = yield this.cosmosClient.signAndBroadcast(txBuilder, privKey, accountNumber);
10746
- if (!txHash)
10747
- throw Error(`Invalid transaction hash: ${txHash}`);
10748
- return txHash;
10742
+ throw Error(`Transfer failed - missing account number`);
10743
+ const privKey = this.getCosmosClient().getPrivKeyFromMnemonic(this.phrase, this.getFullDerivationPath(walletIndex));
10744
+ const signDocBytes = txBuilder.signDocBytes(accountNumber);
10745
+ txBuilder.addSignature(privKey.sign(signDocBytes));
10746
+ const signedTx = txBuilder.txBytes();
10747
+ return this.broadcastTx(signedTx);
10749
10748
  });
10750
10749
  }
10751
10750
  broadcastTx(txHex) {
@@ -10756,6 +10755,7 @@ class Client extends xchainClient.BaseXChainClient {
10756
10755
  /**
10757
10756
  * Transfer without broadcast balances with MsgSend
10758
10757
  *
10758
+ * @deprecated use instead prepareTx
10759
10759
  * @param {TxOfflineParams} params The transfer offline options.
10760
10760
  * @returns {string} The signed transaction bytes.
10761
10761
  */
@@ -10815,6 +10815,42 @@ class Client extends xchainClient.BaseXChainClient {
10815
10815
  }
10816
10816
  });
10817
10817
  }
10818
+ /**
10819
+ * Prepare transfer.
10820
+ *
10821
+ * @param {TxParams&Address&BigNumber} params The transfer options.
10822
+ * @returns {PreparedTx} The raw unsigned transaction.
10823
+ */
10824
+ prepareTx({ sender, recipient, amount, memo, asset = AssetRuneNative, gasLimit = new BigNumber(DEFAULT_GAS_LIMIT_VALUE), sequence, }) {
10825
+ return __awaiter(this, void 0, void 0, function* () {
10826
+ if (!this.validateAddress(sender))
10827
+ throw Error('Invalid sender address');
10828
+ if (!this.validateAddress(recipient))
10829
+ throw Error('Invalid recipient address');
10830
+ const denom = getDenom(asset);
10831
+ const txBody = yield buildTransferTx({
10832
+ fromAddress: sender,
10833
+ toAddress: recipient,
10834
+ memo,
10835
+ assetAmount: amount,
10836
+ assetDenom: denom,
10837
+ chainId: this.getChainId(),
10838
+ nodeUrl: this.getClientUrl().node,
10839
+ });
10840
+ const account = yield this.getCosmosClient().getAccount(cosmosclient__default["default"].AccAddress.fromString(sender));
10841
+ const { pub_key: pubkey } = account;
10842
+ if (!pubkey)
10843
+ throw Error(`Transfer failed - missing pub key`);
10844
+ const txBuilder = buildUnsignedTx({
10845
+ cosmosSdk: this.getCosmosClient().sdk,
10846
+ txBody,
10847
+ gasLimit: Long$1.fromString(gasLimit.toString()),
10848
+ signerPubkey: pubkey,
10849
+ sequence: sequence ? Long$1.fromNumber(sequence) : account.sequence || Long$1.ZERO,
10850
+ });
10851
+ return { rawUnsignedTx: txBuilder.txBytes() };
10852
+ });
10853
+ }
10818
10854
  }
10819
10855
 
10820
10856
  class MsgNativeTx {