55 lines
1.8 KiB
JavaScript
55 lines
1.8 KiB
JavaScript
|
"use strict";
|
||
|
import { ethers } from "ethers";
|
||
|
import { version } from "./_version";
|
||
|
const logger = new ethers.utils.Logger(version);
|
||
|
export class NonceManager extends ethers.Signer {
|
||
|
constructor(signer) {
|
||
|
logger.checkNew(new.target, NonceManager);
|
||
|
super();
|
||
|
ethers.utils.defineReadOnly(this, "signer", signer);
|
||
|
}
|
||
|
connect(provider) {
|
||
|
return new NonceManager(this.signer.connect(provider));
|
||
|
}
|
||
|
getAddress() {
|
||
|
return this.signer.getAddress();
|
||
|
}
|
||
|
getTransactionCount(blockTag) {
|
||
|
if (blockTag === "pending") {
|
||
|
if (!this._transactionCount) {
|
||
|
this._transactionCount = this.signer.getTransactionCount("pending");
|
||
|
}
|
||
|
return this._transactionCount;
|
||
|
}
|
||
|
return this.signer.getTransactionCount(blockTag);
|
||
|
}
|
||
|
setTransactionCount(transactionCount) {
|
||
|
this._transactionCount = Promise.resolve(transactionCount).then((nonce) => {
|
||
|
return ethers.BigNumber.from(nonce).toNumber();
|
||
|
});
|
||
|
}
|
||
|
incrementTransactionCount(count) {
|
||
|
if (!count) {
|
||
|
count = 1;
|
||
|
}
|
||
|
this._transactionCount = this.getTransactionCount("pending").then((nonce) => {
|
||
|
return nonce + count;
|
||
|
});
|
||
|
}
|
||
|
signMessage(message) {
|
||
|
return this.signer.signMessage(message);
|
||
|
;
|
||
|
}
|
||
|
signTransaction(transaction) {
|
||
|
return this.signer.signTransaction(transaction);
|
||
|
}
|
||
|
sendTransaction(transaction) {
|
||
|
if (transaction.nonce == null) {
|
||
|
transaction = ethers.utils.shallowCopy(transaction);
|
||
|
transaction.nonce = this.getTransactionCount();
|
||
|
}
|
||
|
this.setTransactionCount(transaction.nonce);
|
||
|
return this.signer.sendTransaction(transaction);
|
||
|
}
|
||
|
}
|