Skip to content

thales-markets/thales-subgraph

Repository files navigation

Thales Subgraph

Discord Twitter Follow

The Graph exposes a GraphQL endpoint to query the events and entities within the Thales protocol.
Thales has multiple subgraphs generated from this repository.

Token Subgraph

Entities:

  • TokenTransaction
  • Staker
  • CanClaimOnBehalfItem

Positional Markets

Entities:

  • Market
  • RangedMarket
  • OptionTransaction
  • Trade
  • AccountBuyVolume
  • ReferralTransfer
  • Referrer
  • ReferredTrader
  • Position
  • PositionBalance
  • RangedPosition
  • RangedPositionBalance
  • LiquidityPool
  • LiquidityPoolPnl
  • LiquidityPoolUserTransaction
  • Vault
  • VaultPnl
  • VaultTransactions
  • VaultUserTransactions

Sport Markets

Entities:

  • SportMarket
  • MarketTransaction
  • ClaimTx
  • BuyTransaction
  • Position
  • PositionBalance
  • ParlayMarket
  • GameIdToParentMarket
  • ParentMarketToDoubleChanceMarket
  • LiquidityPool
  • LiquidityPoolPnl
  • LiquidityPoolUserTransaction
  • OvertimeVoucher
  • Vault
  • VaultPnl
  • VaultTransactions
  • VaultUserTransactions
  • ParlayVaultTransaction
  • Referrer
  • ReferredTrader
  • ReferralTransaction
  • User
  • Zebro

To run and deploy locally

For any of the supported networks (Optimism, Arbitrum, Polygon, BNBChain, Optimism-Goerli):

  1. Run the npm run codegen:[subgraph] task to prepare the TypeScript sources for the GraphQL (generated/schema) and the ABIs (generated/[ABI]/*). Note: subgraph parameter can be found in the package.json file.
  2. [Optional] run the npm run build:[subgraph] task for the subgraph
  3. Deploy via npm run deploy:[subgraph]. Note: requires env variable of $THEGRAPH_THALES_ACCESS_TOKEN set in bash to work.

To query this subgraph

Please use our node & browser utility: thales-data.

Or to query the subgraph without any JS library

In it's simplest version (on a modern browser assuming async await support and fetch):

// Fetch all markets in the last 24 hours
(async () => {
  const ts = Math.floor(Date.now() / 1e3);
  const oneDayAgo = ts - 3600 * 24;
  const body = JSON.stringify({
    query: `{
      markets(
        orderBy:timestamp,
        orderDirection:desc,
        where:{timestamp_gt: ${oneDayAgo}}
      )
      {
        id
        timestamp
        creator
        currencyKey
        strikePrice
        maturityDate
        expiryDate
        isOpen
        poolSize
        longAddress
        shortAddress
        result
        customMarket
        customOracle
      }
    }`,
    variables: null,
  });

  const response = await fetch('https://api.thegraph.com/subgraphs/name/thales-markets/thales-markets', {
    method: 'POST',
    body,
  });

  const json = await response.json();
  const { markets } = json.data;
  // ...
  console.log(markets);
})();

Note: due to The Graph limitation, only 1000 results will be returned (the maximum allowed first amount). The way around this is to use paging (using the skip operator in GraphQL). See the function pageResults in thales-data for an example.