Skip to content

Commit

Permalink
fix: queryContracts
Browse files Browse the repository at this point in the history
  • Loading branch information
Hayden0323 committed Jun 21, 2022
1 parent f90621c commit d1b4896
Show file tree
Hide file tree
Showing 20 changed files with 131 additions and 97 deletions.
28 changes: 14 additions & 14 deletions moonriver.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,61 +20,61 @@ network:
file: ./dist/chaintypes.js
dataSources:
- kind: substrate/Moonbeam
startBlock: 790833
startBlock: 790833
processor:
file: './node_modules/@subql/contract-processors/dist/moonbeam.js'
file: './dist/moonbeamEvm.js'
options:
abi: Factory
address: '0xf36AE63d89983E3aeA8AaaD1086C3280eb01438D'
assets:
Factory:
file: ./abis/factory.json
file: ./src/abis/factory.json
ERC20:
file: ./abis/ERC20.json
file: ./src/abis/ERC20.json
ERC20SymbolBytes:
file: ./abis/ERC20SymbolBytes.json
file: ./src/abis/ERC20SymbolBytes.json
ERC20NameBytes:
file: ./abis/ERC20NameBytes.json
file: ./src/abis/ERC20NameBytes.json
mapping:
file: ./dist/index.js
handlers:
- handler: handleNewPair
kind: substrate/MoonbeamEvent
filter:
topics:
- PairCreated(indexed address,indexed address,address,uint256)
- PairCreated(address indexed token0,address indexed token1,address pair,uint256)
templates:
- name: Pair
kind: substrate/Moonbeam
processor:
file: './node_modules/@subql/contract-processors/dist/moonbeam.js'
file: './dist/moonbeamEvm.js'
options:
abi: Pair
assets:
Pair:
file: ./abis/pair.json
file: ./src/abis/pair.json
Factory:
file: ./abis/factory.json
file: ./src/abis/factory.json
mapping:
file: './dist/index.js'
handlers:
- handler: handleMint
kind: substrate/MoonbeamEvent
filter:
topics:
- Mint(indexed address,uint256,uint256)
- Mint(address indexed sender,uint256 amount0,uint256 amount1)
- handler: handleBurn
kind: substrate/MoonbeamEvent
filter:
topics:
- Burn(indexed address,uint256,uint256,indexed address)
- Burn(address indexed sender,uint256 amount0,uint256 amount1,address indexed to)
- handler: handleSwap
kind: substrate/MoonbeamEvent
filter:
topics:
- Swap(indexed address,uint256,uint256,uint256,uint256,indexed address)
- Swap(address indexed sender,uint256 amount0In,uint256 amount1In,uint256 amount0Out,uint256 amount1Out,address indexed to)
- handler: handleTransfer
kind: substrate/MoonbeamEvent
filter:
topics:
- Transfer(indexed address,indexed address,uint256)
- Transfer(address indexed from,address indexed to,uint256 value)
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"main": "dist/index.js",
"private": true,
"scripts": {
"build": "tsc -b",
"build": "subql build",
"prepack": "rm -rf dist && yarn build",
"test": "jest",
"codegen": "./node_modules/.bin/subql codegen",
Expand All @@ -20,7 +20,7 @@
"project.yaml"
],
"workspaces": [
"packages/*"
"src/constants/**"
],
"author": "Hayden0323",
"license": "MIT",
Expand All @@ -35,6 +35,7 @@
"@subql/cli": "latest",
"@subql/contract-processors": "^0.5.4",
"@subql/frontier-evm-processor": "^0.0.3-0",
"@subql/moonbeam-evm-processor": "latest",
"@subql/node": "^1.1.2",
"@subql/types": "latest",
"@typescript-eslint/eslint-plugin": "^5.28.0",
Expand All @@ -43,11 +44,12 @@
"ejs": "^3.1.6",
"ethers": "^5.6.8",
"handlebars": "^4.7.7",
"moonbeam-types-bundle": "^2.0.3",
"moonbeam-types-bundle": "latest",
"mustache": "^4.2.0",
"typescript": "^4.0.0"
},
"exports": {
"chaintypes": "./src/chaintypes.ts"
"chaintypes": "./src/chaintypes.ts",
"moonbeamEvm": "./node_modules/@subql/moonbeam-evm-processor/dist/index.js"
}
}
26 changes: 13 additions & 13 deletions project.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,59 +22,59 @@ dataSources:
- kind: substrate/Moonbeam
startBlock: 790833
processor:
file: './node_modules/@subql/contract-processors/dist/moonbeam.js'
file: './dist/moonbeamEvm.js'
options:
abi: Factory
address: '0xf36AE63d89983E3aeA8AaaD1086C3280eb01438D'
assets:
Factory:
file: ./abis/factory.json
file: ./src/abis/factory.json
ERC20:
file: ./abis/ERC20.json
file: ./src/abis/ERC20.json
ERC20SymbolBytes:
file: ./abis/ERC20SymbolBytes.json
file: ./src/abis/ERC20SymbolBytes.json
ERC20NameBytes:
file: ./abis/ERC20NameBytes.json
file: ./src/abis/ERC20NameBytes.json
mapping:
file: ./dist/index.js
handlers:
- handler: handleNewPair
kind: substrate/MoonbeamEvent
filter:
topics:
- PairCreated(indexed address,indexed address,address,uint256)
- PairCreated(address indexed token0,address indexed token1,address pair,uint256)
templates:
- name: Pair
kind: substrate/Moonbeam
processor:
file: './node_modules/@subql/contract-processors/dist/moonbeam.js'
file: './dist/moonbeamEvm.js'
options:
abi: Pair
assets:
Pair:
file: ./abis/pair.json
file: ./src/abis/pair.json
Factory:
file: ./abis/factory.json
file: ./src/abis/factory.json
mapping:
file: './dist/index.js'
handlers:
- handler: handleMint
kind: substrate/MoonbeamEvent
filter:
topics:
- Mint(indexed address,uint256,uint256)
- Mint(address indexed sender,uint256 amount0,uint256 amount1)
- handler: handleBurn
kind: substrate/MoonbeamEvent
filter:
topics:
- Burn(indexed address,uint256,uint256,indexed address)
- Burn(address indexed sender,uint256 amount0,uint256 amount1,address indexed to)
- handler: handleSwap
kind: substrate/MoonbeamEvent
filter:
topics:
- Swap(indexed address,uint256,uint256,uint256,uint256,indexed address)
- Swap(address indexed sender,uint256 amount0In,uint256 amount1In,uint256 amount0Out,uint256 amount1Out,address indexed to)
- handler: handleTransfer
kind: substrate/MoonbeamEvent
filter:
topics:
- Transfer(indexed address,indexed address,uint256)
- Transfer(address indexed from,address indexed to,uint256 value)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"scripts": {
"codegen": "true",
"build": "true",
"prepare:moonriver": "mustache ../../config/moonriver.json index.template.ts > index.ts",
"prepare:moonbeam": "mustache ../../config/moonbeam.json index.template.ts > index.ts"
"prepare:moonriver": "mustache ../config/moonriver.json index.template.ts > index.ts",
"prepare:moonbeam": "mustache ../config/moonbeam.json index.template.ts > index.ts"
}
}
14 changes: 7 additions & 7 deletions src/mappings/core.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ADDRESS_ZERO, FACTORY_ADDRESS } from '../../packages/constants'
import { ADDRESS_ZERO, FACTORY_ADDRESS } from '../constants'
import {
Pair,
Token,
Expand Down Expand Up @@ -26,8 +26,8 @@ import {
ZERO_BN,
} from './helpers'
import { BigNumber, Contract } from 'ethers'
import { MoonbeamEvent } from '@subql/contract-processors/dist/moonbeam'
import PairAbi from '../../abis/pair.json'
import { MoonbeamEvent } from '@subql/moonbeam-evm-processor'
import PairAbi from '../abis/pair.json'
import BigDecimal from 'bignumber.js'

type TransferEventArgs = [string, string, BigNumber] & { from: string, to: string, value: BigNumber }
Expand All @@ -37,7 +37,7 @@ type SwapEventArgs = [string, BigNumber, BigNumber, BigNumber, BigNumber, string
{ sender: string, amount0In: BigNumber, amount1In: BigNumber, amount0Out: BigNumber, amount1Out: BigNumber, to: string }

async function isCompleteMint(mintId: string): Promise<boolean> {
return (await MintEvent.get(mintId)).sender !== null // sufficient checks
return !!(await MintEvent.get(mintId)).sender // sufficient checks
}

export async function handleTransfer(event: MoonbeamEvent<TransferEventArgs>): Promise<void> {
Expand All @@ -64,7 +64,7 @@ export async function handleTransfer(event: MoonbeamEvent<TransferEventArgs>): P

// get or create transaction
let transaction = await Transaction.get(transactionHash)
if (transaction === null) {
if (!transaction) {
transaction = new Transaction(transactionHash)
transaction.blockNumber = numberToBigint(event.blockNumber)
transaction.timestamp = numberToBigint(event.blockTimestamp.getTime())
Expand Down Expand Up @@ -369,7 +369,7 @@ export async function handleBurn(event: MoonbeamEvent<BurnEventArgs>): Promise<v
let transaction = await Transaction.get(event.transactionHash)

// safety check
if (transaction === null) {
if (!transaction) {
return
}

Expand Down Expand Up @@ -501,7 +501,7 @@ export async function handleSwap(event: MoonbeamEvent<SwapEventArgs>): Promise<v
await uniswap.save()

let transaction = await Transaction.get(event.transactionHash)
if (transaction === null) {
if (!transaction) {
transaction = new Transaction(event.transactionHash)
transaction.blockNumber = numberToBigint(event.blockNumber)
transaction.timestamp = numberToBigint(event.blockTimestamp.getTime())
Expand Down
10 changes: 5 additions & 5 deletions src/mappings/dayUpdates.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MoonbeamEvent } from '@subql/contract-processors/dist/moonbeam';
import { BigNumber } from 'ethers';
import { FACTORY_ADDRESS } from '../../packages/constants'
import { FACTORY_ADDRESS } from '../constants'
import { Bundle, Pair, PairDayData, Token, TokenDayData, ZenlinkDayData, Factory, PairHourData } from '../types'
import { ONE_BI, ZERO_BI } from './helpers';

Expand All @@ -10,7 +10,7 @@ export async function updateUniswapDayData(event: MoonbeamEvent): Promise<Zenlin
let dayID = timestamp / 86400
let dayStartTimestamp = dayID * 86400
let uniswapDayData = await ZenlinkDayData.get(dayID.toString())
if (uniswapDayData === null) {
if (!uniswapDayData) {
uniswapDayData = new ZenlinkDayData(dayID.toString())
uniswapDayData.date = dayStartTimestamp
uniswapDayData.dailyVolumeUSD = 0
Expand All @@ -37,7 +37,7 @@ export async function updatePairDayData(event: MoonbeamEvent): Promise<PairDayDa
.concat(BigNumber.from(dayID).toString())
let pair = await Pair.get(event.address)
let pairDayData = await PairDayData.get(dayPairID)
if (pairDayData === null) {
if (!pairDayData) {
pairDayData = new PairDayData(dayPairID)
pairDayData.date = dayStartTimestamp
pairDayData.token0Id = pair.token0Id
Expand Down Expand Up @@ -68,7 +68,7 @@ export async function updatePairHourData(event: MoonbeamEvent): Promise<PairHour
.concat(BigNumber.from(hourIndex).toString())
let pair = await Pair.get(event.address)
let pairHourData = await PairHourData.get(hourPairID)
if (pairHourData === null) {
if (!pairHourData) {
pairHourData = new PairHourData(hourPairID)
pairHourData.hourStartUnix = hourStartUnix
pairHourData.pairId = event.address
Expand Down Expand Up @@ -99,7 +99,7 @@ export async function updateTokenDayData(token: Token, event: MoonbeamEvent): Pr
.concat(BigNumber.from(dayID).toString())

let tokenDayData = await TokenDayData.get(tokenDayID)
if (tokenDayData === null) {
if (!tokenDayData) {
tokenDayData = new TokenDayData(tokenDayID)
tokenDayData.date = dayStartTimestamp
tokenDayData.tokenId = token.id
Expand Down
26 changes: 14 additions & 12 deletions src/mappings/factory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FACTORY_ADDRESS } from '../../packages/constants'
import { FACTORY_ADDRESS } from '../constants'
import { Bundle, Pair, Token, Factory, createPairDatasource } from '../types'
import {
fetchTokenDecimals,
Expand All @@ -8,14 +8,14 @@ import {
numberToBigint,
ZERO_BI,
} from './helpers'
import { MoonbeamEvent } from '@subql/contract-processors/dist/moonbeam'
import { MoonbeamEvent } from '@subql/moonbeam-evm-processor'

type PairCreatedEventArgs = [string, string, string] & { token0: string, token1: string, pair: string }

export async function handleNewPair(event: MoonbeamEvent<PairCreatedEventArgs>): Promise<void> {
// load factory (create if first exchange)
let factory = await Factory.get(FACTORY_ADDRESS)
if (factory === null) {
if (!factory) {
factory = new Factory(FACTORY_ADDRESS)
factory.pairCount = 0
factory.totalVolumeETH =0
Expand All @@ -28,7 +28,7 @@ export async function handleNewPair(event: MoonbeamEvent<PairCreatedEventArgs>):
// create new bundle
let bundle = new Bundle('1')
bundle.ethPrice = 0
bundle.save()
await bundle.save()
}
factory.pairCount = factory.pairCount + 1
await factory.save()
Expand All @@ -38,15 +38,15 @@ export async function handleNewPair(event: MoonbeamEvent<PairCreatedEventArgs>):
let token1 = await Token.get(event.args.token1)

// fetch info if null
if (token0 === null) {
if (!token0) {
token0 = new Token(event.args.token0)
token0.symbol = await fetchTokenSymbol(event.args.token0)
token0.name = await fetchTokenName(event.args.token0)
token0.totalSupply = (await fetchTokenTotalSupply(event.args.token0)).toBigInt()
let decimals = await fetchTokenDecimals(event.args.token0)
const decimals = await fetchTokenDecimals(event.args.token0)

// bail if we couldn't figure out the decimals
if (decimals === null) {
if (!decimals) {
logger.debug('mybug the decimal on token 0 was null', [])
return
}
Expand All @@ -61,15 +61,16 @@ export async function handleNewPair(event: MoonbeamEvent<PairCreatedEventArgs>):
}

// fetch info if null
if (token1 === null) {
if (!token1) {
token1 = new Token(event.args.token1)
token1.symbol = await fetchTokenSymbol(event.args.token1)
token1.name = await fetchTokenName(event.args.token1)
token1.totalSupply = (await fetchTokenTotalSupply(event.args.token1)).toBigInt()
let decimals = await fetchTokenDecimals(event.args.token1)
const decimals = await fetchTokenDecimals(event.args.token1)

// bail if we couldn't figure out the decimals
if (decimals === null) {
if (!decimals) {
logger.debug('mybug the decimal on token 1 was null', [])
return
}
token1.decimals = decimals.toBigInt()
Expand All @@ -81,7 +82,7 @@ export async function handleNewPair(event: MoonbeamEvent<PairCreatedEventArgs>):
token1.txCount = ZERO_BI
}

let pair = new Pair(event.args.pair)
const pair = new Pair(event.args.pair)
pair.token0Id = token0.id
pair.token1Id = token1.id
pair.liquidityProviderCount = ZERO_BI
Expand All @@ -102,7 +103,8 @@ export async function handleNewPair(event: MoonbeamEvent<PairCreatedEventArgs>):
pair.token1Price = 0

// create the tracked contract based on the template
await createPairDatasource({address: event.args.pair})
await createPairDatasource({ address: event.args.pair })
logger.info(event.args.pair)

// save updated values
await token0.save()
Expand Down
Loading

0 comments on commit d1b4896

Please sign in to comment.