I'm trying to use web3.js with infura.io to interact with my smartcontract. here is my simple greeter contract code

pragma solidity ^0.5.7;
contract greeter{
    string greeting;

    function greet(string memory _greeting)public {
    function getGreeting() public view returns(string memory) {
        return greeting;

Here is what I found from web3.js documentation.

var tx = web3.eth.sendTransaction({
             from: '0x3dCDe57Ad49d639c4D702b607Dd5eBc0aB54A671',
             nonce: web3.toHex(txCount),
             gasLimit: web3.toHex(1000000),
             gasPrice: web3.toHex(web3.toWei('10', 'gwei')),
web3.eth.accounts.signTransaction(tx, privateKey [, callback]);

But it doesn't take me anywhere. Because,where can I pass greeting data? All the documentation talks about sending ether. Then what about sending string data like this? with web3.js 0.20 I can read greeting from chain. I wanted to add new greeting string with greet function. but somehow I have no idea about signing transaction. could anyone help me in understanding how to sign transacion and adding greeting to the contract using web3.js?

  • could you please add some more informations, like are you using truffle? ganache? remix? web3js 1.0 or 0.x? and maybe post your simple js code for read greeting :)
    – Majd TL
    May 17, 2019 at 7:49
  • Added web3.js documentation reference @majdTL
    – ajay
    May 17, 2019 at 8:18
  • are you using Truffle? are you using a private blockchain or a simulated one with truffle?
    – Majd TL
    May 17, 2019 at 11:16
  • I'm trying to send transaction to ropsten network via infura node from command line. that means no truffle, no metamask.
    – ajay
    May 17, 2019 at 12:59
  • okay then my answer will work if you have the private key, and some ether on your ropsten account
    – Majd TL
    May 17, 2019 at 13:24

1 Answer 1


You are not providing too much informations for me to give you an easy way to do that :) , anyway this is one way to how you can send transaction:

Better to use web3 1.0, you need also install ethereumjs-tx: npm install ethereumjs-tx

const Tx = require('ethereumjs-tx');

to get instance of your contract:

new web3.eth.Contract(jsonInterface, address, options)


const myContract = new web3.eth.Contract([...], '..address of your contract..', {
    defaultAccount: '....', // default from address
    defaultGasPrice: '20000000000' // default gas price in wei, 20 gwei in this case

and to get the data for the transaction parameter:

const mydata = myContract.methods.greet("<your-greeting.string>").encodeABI()));

to Sign and Send

const privateKey = Buffer('<your privatekey>', 'hex')
const rawTx = {
    nonce: <your-tx-nonce.. you can also get it with web3 command >,
    gasLimit: web3.toHex(1000000),
    gasPrice: web3.toHex(web3.toWei('10', 'gwei')),
    to: <your contract address>,
    value: '0x00',
    data: mydata 
const tx = new Tx(rawTx);
const serializedTx = tx.serialize();
web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'))
        .on('receipt', console.log);

https://web3js.readthedocs.io/en/1.0/web3-eth.html#sendsignedtransaction https://web3js.readthedocs.io/en/1.0/web3-eth-abi.html?highlight=encode

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.