通过以太币与 BSC 上的智能合约通信会给出 -32603 错误代码

Communicating with a Smart Contract on BSC over ethers gives -32603 error code

你好我是智能合约开发的新手,几天来我试图让它工作,但没有运气。我希望有一个人可以帮助我。我尝试与部署到 BSC 的智能合约进行通信 https://testnet.bscscan.com/address/0x2ED1c3c1Fc6646F321cf546a892684E946435CE9 请参阅下面的源代码。

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.7;

import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";

contract FundMe {

    mapping(address => uint256) public addressToAmountFunded;
    address[] public funders;
    address public owner;
    AggregatorV3Interface internal priceFeed;
    uint balance;
    
    // if you're following along with the freecodecamp video
    // Please see https://github.com/PatrickAlphaC/fund_me
    // to get the starting solidity contract code, it'll be slightly different than this!
    constructor(address _priceFeed) {
        priceFeed = AggregatorV3Interface(_priceFeed);
        owner = msg.sender;
    }

    function fund() public payable {
        uint256 mimimumUSD = 50 * 10**18;
        require(
            getConversionRate(msg.value) >= mimimumUSD,
            "You need to spend more ETH!"
        );
        addressToAmountFunded[msg.sender] += msg.value;
        balance += msg.value;
        funders.push(msg.sender);
    }

    function getVersion() public view returns (uint256) {
        return priceFeed.version();
    }

    function getPrice() public view returns (uint256) {
        (, int price, , , ) = priceFeed.latestRoundData();
        return uint256(price * 10000000000);
    }

    // 1000000000
    function getConversionRate(uint256 ethAmount)
        public
        view
        returns (uint256)
    {
        uint256 ethPrice = getPrice();
        uint256 ethAmountInUsd = (ethPrice * ethAmount) / 1000000000000000000;
        return ethAmountInUsd;
    }

    function getEntranceFee() public view returns (uint256) {
        // mimimumUSD
        uint256 mimimumUSD = 50 * 10**18;
        uint256 price = getPrice();
        uint256 precision = 1 * 10**18;
        return (mimimumUSD * precision) / price;
    }

    modifier onlyOwner() {
        require(msg.sender == owner);
        _;
    }

    function withdraw() public payable onlyOwner {
        payable(msg.sender).transfer(balance);

        for (
            uint256 funderIndex = 0;
            funderIndex < funders.length;
            funderIndex++
        ) {
            address funder = funders[funderIndex];
            addressToAmountFunded[funder] = 0;
        }
        funders = new address[](0);
    }
}

我使用 truffle 部署了智能合约,使用以下迁移脚本

const FundMe = artifacts.require("FundMe");
const BINANCE_BNB_USD_PRICE_FEED = '0x0567F2323251f0Aab15c8dFb1967E4e8A7D42aeE';

module.exports = async (deployer, network, [defaultAccount]) => {

  let priceFeedAddress = BINANCE_BNB_USD_PRICE_FEED
  try {
      await deployer.deploy(FundMe, BINANCE_BNB_USD_PRICE_FEED, { from: defaultAccount })
  } catch (err) {
      console.error(err)
  }
}

我尝试调用与 chainlink 通信的 getPrice() 以获取 BNB/USDT 的最新价格。

这是Javascript

const getContract = () =>
  new Promise(async (resolve, reject) => {
    const contract = await fetch('./build/contracts/FundMe.json')
    const Contract = await contract.json()

    let provider = await detectEthereumProvider()
    if (provider) {
      await provider.request({ method: 'eth_requestAccounts' })
      const networkId = await provider.request({ method: 'net_version' })
      
      provider = new ethers.providers.Web3Provider(provider)
      const signer = provider.getSigner()
      showAddress(signer)

      const contract = new ethers.Contract(
        Contract.networks[networkId].address,
        Contract.abi,
        signer,
      )
      resolve(contract)
      return
    }
    reject('Install Metamask')
  })

const showAddress = async (signer) => {
    address = await signer.getAddress()
    const connectButton = document.getElementById('connect')
    connectButton.innerText = address
}

const getPrice = async (contract) => {
    console.log('contract', contract)
    const price = await contract.getPrice()
    console.log('price', price)
    const priceContainer = document.getElementById('price')
    priceContainer.innerText = price
}

const init = async () => {
    const contract = await getContract()
    getPrice(contract)
}

const fundButton = document.getElementById('fund')
fundButton.addEventListener('click', async () => {
    const fundMe = await getContract()
})

init()

我在浏览器控制台中收到以下错误,但不知道是什么原因。

您的部署脚本将 0x056... 地址作为构造函数的 priceFeed 参数传递。

因此 getPrice() 合约函数正在尝试调用 0x056... 地址上的 latestRoundData(),等待响应。

但是,你的合约是部署在测试网上的,测试网上0x056...地址(link)没有合约到return一个值,导致“ main" 呼叫恢复。