0

In the code bellow when calling foo the storage of TestContract gets not changed. Expected behaviour to my understanding is that state.a is set to 5. The arg passed to the library function foo is a pointer to TestContract storage, and the delegatecall acts in the context of TestContract so it should change the state.a to my understanding. Why is this not the case? (ps: the lib address was correctly set with setLib)

pragma solidity >=0.7.0 <0.9.0;


contract TestContract {
    address public adapterLib;
    struct State {
        uint a;
    }
    State public state;

    function setLib(address lib)public{
        adapterLib = lib;
    }

    function foo()public {
        (bool success, bytes memory returnData) = adapterLib.delegatecall(
                abi.encodeWithSignature('foo(State)', state)
            );
    }
    
}

library Test{
    struct State {
        uint a;
    }
    function foo(State storage state)external{
        state.a = 5;
    }
}
3
  • I don't think that's how you get the signature of a function that takes a custom data type as input. Check if the call reverts, if it does, that's why.
    – Foxxxey
    Nov 6, 2022 at 22:38
  • Hey indeed it reverts. Thanks! How do you pass custom input types to encode with signature?
    – FreddyC
    Nov 7, 2022 at 8:20
  • your structs are declared in different positions on the contracts. they need to match up so that the storage layout of the 'shared' variables is in the same position in both contracts. i.e. State is declared 1st in both contracts
    – sola24
    Nov 7, 2022 at 11:49

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.