Deploy token contract on private chain
Install library
install Truffle and Solidity
$ npm --v 4.1.2 $ sudo npm install -g truffle $ truffle version Truffle v4.0.3 (core: 4.0.3) Solidity v0.4.18 (solc-js) $ cd Desktop $ mkdir myproject && cd myproject $ truffle init Downloading... Unpacking... Setting up... Unbox successful. Sweet! Commands: Compile: truffle compile Migrate: truffle migrate Test contracts: truffle test
install OpenZeppelin
$ npm init About to write to /Users/yen/Desktop/myproject/package.json: { "name": "myproject", "version": "1.0.0", "description": "", "main": "truffle.js", "directories": { "test": "test" }, "scripts": { "compile": "truffle compile", "deploy": "truffle migrate --network test --reset", "test": "echo \"Error: no test specified\" && exit 1" }, "author": "Yen", "license": "ISC" } $ npm install zeppelin-solidity [email protected] /Users/yen/Desktop/myproject └── [email protected]
Compile contract
Clone the contract from zeppelin-soliditycontracts/examples/SimpleToken.sol
to our project /contracts
.
YenToken.sol
pragma solidity ^0.4.18; import 'zeppelin-solidity/contracts/token/StandardToken.sol'; contract YenToken is StandardToken { string public constant name = "YenToken"; string public constant symbol = "YEN"; uint8 public constant decimals = 18; uint256 public constant INITIAL_SUPPLY = 99999 * (10 ** uint256(decimals)); function YenToken() public { totalSupply = INITIAL_SUPPLY; balances[msg.sender] = INITIAL_SUPPLY; } }
Using below codes to compile contract by Truffle, it will save the result to /build/contracts
folder.
compile it
$ npm run compile > [email protected] compile /Users/yen/Desktop/myproject > truffle compile
Then I can find the
ABI
andbytecode
in/build/contracts/YenToken.json
. If I useWeb3
to deploy the contract such as below program, I will need them.
const Web3 = require('web3'); const fs = require('fs'); const ethereumUri = 'http://localhost:8487'; const web3 = new Web3(); web3.setProvider(new web3.providers.HttpProvider(ethereumUri)); ... const contract = JSON.parse(fs.readFileSync("./build/contracts/YenCoin.json", 'utf8')); const gasEstimate = web3.eth.estimateGas({ data: contract.bytecode }); const MyContract = web3.eth.contract(contract.abi); const myContractReturned = MyContract.new([], { from: web3.eth.coinbase, data: contract.bytecode, gas: gasEstimate }, function (err, myContract) { if (!err) { if (!myContract.address) { console.log(`myContract.transactionHash = ${myContract.transactionHash}`); } else { console.log(`myContract.address = ${myContract.address}`); global.contractAddress = myContract.address; } } else { console.log(err); } });
Deploy contract
Make sure the nodes of private chain are launched. Then prepares the configuration of Truffle.
truffle.js
module.exports = { // See <http://truffleframework.com/docs/advanced/configuration> networks: { test: { host: "localhost", port: 8487, network_id: 36291 // Match any network id by "*" } }, solc: { optimizer: { enabled: true, runs: 200 } } };
Prepare the deploy script.
1_initial_migration.js
const Contract = artifacts.require("./Migrations.sol"); module.exports = function(deployer) { };
2_deploy_yen_token.js
const Contract = artifacts.require("./YenToken.sol"); module.exports = function(deployer, network, accounts) { const gasPrice = Contract.web3.eth.gasPrice; const gas = Contract.web3.eth.estimateGas({ data: Contract.bytecode }); console.log(`network is ${network}`); console.log(`accounts are ${accounts}`); console.log(`gas price is ${gasPrice} wei`); console.log(`pending gas limit is ${Contract.web3.eth.getBlock("pending").gasLimit}`); console.log(`estimate gas is ${gas}`); deployer.deploy(Contract, { overwrite: true, gasPrice, gas }); };
deploy
$ npm run deploy > [email protected] deploy /Users/yen/Desktop/myproject > truffle migrate --network test --reset Using network 'test'. Running migration: 1_initial_migration.js Running migration: 2_deploy_yen_token.js network is test accounts are 0x4d0362700bcdc69e2e4e083c3f098ca55efd1119 gas price is 18000000000 wei pending gas limit is 48985297 estimate gas is 666767 Deploying YenToken... ... 0x82c342f48412daef798695fd557a3992764c9716222b3467223fd248304bc384 YenToken: 0xf13c9660190749124ad90c8defc77cc6c5d36494 Saving successful migration to network... ... 0x891ccf201117e75dbcefe627070a037aa852fb8b83479b16dd533e703381ef28 Saving artifacts...
on miner2 console
INFO [12-20|16:40:44] Submitted transaction fullhash=0x891ccf201117e75dbcefe627070a037aa852fb8b83479b16dd533e703381ef28 recipient=0xb883cDEdCbC8A09f4E9745de367F0fF124A4fF86 INFO [12-20|16:40:47] Imported new chain segment blocks=1 txs=0 mgas=0.000 elapsed=218.881µs mgasps=0.000 number=23 hash=0c27c3…a2526a
Truffle will save the contract address to /build/contracts/YenToken.json
.
{
"contractName": "YenToken",
"abi": [ ... ],
"id": 97,
"name": "SourceUnit",
"src": "0:444:1"
},
"compiler": {
"name": "solc",
"version": "0.4.18+commit.9cf6e910.Emscripten.clang"
},
"networks": {
"36291": {
"events": {},
"links": {},
"address": "0xf13c9660190749124ad90c8defc77cc6c5d36494"
}
},
"schemaVersion": "1.0.1",
"updatedAt": "2017-12-20T08:40:50.071Z"
}
files tree
$ tree . ├── build │ └── contracts │ ├── BasicToken.json │ ├── ERC20.json │ ├── ERC20Basic.json │ ├── Migrations.json │ ├── SafeMath.json │ ├── StandardToken.json │ └── YenToken.json ├── contracts │ ├── Migrations.sol │ └── YenToken.sol ├── migrations │ ├── 1_initial_migration.js │ └── 2_deploy_yen_token.js ├── node_modules ├── package.json ├── test └── truffle.js
Issue
exceeds block gas limit
When I deploy contract, I got the error.
Error encountered, bailing. Network state unknown. Review successful transactions manually. Error: exceeds block gas limit at Object.InvalidResponse (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:41484:16) at /usr/local/lib/node_modules/truffle/build/cli.bundled.js:328866:36 at /usr/local/lib/node_modules/truffle/build/cli.bundled.js:176178:11 at /usr/local/lib/node_modules/truffle/build/cli.bundled.js:324536:9 at XMLHttpRequest.request.onreadystatechange (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:327565:7) at XMLHttpRequestEventTarget.dispatchEvent (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:176407:18) at XMLHttpRequest._setReadyState (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:176697:12) at XMLHttpRequest._onHttpResponseEnd (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:176852:12) at IncomingMessage.<anonymous> (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:176812:24) at emitNone (events.js:91:20)
I notice that pending gas limit of block(48985297) is greater than the gas limit definition in genesis block(4700000). So I increase the gas limit of genesis block from
0x47b760
to0x2faf080
, then rebuild the private chain.