2

I am trying to query the unclaimed fees from an uniswap pool with a script. I am using powershell, but a simple curl request can easily be translated.

There are 2 similar questions on stackexchange, but the answers don't solve my problem unfortunately:

UniSwap V3 API : How to track **unclaimed fees** programmatically?

  • the top answer explains how to make requests to Infura, but I don't see these eth_call requests in my dev tools, nor any requests to infura.

  • the second answer is actually the best option I have until now. It uses the openbank api. The only issue with that, is that you can't specify an individual pool id. You have to specify a wallet address and then you'll get info of ALL pools linked to that wallet. The problem is that you don't have a unique id to make sure you are looking at the right pool (an issue when an owner has multiple similar pools with similar amounts of money in them).

Trying to make sense of Uniswap v3 fees (feeGrowthInside0LastX128, feeGrowthGlobal0X128)

  • this uses the "TheGraph" api, but the calculations mentioned in the answers don't even get close to the results shown on Uniswap itself.

Code snippet:

$UrlTheGraph = "https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v3"   
$thegraphBody = @'
{
    "query": 
        "{ position(id: $pool_id ) { owner { id } liquidity token0 { symbol decimals } token1 { symbol decimals } transaction { id } depositedToken0 depositedToken1 withdrawnToken0 withdrawnToken1 collectedFeesToken0 collectedFeesToken1 feeGrowthInside0LastX128 feeGrowthInside1LastX128 pool { liquidity feeGrowthGlobal0X128 feeGrowthGlobal1X128 } tickLower { feeGrowthOutside0X128 feeGrowthOutside1X128 } tickUpper { feeGrowthOutside0X128 feeGrowthOutside1X128 } } }",
    "variables":
        null
}
'@

function Get-PositionInfo {
    param (
        $pool_id
    )
    $body = $ExecutionContext.InvokeCommand.ExpandString($thegraphBody)
    $temp = Invoke-RestMethod -Body $body -Uri $UrlTheGraph -Method Post
    $temp.data.position
}

$temp= Get-PositionInfo -pool_id 148048

[bigint]$feetoken0 = ([bigint]($temp.pool.feeGrowthGlobal0X128 - $temp.tickLower.feeGrowthOutside0X128 - $temp.tickUpper.feeGrowthOutside0X128 - $temp.feeGrowthInside0LastX128) / [bigint]([math]::Pow(2, 128)) * [bigint]$temp.liquidity / (1 * [math]::Pow(10, $temp.token0.decimals)))
[bigint]$feetoken1 = ([bigint]($temp.pool.feeGrowthGlobal1X128 - $temp.tickLower.feeGrowthOutside1X128 - $temp.tickUpper.feeGrowthOutside1X128 - $temp.feeGrowthInside1LastX128) / [bigint]([math]::Pow(2, 128)) * [bigint]$temp.liquidity / (1 * [math]::Pow(10, $temp.token1.decimals)))
$feetoken0
$feetoken1

(the BigInt's where necessary as the numbers got too big)

The pool in the code example currently has 18.55$ in unclaimed fees, made of 7.94 USDC and 0.002503 ETH. With my script, I get 2 times 0 back in this case. I tried different pools (other currencies, higher numbers too), but the results are never close to what the Uniswap interface shows.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.