Created
February 5, 2022 14:05
-
-
Save rkdnddl23/b1cd2e088e14a8f46581c454040f9aae 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
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; | |
using Counters for Counters.Counter; | |
mapping(address => Counters.Counter) private _ownedTokensCount; | |
// mint(tokenId, uri, owner) | |
// transferFrom(from, to, tokenId) | |
function mintWithTokenURI(address to, uint256 tokenId, string memory tokenURI) public returns (bool) { | |
// to에게 tokenId(일련번호)를 발행하겠다. | |
// 적힐 글자는 tokenURI | |
tokenOwner[tokenId] = to; | |
tokenURIs[tokenId] = tokenURI; | |
_ownedTokens[to].push(tokenId); | |
_ownedTokensCount[to].increment(); | |
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; | |
_ownedTokensCount[from].decrement(); | |
_ownedTokensCount[to].increment(); | |
// 만약에 받는쪽이 실행할 코드가 있는 스마트 컨트랙트이면 코드를 실행할 것 | |
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 balanceOf(address owner) public view returns (uint256) { | |
require( | |
owner != address(0), | |
"KIP17: balance query for the zero address" | |
); | |
return _ownedTokensCount[owner].current(); | |
} | |
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 lastTokenIndex = _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][lastTokenIndex]; | |
_ownedTokens[from][lastTokenIndex] = 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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment