3

排错:replacement fee too low

 2 years ago
source link: https://blog.dteam.top/posts/2022-02/replacement-fee-too-low.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

排错:replacement fee too low

胡键 Posted at — Feb 15, 2022 阅读

调用合约方法却得到以下的错误信息:

Error: replacement fee too low (error={"reason":"processing response error","code":"SERVER_ERROR","body":"{\"jsonrpc\":\"2.0\",\"id\":109,\"error\":{\"code\":-32000,\"message\":\"replacement transaction underpriced\"}}","error":{"code":-32000},"requestBody":"{\"method\":\"eth_sendRawTransaction\",\"params\":[\"...\"],\"id\":109,\"jsonrpc\":\"2.0\"}","requestMethod":"POST","url":"..."}, method="sendTransaction", transaction="...", code=REPLACEMENT_UNDERPRICED, version=providers/5.5.2)
    at Logger.makeError (../node_modules/@ethersproject/logger/lib/index.js:199:1)
    at Logger.throwError (../node_modules/@ethersproject/logger/lib/index.js:208:1)
    at checkError (../node_modules/@ethersproject/providers/lib/json-rpc-provider.js:111:1)
    at InfuraProvider.<anonymous> (../node_modules/@ethersproject/providers/lib/json-rpc-provider.js:659:1)
    at step (../node_modules/@ethersproject/providers/lib/json-rpc-provider.js:48:1)
    at Object.throw (../node_modules/@ethersproject/providers/lib/json-rpc-provider.js:29:1)
    at rejected (../node_modules/@ethersproject/providers/lib/json-rpc-provider.js:21:1)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

看似混乱的信息中(上面的错误消息已经做过必要的删减,但即使这样也让人看着头晕),其实已经有一个很明确的错误消息:replacement fee too low 。

这种错误是由于同一账户在较短时间内同时提交了两个 tx,导致前一个 tx 尚未来得及处理后一个 tx 就来了。这种情况下,一般就两种处理:cancel 或加速。

在 ethers 的这个 issue 链接中有对此的详细说明,请自行去了解。

显然这个问题跟并发相关,解决办法自然就是想办法使之串行化或者降低提交 tx 的速度,比如等待 tx 完成(见下例)之后再提交下一个 tx。至于具体解法,只能具体情况具体分析了。

const tx = await contract.writeMethod(...);
await tx.wait();

【附】若对 ethers.js 不熟,可以参见:


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK