-
Notifications
You must be signed in to change notification settings - Fork 6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
abi.encodePacked for tightly-packed arrays #8441
![:rage4: :rage4:](https://github.githubassets.com/images/icons/emoji/rage4.png)
Comments
The functio |
![:rage4: :rage4:](https://github.githubassets.com/images/icons/emoji/rage4.png)
@chriseth That would be awesome, thanks! |
I've found a workaround for this but It requires the BytesLib.sol. Since pragma solidity ^0.8.0;
import "https://github.com/GNSPS/solidity-bytes-utils/blob/master/contracts/BytesLib.sol";
contract Test {
using BytesLib for bytes;
/**
* Workaround for the `https://github.com/ethereum/solidity/issues/8441`.
* @notice Requires `BytesLib.sol` at `https://github.com/GNSPS/solidity-bytes-utils/blob/master/contracts/BytesLib.sol`
*/
function encodeTightlyPacked(address[] memory path) internal pure returns(bytes memory encoded) {
for (uint i = 0; i < path.length; i++) {
encoded = encoded.concat(
abi.encodePacked(path[i])
);
}
}
function test() public view returns (bytes memory t1, bytes memory t2) {
address[] memory path = new address[](3);
path[0] = 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D;
path[1] = 0xE592427A0AEce92De3Edee1F18E0157C05861564;
path[2] = 0xaD6D458402F60fD3Bd25163575031ACDce07538D;
t1 = abi.encodePacked(path[0], path[1], path[2]);
t2 = encodeTightlyPacked(path);
}
} The result;
|
@tarik0 the language supports uint len = arr.length;
for (uint i = 0; i < len; i++) {
encoded = bytes.concat(
encoded,
abi.encodePacked(path[i])
);
} It is not super optimal as it will waste memory quite a bit. |
My current workaround (very verbose, but generic and decently gas-efficient): On a similar note, an |
This issue has been marked as stale due to inactivity for the last 90 days. |
Hi everyone! This issue has been automatically closed due to inactivity. |
![:rage4: :rage4:](https://github.githubassets.com/images/icons/emoji/rage4.png)
Abstract
Currently
abi.encodePacked
adds padding to arrays.abi.encode
can be used for that.Please change
abi.encodePacked
so that it returns bytes of arrays without padding. I ran into a situation whereabi.encodePacked
would have been really useful if it did not pad arrays with zeros.It would be great if the following was true:
The text was updated successfully, but these errors were encountered: