erc721.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import erc721ABI from './erc721ABI.json'
  2. import {
  3. ethers,
  4. ContractTransactionResponse,
  5. ContractTransactionReceipt,
  6. } from 'ethers'
  7. import WrappedContract from './base'
  8. interface NFTTokenURI {
  9. id: number
  10. uri: string
  11. }
  12. export default class Erc72TokenContract extends WrappedContract {
  13. constructor(address: string, provider: ethers.Signer) {
  14. super(erc721ABI, address, provider)
  15. }
  16. getInstance(): ethers.Contract {
  17. return new ethers.Contract(this.address, this.ABI as any, this.provider)
  18. }
  19. async balanceOf(address: string): Promise<number> {
  20. const contract = this.getInstance()
  21. const bn = await contract.balanceOf(address)
  22. return parseInt(bn)
  23. }
  24. async tokenOfOwnerByIndex(address: string, index: number): Promise<number> {
  25. const contract = this.getInstance()
  26. const bn = await contract.tokenOfOwnerByIndex(address, index)
  27. return parseInt(bn)
  28. }
  29. async tokenURI(tokenId: number): Promise<string> {
  30. const contract = this.getInstance()
  31. return await contract.tokenURI(tokenId)
  32. }
  33. async safeTransferFrom(
  34. addressFrom: string,
  35. addressTo: string,
  36. tokenId: number
  37. ): Promise<ContractTransactionReceipt> {
  38. const contract = this.getInstance()
  39. const method = contract['safeTransferFrom(address,address,uint256)']
  40. const tx: ContractTransactionResponse = await method(
  41. addressFrom,
  42. addressTo,
  43. tokenId
  44. )
  45. const txwait = await tx.wait()
  46. if (!txwait) {
  47. throw new Error('Transaction failed')
  48. }
  49. return txwait
  50. }
  51. async approve(
  52. addressTo: string,
  53. tokenId: number
  54. ): Promise<ContractTransactionReceipt> {
  55. const contract = this.getInstance()
  56. const tx: ContractTransactionResponse = await contract.approve(
  57. addressTo,
  58. tokenId
  59. )
  60. const txwait = await tx.wait()
  61. if (!txwait) {
  62. throw new Error('Transaction failed')
  63. }
  64. return txwait
  65. }
  66. async getApproved(tokenId: number): Promise<string> {
  67. const contract = this.getInstance()
  68. return await contract.getApproved(tokenId)
  69. }
  70. // other
  71. async tokenIdsOf(address: string): Promise<number[]> {
  72. const count = await this.balanceOf(address)
  73. const list: number[] = []
  74. for (let i = 0; i < count; i++) {
  75. list[i] = await this.tokenOfOwnerByIndex(address, i)
  76. }
  77. return list
  78. }
  79. async tokenURIListOf(address: string): Promise<NFTTokenURI[]> {
  80. const ids = await this.tokenIdsOf(address)
  81. const list: NFTTokenURI[] = []
  82. for (let i = 0; i < ids.length; i++) {
  83. const uri = await this.tokenURI(ids[i])
  84. list[i] = {
  85. id: ids[i],
  86. uri,
  87. }
  88. }
  89. return list
  90. }
  91. }