Giter VIP home page Giter VIP logo

uniswap-subgraph's Introduction

Uniswap-subgraph

Uniswap is a decentralized protocol for automated token exchange on Ethereum.

This Subgraph dynamically tracks any exchange created by the uniswap factory. Any exchange, any user of the protocol, and any transaction of the protocol can be queried.

Running Locally

Make sure to update package.json settings to point to your own graph account.

Important Info

  • USD values are based on the Compound Finance DAI oracle, which gets its prices from the Maker oracle.
  • You can find the profit of a liquidity provider with the following information:
    • Find the ratio of total tokens owned by the user out of all tokens:
      • (liquidityTokensMinted - liquidityTokensBurned) / totalLiquidityTokens = ratio
    • With that ratio, figure out how much ETH and token could be withdrawn. You must take the tokens and convert to ETH for total ETH they could withdraw:
      • ratio * ethBalance = ethWithdrawable
      • ratio * tokenBalance * tokenPrice = tokenWithdrawableInEth
      • ethWithdrawable + tokenWithdrawable = totalWithdrawable
    • Now take ethDeposited - ethWithdrawn = totalEth
    • And (tokenDeposited - tokenWithdrawn) * tokenPrice = totalTokensInEth
    • Current Liquidity profit is:
      • profit = totalWithdrawable - totalEth - totalTokensInEth

Currently only the top 50 coins have decimals incorporated into values. This is because not all uniswap exchanges created have proper ERC-20 interfaces, and calling decimals fails. So with dynamic contracts, we had to opt for hardcoding in the top 50 exchanges by volume. The others have tokens represented as the full large number. (i.e. 123456789123456789 instead of 1.23456789123456789). We are tracking this issue here: graphprotocol/graph-node#892

Queries

Below are a few ways to show how to query the uniswap-subgraph for data. The queries show most of the information that is queryable, but there are many other filtering options that can be used, just check out the querying api. These queries can be used locally or in The Graph Explorer playground.

Querying Aggregated Uniswap Data

This query fetches aggredated data from all uniswap exchanges, to give a view into how much activity is happening within the whole protocol

{
  uniswap(id: "1") {
    exchangeCount
    totalVolumeInEth
    totalLiquidityInEth
    totalVolumeUSD
    totalLiquidityUSD

    totalTokenBuys
    totalTokenSells
    totalAddLiquidity
    totalRemoveLiquidity
  }
}

Querying a Uniswap Exchange

This query fetches high level information on each uniswap exchange contract.

{
  exchanges(where: { tokenSymbol: "MKR" }) {
    id
    tokenAddress
    tokenSymbol
    tokenName
    tokenDecimals
    fee
    version
    startTime

    ethLiquidity
    tokenLiquidity
    ethBalance
    tokenBalance
    combinedBalanceInEth
    combinedBalanceInUSD
    ROI
    totalUniToken

    addLiquidityCount
    removeLiquidityCount
    sellTokenCount
    buyTokenCount

    lastPrice
    price
    tradeVolumeToken
    tradeVolumeEth
    totalValue
    weightedAvgPrice

    lastPriceUSD
    priceUSD
    weightedAvgPriceUSD
  }
}

Querying User Data

Transactions

This query fetches a user trading Dai between two timestamps, and returns a maximum of ten of their transactions.

{
  transactions(
    where: {
      timeStamp_gt: 1544832000
      timeStamp_lt: 1545696000
      tokenSymbol: "DAI"
      userAddress: "0x85c5c26dc2af5546341fc1988b9d178148b4838b"
    }
    first: 10
  ) {
    id
    exchangeAddress
    userAddress
    block
    ethAmount
    tokenAmount
    fee
    event
    timeStamp
  }
}

User Balances on Exchanges

This query fetches a single user, and all their exchange balances.

{
  user(id: "0x0000000000c90bc353314b6911180ed7e06019a9") {
    exchangeBalances {
      userAddress
      exchangeAddress

      ethDeposited
      tokensDeposited
      ethWithdrawn
      tokensWithdrawn
      uniTokensMinted
      uniTokensBurned

      ethBought
      ethSold
      tokensBought
      tokensSold
      ethFeesPaid
      tokenFeesPaid
      ethFeesInUSD
      tokenFeesInUSD
    }
  }
}

Querying Historical Data

ExchangeHistory

This query fetches historical data for the MKR exchange, ordered by time:

{
  exchangeHistories(where: { tokenSymbol: "MKR" }, orderBy: timestamp, orderDirection: desc) {
    id
    exchangeAddress
    tokenSymbol
    tokenAddress
    type
    timestamp
    ethLiquidity
    tokenLiquidity
    ethBalance
    tokenBalance
    combinedBalanceInEth
    combinedBalanceInUSD
    ROI
    totalUniToken
    priceUSD
    price
    tradeVolumeToken
    tradeVolumeEth
    feeInEth
  }
}

These queries fetch historical data as the events that were emitted, split into trade events and liquidity events:

{
  liquidityEvents {
    id
    type
    provider
    ethAmount
    tokenAmount
    exchangeAddress
    timestamp
    txhash
    block
    tokenAddress
    symbol
    decimals
    name
  }
  tradeEvents {
    id
    type
    buyer
    eth
    token
    exchangeAddress
    timestamp
    txhash
    block
    tokenFee
    ethFee
    tokenAddress
    symbol
    decimals
    name
  }
}

uniswap-subgraph's People

Contributors

davekaj avatar dependabot[bot] avatar fordn avatar ianlapham avatar leoyvens avatar lutter avatar mingela avatar

Watchers

 avatar

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.