Introducing Expressions in GraphQL Queries

One of the core missions of Bitquery is to provide better accessibility to blockchain data. Therefore we choose GraphQL to provide control to developers pulling data through our APIs.

We continue to make the GraphQL interface more powerful and today we introduce the support for “Expressions” in GraphQL APIs.

What is an Expression?

Expression will allow users to apply essential mathematical functions (like subtract, multiply, sum, divide) in the queries themselves. However, expression can only be applied to metrics. To learn more about metrics, please read our article explaining Bitquery query architecture.

Note: There is no standard definition of metrics, but a simple rule of thumb is Metics have a “calculate” field.

Breaking down Expressions

Check out the following query.

query GetTopGainers {
  ethereum(network: bsc) {
    dexTrades(
      options: {desc: ["baseCurrency.name", "amount_expression"], limit: 50, limitBy: {each: "buyCurrency.name, a.b", limit: 2}}
      quoteCurrency: {is: "0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c"}
      time: {since: "2022-12-01"}
      tradeAmountUsd: {gt: 10}
    ) {
      baseCurrency {
        name
        address
        symbol
      }
      tradeAmount(in: USD)
      open_price: minimum(of: block, get: quote_price)
      close_price: maximum(of: block, get: quote_price)
      buyAmount
      sellAmount
      price_expression: expression(get: "close_price / open_price")
      Price_expressionX2: expression(get: "price_expression * 2")
      open_price_x2: expression(get: "open_price * 2")
      amount_expression: expression(get: "buyAmount - sellAmount")
      buyCurrency {
        address
        name
      }
      a: sellCurrency {
        b: address
      }
    }
  }
}

Now in the above query, you can see the following line

price_expression: expression(get: "close_price / open_price")

Here, we are getting “open_price” and “close_price” and then dividing “close_price” from “open_price” using “expression”.

Let’s see another example

{
  ethereum {
    transactions {
      gasValue(calculate: sum)
      coun2x: expression(get: "gasValue * 2")
    }
  }
}

Here you can see how we are multiplying gasValue with 2.

Allowing more than 1 field in limitby

If you want to limit results per specific field, you can use limitby. Now we have added support for more than 1 field in limitby field.

Look at the below example where we have used both buyCurrency.address, sellCurrency.address in the limitBy field. (query URL)

{
ethereum(network: ethereum) {
dexTrades(
options: {desc: ["block.height", "tradeIndex"],
limit: 10, offset: 0,
limitBy: {each: "buyCurrency.address,sellCurrency.address", limit: 1}}
date: {since: "2022-12-27", till: "2023-01-03"}
) {
block {
timestamp {
time(format: "%Y-%m-%d %H:%M:%S")
}
height
}
tradeIndex
protocol
exchange {
fullName
}
smartContract {
address {
address
annotation
}
}
buyAmount
buyCurrency {
address
symbol
}
buy_amount_usd: buyAmount(in: USD)
sellAmount
sellCurrency {
address
symbol
}
sell_amount_usd: sellAmount(in: USD)
transaction {
hash
}
}
}
}

If you have any questions about our products, ask them on our Discord and Telegram channel or email us at support@bitquery.io.

Hello there
I need help fix token liquidity and calls and more action
i staked in my tron wallet before three years many token and coins and there is a lot of action to do: sign in, approve, update token info and more
I hade crashing with my car and didnt sign in for long time How can I fix all of this pls
I will pay you if you help me fix this case
thanks