Skip to main content

Optimism DEX Trades API

If you were using Optimism RPC till now to get data, forget about it.

Our Optimism real time streams are perfect alternative for Optimism web3 subscribe.

In this section we will see how to get Optimism DEX trades information using our GraphQL APIs.

Live DEX swap stream (Optimism)

Crypto Trades API: one row per swap, with USD and supply. Filter Pair.Market.Network: Optimism. When to use this vs chain DEX APIs.

Run this subscription in the Bitquery IDE (open a new tab, paste the subscription below, and run).

subscription {
Trading {
Trades(where: { Pair: { Market: { Network: { is: "Optimism" } } } }) {
Side
Supply {
MaxSupply
TotalSupply
FullyDilutedValuationUsd
CirculatingSupply
MarketCap
}
Trader {
Address
}
TransactionHeader {
Fee
FeePayer
Sender
To
Hash
Index
}
Amounts {
Base
Quote
}
AmountsInUsd {
Base
Quote
}
Block {
Date
Time
Timestamp
}
Pair {
Currency {
Id
Name
Symbol
}
Market {
Address
Program
Network
}
QuoteCurrency {
Id
Name
Symbol
}
Token {
Address
Id
IsNative
Symbol
TokenId
Network
}
QuoteToken {
Address
Id
IsNative
Symbol
TokenId
Network
}
}
Price
PriceInUsd
}
}
}

This query returns the top trending trading pairs on Optimism based on the Trade Volume, and returns info like unique buyers and sellers, number of markets where the pair exist, latest price and price at a given time and much more.

Read DEXTrades vs DEXTradeByTokens vs Trades cube to get a better understanding on when to use which cube.

query pairs(
$min_count: String
$network: evm_network
$time_ago: DateTime
$time_10min_ago: DateTime
$time_1h_ago: DateTime
$time_3h_ago: DateTime
$weth: String!
$usdc: String!
$usdt: String!
$usdc2: String!
) {
EVM(network: $network) {
DEXTradeByTokens(
where: {
Block: { Time: { since: $time_ago } }
any: [
{ Trade: { Side: { Currency: { SmartContract: { is: $usdt } } } } }
{
Trade: {
Side: { Currency: { SmartContract: { is: $usdc } } }
Currency: { SmartContract: { notIn: [$usdt] } }
}
}
{
Trade: {
Side: { Currency: { SmartContract: { is: $usdc2 } } }
Currency: { SmartContract: { notIn: [$usdt, $usdc] } }
}
}
{
Trade: {
Side: { Currency: { SmartContract: { is: $weth } } }
Currency: { SmartContract: { notIn: [$usdc, $usdt, $usdc2] } }
}
}
{
Trade: {
Side: {
Currency: { SmartContract: { notIn: [$usdc, $usdt, $weth] } }
}
Currency: {
SmartContract: { notIn: [$usdc, $usdc2, $usdt, $weth] }
}
}
}
]
}
orderBy: { descendingByField: "usd" }
limit: { count: 100 }
) {
Trade {
Currency {
Symbol
Name
SmartContract
ProtocolName
}
Side {
Currency {
Symbol
Name
SmartContract
ProtocolName
}
}
price_last: PriceInUSD(maximum: Block_Number)
price_10min_ago: PriceInUSD(
maximum: Block_Number
if: { Block: { Time: { before: $time_10min_ago } } }
)
price_1h_ago: PriceInUSD(
maximum: Block_Number
if: { Block: { Time: { before: $time_1h_ago } } }
)
price_3h_ago: PriceInUSD(
maximum: Block_Number
if: { Block: { Time: { before: $time_3h_ago } } }
)
}
dexes: uniq(of: Trade_Dex_OwnerAddress)
amount: sum(of: Trade_Side_Amount)
usd: sum(of: Trade_Side_AmountInUSD)
sellers: uniq(of: Trade_Seller)
buyers: uniq(of: Trade_Buyer)
count(selectWhere: { ge: $min_count })
}
}
}

The example of this could be seen on the DEXRabbit.

Trending Pairs on Optimism

Subscribe to Latest Optimism Trades

This example uses the chain-specific DEXTrades cube via EVM(network: optimism) { DEXTrades } (pool-side Buy/Sell; see DEXTrades cube). USD can be weak on thin pools. For trader + USD swap rows, use the stream at the top.

You can find the query here

subscription {
EVM(network: optimism) {
DEXTrades {
Block {
Time
}
Trade {
Dex {
ProtocolName
ProtocolFamily
SmartContract
}
Buy {
Amount
Buyer
Seller
Currency {
Decimals
Fungible
HasURI
Name
ProtocolName
SmartContract
Symbol
}
OrderId
}
Sell {
Buyer
Seller
Currency {
Decimals
Fungible
HasURI
Name
ProtocolName
SmartContract
Symbol
}
}
}
}
}
}


Get Top Traders on Optimism

This query returns the top traders om Optimism chain based on the number of unique tokens held and number of trades. This also provides info like Buyer Address and Seller Address.

query topTraders($network: evm_network, $time_ago: DateTime) {
EVM(network: $network) {
DEXTradeByTokens(
orderBy: { descendingByField: "trades" }
limit: { count: 100 }
where: { Block: { Time: { since: $time_ago } } }
) {
Trade {
Seller
Buyer
}
trades: count(if: { Trade: { Side: { Type: { is: buy } } } })
tokens: uniq(of: Trade_Currency_SmartContract)
}
}
}

You can checkout a completed product using this info on DEXRabbit.

Top Traders on Optimism

Get Top Traders for a Pair on Optimism

This query returns the top traders of a pair based on the trade volume in USD. For this example we are taking the pair of WLD 0xdc6ff44d5d932cbd77b52e5612ba0529dc6226f1 and USDC 0x0b2c639c533813f4aa9d7837caf62653d097ff85, including amount sold, amount bought, volume and volume in USD.

query pairTopTraders(
$network: evm_network
$token: String
$base: String
$time_ago: DateTime
) {
EVM(network: $network) {
DEXTradeByTokens(
orderBy: { descendingByField: "volumeUsd" }
limit: { count: 100 }
where: {
Trade: {
Currency: { SmartContract: { is: $base } }
Side: {
Amount: { gt: "0" }
Currency: { SmartContract: { is: $token } }
}
}
Block: { Time: { since: $time_ago } }
}
) {
Trade {
Buyer
}
bought: sum(
of: Trade_Amount
if: { Trade: { Side: { Type: { is: buy } } } }
)
sold: sum(
of: Trade_Amount
if: { Trade: { Side: { Type: { is: sell } } } }
)
volume: sum(of: Trade_Amount)
sideVolume: sum(of: Trade_Side_Amount)
volumeUsd: sum(of: Trade_Side_AmountInUSD)
}
}
}

An example for the same could be seen in the DEXRabbit as shown below.

Top Traders for a Pair

Subscribe to Latest Price of a Token in Real-time

This query provides real-time updates on price of WETH 0x4200000000000000000000000000000000000006 in terms of USD Coin 0x7f5c764cbc14f9669b88837ca1490cca17c31607, including details about the DEX, market, and order specifics. Find the query here

subscription {
EVM(network: optimism) {
DEXTrades(
where: {Trade: {Sell: {Currency: {SmartContract: {is: "0x4200000000000000000000000000000000000006"}}}, Buy: {Currency: {SmartContract: {is: "0x7f5c764cbc14f9669b88837ca1490cca17c31607"}}}}}
) {
Block {
Time
}
Trade {
Buy {
Amount
Buyer
Seller
Price_in_terms_of_sell_currency: Price
Currency {
Name
Symbol
SmartContract
}
}
Sell {
Amount
Buyer
Seller
Price_in_terms_of_buy_currency: Price
Currency {
Symbol
SmartContract
Name
}
}
}
}
}
}

This query returns the top trending token info based on the number of trades and returns values like number of unique buyers, sellers, markets, pools along with volume in USD.

query topTokens($network: evm_network, $time_ago: DateTime!) {
EVM(network: $network) {
DEXTradeByTokens(
orderBy: { descendingByField: "count" }
limit: { count: 100 }
where: { Block: { Time: { since: $time_ago } } }
) {
Trade {
Currency {
Symbol
SmartContract
Fungible
Name
}
Amount(maximum: Block_Number)
AmountInUSD(maximum: Block_Number)
}
pairs: uniq(of: Trade_Side_Currency_SmartContract)
dexes: uniq(of: Trade_Dex_SmartContract)
amount: sum(of: Trade_Amount)
usd: sum(of: Trade_AmountInUSD)
buyers: uniq(of: Trade_Buyer)
sellers: uniq(of: Trade_Sender)
count
}
}
}

An example of the utilisation of this data could be seen on DEXRabbit.

Top Tokens on Optimism

Latest USD Price of a Token

The below query retrieves the USD price of a token on Optimism by setting SmartContract: {is: "0x68f180fcCe6836688e9084f035309E29Bf0A2095"} . Check the field PriceInUSD for the USD value. You can access the query here.

subscription {
EVM(network: optimism) {
DEXTradeByTokens(
where: {Trade: {Currency: {SmartContract: {is: "0x68f180fcCe6836688e9084f035309E29Bf0A2095"}}}}
) {
Transaction {
Hash
}
Trade {
Buyer
AmountInUSD
Amount
Price
PriceInUSD
Seller
Currency {
Name
Symbol
SmartContract
}
Dex {
ProtocolFamily
SmartContract
ProtocolName
}
Side {
Amount
AmountInUSD
Buyer
Seller
Currency {
Name
SmartContract
Symbol
}
}
}
}
}
}