LimeChain / matchstick

🔥 Unit testing framework for Subgraph development on The Graph protocol. ⚙️
MIT License
207 stars 17 forks source link

Cannot return struct from createMockedFunction #376

Closed RayXpub closed 1 year ago

RayXpub commented 1 year ago

Hi,

I am trying to mock a function call returning a struct type in solidity but I can't seem to be able to find the correct return ethereum.Value syntax.

example struct :

struct Test {
    address a;
    uint96 b;
    uint256 c;
    uint256 d;
}

mock function :

createMockedFunction(
      contractAddress,
      "getStruct",
      "getStruct(uint256):((address,uint96,uint256,uint256))"
    ) 
      .withArgs([ethereum.Value.fromUnsignedBigInt(id)])
      .returns(?);

Is there a way to achieve this or are struct return types no supported ?

dimitrovmaksim commented 1 year ago

Hey, there's no way to convert struct to ethereum.Value, and i think graph-cli generally converts structs into tuples. You can try returning a tuple. Here's how you can create a tuple https://github.com/LimeChain/demo-subgraph/blob/main/tests/gravity/gravity.test.ts#L32-L34

RayXpub commented 1 year ago

@dimitrovmaksim Thanks for the answer. I indeed managed to do so by using tuples as follow :

    let callResult = new ethereum.Tuple();
    callResult.push(ethereum.Value.fromAddress(a));
    callResult.push(ethereum.Value.fromUnsignedBigInt(b));
    callResult.push(ethereum.Value.fromUnsignedBigInt(c));
    callResult.push(ethereum.Value.fromUnsignedBigInt(d));

    createMockedFunction(
      contractAddress,
      "getStruct",
      "getStruct(uint256):((address,uint96,uint256,uint256))"
    ) 
      .withArgs([ethereum.Value.fromUnsignedBigInt(id)])
      .returns([ethereum.Value.fromTuple(callResult)]);