- Tải ERC721 contract tại đây: https://github.com/OpenZeppelin/openzeppelin-contracts/tree/master/contracts
- 1 Copy toàn bộ thư mục contracts của
openzeppelin-contractsvào thư mục contracts của truffles - 2 Sau khi copy chúng ta sẽ cấu trúc thư mục kiểu này:
- 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;
}
}
- 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"
}
}
};
- Sửa file 1_initial_migration.js thế này:
const GameItem = artifacts.require("token/GameItem");
module.exports = function (deployer) {
deployer.deploy(
GameItem
);
};
- Chạy truffle compile
- Chạy trufle migrate
- Sau khi đã deploy contract thành công chúng ta sẽ chạy
truffle consolevà 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)> contract.ownerOf(1) '0x0070C24257F49E73359Cd8eff347bfD5e6081E23' truffle(development)> contract.tokenURI(1) 'https://public.nftstatic.com/static/nft/zipped/4d7dfb4cdffc43a583c71c1bb5e07597_zipped.png' truffle(development)>