简体   繁体   English

web3js 说在 ERC20 合约中找不到方法

[英]web3js says method is not found in ERC20 contract

The Open Zeppelin ERC20 contract has a name(), symbol, and totalSupply() function. Open Zeppelin ERC20 合约有一个 name()、symbol 和 totalSupply() 函数。 This is my smart contract that inherits the ERC20 contract这是我继承ERC20合约的智能合约

pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";

contract QUAD_EK is ERC20, ERC20Burnable {
    constructor(address[] memory wallets, uint256[] memory amounts) ERC20("Quadency Token", "QUAD") {
        require(wallets.length == amounts.length, "QUAD: wallets and amounts mismatch");
        for (uint256 i = 0; i < wallets.length; i++){
            _mint(wallets[i], amounts[i]);
            if(i == 10){
                break;
            }
        }
    }

I've tested that the name() function works in Truffle.我已经测试过 name() 函数在 Truffle 中有效。

 describe('quad ek methods', async () => {
        it('gets token name', async () => {
            let quadEK = await QUAD_EK.new([account_0, account_1, account_2, account_3, account_4], amounts);
            let quadName = await quadEK.name();
            assert.equal(quadName.toString(), 'Quadency Token')
        })
    })

But when I tried to call the name() method in web3, I get the following error: TypeError: quad_ek.methods.name is not a function但是当我尝试在 web3 中调用 name() 方法时,出现以下错误: TypeError: quad_ek.methods.name is not a function

My code below (the contract is loaded correctly):我的代码如下(合同已正确加载):

 module.exports = async function(callback) {
    try{
        const web3 = new Web3(new Web3.providers.HttpProvider(
            `process.env.INFURA)`
            )
        );

        const signInfo = web3.eth.accounts.privateKeyToAccount('process.env.QUAD_TEST0')
        console.log("signingInfo", signInfo)
        const nonce = await web3.eth.getTransactionCount(signInfo.address)
        console.log("nonce", nonce)
        const quad_ek = new web3.eth.Contract(token_abi, token_address )
      **
        quad_ek.methods.name().call({from: '0x72707D86053cb767A710957a6b9D8b56Dd7Fd835'}, function(error, result){
        
         });**

    }

    catch(error){
        console.log(error)
    }

    callback()
}


I'm using the documentation from web3:我正在使用来自 web3 的文档:

myContract.methods.myMethod(123).call({from: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe'}, function(error, result){
    ...
});

My token abi does have the name function:我的令牌 abi 确实具有 name 功能:

        "name()": {
          "details": "Returns the name of the token."
        },
        "symbol()": {
          "details": "Returns the symbol of the token, usually a shorter version of the name."
        },
        "totalSupply()": {
          "details": "See {IERC20-totalSupply}."
        },
        "transfer(address,uint256)": {
          "details": "See {IERC20-transfer}. Requirements: - `to` cannot be the zero address. - the caller must have a balance of at least `amount`."
        },
        "transferFrom(address,address,uint256)": {
          "details": "See {IERC20-transferFrom}. Emits an {Approval} event indicating the updated allowance. This is not required by the EIP. See the note at the beginning of {ERC20}. NOTE: Does not update the allowance if the current allowance is the maximum `uint256`. Requirements: - `from` and `to` cannot be the zero address. - `from` must have a balance of at least `amount`. - the caller must have allowance for ``from``'s tokens of at least `amount`."
        }

In your post, you used the the full JSON file (or just its devDocs section, can't tell from the context) as the value of token_abi .在您的帖子中,您使用了完整的 JSON 文件(或者只是它的devDocs部分,无法从上下文中分辨出来)作为token_abi的值。 This whole file is an output from the compilation process, it contains the ABI as well as other data produced by the compilator (bytecode, list of warnings, ...).整个文件是编译过程的输出,它包含 ABI 以及编译器生成的其他数据(字节码、警告列表……)。

You need to pass just the abi section to new web3.eth.Contract() ;您只需将abi部分传递给new web3.eth.Contract()

const compileOutput = JSON.parse(fs.readFileSync("./QUAD_EK.JSON"));
const token_abi = compileOutput[0].abi;

const quad_ek = new web3.eth.Contract(token_abi, token_address);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 web3js 发送 ERC20 令牌 - How to send an ERC20 token with web3js 使用 web3js 和元掩码的 ERC20 transferFrom() 私钥 - ERC20 transferFrom() private key with web3js and metamask 如何将 ERC20 代币发送到智能合约余额? - How to send ERC20 token to smart contract balance? 出现错误:返回错误:必须经过身份验证! ,同时使用 web3.js 发送 erc20 代码 - Getting Error: Returned error: Must be authenticated! , while sending erc20 code using web3.js 有人成功地用 Web3.js@1.0.0 获得了 ERC20 Token 的余额吗? - Someone has managed to get the balance of an ERC20 Token with Web3.js@1.0.0? .allowance 方法用于将 erc20 令牌导入元掩码 - .allowance method for importing erc20 token to metamask 我可以使用PHP从ERC20合同中转移令牌吗? - Can I transfer tokens from an ERC20 contract using PHP? Mint功能的传输事件和ERC20智能合约的传输功能有什么区别? - what is difference between transfer event of Mint function and Transfer function of ERC20 Smart Contract? 当我从其他合约调用 ERC20 合约中的转账 function 时出错,但是,可以使用 mint/burn - Error when I call transfer function in ERC20 contract from other contract, But, can use mint/burn Web3js 智能合约交互 Gas 和签名 - Web3js smart contract interaction gas and signing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM