首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >contract.methods.balance不是一个函数

contract.methods.balance不是一个函数
EN

Stack Overflow用户
提问于 2022-10-23 14:16:58
回答 1查看 86关注 0票数 0

代码语言:javascript
复制
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;
  }
}

代码语言:javascript
复制
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中有第二个连接

EN

回答 1

Stack Overflow用户

发布于 2022-10-23 16:25:45

这可能是由于ABI接口的缘故,对于您的合同来说,这个接口应该是:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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)]
// }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74171846

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档