Post by @gretzke • Hey
1/5 Working with arbitrary data (bytes) and parsing it on chain in Solidity can be a pain. There is a little known library called TypedMemView that makes
Stats
Actions: 0
Comments: 4
Likes: 0
Mirrors: 0
Quotes: 0
Comments
2/5 Especially when working with cross chain applications (e.g., bridges) a Solidity dev might more often than not work with arbitrary data that can be parsed into multiple different data structures, encoded as `bytes memory`. For example an ERC20, ERC721 or ERC1155 transfer.
3/5 The smart contract does not know what kind of data it's working with. Parsing this data can pose a challenge. Bytes memory cannot index ranges effectively and can't be sliced without copying. And because Solidity never deallocates memory, the gas costs grow exponentially.
4/5 TypedMemView manipulates the stack, instead of the memory itself, making it a lot more flexible and cheaper to use. It can slice and index arbitrary ranges on the stack without copying any data! How does this allow us to build more efficient data parsers with Solidity?
5/5 My repo demonstrates a POC of a bridge contract that can handle ERC20 and NFT transfers, as well as make arbitrary smart contract calls. Using the TypedMemView library, the smart contract can easily parse the encoded data. You can even nest multiple MemViews within each other