Get token quote without using DexTrades query?

I have an odd use case, in that I would like to get the price of a token at or before a certain block. This is working fine if there have been previous trades (as per below query). But it runs into issued if there have been no previous trades. Is there a way I can expand the query to look for price action events outside the scope of trades, such as an addLiquidity event, which will produce a price based on the initial reserves in the LP? Can this be tied into the query below or would it need to be a new query altogether for this specific case?

Current query for reference:

{
ethereum(network: bsc) {
dexTrades(
options: {desc: [“block.height”, “tradeIndex”], limit: 1}
exchangeName: {in: [“Pancake v2”]}
baseCurrency: {is: “0x2eba09fb8063aedba27424de9bf0dcb41567a943”}
quoteCurrency: {is: “0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c”}
height: {lteq: 9939407}
) {
transaction {
hash
}
tradeIndex
smartContract {
address {
address
}
contractType
currency {
name
}
}
tradeIndex
block {
timestamp {
time(format: “%Y-%m-%d %H:%M:%S”)
unixtime
}
height
}
baseCurrency {
symbol
address
}
quoteCurrency {
symbol
address
}
quotePrice
}
}
}

Many thanks!

1 Like

Hi @stcroix your GraphQL query filters the blocks in the descending order of their height which means that the particular height: {lteq: 9939407} might not have got rendered. I tried the same query with a different height parameter and it worked.

Yes there is way you can expand the scope of the query outside dexTrades by using aliasing. An example of the same is shown below. However, you can read more about aliasing in Bitquery from here: How to use GraphQL Alias and Aggregation?

{
  price_of_a_token: ethereum(network: bsc) {
    dexTrades(
      options: {desc: ["block.height", "tradeIndex"], limit: 1}
      exchangeName: {in: ["Pancake v2"]}
      baseCurrency: {is: "0x2eba09fb8063aedba27424de9bf0dcb41567a943"}
      quoteCurrency: {is: "0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c"}
      height: {lteq: 9939882}
    ) {
      transaction {
        hash
      }
      tradeIndex
      smartContract {
        address {
          address
        }
        contractType
        currency {
          name
        }
      }
      tradeIndex
      block {
        timestamp {
          time(format: "%Y-%m-%d %H:%M:%S")
          unixtime
        }
        height
      }
      baseCurrency {
        symbol
        address
      }
      quoteCurrency {
        symbol
        address
      }
      quotePrice
    }
  }
  addLiquidity: ethereum(network: bsc){
    smartContractEvents(
      options: {limit: 10}
      date: {since: "2021-08-11"}
      smartContractEvent: {is: "Mint"}
      smartContractAddress: {is: "0x6e559a800e1c098029b0d7fccef14845f18b0078"}
    ){
      arguments{
        value
        argument
      }
      smartContract{
        address{
          address
        }
        currency{
          symbol
        }
        protocolType
      }
    }
  }
}

If you using “aliasing” just like in the above query in the GraphQL IDE you need to use the dropdown provided in the IDE in order to switch between the aliased queries as shown in the image below.

However, if you are using aliasing through an API call in your application, you might get the complete query result.

1 Like

Can you please explain what comes under the criteria of “price action event”?

Thanks again @sayon - The aliasing is very interesting! Can you please explain more about why my initial query didn’t work because of the block height filtering? As thats the actual block I want to start searching backwards from.

My use of the term “price action” may be incorrect. Essentially what i’m wanting the query to do is: "get the last spot price of the token as of block 9939407, even if there have been no trades (using other events to determine the price if that’s the case)

Hope that clears up what i’m trying to do