2

I want to get some info about some Uniswap transactions using The Graph but I don't know how to go about filtering.

I want to get only Swaps that one of their tokens is ether. That means that the symbol of the token is "ETH". Is there a way to do it?

The relevant GraphQL schema is:

type Swap @entity {
  id: ID!
  token0: Token!
  token1: Token!
}

type Token @entity {
  id: ID!
  symbol: String!
}

Here is the complete GraphQL schema: https://thegraph.com/hosted-service/subgraph/uniswap/uniswap-v3?query=Example%20query

To make my question clearer, if this was SQL, I would query something like this:

SELECT Swap.id
FROM Swap JOIN Token ON Swap.token1_id == Token.id
WHERE Token.symbol

Thank you!

0

2 Answers 2

2

Filtering by subfield is a new feature just added to the Graph.
The syntax supported is as follows: where: {token0_: { subfield : "value" }}

For instance to get swaps where the first token has the symbol "WETH":

{
  swaps(first: 1, where: {token0_: {symbol : "WETH"}}) {
    token0 {symbol}
    token1 {symbol}
    amount0
    amount1
  }
}

Result:

{
  "data": {
    "swaps": [
      {
        "token0": {
          "symbol": "WETH"
        },
        "token1": {
          "symbol": "USDT"
        },
        "amount0": "39.227335678972562535",
        "amount1": "-101256.052655"
      }
    ]
  }
}

A more reliable way to do it would be to filter by token's ID, that way scam token someone named "WETH" would not be included, but I used symbol for the sake of clarity of the example.

Sign up to request clarification or add additional context in comments.

Comments

1

I found another way to query what I wanted even though it doesn't involve nested objects. I looked at the code and figured out how to do it: https://github.com/Uniswap/v3-info

The query that does the work I need is this:

{
  swaps(
    where: {token0: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"}
  ) {
    token0 {symbol}
    token1 {symbol}
  }
}

Where the long hexadecimal is the address of the token.

By the way, it appears that it's quite simple to do this according to this GraphGL docs: https://hasura.io/docs/latest/queries/postgres/query-filters/#the-where-argument

According to it, the query should be

{
  swaps(
    where: {token0: {symbol : {_eq: "WETH"}}}
  ) {
    token0 {symbol}
    token1 {symbol}
  }
}

But it doesn't work with the playground of The Graph.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.