From 965df4b8482c2986279588cab09de541f7d66a94 Mon Sep 17 00:00:00 2001 From: Thrilok Kumar Date: Thu, 9 Jul 2020 00:13:34 +0530 Subject: [PATCH] Added getPosition function for uniswap --- contracts/protocols/uniswapV2.sol | 45 +++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/contracts/protocols/uniswapV2.sol b/contracts/protocols/uniswapV2.sol index 362a213..2b11a37 100644 --- a/contracts/protocols/uniswapV2.sol +++ b/contracts/protocols/uniswapV2.sol @@ -259,6 +259,51 @@ contract Resolver is UniswapHelpers { slippage ); } + + struct TokenPair { + TokenInterface tokenA; + TokenInterface tokenB; + } + + struct PoolData { + uint tokenAShare; + uint tokenBShare; + uint uniAmt; + uint totalSupply; + } + + function getPosition( + address owner, + TokenPair[] memory tokenPairs + ) public view returns (PoolData[] memory) + { + IUniswapV2Router02 router = IUniswapV2Router02(getUniswapAddr()); + uint _len = tokenPairs.length; + PoolData[] memory poolData = new PoolData[](_len); + for (uint i = 0; i < _len; i++) { + TokenInterface tokenA = tokenPairs[i].tokenA; + TokenInterface tokenB = tokenPairs[i].tokenB; + address exchangeAddr = IUniswapV2Factory(router.factory()).getPair( + address(tokenA), + address(tokenB) + ); + if (exchangeAddr != address(0)) { + TokenInterface uniToken = TokenInterface(exchangeAddr); + uint uniAmt = uniToken.balanceOf(owner); + uint totalSupply = uniToken.totalSupply(); + uint share = wdiv(uniAmt, totalSupply); + uint amtA = wmul(tokenA.balanceOf(exchangeAddr), share); + uint amtB = wmul(tokenB.balanceOf(exchangeAddr), share); + poolData[i] = PoolData( + amtA, + amtB, + uniAmt, + totalSupply + ); + } + } + return poolData; + } }