ethers.js/packages/experimental/lib/nonce-manager.js

80 lines
3.4 KiB
JavaScript
Raw Permalink Normal View History

2019-05-14 18:48:48 -04:00
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
2021-03-07 18:24:04 -05:00
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
2019-05-14 18:48:48 -04:00
return extendStatics(d, b);
};
return function (d, b) {
2021-03-07 18:24:04 -05:00
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
2019-05-14 18:48:48 -04:00
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
2021-03-07 18:24:04 -05:00
exports.NonceManager = void 0;
2019-05-14 18:48:48 -04:00
var ethers_1 = require("ethers");
2019-11-20 18:57:38 +09:00
// @TODO: Keep a per-NonceManager pool of sent but unmined transactions for
// rebroadcasting, in case we overrun the transaction pool
2019-05-14 18:48:48 -04:00
var NonceManager = /** @class */ (function (_super) {
__extends(NonceManager, _super);
function NonceManager(signer) {
2022-05-12 17:30:28 -04:00
var _this = _super.call(this) || this;
2019-11-20 18:57:38 +09:00
_this._deltaCount = 0;
2019-05-14 18:48:48 -04:00
ethers_1.ethers.utils.defineReadOnly(_this, "signer", signer);
2021-03-07 18:24:04 -05:00
ethers_1.ethers.utils.defineReadOnly(_this, "provider", signer.provider || null);
2019-05-14 18:48:48 -04:00
return _this;
}
NonceManager.prototype.connect = function (provider) {
return new NonceManager(this.signer.connect(provider));
};
NonceManager.prototype.getAddress = function () {
return this.signer.getAddress();
};
NonceManager.prototype.getTransactionCount = function (blockTag) {
if (blockTag === "pending") {
2019-11-20 18:57:38 +09:00
if (!this._initialPromise) {
this._initialPromise = this.signer.getTransactionCount("pending");
2019-05-14 18:48:48 -04:00
}
2019-11-20 18:57:38 +09:00
var deltaCount_1 = this._deltaCount;
return this._initialPromise.then(function (initial) { return (initial + deltaCount_1); });
2019-05-14 18:48:48 -04:00
}
return this.signer.getTransactionCount(blockTag);
};
NonceManager.prototype.setTransactionCount = function (transactionCount) {
2019-11-20 18:57:38 +09:00
this._initialPromise = Promise.resolve(transactionCount).then(function (nonce) {
2019-05-14 18:48:48 -04:00
return ethers_1.ethers.BigNumber.from(nonce).toNumber();
});
2019-11-20 18:57:38 +09:00
this._deltaCount = 0;
2019-05-14 18:48:48 -04:00
};
NonceManager.prototype.incrementTransactionCount = function (count) {
2022-06-17 02:02:09 -04:00
this._deltaCount += ((count == null) ? 1 : count);
2019-05-14 18:48:48 -04:00
};
NonceManager.prototype.signMessage = function (message) {
return this.signer.signMessage(message);
;
};
NonceManager.prototype.signTransaction = function (transaction) {
return this.signer.signTransaction(transaction);
};
NonceManager.prototype.sendTransaction = function (transaction) {
if (transaction.nonce == null) {
transaction = ethers_1.ethers.utils.shallowCopy(transaction);
2019-11-20 18:57:38 +09:00
transaction.nonce = this.getTransactionCount("pending");
this.incrementTransactionCount();
2019-05-14 18:48:48 -04:00
}
2019-11-20 18:57:38 +09:00
else {
this.setTransactionCount(transaction.nonce);
2022-06-17 02:02:09 -04:00
this._deltaCount++;
2019-11-20 18:57:38 +09:00
}
return this.signer.sendTransaction(transaction).then(function (tx) {
return tx;
});
2019-05-14 18:48:48 -04:00
};
return NonceManager;
}(ethers_1.ethers.Signer));
exports.NonceManager = NonceManager;
2020-07-13 08:03:56 -04:00
//# sourceMappingURL=nonce-manager.js.map