1

Can someone tell me how to send eth to the address locally? I tried to create an account, but I don’t know how to send funds to it locally

Web3 = require('web3');

web3 = new Web3(new Web3.providers.HttpProvider("http://127.0.0.1:8545"));
console.log(web3.eth.accounts.create());
Ayurpwnz
  • 195
  • 1
  • 1
  • 7

1 Answers1

2

You can reference the web3.js docs around sending a transaction here: https://web3js.readthedocs.io/en/v1.2.4/web3-eth.html#sendtransaction

the example looks like:

// using the promise
web3.eth.sendTransaction({
    from: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe',
    to: '0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe',
    value: '1000000000000000'
})
.then(function(receipt){
    ...
});

If the from account is not unlocked, you may need to either unlock it or sign the transaction.

Unlocking (results in security concerns) https://web3js.readthedocs.io/en/v1.2.0/web3-eth-personal.html#unlockaccount

Signing Raw Transaction: https://web3js.readthedocs.io/en/v1.2.0/web3-eth.html#sendsignedtransaction

Example

Step 1: Start ganache enter image description here

Step 2: Init NPM project and npm install web3 (version 1.2.4 in this example)

Step 3: Write code (take addresses from ganache available accounts)

var Web3 = require('web3');

web3 = new Web3(new Web3.providers.HttpProvider("http://127.0.0.1:8545"));

web3.eth.getBalance('0x9e5ef1c0a4b1619be3e83184a6549b5ee11a159c').then(console.log);

// using the promise
web3.eth.sendTransaction({
    from: '0x557d3aec51e4461b52c504f087e7122c8906be6c',
    to: '0x9e5ef1c0a4b1619be3e83184a6549b5ee11a159c',
    value: '10000000'
})
.then(function(receipt){
  console.log(receipt.transactionHash);
  console.log("ending balance:");
  web3.eth.getBalance('0x9e5ef1c0a4b1619be3e83184a6549b5ee11a159c').then(console.log);
});

step 4: run code

enter image description here

Steven V
  • 1,461
  • 10
  • 15
  • i get an error (node:7928) UnhandledPromiseRejectionWarning: Error: Invalid JSON RPC response: – Ayurpwnz Nov 21 '19 at 21:36
  • are you running ganache? if so what command are you using to start it? maybe try something more simple like getting the balance of an address to confirm your provider is set up correctly – Steven V Nov 21 '19 at 21:38
  • Thanks for helping! When I use the ganache-cli command at the end I have Listening on 127.0.0.1:8545 and I can not use other code – Ayurpwnz Nov 22 '19 at 10:51
  • do you have telegram? – Ayurpwnz Nov 22 '19 at 19:38
  • no - but you can add another question or add more detail and i'd be happy to try to help – Steven V Nov 22 '19 at 22:01
  • I enter the ganache-cli command, after which I get 10 accounts. How do I continue to work with these accounts? Are these accounts stored somewhere? – Ayurpwnz Nov 24 '19 at 12:13
  • when you run ganache-cli you can use -m "use your own mnemonic just a random set of words" -- this will make it so you can get the same accounts / private keys every time. At this point I'll usually just hardcode the addresses / private keys as configs (this is insecure and just for dev purposes). You can also get accounts programmatically web3.eth.getAccounts().then((accounts) => console.log(accounts[0])); According to this - https://ethereum.stackexchange.com/questions/60995/how-to-get-private-keys-on-truffle-from-ganache-accounts you can get private keys too but never tried – Steven V Nov 24 '19 at 14:26
  • And how can i create an ethereum address not locally, but through a browser, for example, when registering a user – Ayurpwnz Nov 24 '19 at 20:26
  • web3.eth.accounts.create([entropy]); -- Generates an account object with private key and public key. https://web3js.readthedocs.io/en/v1.2.0/web3-eth-accounts.html – Steven V Nov 24 '19 at 21:32