58web3 / drawww

0 stars 0 forks source link

Add NFT mint #18

Closed hanchenglin closed 2 years ago

hanchenglin commented 2 years ago

KR: Add NFT mint

Sample code:

  async createNftAsset (transactionHash?:string, nftMetaUri?:string, blockchain?:string,
    tokenId?:string, accountAddress?:string, explorerUrl?:string) {
    const currentTime = utc(new Date()).format('x')
    const formData :any = {
      nft_meta_id: this.nft.asset_id,
      blockchain,
      token_id: tokenId,
      contract_address: blockchain === BLOCKCHAIN.LINE_BLOCKCHAIN
        ? ITEM_TOKEN_CONTRACT_ID
        : blockchain === BLOCKCHAIN.ASTAR ? ASTAR_NFT_USE_ERC721_ADDRESS_CONTRACT : NFT_USE_ERC721_ADDRESS_CONTRACT,
      owner: null,
      owner_address: blockchain !== BLOCKCHAIN.LINE_BLOCKCHAIN ? accountAddress : OWNER_ADDRESS,
      copyright_holder: null,
      metadata_uri: nftMetaUri,
      price: this.nft.price,
      created_at: Number(currentTime),
      updated_at: Number(currentTime),
      created_by: null,
      updated_by: null,
      transaction_hash: transactionHash,
      delete_flg: CONFIG.DELETED_FLAG.NOT_DELETED,
      explorer_url: explorerUrl
    }
    await NFTService.createNftAsset(formData).then(
      async (result:any) => {
        if (result) {
          // Call new API to get NFT asset
          await this.getNFTAsset()
        }
      },
      (error:any) => {
        if (error) {
          this.$toast.error('Create fail')
        }
      }
    )
  }

  async makeMetaData (data:any):Promise<string> {
    let urlMetadata:string = ''
    const metaData: INFTMetadata = {
      description: data.description,
      external_url: data.external_url,
      image: data.svg_data_url,
      background_color: data.background_color,
      animation_url: data.animation_url,
      youtube_url: data.youtube_url,
      name: data.name,
      attributes: []
    }
    const json = JSON.stringify(metaData)
    const file = new Blob([json], { type: 'application/json' })
    await NFTService.uploadFile(file).then(
      (result: any) => {
        urlMetadata = result.data
      },
      (error: any) => {
        if (error) {
          this.$toast.error('Upload fail')
        }
      }
    )
    return urlMetadata
  }

import contract from '~/contracts/NFTERC721.json'
const abi = contract.abi
const NFT_USE_ERC721_ADDRESS_CONTRACT =
  process.env.NFT_USE_ERC721_ADDRESS_CONTRACT
const GAS_LIMIT = process.env.GAS_LIMIT

  async mint (accountAddress:string, urlMetaData:string): Promise<any> {
    const windowInstance: any = window
    const web3 = windowInstance.web3
    let transactionSuccess = null
    let transactionErr = null
    const nftErc721 = new web3.eth.Contract(
      abi,
      NFT_USE_ERC721_ADDRESS_CONTRACT
    )

    try {
      await nftErc721.methods
        .mintSingleNFT(urlMetaData)
        .send({
          from: accountAddress,
          gasLimit: Number(GAS_LIMIT)
        })
        .on('transactionHash', function () {})
        .on('receipt', function (receipt:any) {
          transactionSuccess = receipt
        //   console.log('this is recept ether', receipt)
        })
    } catch (err:any) {
      transactionErr = err
    }
    return { transactionSuccess, transactionErr }
  }
minhtuan412 commented 2 years ago

https://github.com/58web3/drawww/pull/22

hanchenglin commented 2 years ago

@minhtuan412 Please explain what did you do!

hanchenglin commented 2 years ago

@minhtuan412 Please attach the test evidence!

hanchenglin commented 2 years ago

https://github.com/58web3/drawww/pull/22