Reading Asset Balances
Read releasable asset balances across TokenJar, Uniswap v2 LP tokens, and Uniswap v3 pools.
This guide also covers Uniswap v4Â pools.
The TokenJar's available asset balance is observed in these places:
- the contract address of the TokenJar itself
- the UniswapV3Pool contract addresses
- the UniswapV2Pair liquidity tokens
- the PoolManager contract, for Uniswap v4Â pools
For example, the releasable USDC balance is USDC.balanceOf(address(tokenJar)) + sum of UniswapV3Pool.protocolFees() + sum of UniswapV2Pair.burn() for each USDC-paired pool.
For v4 pools, add PoolManager.protocolFeesAccrued(USDC), which already covers every v4Â pool.
Uniswap v3 Pool Protocol Fees
Reading available Uniswap v3 Pool protocol fees requires calling .protocolFees() on each UniswapV3Pool contract.
import {IERC20} from "forge-std/interfaces/IERC20.sol";
import {IUniswapV3Pool} from "@uniswap/v3-core/contracts/interfaces/pool/IUniswapV3Pool.sol";
import {ITokenJar} from "@uniswap/phoenix-fees/src/interfaces/ITokenJar.sol";
contract Example {
ITokenJar tokenJar = ITokenJar(address(0));
function releaseableBalance(address token, address[] calldata pools)
external
view
returns (uint256)
{
return IERC20(token).balanceOf(address(tokenJar))
+ _getV3ProtocolFeesUnclaimed(token, pools);
}
function _getV3ProtocolFeesUnclaimed(address token, address[] calldata pools)
internal
view
returns (uint256 feesUnclaimed)
{
uint128 token0Unclaimed;
uint128 token1Unclaimed;
for (uint256 i; i < pools.length; i++) {
(token0Unclaimed, token1Unclaimed) = IUniswapV3Pool(pools[i]).protocolFees();
// determine if the requested `token` parameter is `pool.token0()` or `pool.token1()`
feesUnclaimed += IUniswapV3Pool(pools[i]).token0() == token
? token0Unclaimed
: IUniswapV3Pool(pools[i]).token1() == token
? token1Unclaimed
: 0;
}
}
}Uniswap v3 does not support native Ether tokens. Protocol fees in ETH are accrued as Wrapped Ether (WETH).
Because unclaimed tokens are stored in Uniswap v3 Pool contracts, integrators should use offchain indexing to track which pools contain the asset of interest. For example, USDC protocol fees are accrued in many different pools: USDC/TOKENA 0.05%, USDC/TOKENA 0.30%, USDC/TOKENB 0.30%, and so on.
To track which pools contain the asset of interest, we recommend indexing the PoolCreated event emitted by the UniswapV3Factory contract.
Uniswap v4 Pool Protocol Fees
Similar to v3, Uniswap v4 protocol fees accrue directly on the PoolManager rather than being pushed automatically. Reading a pool's unclaimed v4 protocol fees uses PoolManager.protocolFeesAccrued(Currency currency), which returns the unclaimed amount for that currency across every pool using it. It is keyed by currency rather than by individual pool, so no per-pool offchain indexing is needed the way v3Â requires.
Uniswap v2 Pool Protocol Fees
Uniswap v2 protocol fees are automatically "pushed" to the TokenJar so no additional calls are required to make the assets releasable. However, Uniswap v2 protocol fees are accrued in the form of liquidity tokens (LP tokens) which are redeemable for underlying assets. Ownership of the LP token represents the proportional share of the pool's assets.
(LP Token Balance / LP Token Total Supply) * Pool Reserves
IUniswapV2Pair lpToken;
uint256 amount0 = (lpToken.balanceOf(address(tokenJar)) * IERC20(lpToken.token0()).balanceOf(address(lpToken))) / lpToken.totalSupply();
uint256 amount1 = (lpToken.balanceOf(address(tokenJar)) * IERC20(lpToken.token1()).balanceOf(address(lpToken))) / lpToken.totalSupply();To access the underlying assets, integrators should call IUniswapV2Pair.burn().
(uint256 amount0, uint256 amount1) = IUniswapV2Pair(pool).burn(recipient);