Conditional Aggregation in GraphQL APIs

We have introduced three new functions to enable conditional aggregation in our APIs.

These three functions are:

  • any
  • maximum
  • minimum

These functions allow different types of aggregation on any APIs and also provide a natural way to extract the events that were not possible previously.

In this article, we will show you getting events using these as well as how to use these functions in any other API. 

Fetching Uniswap Events 

In the following example, we are getting Swap event for the USDC/ETH pair on the Uniswap. 

You can run the query below to check the results and see how Bitquery APIs are giving events in a natural way, unlike before, where you need to add some logic to parse them.

Let’s dive deep into the above query and see what’s going on.

As you can see, we have a new function any that takes multiple optional arguments. (Only of is a mandatory argument)

  • of* : You can use any attribute here, date, txHash, argument_value
  • get: specific field you want (will see example below)
  • Filters: In the above example, it is argument

For example, the following line means, give me any argument’s value where the argument is amount0In .

amount0In: any(of: argument_value, argument: {is: "amount0In"})

Also notice, we were using limitBy clause for limiting our results based on the argument name. You can use any argument name to limit your results.

Now, let’s say you want the maximum value of a particular argument by comparing all events. You can do something like the following. 

Focus on the following line, in which we are getting the maximum of the argument’s value where the argument is amount1Out.

amount1Out: maximum(of: argument_value, argument: {is: "amount1Out"})

Let’s say you want to know who was the caller for this transaction or when this transaction was executed. You can replace the above line with the following.

Getting the Caller for the tx which emitted event with max amount1Out. 👇

amount1Out: maximum(of: argument_value, get: caller, 
argument: {is: "amount1Out"})

Getting the date for the tx which emitted event with max amount1Out. 👇

amount1Out: maximum(of: argument_value, get: date, 
argument: {is: "amount1Out"})

Digging into a particular address

Now let’s say you want to know the maximum amount1Out for a particular address. You can use txFrom attributes in the filter or in the maximum function. 

txFrom in maximum function

txFrom in the Filters

Using conditional aggregation in DEX APIs

Let’s say you want to know the details of Uniswap trade with the maximum buy amount. Here is how you can use the maximum function to get that.

Transaction APIs

Let’s say you want to know the biggest transaction done by an address. You can get the using our transaction API and maximum function.

If you dig into our schema, you will see that any, maximum and minimum function supports many different attributes and can be used with different types of APIs such as smart contract, transaction, caller, event, etc.

You can use minimum function similar to the maximum function.

In the coming months, we will be adding more functions to make it easier to work with Blockchain data. These functions are an essential part of our next big update Flexigraph, which will allow anyone to build their own schema on our schema and customize it based on your application context.

If you have any questions, join our Telegram group and let us know.

You might also be interested in:

About Bitquery

Bitquery is a set of software tools that parse, index, access, search, and use information across blockchain networks in a unified way. Our products are:

  • Digital Assets API provides index information related to all major cryptocurrencies, coins, and tokens.
  • DEX API provides real-time deposits and transactions, trades, and other related data on different DEX protocols like Uniswap, Kyber Network, Airswap, Matching Network, etc.

If you have any questions about our products, ask them on our Telegram channel or email us at hello@bitquery.io. Also, subscribe to our newsletter below, we will keep you updated with the latest in the cryptocurrency world.