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.
- Does it mean I can just use
token.balanceOf(address).toNumber()
to find the token count? - How do I identify the tokenId of these tokens so I can query more information about the token?
- How do I execute
totalSupply()
? I tried tokentoken.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.
totalSupply()
function in your ERC721 contract? Is it getting updated when you callcreateToken()
? – Shawn Tabrizi Nov 10 '18 at 16:09