Moonbeam DEXs API
Table of Contents
Introduction
Before starting I recommend visiting this article Moonbeam Blockchain API
In this article we will see how to obtain data from the decentralized exchanges of the Moonbeam network with Bitquery.
Some of the things that we will be able to obtain with Bitquery will be:
- Name of the exchanges
- Volume of the exchanges (trades, usd)
- Latest trades
- Price of a token
- Among other things!
DEXs API
Bitquery also supports the different exchanges that exist in Moonbeam, so there is compatibility with the dexTrades api.
In the following query we will get the names of the DEXs, the amount of transactions and the volume in USD in a day’s time.
query {
ethereum(network: moonbeam) {
dexTrades(options: {desc: "tradeAmount"}, date: {is: "2022-08-24"}) {
exchange {
fullName
}
trades: count
tradeAmount(in: USD)
currencies: count(uniq: buy_currency)
contracts: count(uniq: smart_contracts)
}
}
}
The output will be something like this:
{
"ethereum": {
"dexTrades": [
{
"exchange": {
"fullName": "StellaSwap"
},
"trades": 6374,
"tradeAmount": 1283807.2841255318,
"currencies": 33,
"contracts": 55
},
{
"exchange": {
"fullName": "Beamswap"
},
"trades": 1647,
"tradeAmount": 168945.4652667018,
"currencies": 18,
"contracts": 25
},
{
"exchange": {
"fullName": "<Uniswap v2>"
},
"trades": 1287,
"tradeAmount": 104371.33092475001,
"currencies": 36,
"contracts": 75
},
]
}
}
The output has been trimmed to make it easier to read.
Some of the most used exchanges in Moonbeam according to Bitquery would be:
- StellaSwap
- Beamswap
- UniswapV2 based
- Solarflare
- PADSwap
The exchange with the highest volume in this case would be StellaSwap.
Total Trades
Sometimes it may be useful to know the total number of total trades in the network, with the following query we can calculate the total number of trades, regardless of the exchange or currency:
query {
ethereum(network: moonbeam){
dexTrades {
count
}
}
}
The output will it be like:
{
"ethereum": {
"dexTrades": [
{
"count": 4758492
}
]
}
}
This graph was made with this query
Latest Trade and Price
Now we will get the last trade as well as the last price with this query:
query {
ethereum(network: moonbeam){
dexTrades(
baseCurrency: {is: "0x0e358838ce72d5e61e0018a2ffac4bec5f4c88d2"}
quoteCurrency: {is: "0xacc15dc74880c9944775448304b263d191c6077f"}
options: {desc: ["block.height", "transaction.index"], limit: 1}
date: {is: "2022-08-28"}
) {
block {
height
timestamp {
time(format: "%Y-%m-%d %H:%M:%S")
}
}
transaction {
hash
index
}
baseCurrency {
symbol
}
quoteCurrency {
symbol
}
quotePrice
}
}
}
We have indicated STELLA and WGMLR as currencies.
The output will it be like:
{
"ethereum": {
"dexTrades": [
{
"block": {
"height": 1750187,
"timestamp": {
"time": "2022-08-28 19:45:12"
}
},
"transaction": {
"hash": "0xc154daa3d85ddef1a2ac53ce89bd80fd795f9c50baa3094ae5281115f4aea58e",
"index": 1
},
"baseCurrency": {
"symbol": "STELLA"
},
"quoteCurrency": {
"symbol": "WGLMR"
},
"quotePrice": 0.18199682226424807
}
]
}
}
As we can see we will obtain with:
- Transactions hash
- Block
- Names of the currencies
- Quote price, in this case in WGLMR
Creating Charts | OHLC
Sometimes it will be very useful to get the price depending on the time, it can be useful to track a token or to get the highest/lowest price of a specific day. This time we will create a chart with the help of the Bitquery IDE using Tradingview, obtaining the OHLC of a token.
What is OHLC?
- O: Open
- H: High
- L: Low
- C: Close
In a few words, with bitquery we will be able to obtain all that on a given date.
We will use the following query:
query {
ethereum(network: moonbeam) {
dexTrades(
baseCurrency: { is: "0x0e358838ce72d5e61e0018a2ffac4bec5f4c88d2" }
quoteCurrency: { is: "0xacc15dc74880c9944775448304b263d191c6077f" }
date: { between: ["2022-08-25", "2022-08-26"] }
priceAsymmetry: {lteq: 10},
tradeAmountUsd: {gteq: 1}
) {
timeInterval {
minute(format:"%FT%TZ", count: 30)
}
buyCurrency: baseCurrency {
symbol
address
}
buyAmount: baseAmount
sellCurrency: quoteCurrency {
symbol
address
}
volume: quoteAmount
trades: count
high: quotePrice(calculate: maximum)
low: quotePrice(calculate: minimum)
open: minimum(of: block, get: quote_price)
close: maximum(of: block, get: quote_price)
}
}
}
In this query we are specifying two dates, two filters (so that there are no strange prices) and we are indicating a 30 minutes temporality.
In the IDE we will get something like this:
This data can be used in different ways, I recommend you to read these articles:
How to Create Charts with Bitquery And Chart.js
Bitquery API integration with TradingView Technical Analysis Charts (DEX APIs)
Top Trades
Bitquery allows an incredible flexibility when obtaining certain data, in this case we will obtain the trades with the highest amount in $ among all the Moonbeam network.
We will use the following query, limit the output to 10 and specify a start date:
{
ethereum(network: moonbeam) {
dexTrades(options: {limit: 10 desc: "tradeAmount"}
date: {after: "2022-08-01"}) {
transaction {
hash
}
tradeIndex
date{
date
}
buyAmount
buyAmountInUsd:buyAmount(in: USD)
buyCurrency{
symbol
}
sellAmount
sellCurrency{
symbol
}
sellAmountInUsd:sellAmount(in: USD)
tradeAmount(in: USD)
exchange{
fullName
}
}
}
}
We will obtain a result like this:
{
"ethereum": {
"dexTrades": [
{
"transaction": {
"hash": "0x0ed8ba81ff2b3bc2e64c02f36eecd74ae5f25ae5bab7087cb53b22a604458723"
},
"tradeIndex": "5",
"date": {
"date": "2022-08-02"
},
"buyAmount": 1727939.585567,
"buyAmountInUsd": 0,
"buyCurrency": {
"symbol": "USDC"
},
"sellAmount": 362731.25561675173,
"sellCurrency": {
"symbol": "WGLMR"
},
"sellAmountInUsd": 262250.9970787065,
"tradeAmount": 262250.9970787065,
"exchange": {
"fullName": "StellaSwap"
}
}
]
}
}
Output has been reduced
Overview:
With Bitquery we will be able to access very important data in a very flexible way, so it will be of great help.
Some links that will help you:
— Bitquery Resources —
— Moonbeam Resources —