I need to encode a bunch of variables that are part of an EIP712 message. Unfortunately I hit the stack too deep exception when I added hashParent(swap.parent)
:(
bytes32 swapHash = keccak256(
abi.encode(
SWAP_TYPEHASH,
swap.scriptId,
swap.tokenFrom,
swap.tokenTo,
swap.amount,
swap.user,
swap.executor,
swap.chainId,
hashBalance(swap.balance),
hashFrequency(swap.frequency),
hashPrice(swap.price),
hashRepetitions(swap.repetitions),
hashParent(swap.parent)
)
);
CompilerError: Stack too deep, try removing local variables.
44 | hashParent(swap.parent)
| ^^^^
Error HH600: Compilation failed
My first idea was to split the abi.encode
in two, but I see that abi.encode('a', 'b')
is not the same as abi.encode(abi.encode('a'), abi.encode('b'))
.
Is there a way to go around this limitation?
Edit:
I've made a pastebin with some example code that can be played with on Remix. the idea is to find a way to write hash2
in a way to return the same result of hash
, without passing that many arguments to abi.encode
keccak256(abi.encode(swap))
. If swap has other fields you don't want you could create another struct that only has needed fields.