1

I'd like to append some data using some generic call in solidity. Lets say I have a function that looks like so:

function doWork(uint256 number, string name) {
   //...
}
 
//Pseudo code the tuple is a struct
function someOther (uint256 number, string name, (uint256 votes, bytes[] data)[] results) {
   //...
}

I'm building the calldata in typescript with just the number and name properties like so:

defaultAbiCoder.encode([abi], [value, name]);

Now in solidity, I'd like to generically append the data to each call. I'm pretty sure if extra data is passed in the function call it will not throw.

Normally this execute like so:

function _execute(
    address target,
    uint256 value,
    bytes calldata data
) internal virtual {
    (bool success, ) = target.call{value: value}(data);
    require(success, "TLC: underlying transaction reverted");
}

I'm trying to encode the additional data like so:

function appendResultsToCalldata(ProposalCore storage proposal) internal virtual returns (bytes[] memory) {
    bytes[] memory combinedData = new bytes[](proposal.calldatas.length);
    for (uint i = 0; i < proposal.calldatas.length; i++) {
        // Calculate the total length of the new combined data
        bytes memory votesData = abi.encode(proposal.votes[i]);
        uint totalLength = proposal.calldatas[i].length + votesData.length + proposal.voteChoices[i].data.length;
        // Create a new byte array with the combined data
        bytes memory data = new bytes(totalLength);

        // Initialize an offset variable to keep track of where we are in the array
        uint offset = 0;
        // Copy the calldata into the combined data array
        for (uint j = 0; j < proposal.calldatas[i].length; j++) {
            data[offset++] = proposal.calldatas[i][j];
        }
        // Convert the vote value to bytes and copy it into the combined data array
        for (uint j = 0; j < votesData.length; j++) {
            data[offset++] = votesData[j];
        }
        // Copy the vote choice data into the combined data array
        for (uint j = 0; j < proposal.voteChoices[i].data.length; j++) {
            data[offset++] = proposal.voteChoices[i].data[j];
        }
        // Add the combined data to the combinedData array
        combinedData[i] = data;
    }

    return combinedData;
}

I don't think this is the proper way to append the calldata. How can I generically append calldata so that (uint256 votes, bytes[] data)[] is appended to each function call?

1 Answer 1

0

check it out: https://blog.emn178.cc/en/post/solidity-gas-optimization-data-compression/#Data-Merging

I read it and found some solutions for merging data.

3
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Sep 21, 2023 at 12:28
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
    – Maka
    Sep 22, 2023 at 3:08
  • Sending in packed abi data is good for calldata optimizations however arbitrary dynamic data doesn't really work. Night be a way if I can deserialize from the end instead
    – Ritzy Dev
    Oct 31, 2023 at 6:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.