Solidity – How to Pass the Value in Bytes32[] Array

remixsolidity

Given this constructor:

function Voting(bytes32[] candidateNames) public {
    candidateList = candidateNames;
}

How to pass pass the value in the parameter candidateNames when I deploy the contract in remix?

enter image description here

Best Answer

If you're willing to do a little text editing, here's a mostly painless way. Make new a contract (or temporarily add the function below to this contract and pass [] to the constructor when you run it). Hardcode the candidate names (note that I set the array size to 3 since I know it for this example):

function getBytes32ArrayForInput() pure public returns (bytes32[3] b32Arr) {
    b32Arr = [bytes32("candidate1"), bytes32("c2"), bytes32("c3")];
}

Compile and run the function. You should get this output:

0x63616e6469646174653100000000000000000000000000000000000000000000,0x6332000000000000000000000000000000000000000000000000000000000000,0x6333000000000000000000000000000000000000000000000000000000000000

Then use a text editor to format it like this:

["0x63616e6469646174653100000000000000000000000000000000000000000000","0x6332000000000000000000000000000000000000000000000000000000000000","0x6333000000000000000000000000000000000000000000000000000000000000"]

It's now in the proper format so that Remix will accept it in the constructor text field as bytes32[].

Related Topic