Solidity packing doesn't support arrays
See original GitHub issueI have been trying to reproduce Solidity’s sha3 in Javascript, and have gotten everything to work with my own code except arrays. Once I found out about this library, I tried to use it for arrays, but I haven’t been able to get the same hash as Solidity produces yet. It would be great if you might be able to help me understand this better.
How would I generate the same sha3 hash using ethereumjs-abi as in this Solidity example?
contract Contract {
function Test() constant returns(bytes32) {
uint[] memory amounts = new uint[](2);
amounts[0] = 1;
amounts[1] = 2;
bytes8[] memory tickers = new bytes8[](2);
tickers[0] = "BTC";
tickers[1] = "LTC";
// 0x4282aea708b799612aa9d311309ef4b8eb1374cf0740c3e9d5914cb5ce9b93f2
return sha3(amounts, tickers);
}
}
My attempt gives an error of Cannot read property 1 of undefined
:
abi.soliditySHA3(['uint[]', 'bytes8[]'], [[1,2], ['BTC', 'LTC']])
Issue Analytics
- State:
- Created 7 years ago
- Comments:16 (10 by maintainers)
Top Results From Across the Web
Do the tight variable packing rules apply to arrays in structs?
According to the docs, struct types and array types will always pack the elements tightly to save storage space. You can read about...
Read more >Layout of State Variables in Storage - Solidity
Structs and array data always start a new slot and their items are packed tightly according to these rules.
Read more >How an array is "tightly packed" in Solidity - Stack Overflow
I am trying to create a hash (sha256) of an array but I don't understand how it works. for example : pragma solidity...
Read more >Gas Optimization in Solidity Part I: Variables - Medium
Variable packing only occurs in storage — memory and call data does not get packed. You will not save space trying to pack...
Read more >Tight Variable Packing | solidity-patterns - GitHub Pages
Function parameters or dynamically-sized arrays do not benefit from it. On the contrary, as seen in the contract creation costs in the Gas...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@alex-forshtat-tbk thanks, very helpful! I will try to use the “Update branch” function from GitHub and manually resolve conflicts in the next couple of days, since the original author of the PR is not responsive anymore (which is no wonder 😛).
For the time being (as @djrtwo explained) you can do the following:
and in JS…