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());

1 Answer 1

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

9
  • i get an error (node:7928) UnhandledPromiseRejectionWarning: Error: Invalid JSON RPC response:
    – Ayurpwnz
    Commented Nov 21, 2019 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
    Commented Nov 21, 2019 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
    Commented Nov 22, 2019 at 10:51
  • do you have telegram?
    – Ayurpwnz
    Commented Nov 22, 2019 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
    Commented Nov 22, 2019 at 22:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.