Categories
Cryptocurrency Ethereum Mastering Development

Passing storage of one library to another library

I have two libraries: A and B as well as contract C using them.

Library B, in one of its functions, needs to call a couple of functions defined for A.Storage.

The code below compiles, tests are passing, but I have no idea how should I interpret storage modifier when passing library as a parameter: A.Storage storage a. Is this setup safe? Am I referencing the memory of contract C and not creating a copy of A.Storage?

From what I tested, passing A.Storage memory a does not work, the compiler complains:

TypeError: Member "someFuncDefinedForA" is not available in struct A.Storage memory outside of storage.

library A {
  struct Storage { ... }
}

library B {
  using A for A.Storage;

  struct Storage { ... }

  function myFunc(
      Storage storage self,
      A.Storage storage a // <-- ?
  ) { 
    // (...)
    a.someFuncDefinedForA();
  }
}

contract C {
  using A for A.Storage;
  using B for B.Storage;

  A.Storage internal a;
  B.Storage internal b;

  function doIt() public {
    b.myFunc(a);
  }
}

Leave a Reply

Your email address will not be published. Required fields are marked *