Skip to content

https-github-com-okeamah/dharma-subgraph

 
 

Repository files navigation

Dharma Subgraph

This is a subgraph for the Dharma Protocol. It is specifically designed so that the subgraph holds all the information required for the Dharma Plex Dapp to function.

This requires the following four contracts to be ingested by the subgraph, with the events that were sourced to be stored in the subgraph database:

  • DebtKernal.sol
    • LogDebtOrderFilled
    • LogIssuanceCancelled
    • LogDebtOrderCancelled
  • DebtRegistry.sol
    • LogInsertEntry
    • LogModifyEntryBeneficiary
  • Collateralizer.sol
    • CollateralLocked
    • CollateralSeized
    • CollateralReturned
  • RepaymentRouter.sol
    • LogRepayment

This can be used for both the Kovan network contracts and the Mainnet contracts. In order to do so the subgraph.yaml file will need to have the contract addresses changed to point to the correct address for each respective network.

Brief Description of The Graph Node Setup

A Graph Node can run multiple subgraphs, and in this case it can have a subgraph for both Mainnet and Kovan. The subgraph ingests event data by calling to Infura through http. It can also connect to any geth or parity node that accepts RPC calls. Fast synced geth nodes work. To use parity, the --no-warp flag must be used. Setting up a local Ethereum node is more reliable and faster, but Infura is the easiest way to get started.

This subgraph has three types of files which tell the Graph Node to ingest events from specific contracts. They are:

  • The subgraph manifest (subgraph.yaml)
  • A GraphQL schema (schema.graphql)
  • Mapping scripts (debt-kernal.ts, debt-registry.ts, repayment-router.ts, collateralizer.ts)

This repository has these files created and ready to compile. The only thing that needs to be edited is the contract addresses in the subgraph.yaml file to change between Kovan or Mainnet.

We have provided a quick guide on how to start up the Dharma-Subgraph graph node. If these steps aren't descriptive enough, the getting started guide has in depth details on running a subgraph.

Steps to Deploying The Dharma-Subgraph Locally

  1. Install IPFS and run ipfs init followed by ipfs daemon
  2. Install PostgreSQL and run initdb -D .postgres followed by pg_ctl -D .postgres start and createdb dharma-mainnet (note this db name is used in the commands below for the mainnet examples)
  3. If using Ubuntu, you may need to install additional packages: sudo apt-get install -y clang libpq-dev libssl-dev pkg-config
  4. Clone this repository, and run the following:
    • yarn
    • yarn codegen
  5. Clone https://github.com/graphprotocol/graph-node from master and cargo build (this might take a while)
  6. a) Now that all the dependencies are running, you can run the following command to connect to Infura Mainnet (it may take a few minutes to compile). Password might be optional, it depends on your postrgres setup:
  cargo run -p graph-node --release -- \
  --postgres-url postgresql://USERNAME:[PASSWORD]@localhost:5432/dharma-mainnet \
  --ipfs 127.0.0.1:5001 \
  --ethereum-rpc mainnet-infura:https://mainnet.infura.io 
  1. b) Or Mainnet with a Local Ethereum node. This is very common if you are working with brand new contracts, and you have deployed them to a testnet environment like ganache (note that ganache commonly uses port 9545 rather than 8545):
  cargo run -p graph-node --release -- \
  --postgres-url postgresql://USERNAME:[PASSWORD]@localhost:5432/dharma-mainnet \
  --ipfs 127.0.0.1:5001 \
  --ethereum-rpc mainnet-local:http://127.0.0.1:8545 
  1. c) Or Infura Kovan (NOTE: Infura Kovan is not reliable right now, we get inconsistent results returned. If Kovan data is needed, it is suggested to run your own Kovan node)
    cargo run -p graph-node --release --   \
    --postgres-url postgresql://USERNAME:[PASSWORD]@localhost:5432/dharma-kovan \
    --ipfs 127.0.0.1:5001 \
    --ethereum-rpc kovan-infura:https://kovan.infura.io 

  1. d) Or a Kovan local node which was started with parity --chain=kovan --no-warp --jsonrpc-apis="all" :
  cargo run -p graph-node --release -- \
  --postgres-url postgresql://USERNAME:[PASSWORD]@localhost:5432/dharma-kovan-subgraph \
  --ipfs 127.0.0.1:5001 \
  --ethereum-rpc kovan-local:http://127.0.0.1:8545

  1. Now create the subgraph locally on The Graph Node with yarn create-local. (On The Graph Hosted service, creating the subgraph is done in the web broswer).

  2. Now deploy the dharma subgraph to The Graph Node with yarn deploy. You should see a lot of blocks being skipped in the graph-node terminal, and then it will start ingesting events from the moment the contracts were uploaded to the network.

Now that you have subgraph is running you may open a Graphiql browser at 127.0.0.1:8000 and get started with querying.

Viewing the Subgraph on the Graph Hosted Service

This subgraph has already been deploy to the hosted service, and you can see it under on The Graph Explorer. To understand how deploying to the hosted service works, check out the Deploying Instructions in the official documentation. The most important part of deploying to the hosted service is ensuring that the npm script for deploy is updated to the correct name that you want to deploy with.

Getting started with Querying

Below are a few ways to show how to query the Dharma-Subgraph for data.

Querying all possible data that is being stored

The query below shows all the information that is possible to query, but is limited to the first 10 instances. There are many other filtering options that can be used, just check out the querying api.

{
  debtOrders(first: 10) {
    id
    beneficiary
    issuanceCancelledBy
    collaterals{
      id
      status
      tokenAddress
      collateralReturnedTo
      amount
    }
    principle
    principleToken
    relayer
    relayerFee
    repayments{
      id
      payers
      amounts
      beneficiaries
    }
    termsContract
    termsContractParameters
    underwriter
    underwriterFee
    underwriterRiskRating
   }
  
  cancelledDebtOrders(first: 10){
    id
    cancelledBy
  }
}

The command above can be copy pasted into the Graphiql interface in your browser at 127.0.0.1:8000.

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 100.0%