i hava contract like this

pragma solidity ^0.5.0;

contract Sample {
    string public name = "sample";

    function set(string memory _name) public {
        name = _name;

    function get() public view returns (string memory) {
        return name;

and i want to send data to contract's function named 'set'. Like this:

web3.eth.getAccounts(function(error, result) {
        data: "0xdf"
            }, function(err, transactionHash) {
      if (!err)
        console.log(transactionHash + " success"); 

but how can i specify function name('get') in below code? Then i want to see the change in following code:

    .then(result => console.log('Sonuç: ' + result))
    .catch(error => console.log('An error occured! --> ' + error));

I think you're just looking for this?

sampleContract.methods.set("myname").send({ from: "0xaD950d2Ae9234900B59F9fb16202a195D9fbC74a" })

The selector of the function is made of the first 4 bytes of the hash of the function name and parameter types. In your case the data field is calculated by first getting the hash of the function name: sha3('get()') then get the first 4 bytes of that as your data.

If the function has parameters, for instance: myfunction(uint256 value1, uint256 value2)

First get first 4 bytes of sha3('myfunction(uint256,uint256)') and then concatenete the result with the variables value1 and value2: (4bytes,values1,values2)

Hope this helps

  • sorry i made mistake. i meant to write "set". now how can write set function with parameter? May 2 '19 at 21:08
  • I edited the answer.
    – Jaime
    May 2 '19 at 21:22

Function is specified by the first 4 bytes of transaction data, that should be equal to the first 4 bytes of keccak256 hash code of the signature of the function. Signature of get function is get(), keccak256 of this signature is 0x6D4CE63CAA65600744AC797760560DA39EBD16E8240936B51F53368EF9E0E01F, first four bytes are 0x6D4CE63C, so you need to do something like this:

web3.eth.getAccounts (
  function (error, result) {
    web3.eth.sendTransaction (
        data: "0x6D4CE63C"
      function (err, transactionHash) {
        if (!err)
          console.log(transactionHash + " success"); 

In case you need to send parameters to the function, you need to encode them and append to the transaction data after 4-bytes function selector. For single argument of type string the encoding will be:

string offset: 32 bytes
string body length: 32 bytes
string body padded right to the round 32-bytes

So if you want to call your set function and pass "The quick brown fox jumps over the lazy dog" as a parameter, you need to attach the following data to the transaction:

4ed3885e                                                         // 4-bytes function selector
0000000000000000000000000000000000000000000000000000000000000020 // string body offset (32)
000000000000000000000000000000000000000000000000000000000000002b // string body length (43)
54686520717569636b2062726f776e20666f78206a756d7073206f7665722074 // string body padded
6865206c617a7920646f67000000000000000000000000000000000000000000 // to round 32 bytes

Note that offset points to the string length and does not take function selector into account.

  • sorry i made a mistake in below. i mean to "set". May 2 '19 at 21:03
  • now how can i write set function with parameter? May 2 '19 at 21:08
  • Updated the answer May 2 '19 at 22:06

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.