DEV Community

Discussion on: Testing Interactions with other Smart Contracts

Collapse
 
royalaid profile image
Mark Aiken • Edited

I am curious how to mock a value like ftmscan.com/address/0x230917f8a262..., line 735, which holds a Mapping of a Mapping to a struct:
mapping (uint256 => mapping (address => UserInfo)) public userInfo;

Collapse
 
austinbv profile image
Austin Vance

Thanks and great question!

Mocking return values is the same, if it's a complex object like arrays or structs or a primitive type. The short answer is you can mock it like this

For userInfo it's a nested map. Nested maps in solidity create a getter functions that have N parameters where N is the number of maps nested. The final return from your getter is

const fake = await smock.fake<Farm>(
    Farm.abi,
    {address: "0x230917f8a262bF9f2C3959eC495b11D1B7E1aFfC"}
  );
fake.userInfo.returns({ amount: 20, rewardDebt: 1 })
Enter fullscreen mode Exit fullscreen mode

The longer answer is Solidity creates getter functions for maps. Each of these functions has N parameters, where N is the number of maps in storage. If you take a look at the ABI for the Farm contract userInfo has two params.

  {
    "constant": true,
    "inputs": [
      {
        "internalType": "uint256",
        "name": "",
        "type": "uint256"
      },
      {
        "internalType": "address",
        "name": "",
        "type": "address"
      }
    ],
    "name": "userInfo",
    "outputs": [
      {
        "internalType": "uint256",
        "name": "amount",
        "type": "uint256"
      },
      {
        "internalType": "uint256",
        "name": "rewardDebt",
        "type": "uint256"
      }
    ],
    "payable": false,
    "stateMutability": "view",
    "type": "function"
  }
Enter fullscreen mode Exit fullscreen mode

This represents a solidity interface

  function userInfo ( uint256, address ) external view returns ( uint256 amount, uint256 rewardDebt );
Enter fullscreen mode Exit fullscreen mode

You can mock the userInfo function to return based on specific parameters or have a single general return value.

Deeper info can be found in their docs smock.readthedocs.io/en/latest/fak...