Truffle을 이용하여 ERC-721 개발을 해보자?

hams·2022년 6월 10일
2

1. 👾 erc-721이란?

ERC-20 토큰과는 다르게 대체 불가능한 토큰(Non-Fungible Token) ERC-721로 발행되는 토큰은 모두 각각의 가치를 가지고 있다.

  • 소유권을 주장할 수 있다.

Remix란?

브라우저에서 Solidity 개발을 위한 IDE(통합 개발 환경)

  • 스마트 컨트랙트 개발과 구축을 지원
  • 사설망이나 테스트넷의 이더리움 블록체인에 연결헤 스마트 계약 배포와 테스트를 할 수 있다.

Truffle이란?

이더리움 기반 디앱을 쉽게 개발할 수 있도록 도와주는 블록체인 프레임워크

  • 이더리움에서 사용되는 solidity 라는 언어로 개발,
  • 테스트 및 배포까지 쉽게 관리할 수 있게 도와주는 프레임워크

2. 구현 목표

ERC-721을 개발
Truffle을 이용한 배포


3. 구현 계획 💭

eth-lightwallet 모듈에 내장되어 있는 함수를 사용하여 개발

  • 랜덤한 니모닉 코드를 생성
  • 니모닉을 시드로 키스토어를 생성

Postman을 사용하여 결과 확인
fs 모듈을 이용한 키스토어 로컬 저장


4. 🖇 실습해보자

  1. 기본설정
  • 폴더 만들기

    mkdir newErc721 //폴더생성
    cd newErc721 //폴더 진입
    truffle init //트러플 초기화
    npm init //npm초기화
  • 모듈 설치

    npm init 
    npm install -g truffle
    npm install -g ganache-cli
    npm install @openzeppelin/contracts
    npm i --save truffle-hdwallet-provider
    npm i dotenv
- package.json 초기화
- 트러플설치
- 가나슈설치
- 오픈제플린 설치
- HDWalletProvider 설치
- dotenv 설치
  1. truffle-config.js 를 열고, solc 수정
//상단에 적어주세요
// truffle-config.js
var HDWalletProvider = require("truffle-hdwallet-provider");
require('dotenv').config()
const mnemonic = process.env.MNEMONIC
const infuraEndpoint = process.env.INFURA_ENDPOINT
  • 솔리디티 코드를 수정해서 맞춰줌

// ropsten 주석 풀고 
networks: {
    ropsten: {
      provider: function() {
        return new HDWalletProvider(mnemonic, infuraEndpoint)
      },
      network_id: '*',
      gas: 30000000
    }
  • 네트워크를 ropsten 변경해서 네트워크 설정 변경
  1. contracts 폴더 안에 새로 MyNFTs.sol 코드 입력
//Contract based on [https://docs.openzeppelin.com/contracts/3.x/erc721](https://docs.openzeppelin.com/contracts/3.x/erc721)
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";

contract MyNFTs is ERC721URIStorage, Ownable {
    using Counters for Counters.Counter;
    Counters.Counter private _tokenIds;

    constructor() public ERC721("JHNFTs", "JNFT") {}

    function mintNFT(string memory tokenURI)
        public onlyOwner
        returns (uint256)
    {
        _tokenIds.increment();

        uint256 newItemId = _tokenIds.current();
        _mint(msg.sender, newItemId);
        _setTokenURI(newItemId, tokenURI);

        return newItemId;
    }
}
  1. openZeppelin 패키지 설치
    npm i @openzeppelin/contracts

  2. Migrations.sol 파일 수정

const Migrations = artifacts.require('Migrations');
const MyNFTs = artifacts.require('MyNFTs.sol'); // MyNFTs.sol 파일 추가

module.exports = function (deployer) {
	deployer.deploy(Migrations);
	deployer.deploy(MyNFTs); // MyNFTs를 배포에 추가
};
  1. 환경변수 파일 생성하여 니모닉과, infura ropsten endpoint 작성

  1. Truffle 이용해 컨트랙트를 배포
  • 새로 터미널을 열어서 truffle migrate --compile-all --network truffle
  1. Truffle console을 통해 ropsten에 진입
    truffle console --network ropsten

  2. 작성한 solidity 파일에서 입력한 NFT설정과 일치하는지 확인

truffle(ropsten)> instance = await MyNFT.deployed()
undefined
truffle(ropsten)> instance.name()
'JHNFT'
truffle(ropsten)> instance.symbol()
'JNFT'

https://ropsten.etherscan.io/tx/0x2aff985cd7057f5a95254acfa8a62526665c5485423d650d3af489ccb19ab9ae

컨트랙트 해시 0x4e3f4a1234a16ad2adf7dcfa4b473a31160a686ca29563f31bbf0a46ac2606c9

🥹 회고

배포가 안 되서 죽을번함

출처
https://eips.ethereum.org/EIPS/eip-721
https://docs.openzeppelin.com/contracts/3.x/api/token/erc721

0개의 댓글