banteg / multicall.py

aggregate results of multiple smart contract calls into one
MIT License
251 stars 108 forks source link

Need help: How do I use multicall with this method? #40

Closed prabhatverma286 closed 2 years ago

prabhatverma286 commented 2 years ago

Hi, I'm trying to use multicall to fetch balancer pool balances.

The method to fetch pool balances seems to be: https://etherscan.io/address/0xba12222222228d8ba445958a75a0704d566bf2c8#readContract#F10

I'm struggling with the parsing of the return values - I could not find an example of decoding return values of type lists. To the best of my understanding, I've been trying to use multicall using the following Call method:

Call('0xBA12222222228d8Ba445958a75a0704d566BF2C8', ['getPoolTokens(bytes32)(((address),(uint256),uint256))', bytes(pool_id_here)],
[["reserves", None]])

however, this returns the following data -

(('0x0000000000000000000000000000000000000060',), (192,), 15238955)

which is of course, incorrect.

What is the correct function signature to use here?

DefiDebauchery commented 2 years ago

You have a couple of issues in your invocation:

First, the signature is incorrect; not only are you making a tuple out of the entire return with your extra parentheses, the individual return types are not denoted as []. You can build the function signature from the ABI:

  {
    "inputs": [
      {
        "internalType": "bytes32",
        "name": "poolId",
        "type": "bytes32"
      }
    ],
    "name": "getPoolTokens",
    "outputs": [
      {
        "internalType": "contract IERC20[]",
        "name": "tokens",
        "type": "address[]"
      },
      {
        "internalType": "uint256[]",
        "name": "balances",
        "type": "uint256[]"
      },
      {
        "internalType": "uint256",
        "name": "lastChangeBlock",
        "type": "uint256"
      }
    ],
    "stateMutability": "view",
    "type": "function"
  },

This gives us getPoolTokens(bytes32)(address[],uint256[],uint256)

Second, because this method has three return values, you should be listing three keys for the return dict (unless you really do only care about the first one, of course)

Taking the first Pool ID I found in event logs, the following call (edited for readability) yielded the same data that etherscan's UI provided:

Call(
  '0xBA12222222228d8Ba445958a75a0704d566BF2C8',
  ['getPoolTokens(bytes32)(address[],uint256[],uint256)', HexBytes('0x8eb6c82c3081bbbd45dcac5afa631aac53478b7c000100000000000000000270')],
  [['tokens', None], ['balances', None], ['lastChangeBlock', None]],
  _w3=eth
)()

{'tokens': ('0x2260fac5e5542a773aa44fbcfedf7c193bc2c599',
  '0x798d1be841a82a273720ce31c822c61a67a601c3',
  '0xba485b556399123261a5f9c95d413b4f93107407'),
 'balances': (16480387972, 323875735362, 581643160963723873429632),
 'lastChangeBlock': 15293254}
prabhatverma286 commented 2 years ago

Thanks @DefiDebauchery! I think I did not understand the way to use the library properly. Indeed, in the end, I ended up with exactly the same solution as you have written.

I remember going through the examples but couldn't find a nice one where the return types are multiple lists. Maybe this could be added to the example usages.