
Smart contract :
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
contract Booster {
address owner;
mapping(address => uint256) public accountBalances;
event PaymentAdded(address user, uint256 amount, uint256 timestamp);
modifier onlyOwner(){
require(msg.sender!=owner,"only owner can call this");
_;
}
constructor() {
owner = msg.sender;
}
function fund() public payable{
require (msg.value > 0, "Empty transact");
//payable(address(this)).transfer(msg.value);
accountBalances[msg.sender]=msg.value;
emit PaymentAdded(msg.sender, msg.value, block.timestamp);
}
function withdraw() public payable {
payable(owner).transfer(address(this).balance);
}
function balance() public view returns (uint256){
return payable(address(this)).balance;
}
}
connection:
if(typeof window.ethereum !== 'undefined') {
await connectwallet();
//const provider = new ethers.providers.Web3Provider(window.ethereum);
console.log("abi",ContractAbi);
const contract = new web3provide.eth.Contract( ContractAbi, ContractAddress,{gasPrice: '20000000000'});
console.log(contract.methods);
await contract.methods.balance().call.then(function(bal){
setBalance(bal);
})
} }
当通过web3.0从前端连接智能契约时,会显示零方法,但在智能contracts.First智能契约中有方法,在react中有第二个连接
发布于 2022-10-23 16:25:45
这可能是由于ABI接口的缘故,对于您的合同来说,这个接口应该是:
const CONTRACT_ABI = [
{
inputs: [],
stateMutability: "nonpayable",
type: "constructor",
},
{
anonymous: false,
inputs: [
{
indexed: false,
internalType: "address",
name: "user",
type: "address",
},
{
indexed: false,
internalType: "uint256",
name: "amount",
type: "uint256",
},
{
indexed: false,
internalType: "uint256",
name: "timestamp",
type: "uint256",
},
],
name: "PaymentAdded",
type: "event",
},
{
inputs: [
{
internalType: "address",
name: "",
type: "address",
},
],
name: "accountBalances",
outputs: [
{
internalType: "uint256",
name: "",
type: "uint256",
},
],
stateMutability: "view",
type: "function",
},
{
inputs: [],
name: "balance",
outputs: [
{
internalType: "uint256",
name: "",
type: "uint256",
},
],
stateMutability: "view",
type: "function",
},
{
inputs: [],
name: "fund",
outputs: [],
stateMutability: "payable",
type: "function",
},
{
inputs: [],
name: "withdraw",
outputs: [],
stateMutability: "payable",
type: "function",
},
]然后,如果您使用的是web3.js:
const Web3 = require("web3")
const web3 = new Web3(window.ethereum)
const contract = new web3.eth.Contract(CONTRACT_ABI, CONTRACT_ADDRESS)
console.log(contract.methods)
// {
// accountBalances: [Function: bound _createTxObject],
// '0x6ff96d17': [Function: bound _createTxObject],
// 'accountBalances(address)': [Function: bound _createTxObject],
// balance: [Function: bound _createTxObject],
// '0xb69ef8a8': [Function: bound _createTxObject],
// 'balance()': [Function: bound _createTxObject],
// fund: [Function: bound _createTxObject],
// '0xb60d4288': [Function: bound _createTxObject],
// 'fund()': [Function: bound _createTxObject],
// withdraw: [Function: bound _createTxObject],
// '0x3ccfd60b': [Function: bound _createTxObject],
// 'withdraw()': [Function: bound _createTxObject]
// }或者,如果您使用的是ethers.js:
const { ethers } = require("ethers")
const provider = new ethers.providers.Web3Provider(window.ethereum)
const contract = new ethers.Contract(CONTRACT_ADDRESS, CONTRACT_ABI, provider)
console.log(contract.functions)
// {
// 'accountBalances(address)': [Function (anonymous)],
// 'balance()': [Function (anonymous)],
// 'fund()': [Function (anonymous)],
// 'withdraw()': [Function (anonymous)],
// accountBalances: [Function (anonymous)],
// balance: [Function (anonymous)],
// fund: [Function (anonymous)],
// withdraw: [Function (anonymous)]
// }https://stackoverflow.com/questions/74171846
复制相似问题