├── .gitignore ├── LICENSE ├── README.md ├── contracts └── BasicToken.sol ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | imgs/ 3 | chain_config/ -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 yaohsin 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # This is a tutorial of deploying a smart contract. 2 | 3 | For more information, please visit [here](https://medium.com/@yaohsin/%E4%BD%BF%E7%94%A8node-js%E9%83%A8%E7%BD%B2%E6%99%BA%E8%83%BD%E5%90%88%E7%B4%84-smart-contract-520534305aaf#.6oe2iltom). 4 | -------------------------------------------------------------------------------- /contracts/BasicToken.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.4.8; 2 | 3 | /* 4 | * Basic token 5 | * Basic version of StandardToken, with no allowances 6 | */ 7 | contract BasicToken { 8 | address public owner; 9 | uint public totalSupply; 10 | mapping(address => uint) balances; 11 | 12 | event Transfer(address indexed from, address indexed to, uint value); 13 | 14 | function BasicToken() { 15 | owner = msg.sender; 16 | balances[owner] = 10000; 17 | } 18 | 19 | function transfer(address _to, uint _value) { 20 | balances[msg.sender] = safeSub(balances[msg.sender], _value); 21 | balances[_to] = safeAdd(balances[_to], _value); 22 | Transfer(msg.sender, _to, _value); 23 | } 24 | 25 | function balanceOf(address _owner) constant returns (uint balance) { 26 | return balances[_owner]; 27 | } 28 | 29 | function safeSub(uint a, uint b) internal returns (uint) { 30 | assert(b <= a); 31 | return a - b; 32 | } 33 | 34 | function safeAdd(uint a, uint b) internal returns (uint) { 35 | uint c = a + b; 36 | assert(c>=a && c>=b); 37 | return c; 38 | } 39 | 40 | function assert(bool assertion) internal { 41 | if (!assertion) { 42 | throw; 43 | } 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const fs = require('fs'); 2 | const Web3 = require('web3'); 3 | const solc = require('solc'); 4 | 5 | /* 6 | * connect to ethereum node 7 | */ 8 | const ethereumUri = 'http://localhost:8540'; 9 | const address = '0x004ec07d2329997267Ec62b4166639513386F32E'; // user 10 | 11 | let web3 = new Web3(); 12 | web3.setProvider(new web3.providers.HttpProvider(ethereumUri)); 13 | 14 | if(!web3.isConnected()){ 15 | throw new Error('unable to connect to ethereum node at ' + ethereumUri); 16 | }else{ 17 | console.log('connected to ehterum node at ' + ethereumUri); 18 | let coinbase = web3.eth.coinbase; 19 | console.log('coinbase:' + coinbase); 20 | let balance = web3.eth.getBalance(coinbase); 21 | console.log('balance:' + web3.fromWei(balance, 'ether') + " ETH"); 22 | let accounts = web3.eth.accounts; 23 | console.log(accounts); 24 | 25 | if (web3.personal.unlockAccount(address, 'user')) { 26 | console.log(`${address} is unlocaked`); 27 | }else{ 28 | console.log(`unlock failed, ${address}`); 29 | } 30 | } 31 | 32 | /* 33 | * Compile Contract and Fetch ABI 34 | */ 35 | let source = fs.readFileSync("./contracts/BasicToken.sol", 'utf8'); 36 | 37 | console.log('compiling contract...'); 38 | let compiledContract = solc.compile(source); 39 | console.log('done'); 40 | 41 | for (let contractName in compiledContract.contracts) { 42 | // code and ABI that are needed by web3 43 | // console.log(contractName + ': ' + compiledContract.contracts[contractName].bytecode); 44 | // console.log(contractName + '; ' + JSON.parse(compiledContract.contracts[contractName].interface)); 45 | var bytecode = compiledContract.contracts[contractName].bytecode; 46 | var abi = JSON.parse(compiledContract.contracts[contractName].interface); 47 | } 48 | 49 | console.log(JSON.stringify(abi, undefined, 2)); 50 | 51 | /* 52 | * deploy contract 53 | */ 54 | let gasEstimate = web3.eth.estimateGas({data: '0x' + bytecode}); 55 | console.log('gasEstimate = ' + gasEstimate); 56 | let MyContract = web3.eth.contract(abi); 57 | console.log('deploying contract...'); 58 | let myContractReturned = MyContract.new( { 59 | from: address, 60 | data: '0x'+ bytecode, 61 | gas: gasEstimate + 50000 62 | }, function (err, myContract) { 63 | if (!err) { 64 | // NOTE: The callback will fire twice! 65 | // Once the contract has the transactionHash property set and once its deployed on an address. 66 | 67 | // e.g. check tx hash on the first call (transaction send) 68 | if (!myContract.address) { 69 | console.log(`myContract.transactionHash = ${myContract.transactionHash}`); // The hash of the transaction, which deploys the contract 70 | 71 | // check address on the second call (contract deployed) 72 | } else { 73 | console.log(`myContract.address = ${myContract.address}`); // the contract address 74 | global.contractAddress = myContract.address; 75 | } 76 | 77 | // Note that the returned "myContractReturned" === "myContract", 78 | // so the returned "myContractReturned" object will also get the address set. 79 | } else { 80 | console.log(err); 81 | } 82 | }); 83 | 84 | 85 | (function wait () { 86 | setTimeout(wait, 1000); 87 | })(); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "deploy-contract", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "ISC", 11 | "dependencies": { 12 | "express": "^4.14.1", 13 | "solc": "^0.4.9", 14 | "web3": "^0.18.2" 15 | } 16 | } 17 | --------------------------------------------------------------------------------