Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connect to remote geth ip rpc #1

Merged
merged 27 commits into from
Mar 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
node_modules
.env
notes
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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





4 changes: 0 additions & 4 deletions eth-node-providers.js

This file was deleted.

9 changes: 9 additions & 0 deletions eth-nodes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
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
lincolnPark: process.env.LP,
myEtherWallet: 'https://api.myetherapi.com/eth',
}

129 changes: 14 additions & 115 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,120 +1,19 @@
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
require('dotenv').config()
const ethNodes = require('./eth-nodes')
const scripts = require('./scripts')
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));
}

// console.log('\n======== web3.eth =======\n', web3.eth.Contract) // web3.accounts [...]
// 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[4]] || ethNodes.myEtherWallet
let web3 = new Web3(new Web3.providers.HttpProvider(node))

//*TODO* - need add an account to GETH. see if can access it as web3.eth.accounts[0] 11:59a
// 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, 0.003) // process.argv[3])

// 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

// NOTES and alternative code
// web3 = new Web3(new Web3.providers.HttpProvider(gethLocal));
// web3.setProvider(new web3.providers.HttpProvider(myEtherWallet)); // https://www.myetherapi.com/
Loading