Ethereum-Truffle
Overview:
Install:
$ npm install -g truffle $ truffle init $ truffle -v Truffle v4.0.6
Quick Start:
$ truffle init ./ ├── contracts/ │ └── Migrations.sol ├── migrations/ │ └── 1_initial_migration.js ├── test/ ├── truffle-config.js └── truffle.js
Tutorial: OpenZeppelin ERC20 トークン
$ truffle init ./ ├── contracts/ │ └── Migrations.sol ├── migrations/ │ └── 1_initial_migration.js ├── test/ ├── truffle-config.js └── truffle.js $ npm init -f { "name": "test", "version": "1.0.0", "description": "", "main": "truffle-config.js", "directories": { "test": "test" }, "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC" } $ npm install zeppelin-solidity --save file: contracts/MyToken.sol edit: | pragma solidity ^0.4.18; import "zeppelin-solidity/contracts/token/ERC20/StandardToken.sol"; contract MyToken is StandardToken { string public name = "MyToken"; string public symbol = "MTKN"; uint public decimals = 18; function MyToken(uint initialSupply) public { totalSupply_ = initialSupply; balances[msg.sender] = initialSupply; } } file: migrations/2_deploy_my_token.js edit: | const MyToken = artifacts.require('./MyToken.sol') module.exports = (deployer) => { const initialSupply = 50000e18 deployer.deploy(MyToken, initialSupply) } $ truffle compile $ truffle develop > migrate > myToken = MyToken.at(MyToken.address) > myToken.name() > web3.eth.accounts > myToken.totalSupply() > myToken.balanceOf(web3.eth.accounts[0]) > myToken.balanceOf(web3.eth.accounts[1]) > myToken.transfer(web3.eth.accounts[1], 1000e18) > myToken.balanceOf(web3.eth.accounts[0]) BigNumber { s: 1, e: 22, c: [ 490000000 ] } > myToken.balanceOf(web3.eth.accounts[1]) BigNumber { s: 1, e: 21, c: [ 10000000 ] } > .exit
Tutorial: Pet Shop
$ truffle unbox pet-shop ./ ├── box-img-lg.png ├── box-img-sm.png ├── bs-config.json ├── package-lock.json ├── package.json ├── node_modules/ # Module ├── src/ # │ ├── css │ ├── fonts │ ├── images │ ├── index.html │ ├── js │ └── pets.json ├── contracts/ # Solidity source files │ └── Migrations.sol ├── migrations/ # Deploy │ └── 1_initial_migration.js ├── test/ # └── truffle.json # Truffle configuration file $ truffle create contract Adoption pragma solidity ^0.4.4; contract Adoption { function Adoption() { // constructor } } file: contracts/Adoption.sol edit: | pragma solidity ^0.4.18; contract Adoption { address[16] public adopters; function adopt(uint petId) public returns(uint) { require(petId >= 0 && petId <= 15); adopters[petId] = msg.sender; return petId; } function getAdopters() public returns (address[16]) { return adopters; } } file: migrations/2_deploy_contracts.js edit: | const Adoption = artifacts.require("Adoption") module.exports = (deployer) => { deployer.deploy(Adoption) } file: test/TestAdoption.sol edit: | pragma solidity ^0.4.11; import "truffle/Assert.sol"; import "truffle/DeployedAddresses.sol"; import "../contracts/Adoption.sol"; contract TestAdoption { Adoption adoption = Adoption(DeployedAddresses.Adoption()); function testUserCanAdoptPet() { uint returnedId = adoption.adopt(8); uint expected = 8; Assert.equal(returnedId, expected, "Adoption of pet ID 8 should be recorded."); } function testGetAdopterAddressByPetId() { address expected = this; address adopter = adoption.adopters(8); Assert.equal(adopter, expected, "Owner of pet ID 8 should be recorded."); } function testGetAdopterAddressByPetIdInArray() { address expected = this; address[16] memory adopters = adoption.getAdopters(); Assert.equal(adopters[8], expected, "Owner of pet ID 8 should be recorded."); } } $ truffle develop > compile > migrate > test