0

Enviroment Node version: v10.15.3 Web3: ^1.0.0-beta.50 Net: Ropsten

My smart contract is a very simple payable/balance one and my goal is to contribute 0.1 ETH to raise the contract balance using web3:

pragma solidity >=0.4.25 <0.6.0;

contract Demo {
 uint public balance;

 constructor() public
 {
  balance = 0;
 }

 function contribute() public payable
 {
  balance += msg.value;
 }
}

And Im trying to call contribute function with web3:

import Web3 from "web3";
import Tx from "ethereumjs-tx";
import contractAbi from "./contractAbi";

    const web3 = new Web3("https://ropsten.infura.io/v3/xxxxxxx");

    const contractAdress = "xxxx";
    const my_public_key_address_1 = "yyyy";
    const privateKey1 = "vvv";

    const txCount = await web3.eth.getTransactionCount(my_public_key_address_1);
    const contract = await new web3.eth.Contract(contractAbi, contractAdress);

    const txObject = {
      nonce: web3.utils.toHex(txCount),
      to: "xxx",
      gasLimit: web3.utils.toHex(41000),
      gasPrice: web3.utils.toHex(web3.utils.toWei("4", "gwei")),
      data: contract.methods.contribute(100000000000000000).encodeABI(),
      chainId: 3,
    };
    //
    const tx = new Tx(txObject);
    tx.sign(privateKey1)
    const serializedTx = tx.serialize()
    const raw = '0x' + serializedTx.toString('hex')

    const txHash = await web3.eth.sendSignedTransaction(raw)

And Im getting this error:

enter image description here

  • 1
    Note that 100000000000000000 > Number.MAX_SAFE_INTEGER. – goodvibration Mar 27 at 17:03
  • And in any case, function contribute does not take any input arguments, so contract.methods.contribute(100000000000000000) is not a valid transaction. – goodvibration Mar 27 at 17:04
1

contribute doesn't take any parameters, but you're trying to pass one. You probably instead wanted to call the function with no parameters but attach ether to the transaction:

const txObject = {
  nonce: web3.utils.toHex(txCount),
  to: "xxx",
  gasLimit: web3.utils.toHex(41000),
  gasPrice: web3.utils.toHex(web3.utils.toWei("4", "gwei")),

  // NOTE: Drop the parameter here:
  data: contract.methods.contribute().encodeABI(),

  chainId: 3,

  // NOTE: Add a value (in wei) here:
  value: "100000000000000000",
};

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.