Skip to content
This repository has been archived by the owner on Oct 26, 2022. It is now read-only.

Commit

Permalink
Merge pull request #33 from LufyCZ/master
Browse files Browse the repository at this point in the history
Add dexcandles subgraph
  • Loading branch information
matthewlilley authored Feb 8, 2021
2 parents 9cff4d2 + 5a14b5d commit 93b20e0
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 0 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,21 @@
"codegen:maker": "graph codegen subgraphs/maker.yaml",
"codegen:masterchef": "graph codegen subgraphs/masterchef.yaml",
"codegen:timelock": "graph codegen subgraphs/timelock.yaml",
"codegen:dexcandles": "graph codegen subgraphs/dexcandles.yaml",
"build": "graph build",
"build:bar": "graph build subgraphs/bar.yaml",
"build:exchange": "graph build subgraphs/exchange.yaml",
"build:maker": "graph build subgraphs/maker.yaml",
"build:masterchef": "graph build subgraphs/masterchef.yaml",
"build:timelock": "graph build subgraphs/timelock.yaml",
"build:dexcandles": "graph build subgraphs/dexcandles.yaml",
"deploy": "graph deploy --node https://api.thegraph.com/deploy/ --ipfs https://api.thegraph.com/ipfs/",
"deploy:bar": "graph deploy --node https://api.thegraph.com/deploy/ --ipfs https://api.thegraph.com/ipfs/ sushiswap/sushi-bar subgraphs/bar.yaml",
"deploy:exchange": "graph deploy --node https://api.thegraph.com/deploy/ --ipfs https://api.thegraph.com/ipfs/ sushiswap/exchange subgraphs/exchange.yaml",
"deploy:maker": "graph deploy --node https://api.thegraph.com/deploy/ --ipfs https://api.thegraph.com/ipfs/ sushiswap/sushi-maker subgraphs/maker.yaml",
"deploy:masterchef": "graph deploy --node https://api.thegraph.com/deploy/ --ipfs https://api.thegraph.com/ipfs/ sushiswap/masterchef subgraphs/masterchef.yaml",
"deploy:timelock": "graph deploy --node https://api.thegraph.com/deploy/ --ipfs https://api.thegraph.com/ipfs/ sushiswap/sushi-timelock subgraphs/timelock.yaml",
"deploy:dexcandles": "graph deploy --node https://api.thegraph.com/deploy/ --ipfs https://api.thegraph.com/ipfs/ sushiswap/dexcandles subgraphs/dexcandles.yaml",
"create-local": "graph create --node http://localhost:8020/ /sushiswap/sushiswap",
"remove-local": "graph remove --node http://localhost:8020/ /sushiswap/sushiswap",
"deploy-local": "graph deploy --node http://localhost:8020/ --ipfs http://localhost:5001 /sushiswap/sushiswap"
Expand Down
61 changes: 61 additions & 0 deletions src/dexcandles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { BigInt, Bytes } from '@graphprotocol/graph-ts'
import { concat } from '@graphprotocol/graph-ts/helper-functions'
import { Swap } from '../generated/templates/Pair/Pair'
import { PairCreated } from '../generated/Factory/Factory'
import { Pair as PairTemplate } from '../generated/templates'
import { Pair, Candle } from '../generated/schema'

export function handleNewPair(event: PairCreated): void {
let pair = new Pair(event.params.pair.toHex());
pair.token0 = event.params.token0;
pair.token1 = event.params.token1;
pair.save();

PairTemplate.create(event.params.pair)
}

export function handleSwap(event: Swap): void {
let token0Amount: BigInt = event.params.amount0In.minus(event.params.amount0Out).abs();
let token1Amount: BigInt = event.params.amount1Out.minus(event.params.amount1In).abs();
if (token0Amount.isZero() || token1Amount.isZero()) {
return;
}

let pair = Pair.load(event.address.toHex());
let price = token0Amount.divDecimal(token1Amount.toBigDecimal());
let tokens = concat(pair.token0, pair.token1);
let timestamp = event.block.timestamp.toI32();

let periods: i32[] = [5*60, 15*60, 60*60, 4*60*60, 24*60*60, 7*24*60*60];
for (let i = 0; i < periods.length; i++) {
let time_id = timestamp / periods[i];
let candle_id = concat(concat(Bytes.fromI32(time_id), Bytes.fromI32(periods[i])), tokens).toHex();
let candle = Candle.load(candle_id);
if (candle === null) {
candle = new Candle(candle_id);
candle.time = timestamp;
candle.period = periods[i];
candle.token0 = pair.token0;
candle.token1 = pair.token1;
candle.open = price;
candle.low = price;
candle.high = price;
candle.token0TotalAmount = BigInt.fromI32(0);
candle.token1TotalAmount = BigInt.fromI32(0);
}
else {
if (price < candle.low) {
candle.low = price;
}
if (price > candle.high) {
candle.high = price;
}
}

candle.close = price;
candle.lastBlock = event.block.number.toI32();
candle.token0TotalAmount = candle.token0TotalAmount.plus(token0Amount);
candle.token1TotalAmount = candle.token1TotalAmount.plus(token1Amount);
candle.save();
}
}
21 changes: 21 additions & 0 deletions subgraphs/dexcandles.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
type Pair @entity {
id: ID! # address
token0: Bytes!
token1: Bytes!
}

type Candle @entity {
id: ID! # time + period + srcToken + dstToken
time: Int!
period: Int!
lastBlock: Int!
token0: Bytes!
token1: Bytes!

token0TotalAmount: BigInt!
token1TotalAmount: BigInt!
open: BigDecimal!
close: BigDecimal!
low: BigDecimal!
high: BigDecimal!
}
45 changes: 45 additions & 0 deletions subgraphs/dexcandles.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
specVersion: 0.0.2
description: DEX trades candles (5m/15m/1h/4h/1d/1w)
repository: https://github.com/sushiswap/sushiswap-subgraph
schema:
file: ./dexcandles.graphql
dataSources:
- kind: ethereum/contract
name: Factory
network: mainnet
source:
address: '0xC0AEe478e3658e2610c5F7A4A2E1777cE9e4f2Ac'
abi: Factory
startBlock: 10000834
mapping:
kind: ethereum/events
apiVersion: 0.0.3
language: wasm/assemblyscript
file: ../src/dexcandles.ts
entities:
- Pair
abis:
- name: Factory
file: ../node_modules/@sushiswap/core/build/contracts/UniswapV2Factory.json
eventHandlers:
- event: PairCreated(indexed address,indexed address,address,uint256)
handler: handleNewPair
templates:
- kind: ethereum/contract
name: Pair
network: mainnet
source:
abi: Pair
mapping:
kind: ethereum/events
apiVersion: 0.0.3
language: wasm/assemblyscript
file: ../src/dexcandles.ts
entities:
- Pair
abis:
- name: Pair
file: ../node_modules/@sushiswap/core/build/contracts/UniswapV2Pair.json
eventHandlers:
- event: Swap(indexed address,uint256,uint256,uint256,uint256,indexed address)
handler: handleSwap

0 comments on commit 93b20e0

Please sign in to comment.