【问题标题】:How can I get rid of 504 timeout error on web3 when calling a smart contract method?调用智能合约方法时如何摆脱 web3 上的 504 超时错误?
【发布时间】:2022-02-17 00:43:01
【问题描述】:

我不知道该怎么做,我这样做增加了超时

var options = {
    timeout: 60000 * 2, // ms
    // Enable auto reconnection
    reconnect: {
        auto: true,
        delay: 5000, // ms
        maxAttempts: 5,
        onTimeout: false
    }
};
const web3 = new Web3( new Web3.providers.HttpProvider(web3Settings.provider, options));

但有时在调用合约函数时我仍然会收到 504 超时错误

2022-02-16T08:19:00.456908+00:00 app[web.1]: (node:22) UnhandledPromiseRejectionWarning: Error: Invalid JSON RPC response: "<html>\r\n<head><title>504 Gateway Time-out</title></head>\r\n<body>\r\n<center><h1>504 Gateway Time-out</h1></center>\r\n</body>\r\n</html>\r\n"
2022-02-16T08:19:00.456917+00:00 app[web.1]: at Object.InvalidResponse (/app/node_modules/web3-core-helpers/lib/errors.js:43:16)
2022-02-16T08:19:00.456918+00:00 app[web.1]: at XMLHttpRequest.request.onreadystatechange (/app/node_modules/web3-providers-http/lib/index.js:95:32)
2022-02-16T08:19:00.456918+00:00 app[web.1]: at XMLHttpRequestEventTarget.dispatchEvent (/app/node_modules/xhr2-cookies/dist/xml-http-request-event-target.js:34:22)
2022-02-16T08:19:00.456919+00:00 app[web.1]: at XMLHttpRequest._setReadyState (/app/node_modules/xhr2-cookies/dist/xml-http-request.js:208:14)
2022-02-16T08:19:00.456919+00:00 app[web.1]: at XMLHttpRequest._onHttpResponseEnd (/app/node_modules/xhr2-cookies/dist/xml-http-request.js:318:14)
2022-02-16T08:19:00.456919+00:00 app[web.1]: at IncomingMessage.<anonymous> (/app/node_modules/xhr2-cookies/dist/xml-http-request.js:289:61)

我正在使用 bsc 测试网网络。谢谢

【问题讨论】:

  • 你的智能合约在哪里?
  • 智能合约部署在 bsc 测试网上

标签: node.js ethereum solidity web3


【解决方案1】:

如果你在前端,我们应该使用初始化 web3

new Web3(window.ethereum);
await window.ethereum.enable();

如果您在后端,那么使用它们的 HttpProvider 是一个不错的选择。

你可以做一些事情:

  • 确保您使用的 rpc 有效。可能是指向错误的地址。
  • 尝试与其他 SC 交互以查看代码是否有效。

【讨论】:

  • 我在后端使用它。它有时会工作,有时会失败。现在还不稳定。它正在工作
  • 如果您在 bsctestnet 中进行测试,那没关系。它以不稳定而闻名。或者可以使用 Alchemy 或 Ganache 进行测试。它们在本地运行,因此您不会对这种 503 感到恼火。
  • 谢谢@PySoL
猜你喜欢
  • 2018-12-28
  • 2020-01-28
  • 2018-07-10
  • 2019-01-24
  • 2022-09-29
  • 1970-01-01
  • 2019-12-26
  • 2021-06-29
  • 2019-10-29
相关资源
最近更新 更多