Skip to main content

UniswapV2PoolTokenPrice

Git Source

Inherits: PriceSubmodule

Title: UniswapV2PoolTokenPrice

Author: 0xJem

forge-lint: disable-start(mixed-case-function,mixed-case-variable)

Provides prices derived from a Uniswap V2 pool

State Variables

MAX_DECIMALS

Any token or pool with a decimal scale greater than this would result in an overflow

UniswapV2 uses uint112 to store token balances. Token decimals over this number will result in truncated balances.

uint8 internal constant MAX_DECIMALS = 26

BALANCES_COUNT

The number of balances expected to be in the pool

uint256 internal constant BALANCES_COUNT = 2

POOL_PARAMS_LENGTH

The expected length of the encoded pool parameters (1 address = 32 bytes)

uint256 internal constant POOL_PARAMS_LENGTH = 32

Functions

constructor

constructor(Module parent_) Submodule(parent_);

SUBKEYCODE

20 byte identifier for the submodule. First 5 bytes must match PARENT().

function SUBKEYCODE() public pure override returns (SubKeycode);

VERSION

function VERSION() public pure override returns (uint8 major, uint8 minor);

_getTokens

Returns the tokens of a UniswapV2 pool in an array

function _getTokens(IUniswapV2Pair pool_) internal view returns (address[] memory);

Parameters

NameTypeDescription
pool_IUniswapV2PairUniswapV2 pool

Returns

NameTypeDescription
<none>address[]address[] Array of length 2 containing token addresses

_getReserves

Returns the reserves of a UniswapV2 pool

This function reverts if the pool does not have the getReserves() function.

function _getReserves(IUniswapV2Pair pool_) internal view returns (uint112[] memory);

Parameters

NameTypeDescription
pool_IUniswapV2PairUniswapV2 pool

Returns

NameTypeDescription
<none>uint112[]uint112[] Reserves of the pool in their native decimals

_convertERC20Decimals

Converts the given value from the ERC20 token's decimals to outputDecimals_

This function will revert if:

  • Converting the token's decimals would result in an overflow.
function _convertERC20Decimals(uint112 value_, address token_, uint8 outputDecimals_)
internal
view
returns (uint256);

Parameters

NameTypeDescription
value_uint112Value in native ERC20 token decimals
token_addressThe address of the ERC20 token
outputDecimals_uint8The resulting number of decimals

Returns

NameTypeDescription
<none>uint256uint256 Value in the scale of outputDecimals_

getPoolTokenPrice

Determines the unit price of the pool token for the UniswapV2 pool specified in params_.

The pool token price is determined using the "fair LP pricing" described here: https://cmichel.io/pricing-lp-tokens/

This approach is implemented in order to reduce the susceptibility to manipulation of the pool token price

through the pool's reserves.

function getPoolTokenPrice(address, uint8 outputDecimals_, bytes calldata params_) external view returns (uint256);

Parameters

NameTypeDescription
<none>address
outputDecimals_uint8The number of output decimals (assumed to be the same as PRICE decimals)
params_bytesUniswapV2 pool parameters of type UniswapV2PoolParams

Returns

NameTypeDescription
<none>uint256uint256 Price in the scale of outputDecimals_

getTokenPrice

Determines the spot price of the specified token from the UniswapV2 pool specified in params_

It does this by:

  • Determining the price and reserves of the token paired with lookupToken_

  • Determining the corresponding price of lookupToken_

NOTE: as the reserves of UniswapV2 pools can be manipulated using flash loans, the spot price

can also be manipulated. Price feeds are a preferred source of price data. Use this function with caution.

function getTokenPrice(address lookupToken_, uint8 outputDecimals_, bytes calldata params_)
external
view
returns (uint256);

Parameters

NameTypeDescription
lookupToken_addressThe token to determine the price of
outputDecimals_uint8The number of output decimals (assumed to be the same as PRICE decimals)
params_bytesUniswapV2 pool parameters of type UniswapV2PoolParams

Returns

NameTypeDescription
<none>uint256uint256 Price in the scale of outputDecimals_

Errors

UniswapV2_ParamsInvalid

The provided parameters are invalid

error UniswapV2_ParamsInvalid(bytes params_);

Parameters

NameTypeDescription
params_bytesThe encoded parameters

UniswapV2_AssetDecimalsOutOfBounds

The decimals of the asset are out of bounds

error UniswapV2_AssetDecimalsOutOfBounds(address asset_, uint8 assetDecimals_, uint8 maxDecimals_);

Parameters

NameTypeDescription
asset_addressThe address of the asset
assetDecimals_uint8The number of decimals of the asset
maxDecimals_uint8The maximum number of decimals allowed

UniswapV2_LookupTokenNotFound

The lookup token was not found in the pool

error UniswapV2_LookupTokenNotFound(address pool_, address asset_);

Parameters

NameTypeDescription
pool_addressThe address of the pool
asset_addressThe address of the asset

UniswapV2_OutputDecimalsOutOfBounds

The output decimals are out of bounds

error UniswapV2_OutputDecimalsOutOfBounds(uint8 outputDecimals_, uint8 maxDecimals_);

Parameters

NameTypeDescription
outputDecimals_uint8The number of decimals to return the price in
maxDecimals_uint8The maximum number of decimals allowed

UniswapV2_PoolTokenBalanceInvalid

The token balance of a pool is invalid

error UniswapV2_PoolTokenBalanceInvalid(address pool_, uint8 balanceIndex_, uint256 balance_);

Parameters

NameTypeDescription
pool_addressThe address of the pool
balanceIndex_uint8The index of the balance
balance_uint256The balance of the token

UniswapV2_PoolBalancesInvalid

The pool balances are invalid

error UniswapV2_PoolBalancesInvalid(address pool_, uint256 balanceCount_, uint256 expectedBalanceCount_);

Parameters

NameTypeDescription
pool_addressThe address of the pool
balanceCount_uint256The number of balances returned by the pool
expectedBalanceCount_uint256The number of balances expected

UniswapV2_ParamsPoolInvalid

The pool specified in the parameters is invalid

error UniswapV2_ParamsPoolInvalid(uint8 paramsIndex_, address pool_);

Parameters

NameTypeDescription
paramsIndex_uint8The index of the parameter
pool_addressThe address of the pool

UniswapV2_PoolSupplyInvalid

The total supply returned by the pool is invalid

This currently only occurs if the total supply is 0

error UniswapV2_PoolSupplyInvalid(address pool_, uint256 supply_);

Parameters

NameTypeDescription
pool_addressThe address of the pool
supply_uint256The total supply returned by the pool

UniswapV2_PoolTokensInvalid

The pool tokens are invalid

error UniswapV2_PoolTokensInvalid(address pool_, uint256 tokenIndex_, address token_);

Parameters

NameTypeDescription
pool_addressThe address of the pool
tokenIndex_uint256The index of the token
token_addressThe address of the token

UniswapV2_PoolTypeInvalid

The pool is invalid

This is triggered if the pool reverted when called,

and indicates that the feed address is not a UniswapV2 pool.

error UniswapV2_PoolTypeInvalid(address pool_);

Parameters

NameTypeDescription
pool_addressThe address of the pool

Structs

UniswapV2PoolParams

UniswapV2 pool parameters

struct UniswapV2PoolParams {
IUniswapV2Pair pool;
}

Properties

NameTypeDescription
poolIUniswapV2PairAddress of the UniswapV2 pool