├── app.js ├── examples ├── 1_accounts.js ├── 7_inspecting_blocks.js ├── 8_web3_extras.js ├── 3_send_signed_transaction.js ├── 5_write_contract.js ├── 6_contract_event_stream.js ├── 2_read_smart_contract.js └── 4_deploy_contract.js ├── readme.md └── package.json /app.js: -------------------------------------------------------------------------------- 1 | // Your sandbox code goes here! 2 | // See ./examples/*.js for code exmaples 3 | -------------------------------------------------------------------------------- /examples/1_accounts.js: -------------------------------------------------------------------------------- 1 | const Web3 = require('web3') 2 | const rpcURL = '' // Your RCkP URL goes here 3 | const web3 = new Web3(rpcURL) 4 | const address = '' // Your account address goes here 5 | web3.eth.getBalance(address, (err, wei) => { balance = web3.utils.fromWei(wei, 'ether') }) 6 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Web3 by Example 2 | 3 | Learn how to use web3.js with these examples! 4 | 5 | Follow the step-by-step tutorial series on my Youtube channel here: 6 | 7 | https://www.youtube.com/playlist?list=PLS5SEs8ZftgXlCGXNfzKdq7nGBcIaVOdN 8 | 9 | ## Getting Started 10 | Install dependencies. 11 | 12 | `$ npm install` 13 | 14 | Run the sandbox `app.js` file with your code examples: 15 | 16 | `$ node app.js` 17 | 18 | See the code examples in the `./examples` directory for inspiration! :) 19 | -------------------------------------------------------------------------------- /examples/7_inspecting_blocks.js: -------------------------------------------------------------------------------- 1 | const Web3 = require('web3') 2 | const web3 = new Web3('https://mainnet.infura.io/hqRzEqFKv6IsjRxfVUWH') 3 | 4 | // get latest block number 5 | web3.eth.getBlockNumber().then(console.log) 6 | 7 | // // get latest block 8 | web3.eth.getBlock('latest').then(console.log) 9 | 10 | // get latest 10 blocks 11 | web3.eth.getBlockNumber().then((latest) => { 12 | for (let i = 0; i < 10; i++) { 13 | web3.eth.getBlock(latest - i).then(console.log) 14 | } 15 | }) 16 | 17 | // get transaction from specific block 18 | const hash = '0x66b3fd79a49dafe44507763e9b6739aa0810de2c15590ac22b5e2f0a3f502073' 19 | web3.eth.getTransactionFromBlock(hash, 2).then(console.log) 20 | -------------------------------------------------------------------------------- /examples/8_web3_extras.js: -------------------------------------------------------------------------------- 1 | const Web3 = require('web3') 2 | const web3 = new Web3('https://mainnet.infura.io/hqRzEqFKv6IsjRxfVUWH') 3 | 4 | // Get average gas price in wei from last few blocks median gas price 5 | web3.eth.getGasPrice().then((result) => { 6 | console.log(web3.utils.fromWei(result, 'ether') 7 | }) 8 | 9 | // Use sha256 Hashing function 10 | console.log(web3.utils.sha3('Dapp University')) 11 | 12 | // Use keccak256 Hashing function (alias) 13 | console.log(web3.utils.keccak256('Dapp University')) 14 | 15 | // Get a Random Hex 16 | console.log(web3.utils.randomHex(32)) 17 | 18 | // Get access to the underscore JS library 19 | const _ = web3.utils._ 20 | 21 | _.each({ key1: 'value1', key2: 'value2' }, (value, key) => { 22 | console.log(key) 23 | }) 24 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "web3-examples", 3 | "version": "1.0.0", 4 | "description": "How to use Web3.js with Dapp Univeristy", 5 | "main": "app.js", 6 | "directories": { 7 | "example": "examples" 8 | }, 9 | "scripts": { 10 | "test": "echo \"Error: no test specified\" && exit 1" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "git+ssh://git@github.com/dapp_univeristy/web3_examples.git" 15 | }, 16 | "keywords": [ 17 | "web3.js", 18 | "web3", 19 | "ethereum" 20 | ], 21 | "author": "gregory@dappuniversity.com", 22 | "license": "ISC", 23 | "bugs": { 24 | "url": "https://github.com/dapp_univeristy/web3_examples/issues" 25 | }, 26 | "homepage": "https://github.com/dapp_univeristy/web3_examples#readme", 27 | "dependencies": { 28 | "ethereumjs-tx": "^1.3.5", 29 | "web3": "^1.0.0-beta.34" 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /examples/3_send_signed_transaction.js: -------------------------------------------------------------------------------- 1 | var Tx = require('ethereumjs-tx') 2 | const Web3 = require('web3') 3 | const web3 = new Web3('https://ropsten.infura.io/YOUR_INFURA_API_KEY') 4 | 5 | const account1 = '' // Your account address 1 6 | const account2 = '' // Your account address 2 7 | 8 | const privateKey1 = Buffer.from('YOUR_PRIVATE_KEY_1', 'hex') 9 | const privateKey2 = Buffer.from('YOUR_PRIVATE_KEY_2', 'hex') 10 | 11 | web3.eth.getTransactionCount(account1, (err, txCount) => { 12 | // Build the transaction 13 | const txObject = { 14 | nonce: web3.utils.toHex(txCount), 15 | to: account2, 16 | value: web3.utils.toHex(web3.utils.toWei('0.1', 'ether')), 17 | gasLimit: web3.utils.toHex(21000), 18 | gasPrice: web3.utils.toHex(web3.utils.toWei('10', 'gwei')) 19 | } 20 | 21 | // Sign the transaction 22 | const tx = new Tx(txObject) 23 | tx.sign(privateKey1) 24 | 25 | const serializedTx = tx.serialize() 26 | const raw = '0x' + serializedTx.toString('hex') 27 | 28 | // Broadcast the transaction 29 | web3.eth.sendSignedTransaction(raw, (err, txHash) => { 30 | console.log('txHash:', txHash) 31 | // Now go check etherscan to see the transaction! 32 | }) 33 | }) 34 | -------------------------------------------------------------------------------- /examples/5_write_contract.js: -------------------------------------------------------------------------------- 1 | const Web3 = require('web3') 2 | const web3 = new Web3('https://ropsten.infura.io/YOUR_INFURA_API_KEY') 3 | 4 | const account1 = '' // Your account address 1 5 | const account2 = '' // Your account address 2 6 | 7 | const privateKey1 = Buffer.from('YOUR_PRIVATE_KEY_1', 'hex') 8 | const privateKey2 = Buffer.from('YOUR_PRIVATE_KEY_2', 'hex') 9 | 10 | // Read the deployed contract - get the addresss from Etherscan 11 | const contractAddress = '0xd03696B53924972b9903eB17Ac5033928Be7D3Bc' 12 | const contractABI = [{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"}],"name":"approve","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"standard","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"},{"name":"","type":"address"}],"name":"allowance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"name":"_from","type":"address"},{"indexed":true,"name":"_to","type":"address"},{"indexed":false,"name":"_value","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"_owner","type":"address"},{"indexed":true,"name":"_spender","type":"address"},{"indexed":false,"name":"_value","type":"uint256"}],"name":"Approval","type":"event"}] 13 | 14 | const contract = new web3.eth.Contract(abi, contractAddress) 15 | 16 | // Transfer some tokens 17 | web3.eth.getTransactionCount(account1, (err, txCount) => { 18 | 19 | const txObject = { 20 | nonce: web3.utils.toHex(txCount), 21 | gasLimit: web3.utils.toHex(800000), // Raise the gas limit to a much higher amount 22 | gasPrice: web3.utils.toHex(web3.utils.toWei('10', 'gwei')), 23 | to: contractAddress, 24 | data: contract.methods.transfer(account2, 1000).encodeABI() 25 | } 26 | 27 | const tx = new Tx(txObject) 28 | tx.sign(privateKey1) 29 | 30 | const serializedTx = tx.serialize() 31 | const raw = '0x' + serializedTx.toString('hex') 32 | 33 | web3.eth.sendSignedTransaction(raw, (err, txHash) => { 34 | console.log('err:', err, 'txHash:', txHash) 35 | // Use this txHash to find the contract on Etherscan! 36 | }) 37 | }) 38 | 39 | // Check Token balance for account1 40 | contract.methods.balanceOf(account1).call((err, balance) => { 41 | console.log({ err, balance }) 42 | }) 43 | 44 | // Check Token balance for account2 45 | contract.methods.balanceOf(account2).call((err, balance) => { 46 | console.log({ err, balance }) 47 | }) 48 | -------------------------------------------------------------------------------- /examples/6_contract_event_stream.js: -------------------------------------------------------------------------------- 1 | const Web3 = require('web3') 2 | const web3 = new Web3('https://mainnet.infura.io/hqRzEqFKv6IsjRxfVUWH') 3 | 4 | // OMG Token Contract 5 | const abi = [{"constant":true,"inputs":[],"name":"mintingFinished","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"}],"name":"approve","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"unpause","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_amount","type":"uint256"}],"name":"mint","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"paused","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"balance","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"finishMinting","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"pause","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_amount","type":"uint256"},{"name":"_releaseTime","type":"uint256"}],"name":"mintTimelocked","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"},{"name":"_spender","type":"address"}],"name":"allowance","outputs":[{"name":"remaining","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"payable":false,"type":"function"},{"anonymous":false,"inputs":[{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Mint","type":"event"},{"anonymous":false,"inputs":[],"name":"MintFinished","type":"event"},{"anonymous":false,"inputs":[],"name":"Pause","type":"event"},{"anonymous":false,"inputs":[],"name":"Unpause","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"},{"indexed":true,"name":"spender","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"}] 6 | const address = '0xd26114cd6EE289AccF82350c8d8487fedB8A0C07' 7 | 8 | const contract = new web3.eth.Contract(abi, address) 9 | 10 | // Get Contract Event Stream 11 | contract.getPastEvents( 12 | 'AllEvents', 13 | { 14 | fromBlock: 5854000, 15 | toBlock: 'latest' 16 | }, 17 | (err, events) => { console.log(events) } 18 | ) 19 | -------------------------------------------------------------------------------- /examples/2_read_smart_contract.js: -------------------------------------------------------------------------------- 1 | const Web3 = require('web3') 2 | const rpcURL = '' // Your RCP URL goes here 3 | const web3 = new Web3(rpcURL) 4 | 5 | const abi = [{"constant":true,"inputs":[],"name":"mintingFinished","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"}],"name":"approve","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"unpause","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_amount","type":"uint256"}],"name":"mint","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"paused","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"balance","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"finishMinting","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"pause","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_amount","type":"uint256"},{"name":"_releaseTime","type":"uint256"}],"name":"mintTimelocked","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"},{"name":"_spender","type":"address"}],"name":"allowance","outputs":[{"name":"remaining","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"payable":false,"type":"function"},{"anonymous":false,"inputs":[{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Mint","type":"event"},{"anonymous":false,"inputs":[],"name":"MintFinished","type":"event"},{"anonymous":false,"inputs":[],"name":"Pause","type":"event"},{"anonymous":false,"inputs":[],"name":"Unpause","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"},{"indexed":true,"name":"spender","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"}] 6 | const address = '0xd26114cd6EE289AccF82350c8d8487fedB8A0C07' 7 | 8 | const contract = new web3.eth.Contract(abi, address) 9 | 10 | contract.methods.totalSupply().call((err, result) => { console.log(result) }) 11 | contract.methods.name().call((err, result) => { console.log(result) }) 12 | contract.methods.symbol().call((err, result) => { console.log(result) }) 13 | contract.methods.balanceOf('0xd26114cd6EE289AccF82350c8d8487fedB8A0C07').call((err, result) => { console.log(result) }) 14 | -------------------------------------------------------------------------------- /examples/4_deploy_contract.js: -------------------------------------------------------------------------------- 1 | var Tx = require('ethereumjs-tx') 2 | const Web3 = require('web3') 3 | const web3 = new Web3('https://ropsten.infura.io/YOUR_INFURA_API_KEY') 4 | 5 | const account1 = '' // Your account address 1 6 | const account2 = '' // Your account address 2 7 | 8 | const privateKey1 = Buffer.from('YOUR_PRIVATE_KEY_1', 'hex') 9 | const privateKey2 = Buffer.from('YOUR_PRIVATE_KEY_2', 'hex') 10 | 11 | // Deploy the contract 12 | web3.eth.getTransactionCount(account1, (err, txCount) => { 13 | const data = '0x60806040526040805190810160405280600a81526020017f4441707020546f6b656e000000000000000000000000000000000000000000008152506000908051906020019061004f92919061014e565b506040805190810160405280600481526020017f44415050000000000000000000000000000000000000000000000000000000008152506001908051906020019061009b92919061014e565b506040805190810160405280600f81526020017f4441707020546f6b656e2076312e300000000000000000000000000000000000815250600290805190602001906100e792919061014e565b503480156100f457600080fd5b506000620f4240905080600460003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000208190555080600381905550506101f3565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061018f57805160ff19168380011785556101bd565b828001600101855582156101bd579182015b828111156101bc5782518255916020019190600101906101a1565b5b5090506101ca91906101ce565b5090565b6101f091905b808211156101ec5760008160009055506001016101d4565b5090565b90565b610b99806102026000396000f300608060405260043610610099576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806306fdde031461009e578063095ea7b31461012e57806318160ddd1461019357806323b872dd146101be5780635a3b7e421461024357806370a08231146102d357806395d89b411461032a578063a9059cbb146103ba578063dd62ed3e1461041f575b600080fd5b3480156100aa57600080fd5b506100b3610496565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100f35780820151818401526020810190506100d8565b50505050905090810190601f1680156101205780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34801561013a57600080fd5b50610179600480360381019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919080359060200190929190505050610534565b604051808215151515815260200191505060405180910390f35b34801561019f57600080fd5b506101a8610626565b6040518082815260200191505060405180910390f35b3480156101ca57600080fd5b50610229600480360381019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803573ffffffffffffffffffffffffffffffffffffffff1690602001909291908035906020019092919050505061062c565b604051808215151515815260200191505060405180910390f35b34801561024f57600080fd5b5061025861089b565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561029857808201518184015260208101905061027d565b50505050905090810190601f1680156102c55780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b3480156102df57600080fd5b50610314600480360381019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050610939565b6040518082815260200191505060405180910390f35b34801561033657600080fd5b5061033f610951565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561037f578082015181840152602081019050610364565b50505050905090810190601f1680156103ac5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b3480156103c657600080fd5b50610405600480360381019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803590602001909291905050506109ef565b604051808215151515815260200191505060405180910390f35b34801561042b57600080fd5b50610480600480360381019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050610b48565b6040518082815260200191505060405180910390f35b60008054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561052c5780601f106105015761010080835404028352916020019161052c565b820191906000526020600020905b81548152906001019060200180831161050f57829003601f168201915b505050505081565b600081600560003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055508273ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925846040518082815260200191505060405180910390a36001905092915050565b60035481565b6000600460008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054821115151561067c57600080fd5b600560008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054821115151561070757600080fd5b81600460008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254039250508190555081600460008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254019250508190555081600560008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825403925050819055508273ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef846040518082815260200191505060405180910390a3600190509392505050565b60028054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156109315780601f1061090657610100808354040283529160200191610931565b820191906000526020600020905b81548152906001019060200180831161091457829003601f168201915b505050505081565b60046020528060005260406000206000915090505481565b60018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156109e75780601f106109bc576101008083540402835291602001916109e7565b820191906000526020600020905b8154815290600101906020018083116109ca57829003601f168201915b505050505081565b600081600460003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205410151515610a3f57600080fd5b81600460003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254039250508190555081600460008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055508273ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef846040518082815260200191505060405180910390a36001905092915050565b60056020528160005260406000206020528060005260406000206000915091505054815600a165627a7a723058204c3f690997294d337edc3571d8e77afc5b0e56a2f4bfae6fb59139c8e4eb2f7e0029' 14 | 15 | const txObject = { 16 | nonce: web3.utils.toHex(txCount), 17 | gasLimit: web3.utils.toHex(1000000), // Raise the gas limit to a much higher amount 18 | gasPrice: web3.utils.toHex(web3.utils.toWei('10', 'gwei')), 19 | data: data 20 | } 21 | 22 | const tx = new Tx(txObject) 23 | tx.sign(privateKey1) 24 | 25 | const serializedTx = tx.serialize() 26 | const raw = '0x' + serializedTx.toString('hex') 27 | 28 | web3.eth.sendSignedTransaction(raw, (err, txHash) => { 29 | console.log('err:', err, 'txHash:', txHash) 30 | // Use this txHash to find the contract on Etherscan! 31 | }) 32 | }) 33 | 34 | // Read the deployed contract - get the addresss from Etherscan 35 | const abi = [{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"}],"name":"approve","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"standard","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"},{"name":"","type":"address"}],"name":"allowance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"name":"_from","type":"address"},{"indexed":true,"name":"_to","type":"address"},{"indexed":false,"name":"_value","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"_owner","type":"address"},{"indexed":true,"name":"_spender","type":"address"},{"indexed":false,"name":"_value","type":"uint256"}],"name":"Approval","type":"event"}] 36 | const address = '0xF27844E4bBBAa29F4A20E2F6a3Df83AD49DDB39C' 37 | 38 | const contract = new web3.eth.Contract(abi, address) 39 | 40 | contract.methods.name().call((err, name) => { 41 | console.log('name:', name) 42 | }) 43 | --------------------------------------------------------------------------------