iT邦幫忙

2022 iThome 鐵人賽

DAY 24
0
影片教學

在 2022 年,我們該如何寫智能合約系列 第 24

ERC721 實作 查詢篇

  • 分享至 

  • xImage
  •  

#鐵人賽 #ethereum #solidity

本日課程將實作 ERC721 的查詢用函式與基礎的資料結構。

  1. balanceOf()
  2. ownerOf()

本日影片連結: https://youtu.be/E3UsO0DA0lI
本日合約:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;

interface IERC721 {
    // Event
    /*
    event Transfer(address indexed from, address indexed to, uint256 indexed tokenId);
    event Approval(address indexed owner, address indexed approved, uint256 indexed tokenId);
    event ApprovalForAll(address indexed owner, address indexed operator, bool approved);
    */
    // Query
    function balanceOf(address owner) external view returns (uint256 balance);
    function ownerOf(uint256 tokenId) external view returns (address owner);
    // Transfer
    /*
    function safeTransferFrom(address from, address to, uint256 tokenId, bytes calldata data) external;
    function safeTransferFrom(address from, address to, uint256 tokenId) external;
    function transferFrom(address from, address to, uint256 tokenId) external;
    */
    // Approve
    /*
    function approve(address to, uint256 tokenId) external;
    function setApprovalForAll(address operator, bool _approved) external;
    function getApproved(uint256 tokenId) external view returns (address operator);
    function isApprovedForAll(address owner, address operator) external view returns (bool);
    */
}

contract ERC721 is IERC721 {
    mapping(address => uint256) _balances;
    mapping(uint256 => address) _owners;

    function balanceOf(address owner) public view returns (uint256) {
        require(owner != address(0), "ERROR: address 0 cannot be owner");
        return _balances[owner];
    }

    function ownerOf(uint256 tokenId) public view returns (address) {
        address owner = _owners[tokenId];
        require(owner != address(0), "ERROR: tokenId is not valid Id");
        return owner;
    }
}

本影片提到的連結:
「Remix IDE」: https://remix.ethereum.org/
「在 2022 年,我們該如何寫智能合約」: https://ithelp.ithome.com.tw/users/20083367/ironman/5019
「那些關於 Ethereum 的事」: https://ithelp.ithome.com.tw/users/20083367/ironman/5136
「一本關於 Ethereum 與 Solidity 智能合約的書」: https://solidity.tw
「文章或主題許願池」: https://github.com/hydai/solidity-book/issues
「本系列播放清單」: https://www.youtube.com/playlist?list=PLHmOMPRfmOxQYDnXAc1hKY6ra4WDU8ZlM


上一篇
ERC721 介面
下一篇
ERC721 實作 授權篇
系列文
在 2022 年,我們該如何寫智能合約30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言