繁体   English   中英

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

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

Open Zeppelin ERC20 合约有一个 name()、symbol 和 totalSupply() 函数。 这是我继承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;
            }
        }
    }

我已经测试过 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')
        })
    })

但是当我尝试在 web3 中调用 name() 方法时,出现以下错误: TypeError: quad_ek.methods.name is not a function

我的代码如下(合同已正确加载):

 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()
}


我正在使用来自 web3 的文档:

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

我的令牌 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`."
        }

在您的帖子中,您使用了完整的 JSON 文件(或者只是它的devDocs部分,无法从上下文中分辨出来)作为token_abi的值。 整个文件是编译过程的输出,它包含 ABI 以及编译器生成的其他数据(字节码、警告列表……)。

您只需将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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM