【发布时间】:2023-01-30 07:28:36
【问题描述】:
我想使用一些通用调用来附加一些数据。可以说我有一个看起来像这样的函数:
function doWork(uint256 number, string name) {
//...
}
function someOther (uint256 number, string name, (uint256 votes, bytes[] data)[]) {
//...
}
我正在使用 number 和 name 属性在打字稿中构建调用数据,如下所示:
defaultAbiCoder.encode([abi], [value, name]);
现在,我想将数据一般地附加到每个调用中。我很确定如果在函数调用中传递额外数据它不会抛出。
通常这会像这样执行:
function _execute(
address target,
uint256 value,
bytes calldata data
) internal virtual {
(bool success, ) = target.call{value: value}(data);
require(success, "TLC: underlying transaction reverted");
}
我正在尝试像这样对附加数据进行编码:
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;
}
我认为这不是附加调用数据的正确方法。我如何一般地附加 calldata 以便 (uint256 votes, bytes[] data)[] 附加到每个函数调用?
【问题讨论】:
标签: generics encoding solidity