0

I am trying to send ehter using this code

  web3.eth.accounts.wallet.add(privateKey);
  web3.eth.sendTransaction({
          to:someAddress,
          from:onwer,
          value:amount*1,
          gasPrice:result,
          gas:21000,
          nonce:nonce
       }).
  then(function (r) {
           res.json({
                 response:r
                })
  }).catch(function(err){
         res.json({error:err.message+"unknown tx"});
    });

I keep getting

error: unknown account

But I add sender wallet explicitly in this part

 web3.eth.accounts.wallet.add(privateKeyHere);
 result is fetched from  web3.eth.getGasPrice() Promise
 nonce is fetched  web3.eth.getTransactionCount(sender_address_here) Promise

What is wrong with the code ?

Can i send ether without sendSignedTransaction or other methods?

Which is the best (and most simple) method to send ether when you have private key using web3 ??

Thanks

EDIT 1 Account was create using

web3.eth.accounts.create();
  • How did you create owner account? which command have you used? – rahul saini Apr 30 '18 at 7:55
0

Answering my own question I found solution using this code

var Web3 = require("web3");
const web3 = new Web3('http://localhost:8545');
var connection = require("../services/connection");
const axios = require('axios');
const EthereumTx = require('ethereumjs-tx');

 let response = axios.get('https://ethgasstation.info/json/ethgasAPI.json').
    then(function (response) {
       let prices = {
             low: response.data.safeLow / 10,
             medium: response.data.average / 10,
             high: response.data.fast / 10
            }
      let nonce = web3.eth.getTransactionCount(token.public).then(function (nonce) {
           let sendAmount = (prices.high * 1e9) + (prices.high * 1e8);
                let details = {
                          "to": user.public,
                          "value": sendAmount,
                          "gas": 21000,
                          "gasPrice": prices.low * 1e9,
                          "nonce": nonce,
                          "chainId": 4 //chainId - mainnet: 1, rinkeby: 4
                      }
    const transaction = new EthereumTx(details)
    transaction.sign(Buffer.from(your_pv_key_without0xprefix, 'hex'))
    const serializedTransaction = transaction.serialize()
    web3.eth.sendSignedTransaction('0x' + serializedTransaction.toString('hex')).
           then(function (transactionDetails) {
             //handle transaction details
         }).catch(function (err) {
              res.json({error: err.message});
         })
     }).catch(function (err) {
                 res.json({error: err.message});
     })
  }).catch(function (err) {
       res.json({error: err.message});
   })

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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