Avatar
0
tvd12 Enlightened
tvd12 Enlightened
Deploy NFT Game smart contract lên mạng private
Dể deploy NFT Game smart contract lên mạng private, chúng ta cần trải qua các bước sau:
  1. Tải ERC721 contract tại đây: https://github.com/OpenZeppelin/openzeppelin-contracts/tree/master/contracts
  2. 1 Copy toàn bộ thư mục contracts của <code>openzeppelin-contracts</code> vào thư mục contracts của truffles
  3. 2 Sau khi copy chúng ta sẽ cấu trúc thư mục kiểu này:
  4. Tạo 1 contract mới thế này:
    // contracts/GameItem.sol
    // SPDX-License-Identifier: MIT
    pragma solidity ^0.8.0;
    
    import "./extensions/ERC721URIStorage.sol";
    import "./../../utils/Counters.sol";
    
    contract GameItem is ERC721URIStorage {
        using Counters for Counters.Counter;
        Counters.Counter private _tokenIds;
    
        constructor() public ERC721("GameItem", "ITM") {}
    
        function awardItem(address player, string memory tokenURI)
            public
            returns (uint256)
        {
            _tokenIds.increment();
    
            uint256 newItemId = _tokenIds.current();
            _mint(player, newItemId);
            _setTokenURI(newItemId, tokenURI);
    
            return newItemId;
        }
    }
    
  1. Sửa file truffle-config.js thế này:
    module.exports = {
        networks: {
            development: {
                host: "localhost",
                port: 8545,
                network_id: "*",
                gas: 0,
                from: "0x3c31b4b9d6c24bad29498f99aaa6914231362c7f"
            }
        },
        compilers: {
            solc: {
                version: "0.8.0" 
            }
        }
    };
    
  1. Sửa file 1_initial_migration.js thế này:
    const GameItem = artifacts.require("token/GameItem");
    
    module.exports = function (deployer) {
        deployer.deploy(
            GameItem
        );
    };
    
  1. Chạy truffle compile
  2. Chạy trufle migrate
  3. Sau khi đã deploy contract thành công chúng ta sẽ chạy <code>truffle console</code> và gọi thử các lệnh:
    let contract = await GameItem.deployed()
    let contract = contract.awardItem("0x0070c24257f49e73359cd8eff347bfd5e6081e23", "https://public.nftstatic.com/static/nft/zipped/4d7dfb4cdffc43a583c71c1bb5e07597_zipped.png")
    contract.ownerOf(1)
    contract.tokenURI(1)
    
Kết quả chúng ta sẽ nhận được:
truffle(development)&gt; contract.ownerOf(1)
'0x0070C24257F49E73359Cd8eff347bfD5e6081E23'
truffle(development)&gt; contract.tokenURI(1)
'https://public.nftstatic.com/static/nft/zipped/4d7dfb4cdffc43a583c71c1bb5e07597_zipped.png'
truffle(development)&gt; 
  • Answer
nft nft game
Remain: 5
1 Answer
Avatar
monkey Enlightened
monkey Enlightened
Sharing
  • 0
  • Reply