├── .gitignore ├── 1_account ├── create_new_account.js ├── wallet.js └── get_account_balance.js ├── 2_blockchain ├── subscribe_block_headers.js ├── subscribe_logs_part_1.js ├── subscribe_logs_part_2.js ├── subscribe_pending_transactions.js └── block_inspection.js ├── 0_network └── create_web3_object.js ├── README.md ├── 4_transaction ├── transaction_to_send_ether_part_1.js ├── transaction_to_send_ether_part_2.js ├── transaction_to_execute_smart_contract.js └── transaction_to_deploy_contract.js └── 3_contract ├── create_contract.js ├── method_call.js └── subscribe_event.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /1_account/create_new_account.js: -------------------------------------------------------------------------------- 1 | let Web3 = require('web3') 2 | let url = 'http://127.0.0.1:7545' 3 | let web3 = new Web3(url) 4 | 5 | 6 | console.log(web3.eth.accounts.create()) 7 | -------------------------------------------------------------------------------- /2_blockchain/subscribe_block_headers.js: -------------------------------------------------------------------------------- 1 | const Web3 = require('web3') 2 | let ws = 'wss://mainnet.infura.io/ws' 3 | var web3 = new Web3(ws); 4 | 5 | web3.eth.subscribe('newBlockHeaders', (error, blockheader) => { 6 | if(!error){ 7 | console.log(blockheader) 8 | }else{ 9 | console.log(error) 10 | } 11 | }) 12 | 13 | -------------------------------------------------------------------------------- /1_account/wallet.js: -------------------------------------------------------------------------------- 1 | let Web3 = require('web3') 2 | let url = "https://mainnet.infura.io/v3/abbe691d210c470bbb9e2956e2f82c49" 3 | let web3 = new Web3(url) 4 | 5 | let wallet = web3.eth.accounts.wallet.create(2, 'some random string') 6 | 7 | let account = web3.eth.accounts.create() 8 | 9 | wallet.add(account.privateKey) 10 | 11 | console.log(wallet) -------------------------------------------------------------------------------- /1_account/get_account_balance.js: -------------------------------------------------------------------------------- 1 | let Web3 = require('web3') 2 | let url = "https://mainnet.infura.io/v3/abbe691d210c470bbb9e2956e2f82c49" 3 | let web3 = new Web3(url) 4 | 5 | let address = "0x281055Afc982d96fAB65b3a49cAc8b878184Cb16" 6 | 7 | web3.eth.getBalance(address, function(error, balance){ 8 | if(!error) 9 | { 10 | console.log(web3.utils.fromWei(balance, 'ether')) 11 | } 12 | else{ 13 | console.log(error) 14 | } 15 | }) -------------------------------------------------------------------------------- /0_network/create_web3_object.js: -------------------------------------------------------------------------------- 1 | let Web3 = require('web3') 2 | // ganache local blockchain 3 | let url_ganache = 'http://127.0.0.1:7545' 4 | 5 | // infura 6 | let url_infura = 'https://mainnet.infura.io/v3/abbe691d210c470bbb9e2956e2f82c49' 7 | 8 | // using geth 9 | let url_geth = 'http://127.0.0.1:8545' 10 | 11 | // let web3 = new Web3(url_ganache) 12 | // let web3 = new Web3(url_infura) 13 | let web3 = new Web3(url_geth) 14 | 15 | 16 | console.log(web3) -------------------------------------------------------------------------------- /2_blockchain/subscribe_logs_part_1.js: -------------------------------------------------------------------------------- 1 | const Web3 = require('web3') 2 | const ws = 'wss://mainnet.infura.io/ws' 3 | var web3 = new Web3(ws); 4 | 5 | // web3.eth.subscribe('logs', {}, (error, log) => { 6 | // if(!error){ 7 | // console.log(log) 8 | // } 9 | // }) 10 | 11 | // fileter out all log happen with cryptokitties 12 | web3.eth.subscribe('logs', { 13 | address: '0x06012c8cf97BEaD5deAe237070F9587f8E7A266d' 14 | }, (error, log) => { 15 | if(!error){ 16 | console.log(log) 17 | } 18 | }) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | web3js 2 | 3 | - installation 4 | - the big picture 5 | - network 6 | - create_web3_object 7 | - local hosted with ganache 8 | - remote hosted with infura 9 | - account 10 | - create new account 11 | - get account balance 12 | - wallet 13 | - block chain 14 | - block inspection 15 | - subscribe block headers 16 | - subscribe pending transactions 17 | - contract 18 | - create contract 19 | - call method 20 | - sub event 21 | - transaction 22 | - transaction deploy contract 23 | - transaction send ether 24 | - transaction write to smart contract 25 | -------------------------------------------------------------------------------- /2_blockchain/subscribe_logs_part_2.js: -------------------------------------------------------------------------------- 1 | const Web3 = require('web3') 2 | const ws = 'wss://mainnet.infura.io/ws' 3 | var web3 = new Web3(ws); 4 | 5 | // fileter out all log happen with cryptokitties 6 | web3.eth.subscribe('logs', { 7 | address: '0x06012c8cf97BEaD5deAe237070F9587f8E7A266d', 8 | topics:['0x241ea03ca20251805084d27d4440371c34a0b85ff108f6bb5611248f73818b80'], 9 | fromBlock: 'latest' 10 | }, (error, log) => { 11 | if(!error){ 12 | console.log(log) 13 | } 14 | }) 15 | 16 | // pregnant_event = "Pregnant(address,uint256,uint256,uint256)" 17 | // pregnant_event_hashed = web3.utils.sha3(pregnant_event) 18 | // console.log(pregnant_event_hashed) 19 | // 0x241ea03ca20251805084d27d4440371c34a0b85ff108f6bb5611248f73818b80 -------------------------------------------------------------------------------- /2_blockchain/subscribe_pending_transactions.js: -------------------------------------------------------------------------------- 1 | const Web3 = require('web3') 2 | const ws = 'wss://mainnet.infura.io/ws' 3 | const web3 = new Web3(ws); 4 | 5 | // web3.eth.subscribe('pendingTransactions', (error, txhash) => { 6 | // console.log(txhash) 7 | // }) 8 | 9 | 10 | web3.eth.subscribe('pendingTransactions', (error, txhash) => { 11 | if(!error) 12 | { 13 | web3.eth.getTransaction(txhash, (error, tx)=>{ 14 | if(tx!=null){ 15 | // console.log(tx) 16 | // filler transaction >= 1eth 17 | if(tx.value >= 1000000000000000000){ 18 | console.log(txhash) 19 | console.log(web3.utils.fromWei(tx.value,'ether'), 'ether') 20 | } 21 | } 22 | }) 23 | }else{ 24 | console.log(error) 25 | } 26 | }) 27 | -------------------------------------------------------------------------------- /2_blockchain/block_inspection.js: -------------------------------------------------------------------------------- 1 | const Web3 = require('web3') 2 | const url = 'https://mainnet.infura.io/v3/abbe691d210c470bbb9e2956e2f82c49' 3 | const web3 = new Web3(url) 4 | 5 | web3.eth.getBlockNumber().then((result) => { 6 | console.log(result) 7 | }) 8 | 9 | web3.eth.getBlock(6257684).then((block) => { 10 | console.log(block) 11 | }) 12 | 13 | web3.eth.getBlockNumber().then((result) => { 14 | for(let i=0;i<100;i++){ 15 | web3.eth.getBlock(result-i).then((block) => { 16 | console.log(block.number) 17 | }) 18 | } 19 | }) 20 | 21 | web3.eth.getBlock('latest').then((block) => { 22 | console.log(block.number) 23 | }) 24 | 25 | web3.eth.getBlock('pending').then((block) => { 26 | console.log(block.number) 27 | }) 28 | 29 | web3.eth.getBlock('0x8fe37851d97e4eb6fdac5946aa1cd6cdbdf476473ea2989bfc6d0ae004e71ab9').then((block) => { 30 | console.log(block.number) 31 | }) 32 | 33 | web3.eth.getTransactionFromBlock(6257707, 2).then(console.log) -------------------------------------------------------------------------------- /4_transaction/transaction_to_send_ether_part_1.js: -------------------------------------------------------------------------------- 1 | const Tx = require('ethereumjs-tx') 2 | 3 | const Web3 = require('web3') 4 | const url = 'https://ropsten.infura.io/v3/abbe691d210c470bbb9e2956e2f82c49' 5 | const web3 = new Web3(url) 6 | 7 | // { address: '0x5f3459cAf34e35E9A52117a442Aad55A6e2eb187', 8 | // privateKey: '0xa27b4f867b530627986b81e124e53922369521732d65d71c6c33414399d13691', 9 | // } 10 | // { address: '0x25e20af4D202CfE4EA483EE4Bd87ED42420fFdC0', 11 | // privateKey: '0xc94c956497f9a582f8b2c8efc6758dfb20b0bb517d5e934a421d8900169c2014', 12 | // } 13 | 14 | const account1 = '0x5f3459cAf34e35E9A52117a442Aad55A6e2eb187' 15 | const privateKey1 = Buffer.from("a27b4f867b530627986b81e124e53922369521732d65d71c6c33414399d13691", "hex") 16 | const account2 = '0x25e20af4D202CfE4EA483EE4Bd87ED42420fFdC0' 17 | 18 | web3.eth.getBalance(account1, (error, balance) => { 19 | console.log('account 1 balance ', balance) 20 | }) 21 | 22 | web3.eth.getBalance(account2, (error, balance) => { 23 | console.log('account 2 balance ', balance) 24 | }) -------------------------------------------------------------------------------- /4_transaction/transaction_to_send_ether_part_2.js: -------------------------------------------------------------------------------- 1 | const Tx= require('ethereumjs-tx') 2 | const Web3 = require('web3') 3 | const url = 'https://ropsten.infura.io/v3/abbe691d210c470bbb9e2956e2f82c49' 4 | const web3 = new Web3(url) 5 | 6 | const account1 = '0x5f3459cAf34e35E9A52117a442Aad55A6e2eb187' 7 | const privateKey1 = Buffer.from("a27b4f867b530627986b81e124e53922369521732d65d71c6c33414399d13691", "hex") 8 | const account2 = '0x25e20af4D202CfE4EA483EE4Bd87ED42420fFdC0' 9 | 10 | web3.eth.getTransactionCount(account1, (error, txCount) => { 11 | // build a transaction object 12 | const txObject = { 13 | nonce: web3.utils.toHex(txCount), 14 | to: account2, 15 | value: web3.utils.toHex(web3.utils.toWei('0.3', 'ether')), 16 | gasLimit: web3.utils.toHex(21000), 17 | gasPrice: web3.utils.toHex(web3.utils.toWei('10', 'gwei')) 18 | } 19 | 20 | // console.log(txObject) 21 | 22 | // sign traction with private key of sender 23 | const tx = new Tx(txObject) 24 | tx.sign(privateKey1) 25 | 26 | // serialize the transaction 27 | const serializedTransaction = tx.serialize() 28 | const raw = '0x' + serializedTransaction.toString('hex') 29 | 30 | // broadcast transaction to the network 31 | web3.eth.sendSignedTransaction(raw, (error, txHash) => { 32 | console.log(txHash) 33 | }) 34 | }) -------------------------------------------------------------------------------- /4_transaction/transaction_to_execute_smart_contract.js: -------------------------------------------------------------------------------- 1 | const Tx= require('ethereumjs-tx') 2 | const Web3 = require('web3') 3 | const web3 = new Web3('https://ropsten.infura.io/v3/abbe691d210c470bbb9e2956e2f82c49') 4 | 5 | const account1 = '0x5f3459cAf34e35E9A52117a442Aad55A6e2eb187' 6 | const privateKey1 = Buffer.from("a27b4f867b530627986b81e124e53922369521732d65d71c6c33414399d13691", "hex") 7 | 8 | 9 | const election_address = '0x48BEB4D8dd3AEe098811082Ad5831a2856fB784a' 10 | const election_abi = [ 11 | { 12 | "constant": true, 13 | "inputs": [ 14 | { 15 | "name": "", 16 | "type": "uint256" 17 | } 18 | ], 19 | "name": "candidates", 20 | "outputs": [ 21 | { 22 | "name": "id", 23 | "type": "uint256" 24 | }, 25 | { 26 | "name": "name", 27 | "type": "string" 28 | }, 29 | { 30 | "name": "voteCount", 31 | "type": "uint256" 32 | } 33 | ], 34 | "payable": false, 35 | "stateMutability": "view", 36 | "type": "function" 37 | }, 38 | { 39 | "constant": true, 40 | "inputs": [ 41 | { 42 | "name": "", 43 | "type": "address" 44 | } 45 | ], 46 | "name": "voters", 47 | "outputs": [ 48 | { 49 | "name": "", 50 | "type": "bool" 51 | } 52 | ], 53 | "payable": false, 54 | "stateMutability": "view", 55 | "type": "function" 56 | }, 57 | { 58 | "constant": true, 59 | "inputs": [], 60 | "name": "candidateCount", 61 | "outputs": [ 62 | { 63 | "name": "", 64 | "type": "uint256" 65 | } 66 | ], 67 | "payable": false, 68 | "stateMutability": "view", 69 | "type": "function" 70 | }, 71 | { 72 | "inputs": [], 73 | "payable": false, 74 | "stateMutability": "nonpayable", 75 | "type": "constructor" 76 | }, 77 | { 78 | "anonymous": false, 79 | "inputs": [ 80 | { 81 | "indexed": true, 82 | "name": "candidateId", 83 | "type": "uint256" 84 | } 85 | ], 86 | "name": "voted", 87 | "type": "event" 88 | }, 89 | { 90 | "constant": false, 91 | "inputs": [ 92 | { 93 | "name": "candidateId", 94 | "type": "uint256" 95 | } 96 | ], 97 | "name": "vote", 98 | "outputs": [], 99 | "payable": false, 100 | "stateMutability": "nonpayable", 101 | "type": "function" 102 | } 103 | ] 104 | 105 | const electionContract = new web3.eth.Contract(election_abi, election_address) 106 | // electionContract.methods.candidateCount().call((error, result) => {console.log(result)}) 107 | 108 | 109 | web3.eth.getTransactionCount(account1, (error, txCount) => { 110 | const data = electionContract.methods.vote(1).encodeABI() 111 | // build a transaction object 112 | const txObject = { 113 | nonce: web3.utils.toHex(txCount), 114 | to: election_address, 115 | gasLimit: web3.utils.toHex(1000000), 116 | gasPrice: web3.utils.toHex(web3.utils.toWei('10', 'gwei')), 117 | data: data 118 | } 119 | 120 | // console.log(txObject) 121 | 122 | // sign traction with private key of sender 123 | const tx = new Tx(txObject) 124 | tx.sign(privateKey1) 125 | 126 | // serialize the transaction 127 | const serializedTransaction = tx.serialize() 128 | const raw = '0x' + serializedTransaction.toString('hex') 129 | 130 | // broadcast transaction to the network 131 | web3.eth.sendSignedTransaction(raw, (error, txHash) => { 132 | console.log(txHash) 133 | }) 134 | }) 135 | 136 | -------------------------------------------------------------------------------- /3_contract/create_contract.js: -------------------------------------------------------------------------------- 1 | var Web3 = require('web3'); 2 | let url = "https://mainnet.infura.io/v3/abbe691d210c470bbb9e2956e2f82c49"; 3 | let web3 = new Web3(url) 4 | 5 | const omg_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 omg_address = '0xd26114cd6EE289AccF82350c8d8487fedB8A0C07' 7 | 8 | const omg_contract = new web3.eth.Contract(omg_abi, omg_address) 9 | 10 | console.log(omg_contract) -------------------------------------------------------------------------------- /3_contract/method_call.js: -------------------------------------------------------------------------------- 1 | var Web3 = require('web3'); 2 | let url = "https://mainnet.infura.io/v3/abbe691d210c470bbb9e2956e2f82c49"; 3 | let web3 = new Web3(url) 4 | 5 | const omg_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 omg_address = '0xd26114cd6EE289AccF82350c8d8487fedB8A0C07' 7 | const omg_contract = new web3.eth.Contract(omg_abi, omg_address) 8 | 9 | omg_contract.methods.name().call((error, result) => { 10 | console.log(result) 11 | }) 12 | 13 | omg_contract.methods.totalSupply().call((error, result) => { 14 | console.log('total supply ', result) 15 | }) 16 | 17 | omg_contract.methods.balanceOf('0x2551d2357C8DA54b7d330917E0E769d33F1F5B93').call((error, result)=>{ 18 | console.log('balance of 0x2551d2357C8DA54b7d330917E0E769d33F1F5B93 ', result) 19 | }) -------------------------------------------------------------------------------- /4_transaction/transaction_to_deploy_contract.js: -------------------------------------------------------------------------------- 1 | const Tx= require('ethereumjs-tx') 2 | const Web3 = require('web3') 3 | const web3 = new Web3('https://ropsten.infura.io/v3/abbe691d210c470bbb9e2956e2f82c49') 4 | 5 | const account1 = '0x5f3459cAf34e35E9A52117a442Aad55A6e2eb187' 6 | const privateKey1 = Buffer.from("a27b4f867b530627986b81e124e53922369521732d65d71c6c33414399d13691", "hex") 7 | 8 | // web3.eth.getTransactionCount(account1, (error, txCount) => { 9 | // const data = "0x608060405234801561001057600080fd5b5061005e6040805190810160405280600b81526020017f63616e64696461746520310000000000000000000000000000000000000000008152506100b0640100000000026401000000009004565b6100ab6040805190810160405280600b81526020017f63616e64696461746520320000000000000000000000000000000000000000008152506100b0640100000000026401000000009004565b6101cf565b6001806000828254019250508190555060606040519081016040528060015481526020018281526020016000815250600080600154815260200190815260200160002060008201518160000155602082015181600101908051906020019061011992919061012a565b506040820151816002015590505050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061016b57805160ff1916838001178555610199565b82800160010185558215610199579182015b8281111561019857825182559160200191906001019061017d565b5b5090506101a691906101aa565b5090565b6101cc91905b808211156101c85760008160009055506001016101b0565b5090565b90565b6104f6806101de6000396000f300608060405260043610610062576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680630121b93f146100675780633477ee2e14610094578063a3ec138d14610148578063a9a981a3146101a3575b600080fd5b34801561007357600080fd5b50610092600480360381019080803590602001909291905050506101ce565b005b3480156100a057600080fd5b506100bf600480360381019080803590602001909291905050506103e2565b6040518084815260200180602001838152602001828103825284818151815260200191508051906020019080838360005b8381101561010b5780820151818401526020810190506100f0565b50505050905090810190601f1680156101385780820380516001836020036101000a031916815260200191505b5094505050505060405180910390f35b34801561015457600080fd5b50610189600480360381019080803573ffffffffffffffffffffffffffffffffffffffff1690602001909291905050506104a4565b604051808215151515815260200191505060405180910390f35b3480156101af57600080fd5b506101b86104c4565b6040518082815260200191505060405180910390f35b600154811115151561026e576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260378152602001807f63616e6469646174652069642073686f756c6420736d616c6c6572206f72206581526020017f7175616c20746f2063616e64696461746520636f756e7400000000000000000081525060400191505060405180910390fd5b60001515600260003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060009054906101000a900460ff161515141515610336576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252601a8152602001807f766f746572206e65656420746f206e6f742079657420766f746500000000000081525060200191505060405180910390fd5b6001600260003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060006101000a81548160ff021916908315150217905550600160008083815260200190815260200160002060020160008282540192505081905550807f8fbb38ff86a5b319c33b22609be0afce04335d5db18c6bd100767e251d4028cc60405160405180910390a250565b6000602052806000526040600020600091509050806000015490806001018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156104945780601f1061046957610100808354040283529160200191610494565b820191906000526020600020905b81548152906001019060200180831161047757829003601f168201915b5050505050908060020154905083565b60026020528060005260406000206000915054906101000a900460ff1681565b600154815600a165627a7a723058203a3fcaafc5a3a39591728d563b2c87ee132dde1291d09adfbf80f74c91392eb40029" 10 | // // build a transaction object 11 | // const txObject = { 12 | // nonce: web3.utils.toHex(txCount), 13 | // // to: account2, 14 | // // value: web3.utils.toHex(web3.utils.toWei('0.3', 'ether')), 15 | // gasLimit: web3.utils.toHex(1000000), 16 | // gasPrice: web3.utils.toHex(web3.utils.toWei('10', 'gwei')), 17 | // data: data 18 | // } 19 | 20 | // // console.log(txObject) 21 | 22 | // // sign traction with private key of sender 23 | // const tx = new Tx(txObject) 24 | // tx.sign(privateKey1) 25 | 26 | // // serialize the transaction 27 | // const serializedTransaction = tx.serialize() 28 | // const raw = '0x' + serializedTransaction.toString('hex') 29 | 30 | // // broadcast transaction to the network 31 | // web3.eth.sendSignedTransaction(raw, (error, txHash) => { 32 | // console.log(txHash) 33 | // }) 34 | // }) 35 | 36 | const election_address = '0x48BEB4D8dd3AEe098811082Ad5831a2856fB784a' 37 | const election_abi = [ 38 | { 39 | "constant": true, 40 | "inputs": [ 41 | { 42 | "name": "", 43 | "type": "uint256" 44 | } 45 | ], 46 | "name": "candidates", 47 | "outputs": [ 48 | { 49 | "name": "id", 50 | "type": "uint256" 51 | }, 52 | { 53 | "name": "name", 54 | "type": "string" 55 | }, 56 | { 57 | "name": "voteCount", 58 | "type": "uint256" 59 | } 60 | ], 61 | "payable": false, 62 | "stateMutability": "view", 63 | "type": "function" 64 | }, 65 | { 66 | "constant": true, 67 | "inputs": [ 68 | { 69 | "name": "", 70 | "type": "address" 71 | } 72 | ], 73 | "name": "voters", 74 | "outputs": [ 75 | { 76 | "name": "", 77 | "type": "bool" 78 | } 79 | ], 80 | "payable": false, 81 | "stateMutability": "view", 82 | "type": "function" 83 | }, 84 | { 85 | "constant": true, 86 | "inputs": [], 87 | "name": "candidateCount", 88 | "outputs": [ 89 | { 90 | "name": "", 91 | "type": "uint256" 92 | } 93 | ], 94 | "payable": false, 95 | "stateMutability": "view", 96 | "type": "function" 97 | }, 98 | { 99 | "inputs": [], 100 | "payable": false, 101 | "stateMutability": "nonpayable", 102 | "type": "constructor" 103 | }, 104 | { 105 | "anonymous": false, 106 | "inputs": [ 107 | { 108 | "indexed": true, 109 | "name": "candidateId", 110 | "type": "uint256" 111 | } 112 | ], 113 | "name": "voted", 114 | "type": "event" 115 | }, 116 | { 117 | "constant": false, 118 | "inputs": [ 119 | { 120 | "name": "candidateId", 121 | "type": "uint256" 122 | } 123 | ], 124 | "name": "vote", 125 | "outputs": [], 126 | "payable": false, 127 | "stateMutability": "nonpayable", 128 | "type": "function" 129 | } 130 | ] 131 | 132 | const electionContract = new web3.eth.Contract(election_abi, election_address) 133 | electionContract.methods.candidateCount().call((error, result) => {console.log(result)}) -------------------------------------------------------------------------------- /3_contract/subscribe_event.js: -------------------------------------------------------------------------------- 1 | const Web3 = require('web3') 2 | const ws = 'wss://mainnet.infura.io/ws' 3 | const web3 = new Web3(ws); 4 | 5 | const kitties_abi = [{"constant":true,"inputs":[{"name":"_interfaceID","type":"bytes4"}],"name":"supportsInterface","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"cfoAddress","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_tokenId","type":"uint256"},{"name":"_preferredTransport","type":"string"}],"name":"tokenMetadata","outputs":[{"name":"infoUrl","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"promoCreatedCount","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_tokenId","type":"uint256"}],"name":"approve","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"ceoAddress","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"GEN0_STARTING_PRICE","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_address","type":"address"}],"name":"setSiringAuctionAddress","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"pregnantKitties","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_kittyId","type":"uint256"}],"name":"isPregnant","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"GEN0_AUCTION_DURATION","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"siringAuction","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_tokenId","type":"uint256"}],"name":"transferFrom","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_address","type":"address"}],"name":"setGeneScienceAddress","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_newCEO","type":"address"}],"name":"setCEO","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_newCOO","type":"address"}],"name":"setCOO","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_kittyId","type":"uint256"},{"name":"_startingPrice","type":"uint256"},{"name":"_endingPrice","type":"uint256"},{"name":"_duration","type":"uint256"}],"name":"createSaleAuction","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[],"name":"unpause","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"sireAllowedToAddress","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_matronId","type":"uint256"},{"name":"_sireId","type":"uint256"}],"name":"canBreedWith","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"kittyIndexToApproved","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_kittyId","type":"uint256"},{"name":"_startingPrice","type":"uint256"},{"name":"_endingPrice","type":"uint256"},{"name":"_duration","type":"uint256"}],"name":"createSiringAuction","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"val","type":"uint256"}],"name":"setAutoBirthFee","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_addr","type":"address"},{"name":"_sireId","type":"uint256"}],"name":"approveSiring","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_newCFO","type":"address"}],"name":"setCFO","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_genes","type":"uint256"},{"name":"_owner","type":"address"}],"name":"createPromoKitty","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"secs","type":"uint256"}],"name":"setSecondsPerBlock","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"paused","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"withdrawBalance","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"_tokenId","type":"uint256"}],"name":"ownerOf","outputs":[{"name":"owner","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"GEN0_CREATION_LIMIT","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"newContractAddress","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_address","type":"address"}],"name":"setSaleAuctionAddress","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"count","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_v2Address","type":"address"}],"name":"setNewAddress","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"secondsPerBlock","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"pause","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"tokensOfOwner","outputs":[{"name":"ownerTokens","type":"uint256[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_matronId","type":"uint256"}],"name":"giveBirth","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[],"name":"withdrawAuctionBalances","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"cooldowns","outputs":[{"name":"","type":"uint32"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"kittyIndexToOwner","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_tokenId","type":"uint256"}],"name":"transfer","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"cooAddress","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"autoBirthFee","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"erc721Metadata","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_genes","type":"uint256"}],"name":"createGen0Auction","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"_kittyId","type":"uint256"}],"name":"isReadyToBreed","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"PROMO_CREATION_LIMIT","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_contractAddress","type":"address"}],"name":"setMetadataAddress","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"saleAuction","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_id","type":"uint256"}],"name":"getKitty","outputs":[{"name":"isGestating","type":"bool"},{"name":"isReady","type":"bool"},{"name":"cooldownIndex","type":"uint256"},{"name":"nextActionAt","type":"uint256"},{"name":"siringWithId","type":"uint256"},{"name":"birthTime","type":"uint256"},{"name":"matronId","type":"uint256"},{"name":"sireId","type":"uint256"},{"name":"generation","type":"uint256"},{"name":"genes","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_sireId","type":"uint256"},{"name":"_matronId","type":"uint256"}],"name":"bidOnSiringAuction","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"constant":true,"inputs":[],"name":"gen0CreatedCount","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"geneScience","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_matronId","type":"uint256"},{"name":"_sireId","type":"uint256"}],"name":"breedWithAuto","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"payable":true,"stateMutability":"payable","type":"fallback"},{"anonymous":false,"inputs":[{"indexed":false,"name":"owner","type":"address"},{"indexed":false,"name":"matronId","type":"uint256"},{"indexed":false,"name":"sireId","type":"uint256"},{"indexed":false,"name":"cooldownEndBlock","type":"uint256"}],"name":"Pregnant","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"from","type":"address"},{"indexed":false,"name":"to","type":"address"},{"indexed":false,"name":"tokenId","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"owner","type":"address"},{"indexed":false,"name":"approved","type":"address"},{"indexed":false,"name":"tokenId","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"owner","type":"address"},{"indexed":false,"name":"kittyId","type":"uint256"},{"indexed":false,"name":"matronId","type":"uint256"},{"indexed":false,"name":"sireId","type":"uint256"},{"indexed":false,"name":"genes","type":"uint256"}],"name":"Birth","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"newContract","type":"address"}],"name":"ContractUpgrade","type":"event"}] 6 | const kitties_address = '0x06012c8cf97BEaD5deAe237070F9587f8E7A266d' 7 | const kitties_contract = new web3.eth.Contract(kitties_abi, kitties_address) 8 | 9 | // kitties_contract.events.allEvents({fromBlock:'latest'}, (error, result) => { 10 | // console.log(result) 11 | // }) 12 | 13 | kitties_contract.events.Transfer({fromBlock:'latest'}, (error, result) => { 14 | console.log(result) 15 | }) --------------------------------------------------------------------------------