ERC721
You can interact with erc721 token by creating instance of class ERC721
. ERC721 class contains all different methods required for standard erc721 contract.
ERC721 instance can be created by passing token address in constructor.
import { ERC721 } from "@ethcontracts/core";
const token = new ERC721(<tokenAddress>);
init
initiate token with provider.
ethers.js
import { ERC721 } from "@ethcontracts/core";
import { EthersClient } from "@ethcontracts/ethers";
await token.init(new EthersClient(<wallet provider>));
web3.js
import { ERC721 } from "@ethcontracts/core";
import { Web3Client } from "@ethcontracts/web3";
await token.init(new Web3Client(<wallet provider>));
👉 you can call init
multiple times with different provider. This allows you to use the same token instance in multichain dapp.
getName
getName returns the name of the token.
const tokenName = await token.getName();
getSymbol
getSymbol returns the symbol of the token.
const symbol = await token.getSymbol();
getTokenCount
getTokenCount returns the no of token user owns.
const count = await token.getTokenCount(<userAddress>);
getOwner
get owner of the token
const owner = await token.getOwner(tokenId);
getTokenURI
get token uri
const tokenURI = await token.getTokenURI(tokenId);
getApprovedAccount
get approved account for a tokenId
const account = await token.getApprovedAccount(tokenId);
isApprovedForAll
check if spender is approved for all NFT of a user
const isApproved = await token.isApprovedForAll(owner, operator);
isInterfaceSupported
check if an interface is supported
const isApproved = await token.isInterfaceSupported(interfaceId);
approve
approve method approves a spender for the tokenId.
const [getTransactionHash, getTransactionReceipt] = token.approve(<spenderAddress>, <tokenId>);
const txhash = await getTransactionHash();
const txReceipt = await getTransactionReceipt();
setApprovalForAll
setApprovalForAll method approves a spender for all tokens of a user. The last parameter is boolean value - so you can pass true or false to approve or remove approval.
token.setApprovalForAll(<spenderAddress>, <approved:boolean>);
const [getTransactionHash, getTransactionReceipt] = token.setApprovalForAll(<spenderAddress>, true);
const txhash = await getTransactionHash();
const txReceipt = await getTransactionReceipt();
transferFrom
transferFrom method can be used to transfer a token to another user by a spender or owner.
const [getTransactionHash, getTransactionReceipt] = token.transferFrom(<fromAddress>,<toAddress>, <tokenId>);
const txhash = await getTransactionHash();
const txReceipt = await getTransactionReceipt();
safeTransferFrom
safeTransferFrom method is similar to transferFrom except it will be used by contracts like NFT marketplace.
const [getTransactionHash, getTransactionReceipt] = token.safeTransferFrom(<fromAddress>,<toAddress>, <tokenId>);
const txhash = await getTransactionHash();
const txReceipt = await getTransactionReceipt();
safeTransferFromWithData
safeTransferFromWithData method is similar to safeTransferFrom with option to pass additional data as bytes.
const [getTransactionHash, getTransactionReceipt] = token.safeTransferFromWithData(<fromAddress>,<toAddress>, <tokenId>,<data>);
const txhash = await getTransactionHash();
const txReceipt = await getTransactionReceipt();