I want to execute a smart contract on the blockchain. As far as I understood this web3js offers two options

  • call: executes the contract method locally without using gas
  • execute: executes the contract method on the blockchain and requires gas

What I don't get how to use a specific wallet e.g.

const ABI = require('./contracts/abi.json');
const addressContract = "xyz";
const Web3 = require('web3');
const Tx = require('ethereumjs-tx');

const wallet = {
  public: somePublicKey,
  private: somePrivateKey

const web3 = new Web3(new Web3.providers.HttpProvider(config.provider));
const contract = new self.web3.eth.Contract(ABI, addressContract);
const options= {
    from: wallet.public,
    gasPrice: process.env.GAS
    gasLimit: 250000

contract.methods.methodA(param1,param2,param3).send(options, (err, txHash)=> {

I found that to execute a contract there is a plugin called ethereumjs-tx which would modify my code to the following:

//options.data = ?
options.to = addressContract
options.nonce = someNonce
const tx = new Tx(options);
tx.sign(new Buffer(wallet.private, 'hex'));
const rawTx = `0x${tx.serialize().toString('hex')}`;
self.web3.eth.sendRawTransaction(rawTx, (err, result) => {

I'm struggling basically with the data property. Because contract.methods.methodA.getData does not exists.


1) How to get my params into the data format? / Or do I just add them to options instead of options.data

EDIT: I guess I could feed all the params into: https://github.com/ethereumjs/ethereumjs-abi

var parameterTypes = ["address", "uint256", "address", "uint256", "uint256", "uint256", "address", "uint8", "bytes32", "bytes32", "uint256"];
var parameterValues = ["0x1234567812345678", 256, "0x1234567812345678", 256, 256, 256, "0x1234567812345678", 8, 32, 32, 256];

var encoded = abi.rawEncode(parameterTypes, parameterValues);

And attach the result to data with '0x'+encoded.

useful links


1 Answer 1


What is the error you are getting when you are trying to call the function using .send()? That's actually much simpler, generally, if you're able to figure that out. To send from a specific wallet/address, you need to enter that address as the value of the from field in your options.

And you need to make sure that wallet is unlocked.

If you're using this code to basically unlock the account (i.e. sign the transaction), and you have the private key anyways (wallet.private), it is easier to do that directly in the blockchain node (web3.personal or geth/truffle console: personal.unlockAccount(address, "password"))

OK, but back to your question.

It seems like .getData() has been removed from web3 1.0 in place of .encodeABI(). So you can try with .encodeABI() (I have not tried this yet, but from the docs seems like this should do it), or use an earlier version of web3 (which would require you to change some of your code, check here) and getData.

Your documentation linked in your question is for web3 0.2x.x. But your initial code of getting the contract instance looks like web3 1.0.x. There are quite some pretty big changes, and the reason why you can't fine getData.

  • When calling I get no error, if sending I get Invalid JSON RPC response: “”. I would prefer to work with the simpler solution. To unlock the wallet I need to run geth and download the blockchain, right? Currently, I use https://mainnet.infura.io/xyz as HttpProvider. Or can I unlock the wallet in web3js?
    – Andi Giga
    Commented Nov 29, 2017 at 8:22
  • This looks promising: ethereum.stackexchange.com/questions/6903/…
    – Andi Giga
    Commented Nov 29, 2017 at 8:25
  • I would avoid running a node, so I probably need to sign the transaction offline?
    – Andi Giga
    Commented Nov 29, 2017 at 9:04
  • Ok your error message looks like you are not connecting to the web3 provider properly. You can test out first with some basic functions to confirm (such as web3.eth.getBlocknumber(console.log) or checking the balance of some account web3.eth.getBalance). If it’s an unlocking account issue, the error message will typically say your account is not unlocked/authorized. Are you trying directly on mainnet (from your infura url)? And to your question, yes. If you are not running a local node, then you would have to sign offline and use ethereumtx-js / send data as you had started to do.
    – carlolm
    Commented Nov 29, 2017 at 9:27
  • I got so far that I can send a signed Transaction now, it appears on etherscan and fails. As soon as I get the whole solution I will post it here.
    – Andi Giga
    Commented Nov 29, 2017 at 9:38

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.