I need to hardcode some multi-Kb bytes
into my smart contract. So far, the best way I have found to do this is to use abi.encodePacked
...
return abi.encodePacked(
abi.encodePacked(
uint256(0x052518b10006000745055415414ffe41524faa415e4f5b41764e5841c44d4341),
uint256(0xc44c4041764bf2415e4b9e41524b46415449b2415e485b42a84842443c482646),
uint256(0x09499347874b5a47870031903190000000000004c2a476c14d3c47224e5f4722),
uint256(0x4f72476c00319031900000000000050414787152084787537646095359443c53),
uint256(0x4042a851ea415e5055415490005251ffbad36000544dce44d514d1b44d54c724),
uint256(0x4f14be0452c4b0c45824a66461c4a1347044a7747314ae6474a4b5a474a25041),
uint256(0x150b6474a5125473151884704513f463550b445a55001454b4f6044fb4e9e44d),
uint256(0x54dce44d5900057504034124424434440a6000745055417914ffe41774faa418),
uint256(0x34f5b419b4e5841e94d4341e94c40419b4bf241834b9e41774b46417949b2418),
uint256(0x3485b42cd484244604826462d499347ac4b5a47ac0031903190000000000004c),
uint256(0x2a479114d3c47474e5f47474f724791003190319000000000000504147ac1520),
uint256(0x847ac5376462e53594460534042cd51ea4183505541799000516000744ce33fb),
uint256(0x04baa3e7714ad93da64b6d3c404c953c4024f061502e3c4050c23da64ff13e77)
), abi.encodePacked(
uint256(0x34eb83fb014e3740314d6540314ce33fb090003a50403411e424434440a0307c),
uint256(0x70606370407c707069400)
)
);
As you can see, this suffers from multiple problems. First, each call to abi.encodePacked
can only accept 13 arguments before the callstack blows up (thus the multiple calls). Next, it adds a bunch of overhead. There are 479
bytes encoded in the above data, but the size added to the contract is 589
bytes.
FWIW, I also tried to directly encode the bytes as a string (e.g., string("")
). However, I do not believe it is possible to write such strings (e.g., OÜ/5+ãaR¾.RÁ-ÎRÁ-958,GÊ,G·-G·-G·-ÎG».GÀ.64¤2ï1JS.õJc.øJs.úKc/LU/.ME/.MÎ/.NW/(NÞ/O3/O/
) in a smart contract.
Is there a way to encode this data without suffering the ~20% overhead described above?
bytes constant x = hex"12341234";
to encode raw bytes.