Varun Krishna
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # Leveraging the graph protocol to retrieve analytical data off the chain ## Brief Outline - Timeswap/blog intro - How our frontend interacts with the blockchain - Need for a data source which can provide us with analytical data - How the graph protocol solves the problem ## // Timeswap stuff ## Interacting with the blockchain Just like most of the other DApps, [Timeswap](https://timeswap.io/) also interacts with the blockchain using a node provider. All the user-driven transactions are sent via the wallet the user has connected with. This means if a user is using metamask as their wallet, the transaction is signed with the private key which is kept locally, and the signature is sent to infura to broadcast the transaction to the blockchain. Well, that solves the writing problem (i.e. sending a transaction). What about getting the current state of the blockchain? What about extracting data from the events emitted/logs? The most obvious answer would be to use the same node provider like infura to fetch the details regarding the chain. Using a node provider and a web3 library like ethers one can easily get the state of the contract, filter, and fetch event details. And that is precisely what we use for getting transaction-specific details. ![](https://i.imgur.com/VUUyl2a.png) Well, problem solved right? Not quite. Even though we can get transaction-specific information easily via a node provider, several calls are usually required to fetch user balances, pool information, etc. This makes the process of fetching user/pool relevant information on the frontend time consuming, complex and expensive. This led to us writing a small service(blog for another day:smile:) to cache pool-related information, to reduce the calls made to the node provider. This allowed our frontend to make fewer calls to the node provider and also faster. However, a problem that remained was how to fetch aggregate information for analytical tasks? Analytical data meant getting all sorts of details with regards to a pool, user, transaction, and more. It helps us gain relevant insights as to how a pool is performing, no of users interacting with the pools, total volume locked among others. Getting this data would mean making a call to the node provider for every transaction, filtering the events, and mapping them together. This is extremely inefficient and laborious as there are several thousand transactions/events per pool. Here, dune analytics provided to be an acceptable solution just for this. It provides us with a service to build analytical dashboards by indexing blockchain event logs and function calls in Postgres, all we had to write was SQL queries. Though it was easy to build complex analytical dashboards via SQL queries, two problems remained. - Accessing the data in an API like format, so that it may be consumed on the frontend - It worked only on mainnet, which means we cant use it to dune analytics on different testnet This leads us to our problem statement for an ideal data source that can fetch aggregate data of timeswap pools : - A way to obtain data from the blockchain with regards to timeswap contracts, which satisfies the following criteria a) It should be accessible in an API like the format b) It should be accessible across testnets c) It should be fairly straightforward to write logic to map the raw information together such that, meaningful aggregated data may be obtained d) It should be able to discover our pools without hardcoding different pair contract addresses ## Enter the graph protocol What is the graph protocol? -[The Graph](https://thegraph.com/en/) is an indexing protocol for querying networks like Ethereum and IPFS. Anyone can build and publish open APIs, called subgraphs, making data easily accessible. The blockchain is essentially a decentralized state machine, though it's good at storing state in a decentralized fashion a layer to query/index relevant information is absent. The graph protocol solves exactly this problem in a decentralized fashion. We can use this to curate aggregate information regarding timeswap pools. - The graph protocol uses [GraphQL](https://graphql.org/) to query information, hence we can query information from any client that supports GraphQL. - It supports a wide variety of testnets including rinkeby and polygon Mumbai testnet Thus we wrote our own [subgraph](https://thegraph.com/hosted-service/subgraph/emmanuelantony2000/timeswap-v1/) for timeswap, which indexed many entities including transaction, pool, pair, user, etc. Though this solves our problem in running complex aggregate queries on different timeswap pools, one problem that remains is how we would automatically detect new pools being created and index their data. A common pattern of writing contracts is writing a factory contract which creates instances of the desired contract. In this architecture there usually is a factory contract, which is solely deployed for the sake of deploying similar instances of a particular contract/s. In our architecture too, there exists a factory contract which is responsible for creating new pair contracts. Each pair contract hosts pools of different maturity with the same asset-collateral pair. Thankfully the graph protocol has a solution just for this, Data Source Templates We first define a data source for the factory contract, which means in our case the TimeswapFactory.sol. Here we hardcode the address of the factory contract in the address attribute. ![](https://i.imgur.com/Xf2Tlmb.png) Once we add the data source templates to the manifest. Usually, one template is defined for a contract the factory creates. That is if the factory creates 4 kinds of contracts, there usually are 4 templates defined in the manifest. The only difference between a data source and a template is that one has a predefined address while the other one is instantiated each time a particular contract is deployed by the factory. ![](https://i.imgur.com/FvTgtmH.png) Though the template has been added, TimeswapPair still has to be instantiated, each time the factory contract deploys the pair contract. This is done by the event handler. In our case that would translate to: ![](https://i.imgur.com/xOjBTGU.png) This would ensure a fresh data source is created for the Pair, each time it's deployed. It is also to be noted that a data source newly added, can only index events and another information post the block it was created. This can now be used to fetch pool information of all timeswap pools just by hardcoding the factory address, the rest is handled in an automatic manner With this, we can now access the timeswap pool-related data, both individual/aggregate data at ease using the graph protocol.

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully