Pass parameter as bytes32 to Solidity Smart Contract

+1 vote

I am using this Ethereum Go Client and trying to pass a string / bytes32 to Solidity.

The function in the smart contract is very simple (for testing now):

  function vote(bytes32 id) {
    //id has the value 0x0000000000000000000000000000000000000000000000000000000000000000


hash, err := contract.Send(transaction, "vote", "myString")

will result in 0x0000000000000000000000000000000000000000000000000000000000000000

for the bytes32 param id...

How would I have to pass in the parameter to my Smart Contract from Go so that solidity will have the correct value?

Sep 27, 2018 in Blockchain by slayer
• 29,310 points

3 answers to this question.

+1 vote

I think you have to encode it

answered Sep 27, 2018 by digger
• 26,720 points
+1 vote

To convert a string to a bytes32 for solidity all you have to do is create a fixed length byte array in Go and copy the string the value to it.

value := [32]byte{}
copy(key[:], []byte("hello"))

Then you may pass the value to the smart contract function:

hash, err := contract.Send(transaction, "vote", value)
answered Oct 12, 2018 by Firoz
+1 vote

Use the common.HexToHash package and pass the string value.

convertedValue := common.HexToHash("myString")
hash, err := contract.Send(transaction, "vote", convertedValue)
answered Sep 19, 2019 by Grashias

edited Sep 19, 2019 by Omkar

Related Questions In Blockchain

0 votes
1 answer
+1 vote
4 answers

How to estimate the cost for deploying smart contract on mainnet?

Since you have already deployed the contract ...READ MORE

answered Apr 10, 2018 in Blockchain by Shashank
• 10,400 points
0 votes
1 answer

How do I send back ethers to the sender of the tokens in a smart contract?

Whenever a smart contract receives ether via ...READ MORE

answered May 30, 2018 in Blockchain by Perry
• 17,100 points
0 votes
1 answer

How is a request sent from an app to a smart contract?

Yes, the contract is distributed by every node ...READ MORE

answered Jun 4, 2018 in Blockchain by Perry
• 17,100 points
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,830 points
0 votes
1 answer
+3 votes
2 answers

How to run ethereumjs using Node.JS

You need to install testrpc globally on ...READ MORE

answered Mar 27, 2018 in Blockchain by ned_crew
• 1,610 points
0 votes
1 answer

How to use real world/live data in my smart contract?

You cant access/embed real world data using ...READ MORE

answered Jul 12, 2018 in Blockchain by digger
• 26,720 points
0 votes
1 answer

How to limit execution of smart contract only to my dapp?

You can use modifiers to do it ...READ MORE

answered Jul 17, 2018 in Blockchain by digger
• 26,720 points