///
Search
🏀

(220320) Klaytn을 통한 ERC-721 활용방법

1. 계정생성하기

사이트 접속
아래는 사이트 화면
create account 클릭
비밀번호 입력하기
Download & Next Step 클릭
다운로드 받은 json 파일을 가져와서 별도의 파일에 저장해둔다.
다음은 왼쪽에 있는 klay Faucet을 눌러서 로그인을 시도한다.(아래 그림처럼)
json 파일 업로드 하고 비밀번호를 입력해준다.
아래는 로그인 화면이다.
밑에 버튼에 RunFaucet을 눌러 계정 잔액을 늘려주자. (220323 기준 150개 생김)
이렇게 계정을 3개 만들고 다음단계로 넘어간다.

2. 인터페이스 만들기

아래의 사이트를 가자 깃허브 ethereum eip-721 자료 소개
복사할 텍스트는 Specification에서 아래의 solidity 코드이다.
pragma solidity ^0.4.20; /// @title ERC-721 Non-Fungible Token Standard /// @dev See https://eips.ethereum.org/EIPS/eip-721 /// Note: the ERC-165 identifier for this interface is 0x80ac58cd. interface ERC721 /* is ERC165 */ { /// @dev This emits when ownership of any NFT changes by any mechanism. /// This event emits when NFTs are created (`from` == 0) and destroyed /// (`to` == 0). Exception: during contract creation, any number of NFTs /// may be created and assigned without emitting Transfer. At the time of /// any transfer, the approved address for that NFT (if any) is reset to none. event Transfer(address indexed _from, address indexed _to, uint256 indexed _tokenId); /// @dev This emits when the approved address for an NFT is changed or /// reaffirmed. The zero address indicates there is no approved address. /// When a Transfer event emits, this also indicates that the approved /// address for that NFT (if any) is reset to none. event Approval(address indexed _owner, address indexed _approved, uint256 indexed _tokenId); /// @dev This emits when an operator is enabled or disabled for an owner. /// The operator can manage all NFTs of the owner. event ApprovalForAll(address indexed _owner, address indexed _operator, bool _approved); /// @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) external view returns (uint256); /// @notice Find the owner of an NFT /// @dev NFTs assigned to zero address are considered invalid, and queries /// about them do throw. /// @param _tokenId The identifier for an NFT /// @return The address of the owner of the NFT function ownerOf(uint256 _tokenId) external view returns (address); /// @notice Transfers the ownership of an NFT from one address to another address /// @dev Throws unless `msg.sender` is the current owner, an authorized /// operator, or the approved address for this NFT. Throws if `_from` is /// not the current owner. Throws if `_to` is the zero address. Throws if /// `_tokenId` is not a valid NFT. When transfer is complete, this function /// checks if `_to` is a smart contract (code size > 0). If so, it calls /// `onERC721Received` on `_to` and throws if the return value is not /// `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`. /// @param _from The current owner of the NFT /// @param _to The new owner /// @param _tokenId The NFT to transfer /// @param data Additional data with no specified format, sent in call to `_to` function safeTransferFrom(address _from, address _to, uint256 _tokenId, bytes data) external payable; /// @notice Transfers the ownership of an NFT from one address to another address /// @dev This works identically to the other function with an extra data parameter, /// except this function just sets data to "". /// @param _from The current owner of the NFT /// @param _to The new owner /// @param _tokenId The NFT to transfer function safeTransferFrom(address _from, address _to, uint256 _tokenId) external payable; /// @notice Transfer ownership of an NFT -- THE CALLER IS RESPONSIBLE /// TO CONFIRM THAT `_to` IS CAPABLE OF RECEIVING NFTS OR ELSE /// THEY MAY BE PERMANENTLY LOST /// @dev Throws unless `msg.sender` is the current owner, an authorized /// operator, or the approved address for this NFT. Throws if `_from` is /// not the current owner. Throws if `_to` is the zero address. Throws if /// `_tokenId` is not a valid NFT. /// @param _from The current owner of the NFT /// @param _to The new owner /// @param _tokenId The NFT to transfer function transferFrom(address _from, address _to, uint256 _tokenId) external payable; /// @notice Change or reaffirm the approved address for an NFT /// @dev The zero address indicates there is no approved address. /// Throws unless `msg.sender` is the current NFT owner, or an authorized /// operator of the current owner. /// @param _approved The new approved NFT controller /// @param _tokenId The NFT to approve function approve(address _approved, uint256 _tokenId) external payable; /// @notice Enable or disable approval for a third party ("operator") to manage /// all of `msg.sender`'s assets /// @dev Emits the ApprovalForAll event. The contract MUST allow /// multiple operators per owner. /// @param _operator Address to add to the set of authorized operators /// @param _approved True if the operator is approved, false to revoke approval function setApprovalForAll(address _operator, bool _approved) external; /// @notice Get the approved address for a single NFT /// @dev Throws if `_tokenId` is not a valid NFT. /// @param _tokenId The NFT to find the approved address for /// @return The approved address for this NFT, or the zero address if there is none function getApproved(uint256 _tokenId) external view returns (address); /// @notice Query if an address is an authorized operator for another address /// @param _owner The address that owns the NFTs /// @param _operator The address that acts on behalf of the owner /// @return True if `_operator` is an approved operator for `_owner`, false otherwise function isApprovedForAll(address _owner, address _operator) external view returns (bool); }
Solidity
복사
아래 사이트로 들어가자

실험환경

일단 맨처음 실습하기에 앞서서 안에 예제이 있던 모든 파일 및 폴더를 삭제했다.
browser라는 폴더를 생성한 다음에 안에 “ERC-721”라는 이름의 sol파일을 제작했다.
아래는 예시 화면
sol 파일 안에는 상기의 sol 코드를 복사해서 붙여넣기 한건데 편의를 위해서 주석을 전부 삭제해 주었다.
그리고 왼쪽 화면을 보게 된다면 sol 모양의 표시가 있는데 그걸 클릭해서 컴파일러 설정을 해줘야 한다.
COMPILER는 0.4.20 버전으로 세팅해주고 auto compile에 체크표시를 해서 상기 그림처럼 나오도록 세팅하여준다. 그러면 다음처럼 디렉토리가 변경 되어 있을 것이다.
그러면 소스코드를 한번 살펴보면 payable이라고 적혀있는 4개의 줄이 있을텐데 함수구현을 할때는 지워도 된다. 이유는 클레이를 보내야하는데 굳이 클레이 소모없이 하기 위해서 삭제를 해준다.
그리고 external도 public으로 바꿔준다. 함수의 가시성을 높이기 위해서, 내부함수끼리 참조하기 위해서 바꿔준다.

참고문서

Caveats The 0.4.20 Solidity interface grammar is not expressive enough to document the ERC-721 standard. A contract which complies with ERC-721 MUST also abide by the following: Solidity issue #3412: The above interfaces include explicit mutability guarantees for each function. Mutability guarantees are, in order weak to strong: payable, implicit nonpayable, view, and pure. Your implementation MUST meet the mutability guarantee in this interface and you MAY meet a stronger guarantee. For example, a payable function in this interface may be implemented as nonpayable (no state mutability specified) in your contract. We expect a later Solidity release will allow your stricter contract to inherit from this interface, but a workaround for version 0.4.20 is that you can edit this interface to add stricter mutability before inheriting from your contract. Solidity issue #3419: A contract that implements ERC721Metadata or ERC721Enumerable SHALL also implement ERC721. ERC-721 implements the requirements of interface ERC-165. Solidity issue #2330: If a function is shown in this specification as external then a contract will be compliant if it uses public visibility. As a workaround for version 0.4.20, you can edit this interface to switch to public before inheriting from your contract. Solidity issues #3494, #3544: Use of this.*.selector is marked as a warning by Solidity, a future version of Solidity will not mark this as an error. If a newer version of Solidity allows the caveats to be expressed in code, then this EIP MAY be updated and the caveats removed, such will be equivalent to the original specification.
Solidity
복사
payable 키워드는 안써도 된다고 적혀 있고 public으로 바꿔서 써도 된다고 알려주고있다.
지금까지 작성한 코드 공유
pragma solidity ^0.4.20; /// @title ERC-721 Non-Fungible Token Standard /// @dev See https://eips.ethereum.org/EIPS/eip-721 /// Note: the ERC-165 identifier for this interface is 0x80ac58cd. // Klaytn IDE uses solidity 0.4.24, 0.5.6 versions. interface ERC721 /* is ERC165 */ { event Transfer(address indexed _from, address indexed _to, uint256 indexed _tokenId); event Approval(address indexed _owner, address indexed _approved, uint256 indexed _tokenId); event ApprovalForAll(address indexed _owner, address indexed _operator, bool _approved); function balanceOf(address _owner) public view returns (uint256); function ownerOf(uint256 _tokenId) public view returns (address); function safeTransferFrom(address _from, address _to, uint256 _tokenId, bytes data) public; function safeTransferFrom(address _from, address _to, uint256 _tokenId) public; function transferFrom(address _from, address _to, uint256 _tokenId) public; function approve(address _approved, uint256 _tokenId) public; function setApprovalForAll(address _operator, bool _approved) public; function getApproved(uint256 _tokenId) public view returns (address); function isApprovedForAll(address _owner, address _operator) public view returns (bool); }
Solidity
복사

3. 토큰을 발행하는 함수

방금까지 포스팅한 위의 함수(ERC721)를 상속받도록 해야한다.
contract ERC721Implementation is ERC721{ }
Solidity
복사
그리고 위의 함수 안에 토큰발행하는 함수를 작성한다.
_to : 계정주소를 받는 변수. (누가 받는지)
_tokenID : 토큰을 받는 변수. (몇번째 토큰인지)
contract ERC721Implementation is ERC721{ function mint(address _to, uint _tokenId) public{ } }
Solidity
복사
앞으로 다음은 아래의 두가지 일을 수행하는 함수를 작성한다.
토큰 아이디를 투 계정이 소유한다.
투 계정을 한개 더 증가를 시켜준다.