From ee82e86ccc439825259d20825a00050217890ad3 Mon Sep 17 00:00:00 2001 From: Richard Moore Date: Thu, 10 Jun 2021 18:22:02 -0400 Subject: [PATCH] Fixed replacement transaction detection for JsonRpcSigner (#1658). --- .../providers/src.ts/json-rpc-provider.ts | 32 ++++++++++++------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/packages/providers/src.ts/json-rpc-provider.ts b/packages/providers/src.ts/json-rpc-provider.ts index cb374a1a8..c5d0b44e3 100644 --- a/packages/providers/src.ts/json-rpc-provider.ts +++ b/packages/providers/src.ts/json-rpc-provider.ts @@ -207,18 +207,26 @@ export class JsonRpcSigner extends Signer implements TypedDataSigner { }); } - sendTransaction(transaction: Deferrable): Promise { - return this.sendUncheckedTransaction(transaction).then((hash) => { - return poll(() => { - return this.provider.getTransaction(hash).then((tx: TransactionResponse) => { - if (tx === null) { return undefined; } - return this.provider._wrapTransaction(tx, hash); - }); - }, { oncePoll: this.provider }).catch((error: Error) => { - (error).transactionHash = hash; - throw error; - }); - }); + async sendTransaction(transaction: Deferrable): Promise { + // This cannot be mined any earlier than any recent block + const blockNumber = await this.provider._getInternalBlockNumber(100 + 2 * this.provider.pollingInterval); + + // Send the transaction + const hash = await this.sendUncheckedTransaction(transaction); + + try { + // Unfortunately, JSON-RPC only provides and opaque transaction hash + // for a response, and we need the actual transaction, so we poll + // for it; it should show up very quickly + return await poll(async () => { + const tx = await this.provider.getTransaction(hash); + if (tx === null) { return undefined; } + return this.provider._wrapTransaction(tx, hash, blockNumber); + }, { oncePoll: this.provider }); + } catch (error) { + (error).transactionHash = hash; + throw error; + } } async signMessage(message: Bytes | string): Promise {