This repository has been archived by the owner on Oct 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 257
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from LufyCZ/master
Add dexcandles subgraph
- Loading branch information
Showing
4 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |