-
-
Save dwook/f413473aadcddcddac09f2cce909d19c to your computer and use it in GitHub Desktop.
공통부문 Smart Contract
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 NFTSimple { | |
string public name = "KlayLion"; | |
string public symbol = "KL"; | |
mapping(uint256 => address) public tokenOwner; | |
mapping(uint256 => string) public tokenURIs; | |
// 소유한 토큰 리스트 | |
mapping(address => uint256[]) private _ownedTokens; | |
bytes4 private constant _KIP17_RECEIVED = 0x6745782b; | |
// 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, | |
bytes memory _data | |
) 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; | |
// | |
require( | |
_checkOnKIP17Received(from, to, tokenId, _data), | |
"KIP17: transfer to non KIP17Receiver implementer" | |
); | |
} | |
function _removeTokenFromList(address from, uint256 tokenId) private { | |
// [10, 15, 19, 20] -> 19번을 삭제 하고 싶어요 | |
// [20, 15, 20, 19] | |
// [10, 15, 20] | |
uint256 lastTokenIdex = _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][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 _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) { | |
// This method relies in extcodesize, which returns 0 for contracts in | |
// construction, since the code is only stored at the end of the | |
// constructor execution. | |
uint256 size; | |
// solhint-disable-next-line no-inline-assembly | |
assembly { | |
size := extcodesize(account) | |
} | |
return size > 0; | |
} | |
/// @notice Count all NFTs assigned to an owner | |
/// @dev NFTs assigned to the zero address are considered invalid, and this function throws for queries about the zero address. | |
/// @param owner An address for whom to query the balance | |
/// @return The number of NFTs owned by `owner`, possibly zero | |
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