Skip to main content

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

NameTypeDescription
router_contractstext[]An array of Uniswap V3 router contract addresses to retrieve exchange rates for.
start_timetimestamptzThe starting point for the data search. Only swaps that occurred at or after this time will be considered. Defaults to 7 days ago.
end_timetimestamptzThe 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:

NameTypeDescription
transaction_hashtextThe hash of the swap transaction the exchange rate was derived from
consensus_timetimestamptzThe time at which the exchange rate was recorded.
participanttextThe address of the initiator of the swap
token_1textThe address of the first token in the exchange.
token_2textThe address of the second token in the exchange.
token_1_valnumericThe value of the first token in the exchange, converted to the appropriate decimal places.
token_2_valnumericThe value of the second token in the exchange, converted to the appropriate decimal places.
exchange_ratenumericThe exchange rate between the two tokens, calculated by dividing the value of token 1 by the value of token 2.
raw_token_1_valnumericThe raw value of the first token in the exchange.
raw_token_2_valnumericThe raw value of the second token in the exchange.
raw_exchange_ratenumericThe 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_addresstextThe address of the first token in the exchange.
token_2_addresstextThe 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]');