Code Monkey home page Code Monkey logo

synthetix-subgraph's Introduction

Synthetix Subgraph

Overview

The Graph is a decentralized protocol for indexing and querying data on the Ethereum blockchain. Subgraphs define the data The Graph will index and how it will be stored. This data is then provided via a GraphQL API.

The Graph currently consists of a hosted service and a decentralized network. In the future, the hosted service will be gradually sunset after the decentralized network achieves feature parity.

Synthetix currently maintains one official subgraph per network on the hosted services.

Hosted Service

The Synthetix subgraph is available on the hosted service on mainnet, optimism, kovan, and optimism-kovan

⚠️ Using subgraphs with the hosted service may introduce breaking changes. The Synthetix subgraphs are under active development. Because The Graph does not currently support pinning subgraph versions on the hosted service, these subgraphs should be used with caution.

Note that data queried from the Optimism networks may be incomplete due the regenesis on 11/11/21.

The Graph Network

The subgraph can also be found on The Graph’s decentralized network here.

The decentralized network supports pinning versions. Subgraphs on the decentralized network can only query data on mainnet. The latest version available on the decentralized network may not always be synchronized with the latest version on the hosted service.

Usage

Query the hosted service directly

This code snippet demonstrates how to retrieve all exchanges that occured in the last 24 hours:

(async () => {
  const ts = Math.floor(Date.now() / 1e3);
  const oneDayAgo = ts - 3600 * 24;
  const body = JSON.stringify({
    query: `{
      synthExchanges(
        orderBy:timestamp,
        orderDirection:desc,
        where:{timestamp_gt: ${oneDayAgo}}
      )
      {
        fromAmount
        fromAmountInUSD
        fromCurrencyKey
        toCurrencyKey
        block
        timestamp
        toAddress
        toAmount
        toAmountInUSD
        feesInUSD
      }
    }`,
    variables: null,
  });

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

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

Explore all of the entities available in the subgraph in the playground.

Due to limitation imposed by The Graph, only 1,000 results will be returned from the query above. Review The Graph's documentation on pagination for more information.

@synthetixio/queries

@synthetixio/queries is a JavaScript library that retrieves Synthetix’s data from The Graph. The library provides TypeScript support for the returned data, automatically handles pagination, and allows you to subscribe to real-time updates.

Subgraph Entities

Find detailed documentation for the more commonly accessed subgraph entities below.

Candle

The Candle entity stores data pertaining to the price of the SNX token and each synth, as reported by Chainlink oracles, over various time periods. This contains all of the data necessary to generate candlestick charts.

  • id (string) - The unique identifier for this candle, represented as synth-period-periodId. (periodId is calculated by dividing the current timestamp by the period.)
  • synth (string) - The ticker symbol for synth (e.g. sUSD) or SNX.
  • period (integer) - The duration this candle is tracking, in seconds. The following periods are available: year (31556736), quarter (7889184), month (2629728), week (604800), day (86400), hour (3600), and 15 minutes (900).
  • open (decimal) - The price reported at the beginning of this period.
  • high (decimal) - The highest price reported during this period.
  • low (decimal) - The lowest price reported during this period.
  • close (decimal) - The price reported at the end of this period.
  • timestamp (integer) - The timestamp, in seconds, at the beginning of this period.
  • average (decimal) - The average of the prices reported during this period.
  • aggregatedPrices (integer) - The number of times the price was reported during this period. (See the Rate Updates entity for individual price reports.)

SynthExchange

Each time a synth is exchanged, a new SynthExchange entity is created.

  • id (string) - The unique identifier for this exchange, represented as transactionHash-eventLogIndex.
  • account (Exchanger) - The account that executed the exchange.
  • fromSynth (Synth) - The synth being exchanged.
  • toSynth (Synth) - The synth being received in the exchange.
  • fromAmount (decimal) - The amount of synths being exchanged.
  • fromAmountInUSD (decimal) - The value of synths being exchanged, denominated in USD at the price of the synths at the time of exchange.
  • toAmount (decimal) - The amount of synths being received.
  • toAmountInUSD (decimal) - The value of synths being received, denominated in USD at the price of the synths at the time of exchange.
  • feesInUSD (decimal) - The fees collected by the protocol for this exchange, denominated in USD.
  • toAddress (bytes) - The address of the synth being received in the exchange.
  • timestamp (integer) - The timestamp, in seconds, when this exchange occured.
  • gasPrice (integer) - The gas price for the exchange.

Total

The Total entities aggregate SynthExchange data and futures trades over time. Note that we are only collecting a single entity to record all-time totals for each combination of bucketMagnitude and synth filters. (i.e. All totals with a period of 0 have a timestamp of 0.)

  • id (string) - The unique identifier for this total, represented as timestamp-bucketMagnitude-synth-period
  • period (integer) - The duration this candle is tracking, in seconds. The following periods are available: year (31556736), quarter (7889184), month (2629728), week (604800), day (86400), 15 minutes (900), and all-time (0). This is especially useful for filtering.
  • timestamp (integer) - The timestamp, in seconds, at the beginning of this period. This is especially useful for filtering.
  • bucketMagnitude (integer) - The minimum power of 10 that the exchange's fromAmountInUSD value must be. (e.g. 2 will total trades valued at $100 or higher.) This is especially useful for filtering.
  • synth (Synth) - The synth being exchanged. This is especially useful for filtering.
  • trades (integer) - The number of individual exchanges completed in this Total.
  • exchangers (integer) - The number of unique exchangers who were seen in this Total.
  • newExchangers (integer) - The number of exchangers who were first seen in this Total.
  • exchangeUSDTally (decimal) - The value of all synths exchanged, denominated in USD, in this Total.
  • totalFeesGeneratedInUSD (decimal) - The value of all exchange fees, denominated in USD, generated in this Total.

SnxHolder

This entity represents each individual holder of SNX tokens.

  • id (string) - The user's address.
  • block (integer) - The number of the last block where an event from this user updated this entity.
  • timestamp (integer) - The timestamp, in second, of the last block where an event from this user updated this entity.
  • balanceOf (decimal) - The current balance of SNX tokens held by this user.
  • collateral (decimal) - The amount of SNX which is being used for collateral as of last event and cannot be spent
  • transferable (decimal) - The amount of SNX which can be spent as of the last event.
  • initialDebtOwnership (integer) - The percentage of the total debt owned at the time of issuance. This number is modified by the global debt delta array. You can figure out a user's exit price and collateralization ratio using a combination of their initial debt and the slice of global debt delta which applies to them.
  • debtEntryAtIndex (integer) - An index which lets us know when (in relative terms) the user entered the debt pool so we can calculate their exit price and collateralization ratio.
  • claims (integer) - The number of fee claims performed by this user.
  • mints (integer) - The number of mints performed by this user.

Build and Deploy

To build and deploy the subgraphs, run npm run deploy for a CLI. You will have the option to update the Synthetix contract ABIs, build the updated subgraph, and deploy to the hosted service and/or decentralized network.

The CLI automatically generates the main subgraph, which is composed of the other subgraph in the subgraphs directory. You can also use the CLI to deploy the component subgraphs to the hosted service for faster development and testing.

All of the prompts in the CLI can be provided via options. For more information, run npm run deploy -- --help.

synthetix-subgraph's People

Contributors

0xclem avatar 0xjocke avatar dbeal-eth avatar evgenyboxer avatar jjgonecrypto avatar kmeraz avatar noahlitvin avatar sistemico avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

synthetix-subgraph's Issues

SNX Holders collateral fix

Right now SNXHolder.collateral is only calculated on an SNX Transfer event in or out. This needs to take into account any reward claiming activity.

Subgraphs migration to The Graph's mainnet

Hi,
I'm not sure if this is the right place to ask but i'm giving it a shot.
What is the SNXs opinion of the Graph Protocol? Given that Graph team is pushing for more migration from hosted service to decentralized network this year, I'm wondering what's Synthetix take on it? How far are you from migrating subgraphs to decentralized network?

Exchanges in the same block as oracle do not respect transaction ordering

There have been times where Synthetix.exchange() invocations were in the same block yet positioned before ExchangeRate.updateRates() and traded on a price that changed in the latter transaction. When we calculate the effectiveValue of the source and destination via The Graph, it gives us the value as at the end of the block, not at the point when the exchange happens. This has lead to a few instances of reporting negative fees.

These can be seen by running the following query against the SNX Exchanges subgraph

 synthExchanges(where:{feesInUSD_lt:0}) {
    id
    account
    from
    fromCurrencyKey
    fromAmountInUSD
    toCurrencyKey
    toAmountInUSD
    feesInUSD
  }
}

We are discussing with members of The Graph engineering team as to how to deal with this issue.

Track SNX Holders cratio and locked pcent

  • Synthetix.collateral
  • Synthetix.collateralisationRatio
  • Synthetix.transferableSynthetix (unlocked SNX)
  • lockedSnxRatio (Math.min(1, collateralisationRatio/issuanceRatio)) (where issuanceRatio = FeePool.getPenaltyThresholdRatio())

This changes any time the user:

  • Synthetix.issueSynths, Synthetix.burnSynths, RewardEscrow vest, or SNX transfer in or out.

crash at block #10773150

I'm syncing this subgraph on mainnet and stuck at #10773150 with log :

0|graph_sy | Jul 25 19:53:24.524 ERRO Subgraph instance failed to run: Mapping aborted at ~lib/@graphprotocol/graph-ts/chain/ethereum.ts, line 483, column 6, with message: accessed value of a reverted call, please check the revertedfield before accessing thevaluefield wasm backtrace: 0: 0x188b - <unknown>!~lib/@graphprotocol/graph-ts/chain/ethereum/ethereum.CallResult<~lib/array/Array<~lib/@graphprotocol/graph-ts/chain/ethereum/ethereum.Value>>#get:value 1: 0x1d3a - <unknown>!../src/global-debt/trackGlobalDebt 2: 0x1f55 - <unknown>!../src/global-debt/handleBlock in handlerhandleBlockat block #10773150 (7de9e392d04e7a90e369fb3165d3c615af0fe327644e4e2ae484f8f25fad5d0e), code: SubgraphSyncingFailure, sgd: 74, subgraph_id: QmdowFoTBBCd5t6PXAtTVBPY5BFgJxMhCbN9gwuKT4REb9, component: SubgraphInstanceManager

Add an Entity for all RewardEscrow addresses

In order to properly complete Synthetix SIP-13, we want to easily track all addresses that have RewardEscrow entries.

We need to track both VestingEntryCreated and Vested events. The entity should have both the address and the balanceOf at the time of the event.

snx: FeePool.FeesClaimed tracking

We need support for the FeePool.FesClaimed event, which is emitted off the ProxyFeePool.

One issue with this is that the FeePool abi isn't parsed by The Graph automatically as it has a multidimensional array in the output field of the feesByPeriod entry (see below):

{
    "constant": true,
    "inputs": [
      {
        "name": "account",
        "type": "address"
      }
    ],
    "name": "feesByPeriod",
    "outputs": [
      {
        "name": "results",
        "type": "uint256[2][3]"
      }
    ],
    "payable": false,
    "stateMutability": "view",
    "type": "function",
    "signature": "0x33140016"
  },

The solution here is to remove this portion of the ABI in our prepare-abis process, re-run it, then add support for FeePool.FeesClaimed

No timestamp on dailyTotals

I am trying to use the dailyTotals endpoint on https://api.thegraph.com/subgraphs/name/synthetixio-team/synthetix-exchanges. It works fine with a query like dailyTotals { exchangeUSDTally } but I do not understand how I am supposed to interpret this data, since there is no timestamp field that can be passed as a parameter or extracted from the endpoint. To which days do these totals correspond?

snx: Issued and Burned events missing value

In Synthetix subgraph

  • Issued events that come from issueMaxSynths are missing value entries. This is because the issueMaxSynths call handler cannot determine how many synths were issued
  • Burned events that originate from burnSynthsToTarget are missing value entries. (same reason as above - no argument given for value). (Not to mention that burnSynthsToTarget isn't being tracked right now regardless.

One potential solution is to use the ProxysUSD.Issued and ProxysUSD.Burned events for this. The problems with using them are:

  1. Earlier in our history, any flavor of synth could be issued, so we'd have to listen to all synth Issued and Burn events, which is suboptimal.
  2. They are also emitted for other actions - such as a SynthExchange, or an EtherCollateral loan open or close.

For 1), we can either auto-generate our synth listener YML section to include all synths, or we accept that if we only listed to sUSD ones that we only get these events since Vega (2.14) where we removed the ability to issue and burn into anything other than sUSD.
For 2) we can use a feature of the transaction underlying the event itself, and if we detect the data field of one of our expected function calls on Synthetix, use that.

Null value resolved for non-null field `account` in synthExchanges query

Running this query in the playground

{

  synthExchanges(
    first: 100,
    orderBy:id,
  	where:{
          id_gt:"0x0b2356f3c53f2051bb3570cb8c7c8044f13a824054567962cd177481906143cc-382",
    	  timestamp_gt: "1622530800",
          timestamp_lt:"1641024000"
    }
  ) {
    account{
      id
    }
  }
}

Gives the error:

{
  "errors": [
    {
      "locations": [
        {
          "column": 5,
          "line": 12
        }
      ],
      "message": "Null value resolved for non-null field `account`"
    }
  ]
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.