Hey guys, I'm using this constructor for my ballot contract:

constructor(bytes32[] memory proposalNames) public {
chairperson = msg.sender;
voters[chairperson].weight = 1;


As parameters I converted the names Alice and Bob to bytes32
0x416c696365 and 0x426f62

But when I deploy on Remix using these two parameters (comma separated) I get the following error:

creation of Ballot errored: Error encoding arguments: Error: types/values length mismatch (count={"types":1,"values":2}, value={"types":["bytes32[]"],"values":["0x416c696365","0x426f62"]}, version=4.0.47)


Should the parameters be of different length?

Nov 6, 2020, 2:27 PM
Is there a way to convert Alice to a fixed 32 bytes array?
or any string
If I change the solidity code to bytes[] instead of bytes32[] it throws:

TypeError: This type is only supported in ABIEncoderV2. Use "pragma experimental ABIEncoderV2;" to enable the feature.
this is an example contract from the solidity docs
I could use strings but I think I'll run in to this problem again later so it sounds like a good idea to understand the problem
Isn't there a simple way to convert a string in to a bytes32 array?
Nov 6, 2020, 2:38 PM
bytes is already a dynamic length variable
bytes[] is an array of those
2-dimensional arrays are not working w/o the experimental pragma
Nov 6, 2020, 2:39 PM
I never ran into this problem. If you really have to you can pad 0 so you have 0x00000..0000416c696365
Nov 6, 2020, 2:42 PM
So that would be
alice converts to 0x00000000000000000000616c696365
bob converts to 0x000000000000000000000000626f62

?
Nov 6, 2020, 2:45 PM
yeah
Nov 6, 2020, 2:45 PM
still throws

creation of Ballot errored: Error encoding arguments: Error: types/values length mismatch (count={"types":1,"values":2}, value={"types":["bytes32[]"],"values":["0x00000000000000000000616c696365","0x000000000000000000000000626f62"]}, version=4.0.47)

šŸ™
Nov 6, 2020, 2:45 PM
is it exactly 32 bytes?
Nov 6, 2020, 2:46 PM
It's exactly 32 characters (including the 0x)
Does that mean its 32 bytes?
Nov 6, 2020, 2:46 PM
no
function stringToBytes32(string memory source) public pure returns (bytes32 result) {
bytes memory tempEmptyStringTest = bytes(source);
if (tempEmptyStringTest.length == 0) {
return 0x0;
}

assembly {
result := mload(add(source, 32))
}
}
try this
Nov 6, 2020, 2:48 PM
Thanks, that returned for alice and bob respectively:

0x616c696365000000000000000000000000000000000000000000000000000000
0x626f620000000000000000000000000000000000000000000000000000000000
But when I try to deploy the ballot contract with those two values in the constructor (comma separated) it still throws

creation of Ballot errored: Error encoding arguments: Error: types/values length mismatch (count={"types":1,"values":2}, value={"types":["bytes32[]"],"values":["0x616c696365000000000000000000000000000000000000000000000000000000","0x626f620000000000000000000000000000000000000000000000000000000000"]}, version=4.0.47)
Nov 6, 2020, 2:54 PM
ohhh ok
because its an array of an array
bytes32 is an array itself
so you cant have an array of bytes32 like that
im not sure but that might be it
Nov 6, 2020, 2:57 PM

Ā© 2024 Draquery.com All rights reserved.