From 9edd8371852d73c5b2c8fe451348dd5279bc4004 Mon Sep 17 00:00:00 2001 From: thinkocapo Date: Mon, 26 Feb 2018 11:58:21 -0500 Subject: [PATCH 01/27] many updates. experimentation --- eth-node-providers.js | 25 ++++++++- index.js | 123 +++++------------------------------------- indexOLD.js | 123 ++++++++++++++++++++++++++++++++++++++++++ web3-scripts.js | 64 ++++++++++++++++++++++ 4 files changed, 223 insertions(+), 112 deletions(-) create mode 100644 indexOLD.js create mode 100644 web3-scripts.js diff --git a/eth-node-providers.js b/eth-node-providers.js index 7e79526..1089f6a 100644 --- a/eth-node-providers.js +++ b/eth-node-providers.js @@ -1,4 +1,25 @@ module.exports = { gethLocal: 'http://localhost:8545', - myEtherWallet: 'https://api.myetherapi.com/eth' -} \ No newline at end of file + myEtherWallet: 'https://api.myetherapi.com/eth', + // 8545 is standard port that geth runs on, see gethLocal ^^ + // chicagoGeth: 'http:65.79.136.27:8545' // Unhandled rejection Error: Invalid JSON RPC response: "" + chicagoGeth: 'http:65.79.136.27:30303' // Unhandled rejection Error: Invalid JSON RPC response: "" + // chicagoGeth: 'http://65.79.136.27:8545', // no error and no response either // correct URL format https://stackoverflow.com/questions/6940120/how-to-pass-ip-address-and-portnumber-as-url + //chicagoGeth: 'http://65.79.136.27:30303' // Unhandled rejection Error: Invalid JSON RPC response: "" + // chicagoGeth: '65.79.136.27:1988' // ssh port + + + // 30303 +} + +// correct format + +// https://github.com/ethereum/go-ethereum/issues/2982 +// https://github.com/ethereum/wiki/wiki/JSON-RPC#go + +// https://ethereum.stackexchange.com/questions/3163/how-can-i-expose-geths-rpc-server-to-external-connections + + +// ** +// geth --rpc --rpccorsdomain "http://localhost:3000" +// geth --rpc --rpccorsdomain "http://190.157.85.51:?" // My IP Address \ No newline at end of file diff --git a/index.js b/index.js index 24df155..a442c51 100644 --- a/index.js +++ b/index.js @@ -1,120 +1,23 @@ const ethNodeProviders = require('./eth-node-providers') const gethLocal = ethNodeProviders.gethLocal const myEtherWallet = ethNodeProviders.myEtherWallet -console.log('ethNodeProviders\n',ethNodeProviders) -return -// RUN A GETH NODE USING -// geth --syncmode "fast" --cache 2048 --rpc +const chicagoGeth = ethNodeProviders.chicagoGeth + const Web3 = require('web3') let web3 if (typeof web3 !== 'undefined') { web3 = new Web3(web3.currentProvider); } else { - web3 = new Web3(new Web3.providers.HttpProvider(gethLocal)); - web3.setProvider(new web3.providers.HttpProvider(myEtherWallet)); + // web3 = new Web3(new Web3.providers.HttpProvider(gethLocal)); + console.log('chi town', chicagoGeth) + web3 = new Web3(new Web3.providers.HttpProvider(chicagoGeth)) + // web3.setProvider(new web3.providers.HttpProvider(myEtherWallet)); // https://www.myetherapi.com/ } -// console.log('\n======== web3.eth =======\n', web3.eth.Contract) // web3.accounts [...] - -//*TODO* - need add an account to GETH. see if can access it as web3.eth.accounts[0] 11:59a - -// UNLOCK ETHER ACCOUNT ADDRESS -// Next, we have to specify a default ethereum account to use through the web3.eth.defaultAccount method: -// Remember when we ran the testrpc console command? It provided us with 10 accounts. We're simply choosing the first account here to use. -// Can use js/web3 to unlock a wallet? -// Or must do that from geth commandline first? -// Try sending to my own address, from commandLine, first. or doesnt matter, because should ahve txId, will see it or WONT see it under EOS Contract Address Activity -// unlock account, may need to use Truffle - -web3.eth.getAccounts(function (error, res) { - const accounts = res - const myAccount = res[0].toString() - console.log('===== Account is ======', myAccount) // undefined *TODO* - - // *TODO* use somewhere...claimAll... - //web3.eth.defaultAccount = web3.eth.accounts[0]; // test accounts, ganache. or real. - - // *TODO* need Unlock? - // tried > personal.unlockAccount("0x7602acd0a747332ce638a0b9f6d7532767303c8f") - - // var balance = web3.eth.getBalance(myAccount); // returns Promise... - // console.log('\n====== balance ====\n', balance) - - // > eth.syncing to know if geth is done syncing https://ethereum.stackexchange.com/questions/16333/how-can-i-tell-if-geth-is-done-running - // should say 'imported chain segment' when complete https://www.reddit.com/r/EtherMining/comments/6dxaci/how_do_you_tell_when_geth_has_finished/ - // sync as a service... bash. https://medium.com/@jacksonngtech/syncing-geth-to-the-ethereum-blockchain-9571666f3cfc - // not possible? https://github.com/ethereum/go-ethereum/issues/14338 - // asks "WHen is it ever done syncing?" https://bitcointalk.org/index.php?topic=1861097.0 and https://github.com/ethereum/go-ethereum/issues/2936 - // > eth.getBlock("latest") - - web3.eth.getBalance(myAccount, function (err, res) { // highestBlock vs currentBlock https://stackoverflow.com/questions/47161038/web3-js-getbalance-always-showing-0 - if (err) console.log('err', err) - console.log('====== getBalance result ====', res) // 0 - }) - - // says im not synced up yet https://ethereum.stackexchange.com/questions/511/why-is-geth-always-returning-a-0-balance - - // function checkAllBalances() { - // const eth = web3.eth - // var totalBal = 0; - // for (var acctNum in accounts) { - // var acct = accounts[acctNum]; - // var acctBal = web3.fromWei(eth.getBalance(acct), "ether"); // cant read fromWei, error - // totalBal += parseFloat(acctBal); - // console.log(" accounts[" + acctNum + "]: \t" + acct + " \tbalance: " + acctBal + " ether"); - // } - // console.log(" Total balance: " + totalBal + " ether"); - // }; - // checkAllBalances() - - // return web3.eth.getBalance(res[0], 5092367) -}) - // .then((result) => { - // console.log('====== balance =======', result); // logs the account address again... - // }) - - -// SEND ETHER TO EOS CROWDSALE CONTRACT ADDRESS -// w/ gas limit <= 90000. from web3.eth.account wallet? -const eosContractAddress = '0xd0a6E6C54DbC68Db5db3A091B171A77407Ff7ccf' -const gasLimit = 90000 -// Create an account....web3 -// https://github.com/ethereum/wiki/wiki/JavaScript-API ? -// or -// Create an account...Geth -// https://github.com/ethereum/go-ethereum/wiki/Managing-your-accounts - - -// GET THE EOS CONTRACT INSTANCE SO WE CAN INVOKE THE .CLAIMALL SMARTCONTRACT METHOD TO GET OUR TOKENS -// It accepts one parameter, which is referred to as the ABI (Application Binary Interface). -// This ABI allows you to call functions and receive data from your smart contract. -// Use the web3.eth.contract() method to initiatlize (or create) the contract on an address. -// Rather than paste ABI here, could have compiled the entire .sol file? using npm solc command... ? // https://medium.com/@mvmurthy/full-stack-hello-world-voting-ethereum-dapp-tutorial-part-1-40d2d0d807c2 -const eosContractABI = [{"constant":true,"inputs":[{"name":"","type":"uint256"},{"name":"","type":"address"}],"name":"claimed","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"owner_","type":"address"}],"name":"setOwner","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"time","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint128"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"foundersAllocation","outputs":[{"name":"","type":"uint128"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"day","type":"uint256"}],"name":"claim","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"foundersKey","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"},{"name":"","type":"address"}],"name":"userBuys","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"day","type":"uint256"}],"name":"createOnDay","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"freeze","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"keys","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"startTime","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"authority_","type":"address"}],"name":"setAuthority","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"dailyTotals","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"buy","outputs":[],"payable":true,"type":"function"},{"constant":true,"inputs":[],"name":"openTime","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"EOS","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"today","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"authority","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"eos","type":"address"}],"name":"initialize","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"createFirstDay","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"claimAll","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"timestamp","type":"uint256"}],"name":"dayFor","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"day","type":"uint256"},{"name":"limit","type":"uint256"}],"name":"buyWithLimit","outputs":[],"payable":true,"type":"function"},{"constant":false,"inputs":[],"name":"collect","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"numberOfDays","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"key","type":"string"}],"name":"register","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"createPerDay","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"inputs":[{"name":"_numberOfDays","type":"uint256"},{"name":"_totalSupply","type":"uint128"},{"name":"_openTime","type":"uint256"},{"name":"_startTime","type":"uint256"},{"name":"_foundersAllocation","type":"uint128"},{"name":"_foundersKey","type":"string"}],"payable":false,"type":"constructor"},{"payable":true,"type":"fallback"},{"anonymous":false,"inputs":[{"indexed":false,"name":"window","type":"uint256"},{"indexed":false,"name":"user","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"LogBuy","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"window","type":"uint256"},{"indexed":false,"name":"user","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"LogClaim","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"user","type":"address"},{"indexed":false,"name":"key","type":"string"}],"name":"LogRegister","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"amount","type":"uint256"}],"name":"LogCollect","type":"event"},{"anonymous":false,"inputs":[],"name":"LogFreeze","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"authority","type":"address"}],"name":"LogSetAuthority","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"}],"name":"LogSetOwner","type":"event"}] // should NOT be a string or will throw error in web3js/contracts.js -const eosContractInstance = new web3.eth.Contract(eosContractABI, eosContractAddress); -// var contract = new web3.eth.Contract(abi, contractAddress) -// const eosContractInstance = eosContract.at(eosContractAddress); -console.log('\n === eosContractInstance.methods.claimAll === \n', eosContractInstance.methods.claimAll); // 'function () { [native code] }' -return - -// GET TOKENS -// Amount to Send: 0, GasLimit: 90000 -// Invoke 'claimAll' off of web3.getContract(eos).claimAll [pseudocode] or see EOS obj I made 5:16p: -// Does this put tokens at the address? Can see them there afterwards? -eosContractInstance.methods.claimAll(data, {from: web3.eth.accounts[0]}, function (result) { // function(err, result) {} - console.log('This is a callback?', result) - console.log('Log the amount of EOS obtaine, or email the # to somewhere') -}) - -// SEND EOS FROM DEFAULT ACCOUNT WALLET TO EXCHANGE WALLET - -// ... - -// TRADE EOS FOR ETHER USING EXCHANGE API - -// ... - -// SEND ETHER BACK TO ORIGINAL web3.eth.defaultAccount address - -// REPEAT - +const web3Scripts = require('./web3-scripts') +// console.log('\filterWatches', web3Scripts.filterWatches) +// console.log('process.argv[1]', process.argv[1]) +// await web3Scripts[process.argv[1]](web3) +const sendRawTxTest = web3Scripts.sendRawTxTest +const filterWatches = web3Scripts.filterWatches // getBlock(5000000)... +filterWatches(web3) \ No newline at end of file diff --git a/indexOLD.js b/indexOLD.js new file mode 100644 index 0000000..1623361 --- /dev/null +++ b/indexOLD.js @@ -0,0 +1,123 @@ +const ethNodeProviders = require('./eth-node-providers') +const gethLocal = ethNodeProviders.gethLocal +const myEtherWallet = ethNodeProviders.myEtherWallet + +// RUN A GETH NODE USING +// geth --syncmode "fast" --cache 2048 --rpc +const Web3 = require('web3') +let web3 +if (typeof web3 !== 'undefined') { + web3 = new Web3(web3.currentProvider); +} else { + // web3 = new Web3(new Web3.providers.HttpProvider(gethLocal)); + web3 = new Web3(new Web3.providers.HttpProvider(myEtherWallet)) + // web3.setProvider(new web3.providers.HttpProvider(myEtherWallet)); // https://www.myetherapi.com/ +} + +// console.log('\n======== web3.eth =======\n', web3.eth.Contract) // web3.accounts [...] + +//*TODO* - need add an account to GETH. see if can access it as web3.eth.accounts[0] 11:59a + +// UNLOCK ETHER ACCOUNT ADDRESS +// Next, we have to specify a default ethereum account to use through the web3.eth.defaultAccount method: +// Remember when we ran the testrpc console command? It provided us with 10 accounts. We're simply choosing the first account here to use. +// Can use js/web3 to unlock a wallet? +// Or must do that from geth commandline first? +// Try sending to my own address, from commandLine, first. or doesnt matter, because should ahve txId, will see it or WONT see it under EOS Contract Address Activity +// unlock account, may need to use Truffle + +web3.eth.getAccounts(function (error, res) { + console.log('web3.eth.getAccounts response \n', res) + const accounts = res + // const myAccount = res[0].toString() + // console.log('===== Account is ======', myAccount) // undefined *TODO* + + + + // *TODO* use somewhere...claimAll... + //web3.eth.defaultAccount = web3.eth.accounts[0]; // test accounts, ganache. or real. + + // *TODO* need Unlock? + // tried > personal.unlockAccount("0x7602acd0a747332ce638a0b9f6d7532767303c8f") + + // var balance = web3.eth.getBalance(myAccount); // returns Promise... + // console.log('\n====== balance ====\n', balance) + + // > eth.syncing to know if geth is done syncing https://ethereum.stackexchange.com/questions/16333/how-can-i-tell-if-geth-is-done-running + // should say 'imported chain segment' when complete https://www.reddit.com/r/EtherMining/comments/6dxaci/how_do_you_tell_when_geth_has_finished/ + // sync as a service... bash. https://medium.com/@jacksonngtech/syncing-geth-to-the-ethereum-blockchain-9571666f3cfc + // not possible? https://github.com/ethereum/go-ethereum/issues/14338 + // asks "WHen is it ever done syncing?" https://bitcointalk.org/index.php?topic=1861097.0 and https://github.com/ethereum/go-ethereum/issues/2936 + // > eth.getBlock("latest") + + web3.eth.getBalance(myAccount, function (err, res) { // highestBlock vs currentBlock https://stackoverflow.com/questions/47161038/web3-js-getbalance-always-showing-0 + if (err) console.log('err', err) + console.log('====== getBalance result ====', res) // 0 + }) + + // says im not synced up yet https://ethereum.stackexchange.com/questions/511/why-is-geth-always-returning-a-0-balance + + // function checkAllBalances() { + // const eth = web3.eth + // var totalBal = 0; + // for (var acctNum in accounts) { + // var acct = accounts[acctNum]; + // var acctBal = web3.fromWei(eth.getBalance(acct), "ether"); // cant read fromWei, error + // totalBal += parseFloat(acctBal); + // console.log(" accounts[" + acctNum + "]: \t" + acct + " \tbalance: " + acctBal + " ether"); + // } + // console.log(" Total balance: " + totalBal + " ether"); + // }; + // checkAllBalances() + + // return web3.eth.getBalance(res[0], 5092367) +}) + // .then((result) => { + // console.log('====== balance =======', result); // logs the account address again... + // }) + + +// SEND ETHER TO EOS CROWDSALE CONTRACT ADDRESS +// w/ gas limit <= 90000. from web3.eth.account wallet? +const eosContractAddress = '0xd0a6E6C54DbC68Db5db3A091B171A77407Ff7ccf' +const gasLimit = 90000 +// Create an account....web3 +// https://github.com/ethereum/wiki/wiki/JavaScript-API ? +// or +// Create an account...Geth +// https://github.com/ethereum/go-ethereum/wiki/Managing-your-accounts + + +// GET THE EOS CONTRACT INSTANCE SO WE CAN INVOKE THE .CLAIMALL SMARTCONTRACT METHOD TO GET OUR TOKENS +// It accepts one parameter, which is referred to as the ABI (Application Binary Interface). +// This ABI allows you to call functions and receive data from your smart contract. +// Use the web3.eth.contract() method to initiatlize (or create) the contract on an address. +// Rather than paste ABI here, could have compiled the entire .sol file? using npm solc command... ? // https://medium.com/@mvmurthy/full-stack-hello-world-voting-ethereum-dapp-tutorial-part-1-40d2d0d807c2 +const eosContractABI = [{"constant":true,"inputs":[{"name":"","type":"uint256"},{"name":"","type":"address"}],"name":"claimed","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"owner_","type":"address"}],"name":"setOwner","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"time","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint128"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"foundersAllocation","outputs":[{"name":"","type":"uint128"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"day","type":"uint256"}],"name":"claim","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"foundersKey","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"},{"name":"","type":"address"}],"name":"userBuys","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"day","type":"uint256"}],"name":"createOnDay","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"freeze","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"keys","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"startTime","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"authority_","type":"address"}],"name":"setAuthority","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"dailyTotals","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"buy","outputs":[],"payable":true,"type":"function"},{"constant":true,"inputs":[],"name":"openTime","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"EOS","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"today","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"authority","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"eos","type":"address"}],"name":"initialize","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"createFirstDay","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"claimAll","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"timestamp","type":"uint256"}],"name":"dayFor","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"day","type":"uint256"},{"name":"limit","type":"uint256"}],"name":"buyWithLimit","outputs":[],"payable":true,"type":"function"},{"constant":false,"inputs":[],"name":"collect","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"numberOfDays","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"key","type":"string"}],"name":"register","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"createPerDay","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"inputs":[{"name":"_numberOfDays","type":"uint256"},{"name":"_totalSupply","type":"uint128"},{"name":"_openTime","type":"uint256"},{"name":"_startTime","type":"uint256"},{"name":"_foundersAllocation","type":"uint128"},{"name":"_foundersKey","type":"string"}],"payable":false,"type":"constructor"},{"payable":true,"type":"fallback"},{"anonymous":false,"inputs":[{"indexed":false,"name":"window","type":"uint256"},{"indexed":false,"name":"user","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"LogBuy","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"window","type":"uint256"},{"indexed":false,"name":"user","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"LogClaim","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"user","type":"address"},{"indexed":false,"name":"key","type":"string"}],"name":"LogRegister","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"amount","type":"uint256"}],"name":"LogCollect","type":"event"},{"anonymous":false,"inputs":[],"name":"LogFreeze","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"authority","type":"address"}],"name":"LogSetAuthority","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"}],"name":"LogSetOwner","type":"event"}] // should NOT be a string or will throw error in web3js/contracts.js +const eosContractInstance = new web3.eth.Contract(eosContractABI, eosContractAddress); +// var contract = new web3.eth.Contract(abi, contractAddress) +// const eosContractInstance = eosContract.at(eosContractAddress); +console.log('\n === eosContractInstance.methods.claimAll === \n', eosContractInstance.methods.claimAll); // 'function () { [native code] }' +return + +// GET TOKENS +// Amount to Send: 0, GasLimit: 90000 +// Invoke 'claimAll' off of web3.getContract(eos).claimAll [pseudocode] or see EOS obj I made 5:16p: +// Does this put tokens at the address? Can see them there afterwards? +eosContractInstance.methods.claimAll(data, {from: web3.eth.accounts[0]}, function (result) { // function(err, result) {} + console.log('This is a callback?', result) + console.log('Log the amount of EOS obtaine, or email the # to somewhere') +}) + +// SEND EOS FROM DEFAULT ACCOUNT WALLET TO EXCHANGE WALLET + +// ... + +// TRADE EOS FOR ETHER USING EXCHANGE API + +// ... + +// SEND ETHER BACK TO ORIGINAL web3.eth.defaultAccount address + +// REPEAT + diff --git a/web3-scripts.js b/web3-scripts.js new file mode 100644 index 0000000..957f067 --- /dev/null +++ b/web3-scripts.js @@ -0,0 +1,64 @@ +module.exports = { + sendRawTxTest: async function (web3) { + + + /** + * let transaction = { 'to': '0xF0109fC8DF283027b6285cc889F5aA624EaC1F55', + 'value': 1000000000, + 'gas': 2000000, + 'gasPrice': 234567897654321, + 'nonce': 0, + 'chainId': 1 } + + let signed = w3.eth.account.signTransaction(transaction, key) + w3.eth.sendRawTransaction(signed.rawTransaction) + */ + }, + filterWatches: async function (web3) { + + // erroing i think...try promise-chain. + //web3.eth.getBalance("0x7cB57B5A97eAbe94205C07890BE4c1aD31E486A8").toString(); + + + // https://github.com/trufflesuite/truffle/issues/492 // Unhandled rejection Error: Invalid JSON RPC response: "" + + // 1 confirm its on 8545 + // 2 confirm this url:port is good 'http://65.79.136.27:8545' + // 3 pass --rpc flag + // 4 see if was any activity from 2:45a - 3:15a EST 02/26/18 + + web3.eth.getBlock(5000000) + .then((err, res) => { // err + console.log('\n ======= err =====\n', err); + console.log('\n ======= res =====\n', res); + }) + + // ERROR also won't run... Error: Invalid JSON RPC response: " Date: Mon, 26 Feb 2018 12:20:26 -0500 Subject: [PATCH 02/27] refactor scripting process. clean --- eth-node-providers.js => eth-nodes.js | 2 +- index.js | 28 +++++++------- tests/test.js | 5 +++ web3-scripts.js | 53 ++++++++++++--------------- 4 files changed, 43 insertions(+), 45 deletions(-) rename eth-node-providers.js => eth-nodes.js (94%) create mode 100644 tests/test.js diff --git a/eth-node-providers.js b/eth-nodes.js similarity index 94% rename from eth-node-providers.js rename to eth-nodes.js index 1089f6a..dd7a2f2 100644 --- a/eth-node-providers.js +++ b/eth-nodes.js @@ -3,7 +3,7 @@ module.exports = { myEtherWallet: 'https://api.myetherapi.com/eth', // 8545 is standard port that geth runs on, see gethLocal ^^ // chicagoGeth: 'http:65.79.136.27:8545' // Unhandled rejection Error: Invalid JSON RPC response: "" - chicagoGeth: 'http:65.79.136.27:30303' // Unhandled rejection Error: Invalid JSON RPC response: "" + lincolnPark: 'http:65.79.136.27:30303' // Unhandled rejection Error: Invalid JSON RPC response: "" // chicagoGeth: 'http://65.79.136.27:8545', // no error and no response either // correct URL format https://stackoverflow.com/questions/6940120/how-to-pass-ip-address-and-portnumber-as-url //chicagoGeth: 'http://65.79.136.27:30303' // Unhandled rejection Error: Invalid JSON RPC response: "" // chicagoGeth: '65.79.136.27:1988' // ssh port diff --git a/index.js b/index.js index a442c51..f9584ec 100644 --- a/index.js +++ b/index.js @@ -1,23 +1,21 @@ -const ethNodeProviders = require('./eth-node-providers') -const gethLocal = ethNodeProviders.gethLocal -const myEtherWallet = ethNodeProviders.myEtherWallet -const chicagoGeth = ethNodeProviders.chicagoGeth +const ethNodes = require('./eth-nodes') +const web3Scripts = require('./web3-scripts') +// WEB3 CONFIG const Web3 = require('web3') let web3 if (typeof web3 !== 'undefined') { web3 = new Web3(web3.currentProvider); } else { - // web3 = new Web3(new Web3.providers.HttpProvider(gethLocal)); - console.log('chi town', chicagoGeth) - web3 = new Web3(new Web3.providers.HttpProvider(chicagoGeth)) - // web3.setProvider(new web3.providers.HttpProvider(myEtherWallet)); // https://www.myetherapi.com/ + console.log('chi town', lincolnPark) + web3 = new Web3(new Web3.providers.HttpProvider(ethNodes.lincolnPark)) } -const web3Scripts = require('./web3-scripts') -// console.log('\filterWatches', web3Scripts.filterWatches) -// console.log('process.argv[1]', process.argv[1]) -// await web3Scripts[process.argv[1]](web3) -const sendRawTxTest = web3Scripts.sendRawTxTest -const filterWatches = web3Scripts.filterWatches // getBlock(5000000)... -filterWatches(web3) \ No newline at end of file +// Run script that was passed as commandLine parameter +const scriptName = process.argv[2] +web3Scripts[scriptName](web3) + + +// NOTES and alternative code +// web3 = new Web3(new Web3.providers.HttpProvider(gethLocal)); +// web3.setProvider(new web3.providers.HttpProvider(myEtherWallet)); // https://www.myetherapi.com/ \ No newline at end of file diff --git a/tests/test.js b/tests/test.js new file mode 100644 index 0000000..35c8ef6 --- /dev/null +++ b/tests/test.js @@ -0,0 +1,5 @@ +const ethNodes = require('./eth-nodes') +const web3Scripts = require('./web3-scripts') + +// *TODO* +// Run a test that tests each of the scripts in web3Scripts \ No newline at end of file diff --git a/web3-scripts.js b/web3-scripts.js index 957f067..b08e027 100644 --- a/web3-scripts.js +++ b/web3-scripts.js @@ -1,36 +1,15 @@ module.exports = { - sendRawTxTest: async function (web3) { - - - /** - * let transaction = { 'to': '0xF0109fC8DF283027b6285cc889F5aA624EaC1F55', - 'value': 1000000000, - 'gas': 2000000, - 'gasPrice': 234567897654321, - 'nonce': 0, - 'chainId': 1 } - - let signed = w3.eth.account.signTransaction(transaction, key) - w3.eth.sendRawTransaction(signed.rawTransaction) - */ - }, - filterWatches: async function (web3) { - + getBalance: async function (web3) { // erroing i think...try promise-chain. - //web3.eth.getBalance("0x7cB57B5A97eAbe94205C07890BE4c1aD31E486A8").toString(); - - - // https://github.com/trufflesuite/truffle/issues/492 // Unhandled rejection Error: Invalid JSON RPC response: "" - - // 1 confirm its on 8545 - // 2 confirm this url:port is good 'http://65.79.136.27:8545' - // 3 pass --rpc flag - // 4 see if was any activity from 2:45a - 3:15a EST 02/26/18 + web3.eth.getBalance("0x7cB57B5A97eAbe94205C07890BE4c1aD31E486A8").toString(); + }, + getBlock: async function (web3) { - web3.eth.getBlock(5000000) + return web3.eth.getBlock(5000000) .then((err, res) => { // err console.log('\n ======= err =====\n', err); console.log('\n ======= res =====\n', res); + return }) // ERROR also won't run... Error: Invalid JSON RPC response: " Date: Mon, 26 Feb 2018 12:32:43 -0500 Subject: [PATCH 03/27] config --- .gitignore | 1 + eth-nodes.js | 7 +++++-- index.js | 2 +- package.json | 1 + 4 files changed, 8 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 3c3629e..1dcef2d 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +.env \ No newline at end of file diff --git a/eth-nodes.js b/eth-nodes.js index dd7a2f2..f9569ee 100644 --- a/eth-nodes.js +++ b/eth-nodes.js @@ -3,9 +3,12 @@ module.exports = { myEtherWallet: 'https://api.myetherapi.com/eth', // 8545 is standard port that geth runs on, see gethLocal ^^ // chicagoGeth: 'http:65.79.136.27:8545' // Unhandled rejection Error: Invalid JSON RPC response: "" - lincolnPark: 'http:65.79.136.27:30303' // Unhandled rejection Error: Invalid JSON RPC response: "" + + lincolnPark: process.env.LP + + // lincolnPark: 'http:65.79.136.27:30303' // Unhandled rejection Error: Invalid JSON RPC response: "" // chicagoGeth: 'http://65.79.136.27:8545', // no error and no response either // correct URL format https://stackoverflow.com/questions/6940120/how-to-pass-ip-address-and-portnumber-as-url - //chicagoGeth: 'http://65.79.136.27:30303' // Unhandled rejection Error: Invalid JSON RPC response: "" + // chicagoGeth: 'http://65.79.136.27:30303' // Unhandled rejection Error: Invalid JSON RPC response: "" // chicagoGeth: '65.79.136.27:1988' // ssh port diff --git a/index.js b/index.js index f9584ec..e07907d 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,4 @@ +require('dotenv').config() const ethNodes = require('./eth-nodes') const web3Scripts = require('./web3-scripts') @@ -7,7 +8,6 @@ let web3 if (typeof web3 !== 'undefined') { web3 = new Web3(web3.currentProvider); } else { - console.log('chi town', lincolnPark) web3 = new Web3(new Web3.providers.HttpProvider(ethNodes.lincolnPark)) } diff --git a/package.json b/package.json index 34c78d7..04ac057 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "author": "", "license": "ISC", "dependencies": { + "dotenv": "^5.0.0", "web3": "^1.0.0-beta.30" } } From 30c24d45526a29e0ab1909e22f920a9333d69fb3 Mon Sep 17 00:00:00 2001 From: thinkocapo Date: Tue, 27 Feb 2018 23:35:06 -0500 Subject: [PATCH 04/27] Works with MyEtherWallet API. ethereumjs-tx --- eth-nodes.js | 25 ++---------------- index.js | 3 ++- indexOLD.js | 4 +++ package.json | 1 + web3-scripts.js | 68 ++++++++++++++++++++++++++++++++++++++++++++++--- 5 files changed, 73 insertions(+), 28 deletions(-) diff --git a/eth-nodes.js b/eth-nodes.js index f9569ee..48de749 100644 --- a/eth-nodes.js +++ b/eth-nodes.js @@ -1,28 +1,7 @@ module.exports = { gethLocal: 'http://localhost:8545', + lincolnPark: process.env.LP, myEtherWallet: 'https://api.myetherapi.com/eth', - // 8545 is standard port that geth runs on, see gethLocal ^^ - // chicagoGeth: 'http:65.79.136.27:8545' // Unhandled rejection Error: Invalid JSON RPC response: "" - - lincolnPark: process.env.LP - - // lincolnPark: 'http:65.79.136.27:30303' // Unhandled rejection Error: Invalid JSON RPC response: "" - // chicagoGeth: 'http://65.79.136.27:8545', // no error and no response either // correct URL format https://stackoverflow.com/questions/6940120/how-to-pass-ip-address-and-portnumber-as-url - // chicagoGeth: 'http://65.79.136.27:30303' // Unhandled rejection Error: Invalid JSON RPC response: "" - // chicagoGeth: '65.79.136.27:1988' // ssh port - - - // 30303 + etherScan: '' } -// correct format - -// https://github.com/ethereum/go-ethereum/issues/2982 -// https://github.com/ethereum/wiki/wiki/JSON-RPC#go - -// https://ethereum.stackexchange.com/questions/3163/how-can-i-expose-geths-rpc-server-to-external-connections - - -// ** -// geth --rpc --rpccorsdomain "http://localhost:3000" -// geth --rpc --rpccorsdomain "http://190.157.85.51:?" // My IP Address \ No newline at end of file diff --git a/index.js b/index.js index e07907d..b721d51 100644 --- a/index.js +++ b/index.js @@ -8,10 +8,11 @@ let web3 if (typeof web3 !== 'undefined') { web3 = new Web3(web3.currentProvider); } else { - web3 = new Web3(new Web3.providers.HttpProvider(ethNodes.lincolnPark)) + web3 = new Web3(new Web3.providers.HttpProvider(ethNodes.myEtherWallet)) } // Run script that was passed as commandLine parameter +if (!process.argv[2]) throw 'Must pass name of script as argument' const scriptName = process.argv[2] web3Scripts[scriptName](web3) diff --git a/indexOLD.js b/indexOLD.js index 1623361..c43a18a 100644 --- a/indexOLD.js +++ b/indexOLD.js @@ -100,6 +100,10 @@ const eosContractInstance = new web3.eth.Contract(eosContractABI, eosContractAdd console.log('\n === eosContractInstance.methods.claimAll === \n', eosContractInstance.methods.claimAll); // 'function () { [native code] }' return +// Send Ether to eosContractAddress + +// wait for 24hr period to end... + // GET TOKENS // Amount to Send: 0, GasLimit: 90000 // Invoke 'claimAll' off of web3.getContract(eos).claimAll [pseudocode] or see EOS obj I made 5:16p: diff --git a/package.json b/package.json index 04ac057..00a1943 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "license": "ISC", "dependencies": { "dotenv": "^5.0.0", + "ethereumjs-tx": "^1.3.3", "web3": "^1.0.0-beta.30" } } diff --git a/web3-scripts.js b/web3-scripts.js index b08e027..1c34db0 100644 --- a/web3-scripts.js +++ b/web3-scripts.js @@ -1,12 +1,72 @@ +const EthereumTx = require('ethereumjs-tx') + +var createTransactionObject = function (eosContractAddress) { + const txParams = { + nonce: '0x00', + // gas: // ? + gasPrice: '0x09184e72a000', + gasLimit: '0x2710', + to: eosContractAddress, //'0x0000000000000000000000000000000000000000' + value: '0x00', // wei? + data: '0x7f7465737432000000000000000000000000000000000000000000000000000000600057', // ? + // EIP 155 chainId - mainnet: 1, ropsten: 3 + chainId: 1 + // amount of eth to send? + } + return new EthereumTx(txParams) +} + +var signTransactionWithPrivateKey = function (tx, pKey) { + const privateKey = Buffer.from(pKey, 'hex') // toString() + tx.sign(privateKey) + const serializedTx = tx.serialize() + return serializedTx +} + module.exports = { + eos: async function (web3) { + const eosContractAddress = '0xd0a6E6C54DbC68Db5db3A091B171A77407Ff7ccf' + + // 1 CREATE TRANSACTION FOR SENDING ETHER FROM ACCOUNT ADDRESS TO EOS CONTRACT ADDRESS + const tx = createTransactionObject(eosContractAddress) + console.log('tx\n', tx) + const serializedTx = signTransactionWithPrivateKey(tx, process.env.PK) // makes it so funds are coming from the account address for that private key? + console.log('serializedTx\n', serializedTx) + // web3.eth.sendRawTransaction(signed.rawTransaction) // raw vs serialized ? + + // 24Hr window closes... + // 2 CLAIM EOS TOKENS + return + const gasLimit = 90000 + const eosContractABI = [{"constant":true,"inputs":[{"name":"","type":"uint256"},{"name":"","type":"address"}],"name":"claimed","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"owner_","type":"address"}],"name":"setOwner","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"time","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint128"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"foundersAllocation","outputs":[{"name":"","type":"uint128"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"day","type":"uint256"}],"name":"claim","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"foundersKey","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"},{"name":"","type":"address"}],"name":"userBuys","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"day","type":"uint256"}],"name":"createOnDay","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"freeze","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"keys","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"startTime","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"authority_","type":"address"}],"name":"setAuthority","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"dailyTotals","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"buy","outputs":[],"payable":true,"type":"function"},{"constant":true,"inputs":[],"name":"openTime","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"EOS","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"today","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"authority","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"eos","type":"address"}],"name":"initialize","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"createFirstDay","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"claimAll","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"timestamp","type":"uint256"}],"name":"dayFor","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"day","type":"uint256"},{"name":"limit","type":"uint256"}],"name":"buyWithLimit","outputs":[],"payable":true,"type":"function"},{"constant":false,"inputs":[],"name":"collect","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"numberOfDays","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"key","type":"string"}],"name":"register","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"createPerDay","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"inputs":[{"name":"_numberOfDays","type":"uint256"},{"name":"_totalSupply","type":"uint128"},{"name":"_openTime","type":"uint256"},{"name":"_startTime","type":"uint256"},{"name":"_foundersAllocation","type":"uint128"},{"name":"_foundersKey","type":"string"}],"payable":false,"type":"constructor"},{"payable":true,"type":"fallback"},{"anonymous":false,"inputs":[{"indexed":false,"name":"window","type":"uint256"},{"indexed":false,"name":"user","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"LogBuy","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"window","type":"uint256"},{"indexed":false,"name":"user","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"LogClaim","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"user","type":"address"},{"indexed":false,"name":"key","type":"string"}],"name":"LogRegister","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"amount","type":"uint256"}],"name":"LogCollect","type":"event"},{"anonymous":false,"inputs":[],"name":"LogFreeze","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"authority","type":"address"}],"name":"LogSetAuthority","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"}],"name":"LogSetOwner","type":"event"}] // should NOT be a string or will throw error in web3js/contracts.js + const eosContractInstance = new web3.eth.Contract(eosContractABI, eosContractAddress); + console.log('\n === eosContractInstance.methods.claimAll === \n', eosContractInstance.methods); // 'function () { [native code] }' + }, + + + + + getBalance: async function (web3) { // erroing i think...try promise-chain. - web3.eth.getBalance("0x7cB57B5A97eAbe94205C07890BE4c1aD31E486A8").toString(); + // const bal = + // web3.eth.getBalance("0x7cB57B5A97eAbe94205C07890BE4c1aD31E486A8") + web3.eth.getBalance("0x7602aCd0a747332Ce638a0b9f6d7532767303C8F") + .then((res, err) => { + console.log('\n ======= err =====\n', err); + console.log('\n ======= res =====\n', res) // .toString(10)); + const wei = res + // 02/27/18 below line fails... + // but if you paste the Wei into ether using https://etherconverter.online/ + // then you can see its the right amount of ether + // const ether = web3.eth.fromWei(wei, 'ether') + // console.log('\n ====== ether =====', ether) + }) }, getBlock: async function (web3) { - - return web3.eth.getBlock(5000000) - .then((err, res) => { // err + console.log('GET BLOCK\n') + return web3.eth.getBlock(5100000) + .then((res, err) => { // err console.log('\n ======= err =====\n', err); console.log('\n ======= res =====\n', res); return From bd37d38075952d1060628331de6fcff2a6d0dac9 Mon Sep 17 00:00:00 2001 From: thinkocapo Date: Wed, 28 Feb 2018 11:56:57 -0500 Subject: [PATCH 05/27] methods. raw tx --- eth-nodes.js | 3 +- index.js | 23 ++++++++---- web3-scripts.js | 97 +++++++++++++++++++++++++++++++------------------ 3 files changed, 80 insertions(+), 43 deletions(-) diff --git a/eth-nodes.js b/eth-nodes.js index 48de749..28d0e5f 100644 --- a/eth-nodes.js +++ b/eth-nodes.js @@ -1,7 +1,8 @@ module.exports = { + etherScan: '', gethLocal: 'http://localhost:8545', + infura: 'https://ethereum.stackexchange.com/questions/36358/how-to-properly-create-a-raw-transaction-and-sign-it-using-web3-in-browser?rq=1', // privateKey to infura lincolnPark: process.env.LP, myEtherWallet: 'https://api.myetherapi.com/eth', - etherScan: '' } diff --git a/index.js b/index.js index b721d51..d25c783 100644 --- a/index.js +++ b/index.js @@ -2,14 +2,23 @@ require('dotenv').config() const ethNodes = require('./eth-nodes') const web3Scripts = require('./web3-scripts') +const node = ethNodes.myEtherWallet || ethNodes[process.argv[3]] + // WEB3 CONFIG -const Web3 = require('web3') -let web3 -if (typeof web3 !== 'undefined') { - web3 = new Web3(web3.currentProvider); -} else { - web3 = new Web3(new Web3.providers.HttpProvider(ethNodes.myEtherWallet)) -} +// if you don't pass httpProvider then it will not be connected to a geth instance +let web3Class = new Web3 +let web3 = new Web3(web3Class.providers.HttpProviders(node)) + +// New +// const Web3 = require('web3') +// let web3 = new Web3(new Web3.providers.HttpProvider(ethNodes.myEtherWallet)) + +// Original +// if (typeof web3 !== 'undefined') { +// web3 = new Web3(web3.currentProvider); +// } else { +// web3 = new Web3(new Web3.providers.HttpProvider(ethNodes.myEtherWallet)) +// } // Run script that was passed as commandLine parameter if (!process.argv[2]) throw 'Must pass name of script as argument' diff --git a/web3-scripts.js b/web3-scripts.js index 1c34db0..307dd43 100644 --- a/web3-scripts.js +++ b/web3-scripts.js @@ -1,51 +1,81 @@ -const EthereumTx = require('ethereumjs-tx') +const TX = require('ethereumjs-tx') +// https://github.com/ethereumjs/ethereumjs-tx/blob/master/examples/transactions.js +// https://github.com/SilentCicero/ethereumjs-accounts +// sendTransaction vs sendRawTransaction +// estimateGas() + +// * +// https://tokenmarket.net/blog/creating-offline-ethereum-transactions-in-javascript/ + +/** + * A raw transaction is a transaction in raw bytes. + * If one has the raw bytes of a valid transaction, they can use sendRawTransaction. + * Otherwise, web3.js creates the signed transaction's bytes for you automatically as part of sendTransaction(). + * Raw bytes are required if you are using a platform like infura.io which does not handle private keys but deal only with signed transactions. + * https://ethereum.stackexchange.com/questions/6905/difference-between-transactions-and-raw-transactions-in-web3-js + * Basically a raw transaction is a machine representation of a transaction, with the signature attached to it. + * https://ethereum.stackexchange.com/questions/18928/what-is-a-raw-transaction-and-what-is-it-used-for + */ + +const eosContractAddress = '0xd0a6E6C54DbC68Db5db3A091B171A77407Ff7ccf' +const eosContractABI = [{"constant":true,"inputs":[{"name":"","type":"uint256"},{"name":"","type":"address"}],"name":"claimed","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"owner_","type":"address"}],"name":"setOwner","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"time","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint128"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"foundersAllocation","outputs":[{"name":"","type":"uint128"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"day","type":"uint256"}],"name":"claim","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"foundersKey","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"},{"name":"","type":"address"}],"name":"userBuys","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"day","type":"uint256"}],"name":"createOnDay","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"freeze","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"keys","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"startTime","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"authority_","type":"address"}],"name":"setAuthority","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"dailyTotals","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"buy","outputs":[],"payable":true,"type":"function"},{"constant":true,"inputs":[],"name":"openTime","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"EOS","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"today","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"authority","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"eos","type":"address"}],"name":"initialize","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"createFirstDay","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"claimAll","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"timestamp","type":"uint256"}],"name":"dayFor","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"day","type":"uint256"},{"name":"limit","type":"uint256"}],"name":"buyWithLimit","outputs":[],"payable":true,"type":"function"},{"constant":false,"inputs":[],"name":"collect","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"numberOfDays","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"key","type":"string"}],"name":"register","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"createPerDay","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"inputs":[{"name":"_numberOfDays","type":"uint256"},{"name":"_totalSupply","type":"uint128"},{"name":"_openTime","type":"uint256"},{"name":"_startTime","type":"uint256"},{"name":"_foundersAllocation","type":"uint128"},{"name":"_foundersKey","type":"string"}],"payable":false,"type":"constructor"},{"payable":true,"type":"fallback"},{"anonymous":false,"inputs":[{"indexed":false,"name":"window","type":"uint256"},{"indexed":false,"name":"user","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"LogBuy","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"window","type":"uint256"},{"indexed":false,"name":"user","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"LogClaim","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"user","type":"address"},{"indexed":false,"name":"key","type":"string"}],"name":"LogRegister","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"amount","type":"uint256"}],"name":"LogCollect","type":"event"},{"anonymous":false,"inputs":[],"name":"LogFreeze","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"authority","type":"address"}],"name":"LogSetAuthority","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"}],"name":"LogSetOwner","type":"event"}] // should NOT be a string or will throw error in web3js/contracts.js +const gasLimit = 90000 var createTransactionObject = function (eosContractAddress) { - const txParams = { - nonce: '0x00', - // gas: // ? - gasPrice: '0x09184e72a000', - gasLimit: '0x2710', - to: eosContractAddress, //'0x0000000000000000000000000000000000000000' + const bytcode = null // bytcode of compiled contract https://ethereum.stackexchange.com/questions/25839/how-to-make-transactions-using-private-key-in-web3 + const rawTx = { + nonce: '0x00', // https://github.com/trufflesuite/ganache-cli/issues/344 + chainId: 1, // EIP 155 chainId - mainnet: 1, ropsten: 3 + data: bytcode, // '0x7f7465737432000000000000000000000000000000000000000000000000000000600057', // get the bytcode https://gist.github.com/tomconte/4edb83cf505f1e7faf172b9252fff9bf + // gas: feeCost, + gasLimit: '0x2710', // 1000 https://ethereum.stackexchange.com/questions/25839/how-to-make-transactions-using-private-key-in-web3 + gasPrice: '0x09184e72a000', // 100 + to: eosContractAddress, value: '0x00', // wei? - data: '0x7f7465737432000000000000000000000000000000000000000000000000000000600057', // ? - // EIP 155 chainId - mainnet: 1, ropsten: 3 - chainId: 1 - // amount of eth to send? + // https://www.reddit.com/r/ethdev/comments/71rhzs/05_eth_bounty_help_needed_for_sending_raw/ } - return new EthereumTx(txParams) + return new TX(rawTx) // Transaction: { raw: [ ], _fields: ['nonce',]} } var signTransactionWithPrivateKey = function (tx, pKey) { + //var feeCost = tx.getUpfrontCost() + //console.log('feeCost', feeCost.toString()) // ? const privateKey = Buffer.from(pKey, 'hex') // toString() - tx.sign(privateKey) + tx.sign(privateKey) // or web3.eth.sign(address, dataToSign, [, callback]) but account would need to be unlocked https://github.com/ethereum/wiki/wiki/JavaScript-API#web3ethsendrawtransaction const serializedTx = tx.serialize() - return serializedTx + return serializedTx // + // console.log('serializedTx.toString(hex)\n', serializedTx.toString('hex')) // f889808609184e72a00082271094d0a6e6c54dbc68d } module.exports = { - eos: async function (web3) { - const eosContractAddress = '0xd0a6E6C54DbC68Db5db3A091B171A77407Ff7ccf' - - // 1 CREATE TRANSACTION FOR SENDING ETHER FROM ACCOUNT ADDRESS TO EOS CONTRACT ADDRESS + + // STEP 1 CREATE TRANSACTION FOR SENDING ETHER FROM ACCOUNT ADDRESS TO EOS CONTRACT ADDRESS + sendEth2EosContract: async function (web3) { const tx = createTransactionObject(eosContractAddress) - console.log('tx\n', tx) const serializedTx = signTransactionWithPrivateKey(tx, process.env.PK) // makes it so funds are coming from the account address for that private key? - console.log('serializedTx\n', serializedTx) - // web3.eth.sendRawTransaction(signed.rawTransaction) // raw vs serialized ? - - // 24Hr window closes... - // 2 CLAIM EOS TOKENS - return - const gasLimit = 90000 - const eosContractABI = [{"constant":true,"inputs":[{"name":"","type":"uint256"},{"name":"","type":"address"}],"name":"claimed","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"owner_","type":"address"}],"name":"setOwner","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"time","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint128"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"foundersAllocation","outputs":[{"name":"","type":"uint128"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"day","type":"uint256"}],"name":"claim","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"foundersKey","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"},{"name":"","type":"address"}],"name":"userBuys","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"day","type":"uint256"}],"name":"createOnDay","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"freeze","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"keys","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"startTime","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"authority_","type":"address"}],"name":"setAuthority","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"dailyTotals","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"buy","outputs":[],"payable":true,"type":"function"},{"constant":true,"inputs":[],"name":"openTime","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"EOS","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"today","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"authority","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"eos","type":"address"}],"name":"initialize","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"createFirstDay","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"claimAll","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"timestamp","type":"uint256"}],"name":"dayFor","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"day","type":"uint256"},{"name":"limit","type":"uint256"}],"name":"buyWithLimit","outputs":[],"payable":true,"type":"function"},{"constant":false,"inputs":[],"name":"collect","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"numberOfDays","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"key","type":"string"}],"name":"register","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"createPerDay","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"inputs":[{"name":"_numberOfDays","type":"uint256"},{"name":"_totalSupply","type":"uint128"},{"name":"_openTime","type":"uint256"},{"name":"_startTime","type":"uint256"},{"name":"_foundersAllocation","type":"uint128"},{"name":"_foundersKey","type":"string"}],"payable":false,"type":"constructor"},{"payable":true,"type":"fallback"},{"anonymous":false,"inputs":[{"indexed":false,"name":"window","type":"uint256"},{"indexed":false,"name":"user","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"LogBuy","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"window","type":"uint256"},{"indexed":false,"name":"user","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"LogClaim","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"user","type":"address"},{"indexed":false,"name":"key","type":"string"}],"name":"LogRegister","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"amount","type":"uint256"}],"name":"LogCollect","type":"event"},{"anonymous":false,"inputs":[],"name":"LogFreeze","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"authority","type":"address"}],"name":"LogSetAuthority","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"}],"name":"LogSetOwner","type":"event"}] // should NOT be a string or will throw error in web3js/contracts.js + + return web3.eth.sendRawTransaction('0x' + serializedTx.toString('hex'), function(err, hash) { if (!err) { + console.log('hash', hash); // "0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385" + } else { console.log(' err sendRawTransaction \n', err)} + }); + }, + + // 24Hr window closes... + // TODO - make a single method that will first check if their are new EOS tokens to claim, and then run claimAll and exchange workflow + // and if no new EOS token available, then send eth to EOS Crowdsale + + // STEP 2 CLAIM EOS TOKENS + claimEos: function (web3) { const eosContractInstance = new web3.eth.Contract(eosContractABI, eosContractAddress); console.log('\n === eosContractInstance.methods.claimAll === \n', eosContractInstance.methods); // 'function () { [native code] }' }, - - - + sendEosToExchange: function () { + // exchange api - send eos to exchange + // exchange api - exchange eos for eth + // exchange api - send eth back to default account address + // log details on what fees/money was made + }, getBalance: async function (web3) { // erroing i think...try promise-chain. @@ -56,8 +86,8 @@ module.exports = { console.log('\n ======= err =====\n', err); console.log('\n ======= res =====\n', res) // .toString(10)); const wei = res - // 02/27/18 below line fails... // but if you paste the Wei into ether using https://etherconverter.online/ + // 02/27/18 below line fails... // then you can see its the right amount of ether // const ether = web3.eth.fromWei(wei, 'ether') // console.log('\n ====== ether =====', ether) @@ -94,8 +124,6 @@ module.exports = { */ }, filterWatches: async function (web3) { - console.log('filterWatches....') - // ERROR web3.eth.filter is not a function... isn't there an newer syntax if you check the wiki doc? // var filter = web3.eth.filter('pending'); // .Filter // var filter = web3.eth.subscribe('pendingTransactions', function (err, res) { @@ -113,7 +141,6 @@ module.exports = { //Result... //checkout latest transactions below, it might take a second to load as it is waiting for incoming txs - }, - myFunc () {} + } } From ff0c9392bc8abd338591d5ba6e79ada0b5b4aa37 Mon Sep 17 00:00:00 2001 From: thinkocapo Date: Wed, 28 Feb 2018 12:01:33 -0500 Subject: [PATCH 06/27] Raw Transaction vs send Transaction notes --- README.md | 21 +++++++++++++++++++++ web3-scripts.js | 9 --------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 30a84cd..13d26fe 100644 --- a/README.md +++ b/README.md @@ -93,3 +93,24 @@ https://github.com/paritytech/parity/wiki/Basic-Usage#javascript-console Applica 1. Save the "snapshots" via parity ui --warp --mode=passive 2. try getting my balance / importing latest block / getting EOS contract 3. Need import private key? Can do from web3.js or JSON-RPC Client + +#### Sending Transaction vs Raw Transaction +**Raw Transaction** - you sign the tx object using a privateKey, before sending to the geth node. tx.sign web3.eth.sendRawTransaction +A raw transaction is a transaction in raw bytes. +If one has the raw bytes of a valid transaction, they can use sendRawTransaction. + +Raw bytes are required if you are using a platform like infura.io which does not handle private keys but deal only with signed transactions. + +https://ethereum.stackexchange.com/questions/6905/difference-between-transactions-and-raw-transactions-in-web3-js +Basically a raw transaction is a machine representation of a transaction, with the signature attached to it. +https://ethereum.stackexchange.com/questions/18928/what-is-a-raw-transaction-and-what-is-it-used-for + +**Transaction ** - web3.accounts[0] - you already unlocked the account at that node, the node can handle privateKeys. +Otherwise, web3.js creates the signed transaction's bytes for you automatically as part of sendTransaction(). + +another way - https://github.com/ethereum/go-ethereum/wiki/Sending-ether + + + + + \ No newline at end of file diff --git a/web3-scripts.js b/web3-scripts.js index 307dd43..b796524 100644 --- a/web3-scripts.js +++ b/web3-scripts.js @@ -7,15 +7,6 @@ const TX = require('ethereumjs-tx') // * // https://tokenmarket.net/blog/creating-offline-ethereum-transactions-in-javascript/ -/** - * A raw transaction is a transaction in raw bytes. - * If one has the raw bytes of a valid transaction, they can use sendRawTransaction. - * Otherwise, web3.js creates the signed transaction's bytes for you automatically as part of sendTransaction(). - * Raw bytes are required if you are using a platform like infura.io which does not handle private keys but deal only with signed transactions. - * https://ethereum.stackexchange.com/questions/6905/difference-between-transactions-and-raw-transactions-in-web3-js - * Basically a raw transaction is a machine representation of a transaction, with the signature attached to it. - * https://ethereum.stackexchange.com/questions/18928/what-is-a-raw-transaction-and-what-is-it-used-for - */ const eosContractAddress = '0xd0a6E6C54DbC68Db5db3A091B171A77407Ff7ccf' const eosContractABI = [{"constant":true,"inputs":[{"name":"","type":"uint256"},{"name":"","type":"address"}],"name":"claimed","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"owner_","type":"address"}],"name":"setOwner","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"time","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint128"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"foundersAllocation","outputs":[{"name":"","type":"uint128"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"day","type":"uint256"}],"name":"claim","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"foundersKey","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"},{"name":"","type":"address"}],"name":"userBuys","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"day","type":"uint256"}],"name":"createOnDay","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"freeze","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"keys","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"startTime","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"authority_","type":"address"}],"name":"setAuthority","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"dailyTotals","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"buy","outputs":[],"payable":true,"type":"function"},{"constant":true,"inputs":[],"name":"openTime","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"EOS","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"today","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"authority","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"eos","type":"address"}],"name":"initialize","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"createFirstDay","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"claimAll","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"timestamp","type":"uint256"}],"name":"dayFor","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"day","type":"uint256"},{"name":"limit","type":"uint256"}],"name":"buyWithLimit","outputs":[],"payable":true,"type":"function"},{"constant":false,"inputs":[],"name":"collect","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"numberOfDays","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"key","type":"string"}],"name":"register","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"createPerDay","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"inputs":[{"name":"_numberOfDays","type":"uint256"},{"name":"_totalSupply","type":"uint128"},{"name":"_openTime","type":"uint256"},{"name":"_startTime","type":"uint256"},{"name":"_foundersAllocation","type":"uint128"},{"name":"_foundersKey","type":"string"}],"payable":false,"type":"constructor"},{"payable":true,"type":"fallback"},{"anonymous":false,"inputs":[{"indexed":false,"name":"window","type":"uint256"},{"indexed":false,"name":"user","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"LogBuy","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"window","type":"uint256"},{"indexed":false,"name":"user","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"LogClaim","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"user","type":"address"},{"indexed":false,"name":"key","type":"string"}],"name":"LogRegister","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"amount","type":"uint256"}],"name":"LogCollect","type":"event"},{"anonymous":false,"inputs":[],"name":"LogFreeze","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"authority","type":"address"}],"name":"LogSetAuthority","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"}],"name":"LogSetOwner","type":"event"}] // should NOT be a string or will throw error in web3js/contracts.js From 561e0ad5cf9b0c6f2219877711e85bf6637afd63 Mon Sep 17 00:00:00 2001 From: thinkocapo Date: Fri, 2 Mar 2018 15:55:43 -0500 Subject: [PATCH 07/27] updates. workflow --- index.js | 13 ++-- scripts-testing.js | 67 +++++++++++++++++++++ web3-scripts.js => scripts.js | 108 +++++++++++----------------------- 3 files changed, 106 insertions(+), 82 deletions(-) create mode 100644 scripts-testing.js rename web3-scripts.js => scripts.js (71%) diff --git a/index.js b/index.js index d25c783..d6eea40 100644 --- a/index.js +++ b/index.js @@ -1,18 +1,15 @@ require('dotenv').config() const ethNodes = require('./eth-nodes') -const web3Scripts = require('./web3-scripts') - +const scripts = require('./scripts') +const Web3 = require('web3') +// node to connect to const node = ethNodes.myEtherWallet || ethNodes[process.argv[3]] // WEB3 CONFIG // if you don't pass httpProvider then it will not be connected to a geth instance -let web3Class = new Web3 +let web3Class = new Web3() let web3 = new Web3(web3Class.providers.HttpProviders(node)) -// New -// const Web3 = require('web3') -// let web3 = new Web3(new Web3.providers.HttpProvider(ethNodes.myEtherWallet)) - // Original // if (typeof web3 !== 'undefined') { // web3 = new Web3(web3.currentProvider); @@ -23,7 +20,7 @@ let web3 = new Web3(web3Class.providers.HttpProviders(node)) // Run script that was passed as commandLine parameter if (!process.argv[2]) throw 'Must pass name of script as argument' const scriptName = process.argv[2] -web3Scripts[scriptName](web3) +scripts[scriptName](web3) // NOTES and alternative code diff --git a/scripts-testing.js b/scripts-testing.js new file mode 100644 index 0000000..58b84d5 --- /dev/null +++ b/scripts-testing.js @@ -0,0 +1,67 @@ +module.exports = { + getBalance: async function (web3) { + // erroing i think...try promise-chain. + // const bal = + // web3.eth.getBalance("0x7cB57B5A97eAbe94205C07890BE4c1aD31E486A8") + web3.eth.getBalance("0x7602aCd0a747332Ce638a0b9f6d7532767303C8F") + .then((res, err) => { + console.log('\n ======= err =====\n', err); + console.log('\n ======= res =====\n', res) // .toString(10)); + const wei = res + // but if you paste the Wei into ether using https://etherconverter.online/ + // 02/27/18 below line fails... + // then you can see its the right amount of ether + // const ether = web3.eth.fromWei(wei, 'ether') + // console.log('\n ====== ether =====', ether) + }) + }, + getBlock: async function (web3) { + console.log('GET BLOCK\n') + return web3.eth.getBlock(5100000) + .then((res, err) => { // err + console.log('\n ======= err =====\n', err); + console.log('\n ======= res =====\n', res); + return + }) + + // ERROR also won't run... Error: Invalid JSON RPC response: ") // https://github.com/trufflesuite/ganache-cli/issues/344 + // maybe this is only for smart contract deployment?? + data: createByteCodeForContract('eos_sale.sol'), //TRY? - need generate bytcode of the contract (or method?) i think EOS Contract '0x7f7465737432000000000000000000000000000000000000000000000000000000600057', // get the bytcode https://gist.github.com/tomconte/4edb83cf505f1e7faf172b9252fff9bf is the contract bytcode?? A. var bytecode = Bytecode of compiled contract https://ethereum.stackexchange.com/questions/25839/how-to-make-transactions-using-private-key-in-web3 + // ^^ says try Remix browser, but still, how many contracts? // https://ethereum.stackexchange.com/questions/27096/how-do-i-get-bytecode-of-the-contract-to-deploy + // also, tries contract data like this: + // Get contract data + // const contractData = contract.new.getData({ + // data: '0x' + bytecode + // }); + + // how to find current gas price + // const gasPrice = web3.eth.gasPrice; // price is in GWEI ? + // const gasPriceHex = web3.toHex(gasPrice); + // const gasLimitHex = web3.toHex(300000); + gasPrice: '0x09184e72a000', // TRY gasPriceHex 100 price is in GWEI ? + gasLimit: '0x2710', // TRY gasLimitHex 1000 https://ethereum.stackexchange.com/questions/25839/how-to-make-transactions-using-private-key-in-web3 + to: eosContractAddress, - value: '0x00', // wei? + value: '0x00', // GOOD try web3.toHex(web3.toWei(value, "ether")); - value comes from process.argv[] vs geth console might allow amount = web3.toWei(0.01, "ether") + gas: null // - based on the data/bytcode size https://github.com/ethereum/wiki/wiki/JavaScript-API#web3ethsendrawtransaction // https://www.reddit.com/r/ethdev/comments/71rhzs/05_eth_bounty_help_needed_for_sending_raw/ } return new TX(rawTx) // Transaction: { raw: [ ], _fields: ['nonce',]} } +// chainId: 1, - might be a default, EIP 155 chainId - mainnet: 1, ropsten: 3 +// gas: feeCost, - seems like mostly gasLimit and gasPrice var signTransactionWithPrivateKey = function (tx, pKey) { //var feeCost = tx.getUpfrontCost() @@ -58,80 +83,15 @@ module.exports = { // STEP 2 CLAIM EOS TOKENS claimEos: function (web3) { const eosContractInstance = new web3.eth.Contract(eosContractABI, eosContractAddress); - console.log('\n === eosContractInstance.methods.claimAll === \n', eosContractInstance.methods); // 'function () { [native code] }' + // console.log('\n === eosContractInstance.methods.claimAll === \n', eosContractInstance.methods); // 'function () { [native code] }' }, + // STEP 3 sendEosToExchange: function () { // exchange api - send eos to exchange // exchange api - exchange eos for eth // exchange api - send eth back to default account address // log details on what fees/money was made - }, - - getBalance: async function (web3) { - // erroing i think...try promise-chain. - // const bal = - // web3.eth.getBalance("0x7cB57B5A97eAbe94205C07890BE4c1aD31E486A8") - web3.eth.getBalance("0x7602aCd0a747332Ce638a0b9f6d7532767303C8F") - .then((res, err) => { - console.log('\n ======= err =====\n', err); - console.log('\n ======= res =====\n', res) // .toString(10)); - const wei = res - // but if you paste the Wei into ether using https://etherconverter.online/ - // 02/27/18 below line fails... - // then you can see its the right amount of ether - // const ether = web3.eth.fromWei(wei, 'ether') - // console.log('\n ====== ether =====', ether) - }) - }, - getBlock: async function (web3) { - console.log('GET BLOCK\n') - return web3.eth.getBlock(5100000) - .then((res, err) => { // err - console.log('\n ======= err =====\n', err); - console.log('\n ======= res =====\n', res); - return - }) - - // ERROR also won't run... Error: Invalid JSON RPC response: " Date: Sat, 3 Mar 2018 15:34:04 -0500 Subject: [PATCH 08/27] update --- scripts.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/scripts.js b/scripts.js index b15c623..7cc4ecb 100644 --- a/scripts.js +++ b/scripts.js @@ -23,7 +23,7 @@ var createByteCodeForContract = function (contract) { return bytcode } -var createTransactionObject = function (eosContractAddress) { +var createTxObject = function (eosContractAddress, value) { const bytcode = null // bytcode of compiled contract https://ethereum.stackexchange.com/questions/25839/how-to-make-transactions-using-private-key-in-web3 const rawTx = { nonce: "", // GOOD try eth.getTransactionCount() // https://github.com/trufflesuite/ganache-cli/issues/344 @@ -53,7 +53,7 @@ var createTransactionObject = function (eosContractAddress) { // chainId: 1, - might be a default, EIP 155 chainId - mainnet: 1, ropsten: 3 // gas: feeCost, - seems like mostly gasLimit and gasPrice -var signTransactionWithPrivateKey = function (tx, pKey) { +var signTxByPrivateKey = function (tx, pKey) { //var feeCost = tx.getUpfrontCost() //console.log('feeCost', feeCost.toString()) // ? const privateKey = Buffer.from(pKey, 'hex') // toString() @@ -67,10 +67,10 @@ module.exports = { // STEP 1 CREATE TRANSACTION FOR SENDING ETHER FROM ACCOUNT ADDRESS TO EOS CONTRACT ADDRESS sendEth2EosContract: async function (web3) { - const tx = createTransactionObject(eosContractAddress) - const serializedTx = signTransactionWithPrivateKey(tx, process.env.PK) // makes it so funds are coming from the account address for that private key? + const tx = createTxObject(eosContractAddress, 10000000) + const txSerialized = signTransactionByPrivateKey(tx, process.env.PK) // makes it so funds are coming from the account address for that private key? - return web3.eth.sendRawTransaction('0x' + serializedTx.toString('hex'), function(err, hash) { if (!err) { + return web3.eth.sendRawTransaction('0x' + txSerialized.toString('hex'), function(err, hash) { if (!err) { console.log('hash', hash); // "0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385" } else { console.log(' err sendRawTransaction \n', err)} }); From 904ed6c788e22384509b7eb25f37a1fc117afd00 Mon Sep 17 00:00:00 2001 From: thinkocapo Date: Sat, 3 Mar 2018 19:25:05 -0500 Subject: [PATCH 09/27] nonce --- scripts.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/scripts.js b/scripts.js index 7cc4ecb..c8d0fae 100644 --- a/scripts.js +++ b/scripts.js @@ -7,7 +7,7 @@ const TX = require('ethereumjs-tx') // * // https://tokenmarket.net/blog/creating-offline-ethereum-transactions-in-javascript/ - +const address = process.env.address const eosContractAddress = '0xd0a6E6C54DbC68Db5db3A091B171A77407Ff7ccf' const eosContractABI = [{"constant":true,"inputs":[{"name":"","type":"uint256"},{"name":"","type":"address"}],"name":"claimed","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"owner_","type":"address"}],"name":"setOwner","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"time","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint128"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"foundersAllocation","outputs":[{"name":"","type":"uint128"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"day","type":"uint256"}],"name":"claim","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"foundersKey","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"},{"name":"","type":"address"}],"name":"userBuys","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"day","type":"uint256"}],"name":"createOnDay","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"freeze","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"keys","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"startTime","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"authority_","type":"address"}],"name":"setAuthority","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"dailyTotals","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"buy","outputs":[],"payable":true,"type":"function"},{"constant":true,"inputs":[],"name":"openTime","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"EOS","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"today","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"authority","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"eos","type":"address"}],"name":"initialize","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"createFirstDay","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"claimAll","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"timestamp","type":"uint256"}],"name":"dayFor","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"day","type":"uint256"},{"name":"limit","type":"uint256"}],"name":"buyWithLimit","outputs":[],"payable":true,"type":"function"},{"constant":false,"inputs":[],"name":"collect","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"numberOfDays","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"key","type":"string"}],"name":"register","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"createPerDay","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"inputs":[{"name":"_numberOfDays","type":"uint256"},{"name":"_totalSupply","type":"uint128"},{"name":"_openTime","type":"uint256"},{"name":"_startTime","type":"uint256"},{"name":"_foundersAllocation","type":"uint128"},{"name":"_foundersKey","type":"string"}],"payable":false,"type":"constructor"},{"payable":true,"type":"fallback"},{"anonymous":false,"inputs":[{"indexed":false,"name":"window","type":"uint256"},{"indexed":false,"name":"user","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"LogBuy","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"window","type":"uint256"},{"indexed":false,"name":"user","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"LogClaim","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"user","type":"address"},{"indexed":false,"name":"key","type":"string"}],"name":"LogRegister","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"amount","type":"uint256"}],"name":"LogCollect","type":"event"},{"anonymous":false,"inputs":[],"name":"LogFreeze","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"authority","type":"address"}],"name":"LogSetAuthority","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"}],"name":"LogSetOwner","type":"event"}] // should NOT be a string or will throw error in web3js/contracts.js const gasLimit = 90000 @@ -26,9 +26,10 @@ var createByteCodeForContract = function (contract) { var createTxObject = function (eosContractAddress, value) { const bytcode = null // bytcode of compiled contract https://ethereum.stackexchange.com/questions/25839/how-to-make-transactions-using-private-key-in-web3 const rawTx = { - nonce: "", // GOOD try eth.getTransactionCount() // https://github.com/trufflesuite/ganache-cli/issues/344 + nonce: eth.getTransactionCount(process.env.address), // getTransactionCountAsync + data: createByteCodeForContract('eos_sale.sol'), // maybe this is only for smart contract deployment?? - data: createByteCodeForContract('eos_sale.sol'), //TRY? - need generate bytcode of the contract (or method?) i think EOS Contract '0x7f7465737432000000000000000000000000000000000000000000000000000000600057', // get the bytcode https://gist.github.com/tomconte/4edb83cf505f1e7faf172b9252fff9bf is the contract bytcode?? A. var bytecode = Bytecode of compiled contract https://ethereum.stackexchange.com/questions/25839/how-to-make-transactions-using-private-key-in-web3 + //TRY? - need generate bytcode of the contract (or method?) i think EOS Contract '0x7f7465737432000000000000000000000000000000000000000000000000000000600057', // get the bytcode https://gist.github.com/tomconte/4edb83cf505f1e7faf172b9252fff9bf is the contract bytcode?? A. var bytecode = Bytecode of compiled contract https://ethereum.stackexchange.com/questions/25839/how-to-make-transactions-using-private-key-in-web3 // ^^ says try Remix browser, but still, how many contracts? // https://ethereum.stackexchange.com/questions/27096/how-do-i-get-bytecode-of-the-contract-to-deploy // also, tries contract data like this: // Get contract data From dc0b3c3510c7d766c54436ead5c7e4ce525644c7 Mon Sep 17 00:00:00 2001 From: thinkocapo Date: Sat, 3 Mar 2018 19:52:48 -0500 Subject: [PATCH 10/27] raw tx params --- .gitignore | 3 ++- scripts.js | 36 ++++++++++++++++++------------------ 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/.gitignore b/.gitignore index 1dcef2d..db8c3a1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules -.env \ No newline at end of file +.env +notes \ No newline at end of file diff --git a/scripts.js b/scripts.js index c8d0fae..9e88418 100644 --- a/scripts.js +++ b/scripts.js @@ -24,31 +24,29 @@ var createByteCodeForContract = function (contract) { } var createTxObject = function (eosContractAddress, value) { - const bytcode = null // bytcode of compiled contract https://ethereum.stackexchange.com/questions/25839/how-to-make-transactions-using-private-key-in-web3 + const gasLimitHex = web3.toHex(300000); + console.log('gasLimitHex', gasLimitHex) + const rawTx = { - nonce: eth.getTransactionCount(process.env.address), // getTransactionCountAsync - data: createByteCodeForContract('eos_sale.sol'), - // maybe this is only for smart contract deployment?? - //TRY? - need generate bytcode of the contract (or method?) i think EOS Contract '0x7f7465737432000000000000000000000000000000000000000000000000000000600057', // get the bytcode https://gist.github.com/tomconte/4edb83cf505f1e7faf172b9252fff9bf is the contract bytcode?? A. var bytecode = Bytecode of compiled contract https://ethereum.stackexchange.com/questions/25839/how-to-make-transactions-using-private-key-in-web3 - // ^^ says try Remix browser, but still, how many contracts? // https://ethereum.stackexchange.com/questions/27096/how-do-i-get-bytecode-of-the-contract-to-deploy - // also, tries contract data like this: - // Get contract data - // const contractData = contract.new.getData({ - // data: '0x' + bytecode - // }); + nonce: web3.eth.getTransactionCount(process.env.address), // getTransactionCountAsync - // how to find current gas price - // const gasPrice = web3.eth.gasPrice; // price is in GWEI ? - // const gasPriceHex = web3.toHex(gasPrice); - // const gasLimitHex = web3.toHex(300000); - gasPrice: '0x09184e72a000', // TRY gasPriceHex 100 price is in GWEI ? - gasLimit: '0x2710', // TRY gasLimitHex 1000 https://ethereum.stackexchange.com/questions/25839/how-to-make-transactions-using-private-key-in-web3 + gasPrice : web3.eth.gasPrice(), // need hex value + // gasPrice: web3.eth.gasPrice(function (err, result) { + // console.log('result', result) // gwei + // const gasPriceHex = web3.toHex(result); // 0x09184e72a000 + // }), + // The gas price is determined by the x latest blocks median gas price. + + gasLimit: '0x2710', + + gas: null, // - based on the data/bytcode size https://github.com/ethereum/wiki/wiki/JavaScript-API#web3ethsendrawtransaction to: eosContractAddress, value: '0x00', // GOOD try web3.toHex(web3.toWei(value, "ether")); - value comes from process.argv[] vs geth console might allow amount = web3.toWei(0.01, "ether") - gas: null // - based on the data/bytcode size https://github.com/ethereum/wiki/wiki/JavaScript-API#web3ethsendrawtransaction // https://www.reddit.com/r/ethdev/comments/71rhzs/05_eth_bounty_help_needed_for_sending_raw/ } + + console.log('\n rawTx \n', rawTx) return new TX(rawTx) // Transaction: { raw: [ ], _fields: ['nonce',]} } // chainId: 1, - might be a default, EIP 155 chainId - mainnet: 1, ropsten: 3 @@ -69,8 +67,10 @@ module.exports = { // STEP 1 CREATE TRANSACTION FOR SENDING ETHER FROM ACCOUNT ADDRESS TO EOS CONTRACT ADDRESS sendEth2EosContract: async function (web3) { const tx = createTxObject(eosContractAddress, 10000000) + console.log('\n TX \n', tx) const txSerialized = signTransactionByPrivateKey(tx, process.env.PK) // makes it so funds are coming from the account address for that private key? + return return web3.eth.sendRawTransaction('0x' + txSerialized.toString('hex'), function(err, hash) { if (!err) { console.log('hash', hash); // "0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385" } else { console.log(' err sendRawTransaction \n', err)} From 6f94fafc4d3a8e38dc1ccf809e4a341cd62bbaec Mon Sep 17 00:00:00 2001 From: thinkocapo Date: Sun, 4 Mar 2018 22:58:51 -0500 Subject: [PATCH 11/27] Raw Transaction instance made. async callbacks managed --- index.js | 17 +++------- scripts.js | 91 ++++++++++++++++++++++++------------------------------ 2 files changed, 45 insertions(+), 63 deletions(-) diff --git a/index.js b/index.js index d6eea40..5505df9 100644 --- a/index.js +++ b/index.js @@ -2,20 +2,11 @@ require('dotenv').config() const ethNodes = require('./eth-nodes') const scripts = require('./scripts') const Web3 = require('web3') -// node to connect to -const node = ethNodes.myEtherWallet || ethNodes[process.argv[3]] -// WEB3 CONFIG -// if you don't pass httpProvider then it will not be connected to a geth instance -let web3Class = new Web3() -let web3 = new Web3(web3Class.providers.HttpProviders(node)) - -// Original -// if (typeof web3 !== 'undefined') { -// web3 = new Web3(web3.currentProvider); -// } else { -// web3 = new Web3(new Web3.providers.HttpProvider(ethNodes.myEtherWallet)) -// } +// WEB3 CONFIG - Web3 1.0 Docs http://web3js.readthedocs.io/en/1.0/index.html // don't confuse with v0.2's which is what most search results give yo +// Ether node for our web3 to connect to +const node = ethNodes[process.argv[3]] || ethNodes.myEtherWallet +let web3 = new Web3(new Web3.providers.HttpProvider(node)) // Run script that was passed as commandLine parameter if (!process.argv[2]) throw 'Must pass name of script as argument' diff --git a/scripts.js b/scripts.js index 9e88418..4a30aaf 100644 --- a/scripts.js +++ b/scripts.js @@ -1,10 +1,9 @@ const TX = require('ethereumjs-tx') +// Web3 1.0 Docs http://web3js.readthedocs.io/en/1.0/index.html // don't confuse with v0.2's which is what most search results give yo // https://github.com/ethereumjs/ethereumjs-tx/blob/master/examples/transactions.js // https://github.com/SilentCicero/ethereumjs-accounts // sendTransaction vs sendRawTransaction // estimateGas() - -// * // https://tokenmarket.net/blog/creating-offline-ethereum-transactions-in-javascript/ const address = process.env.address @@ -12,45 +11,30 @@ const eosContractAddress = '0xd0a6E6C54DbC68Db5db3A091B171A77407Ff7ccf' const eosContractABI = [{"constant":true,"inputs":[{"name":"","type":"uint256"},{"name":"","type":"address"}],"name":"claimed","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"owner_","type":"address"}],"name":"setOwner","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"time","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint128"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"foundersAllocation","outputs":[{"name":"","type":"uint128"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"day","type":"uint256"}],"name":"claim","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"foundersKey","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"},{"name":"","type":"address"}],"name":"userBuys","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"day","type":"uint256"}],"name":"createOnDay","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"freeze","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"keys","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"startTime","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"authority_","type":"address"}],"name":"setAuthority","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"dailyTotals","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"buy","outputs":[],"payable":true,"type":"function"},{"constant":true,"inputs":[],"name":"openTime","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"EOS","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"today","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"authority","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"eos","type":"address"}],"name":"initialize","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"createFirstDay","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"claimAll","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"timestamp","type":"uint256"}],"name":"dayFor","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"day","type":"uint256"},{"name":"limit","type":"uint256"}],"name":"buyWithLimit","outputs":[],"payable":true,"type":"function"},{"constant":false,"inputs":[],"name":"collect","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"numberOfDays","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"key","type":"string"}],"name":"register","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"createPerDay","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"inputs":[{"name":"_numberOfDays","type":"uint256"},{"name":"_totalSupply","type":"uint128"},{"name":"_openTime","type":"uint256"},{"name":"_startTime","type":"uint256"},{"name":"_foundersAllocation","type":"uint128"},{"name":"_foundersKey","type":"string"}],"payable":false,"type":"constructor"},{"payable":true,"type":"fallback"},{"anonymous":false,"inputs":[{"indexed":false,"name":"window","type":"uint256"},{"indexed":false,"name":"user","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"LogBuy","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"window","type":"uint256"},{"indexed":false,"name":"user","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"LogClaim","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"user","type":"address"},{"indexed":false,"name":"key","type":"string"}],"name":"LogRegister","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"amount","type":"uint256"}],"name":"LogCollect","type":"event"},{"anonymous":false,"inputs":[],"name":"LogFreeze","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"authority","type":"address"}],"name":"LogSetAuthority","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"}],"name":"LogSetOwner","type":"event"}] // should NOT be a string or will throw error in web3js/contracts.js const gasLimit = 90000 -const input = fs.readFileSync('Token.sol'); -const output = solc.compile(input.toString(), 1); -const bytecode = output.contracts['Token'].bytecode; - -var createByteCodeForContract = function (contract) { - const input = fs.readFileSync(`${contract}.sol`); // .sol files - put all 3 at same level, of directory - const output = solc.compile(input.toString(), 1); - const bytecode = output.contracts['Token'].bytecode; - return bytcode -} - -var createTxObject = function (eosContractAddress, value) { - const gasLimitHex = web3.toHex(300000); - console.log('gasLimitHex', gasLimitHex) - - const rawTx = { - nonce: web3.eth.getTransactionCount(process.env.address), // getTransactionCountAsync - - gasPrice : web3.eth.gasPrice(), // need hex value - // gasPrice: web3.eth.gasPrice(function (err, result) { - // console.log('result', result) // gwei - // const gasPriceHex = web3.toHex(result); // 0x09184e72a000 - // }), - // The gas price is determined by the x latest blocks median gas price. - - gasLimit: '0x2710', - - gas: null, // - based on the data/bytcode size https://github.com/ethereum/wiki/wiki/JavaScript-API#web3ethsendrawtransaction - - to: eosContractAddress, - value: '0x00', // GOOD try web3.toHex(web3.toWei(value, "ether")); - value comes from process.argv[] vs geth console might allow amount = web3.toWei(0.01, "ether") - // https://www.reddit.com/r/ethdev/comments/71rhzs/05_eth_bounty_help_needed_for_sending_raw/ - } - - console.log('\n rawTx \n', rawTx) - return new TX(rawTx) // Transaction: { raw: [ ], _fields: ['nonce',]} +var createTxObject = function (eosContractAddress, value, web3) { + const gasLimitHex = web3.utils.toHex(300000); // 0x493e0 // what # to use as input? + + // Prepare a Raw Transaction object + let gasPriceHex + return web3.eth.getGasPrice() + .then(gasPrice => { + gasPriceHex = web3.utils.toHex(gasPrice) // 10000000000 + return web3.eth.getTransactionCount(process.env.address) // getTransactionCountAsync + }) + .then(transactionCount => { + const rawTx = { + nonce: transactionCount, + gasPrice : gasPriceHex, // The gas price is determined by the x latest blocks median gas price. + gasLimit: gasLimitHex, //'0x2710' + gas: null, // - based on the data/bytcode size https://github.com/ethereum/wiki/wiki/JavaScript-API#web3ethsendrawtransaction + to: eosContractAddress, + value: '0x00', // GOOD try web3.toHex(web3.toWei(value, "ether")); - value comes from process.argv[] vs geth console might allow amount = web3.toWei(0.01, "ether") + // https://www.reddit.com/r/ethdev/comments/71rhzs/05_eth_bounty_help_needed_for_sending_raw/ + } + console.log('\nrawTx \n', rawTx) + return new TX(rawTx) // Transaction: { raw: [ ], _fields: ['nonce',]} + }) } -// chainId: 1, - might be a default, EIP 155 chainId - mainnet: 1, ropsten: 3 -// gas: feeCost, - seems like mostly gasLimit and gasPrice var signTxByPrivateKey = function (tx, pKey) { //var feeCost = tx.getUpfrontCost() @@ -62,19 +46,26 @@ var signTxByPrivateKey = function (tx, pKey) { // console.log('serializedTx.toString(hex)\n', serializedTx.toString('hex')) // f889808609184e72a00082271094d0a6e6c54dbc68d } -module.exports = { - +module.exports = { // STEP 1 CREATE TRANSACTION FOR SENDING ETHER FROM ACCOUNT ADDRESS TO EOS CONTRACT ADDRESS sendEth2EosContract: async function (web3) { - const tx = createTxObject(eosContractAddress, 10000000) - console.log('\n TX \n', tx) - const txSerialized = signTransactionByPrivateKey(tx, process.env.PK) // makes it so funds are coming from the account address for that private key? + return createTxObject(eosContractAddress, 10000000, web3) + .then(txInstance => { + const tx = txInstance + // console.log('\ntx instance of ethereumjs-tx \n', tx) //Transaction, raw, _fields, methods + + const txSerialized = signTxByPrivateKey(tx, process.env.PK) // makes it so funds are coming from the account address for that private key? + return + return web3.eth.sendRawTransaction('0x' + txSerialized.toString('hex'), function(err, hash) { if (!err) { + console.log('hash', hash); // "0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385" + } else { console.log(' err sendRawTransaction \n', err)} + }); + }) + + + + - return - return web3.eth.sendRawTransaction('0x' + txSerialized.toString('hex'), function(err, hash) { if (!err) { - console.log('hash', hash); // "0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385" - } else { console.log(' err sendRawTransaction \n', err)} - }); }, // 24Hr window closes... From f111524c2010985833e26499fb2e591d747d1ed2 Mon Sep 17 00:00:00 2001 From: thinkocapo Date: Mon, 5 Mar 2018 01:53:53 -0500 Subject: [PATCH 12/27] code quality --- index.js | 4 +- scripts.js | 121 ++++++++++++++++++++++++++++------------------------- 2 files changed, 67 insertions(+), 58 deletions(-) diff --git a/index.js b/index.js index 5505df9..5917ef7 100644 --- a/index.js +++ b/index.js @@ -5,13 +5,13 @@ const Web3 = require('web3') // WEB3 CONFIG - Web3 1.0 Docs http://web3js.readthedocs.io/en/1.0/index.html // don't confuse with v0.2's which is what most search results give yo // Ether node for our web3 to connect to -const node = ethNodes[process.argv[3]] || ethNodes.myEtherWallet +const node = ethNodes[process.argv[4]] || ethNodes.myEtherWallet let web3 = new Web3(new Web3.providers.HttpProvider(node)) // Run script that was passed as commandLine parameter if (!process.argv[2]) throw 'Must pass name of script as argument' const scriptName = process.argv[2] -scripts[scriptName](web3) +scripts[scriptName](web3, 10000000) // process.argv[3]) // NOTES and alternative code diff --git a/scripts.js b/scripts.js index 4a30aaf..5b7e976 100644 --- a/scripts.js +++ b/scripts.js @@ -1,84 +1,50 @@ const TX = require('ethereumjs-tx') +const Web3 = require('web3') + // Web3 1.0 Docs http://web3js.readthedocs.io/en/1.0/index.html // don't confuse with v0.2's which is what most search results give yo // https://github.com/ethereumjs/ethereumjs-tx/blob/master/examples/transactions.js // https://github.com/SilentCicero/ethereumjs-accounts -// sendTransaction vs sendRawTransaction -// estimateGas() +// sendTransaction vs sendRawTransaction , estimateGas() // https://tokenmarket.net/blog/creating-offline-ethereum-transactions-in-javascript/ +/** + * Raw Tx from MEW at 1:19a on 03/05/18 + * Most values are hex's of the actual value + * + * {"nonce":"0x10", // # historical transactions by sender address + * "gasPrice":"0x04e3b29200", // determined by the x latest blocks median gas price. + * "gasLimit":"0x5208", // # like 20000000 21000 + * "to":"0x1eec5a83f78d3952fe86747034a7514f2dc9925c", // address of recipient + * "value":"0x2386f26fc10000", // wei + * "data":"", + * "chainId":1} + */ -const address = process.env.address +const { address, privateKey } = process.env const eosContractAddress = '0xd0a6E6C54DbC68Db5db3A091B171A77407Ff7ccf' const eosContractABI = [{"constant":true,"inputs":[{"name":"","type":"uint256"},{"name":"","type":"address"}],"name":"claimed","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"owner_","type":"address"}],"name":"setOwner","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"time","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint128"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"foundersAllocation","outputs":[{"name":"","type":"uint128"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"day","type":"uint256"}],"name":"claim","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"foundersKey","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"},{"name":"","type":"address"}],"name":"userBuys","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"day","type":"uint256"}],"name":"createOnDay","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"freeze","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"keys","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"startTime","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"authority_","type":"address"}],"name":"setAuthority","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"dailyTotals","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"buy","outputs":[],"payable":true,"type":"function"},{"constant":true,"inputs":[],"name":"openTime","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"EOS","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"today","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"authority","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"eos","type":"address"}],"name":"initialize","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"createFirstDay","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"claimAll","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"timestamp","type":"uint256"}],"name":"dayFor","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"day","type":"uint256"},{"name":"limit","type":"uint256"}],"name":"buyWithLimit","outputs":[],"payable":true,"type":"function"},{"constant":false,"inputs":[],"name":"collect","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"numberOfDays","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"key","type":"string"}],"name":"register","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"createPerDay","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"inputs":[{"name":"_numberOfDays","type":"uint256"},{"name":"_totalSupply","type":"uint128"},{"name":"_openTime","type":"uint256"},{"name":"_startTime","type":"uint256"},{"name":"_foundersAllocation","type":"uint128"},{"name":"_foundersKey","type":"string"}],"payable":false,"type":"constructor"},{"payable":true,"type":"fallback"},{"anonymous":false,"inputs":[{"indexed":false,"name":"window","type":"uint256"},{"indexed":false,"name":"user","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"LogBuy","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"window","type":"uint256"},{"indexed":false,"name":"user","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"LogClaim","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"user","type":"address"},{"indexed":false,"name":"key","type":"string"}],"name":"LogRegister","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"amount","type":"uint256"}],"name":"LogCollect","type":"event"},{"anonymous":false,"inputs":[],"name":"LogFreeze","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"authority","type":"address"}],"name":"LogSetAuthority","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"}],"name":"LogSetOwner","type":"event"}] // should NOT be a string or will throw error in web3js/contracts.js const gasLimit = 90000 - -var createTxObject = function (eosContractAddress, value, web3) { - const gasLimitHex = web3.utils.toHex(300000); // 0x493e0 // what # to use as input? - - // Prepare a Raw Transaction object - let gasPriceHex - return web3.eth.getGasPrice() - .then(gasPrice => { - gasPriceHex = web3.utils.toHex(gasPrice) // 10000000000 - return web3.eth.getTransactionCount(process.env.address) // getTransactionCountAsync - }) - .then(transactionCount => { - const rawTx = { - nonce: transactionCount, - gasPrice : gasPriceHex, // The gas price is determined by the x latest blocks median gas price. - gasLimit: gasLimitHex, //'0x2710' - gas: null, // - based on the data/bytcode size https://github.com/ethereum/wiki/wiki/JavaScript-API#web3ethsendrawtransaction - to: eosContractAddress, - value: '0x00', // GOOD try web3.toHex(web3.toWei(value, "ether")); - value comes from process.argv[] vs geth console might allow amount = web3.toWei(0.01, "ether") - // https://www.reddit.com/r/ethdev/comments/71rhzs/05_eth_bounty_help_needed_for_sending_raw/ - } - console.log('\nrawTx \n', rawTx) - return new TX(rawTx) // Transaction: { raw: [ ], _fields: ['nonce',]} - }) -} - -var signTxByPrivateKey = function (tx, pKey) { - //var feeCost = tx.getUpfrontCost() - //console.log('feeCost', feeCost.toString()) // ? - const privateKey = Buffer.from(pKey, 'hex') // toString() - tx.sign(privateKey) // or web3.eth.sign(address, dataToSign, [, callback]) but account would need to be unlocked https://github.com/ethereum/wiki/wiki/JavaScript-API#web3ethsendrawtransaction - const serializedTx = tx.serialize() - return serializedTx // - // console.log('serializedTx.toString(hex)\n', serializedTx.toString('hex')) // f889808609184e72a00082271094d0a6e6c54dbc68d -} +let webThree = new Web3 +console.log('address', address) module.exports = { - // STEP 1 CREATE TRANSACTION FOR SENDING ETHER FROM ACCOUNT ADDRESS TO EOS CONTRACT ADDRESS - sendEth2EosContract: async function (web3) { - return createTxObject(eosContractAddress, 10000000, web3) + + sendEth2EosContract: async function (web3, value) { + return createRawTx(eosContractAddress, value, web3) .then(txInstance => { const tx = txInstance - // console.log('\ntx instance of ethereumjs-tx \n', tx) //Transaction, raw, _fields, methods - - const txSerialized = signTxByPrivateKey(tx, process.env.PK) // makes it so funds are coming from the account address for that private key? + const txSerialized = createSerializedSignedTx(tx, privateKey) // { + gasPrice = result + return web3.eth.getTransactionCount(process.env.address) // getTransactionCountAsync + }) + .then(transactionCount => { + console.log('transactionCount', transactionCount) + const rawTx = { + nonce: hex(transactionCount), + gasPrice : hex(gasPrice), + gasLimit: hex(300000), + to: eosContractAddress, + value: hex(value), + "data":"", + "chainId":1 + } + // console.log('\nrawTx\n', rawTx) + const tx = new TX(rawTx) + console.log('\ntx\n', tx) + return tx // // Transaction: { raw: [ ], _fields: ['nonce',]} + }) +} + +var createSerializedSignedTx = function (tx, pKey) { + const privateKey = Buffer.from(pKey, 'hex') // toString() + tx.sign(privateKey) // or web3.eth.sign(address, dataToSign, [, callback]) but account would need to be unlocked https://github.com/ethereum/wiki/wiki/JavaScript-API#web3ethsendrawtransaction + const txSerialized = tx.serialize() + // console.log('txSerialized.toString(hex)\n', txSerialized.toString('hex')) // f889808609184e72a00082271094d0a6e6c54dbc68d + console.log('txSerialized\n', txSerialized) + return txSerialized // +} + + // TODO - make a single method that will first check if their are new EOS tokens to claim, and then run claimAll and exchange workflow + // and if no new EOS token available, then send eth to EOS Crowdsale + +var hex = function (gasPrice) { + return webThree.utils.toHex(gasPrice) +} \ No newline at end of file From 27b67f8c4ccace7f43c5da57c2208c4a3c9b44e1 Mon Sep 17 00:00:00 2001 From: thinkocapo Date: Mon, 5 Mar 2018 02:11:44 -0500 Subject: [PATCH 13/27] updates --- scripts.js | 56 ++++++++++++++++++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 23 deletions(-) diff --git a/scripts.js b/scripts.js index 5b7e976..9b24a93 100644 --- a/scripts.js +++ b/scripts.js @@ -12,19 +12,18 @@ const Web3 = require('web3') * * {"nonce":"0x10", // # historical transactions by sender address * "gasPrice":"0x04e3b29200", // determined by the x latest blocks median gas price. - * "gasLimit":"0x5208", // # like 20000000 21000 + * "gasLimit":"0x5208", // # formerly 21000 on MEW or 300000 here * "to":"0x1eec5a83f78d3952fe86747034a7514f2dc9925c", // address of recipient * "value":"0x2386f26fc10000", // wei - * "data":"", + * "data":"", // only for deploying smart contract * "chainId":1} */ -const { address, privateKey } = process.env +const { address, privateKey, chain } = process.env const eosContractAddress = '0xd0a6E6C54DbC68Db5db3A091B171A77407Ff7ccf' const eosContractABI = [{"constant":true,"inputs":[{"name":"","type":"uint256"},{"name":"","type":"address"}],"name":"claimed","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"owner_","type":"address"}],"name":"setOwner","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"time","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint128"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"foundersAllocation","outputs":[{"name":"","type":"uint128"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"day","type":"uint256"}],"name":"claim","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"foundersKey","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"},{"name":"","type":"address"}],"name":"userBuys","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"day","type":"uint256"}],"name":"createOnDay","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"freeze","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"keys","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"startTime","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"authority_","type":"address"}],"name":"setAuthority","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"dailyTotals","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"buy","outputs":[],"payable":true,"type":"function"},{"constant":true,"inputs":[],"name":"openTime","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"EOS","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"today","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"authority","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"eos","type":"address"}],"name":"initialize","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"createFirstDay","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"claimAll","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"timestamp","type":"uint256"}],"name":"dayFor","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"day","type":"uint256"},{"name":"limit","type":"uint256"}],"name":"buyWithLimit","outputs":[],"payable":true,"type":"function"},{"constant":false,"inputs":[],"name":"collect","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"numberOfDays","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"key","type":"string"}],"name":"register","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"createPerDay","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"inputs":[{"name":"_numberOfDays","type":"uint256"},{"name":"_totalSupply","type":"uint128"},{"name":"_openTime","type":"uint256"},{"name":"_startTime","type":"uint256"},{"name":"_foundersAllocation","type":"uint128"},{"name":"_foundersKey","type":"string"}],"payable":false,"type":"constructor"},{"payable":true,"type":"fallback"},{"anonymous":false,"inputs":[{"indexed":false,"name":"window","type":"uint256"},{"indexed":false,"name":"user","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"LogBuy","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"window","type":"uint256"},{"indexed":false,"name":"user","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"LogClaim","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"user","type":"address"},{"indexed":false,"name":"key","type":"string"}],"name":"LogRegister","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"amount","type":"uint256"}],"name":"LogCollect","type":"event"},{"anonymous":false,"inputs":[],"name":"LogFreeze","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"authority","type":"address"}],"name":"LogSetAuthority","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"}],"name":"LogSetOwner","type":"event"}] // should NOT be a string or will throw error in web3js/contracts.js const gasLimit = 90000 let webThree = new Web3 -console.log('address', address) module.exports = { @@ -32,11 +31,11 @@ module.exports = { return createRawTx(eosContractAddress, value, web3) .then(txInstance => { const tx = txInstance - const txSerialized = createSerializedSignedTx(tx, privateKey) // { gasPrice = result return web3.eth.getTransactionCount(process.env.address) // getTransactionCountAsync }) - .then(transactionCount => { - console.log('transactionCount', transactionCount) + .then(result => { + txCount = result + + return web3.eth.getBlock("latest") + }) + .then(block => { + gasLimit = block.gasLimit + + console.log({ + nonce: txCount, + gasPrice: gasPrice, + gasLimit: gasLimit, + to: eosContractAddress, + value: value, + "data":"", + "chainId": chain + }) const rawTx = { - nonce: hex(transactionCount), + nonce: hex(txCount), gasPrice : hex(gasPrice), gasLimit: hex(300000), to: eosContractAddress, value: hex(value), "data":"", - "chainId":1 + "chainId": chain } - // console.log('\nrawTx\n', rawTx) + console.log('\nrawTx\n', rawTx) const tx = new TX(rawTx) - console.log('\ntx\n', tx) + // console.log('\ntx\n', tx) return tx // // Transaction: { raw: [ ], _fields: ['nonce',]} }) } @@ -85,14 +97,12 @@ var createSerializedSignedTx = function (tx, pKey) { const privateKey = Buffer.from(pKey, 'hex') // toString() tx.sign(privateKey) // or web3.eth.sign(address, dataToSign, [, callback]) but account would need to be unlocked https://github.com/ethereum/wiki/wiki/JavaScript-API#web3ethsendrawtransaction const txSerialized = tx.serialize() - // console.log('txSerialized.toString(hex)\n', txSerialized.toString('hex')) // f889808609184e72a00082271094d0a6e6c54dbc68d - console.log('txSerialized\n', txSerialized) - return txSerialized // + console.log('txSerialized\n', txSerialized) // + return txSerialized } - // TODO - make a single method that will first check if their are new EOS tokens to claim, and then run claimAll and exchange workflow - // and if no new EOS token available, then send eth to EOS Crowdsale - var hex = function (gasPrice) { return webThree.utils.toHex(gasPrice) -} \ No newline at end of file +} + // TODO - make a single method that will first check if their are new EOS tokens to claim, and then run claimAll and exchange workflow + // and if no new EOS token available, then send eth to EOS Crowdsale From 37b944bfa85a34dd11d55c32a79739e88be024fb Mon Sep 17 00:00:00 2001 From: thinkocapo Date: Mon, 5 Mar 2018 03:34:13 -0500 Subject: [PATCH 14/27] raw tx rdy for prod use --- index.js | 2 +- scripts.js | 32 +++++++++++++++++--------------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/index.js b/index.js index 5917ef7..e466257 100644 --- a/index.js +++ b/index.js @@ -11,7 +11,7 @@ let web3 = new Web3(new Web3.providers.HttpProvider(node)) // Run script that was passed as commandLine parameter if (!process.argv[2]) throw 'Must pass name of script as argument' const scriptName = process.argv[2] -scripts[scriptName](web3, 10000000) // process.argv[3]) +scripts[scriptName](web3, 0.003) // process.argv[3]) // NOTES and alternative code diff --git a/scripts.js b/scripts.js index 9b24a93..b44baf4 100644 --- a/scripts.js +++ b/scripts.js @@ -1,20 +1,20 @@ -const TX = require('ethereumjs-tx') +const EthTx = require('ethereumjs-tx') const Web3 = require('web3') // Web3 1.0 Docs http://web3js.readthedocs.io/en/1.0/index.html // don't confuse with v0.2's which is what most search results give yo // https://github.com/ethereumjs/ethereumjs-tx/blob/master/examples/transactions.js // https://github.com/SilentCicero/ethereumjs-accounts // sendTransaction vs sendRawTransaction , estimateGas() -// https://tokenmarket.net/blog/creating-offline-ethereum-transactions-in-javascript/ + /** * Raw Tx from MEW at 1:19a on 03/05/18 * Most values are hex's of the actual value * * {"nonce":"0x10", // # historical transactions by sender address - * "gasPrice":"0x04e3b29200", // determined by the x latest blocks median gas price. + * "gasPrice":"0x04e3b29200", // determined by the x latest blocks median gas price. 20gwei or 20000000000 // recently 9 000 000 000 * "gasLimit":"0x5208", // # formerly 21000 on MEW or 300000 here * "to":"0x1eec5a83f78d3952fe86747034a7514f2dc9925c", // address of recipient - * "value":"0x2386f26fc10000", // wei + * "value":"0x2386f26fc10000", // wei web3.toHex(web3.toWei(value,'ether')) * "data":"", // only for deploying smart contract * "chainId":1} */ @@ -22,19 +22,21 @@ const Web3 = require('web3') const { address, privateKey, chain } = process.env const eosContractAddress = '0xd0a6E6C54DbC68Db5db3A091B171A77407Ff7ccf' const eosContractABI = [{"constant":true,"inputs":[{"name":"","type":"uint256"},{"name":"","type":"address"}],"name":"claimed","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"owner_","type":"address"}],"name":"setOwner","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"time","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint128"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"foundersAllocation","outputs":[{"name":"","type":"uint128"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"day","type":"uint256"}],"name":"claim","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"foundersKey","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"},{"name":"","type":"address"}],"name":"userBuys","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"day","type":"uint256"}],"name":"createOnDay","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"freeze","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"keys","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"startTime","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"authority_","type":"address"}],"name":"setAuthority","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"dailyTotals","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"buy","outputs":[],"payable":true,"type":"function"},{"constant":true,"inputs":[],"name":"openTime","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"EOS","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"today","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"authority","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"eos","type":"address"}],"name":"initialize","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"createFirstDay","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"claimAll","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"timestamp","type":"uint256"}],"name":"dayFor","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"day","type":"uint256"},{"name":"limit","type":"uint256"}],"name":"buyWithLimit","outputs":[],"payable":true,"type":"function"},{"constant":false,"inputs":[],"name":"collect","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"numberOfDays","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"key","type":"string"}],"name":"register","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"createPerDay","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"inputs":[{"name":"_numberOfDays","type":"uint256"},{"name":"_totalSupply","type":"uint128"},{"name":"_openTime","type":"uint256"},{"name":"_startTime","type":"uint256"},{"name":"_foundersAllocation","type":"uint128"},{"name":"_foundersKey","type":"string"}],"payable":false,"type":"constructor"},{"payable":true,"type":"fallback"},{"anonymous":false,"inputs":[{"indexed":false,"name":"window","type":"uint256"},{"indexed":false,"name":"user","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"LogBuy","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"window","type":"uint256"},{"indexed":false,"name":"user","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"LogClaim","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"user","type":"address"},{"indexed":false,"name":"key","type":"string"}],"name":"LogRegister","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"amount","type":"uint256"}],"name":"LogCollect","type":"event"},{"anonymous":false,"inputs":[],"name":"LogFreeze","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"authority","type":"address"}],"name":"LogSetAuthority","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"}],"name":"LogSetOwner","type":"event"}] // should NOT be a string or will throw error in web3js/contracts.js -const gasLimit = 90000 +const gasLimit = 21000 let webThree = new Web3 -module.exports = { - +module.exports = { + sendEth2EosContract: async function (web3, value) { return createRawTx(eosContractAddress, value, web3) .then(txInstance => { const tx = txInstance const txSerialized = createSerializedSignedTx(tx, privateKey) + const txSerializedHex = txSerialized.toString('hex') return - return web3.eth.sendRawTransaction('0x' + txSerialized.toString('hex'), function(err, hash) { if (!err) { - console.log('hash', hash); // "0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385" + return web3.eth.sendRawTransaction(`0x${txSerializedHex}`, function(err, txHash) { if (!err) { + console.log('txHash', txHash); // "0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385" + // web3.eth.getTransaction(hash) to verify } else { console.log('err sendRawTransaction \n', err)} }); }) @@ -62,12 +64,10 @@ var createRawTx = function (eosContractAddress, value, web3) { }) .then(result => { txCount = result - return web3.eth.getBlock("latest") }) .then(block => { gasLimit = block.gasLimit - console.log({ nonce: txCount, gasPrice: gasPrice, @@ -77,25 +77,27 @@ var createRawTx = function (eosContractAddress, value, web3) { "data":"", "chainId": chain }) + + console.log('value', value) const rawTx = { nonce: hex(txCount), gasPrice : hex(gasPrice), gasLimit: hex(300000), to: eosContractAddress, - value: hex(value), + value: hex(web3.utils.toWei(value.toString(),'ether')), // *TODO* hard-code the Wei the first time // make sure toString works here "data":"", "chainId": chain } console.log('\nrawTx\n', rawTx) - const tx = new TX(rawTx) + const tx = new EthTx(rawTx) // console.log('\ntx\n', tx) return tx // // Transaction: { raw: [ ], _fields: ['nonce',]} }) } var createSerializedSignedTx = function (tx, pKey) { - const privateKey = Buffer.from(pKey, 'hex') // toString() - tx.sign(privateKey) // or web3.eth.sign(address, dataToSign, [, callback]) but account would need to be unlocked https://github.com/ethereum/wiki/wiki/JavaScript-API#web3ethsendrawtransaction + const privateKeyX = Buffer.from(pKey, 'hex') // toString() // new Buffer(pKey, 'hex') + tx.sign(privateKeyX) const txSerialized = tx.serialize() console.log('txSerialized\n', txSerialized) // return txSerialized From f97d426b0c36b8c253bb2346b2b57a6e7ce4d62f Mon Sep 17 00:00:00 2001 From: thinkocapo Date: Mon, 5 Mar 2018 13:40:19 -0500 Subject: [PATCH 15/27] prepping --- eth-nodes.js | 1 + scripts.js | 11 ++++++----- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/eth-nodes.js b/eth-nodes.js index 28d0e5f..b53fafc 100644 --- a/eth-nodes.js +++ b/eth-nodes.js @@ -1,4 +1,5 @@ module.exports = { + blockCypher: 'https://www.blockcypher.com/dev/ethereum/#get-contract-address-endpoint', etherScan: '', gethLocal: 'http://localhost:8545', infura: 'https://ethereum.stackexchange.com/questions/36358/how-to-properly-create-a-raw-transaction-and-sign-it-using-web3-in-browser?rq=1', // privateKey to infura diff --git a/scripts.js b/scripts.js index b44baf4..fde4e62 100644 --- a/scripts.js +++ b/scripts.js @@ -6,13 +6,15 @@ const Web3 = require('web3') // https://github.com/SilentCicero/ethereumjs-accounts // sendTransaction vs sendRawTransaction , estimateGas() +// ethstats.net for gasprice ratio eth/gas +// eth3r.co for turning pKey into address /** * Raw Tx from MEW at 1:19a on 03/05/18 * Most values are hex's of the actual value * * {"nonce":"0x10", // # historical transactions by sender address * "gasPrice":"0x04e3b29200", // determined by the x latest blocks median gas price. 20gwei or 20000000000 // recently 9 000 000 000 - * "gasLimit":"0x5208", // # formerly 21000 on MEW or 300000 here + * "gasLimit":"0x5208", // # formerly 21000 to send on MEW or 300000 here // "amount of gas you pay is fixed, but the quantity of ethere it costs for that is not fixed (it varies)" * "to":"0x1eec5a83f78d3952fe86747034a7514f2dc9925c", // address of recipient * "value":"0x2386f26fc10000", // wei web3.toHex(web3.toWei(value,'ether')) * "data":"", // only for deploying smart contract @@ -26,7 +28,7 @@ const gasLimit = 21000 let webThree = new Web3 module.exports = { - + sendEth2EosContract: async function (web3, value) { return createRawTx(eosContractAddress, value, web3) .then(txInstance => { @@ -82,15 +84,14 @@ var createRawTx = function (eosContractAddress, value, web3) { const rawTx = { nonce: hex(txCount), gasPrice : hex(gasPrice), - gasLimit: hex(300000), + gasLimit: hex(gasLimit), to: eosContractAddress, value: hex(web3.utils.toWei(value.toString(),'ether')), // *TODO* hard-code the Wei the first time // make sure toString works here "data":"", "chainId": chain } console.log('\nrawTx\n', rawTx) - const tx = new EthTx(rawTx) - // console.log('\ntx\n', tx) + const tx = new EthTx(rawTx) // console.log('\ntx\n', tx) return tx // // Transaction: { raw: [ ], _fields: ['nonce',]} }) } From 2aa2e0de3986ff213db4d3693a301ac08f6a86bd Mon Sep 17 00:00:00 2001 From: thinkocapo Date: Mon, 5 Mar 2018 13:40:50 -0500 Subject: [PATCH 16/27] update --- scripts.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/scripts.js b/scripts.js index fde4e62..91429d3 100644 --- a/scripts.js +++ b/scripts.js @@ -8,6 +8,8 @@ const Web3 = require('web3') // ethstats.net for gasprice ratio eth/gas // eth3r.co for turning pKey into address +// etherutils +// ethereumjs/keythereum /** * Raw Tx from MEW at 1:19a on 03/05/18 * Most values are hex's of the actual value From 46568f51656bd48427040382721478210faa2a98 Mon Sep 17 00:00:00 2001 From: thinkocapo Date: Wed, 7 Mar 2018 12:15:20 -0500 Subject: [PATCH 17/27] log --- scripts.js | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/scripts.js b/scripts.js index 91429d3..66814c4 100644 --- a/scripts.js +++ b/scripts.js @@ -39,7 +39,7 @@ module.exports = { const txSerializedHex = txSerialized.toString('hex') return return web3.eth.sendRawTransaction(`0x${txSerializedHex}`, function(err, txHash) { if (!err) { - console.log('txHash', txHash); // "0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385" + console.log('==== transaction hash ==== ', txHash); // "0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385" // web3.eth.getTransaction(hash) to verify } else { console.log('err sendRawTransaction \n', err)} }); @@ -48,7 +48,6 @@ module.exports = { claimEos: function (web3) { const eosContractInstance = new web3.eth.Contract(eosContractABI, eosContractAddress); - // console.log('\n === eosContractInstance.methods.claimAll === \n', eosContractInstance.methods); // 'function () { [native code] }' }, sendEosToExchange: function () { // exchange api - send eos to exchange @@ -72,27 +71,18 @@ var createRawTx = function (eosContractAddress, value, web3) { }) .then(block => { gasLimit = block.gasLimit - console.log({ - nonce: txCount, - gasPrice: gasPrice, - gasLimit: gasLimit, - to: eosContractAddress, - value: value, - "data":"", - "chainId": chain - }) - console.log('value', value) + logRawTxInputs({txCount,gasPrice,gasLimit,eosContractAddress,value,chain}) const rawTx = { nonce: hex(txCount), gasPrice : hex(gasPrice), gasLimit: hex(gasLimit), to: eosContractAddress, value: hex(web3.utils.toWei(value.toString(),'ether')), // *TODO* hard-code the Wei the first time // make sure toString works here - "data":"", - "chainId": chain + "chainId": chain, + "data":"" } - console.log('\nrawTx\n', rawTx) + console.log('====== Raw Transaction =======\n', rawTx) const tx = new EthTx(rawTx) // console.log('\ntx\n', tx) return tx // // Transaction: { raw: [ ], _fields: ['nonce',]} }) @@ -102,12 +92,14 @@ var createSerializedSignedTx = function (tx, pKey) { const privateKeyX = Buffer.from(pKey, 'hex') // toString() // new Buffer(pKey, 'hex') tx.sign(privateKeyX) const txSerialized = tx.serialize() - console.log('txSerialized\n', txSerialized) // + //console.log('txSerialized\n', txSerialized) // return txSerialized } var hex = function (gasPrice) { return webThree.utils.toHex(gasPrice) } - // TODO - make a single method that will first check if their are new EOS tokens to claim, and then run claimAll and exchange workflow - // and if no new EOS token available, then send eth to EOS Crowdsale +// TODO - make a single method that will first check if their are new EOS tokens to claim, and then run claimAll and exchange workflow +// and if no new EOS token available, then send eth to EOS Crowdsale + +var logRawTxInputs = function (params) {console.log('===== Raw Transaction Inputs =====\n', params)} \ No newline at end of file From 2a12f2d4fd747eb090b32a499e3538986a7ff058 Mon Sep 17 00:00:00 2001 From: thinkocapo Date: Wed, 7 Mar 2018 12:27:38 -0500 Subject: [PATCH 18/27] update --- scripts.js | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/scripts.js b/scripts.js index 66814c4..aa45dce 100644 --- a/scripts.js +++ b/scripts.js @@ -35,10 +35,13 @@ module.exports = { return createRawTx(eosContractAddress, value, web3) .then(txInstance => { const tx = txInstance - const txSerialized = createSerializedSignedTx(tx, privateKey) - const txSerializedHex = txSerialized.toString('hex') + console.log('======= tx =======', tx) + const txSignedSerialized = createSignedSerializedTx(tx, privateKey) + console.log('======= txSignedSerialized =======', tx) + const txSignedSerializedHex = txSignedSerialized.toString('hex') + console.log('======= txSignedSerializedHx =======', tx) return - return web3.eth.sendRawTransaction(`0x${txSerializedHex}`, function(err, txHash) { if (!err) { + return web3.eth.sendRawTransaction(`0x${txSignedSerializedHex}`, function(err, txHash) { if (!err) { console.log('==== transaction hash ==== ', txHash); // "0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385" // web3.eth.getTransaction(hash) to verify } else { console.log('err sendRawTransaction \n', err)} @@ -60,6 +63,7 @@ module.exports = { var createRawTx = function (eosContractAddress, value, web3) { // Make web3 calls to get data for Raw Transaction object tx let gasPrice, txCount, gasLimit; + // *Implement* Promise.All return web3.eth.getGasPrice() .then(result => { gasPrice = result @@ -71,24 +75,29 @@ var createRawTx = function (eosContractAddress, value, web3) { }) .then(block => { gasLimit = block.gasLimit + const weiCalculated = web3.utils.toWei(value.toString(),'ether') // value 0.003 ether is 3000000000000000 wei + console.log(weiCalculated) + const weiEtherConverter = 3000000000000000 // https://etherconverter.online/ + console.log(weiEtherConverter) - logRawTxInputs({txCount,gasPrice,gasLimit,eosContractAddress,value,chain}) const rawTx = { nonce: hex(txCount), gasPrice : hex(gasPrice), gasLimit: hex(gasLimit), to: eosContractAddress, - value: hex(web3.utils.toWei(value.toString(),'ether')), // *TODO* hard-code the Wei the first time // make sure toString works here - "chainId": chain, - "data":"" + value: hex(weiEtherConverter), // eventually use 'value' by hex(web3.utils.toWei(value.toString(),'ether')) + chainId: chain, + data:"" } + logRawTxInputs({txCount,gasPrice,gasLimit,eosContractAddress,value,chain}) console.log('====== Raw Transaction =======\n', rawTx) - const tx = new EthTx(rawTx) // console.log('\ntx\n', tx) - return tx // // Transaction: { raw: [ ], _fields: ['nonce',]} + + const tx = new EthTx(rawTx) + return tx // Transaction: { raw: [ ], _fields: ['nonce',]} }) } -var createSerializedSignedTx = function (tx, pKey) { +var createSignedSerializedTx = function (tx, pKey) { const privateKeyX = Buffer.from(pKey, 'hex') // toString() // new Buffer(pKey, 'hex') tx.sign(privateKeyX) const txSerialized = tx.serialize() From 5d996534e14389b0e7a511158314fb0085fdd4a5 Mon Sep 17 00:00:00 2001 From: thinkocapo Date: Wed, 7 Mar 2018 13:13:09 -0500 Subject: [PATCH 19/27] derive ether cost from gasPrice, comparisons. gasLimit --- scripts.js | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/scripts.js b/scripts.js index aa45dce..eb89079 100644 --- a/scripts.js +++ b/scripts.js @@ -15,8 +15,8 @@ const Web3 = require('web3') * Most values are hex's of the actual value * * {"nonce":"0x10", // # historical transactions by sender address - * "gasPrice":"0x04e3b29200", // determined by the x latest blocks median gas price. 20gwei or 20000000000 // recently 9 000 000 000 - * "gasLimit":"0x5208", // # formerly 21000 to send on MEW or 300000 here // "amount of gas you pay is fixed, but the quantity of ethere it costs for that is not fixed (it varies)" + * "gasPrice":"0x04e3b29200", // 21000 ? etermined by the x latest blocks median gas price. 20gwei or 20000000000 // recently 9 000 000 000 + * "gasLimit":"0x5208", // # formerly 21000 to send on MEW or 300000 here // "amount of gas you pay is fixed, but the quantity of ethere it costs for that is not fixed (it varies)" // wont necessarily use all of this limit? e.g. if set a super high limit... * "to":"0x1eec5a83f78d3952fe86747034a7514f2dc9925c", // address of recipient * "value":"0x2386f26fc10000", // wei web3.toHex(web3.toWei(value,'ether')) * "data":"", // only for deploying smart contract @@ -37,9 +37,9 @@ module.exports = { const tx = txInstance console.log('======= tx =======', tx) const txSignedSerialized = createSignedSerializedTx(tx, privateKey) - console.log('======= txSignedSerialized =======', tx) + // console.log('======= txSignedSerialized =======', tx) const txSignedSerializedHex = txSignedSerialized.toString('hex') - console.log('======= txSignedSerializedHx =======', tx) + // console.log('======= txSignedSerializedHx =======', tx) return return web3.eth.sendRawTransaction(`0x${txSignedSerializedHex}`, function(err, txHash) { if (!err) { console.log('==== transaction hash ==== ', txHash); // "0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385" @@ -66,7 +66,17 @@ var createRawTx = function (eosContractAddress, value, web3) { // *Implement* Promise.All return web3.eth.getGasPrice() .then(result => { - gasPrice = result + gasPrice = result // 20000000000 + + const s1 = gasPrice * 21000 + const totalEther = web3.utils.fromWei(s1.toString(), "ether") + console.log('totalEther ', totalEther) + + s2 = 20 + s3 = web3.utils.toWei(s2.toString(), "gwei") * 21000 + const totalEtherFromVideo = web3.utils.fromWei(s3.toString(), "ether") + console.log('totalEtherFromVideo', totalEtherFromVideo) + return web3.eth.getTransactionCount(process.env.address) // getTransactionCountAsync }) .then(result => { @@ -75,10 +85,11 @@ var createRawTx = function (eosContractAddress, value, web3) { }) .then(block => { gasLimit = block.gasLimit + const weiCalculated = web3.utils.toWei(value.toString(),'ether') // value 0.003 ether is 3000000000000000 wei - console.log(weiCalculated) + console.log('weiCalculated', weiCalculated) const weiEtherConverter = 3000000000000000 // https://etherconverter.online/ - console.log(weiEtherConverter) + console.log('weiEtherConverter', weiEtherConverter) const rawTx = { nonce: hex(txCount), @@ -89,11 +100,10 @@ var createRawTx = function (eosContractAddress, value, web3) { chainId: chain, data:"" } - logRawTxInputs({txCount,gasPrice,gasLimit,eosContractAddress,value,chain}) - console.log('====== Raw Transaction =======\n', rawTx) - const tx = new EthTx(rawTx) - return tx // Transaction: { raw: [ ], _fields: ['nonce',]} + logRawTxInputsAndHexes({txCount,gasPrice,gasLimit,eosContractAddress,value,chain}, rawTx) + + return new EthTx(rawTx) // Transaction: { raw: [ ], _fields: ['nonce',]} }) } @@ -111,4 +121,7 @@ var hex = function (gasPrice) { // TODO - make a single method that will first check if their are new EOS tokens to claim, and then run claimAll and exchange workflow // and if no new EOS token available, then send eth to EOS Crowdsale -var logRawTxInputs = function (params) {console.log('===== Raw Transaction Inputs =====\n', params)} \ No newline at end of file +var logRawTxInputsAndHexes = function (inputs, rawTx) { + console.log('===== Raw Transaction Inputs =====\n', inputs) + console.log('===== Raw Transaction =======\n', rawTx) +} \ No newline at end of file From ac0a9f42bf3c69c71ff3376d72fb699599fe32e3 Mon Sep 17 00:00:00 2001 From: thinkocapo Date: Wed, 7 Mar 2018 13:19:44 -0500 Subject: [PATCH 20/27] updates --- scripts.js | 41 +++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/scripts.js b/scripts.js index eb89079..56be3d3 100644 --- a/scripts.js +++ b/scripts.js @@ -35,7 +35,7 @@ module.exports = { return createRawTx(eosContractAddress, value, web3) .then(txInstance => { const tx = txInstance - console.log('======= tx =======', tx) + // console.log('======= tx =======', tx) const txSignedSerialized = createSignedSerializedTx(tx, privateKey) // console.log('======= txSignedSerialized =======', tx) const txSignedSerializedHex = txSignedSerialized.toString('hex') @@ -63,19 +63,13 @@ module.exports = { var createRawTx = function (eosContractAddress, value, web3) { // Make web3 calls to get data for Raw Transaction object tx let gasPrice, txCount, gasLimit; - // *Implement* Promise.All + + // *TODO* Implement Promise.All + return web3.eth.getGasPrice() .then(result => { - gasPrice = result // 20000000000 - - const s1 = gasPrice * 21000 - const totalEther = web3.utils.fromWei(s1.toString(), "ether") - console.log('totalEther ', totalEther) - - s2 = 20 - s3 = web3.utils.toWei(s2.toString(), "gwei") * 21000 - const totalEtherFromVideo = web3.utils.fromWei(s3.toString(), "ether") - console.log('totalEtherFromVideo', totalEtherFromVideo) + gasPrice = result // 20000000000 + logGasPriceInEther(gasPrice, web3) return web3.eth.getTransactionCount(process.env.address) // getTransactionCountAsync }) @@ -86,10 +80,8 @@ var createRawTx = function (eosContractAddress, value, web3) { .then(block => { gasLimit = block.gasLimit - const weiCalculated = web3.utils.toWei(value.toString(),'ether') // value 0.003 ether is 3000000000000000 wei - console.log('weiCalculated', weiCalculated) - const weiEtherConverter = 3000000000000000 // https://etherconverter.online/ - console.log('weiEtherConverter', weiEtherConverter) + // Calculate the ether we want to send, into wei + const wei = logWeiAmountBeingSent(value, web3) const rawTx = { nonce: hex(txCount), @@ -121,6 +113,23 @@ var hex = function (gasPrice) { // TODO - make a single method that will first check if their are new EOS tokens to claim, and then run claimAll and exchange workflow // and if no new EOS token available, then send eth to EOS Crowdsale +var logWeiAmountBeingSent = function (value, web3) { + const weiCalculated = web3.utils.toWei(value.toString(),'ether') // value 0.003 ether is 3000000000000000 wei + console.log('weiCalculated', weiCalculated) + const weiEtherConverter = 3000000000000000 // https://etherconverter.online/ .003 ether is 3000000000000000 wei + console.log('weiEtherConverter', weiEtherConverter) + return weiCalculated +} +var logGasPriceInEther = function (gasPrice, web3) { + const s1 = gasPrice * 21000 + const totalEther = web3.utils.fromWei(s1.toString(), "ether") + console.log('totalEther ', totalEther) + s2 = 20 + s3 = web3.utils.toWei(s2.toString(), "gwei") * 21000 + const totalEtherFromVideo = web3.utils.fromWei(s3.toString(), "ether") + console.log('totalEtherFromVideo', totalEtherFromVideo) +} + var logRawTxInputsAndHexes = function (inputs, rawTx) { console.log('===== Raw Transaction Inputs =====\n', inputs) console.log('===== Raw Transaction =======\n', rawTx) From 8de7090d404c68251b3d6b8f9bb829c7384d3563 Mon Sep 17 00:00:00 2001 From: thinkocapo Date: Wed, 7 Mar 2018 14:02:11 -0500 Subject: [PATCH 21/27] official log for sending Transaction raw --- scripts.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/scripts.js b/scripts.js index 56be3d3..ecc3fc1 100644 --- a/scripts.js +++ b/scripts.js @@ -26,7 +26,6 @@ const Web3 = require('web3') const { address, privateKey, chain } = process.env const eosContractAddress = '0xd0a6E6C54DbC68Db5db3A091B171A77407Ff7ccf' const eosContractABI = [{"constant":true,"inputs":[{"name":"","type":"uint256"},{"name":"","type":"address"}],"name":"claimed","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"owner_","type":"address"}],"name":"setOwner","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"time","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint128"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"foundersAllocation","outputs":[{"name":"","type":"uint128"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"day","type":"uint256"}],"name":"claim","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"foundersKey","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"},{"name":"","type":"address"}],"name":"userBuys","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"day","type":"uint256"}],"name":"createOnDay","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"freeze","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"keys","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"startTime","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"authority_","type":"address"}],"name":"setAuthority","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"dailyTotals","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"buy","outputs":[],"payable":true,"type":"function"},{"constant":true,"inputs":[],"name":"openTime","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"EOS","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"today","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"authority","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"eos","type":"address"}],"name":"initialize","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"createFirstDay","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"claimAll","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"timestamp","type":"uint256"}],"name":"dayFor","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"day","type":"uint256"},{"name":"limit","type":"uint256"}],"name":"buyWithLimit","outputs":[],"payable":true,"type":"function"},{"constant":false,"inputs":[],"name":"collect","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"numberOfDays","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"key","type":"string"}],"name":"register","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"createPerDay","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"inputs":[{"name":"_numberOfDays","type":"uint256"},{"name":"_totalSupply","type":"uint128"},{"name":"_openTime","type":"uint256"},{"name":"_startTime","type":"uint256"},{"name":"_foundersAllocation","type":"uint128"},{"name":"_foundersKey","type":"string"}],"payable":false,"type":"constructor"},{"payable":true,"type":"fallback"},{"anonymous":false,"inputs":[{"indexed":false,"name":"window","type":"uint256"},{"indexed":false,"name":"user","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"LogBuy","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"window","type":"uint256"},{"indexed":false,"name":"user","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"LogClaim","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"user","type":"address"},{"indexed":false,"name":"key","type":"string"}],"name":"LogRegister","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"amount","type":"uint256"}],"name":"LogCollect","type":"event"},{"anonymous":false,"inputs":[],"name":"LogFreeze","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"authority","type":"address"}],"name":"LogSetAuthority","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"}],"name":"LogSetOwner","type":"event"}] // should NOT be a string or will throw error in web3js/contracts.js -const gasLimit = 21000 let webThree = new Web3 module.exports = { @@ -41,10 +40,11 @@ module.exports = { const txSignedSerializedHex = txSignedSerialized.toString('hex') // console.log('======= txSignedSerializedHx =======', tx) return - return web3.eth.sendRawTransaction(`0x${txSignedSerializedHex}`, function(err, txHash) { if (!err) { - console.log('==== transaction hash ==== ', txHash); // "0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385" + return web3.eth.sendRawTransaction(`0x${txSignedSerializedHex}`, function(err, txHash) { + if (!err) { + console.log('==== transaction hash ==== \n', JSON.stringify(txHash,null,4)); // "0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385" // web3.eth.getTransaction(hash) to verify - } else { console.log('err sendRawTransaction \n', err)} + } else { console.log('err sendRawTransaction \n', JSON.stringify(err,null,4))} }); }) }, @@ -88,7 +88,7 @@ var createRawTx = function (eosContractAddress, value, web3) { gasPrice : hex(gasPrice), gasLimit: hex(gasLimit), to: eosContractAddress, - value: hex(weiEtherConverter), // eventually use 'value' by hex(web3.utils.toWei(value.toString(),'ether')) + value: hex(wei), // eventually use 'value' by hex(web3.utils.toWei(value.toString(),'ether')) chainId: chain, data:"" } @@ -115,19 +115,19 @@ var hex = function (gasPrice) { var logWeiAmountBeingSent = function (value, web3) { const weiCalculated = web3.utils.toWei(value.toString(),'ether') // value 0.003 ether is 3000000000000000 wei - console.log('weiCalculated', weiCalculated) + console.log('weiCalculated being sent ', weiCalculated) const weiEtherConverter = 3000000000000000 // https://etherconverter.online/ .003 ether is 3000000000000000 wei - console.log('weiEtherConverter', weiEtherConverter) + console.log('weiEtherConverter being sent', weiEtherConverter) return weiCalculated } var logGasPriceInEther = function (gasPrice, web3) { const s1 = gasPrice * 21000 const totalEther = web3.utils.fromWei(s1.toString(), "ether") - console.log('totalEther ', totalEther) + console.log('totalEther for gasPrice ', totalEther) s2 = 20 s3 = web3.utils.toWei(s2.toString(), "gwei") * 21000 const totalEtherFromVideo = web3.utils.fromWei(s3.toString(), "ether") - console.log('totalEtherFromVideo', totalEtherFromVideo) + console.log('totalEtherFromVideo for gasPrice', totalEtherFromVideo) } var logRawTxInputsAndHexes = function (inputs, rawTx) { From f61c0a29e98e5e286c79483532a0a3331e8b99d5 Mon Sep 17 00:00:00 2001 From: thinkocapo Date: Wed, 7 Mar 2018 16:39:24 -0500 Subject: [PATCH 22/27] fix --- scripts.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts.js b/scripts.js index ecc3fc1..970f64e 100644 --- a/scripts.js +++ b/scripts.js @@ -93,7 +93,7 @@ var createRawTx = function (eosContractAddress, value, web3) { data:"" } - logRawTxInputsAndHexes({txCount,gasPrice,gasLimit,eosContractAddress,value,chain}, rawTx) + logRawTxInputsAndHexes({nonce: txCount,gasPrice,gasLimit, to: eosContractAddress,value,chain, data: ""}, rawTx) return new EthTx(rawTx) // Transaction: { raw: [ ], _fields: ['nonce',]} }) From c1294c01708afb48e25f196425200a0921fe50d5 Mon Sep 17 00:00:00 2001 From: thinkocapo Date: Wed, 7 Mar 2018 16:55:54 -0500 Subject: [PATCH 23/27] send to test recipient --- scripts.js | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/scripts.js b/scripts.js index 970f64e..d59d2be 100644 --- a/scripts.js +++ b/scripts.js @@ -18,7 +18,7 @@ const Web3 = require('web3') * "gasPrice":"0x04e3b29200", // 21000 ? etermined by the x latest blocks median gas price. 20gwei or 20000000000 // recently 9 000 000 000 * "gasLimit":"0x5208", // # formerly 21000 to send on MEW or 300000 here // "amount of gas you pay is fixed, but the quantity of ethere it costs for that is not fixed (it varies)" // wont necessarily use all of this limit? e.g. if set a super high limit... * "to":"0x1eec5a83f78d3952fe86747034a7514f2dc9925c", // address of recipient - * "value":"0x2386f26fc10000", // wei web3.toHex(web3.toWei(value,'ether')) + * "value":"0x2386f26fc10000", // eventually // eventually hex(web3.utils.toWei(value.toString(),'ether')) * "data":"", // only for deploying smart contract * "chainId":1} */ @@ -40,6 +40,8 @@ module.exports = { const txSignedSerializedHex = txSignedSerialized.toString('hex') // console.log('======= txSignedSerializedHx =======', tx) return + + // *TODO* send to a Account Address not a Contract Address return web3.eth.sendRawTransaction(`0x${txSignedSerializedHex}`, function(err, txHash) { if (!err) { console.log('==== transaction hash ==== \n', JSON.stringify(txHash,null,4)); // "0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385" @@ -80,20 +82,21 @@ var createRawTx = function (eosContractAddress, value, web3) { .then(block => { gasLimit = block.gasLimit + const recipient = process.env.testWallet + // Calculate the ether we want to send, into wei const wei = logWeiAmountBeingSent(value, web3) - const rawTx = { nonce: hex(txCount), gasPrice : hex(gasPrice), gasLimit: hex(gasLimit), - to: eosContractAddress, - value: hex(wei), // eventually use 'value' by hex(web3.utils.toWei(value.toString(),'ether')) - chainId: chain, - data:"" + to: recipient, // for testing send to Account Address, for prod send to Contract Address:eosContractAddress, + value: hex(wei), + chainId: chain + // data:"" } - - logRawTxInputsAndHexes({nonce: txCount,gasPrice,gasLimit, to: eosContractAddress,value,chain, data: ""}, rawTx) + + logRawTxInputsAndHexes({nonce: txCount,gasPrice,gasLimit, to: recipient,value,chain, data: ""}, rawTx) return new EthTx(rawTx) // Transaction: { raw: [ ], _fields: ['nonce',]} }) From 5bd1507934bb97e35d830f0bd12524ff8c83c6e6 Mon Sep 17 00:00:00 2001 From: thinkocapo Date: Wed, 7 Mar 2018 17:18:38 -0500 Subject: [PATCH 24/27] fixed log --- scripts.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/scripts.js b/scripts.js index d59d2be..2e41aab 100644 --- a/scripts.js +++ b/scripts.js @@ -34,11 +34,11 @@ module.exports = { return createRawTx(eosContractAddress, value, web3) .then(txInstance => { const tx = txInstance - // console.log('======= tx =======', tx) + console.log('======= tx =======', tx) const txSignedSerialized = createSignedSerializedTx(tx, privateKey) - // console.log('======= txSignedSerialized =======', tx) + console.log('======= txSignedSerialized =======', txSignedSerialized) const txSignedSerializedHex = txSignedSerialized.toString('hex') - // console.log('======= txSignedSerializedHx =======', tx) + console.log('======= txSignedSerializedHx =======', txSignedSerializedHex) return // *TODO* send to a Account Address not a Contract Address @@ -104,6 +104,10 @@ var createRawTx = function (eosContractAddress, value, web3) { var createSignedSerializedTx = function (tx, pKey) { const privateKeyX = Buffer.from(pKey, 'hex') // toString() // new Buffer(pKey, 'hex') + + // *TODO* + //console.log('from: AccountAddress (produce from web3. privateKey):', process.env.address) + tx.sign(privateKeyX) const txSerialized = tx.serialize() //console.log('txSerialized\n', txSerialized) // From c37e860894e8c3f2b53700d1746add743031c802 Mon Sep 17 00:00:00 2001 From: thinkocapo Date: Wed, 7 Mar 2018 17:39:12 -0500 Subject: [PATCH 25/27] sendSignedTransaction, remove sendRawTransaction. Test Trial --- scripts.js | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/scripts.js b/scripts.js index 2e41aab..0ca95d5 100644 --- a/scripts.js +++ b/scripts.js @@ -35,19 +35,34 @@ module.exports = { .then(txInstance => { const tx = txInstance console.log('======= tx =======', tx) + // console.log('======= tx.from =======', tx.from()) // doesnt help const txSignedSerialized = createSignedSerializedTx(tx, privateKey) - console.log('======= txSignedSerialized =======', txSignedSerialized) + // console.log('======= txSignedSerialized =======', txSignedSerialized) const txSignedSerializedHex = txSignedSerialized.toString('hex') - console.log('======= txSignedSerializedHx =======', txSignedSerializedHex) - return + // console.log('======= txSignedSerializedHx =======', txSignedSerializedHex) + + //web3.eth.getBalance(process.env.address) + // .then(console.log); // shows plenty of wei + + web3.eth.sendSignedTransaction('0x' + txSignedSerializedHex) + .then((err, result) => { + console.log('======= err =======\n', JSON.stringify(err,null,4)) + console.log('======= result =======\n', JSON.stringify(result,null,4)) + console.log('\n========= COMPLETE ==========\n') + }) + + // says (receipt) https://github.com/ethereum/web3.js/issues/1134 + // says (err,result) https://ethereum.stackexchange.com/questions/33473/web3-sendsignedtransaction-transaction-cost + // says .on('receipt', console.log); not useful? https://web3js.readthedocs.io/en/1.0/web3-eth.html#sendsignedtransaction + + // > // see eth.getTransactionReceipt() for details - // *TODO* send to a Account Address not a Contract Address - return web3.eth.sendRawTransaction(`0x${txSignedSerializedHex}`, function(err, txHash) { - if (!err) { - console.log('==== transaction hash ==== \n', JSON.stringify(txHash,null,4)); // "0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385" - // web3.eth.getTransaction(hash) to verify - } else { console.log('err sendRawTransaction \n', JSON.stringify(err,null,4))} - }); + // return web3.eth.sendRawTransaction(`0x${txSignedSerializedHex}`, function(err, txHash) { + // if (!err) { + // console.log('==== transaction hash ==== \n', JSON.stringify(txHash,null,4)); // "0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385" + // // web3.eth.getTransaction(hash) to verify + // } else { console.log('err sendRawTransaction \n', JSON.stringify(err,null,4))} + // }); }) }, From 9b50b5c78fdbcf1d0f55a80d4726d0c67b24cf53 Mon Sep 17 00:00:00 2001 From: thinkocapo Date: Wed, 7 Mar 2018 17:48:50 -0500 Subject: [PATCH 26/27] pending transaction... --- scripts.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/scripts.js b/scripts.js index 0ca95d5..32f6671 100644 --- a/scripts.js +++ b/scripts.js @@ -103,11 +103,13 @@ var createRawTx = function (eosContractAddress, value, web3) { const wei = logWeiAmountBeingSent(value, web3) const rawTx = { nonce: hex(txCount), - gasPrice : hex(gasPrice), - gasLimit: hex(gasLimit), + gas: web3.utils.toHex("21000"), + gasPrice: web3.utils.toHex(web3.utils.toWei('10', 'gwei')), + // gasPrice : hex(gasPrice), + // gasLimit: hex(gasLimit), to: recipient, // for testing send to Account Address, for prod send to Contract Address:eosContractAddress, - value: hex(wei), - chainId: chain + value: hex(wei) + // chainId: chain // data:"" } From 53b43cc7d8c13f2fbf26c4f65e6450cad9e334a9 Mon Sep 17 00:00:00 2001 From: thinkocapo Date: Wed, 7 Mar 2018 18:12:33 -0500 Subject: [PATCH 27/27] clean --- scripts.js | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/scripts.js b/scripts.js index 32f6671..d3daf56 100644 --- a/scripts.js +++ b/scripts.js @@ -97,22 +97,25 @@ var createRawTx = function (eosContractAddress, value, web3) { .then(block => { gasLimit = block.gasLimit - const recipient = process.env.testWallet + const recipient = process.env.testWallet // || eosContractAddress - // Calculate the ether we want to send, into wei const wei = logWeiAmountBeingSent(value, web3) const rawTx = { nonce: hex(txCount), gas: web3.utils.toHex("21000"), gasPrice: web3.utils.toHex(web3.utils.toWei('10', 'gwei')), - // gasPrice : hex(gasPrice), - // gasLimit: hex(gasLimit), - to: recipient, // for testing send to Account Address, for prod send to Contract Address:eosContractAddress, + to: recipient, value: hex(wei) - // chainId: chain - // data:"" - } + } + + // didnt work when used this. github issue's debating, said in mid-2017 chainId became mandatory but its failing for me: + // chainId: chain + // this combo didn't work: + // gasPrice : hex(gasPrice), + // gasLimit: hex(gasLimit), + // not needed, can be used for deploying smart contracts: + // data:"" logRawTxInputsAndHexes({nonce: txCount,gasPrice,gasLimit, to: recipient,value,chain, data: ""}, rawTx) return new EthTx(rawTx) // Transaction: { raw: [ ], _fields: ['nonce',]}