知乎专栏 | 多维度架构 |
OpenZeppelin is an open framework of reusable and secure smart contracts in the Solidity language.
Github: https://github.com/OpenZeppelin/zeppelin-solidity
// contracts/GLDToken.sol // SPDX-License-Identifier: MIT pragma solidity ^0.8.0; import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; contract GLDToken is ERC20 { constructor(uint256 initialSupply) ERC20("Gold", "GLD") { _mint(msg.sender, initialSupply); } }
创建项目目录
neo@MacBook-Pro ~/ethereum/truffle % mkdir TokenERC827 neo@MacBook-Pro ~/ethereum/truffle % cd TokenERC827 neo@MacBook-Pro ~/ethereum/truffle/TokenERC827 % truffle init Downloading... Unpacking... Setting up... Unbox successful. Sweet! Commands: Compile: truffle compile Migrate: truffle migrate Test contracts: truffle test
安装 zeppelin-solidity
neo@MacBook-Pro ~/ethereum/truffle/TokenERC827 % npm init -y Wrote to /Users/neo/ethereum/truffle/TokenERC827/package.json: { "name": "TokenERC827", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC" } neo@MacBook-Pro ~/ethereum/truffle/TokenERC827 % npm install -E zeppelin-solidity npm notice created a lockfile as package-lock.json. You should commit this file. npm WARN TokenERC827@1.0.0 No description npm WARN TokenERC827@1.0.0 No repository field. + zeppelin-solidity@1.7.0 added 8 packages in 2.591s ╭─────────────────────────────────────╮ │ │ │ Update available 5.6.0 → 5.7.1 │ │ Run npm i npm to update │ │ │ ╰─────────────────────────────────────╯
合约被安装在 node_modules/zeppelin-solidity/contracts 目录
创建合约和测试文件
neo@MacBook-Pro ~/ethereum/truffle/TokenERC827 % truffle create contract TokenERC827 neo@MacBook-Pro ~/ethereum/truffle/TokenERC827 % truffle create test TokenERC827
编辑合约文件
pragma solidity ^0.4.19; import "zeppelin-solidity/contracts/token/ERC827/ERC827Token.sol"; contract TokenERC827 is ERC827Token { string public name = "NetkillerCoin"; string public symbol = "NKC"; uint8 public decimals = 4; uint256 public INITIAL_SUPPLY = 1000000; function TokenERC827() public { // constructor totalSupply = INITIAL_SUPPLY; balances[msg.sender] = INITIAL_SUPPLY; } }