• open your truffle project folder, and then run:
npm init -y

it will generate a file package.json in the root folder

  • install openzeppelin contracts:
npm install @openzeppelin/contracts
  • add a test solidity file called MyToken.sol to folder contracts/, and then put these test code:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract MyToken is ERC20, Ownable {
    constructor() ERC20("MyToken", "MTK") {}
    function mint(address to, uint256 amount) public onlyOwner {
        _mint(to, amount);
    }
}

more usage, see node_modules/@openzeppelin/contracts/README.md

note: pragma solidity in the MyToken.sol above is ^0.8.4, and it is 0.8.0 in "@openzeppelin/contracts/token/ERC20/ERC20.sol", but it also compile successful

  • compile this project:
truffle compile

more details, watch this: /playlist?list=PLbbtODcOYIoFdQ37ydykQNO-MNGER6mtd

添加新评论