105. The Ethereum Improvement Proposal (EIP ERC721 Standard)
https://eips.ethereum.org/EIPS/eip-721
EIP-721: Non-Fungible Token Standard
eips.ethereum.org
이제 NFT를 minting하는 smart contract를 짜볼 것이다. 앞서 말했듯이 ERC-721 프로토콜을 따라야 하기 때문에 위의 문서를 참고해서 구현해야 하는 것들을 살펴보자. enumeration extension은 optinal이라고 적혀있지만, NFT marketplace에서 그것은 필수이다.
106. EIP & The Open Zeppelin Token Contracts
앞서서 dependencies를 살펴볼때 openZepplin이라는 모듈이 있었다.
node_modules 폴더를 확인해보면, openZepplin > token > ERC721 에 ERC721.sol 파일이 구현되어 있는 걸 확인할 수 있다! 이렇게 라이브러리를 사용해서 반복되는 부분들을 간단하게 가져올 수 있지만 이번 수업에서는 일단 처음부터 끝까지 직접 짜보는 걸로 한다.
일단 KryptoBirdz.sol 파일 안의 metadata (name, symbol) 을 바깥으로 빼는 작업부터 시작한다.
그 후에 connector smart contract를 만들어서 연결한다.(?)
107. Refactoring Metadata to an ERC721Metadata Smart Contract
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract ERC721Metadata {
string private _name;
string private _symbol;
constructor(string memory named, string memory symbolified) {
_name = named;
_symbol = symbolified;
}
function name() external view returns(string memory) {
return _name;
}
function symbol() external view returns(string memory) {
return _symbol;
}
}
- contracts 폴더 아래에 ERC721Metadata.sol 이라는 파일을 만들어서 위와같이 메타데이터를 따로 빼는 코드를 작성한다.
- 이때 앞서 KryptoBird에서 name과 symbol을 public으로 설정한 것과 다르게 이번에는 private으로 선언했기 때문에 외부에서 접근할 수 있는 function을 따로 만들어주어야 한다. 이름의 형식도 앞에 _ 를 붙인다.
- 인자를 줄 때 memory를 붙이는 이유는 string이 메모리에 저장되어 있기 때문이다.(?)
108. Create The ERC721 Connector Smart Contract
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import './ERC721Metadata.sol';
contract ERC721Connector is ERC721Metadata {
constructor(string memory name, string memory symbol) ERC721Metadata(name, symbol) {
}
}
- 그리고 ERC721Connector.sol 파일을 만들어서 ERC721Metadata.sol을 import 한다.
- 그리고 constructor에서 ERC721Metadata를 호출한다. KryptoBird - Connector - Metadata 이렇게 세 단계로 이어진다고 생각하면 되는 것이다.
110. Solution - Hook up the ERC721Connector To The Main NFT Smart Contract
이제 KryptoBird 파일을 수정한다. Connector가 Metadata를 import 한 것 처럼, KryptoBird는 Connector를 import 한다.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import './ERC721Connector.sol';
contract KryptoBird is ERC721Connector {
constructor () ERC721Connector('KryptoBird', 'KBIRDZ'){
}
}
이때, constructor 안에 ERC721Connector를 위치시키면 에러가 뜨더라. 왜인지 모르겠지만 위와같이 custructor() 옆에 바로 붙여야 에러가 뜨지 않는다.
111. Migrate And Test NFT Smart Contract Updates
이제 변경한 smart contract를 다시 deploy 해보자.
truffle migrate --reset
다시 하려면 뒤에 --reset 을 추가하면 된다.
💣 이때, 또 ganache가 연결이 안되는 오류가 발생..
wsl 이 실행될때마다 ip 주소가 바뀌어서 그런지 이전에 생성해놨던 workspace가 아예 작동이 안되서 QuickStart로 새로 만들었고 truffle-config.js 파일도 다시 변경했다. 아니.. 새로 킬 때마다 이래야 된다고..?! 아마 고정시키는 방법이 있지 않을까 싶은데.. 나중에 찾아봐야겠다....
아무튼 그러면 deploy가 된 로그를 확인할 수 있고, 이전과 똑같이 truffle console을 열어서 컨트랙트를 가져와서 name과 symbol에 접근했을 때 값이 잘 출력되면 성공한 것이다.
113. The Minting NFT Function Steps
building out the minting function:
a. NFT to pint to an address
b. keep track of the token ids
c. keep track of token owner addresses to token ids
d. keep track of how many tokens an owner address has
e. create an even that emits a transfer log - contract address, where it is being minted to, the id
이제 진짜로 ERC721 을 사용한 NFT function을 ERC721.sol 파일에서 이제 구현해볼 것이다.