【问题标题】:UnhandledPromiseRejectionWarning: Error: Returned error: execution revertedUnhandledPromiseRejectionWarning:错误:返回错误:执行恢复
【发布时间】:2021-09-21 11:19:54
【问题描述】:

这是我正在运行的代码,以获取我之前部署到币安智能链的合约余额:

let Web3 = require('web3');
const fs = require('fs');

let web3 = new Web3('https://data-seed-prebsc-1-s1.binance.org:8545');

const contractAddress = '0x43045f0Cec750eEb70478B023885d1956588438E';
const contractAbi = JSON.parse(fs.readFileSync("scripts/contract_abi.json").toString())
const contract = new web3.eth.Contract(contractAbi, contractAddress);

contract.methods.balanceOf(contractAddress).call().then(result=>console.log(result)).catch(err => console.log(err));

此代码向我抛出错误:

Error: Returned error: execution reverted
at Object.ErrorResponse (/home/zuber/Projects/HelloBSC/HelloCoin/node_modules/web3-core-helpers/lib/errors.js:28:19)
at /home/zuber/Projects/HelloBSC/HelloCoin/node_modules/web3-core-requestmanager/lib/index.js:303:36
at XMLHttpRequest.request.onreadystatechange (/home/zuber/Projects/HelloBSC/HelloCoin/node_modules/web3-providers-http/lib/index.js:98:13)
at XMLHttpRequestEventTarget.dispatchEvent (/home/zuber/Projects/HelloBSC/HelloCoin/node_modules/xhr2-cookies/dist/xml-http-request-event-target.js:34:22)
at XMLHttpRequest._setReadyState (/home/zuber/Projects/HelloBSC/HelloCoin/node_modules/xhr2-cookies/dist/xml-http-request.js:208:14)
at XMLHttpRequest._onHttpResponseEnd (/home/zuber/Projects/HelloBSC/HelloCoin/node_modules/xhr2-cookies/dist/xml-http-request.js:318:14)
at IncomingMessage.<anonymous> (/home/zuber/Projects/HelloBSC/HelloCoin/node_modules/xhr2-cookies/dist/xml-http-request.js:289:61)
at IncomingMessage.emit (events.js:387:35)
at endReadableNT (internal/streams/readable.js:1317:12)
at processTicksAndRejections (internal/process/task_queues.js:82:21) {
data: null}

合约从https://github.com/binance-chain/bsc-genesis-contract/blob/master/contracts/bep20_template/BEP20Token.template复制粘贴(仅在第332行添加onlyOwner修饰符)

用于将合约部署到 BSC 的 Truffle 配置:

const HDWalletProvider = require('@truffle/hdwallet-provider');
const fs = require('fs');
const mnemonic = fs.readFileSync(".secret").toString().trim();

module.exports = {
  networks: {
    development: {
      host: "127.0.0.1",     // Localhost (default: none)
      port: 8545,            // Standard BSC port (default: none)
      network_id: "*",       // Any network (default: none)
    },
    testnet: {
      provider: () => new HDWalletProvider(mnemonic, `https://data-seed-prebsc-1-s1.binance.org:8545`),
      network_id: 97,
      confirmations: 10,
      timeoutBlocks: 200,
      skipDryRun: true
    },
    bsc: {
      provider: () => new HDWalletProvider(mnemonic, `https://bsc-dataseed1.binance.org`),
      network_id: 56,
      confirmations: 10,
      timeoutBlocks: 200,
      skipDryRun: true
    },
  },

  // Set default mocha options here, use special reporters etc.
  mocha: {
    // timeout: 100000
  },

  // Configure your compilers
  compilers: {
    solc: {
      version: "0.5.16", // A version or constraint - Ex. "^0.5.0"
    }
  }
}

【问题讨论】:

  • 你能把let result = getBalance();改成getBalance().then(result =&gt; console.log(result)).catch(err =&gt; console.log(err))并发布输出吗
  • 更新代码和错误

标签: solidity smartcontracts truffle web3js binance-smart-chain


【解决方案1】:

这是源自智能合约的一般错误,当合约抛出未处理的异常时。

即使您没有发布contract 源代码,我们也可以从decompiled 代码中获得有关其内容的一些基本信息。

它表明没有balanceOf()(您正在尝试调用),并且fallback()(在您尝试调用不存在的函数时使用)总是抛出异常。


从这里开始,最可能的可能性是您打算部署一个不同的合约(包含 balanceOf() 函数)但错误地部署了这个。

或者如果你想获取合约地址的 BNB 余额(不是代币余额),你可以使用 web3 getBalance() 方法。示例:

const balance = await web3.eth.getBalance(contractAddress);

【讨论】:

  • 您好,感谢您的回复!你怎么知道反编译的代码不包含balanceOf?我提供了我部署的合约的源代码,它肯定包含balanceOf。这是它:github.com/binance-chain/bsc-genesis-contract/blob/master/…
  • @Vassily 点击链接下方 BscScan 上的“反编译字节码”。你会看到反编译的代码......由于它与评论中提供的源代码不同,我猜你只是部署了一个不正确的合约(可能来自同一个文件的不同合约)。
  • 你知道为什么 truffle 部署了错误的合约吗?我确保在 /contracts 目录中有完全相同的代码,但不是部署我的 BEP20 合约,而是部署基础合约
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-26
  • 1970-01-01
  • 2018-02-23
  • 2021-11-17
  • 1970-01-01
  • 2020-11-17
相关资源
最近更新 更多