Notice
Recent Posts
Recent Comments
Link
ยซ   2025/01   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

JS Dev Blog

Truffle๋กœ ERC 721 ๊ฐœ๋ฐœ ๋ณธ๋ฌธ

BlockChain

Truffle๋กœ ERC 721 ๊ฐœ๋ฐœ

chacot 2022. 6. 9. 22:31

Truffle๋กœ ERC 721 ๊ฐœ๋ฐœ

๐Ÿง‘๐Ÿป‍๐Ÿ’ป ๊ฐœ๋ฐœ ๋ชฉํ‘œ

Truffle์„ ์ด์šฉํ•ด์„œ ERC-721์„ ๊ฐœ๋ฐœํ•œ๋‹ค. Openzeppelin ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ๋ฉ”์ธ๋„ท์ด ์•„๋‹ˆ๋ผ ๊ฐ€์ƒํ™˜๊ฒฝ์—์„œ ํ…Œ์ŠคํŠธํ•˜๊ธฐ ์œ„ํ•ด Ganache๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. CLI๋Š” ํ•œ ๋ฒˆ๋„ ์‚ฌ์šฉํ•ด๋ณธ์  ์—†๊ธฐ ๋•Œ๋ฌธ์— ์ผ๋ถ€๋Ÿฌ Ganache-cli๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค.

ERC-721๋ž€?

ERC-20๊ฐ™์€ ๋Œ€์ฒด ๊ฐ€๋Šฅ(fungible)ํ† ํฐ๊ณผ ๋‹ค๋ฅด๊ฒŒ ERC721์€ ๋Œ€์ฒดํ•  ์ˆ˜ ์—†๋Š”(non-fungible) ํ† ํฐ ํ‘œ์ค€์ด๋‹ค. NFT๋Š” ๋ธ”๋ก์ฒด์ธ ํ”Œ๋žซํผ์—์„œ ๋””์ง€ํ„ธ ์„œ๋ช…์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•œ ์†Œ์œ ๊ถŒ์„ ์ธ์ •๋ฐ›๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋œ๋‹ค. ERC721 ํ‘œ์ค€์€ ์ฆ์„œ์— ์˜ํ•ด ๊ทธ ์†Œ์œ ๊ถŒ์ด ๊ณ ์œ ํ•˜๊ฒŒ ์ถ”์ ๋  ์ˆ˜ ์žˆ๋Š” ํ•œ, ๊ทธ ๋Œ€์ƒ์˜ ์ข…๋ฅ˜์— ๋Œ€ํ•ด ์–ด๋–ค ์ œํ•œ์ด๋‚˜ ๊ทœ์ •์„ ๋‘์ง€ ์•Š์œผ๋ฉด, ์ด๋Ÿฌํ•œ ์ถ”์ ์€ 256๋น„ํŠธ ์‹๋ณ„์ž์— ์˜ํ•ด ์ด๋ฃจ์–ด์ง„๋‹ค.

Truffle?

์Šค๋งˆํŠธ ์ปจํŠธ๋ž™ํŠธ๋ฅผ ๊ฐœ๋ฐœ ํ•  ๋•Œ ๊ฐœ๋ฐœ ๋ฐ ํ…Œ์ŠคํŠธ, ๋ฐฐํฌํ™˜๊ฒฝ์„ ์ œ๊ณตํ•ด์ฃผ๋Š” ํ”„๋ ˆ์ž„์›Œํฌ

OpenZeppelin ?

์Šค๋งˆํŠธ ์ปจํŠธ๋ž™ํŠธ ๊ฐœ๋ฐœ์— ๋„์›€์„ ์ฃผ๋Š” ํ”„๋ ˆ์ž„์›Œํฌ

Ganacahe ?

์Šค๋งˆํŠธ ์ปจํŠธ๋ž™ํŠธ๋ฅผ ๋กœ์ปฌ ํ™˜๊ฒฝ์—์„œ ํ…Œ์ŠคํŠธ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋„์™€์คŒ

๐Ÿ”จ ๊ฐœ๋ฐœ๊ณผ์ •

๊ฐœ๋ฐœ ํ™˜๊ฒฝ ์„ธํŒ…

truffle ์„ค์น˜

npm install -g truffle

ํ”„๋กœ์ ํŠธ ์ดˆ๊ธฐํ™”

truffle init
npm init

ganache ์„ค์น˜

npm install -g ganache-cli

์˜คํ”ˆ์ œํ”Œ๋ฆฐ ์„ค์น˜

npm install @openzeppelin/contracts

ํ”„๋กœ์ ํŠธ ๋””๋ ‰ํ† ๋ฆฌ ๊ตฌ์„ฑ

  • Contracts : smart contract ์†Œ์Šค ( *.sol)
  • migrations : ๋ฐฐํฌ์— ํ•„์š”ํ•œ ํŒŒ์ผ
  • test : smart contract ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•œ ํด๋”
  • truffle-config.js : truffle ์…‹ํŒ… ํŒŒ์ผ

truffle-config.js

networks: {
    ganache: {
      host : '127.0.0.1', // local
      port : 8545,
      network_id: '*',
    },

compilers: {
        solc: {
            version: '0.8.7', 
            evmVersion: 'london',
        },
    },

network ์ •๋ณด ์ถ”๊ฐ€ (port 8545๋Š” ganache-cli)

*.sol ํŒŒ์ผ๊ณผ solidity version์„ ์ผ์น˜์‹œ์ผœ์ค€๋‹ค.

contracts/MyNFTs.sol

// 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("MyNFTs", "MNFT") {}

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

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

        return newItemId;
    }
}

์˜คํ”ˆ์ œํ”Œ๋ฆฐ์˜ Ownable, ERC721URIStorage๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค.

migration/1_initial_migration.js

const Migrations = artifacts.require("Migrations");
const MyNFTs = artifacts.require('MyNFTs')

module.exports = function (deployer) {
  deployer.deploy(Migrations);
  deployer.deploy(MyNFTs);

};

ganache-cli
truffle migrate —compile-all —network ganache

ganache cli๋ฅผ ์—ด๊ณ  ๊ฐ€๋‚˜์Šˆ์— ์—ฐ๊ฒฐํ•ด์„œ ๋ชจ๋“  ์ปจํŠธ๋ž™ํŠธ๋ฅผ ๋ฐฐํฌ

 

ganache-cli ์‹คํ–‰์„ฑ๊ณต ํ™”๋ฉด

 

 

truffle console —network ganache

truffle console ์‹คํ–‰

 

๋ฐฐํฌํ•œ ์ปจํŠธ๋ž™ํŠธ์˜ ์ธ์Šคํ„ด์Šค ๋ฐ›๊ณ  ํ™•์ธ

 

mintNFT ํ•จ์ˆ˜ ์‹คํ–‰ํ•˜์—ฌ NFT ๋ฐœํ–‰

 

 

๋ฐœํ–‰ํ•œ tokenURIํ™•์ธ

 

 

๐Ÿ”ฅ Ropsten Testnet์— ๋ฐฐํฌํ•ด๋ณด๊ธฐ

HDWalletProvider, dotenv ์„ค์น˜ํ•จ

 

npm i dotenv

npm i --save truffle-hdwallet-provider

 

.envํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๊ณ  MNEMONIC์— ๋‹ˆ๋ชจ๋‹‰์ฝ”๋“œ๋ฅผ, INFURA_ENDPOINT์— ropsten network ์—”๋“œํฌ์ธํŠธ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.

๐Ÿซต github์— ์˜ฌ๋ฆด๋•Œ๋Š” ๊ผญ .gitignore๋กœ .envํŒŒ์ผ์„ ์ œ์™ธ์‹œ์ผœ ์ค„ ๊ฒƒ(๋‹ˆ๋ชจ๋‹‰ ์ฝ”๋“œ ๋“ฑ์€ ๊ณต์œ ๋˜๋ฉด ์•ˆ ๋จ)

 

truffle-config.js

var HDWalletProvider = require("truffle-hdwallet-provider");
require('dotenv').config()
const mnemonic = process.env.MNEMONIC
const infuraEndpoint = process.env.INFURA_ENDPOINT

ropsten: {
      provider: () => new HDWalletProvider(mnemonic, infuraEndpoint),
      network_id: 3,       // Ropsten's id
      gas: 5500000,        // Ropsten has a lower block limit than mainnet
      // confirmations: 2,    // # of confirmations to wait between deployments. (default: 0)
      // timeoutBlocks: 200,  // # of blocks before a deployment times out  (minimum/default: 50)
      // skipDryRun: true     // Skip dry run before migrations? (default: false for public nets )
    },

ropsten network๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.

 

 

 

 

Truffle๋กœ ๋ฐฐํฌ

> truffle deploy --network ropsten

 

 

> truffle console --network ropsten

truffle console์„ ์ผ  ๋’ค mintNFT ํ•จ์ˆ˜ ์‚ฌ์šฉํ•ด์„œ NFT๋ฐœํ–‰

 

Etherscan Transaction Details

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

 

Ropsten Transaction Hash (Txhash) Details | Etherscan

Ropsten (ETH) detailed transaction info for txhash 0xa9b45b62b6437fd8054db942c80ebc674b683b06572c444f485368af868af294. The transaction status, block confirmation, gas fee, Ether (ETH), and token transfer are shown.

ropsten.etherscan.io

Transaction hash : 0xa9b45b62b6437fd8054db942c80ebc674b683b06572c444f485368af868af294

 

 

https://ropsten.etherscan.io/address/0x1ae0ce7fffd7fb058a5690fa6d1014f177e4219c

 

Contract Address 0x1ae0ce7fffd7fb058a5690fa6d1014f177e4219c | Etherscan

The Contract Address 0x1ae0ce7fffd7fb058a5690fa6d1014f177e4219c page allows users to view the source code, transactions, balances, and analytics for the contract address. Users can also interact and make transactions to the contract directly on Etherscan.

ropsten.etherscan.io

Contract Hash : 0x1AE0ce7FfFD7FB058A5690Fa6D1014F177E4219C

 

๐Ÿ‘€ ๊ฐœ๋ฐœ ํšŒ๊ณ 

Keep

ERC-721 ๊ฐœ๋…๊ณผ Truffle, Ganache์— ๋Œ€ํ•ด์„œ ๋‹ค์‹œ ํ•œ ๋ฒˆ ๊ณต๋ถ€ํ–ˆ๋‹ค. ํŠนํžˆ Ganache-cli๋ฅผ ์‚ฌ์šฉํ•ด ๋ณธ ์ ์ด ์œ ์ตํ–ˆ๋‹ค. Ropsten Network์— ์ง์ ‘ ๋ฐฐํฌํ•ด๋ณด๋Š” ๊ฒƒ๋„ ์ข‹์€ ๊ฒฝํ—˜์ด์—ˆ๋‹ค.

Problem

๋‹จ์ˆœํ•œ ํ•จ์ˆ˜๋งŒ ํ™œ์šฉํ•ด๋ด์„œ ์•„์ง ์Šค๋งˆํŠธ ์ปจํŠธ๋ž™ํŠธ๋ฅผ ์ž์œ ์ž์žฌ๋กœ ๋‹ค๋ฃจ์ง€ ๋ชปํ•˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค.

Try

๋‹ค์–‘ํ•œ ์Šค๋งˆํŠธ ์ปจํŠธ๋ž™ํŠธ ๊ธฐ๋Šฅ์— ๋Œ€ํ•ด์„œ ๊ณต๋ถ€ํ•˜๊ณ  ์‹ค์Šตํ•ด ๋ด์•ผ๊ฒ ๋‹ค. ์ถ”ํ›„์— ERC-721 ๊ด€๋ จ ํ”„๋กœ์ ํŠธ๋ฅผ ํ•˜๋ฉด

 

๐ŸŒˆ Github Repo

https://github.com/jongseokleedev/truffle_ERC721

 

GitHub - jongseokleedev/truffle_ERC721

Contribute to jongseokleedev/truffle_ERC721 development by creating an account on GitHub.

github.com