Search code examples
solidityremixdecentralized-applications

Get pool balances from BalancerV2 in Solidity


I am trying to inspect the pool balances of a decentralized exchange called Balancer. The implementation is done with the main version V2.

I want to get the pool balance with

vault.getPoolTokens(0x5c6ee304399dbdb9c8ef030ab642b10820db8f56000200000000000000000014);

The contract looks like this:

contract BalancerV2 {
    IVault private constant vault = "0xBA12222222228d8Ba445958a75a0704d566BF2C8";

    function getPoolTokens() public view returns(uint) {
        return vault.getPoolTokens(0x5c6ee304399dbdb9c8ef030ab642b10820db8f56000200000000000000000014);
    }
    
}

But it gives me the following error message:

from solidity:
contracts/BalancerV2.sol:8:37: TypeError: Type literal_string "0xBA12222222228d8Ba445958a75a0704d566BF2C8" is not implicitly convertible to expected type contract IVault.
    IVault private constant vault = "0xBA12222222228d8Ba445958a75a0704d566BF2C8";
                                    ^------------------------------------------^

My question is:

How is it possible that even in the official GitHub repository they have a working example of how to initialize the vault with solidity compiler version 0.7.0 and I'm literally trying the same with getting this error?


By the way, the example in the documentation has a typo so it won't compile. Just in case if someone from the devs sees this.

https://dev.balancer.fi/resources/pool-interfacing#pool-balances

I am trying to compile this contract and am expecting to get this result:

tokens:  [0xba100000625a3754423978a60c9317c58a424e3D,
                0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2]
                
balances:  [5720903090084350251216632,
                7939247003721636150710]

Solution

  • Instead of:

    IVault private constant vault = "0xBA12222222228d8Ba445958a75a0704d566BF2C8";
    

    try this:

    IVault private constant vault = IVault(0xBA12222222228d8Ba445958a75a0704d566BF2C8);