Skip to content

v2 events subgraph #191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion config/base/.subgraph-env
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
V2_TOKEN_SUBGRAPH_NAME="v2-tokens-base"
V2_TOKEN_SUBGRAPH_VERSION="v0.0.1"
V2_EVENTS_SUBGRAPH_NAME="v2-events-base"

3 changes: 1 addition & 2 deletions config/unichain-mainnet/.subgraph-env
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
V2_TOKEN_SUBGRAPH_NAME="v2-tokens-unichain-mainnet"
V2_TOKEN_SUBGRAPH_VERSION="v0.0.2"
V2_SUBGRAPH_NAME="uniswap-v2-unichain-mainnet"
V2_SUBGRAPH_VERSION="v0.0.5"
V2_EVENTS_SUBGRAPH_NAME="v2-events-unichain-mainnet"
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
"@uniswap/eslint-config": "^1.2.0",
"cross-env": "^7.0.3",
"dotenv": "^16.0.3",
"eslint-config-prettier": "^6.1.0",
"eslint": "^8.57.0",
"eslint-config-prettier": "^6.1.0",
"fs-extra": "^11.1.0",
"mustache": "^3.1.0",
"prettier": "^1.18.2",
Expand Down
2 changes: 1 addition & 1 deletion script/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { build, deploy } from './utils/deploy-utils'
import { validateNetwork, validateSubgraphType } from './utils/prepareNetwork'

async function main() {
const argv = yargs(process.argv.slice(2))
const argv = await yargs(process.argv.slice(2))
.option('network', {
alias: 'n',
description: 'Network to build for',
Expand Down
6 changes: 3 additions & 3 deletions script/utils/deploy-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const exec = util.promisify(execCallback)

// Creating subgraphs is only available from hosted-service dashboard
// yarn graph create $network_name-v2 --node https://api.thegraph.com/deploy/ --access-token $SUBGRAPH_DEPLOY_KEY"
export const build = async (network, subgraphType) => {
export const build = async (network: string, subgraphType: string) => {
console.log(`Building subgraph for ${network}`)
console.log(`\n Copying constants & templates for ${network} \n`)
await prepare(network, subgraphType)
Expand All @@ -18,7 +18,7 @@ export const build = async (network, subgraphType) => {
await exec(`graph codegen ${subgraphType}-subgraph.yaml`)
}

export const deploy = async (subgraphType) => {
export const deploy = async (subgraphType: string) => {
try {
await exec('git diff-index --quiet HEAD -- && git diff --quiet || (exit 1)')
} catch (e) {
Expand All @@ -41,7 +41,7 @@ export const deploy = async (subgraphType) => {
}
console.log(stdout)
console.log('Subgraph deployed successfully.')
} catch (e) {
} catch (e: any) {
console.log(e.stdout)
console.log('Error: Failed to deploy subgraph. Please try again.')
process.exit(1)
Expand Down
31 changes: 13 additions & 18 deletions script/utils/prepareNetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export enum NETWORK {
export enum SUBGRAPH_TYPE {
V2_TOKENS = 'v2-tokens',
V2 = 'v2',
V2_EVENTS = 'v2-events',
}

const CHAIN_CONSTANTS_FILE_NAME = 'chain.ts'
Expand Down Expand Up @@ -59,32 +60,26 @@ export function validateSubgraphType(subgraphType: string) {
}
}

export function getSubgraphVersion(subgraphType: string) {
dotenv.config({ path: '.subgraph-env' })
if (subgraphType === SUBGRAPH_TYPE.V2_TOKENS) {
if (!process.env.V2_TOKEN_SUBGRAPH_VERSION) {
throw new Error('V2_TOKEN_SUBGRAPH_VERSION must be set')
}
return process.env.V2_TOKEN_SUBGRAPH_VERSION
}
if (!process.env.V2_SUBGRAPH_VERSION) {
throw new Error('V2_SUBGRAPH_VERSION must be set')
}
return process.env.V2_SUBGRAPH_VERSION
}

export function getSubgraphName(subgraphType: string) {
dotenv.config({ path: '.subgraph-env' })
if (subgraphType === SUBGRAPH_TYPE.V2_TOKENS) {
if (!process.env.V2_TOKEN_SUBGRAPH_NAME) {
throw new Error('V2_TOKEN_SUBGRAPH_NAME must be set')
}
return process.env.V2_TOKEN_SUBGRAPH_NAME
} else if (subgraphType === SUBGRAPH_TYPE.V2_EVENTS) {
if (!process.env.V2_EVENTS_SUBGRAPH_NAME) {
throw new Error('V2_EVENTS_SUBGRAPH_NAME must be set')
}
return process.env.V2_EVENTS_SUBGRAPH_NAME
} else if (subgraphType === SUBGRAPH_TYPE.V2) {
if (!process.env.V2_SUBGRAPH_NAME) {
throw new Error('V2_SUBGRAPH_NAME must be set')
}
return process.env.V2_SUBGRAPH_NAME
} else {
throw new Error('Invalid subgraph type')
}
if (!process.env.V2_SUBGRAPH_NAME) {
throw new Error('V2_SUBGRAPH_NAME must be set')
}
return process.env.V2_SUBGRAPH_NAME
}

export function getAlchemyDeploymentParams(): {
Expand Down
114 changes: 114 additions & 0 deletions src/v2-events/mappings/core.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { ByteArray, Bytes } from '@graphprotocol/graph-ts'

import {
Burn as BurnEntity,
Mint as MintEntity,
Swap as SwapEntity,
Sync as SyncEntity,
Transfer as TransferEntity,
} from '../../../generated/schema'
import {
Burn as BurnEvent,
Mint as MintEvent,
Swap as SwapEvent,
Sync as SyncEvent,
Transfer as TransferEvent,
} from '../../../generated/templates/Pair/Pair'

export function handleMint(event: MintEvent): void {
const transactionHash = event.transaction.hash
const logIndex = event.logIndex
const logIndexBytes = Bytes.fromByteArray(ByteArray.fromBigInt(logIndex))
const mintId = transactionHash.concat(logIndexBytes)
const mintEvent = new MintEntity(mintId)
mintEvent.transactionHash = transactionHash
mintEvent.transactionFrom = event.transaction.from
mintEvent.transactionTo = event.transaction.to
mintEvent.logAddress = event.address
mintEvent.logIndex = logIndex
mintEvent.blockNumber = event.block.number
mintEvent.timestamp = event.block.timestamp
mintEvent.sender = event.params.sender
mintEvent.amount0 = event.params.amount0
mintEvent.amount1 = event.params.amount1
mintEvent.save()
}

export function handleBurn(event: BurnEvent): void {
const transactionHash = event.transaction.hash
const logIndex = event.logIndex
const logIndexBytes = Bytes.fromByteArray(ByteArray.fromBigInt(logIndex))
const burnId = transactionHash.concat(logIndexBytes)
const burnEvent = new BurnEntity(burnId)
burnEvent.transactionHash = transactionHash
burnEvent.transactionFrom = event.transaction.from
burnEvent.transactionTo = event.transaction.to
burnEvent.logAddress = event.address
burnEvent.logIndex = logIndex
burnEvent.blockNumber = event.block.number
burnEvent.timestamp = event.block.timestamp
burnEvent.sender = event.params.sender
burnEvent.amount0 = event.params.amount0
burnEvent.amount1 = event.params.amount1
burnEvent.to = event.params.to
burnEvent.save()
}

export function handleSwap(event: SwapEvent): void {
const transactionHash = event.transaction.hash
const logIndex = event.logIndex
const logIndexBytes = Bytes.fromByteArray(ByteArray.fromBigInt(logIndex))
const swapId = transactionHash.concat(logIndexBytes)
const swapEvent = new SwapEntity(swapId)
swapEvent.transactionHash = transactionHash
swapEvent.transactionFrom = event.transaction.from
swapEvent.transactionTo = event.transaction.to
swapEvent.logAddress = event.address
swapEvent.logIndex = logIndex
swapEvent.blockNumber = event.block.number
swapEvent.timestamp = event.block.timestamp
swapEvent.sender = event.params.sender
swapEvent.amount0In = event.params.amount0In
swapEvent.amount1In = event.params.amount1In
swapEvent.amount0Out = event.params.amount0Out
swapEvent.amount1Out = event.params.amount1Out
swapEvent.to = event.params.to
swapEvent.save()
}

export function handleSync(event: SyncEvent): void {
const transactionHash = event.transaction.hash
const logIndex = event.logIndex
const logIndexBytes = Bytes.fromByteArray(ByteArray.fromBigInt(logIndex))
const syncId = transactionHash.concat(logIndexBytes)
const syncEvent = new SyncEntity(syncId)
syncEvent.transactionHash = transactionHash
syncEvent.transactionFrom = event.transaction.from
syncEvent.transactionTo = event.transaction.to
syncEvent.logAddress = event.address
syncEvent.logIndex = logIndex
syncEvent.blockNumber = event.block.number
syncEvent.timestamp = event.block.timestamp
syncEvent.reserve0 = event.params.reserve0
syncEvent.reserve1 = event.params.reserve1
syncEvent.save()
}

export function handleTransfer(event: TransferEvent): void {
const transactionHash = event.transaction.hash
const logIndex = event.logIndex
const logIndexBytes = Bytes.fromByteArray(ByteArray.fromBigInt(logIndex))
const transferId = transactionHash.concat(logIndexBytes)
const transferEvent = new TransferEntity(transferId)
transferEvent.transactionHash = transactionHash
transferEvent.transactionFrom = event.transaction.from
transferEvent.transactionTo = event.transaction.to
transferEvent.logAddress = event.address
transferEvent.logIndex = logIndex
transferEvent.blockNumber = event.block.number
transferEvent.timestamp = event.block.timestamp
transferEvent.from = event.params.from
transferEvent.to = event.params.to
transferEvent.value = event.params.value
transferEvent.save()
}
27 changes: 27 additions & 0 deletions src/v2-events/mappings/factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { ByteArray, Bytes } from '@graphprotocol/graph-ts'

import { PairCreated as PairCreatedEvent } from '../../../generated/Factory/Factory'
import { PairCreated as PairCreatedEntity } from '../../../generated/schema'
import { Pair as PairTemplate } from '../../../generated/templates'

export function handleNewPair(event: PairCreatedEvent): void {
const transactionHash = event.transaction.hash
const logIndex = event.logIndex
const logIndexBytes = Bytes.fromByteArray(ByteArray.fromBigInt(logIndex))
const pairCreatedId = transactionHash.concat(logIndexBytes)
const pairCreatedEvent = new PairCreatedEntity(pairCreatedId)
pairCreatedEvent.transactionHash = transactionHash
pairCreatedEvent.transactionFrom = event.transaction.from
pairCreatedEvent.transactionTo = event.transaction.to
pairCreatedEvent.logAddress = event.address
pairCreatedEvent.blockNumber = event.block.number
pairCreatedEvent.logIndex = logIndex
pairCreatedEvent.timestamp = event.block.timestamp
pairCreatedEvent.token0 = event.params.token0
pairCreatedEvent.token1 = event.params.token1
pairCreatedEvent.pair = event.params.pair
pairCreatedEvent.pairIndex = event.params.param3
pairCreatedEvent.save()

PairTemplate.create(event.params.pair)
}
92 changes: 92 additions & 0 deletions src/v2-events/schema.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
type PairCreated @entity(immutable: true) {
id: Bytes!
logAddress: Bytes!
logIndex: BigInt!
transactionHash: Bytes!
transactionFrom: Bytes!
transactionTo: Bytes
blockNumber: BigInt!
timestamp: BigInt!
token0: Bytes!
token1: Bytes!
pair: Bytes!
pairIndex: BigInt!
}

type Mint @entity(immutable: true) {
id: Bytes!
logAddress: Bytes!
logIndex: BigInt!
transactionHash: Bytes!
transactionFrom: Bytes!
transactionTo: Bytes
blockNumber: BigInt!
timestamp: BigInt!

sender: Bytes!
amount0: BigInt!
amount1: BigInt!
}

type Burn @entity(immutable: true) {
id: Bytes!
logAddress: Bytes!
logIndex: BigInt!
transactionHash: Bytes!
transactionFrom: Bytes!
transactionTo: Bytes
blockNumber: BigInt!
timestamp: BigInt!

sender: Bytes!
amount0: BigInt!
amount1: BigInt!
to: Bytes!
}

type Swap @entity(immutable: true) {
id: Bytes!
logAddress: Bytes!
logIndex: BigInt!
transactionHash: Bytes!
transactionFrom: Bytes!
transactionTo: Bytes
blockNumber: BigInt!
timestamp: BigInt!

sender: Bytes!
amount0In: BigInt!
amount1In: BigInt!
amount0Out: BigInt!
amount1Out: BigInt!
to: Bytes!
}

type Sync @entity(immutable: true) {
id: Bytes!
logAddress: Bytes!
logIndex: BigInt!
transactionHash: Bytes!
transactionFrom: Bytes!
transactionTo: Bytes
blockNumber: BigInt!
timestamp: BigInt!

reserve0: BigInt!
reserve1: BigInt!
}

type Transfer @entity(immutable: true) {
id: Bytes!
logAddress: Bytes!
logIndex: BigInt!
transactionHash: Bytes!
transactionFrom: Bytes!
transactionTo: Bytes
blockNumber: BigInt!
timestamp: BigInt!

from: Bytes!
to: Bytes!
value: BigInt!
}
24 changes: 18 additions & 6 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
{

"compilerOptions": {
"target": "es2018",
"module": "commonjs",
"lib": [
"es6",
"dom"
"es2018",
"dom"
],

}
}
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"types": [
"@graphprotocol/graph-ts"
]
},
"include": [
"src/**/*",
"script/**/*"
]
}
Loading