๐ NFT
- ๊ธ์
- ์์ ์
๐ ํ ํฐ์ด ์กด์ฌํ๋ ค๋ฉด
- ๋ฐํ (์ผ๋ จ๋ฒํธ, ๊ธ์, ์์ ์)
- ์ ์ก (๋๊ฐ, ๋๊ตฌ์๊ฒ, ๋ฌด์์)
โ ํ ํฐ์ ์ฃผ๊ณ ๋ฐ๋ smart contract ๋ง๋ค์ด๋ณด๊ธฐ
- ์ ์กํ ํ ํฐ์ id๋ฅผ ์ฌ์ฉ ํด ์์ ์ฃผ์ ์ง๊ฐ์์ ํด๋น ํ ํฐ ์ญ์
- ์ ์ก๋ ํ ํฐ์ id๋ฅผ ์ฌ์ฉ ํด ์์ ์์ ์ง๊ฐ์ ํด๋น ํ ํฐ ์ถ๊ฐ
pragma solidity >=0.4.24 <=0.5.6;
contract Practice {
string public name = 'KlayLion';
string public symbol = "DD";
mapping (uint256 => address) public tokenOwner;
mapping (uint256 => string) public tokenURIs;
// ์์ ํ ํ ํฐ ๋ฆฌ์คํธ
mapping (address => uint256[]) private _ownedTokens;
// mint(tokenId, uri, owner)
// transferForm(form, to, tokenId) -> owner๊ฐ ๋ฐ๋๋ ๊ฒ(from -> to)
//๋ฐํ
function mintWithTokenURI(address to, uint256 tokenId, string memory tokenURI) public returns (bool) {
// to์๊ฒ tokenId(์ผ๋ จ๋ฒํธ)๋ฅผ ๋ฐํํ๊ฒ ๋ค.
// ์ ํ ๊ธ์๋ tokenURI
tokenOwner[tokenId] = to;
tokenURIs[tokenId] = tokenURI;
// add token to the list
_ownedTokens[to].push(tokenId);
return true;
}
// ์ ์ก
function safeTransferFrom(address from, address to, uint256 tokenId) public {
require(from == msg.sender, "form != msg.sender"); // ๋ณด๋ธ ์ฌ๋์ด fromํ๊ณ ๊ฐ์ ๋
require(from == tokenOwner[tokenId], "your are not the owner of the token"); // ๋ณด๋ธ ์ฌ๋์ด ํ ํฐ์ ์์ ์ฃผ์ผ ๋
// ํด๋น ํ ํฐ๋ง ์ง์ฐ๊ณ
_removeTokenFromList(from, tokenId);
// ์ ์ก ๋ฐ๋ ๊ณณ์ ํด๋นํ ํฐ ์ถ๊ฐ
_ownedTokens[to].push(tokenId);
// ํด๋น ํ ํฐ์ ์ ์ก
tokenOwner[tokenId] = to; // ์ด๊ฒ๋ง ์์ผ๋ฉด ์๋ฌด๋ ํ ํฐ์ ์ ์กํ ์ ์๋ค.
}
// ๊ธฐ์กด ํ ํฐ ์์ ์ฃผ์ ํ ํฐ ์ญ์
// ์ค๋งํธ ์ปจํธ๋ํธ ์์์๋ง ํธ์ถํ๊ณ ์ถ์ผ๋ฉด private
function _removeTokenFromList(address from, uint256 tokenId) private {
// [10, 15, 19, 20] -> 19๋ฒ ์ญ์ ํ๊ณ ์ถ๋ค
// [10, 15, 20, 19]
// [10, 15, 20]
uint256 lastTokenIndex = _ownedTokens[from].length - 1;
for(uint256 i=0 ; i<_ownedTokens[from].length; i++){
// Swap last token with deleting token.
if(tokenId == _ownedTokens[from][i]){
_ownedTokens[from][i] = _ownedTokens[from][lastTokenIndex];
_ownedTokens[from][lastTokenIndex] = tokenId;
break;
}
}
_ownedTokens[from].length--;
}
// ์์ ์ค์ธ ํ ํฐ
function ownerToken(address owner) public view returns (uint256[] memory){
return _ownedTokens[owner];
}
function setTokenUri(uint256 id, string memory uri) public{
tokenURIs[id] = uri;
}
}
โ ํ ํฐ ๋ฏผํ
ํ ํฐ์ ์์ ์ฃผ๋ฅผ ์กฐํํ๋ฉด ๋ฏผํ ๋ ๋ฃ์ address to์ ์ง๊ฐ์ฃผ์(0x403E...)๋ฅผ ํ์ธ ํ ์ ์๋ค.
โ ์์ ์ฃผ ๋ณ๊ฒฝํด๋ณด๊ธฐ
safeTransferFrom์ ์์ ์ฃผ์ ์ฃผ์ (0x403E...) (address from), ๋ฐ๋ ์ด์ ์ฃผ์ (0x00f3...) (address to), ์ ๋ฌํ ํ ํฐ id 0 (uint256 tokenId)๋ฅผ ์ ๋ ฅํ๋ค.
transaction์ด ์ฑ๊ณตํ๋ฉด ํด๋น ํ ํฐ id์กฐํ ์ ์์ ์์ ์ฃผ์๊ฐ (0x00f3...)๋ก ๋ฐ๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
โ๏ธ์์ ์ฃผ๊ฐ ๋ณ๊ฒฝ๋ ๋ค ๊ธฐ์กด ์์ ์ฃผ์ ์ฃผ์ (0x403E...)๋ฅผ from ์ผ๋ก ์ฌ์ฉ ์ ์ค๋ฅ๊ฐ ๋๋ค.
โ safeTransferFrom
- id๊ฐ 10, 11์ธ ํ ํฐ์ 0x403E... ์ง๊ฐ์ ๋ฏผํ
- 10์ 0x00f3...๋ก ์ ์ก
- 0x403E... ์ ์์ ํ ํฐ ์กฐํ : 11
- 0x00f3... ์ ์์ ํ ํฐ ์กฐํ : 10
๋ฐ์ํ
๋๊ธ