Call contract methods with web3 from newly created account

0 votes

I need to call methods from my contract in Ethereum without using MetaMask. I use Infura API and try to call my methods from account, recently created with web3.eth.create() method. This method returns object like this:

{
    address: "0xb8CE9ab6943e0eCED004cG5834Hfn7d",
    privateKey: "0x348ce564d427a3311b6536bbcff9390d69395b06ed6",
    signTransaction: function(tx){...},
    sign: function(data){...},
    encrypt: function(password){...}
} 

I also using infura provider:

 const web3 = new Web3(new Web3.providers.HttpProvider(
    "https://rinkeby.infura.io/5555666777888"
  ))

So, when I try to write smth like that:

contract.methods.contribute().send({
          from: '0xb8CE9ab6943e0eCED004cG5834Hfn7d', // here I paste recently created address
          value: web3.utils.toWei("0.5", "ether")
        });

I have this error:

Error: No "from" address specified in neither the given options, nor the default options.

Sep 25, 2018 in Blockchain by slayer
• 29,350 points
6,088 views

1 answer to this question.

0 votes

In fact, we can't just send transactions from newly created address. We must sign this transaction with our private key. For example, we can use ethereumjs-tx module for NodeJS.

const Web3 = require('web3')
const Tx = require('ethereumjs-tx')

let web3 = new Web3(
  new Web3.providers.HttpProvider(
    "https://ropsten.infura.io/---your api key-----"
  )
)

const account = '0x46fC1600b1869b3b4F9097185...'; //Your account address
const privateKey = Buffer.from('6e4702be2aa6b2c96ca22df40a004c2c944...', 'hex');
const contractAddress = '0x2b622616e3f338266a4becb32...'; // Deployed manually
const abi = [Your ABI from contract]
const contract = new web3.eth.Contract(abi, contractAddress, {
  from: account,
  gasLimit: 3000000,
});

const contractFunction = contract.methods.createCampaign(0.1); // Here you can call your contract functions

const functionAbi = contractFunction.encodeABI();

let estimatedGas;
let nonce;

console.log("Getting gas estimate");

contractFunction.estimateGas({from: account}).then((gasAmount) => {
  estimatedGas = gasAmount.toString(16);

  console.log("Estimated gas: " + estimatedGas);

  web3.eth.getTransactionCount(account).then(_nonce => {
    nonce = _nonce.toString(16);

    console.log("Nonce: " + nonce);
    const txParams = {
      gasPrice: 100000,
      gasLimit: 3000000,
      to: contractAddress,
      data: functionAbi,
      from: account,
      nonce: '0x' + nonce
    };

    const tx = new Tx(txParams);
    tx.sign(privateKey); // Transaction Signing here

    const serializedTx = tx.serialize();

    web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex')).on('receipt', receipt => {
      console.log(receipt);
    })
  });
});

Transaction time is about 20-30 seconds so you should wait quite a bit.

answered Sep 25, 2018 by digger
• 26,740 points

Related Questions In Blockchain

0 votes
1 answer

How to call smart contract methods from transactions?

You can do this using a wrapper. ...READ MORE

answered Aug 13, 2018 in Blockchain by slayer
• 29,350 points
797 views
0 votes
2 answers

how save data from different account using web3.py to smart contract

Hey, @Amal, It will automatically take the address ...READ MORE

answered Jul 28, 2020 in Blockchain by Rajiv
• 8,910 points
1,289 views
0 votes
2 answers

How do I interact with a smart contract on a private network through web3.js

I found a blog that explains how ...READ MORE

answered Aug 20, 2018 in Blockchain by slayer
• 29,350 points
3,677 views
0 votes
1 answer
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,702 views
0 votes
1 answer

Getting the length of public array variable (getter)

The calling contract will need the ABI ...READ MORE

answered Sep 24, 2018 in Blockchain by digger
• 26,740 points
1,191 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,237 views
0 votes
1 answer

Solidity geth: Error encountered during contract execution [Bad instruction]

recipes is a dynamic storage array. You need ...READ MORE

answered Oct 15, 2018 in Blockchain by Omkar
• 69,210 points
1,252 views
0 votes
1 answer

Web3 1.0: What does `web3.eth.call(tx)` return for a contract creation?

The returned bytestring is the deployedBytecode of the contract, ...READ MORE

answered Sep 17, 2018 in Blockchain by digger
• 26,740 points
1,041 views
0 votes
1 answer

Ethereum call contract method that emits an event, from another contract

You need to refer to the EventEmitter by ...READ MORE

answered Sep 25, 2018 in Blockchain by digger
• 26,740 points
1,859 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP