Reading Asset Balance
As noted in Best Practices, the AssetSink's available asset balance is observed in three places:
- the contract address of the
AssetSinkitself - the
UnicornV3Poolcontract addresses - the
UnicornV2Pairliquidity tokens
For example, the releasable USDC balance is USDC.balanceOf(address(assetSink)) + sum of UnicornV3Pool.protocolFees() + sum of UnicornV2Pair.burn() for each USDC-paired pool
Unicorn v3 Pool Protocol Fees
Reading available Unicorn v3 Pool protocol fees requires calling .protocolFees() on each UnicornV3Pool contract
import {IERC20} from "forge-std/interfaces/IERC20.sol";
import {IUnicornV3Pool} from "@unicorn/v3-core/contracts/interfaces/pool/IUnicornV3Pool.sol";
import {IAssetSink} from "@unicorn/phoenix-fees/src/interfaces/IAssetSink.sol";
contract Example {
IAssetSink assetSink = IAssetSink(0x0);
function releaseableBalance(address token, address[] calldata pools)
external
view
returns (uint256)
{
return IERC20(token).balanceOf(address(assetSink)) // [!code hl]
+ _getV3ProtocolFeesUnclaimed(token, pools); // [!code hl]
}
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) = IUnicornV3Pool(pools[i]).protocolFees(); // [!code hl]
// determine if the requested `token` parameter is `pool.token0()` or `pool.token1()`
feesUnclaimed += pool.token0() == token
? token0Unclaimed
: pool.token1() == token
? token1Unclaimed
: 0;
}
}
}
Unicorn v3 does not support native Ether tokens. Protocol fees in ETH are accrued as Wrapped Ether (WETH).
Because unclaimed tokens are stored in Unicorn 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% Fee
- USDC / TOKENA 0.30% Fee
- USDC / TOKENB 0.30% Fee
- USDC / TOKENC 0.30% Fee
- and so on...
To track which pools contain the asset of interest, we recommend to index the PoolCreated
event emitted by the UnicornV3Factory contract
Unicorn v2 Pool Protocol Fees
Unicorn v2 protocol fees are automatically "pushed" to the AssetSink so no additional calls are required to make the assets releasable.
However, Unicorn 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.
- Math
- Solidity
(LP Token Balance / LP Token Total Supply) * Pool Reserves
IERC20 lpToken;
uint256 amount0 = (lpToken.balanceOf(address(assetSink)) * IERC20(lpToken.token0()).balanceOf(address(lpToken))) / lpToken.totalSupply();
uint256 amount1 = (lpToken.balanceOf(address(assetSink)) * IERC20(lpToken.token1()).balanceOf(address(lpToken))) / lpToken.totalSupply();
To access the underlying assets, integrators should call IUnicornV2Pair.burn()
(uint256 amount0, uint256 amount1) = IUnicornV2Pair(pool).burn(recipient);