Skip to content

Instantly share code, notes, and snippets.

@ziro0914
Created February 4, 2022 14:02
Show Gist options
  • Save ziro0914/4771a3d9e81c571baf760d54dd581ef6 to your computer and use it in GitHub Desktop.
Save ziro0914/4771a3d9e81c571baf760d54dd581ef6 to your computer and use it in GitHub Desktop.
// 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;
// mint(tokenId, uri-글자, owner)
//transform(from, to , tokenId)
//mint - token 발급 및 토큰에 메세지 작성
function mintWithTokenURI(address to, uint256 tokenId, string memory tokenURI) public returns(bool){
// to에게 tokenId를 발급하겠다
// 적힐 글자는 tokenURI
tokenOwner[tokenId] = to;
tokenURIs[tokenId] = tokenURI;
//add token to list
_ownedTokens[to].push(tokenId);
return true;
}
// 다른 사용자에게 토큰 전달
function safeTransforFrom(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
for(uint256 i=0; i < _ownedTokens[from].length; i++){
if(tokenId == _ownedTokens[from][i]){
uint256 temp = _ownedTokens[from][i+1];
_ownedTokens[from][i] = _ownedTokens[from][i+1];
_ownedTokens[from][i+1] = temp;
break;
}
}
}
// 소유 토큰 조회
function ownedTokens(address owner) public view returns(uint256[] memory){
return _ownedTokens[owner];
}
function setTokenURI(uint256 id, string memory url ) public {
tokenURIs[id] = url;
}
}
contract NFTMarket{
function buyNFT(uint256 tokenId, address NFTAddress, address to) public returns (bool){
NFTSimple(NFTAddress).safeTransforFrom(address(this), to, tokenId);
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment