Home | 簡體中文 | 繁體中文 | 雜文 | 知乎專欄 | Github | OSChina 博客 | 雲社區 | 雲棲社區 | Facebook | Linkedin | 視頻教程 | 打賞(Donations) | About
知乎專欄多維度架構 | 微信號 netkiller-ebook | QQ群:128659835 請註明“讀者”

15.9. Zeppelin Solidity - OpenZeppelin is a library for writing secure Smart Contracts on Ethereum.

OpenZeppelin is an open framework of reusable and secure smart contracts in the Solidity language.

網站: https://openzeppelin.org

Github: https://github.com/OpenZeppelin/zeppelin-solidity

15.9.1. ERC20

			
pragma solidity ^0.4.19;
import "zeppelin-solidity/contracts/token/StandardToken.sol";

contract NeoCoin is StandardToken {
  string public name = "NeoCoin";
  string public symbol = "BLC";
  uint8 public decimals = 4;
  uint256 public INITIAL_SUPPLY = 666666;
  function NeoCoin() {
    totalSupply = INITIAL_SUPPLY;
    balances[msg.sender] = INITIAL_SUPPLY;
  }
}			
			
			

15.9.2. ERC872

創建項目目錄

			
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;
  }
}