Struct – How to Call Functions with Large Struct Parameters in Solidity

stackstruct

Let's say I have the following code:

// This is in a separate imported interface.
interface MyInterface{
  struct MyStruct {
    uint256 one;
    uint256 two;
    // . . .
    uint256 sixteen;
  }
}

// This is in a separate contract
function doSomething(MyInterface.MyStruct a) public{

How could I doSomething from another contract? Right now, if I create a struct in my other contract:

MyStruct patrickStruct = (one, two, three, ..., sixteen);

I will get:

Compiler run failed
CompilerError: Stack too deep when compiling inline assembly: Variable value0 is 2 slot(s) too deep inside the stack.

How do I fix this?

Best Answer

You wouldn't be able set the variable in storage, even the whole struct as a variable, you'd have to set individual subsets of the variables into storage. This way they can be popped off the stack.

Related Topic