Created
January 18, 2022 13:41
-
-
Save DownyBehind/4ebc471a13be22e9337958818e7dfd6d to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.5.6+commit.b259423e.js&optimize=false&runs=200&gist=
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Klaytn IDE uses solidity 0.4.24 0.5.6 versions. | |
pragma solidity >=0.4.24 <=0.5.6; | |
contract Practice2 { | |
string public name = "KlayLion"; | |
string public symbol = "KL"; // 단위 | |
mapping(uint256 => address) public tokenOwner; // token owner mapping | |
mapping(uint256 => string) public tokenURIs; // key - value type declare | |
// 소유한 토큰 리스트 | |
mapping(address => uint256[]) private _ownedTokens; | |
// mint(tokenId, uri, owner) : 발행 | |
// transferFrom(from, 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, "from !- msg.sender"); // 보내는 사람이 현재 이 함수를 실행한 주체 | |
require(from == tokenOwner[tokenId], "you are not the owner of the token"); // 보내는 사람이 보내려고 하는 토큰의 주인 | |
// | |
_removeTokenFromList(from, tokenId); | |
_ownedTokens[to].push(tokenId); | |
// | |
tokenOwner[tokenId] = to; | |
} | |
function _removeTokenFromList(address from, uint256 tokenId) private{ | |
// [10, 15, 19, 20] -> 19번을 삭제하고 싶어요. | |
// [10, 15, 20, 19] | |
// [10, 15, 20] | |
uint256 lastTokenIdx = _ownedTokens[from].length - 1; | |
for(uint256 i = 0; i < _ownedTokens[from].length; i++){ | |
if(tokenId == _ownedTokens[from][i]){ | |
// swap last token with deleting token; | |
_ownedTokens[from][i] = _ownedTokens[from][lastTokenIdx]; | |
_ownedTokens[from][lastTokenIdx] = tokenId; | |
break; | |
} | |
} | |
_ownedTokens[from].length--; | |
} | |
function ownedTokens(address owner) public view returns (uint256[] memory){ | |
return _ownedTokens[owner]; | |
} | |
function setTokenUri(uint256 id, string memory uri) public { // memory는 좀 긴 타입의 데이터를 표시할 떄 쓴다. | |
tokenURIs[id] = uri; // mapping | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment