exchange_rates
Schema: lib_uniswap_v3
Description
This function allows users to retrieve exchange rates for all pairs traded on the specified Uniswap V3 router contracts between a given start and end time.
Usage
Signature
lib_uniswap_v3.exchange_rates(
router_contracts text[],
start_time timestamptz,
end_time timestamptz
)
Parameters
Name | Type | Description |
---|---|---|
router_contracts | text[] | An array of Uniswap V3 router contract addresses to retrieve exchange rates for. |
start_time | timestamptz | The starting point for the data search. Only swaps that occurred at or after this time will be considered. Defaults to 7 days ago. |
end_time | timestamptz | The endpoint for the data search. Only swaps that occurred at or before this time will be included. Defaults to the current time. |
Results
The function returns a table with the following columns:
Name | Type | Description |
---|---|---|
transaction_hash | text | The hash of the swap transaction the exchange rate was derived from |
consensus_time | timestamptz | The time at which the exchange rate was recorded. |
participant | text | The address of the initiator of the swap |
token_1 | text | The address of the first token in the exchange. |
token_2 | text | The address of the second token in the exchange. |
token_1_val | numeric | The value of the first token in the exchange, converted to the appropriate decimal places. |
token_2_val | numeric | The value of the second token in the exchange, converted to the appropriate decimal places. |
exchange_rate | numeric | The exchange rate between the two tokens, calculated by dividing the value of token 1 by the value of token 2. |
raw_token_1_val | numeric | The raw value of the first token in the exchange. |
raw_token_2_val | numeric | The raw value of the second token in the exchange. |
raw_exchange_rate | numeric | The raw exchange rate between the two tokens, calculated by dividing the raw value of token 1 by the raw value of token 2. |
token_1_address | text | The address of the first token in the exchange. |
token_2_address | text | The address of the second token in the exchange. |
Examples
Get the on-chain USD value of a token based on trading with USDT on the WAGMI DEX on Kava
Replace [token]
with the address of the token traded with USDT on WAGMI
with wagmi_routers as (
select account, alias
from lib.address_metadata_for_company('WAGMI', 'AMM Router')
where context->>'chain' = 'kava'
),
wagmi_router_addresses as (
select array_agg(account) as addresses
from wagmi_routers
)
select
format('%s -> %s', token_1, token_2) as metric_name,
consensus_time as time,
exchange_rate as val,
token_2_address
from lib_uniswap_v3.exchange_rates(
(select addresses from wagmi_router_addresses),
now() - '7 days'::interval,
now()
)
where token_1_address = '0x919c1c267bc06a7039e03fcc2ef738525769109c' -- USDT on Kava
and token_2_address = lower('[token]');