import erc721ABI from './erc721ABI.json' import { ethers, ContractTransactionResponse, ContractTransactionReceipt, } from 'ethers' import WrappedContract from './base' interface NFTTokenURI { id: number uri: string } export default class Erc72TokenContract extends WrappedContract { constructor(address: string, provider: ethers.Signer) { super(erc721ABI, address, provider) } getInstance(): ethers.Contract { return new ethers.Contract(this.address, this.ABI as any, this.provider) } async balanceOf(address: string): Promise { const contract = this.getInstance() const bn = await contract.balanceOf(address) return parseInt(bn) } async tokenOfOwnerByIndex(address: string, index: number): Promise { const contract = this.getInstance() const bn = await contract.tokenOfOwnerByIndex(address, index) return parseInt(bn) } async tokenURI(tokenId: number): Promise { const contract = this.getInstance() return await contract.tokenURI(tokenId) } async safeTransferFrom( addressFrom: string, addressTo: string, tokenId: number ): Promise { const contract = this.getInstance() const method = contract['safeTransferFrom(address,address,uint256)'] const tx: ContractTransactionResponse = await method( addressFrom, addressTo, tokenId ) const txwait = await tx.wait() if (!txwait) { throw new Error('Transaction failed') } return txwait } async approve( addressTo: string, tokenId: number ): Promise { const contract = this.getInstance() const tx: ContractTransactionResponse = await contract.approve( addressTo, tokenId ) const txwait = await tx.wait() if (!txwait) { throw new Error('Transaction failed') } return txwait } async getApproved(tokenId: number): Promise { const contract = this.getInstance() return await contract.getApproved(tokenId) } // other async tokenIdsOf(address: string): Promise { const count = await this.balanceOf(address) const list: number[] = [] for (let i = 0; i < count; i++) { list[i] = await this.tokenOfOwnerByIndex(address, i) } return list } async tokenURIListOf(address: string): Promise { const ids = await this.tokenIdsOf(address) const list: NFTTokenURI[] = [] for (let i = 0; i < ids.length; i++) { const uri = await this.tokenURI(ids[i]) list[i] = { id: ids[i], uri, } } return list } }