Skip to content

Instantly share code, notes, and snippets.

@jennharw
Created January 16, 2022 10:16
Show Gist options
  • Save jennharw/38b1110af0df0c51f631808d43756274 to your computer and use it in GitHub Desktop.
Save jennharw/38b1110af0df0c51f631808d43756274 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.5+commit.47a71e8f.js&optimize=false&runs=200&gist=
pragma solidity >=0.4.24 <0.5.6;
contract NFTSimple { //Simple NFG
string public name = "KlayLion";
string public symbol = "KL"; // 단위
//mint 발행 (tokenId, uri, owner)
//transform 전송 (from, to , uri)
mapping (uint256 => address) public tokenOwner;
mapping (uint256 => string) public tokenURIs; //map
//배열 소유한 토큰 리스트
mapping(address => uint256[]) private _ownedTokens;
function mintWithTokenUri(address to, uint256 tokenId, string memory tokenURI) public returns (bool) {
tokenOwner[tokenId] = to; //msg.sender 에게 토큰 id 발행
tokenURIs[tokenId] = tokenURI; //글자
_ownedTokens[to].push(tokenId); //add Token to the list
return true;
}
function safeTransferform(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{
//_ownedTokens[from]
//맨 뒤랑 바꾸고, 자르기
uint256 lastTokenIdex = _ownedTokens[from].length-1;
for (uint256 i = 0 ; i <= _ownedTokens[from].length; i ++){
if (tokenId == _ownedTokens[from][i]) {
// var temp = _ownedTokens[from][i]; //swap
_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;
}
}
contract NFTMarket {
function buyNFT(uint256 tokenId, address NFTAddress, address to ) public returns (bool) {
NFTSimple(NFTAddress).safeTransferform(address(this), to, tokenId);
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment