Skip to main content

OpenBook DEX API

OpenBook Trades in Real-Time​

The below query gets real-time information whenever there's a new trade on the OpenBook DEX including detailed information about the trade, including the buy and sell details, the block information, and the transaction specifics. You can run the query here

subscription {
Solana {
DEXTrades(
where: {Trade: {Dex: {ProtocolFamily: {is: "OpenBook"}}}}
) {
Trade {
Dex {
ProgramAddress
ProtocolFamily
ProtocolName
}
Buy {
Currency {
Name
Symbol
MintAddress
}
Amount
Account {
Address
}
PriceAgainstSellCurrency:Price
}
Sell {
Account {
Address
}
Amount
Currency {
Name
Symbol
MintAddress
}
PriceAgainstBuyCurrency: Price
}
}
Block {
Time
}
}
}
}

Price Average of Pair on OpenBook​

Here is the query to get average price of a selected pair on a selected day.

query PriceAverage {
Solana {
DEXTrades(
limit: { count: 1 }
orderBy: { descending: Block_Time }
where: {
Trade: {
Dex: { ProtocolFamily: { is: "OpenBook" } }
Sell: { Currency: { Symbol: { is: "USDC" } } }
Buy: { Currency: { Symbol: { is: "WSOL" } } }
}
Block: {
Time: {
after: "2024-06-03T00:00:00Z"
before: "2024-06-04T00:00:00Z"
}
}
}
) {
tokenPrice: average(of: Trade_Buy_Price)
}
}
}

Get All Instructions of OpenBook v2​

To get all the Instructions associated with OpenBook v2, we will utilize the Solana instructions API. You can run this query using this link.

query MyQuery {
Solana {
Instructions(
where: {
Instruction: {
Program: {
Address: { is: "opnb2LAfJYbRMAHHvqjCwQxanZn7ReEHp1k81EohpZb" }
}
}
Transaction: { Result: { Success: true } }
}
) {
Instruction {
Program {
Method
}
}
count
}
}
}

Listen to placeOrder, placeTakeOrder Instructions of OpenBook v2​

We will use this subscription to listen to transactions that are placing orders on OpenBook v2. We will utilize the Solana instructions API. You can also watch a Youtube Tutorial for a better understand here. You can run this query using this link.

subscription {
Solana {
Instructions(
where: {
Instruction: {
Program: {
Address: { is: "opnb2LAfJYbRMAHHvqjCwQxanZn7ReEHp1k81EohpZb" }
Method: { in: ["placeOrder", "placeTakeOrder"] }
}
}
Transaction: { Result: { Success: true } }
}
) {
Transaction {
Signature
}
Instruction {
Accounts {
Address
IsWritable
Token {
Mint
Owner
ProgramId
}
}
Program {
AccountNames
Address
Method
Name
Parsed
Arguments {
Name
Type
Value {
... on Solana_ABI_Json_Value_Arg {
json
}
... on Solana_ABI_Float_Value_Arg {
float
}
... on Solana_ABI_Boolean_Value_Arg {
bool
}
... on Solana_ABI_Bytes_Value_Arg {
hex
}
... on Solana_ABI_BigInt_Value_Arg {
bigInteger
}
... on Solana_ABI_Address_Value_Arg {
address
}
... on Solana_ABI_String_Value_Arg {
string
}
... on Solana_ABI_Integer_Value_Arg {
integer
}
}
}
}
}
}
}
}

Listen to consumeEvents Instructions of OpenBook v2​

We will use this subscription to listen to consumeEvents transactions on OpenBook v2. This instruction processes trade events and other activities such as order cancellations. It updates the order book and ensures that all relevant trade activities are accounted for and settled. We will utilize the Solana instructions API. You can also watch a Youtube TUtorial for a better understand here. You can run this query using this link.

subscription {
Solana {
Instructions(
where: {
Instruction: {
Program: {
Address: { is: "opnb2LAfJYbRMAHHvqjCwQxanZn7ReEHp1k81EohpZb" }
Method: { is: "consumeEvents" }
}
}
Transaction: { Result: { Success: true } }
}
) {
Transaction {
Signature
}
Instruction {
Accounts {
Address
IsWritable
Token {
Mint
Owner
ProgramId
}
}
Program {
AccountNames
Address
Method
Name
Parsed
Arguments {
Name
Type
Value {
... on Solana_ABI_Json_Value_Arg {
json
}
... on Solana_ABI_Float_Value_Arg {
float
}
... on Solana_ABI_Boolean_Value_Arg {
bool
}
... on Solana_ABI_Bytes_Value_Arg {
hex
}
... on Solana_ABI_BigInt_Value_Arg {
bigInteger
}
... on Solana_ABI_Address_Value_Arg {
address
}
... on Solana_ABI_String_Value_Arg {
string
}
... on Solana_ABI_Integer_Value_Arg {
integer
}
}
}
}
}
}
}
}

Video Tutorial | How to Listen to OpenBook v2 Instruction Calls in Realtime - Bitquery API​