首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我使用ethers.js发送一个原始事务,但是它失败了

我使用ethers.js发送一个原始事务,但是它失败了
EN

Ethereum用户
提问于 2023-02-15 16:47:20
回答 1查看 238关注 0票数 1
代码语言:javascript
运行
复制
const {ethers} = require("ethers");
require("dotenv").config();

async function main() {
    const pairaddr = "0xc7c3cCCE4FA25700fD5574DA7E200ae28BBd36A3";
    const pairabi = ["function getReserves() public view returns (uint112 _reserve0, uint112 _reserve1, uint32 _blockTimestampLast)"];
    const provider = new ethers.providers.JsonRpcProvider(`${process.env.BSC_RPC_URL}`);
    const mnemonic = new ethers.Wallet.createRandom();
    const wallet = mnemonic.connect(provider);
    const iface = new ethers.utils.Interface(pairabi);
    const reservesdata = iface.encodeFunctionData("getReserves", []);
    const tx = {
        to: pairaddr,
        data: reservesdata
    };
    const txReceipt = await wallet.sendTransaction(tx);
    console.log(txReceipt);
}

main();

但我发现了一个错误:

代码语言:javascript
运行
复制
var error = new Error(message);
<ref *1> Error: cannot estimate gas; transaction may fail or may require manual gas limit [ See: https://links.ethers.org/v5-errors-UNPREDICTABLE_GAS_LIMIT ]
 (error={"reason":"cannot estimate gas; transaction may fail or may require manual gas limit","code":"UNPREDICTABLE_GAS_LIMIT","error":{"reason":"processing 
response error","code":"SERVER_ERROR","body":"{\"jsonrpc\":\"2.0\",\"id\":52,\"error\":{\"code\":-32000,\"message\":\"gas required exceeds allowance (0)\"}}"
,"error":{"code":-32000},"requestBody":"{\"method\":\"eth_estimateGas\",\"params\":[{\"gasPrice\":\"0x12a05f200\",\"from\":\"0x116667f6a79605c3da16884a91cf2a
72d9901bf5\",\"to\":\"0xc7c3ccce4fa25700fd5574da7e200ae28bbd36a3\",\"data\":\"0x0902f1ac\"}],\"id\":52,\"jsonrpc\":\"2.0\"}","requestMethod":"POST","url":"ht
tps://bsc-dataseed1.defibit.io"},"method":"estimateGas","transaction":{"from":"0x116667F6A79605c3Da16884a91CF2A72D9901bf5","gasPrice":{"type":"BigNumber","he
x":"0x012a05f200"},"to":"0xc7c3cCCE4FA25700fD5574DA7E200ae28BBd36A3","data":"0x0902f1ac","accessList":null}}, tx={"to":{},"data":"0x0902f1ac","from":"0x11666
7F6A79605c3Da16884a91CF2A72D9901bf5","gasPrice":{"type":"BigNumber","hex":"0x012a05f200"},"type":0,"nonce":{},"gasLimit":{},"chainId":{}}, code=UNPREDICTABLE
_GAS_LIMIT, version=abstract-signer/5.7.0)
    at Logger.makeError (E:\SolidityProject\just-for-test\node_modules\@ethersproject\logger\lib\index.js:238:21)
    at Logger.throwError (E:\SolidityProject\just-for-test\node_modules\@ethersproject\logger\lib\index.js:247:20)
    at E:\SolidityProject\just-for-test\node_modules\@ethersproject\abstract-signer\lib\index.js:365:47
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async Promise.all (index 6) {
  reason: 'cannot estimate gas; transaction may fail or may require manual gas limit',
  code: 'UNPREDICTABLE_GAS_LIMIT',
  error: Error: cannot estimate gas; transaction may fail or may require manual gas limit [ See: https://links.ethers.org/v5-errors-UNPREDICTABLE_GAS_LIMIT ]
 (error={"reason":"processing response error","code":"SERVER_ERROR","body":"{\"jsonrpc\":\"2.0\",\"id\":52,\"error\":{\"code\":-32000,\"message\":\"gas requi
red exceeds allowance (0)\"}}","error":{"code":-32000},"requestBody":"{\"method\":\"eth_estimateGas\",\"params\":[{\"gasPrice\":\"0x12a05f200\",\"from\":\"0x
116667f6a79605c3da16884a91cf2a72d9901bf5\",\"to\":\"0xc7c3ccce4fa25700fd5574da7e200ae28bbd36a3\",\"data\":\"0x0902f1ac\"}],\"id\":52,\"jsonrpc\":\"2.0\"}","r
equestMethod":"POST","url":"https://bsc-dataseed1.defibit.io"}, method="estimateGas", transaction={"from":"0x116667F6A79605c3Da16884a91CF2A72D9901bf5","gasPr
ice":{"type":"BigNumber","hex":"0x012a05f200"},"to":"0xc7c3cCCE4FA25700fD5574DA7E200ae28BBd36A3","data":"0x0902f1ac","accessList":null}, code=UNPREDICTABLE_G
AS_LIMIT, version=providers/5.7.2)
      at Logger.makeError (E:\SolidityProject\just-for-test\node_modules\@ethersproject\logger\lib\index.js:238:21)
      at Logger.throwError (E:\SolidityProject\just-for-test\node_modules\@ethersproject\logger\lib\index.js:247:20)
      at checkError (E:\SolidityProject\just-for-test\node_modules\@ethersproject\providers\lib\json-rpc-provider.js:175:16)
      at JsonRpcProvider.<anonymous> (E:\SolidityProject\just-for-test\node_modules\@ethersproject\providers\lib\json-rpc-provider.js:751:47)
      at step (E:\SolidityProject\just-for-test\node_modules\@ethersproject\providers\lib\json-rpc-provider.js:48:23)
      at Object.throw (E:\SolidityProject\just-for-test\node_modules\@ethersproject\providers\lib\json-rpc-provider.js:29:53)
      at rejected (E:\SolidityProject\just-for-test\node_modules\@ethersproject\providers\lib\json-rpc-provider.js:21:65)
      at processTicksAndRejections (node:internal/process/task_queues:96:5) {
    reason: 'cannot estimate gas; transaction may fail or may require manual gas limit',
    code: 'UNPREDICTABLE_GAS_LIMIT',
    error: Error: processing response error (body="{\"jsonrpc\":\"2.0\",\"id\":52,\"error\":{\"code\":-32000,\"message\":\"gas required exceeds allowance (0)
\"}}", error={"code":-32000}, requestBody="{\"method\":\"eth_estimateGas\",\"params\":[{\"gasPrice\":\"0x12a05f200\",\"from\":\"0x116667f6a79605c3da16884a91c
f2a72d9901bf5\",\"to\":\"0xc7c3ccce4fa25700fd5574da7e200ae28bbd36a3\",\"data\":\"0x0902f1ac\"}],\"id\":52,\"jsonrpc\":\"2.0\"}", requestMethod="POST", url="h
ttps://bsc-dataseed1.defibit.io", code=SERVER_ERROR, version=web/5.7.1)
        at Logger.makeError (E:\SolidityProject\just-for-test\node_modules\@ethersproject\logger\lib\index.js:238:21)
        at Logger.throwError (E:\SolidityProject\just-for-test\node_modules\@ethersproject\logger\lib\index.js:247:20)
        at E:\SolidityProject\just-for-test\node_modules\@ethersproject\web\lib\index.js:313:32
        at step (E:\SolidityProject\just-for-test\node_modules\@ethersproject\web\lib\index.js:33:23)
        at Object.next (E:\SolidityProject\just-for-test\node_modules\@ethersproject\web\lib\index.js:14:53)
        at fulfilled (E:\SolidityProject\just-for-test\node_modules\@ethersproject\web\lib\index.js:5:58)
        at processTicksAndRejections (node:internal/process/task_queues:96:5) {
      reason: 'processing response error',
      code: 'SERVER_ERROR',
      body: '{"jsonrpc":"2.0","id":52,"error":{"code":-32000,"message":"gas required exceeds allowance (0)"}}',
      error: Error: gas required exceeds allowance (0)
          at getResult (E:\SolidityProject\just-for-test\node_modules\@ethersproject\providers\lib\json-rpc-provider.js:191:21)
          at processJsonFunc (E:\SolidityProject\just-for-test\node_modules\@ethersproject\web\lib\index.js:356:22)
          at E:\SolidityProject\just-for-test\node_modules\@ethersproject\web\lib\index.js:288:46
          at step (E:\SolidityProject\just-for-test\node_modules\@ethersproject\web\lib\index.js:33:23)
          at Object.next (E:\SolidityProject\just-for-test\node_modules\@ethersproject\web\lib\index.js:14:53)
          at fulfilled (E:\SolidityProject\just-for-test\node_modules\@ethersproject\web\lib\index.js:5:58)
          at processTicksAndRejections (node:internal/process/task_queues:96:5) {
        code: -32000,
        data: undefined
      },
      requestBody: '{"method":"eth_estimateGas","params":[{"gasPrice":"0x12a05f200","from":"0x116667f6a79605c3da16884a91cf2a72d9901bf5","to":"0xc7c3ccce4fa25
700fd5574da7e200ae28bbd36a3","data":"0x0902f1ac"}],"id":52,"jsonrpc":"2.0"}',
      requestMethod: 'POST',
      url: 'https://bsc-dataseed1.defibit.io'
    },
    method: 'estimateGas',
    transaction: {
      from: '0x116667F6A79605c3Da16884a91CF2A72D9901bf5',
      gasPrice: BigNumber { _hex: '0x012a05f200', _isBigNumber: true },
      to: '0xc7c3cCCE4FA25700fD5574DA7E200ae28BBd36A3',
      data: '0x0902f1ac',
      accessList: null
    }
  },
  tx: {
    to: Promise { '0xc7c3cCCE4FA25700fD5574DA7E200ae28BBd36A3' },
    data: '0x0902f1ac',
    from: '0x116667F6A79605c3Da16884a91CF2A72D9901bf5',
    gasPrice: BigNumber { _hex: '0x012a05f200', _isBigNumber: true },
    type: 0,
    nonce: Promise { 0 },
    gasLimit: Promise { <rejected> [Circular *1] },
    chainId: Promise { 56 }
  }
}

我该怎么办?

EN

回答 1

Ethereum用户

发布于 2023-02-15 17:15:12

试着手动估计气体:

代码语言:javascript
运行
复制
...
    const tx = {
      to: pairaddr,
      data: reservesdata,
      gas: 0
    };
    const gasLimit = await provider.estimateGas(tx);
    tx.gas=gasLimit;
...
票数 0
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/144984

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档