Created
February 5, 2022 14:28
-
-
Save rkdnddl23/144fe1f9cca632a124841e7196565a69 to your computer and use it in GitHub Desktop.
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
pragma solidity >=0.4.24 <=0.5.6; | |
/*contract name*/ | |
contract NFTSimple { | |
string public name = "KlytnLion"; | |
string public symbol = "KL"; //사용하는 화폐 | |
mapping (uint256 => address) public tokenOwner; // 주소를 정수형이랑 매핑 | |
mapping (uint256 => string) public tokenURIs; // 글자를 정수형이랑 매핑 | |
// 소유한 토큰 리스트 | |
mapping(address => uint256[]) private _ownedTokens; | |
// onKIP17Received bytes value | |
bytes4 private constant _KIP17_RECEIVED = 0x6745782b; | |
//mint(tokenId, uri, owner) | |
//transferfrom(from, to, token) => owner가 바뀌는 것 from에서 to로 | |
function mintWithTokenURI(address to, uint256 tokenId, string memory tokenURI) public returns (bool) { | |
//to에게 tokenId(일련번호)를 발행하는 함수 | |
//적힐 글자는 tokenURI | |
tokenOwner[tokenId] = to; //msg.sender 가능 | |
tokenURIs[tokenId] = tokenURI; | |
//ownedTokens에 추가 | |
_ownedTokens[to].push(tokenId); | |
return true; | |
} | |
function safeTransferFrom(address from, address to, uint256 tokenId, bytes memory _data) public{ | |
require(from == msg.sender, "from != msg.sender"); | |
require(from == tokenOwner[tokenId], "You're not the owner of the token"); | |
_removeTokenFromList(from, tokenId); | |
_ownedTokens[to].push(tokenId); | |
tokenOwner[tokenId] = to; | |
// 만약에 받는 쪽이 실행할 코드가 있는 스마트 컨트랙트라면 코드를 실행할 것 | |
require( | |
_checkOnKIP17Received(from, to, tokenId, _data), "KIP17: transfer to non KIP17Receiver implementer" | |
); | |
} | |
function _checkOnKIP17Received(address from, address to, uint256 tokenId, bytes memory _data) internal returns (bool){ | |
bool success; | |
bytes memory returndata; | |
if (!isContract(to)){ | |
return true; | |
} | |
(success, returndata) = to.call( | |
abi.encodeWithSelector( | |
_KIP17_RECEIVED, | |
msg.sender, | |
from, | |
tokenId, | |
_data | |
) | |
); | |
if( | |
returndata.length != 0 && | |
abi.decode(returndata, (bytes4)) == _KIP17_RECEIVED | |
) { | |
return true; | |
} | |
return false; | |
} | |
function isContract(address account) internal view returns (bool){ | |
uint256 size; | |
assembly {size := extcodesize(account)} | |
return size > 0; | |
} | |
function _removeTokenFromList(address from, uint256 tokenId) private { | |
uint256 lastTokenIdex = _ownedTokens[from].length - 1; | |
for(uint256 i=0; i<_ownedTokens[from].length; i++){ | |
if(tokenId == _ownedTokens[from][i]){ | |
_ownedTokens[from][i] = _ownedTokens[from][lastTokenIdex]; | |
_ownedTokens[from][lastTokenIdex] = 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{ | |
tokenURIs[id] = uri; | |
} | |
function balanceOf(address owner) public view returns (uint256) { | |
require( | |
owner != address(0), | |
"KIP17: balance query for the zero address" | |
); | |
return _ownedTokens[owner].length; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment