This post highlights how to use CREATE2
to implement smart contract state using contract data. However, the author also says: "if storage doesn’t need to change, you can do away with metamorphic contracts entirely and just deploy static contracts with runtime code that contains the necessary data". How would this be done in Solidity/assembly? (I have a project where I have a lot of immutable data that needs to be accessed on-chain, so logs aren't viable.)
This is how I'm trying to set the data.
function setData(bytes memory _bytecode) private returns (address) {
address addr;
uint256 byteSize;
assembly {
byteSize := mload(_bytecode)
addr := create(0, add(_bytecode, 0x20), mload(_bytecode))
if iszero(extcodesize(addr)) {
revert(0, 0)
}
}
return addr;
}
Then to retrieve the data, I'm using the block here from the Solidity documentation.
Finally, I'm running this code in the constructor to retrieve the original data:
bytes memory myData = bytes("Hello world!");
address addr = setData(myData);
bytes memory _myData = at(addr);
Alas, setData(myData)
returns (Hardhat):
Error: Transaction reverted without a reason
The reversion is happening at revert(0, 0)
. If I remove this line from the assembly block, I get TransactionExecutionError: Transaction ran out of gas
.