Web3.js: Calling contract function with argument not working.

Created on 26 Jan 2018  路  16Comments  路  Source: ChainSafe/web3.js

In solidity the function take four arguments

function somefunction (string name, uint weight, uint height, uint age)

Call the function using web3.js

Contract.somefunction.sendTransaction("hello", 12, 1, 12, {from: web3.eth.accounts[0]})

which will return hash and success submit the transaction,but not update the blockchain data.

"0xd5b8c99b25eff96dcceb11ade000eebda39df5a1bb62245fbc04ca476f3e8f31"

But when using Remix IDE I can success update the data.

Most helpful comment

So, I may be wrong here, but I faced a similar problem before. The transaction information says your provided 90k gas and the transaction receipt says it used 90k gas. So it used all the gas provided. This can mean two things:

  • The transaction did not have enough gas to proceed. In this case, try to provide more gas to the transaction (I personnaly use 200k for this type of tx. It's way more than necessary, but I'm sure it will pass)

  • The transaction used exactly the amount of gas provided and succeed. But if you say that the state did not change, then the I think this is not correct.

All 16 comments

With web3js 1.0.0, you must use the following:
contract_instance.methods.somefunction(arg1, arg2,...).send({});

The web3 version is 0.20.4 with the default yarn add web3
I found other function can success execute using web3,but this one can only execute using Remix IDE not web3.
I hope can see more error message of the execute process,but only saw the hash.

Can you provide the full code of the function please?

Edited.

event Log_string(bytes32 log, string name);

Student[] public students;
struct Student {
    string name;
    uint weight;
    uint height;
    uint age;
}


function add_student(string name, uint weight, uint height, uint age) public {
    students.push(Student(name, weight, height, age));
    Log_string("add", name);
}

Not sure if this is the reason, but you are missing one argument on your log. You declare log_string with a bytes32 and a string inputs, but you only use it with a string.
By the way, With solifity, events name start with uppercase ;)

It it two arguments of the event just miss it on the above code.But I thought it might cause by other things.
Because Remix IDE can success execute it.

Can you get the transaction receipt and the transaction information? A transaction can be mined but can still fail.

The receipt

> web3.eth.getTransactionReceipt("0xad5753f430589c6131ecdf389068d657609735550126eeb29eb8f82323aa09f7")
{
  blockHash: "0x42e9bc698e1f844c85fc666920f6fa4ffea25cef46936289a8549a8bde65b5fc",
  blockNumber: 78,
  contractAddress: null,
  cumulativeGasUsed: 90000,
  from: "0x4df8362a4d03f18b139d9994b03587c747a8565f",
  gasUsed: 90000,
  logs: [],
  logsBloom: "0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  status: "0x0",
  to: "0x9c0a95254ec0a3616dc7ef0cd75d45bdfc0ad5c8",
  transactionHash: "0xad5753f430589c6131ecdf389068d657609735550126eeb29eb8f82323aa09f7",
  transactionIndex: 0
}
>

Please also call getTransaction(hash). So that we can see the gas provided to the transaction.

It look like this

> web3.eth.getTransaction("0xad5753f430589c6131ecdf389068d657609735550126eeb29eb8f82323aa09f7")
{
  blockHash: "0x42e9bc698e1f844c85fc666920f6fa4ffea25cef46936289a8549a8bde65b5fc",
  blockNumber: 78,
  from: "0x4df8362a4d03f18b139d9994b03587c747a8565f",
  gas: 90000,
  gasPrice: 1,
  hash: "0xad5753f430589c6131ecdf389068d657609735550126eeb29eb8f82323aa09f7",
  input: "0xbd258472000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000
0000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000
  nonce: 72,
  r: "0xf02d7b99d6e5cca89f463808ba72d2cf0a0205e7d06a85dffd18e7afb48db0f2",
  s: "0x245d4061401a4d171e63c3c138b9731f537948f6a2ad45995e0267bdead830e7",
  to: "0x9c0a95254ec0a3616dc7ef0cd75d45bdfc0ad5c8",
  transactionIndex: 0,
  v: "0xa96",
  value: 0
}

So, I may be wrong here, but I faced a similar problem before. The transaction information says your provided 90k gas and the transaction receipt says it used 90k gas. So it used all the gas provided. This can mean two things:

  • The transaction did not have enough gas to proceed. In this case, try to provide more gas to the transaction (I personnaly use 200k for this type of tx. It's way more than necessary, but I'm sure it will pass)

  • The transaction used exactly the amount of gas provided and succeed. But if you say that the state did not change, then the I think this is not correct.

Thanks,when I call the function with gas it success updated.

Contract.add_student("hello",12,1,12, {from: web3.eth.accounts[0], gas: 200000})

But how to know how much gas should provide to it.

I am not sure for web3 0.2, but web3 1.0.0 provides a "estimateGas" function. But if you don't want to use this, you can still put your contract in remix. Click on the "details" button, scroll down until you see the "estimate gas" part. With this information, you have an idea of your function gas cost. Just put it a bit higher to be sure not having any problem.

It show that it will be infinite.

"add_student(string,uint256,uint256,uint256)": "infinite",

I had a look on my side. I think it is because of the structure. Solidity is not that nice when it comes to "complex types". So yeah, sorry, won't work with remix. I would run it twice or three times, check the gas consumed. This way you will have the correct gas cost. Or use estimateGas function.

Thanks,I got the correct estimate gas when using the estimateGas function.

Was this page helpful?
0 / 5 - 0 ratings