简体   繁体   中英

web3js says method is not found in ERC20 contract

The Open Zeppelin ERC20 contract has a name(), symbol, and totalSupply() function. This is my smart contract that inherits the ERC20 contract

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.

 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

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:

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

My token abi does have the name function:

        "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 . 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, ...).

You need to pass just the abi section to 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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