ERC1155
You can interact with erc1155 token by creating instance of class ERC1155
. ERC1155 class contains all different methods required for standard erc1155 contract.
ERC1155 instance can be created by passing token address in constructor.
import { ERC1155 } from "@ethcontracts/core";
const token = new ERC1155(<tokenAddress>);
init
initiate token with provider.
ethers.js
import { ERC1155 } from "@ethcontracts/core";
import { EthersClient } from "@ethcontracts/ethers";
await token.init(new EthersClient(<wallet provider>));
web3.js
import { ERC1155 } from "@ethcontracts/core";
import { Web3Client } from "@ethcontracts/web3";
await token.init(new Web3Client(<wallet provider>));
getTokenCount
getTokenCount can be used to get the token count of a user for a particular erc1155
token id. It internally calls balanceOf
method.
const count = await token.getTokenCount(<userAddress>,<token id>);
getTokenCountForMany
getTokenCountForMany can be used to get the token count of multiple user for a particular erc1155
token id. It internally calls balanceOfBatch
method.
const count = await token.getTokenCountForMany(<userAddress[]>,<token id[]>);
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);
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();
safeTransferFrom
safeTransferFrom method can be used to transfer amount of a token to another user by a spender or owner.
const [getTransactionHash, getTransactionReceipt] = token.safeTransferFrom(<fromAddress>,<toAddress>, <tokenId>, <amount>);
const txhash = await getTransactionHash();
const txReceipt = await getTransactionReceipt();
👉 You can also pass additional data as per erc1155 standard
but its optional and internally we handle this to pass empty value as default.
const [getTransactionHash, getTransactionReceipt] = token.safeTransferFrom(<fromAddress>,<toAddress>, <tokenId>, <amount>, <data>);
const txhash = await getTransactionHash();
const txReceipt = await getTransactionReceipt();
safeBatchTransferFrom
safeBatchTransferFrom method can be used to transfer multiple tokens with multiple amounts to another user by a spender or owner. It is similar to safeTransferFrom
but allows to do batch operations.
const [getTransactionHash, getTransactionReceipt] = token.safeTransferFrom(<fromAddress>,<toAddress>, <tokenIds[]>, <amounts[]>, <data?>);
const txhash = await getTransactionHash();
const txReceipt = await getTransactionReceipt();
data
parameter is optional - you can leave it or pass null value.