通过以太币与 BSC 上的智能合约进行通信会给出 -32603 错误代码
Posted
技术标签:
【中文标题】通过以太币与 BSC 上的智能合约进行通信会给出 -32603 错误代码【英文标题】:Communicating with a Smart Contract on BSC over ethers gives -32603 error code 【发布时间】:2022-01-01 01:43:56 【问题描述】:您好,我是智能合约开发的新手,这几天我试图让它工作,但没有运气。我希望有一个人可以帮助我。我尝试与部署到 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)
我尝试调用 getPrice() 与 chainlink 通信以获取 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()
我在浏览器控制台中收到以下错误,不知道是什么原因。
【问题讨论】:
【参考方案1】:您的部署脚本将0x056...
地址作为构造函数的priceFeed
参数传递。
因此,getPrice()
合约函数会尝试调用0x056...
地址上的latestRoundData()
,期待得到响应。
但是,您的合约部署在测试网上,并且测试网 (link) 上的0x056...
地址上没有合约可以返回值,这会导致“main”调用恢复。
【讨论】:
非常感谢!我使用的是主网合约,现在使用的是测试网合约,一切正常。以上是关于通过以太币与 BSC 上的智能合约进行通信会给出 -32603 错误代码的主要内容,如果未能解决你的问题,请参考以下文章