Saturday, October 9, 2021

Ethereum Programming 18 ERC721 token

//logs

Contract: Memory Token
    deployment
      √ deploys successfully
      √ has a name (189ms)
      √ has a symbol (78ms)
    token distribution
total # of tokens: 1
how many token does current owner have: 1
owner of token 8888: 0x147C53c891f32fF377AeD30fb9a77b0227252360
the first token current owner has: 8888
      √ mints tokens (1078ms)

  4 passing (2s)

------------------------
//src/contracts/MemoryToken.sol

pragma solidity ^0.5.0;

import "./ERC721Full.sol";

contract MemoryToken is ERC721Full {
  
  constructor() ERC721Full("Memory Token", "MEMORY") public {
  }

  function mint(address _to, string memory _tokenURI) public returns(bool){
    uint _tokenId = totalSupply().add(8888);
    _mint(_to, _tokenId);
    _setTokenURI(_tokenId, _tokenURI);
    return true;
  }

}

--------------------------
//test/MemoryToken.test.js

const { assert } = require('chai')

const MemoryToken = artifacts.require('./MemoryToken.sol')

require('chai')
  .use(require('chai-as-promised'))
  .should()

contract('Memory Token', (accounts) => {
  let token

  before(async () => {
    token = await MemoryToken.deployed()
  })

  describe('deployment', async () => {
    it('deploys successfully', async () => {
      const address = token.address
      assert.notEqual(address, 0x0)
      assert.notEqual(address, '')
      assert.notEqual(address, null)
      assert.notEqual(address, undefined)
    })

    it('has a name', async () => {
      const name = await token.name()
      assert.equal(name, 'Memory Token')
    })

    it('has a symbol', async () => {
      const symbol = await token.symbol()
      assert.equal(symbol, 'MEMORY')
    })
  })

  describe('token distribution', async () => {
    let result

    it('mints tokens', async () => {
      await token.mint(accounts[0], 'https://www.token-uri.com/nft')
      result = await token.totalSupply()
      console.log('total # of tokens:', result.toString())
      assert.equal(result.toString(), '1', 'total supply is correct')

      result = await token.balanceOf(accounts[0])
      console.log('how many token does current owner have:', result.toString())
      assert.equal(result.toString(), '1', 'balanceOf is correct')

      result = await token.ownerOf('8888')
      console.log('owner of token 8888:', result.toString())
      assert.equal(result.toString(), accounts[0].toString(), 'ownerOf is correct')
      result = await token.tokenOfOwnerByIndex(accounts[0], 0)
      console.log('the first token current owner has:', result.toString())

      let balanceOf = await token.balanceOf(accounts[0])
      let tokenIds = []
      for (let i = 0; i < balanceOf; i++) {
        let id = await token.tokenOfOwnerByIndex(accounts[0], i)
        tokenIds.push(id.toString())
      }
      let expected = ['8888']
      assert.equal(tokenIds.toString(), expected.toString(), 'tokenIds are correct')

      let tokenURI = await token.tokenURI('8888')
      assert.equal(tokenURI, 'https://www.token-uri.com/nft')
    })
  })
})

reference:

No comments:

Post a Comment