Solidity

[CryptoZombie] 1-5 ERC721 & Crypto-Collectibles

๐Ÿงก ERC721 Token

contract ERC721 {
  event Transfer(address indexed _from, address indexed _to, uint256 _tokenId);
  event Approval(address indexed _owner, address indexed _approved, uint256 _tokenId);

  function balanceOf(address _owner) public view returns (uint256 _balance);
  function ownerOf(uint256 _tokenId) public view returns (address _owner);
  function transfer(address _to, uint256 _tokenId) public;
  function approve(address _to, uint256 _tokenId) public;
  function takeOwnership(uint256 _tokenId) public;
}

 

๐Ÿงก ERC721 Token Transfer

1. transfer ํ•จ์ˆ˜๋Š” ํ† ํฐ์˜ ์†Œ์œ ์ž๊ฐ€ ์ „์†ก ์ƒ๋Œ€์˜ address, ์ „์†กํ•˜๊ณ ์ž ํ•˜๋Š” tokenId๋ฅผ ๋„˜๊ฒจ์ฃผ๋Š” ๋ฐฉ์‹์ด๋‹ค.

2. approve ํ•จ์ˆ˜๋Š” ์œ„์—์„œ ๋ณธ ์ •๋ณด๋“ค์„ ๊ฐ–๊ณ  ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ปจํŠธ๋ž™ํŠธ์— ๋ˆ„๊ฐ€ ํ•ด๋‹น ํ† ํฐ์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋„๋ก ํ—ˆ๊ฐ€๋ฅผ ๋ฐ›์•˜๋Š”์ง€ ์ €์žฅํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ˆ„๊ตฐ๊ฐ€๊ฐ€ takeOnwership ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ํ•ด๋‹น ์ปจํŠธ๋ž™ํŠธ๋Š” msg.sender๊ฐ€ ์†Œ์œ ์ž๋กœ๋ถ€ํ„ฐ ํ† ํฐ์„ ๋ฐ›์„ ์ˆ˜ ์žˆ๊ฒŒ ํ—ˆ๊ฐ€๋ฅผ ๋ฐ›์•˜๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.

function transfer(address _to, uint256 _tokenId) public;
function approve(address _to, uint256 _tokenId) public;
function takeOwnership(uint256 _tokenId) public;

 

 

๐Ÿงก OpenZeppelin SafeMath Library

Solidity์—์„œ๋Š” ๋ฒ„ํผ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ๋ฅผ ๋ง‰๊ธฐ ์œ„ํ•ด OpenZeppelin์—์„œ ์ง€์›ํ•˜๋Š” SafeMath ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

using SafeMath for uint256;

uint256 a = 5;
uint256 b = a.add(3); // 5 + 3 = 8
uint256 c = a.mul(2); // 5 * 2 = 10
library SafeMath {

  function mul(uint256 a, uint256 b) internal pure returns (uint256) {
    if (a == 0) {
      return 0;
    }
    uint256 c = a * b;
    assert(c / a == b);
    return c;
  }

  function div(uint256 a, uint256 b) internal pure returns (uint256) {
    // assert(b > 0); // Solidity automatically throws when dividing by 0
    uint256 c = a / b;
    // assert(a == b * c + a % b); // There is no case in which this doesn't hold
    return c;
  }

  function sub(uint256 a, uint256 b) internal pure returns (uint256) {
    assert(b <= a);
    return a - b;
  }

  function add(uint256 a, uint256 b) internal pure returns (uint256) {
    uint256 c = a + b;
    assert(c >= a);
    return c;
  }
}

 

https://share.cryptozombies.io/ko/lesson/5/share/H4XF13LD_MORRIS_๐Ÿ’ฏ๐Ÿ’ฏ๐Ÿ˜Ž๐Ÿ’ฏ๐Ÿ’ฏ?id=Y3p8MTIwMjIz