3

I created a simple contract which basically does nothing but mints new ERC721 tokens. The solidity code for it is as follows:

struct Monster {
        string name;
        uint level;
        uint attackPower;
        uint defensePower;
    }

Monster[] public monsters;
function createToken(string _name, address _to) public{
        require(owner == msg.sender);
        uint id = monsters.length;
        monsters.push(Monster(_name, 1, 100, 100));
        _mint(_to, id);
    }

I invoked the createToken method serveral times and created a token named testToken and sent it to my metamask wallet account. I can see the token in metamask. The contract is deployed on ropsten network and I have the contract address.

Now, I want to use web3.js to simply check this balance from my account (metamask wallet). I am trying to use this library https://www.npmjs.com/package/erc721 for the abi.

I have the following code:

const { MintableNonFungibleToken } = require('erc721');
var abi = MintableNonFungibleToken;
var contractAddress = '0x17b95748b3da7f1c4fbf665b8340b851cec7b856';
var token = web3.eth.contract(abi).at(contractAddress);
console.log(token.balanceOf(address).toNumber()); 

I get the correct token count here. However, I could not find any method token.decimals() like in ERC20 contract.

  1. Does it mean I can just use token.balanceOf(address).toNumber() to find the token count?
  2. How do I identify the tokenId of these tokens so I can query more information about the token?
  3. How do I execute totalSupply()? I tried token token.totalSupply().toNumber(). However, the value is always 0.

PS: I haven't implemented totalSupply(). I am trying to write generic APIs to query ERC721 contracts and get information, so I am looking for something from standard specs from ERC721 instead of depending on the developer to write specific functions in contracts.

2
  • Have you implemented the totalSupply() function in your ERC721 contract? Is it getting updated when you call createToken()? Nov 10, 2018 at 16:09
  • If I haven't implemented it, how do I get the total tokens?
    – kosta
    Nov 10, 2018 at 21:48

1 Answer 1

3

A compliant ERC-721 token will have a totalSupply method implemented, eg

function totalSupply() public view returns (uint) {
    return monsters.length;
}

Your contract will need to implement that method to comply with the spec

5
  • I can write that, but I was wondering if there is any ERC721 standard API which allows me to get the total number of tokens. I want to create generic wrapper apis to query erc721 tokens, so I don't want to depend on the contract content, but want to be a part of the standard ERC721 spec.
    – kosta
    Nov 10, 2018 at 22:23
  • ok, thanks. How about questions 1 and 2?
    – kosta
    Nov 11, 2018 at 1:54
  • 3
    Please note that to be compliant with the ERC-721 standard a contract doesn't need to implement the totalSupply function. However, there is an optional extension, called the enumeration extension, which expects you to implement a totalSupply function. All of this information can be found in the aforementioned links.
    – Gilgameš
    May 20, 2022 at 9:10
  • Is there a way of knowing how many tokens are left to mint using ERC721? This would be maxSupply - totalSupply. Apr 17, 2023 at 6:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.