Global stats (#319)

* partial global stats

* fix integer

* fixes

* liquidty and swap
This commit is contained in:
mihaisc 2022-02-15 18:13:55 +02:00 committed by GitHub
parent 5f67254d42
commit 849464135f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 170 additions and 43 deletions

View File

@ -59,6 +59,7 @@ type Token @entity {
"utility type" "utility type"
type TokenValuePair @entity { type TokenValuePair @entity {
"address of the token"
id : ID! id : ID!
token : Token! token : Token!
value : BigDecimal! value : BigDecimal!
@ -387,23 +388,58 @@ type PoolSnapshot @entity {
datatokenLiquidity: BigDecimal! datatokenLiquidity: BigDecimal!
} }
type GlobalStats @entity {
"utility type"
type GlobalTotalLiquidityPair @entity {
"address of the token"
id : ID!
globalStatistic: GlobalStatistic!
token : Token!
value : BigDecimal!
}
"utility type"
type GlobalTotalPoolSwapPair @entity {
"address of the token"
id : ID!
globalStatistic: GlobalStatistic!
token : Token!
value : BigDecimal!
}
"utility type"
type GlobalTotalFixedSwapPair @entity {
"address of the token"
id : ID!
globalStatistic: GlobalStatistic!
token : Token!
value : BigDecimal!
}
type GlobalStatistic @entity {
id: ID! id: ID!
"total liquidity for each base token" "total liquidity for each base token in pools"
totalLiquidity: [TokenValuePair!]! totalLiquidity: [GlobalTotalLiquidityPair!]! @derivedFrom(field: "globalStatistic")
"total swap volume for each base token. pools and fre" "total swap volume for each base token in pools"
totalSwapVolume: [TokenValuePair!]! totalPoolSwapVolume: [GlobalTotalPoolSwapPair!]! @derivedFrom(field: "globalStatistic")
"number of total consumes, pools + fre+ free" "total swap volume for each base token in fixed rate exchanges"
orderCount: BigInt! totalFixedSwapVolume: [GlobalTotalFixedSwapPair!] @derivedFrom(field: "globalStatistic")
"total nfts created" "number of total orders. pool orders + fixed rate exchange orders + dispenser orders"
nftCount: BigInt! orderCount: Int!
"total datatokens created"
datatokenCount:BigInt! "total nfts(erc721) created"
nftCount: Int!
"total datatokens (tokens with isDatatoken = true) created"
datatokenCount:Int!
"number of pools" "number of pools"
poolCount: Int! poolCount: Int!
"number of fixed rate exchanges"
fixedCount: Int!
"number of dispensers created"
dispenserCount: Int!
} }

View File

@ -10,6 +10,7 @@ import { Dispenser, DispenserTransaction } from '../@types/schema'
import { decimal } from './utils/constants' import { decimal } from './utils/constants'
import { getDispenser } from './utils/dispenserUtils' import { getDispenser } from './utils/dispenserUtils'
import { weiToDecimal } from './utils/generic' import { weiToDecimal } from './utils/generic'
import { addDispenser } from './utils/globalUtils'
import { getToken } from './utils/tokenUtils' import { getToken } from './utils/tokenUtils'
import { getUser } from './utils/userUtils' import { getUser } from './utils/userUtils'
@ -34,6 +35,8 @@ export function handleNewDispenser(event: DispenserCreated): void {
dispenser.tx = event.transaction.hash.toHex() dispenser.tx = event.transaction.hash.toHex()
dispenser.block = event.block.number.toI32() dispenser.block = event.block.number.toI32()
dispenser.save() dispenser.save()
addDispenser()
} }
export function handleActivate(event: DispenserActivated): void { export function handleActivate(event: DispenserActivated): void {

View File

@ -7,7 +7,7 @@ import {
import { integer } from './utils/constants' import { integer } from './utils/constants'
import { weiToDecimal } from './utils/generic' import { weiToDecimal } from './utils/generic'
import { getGlobalStats } from './utils/globalUtils' import { addOrder } from './utils/globalUtils'
import { getToken } from './utils/tokenUtils' import { getToken } from './utils/tokenUtils'
import { getUser } from './utils/userUtils' import { getUser } from './utils/userUtils'
@ -55,12 +55,9 @@ export function handleOrderStarted(event: OrderStarted): void {
order.tx = event.transaction.hash.toHex() order.tx = event.transaction.hash.toHex()
order.block = event.block.number.toI32() order.block = event.block.number.toI32()
const globalStats = getGlobalStats()
globalStats.orderCount = globalStats.orderCount.plus(integer.ONE)
globalStats.save()
order.save() order.save()
token.save() token.save()
addOrder()
} }
export function handleNewPaymentCollector(event: NewPaymentCollector): void {} export function handleNewPaymentCollector(event: NewPaymentCollector): void {}

View File

@ -2,9 +2,9 @@ import { log } from '@graphprotocol/graph-ts'
import { NFTCreated, TokenCreated } from '../@types/ERC721Factory/ERC721Factory' import { NFTCreated, TokenCreated } from '../@types/ERC721Factory/ERC721Factory'
import { Nft, Token } from '../@types/schema' import { Nft, Token } from '../@types/schema'
import { ERC20Template, ERC721Template } from '../@types/templates' import { ERC20Template, ERC721Template } from '../@types/templates'
import { decimal, integer } from './utils/constants' import { decimal } from './utils/constants'
import { weiToDecimal } from './utils/generic' import { weiToDecimal } from './utils/generic'
import { getGlobalStats } from './utils/globalUtils' import { addDatatoken, addNft } from './utils/globalUtils'
import { getUser } from './utils/userUtils' import { getUser } from './utils/userUtils'
export function handleNftCreated(event: NFTCreated): void { export function handleNftCreated(event: NFTCreated): void {
@ -21,10 +21,7 @@ export function handleNftCreated(event: NFTCreated): void {
nft.tx = event.transaction.hash.toHex() nft.tx = event.transaction.hash.toHex()
nft.block = event.block.number.toI32() nft.block = event.block.number.toI32()
const globalStats = getGlobalStats() addNft()
globalStats.nftCount = globalStats.nftCount.plus(integer.ONE)
globalStats.save()
nft.save() nft.save()
} }
@ -45,9 +42,6 @@ export function handleNewToken(event: TokenCreated): void {
token.supply = decimal.ZERO token.supply = decimal.ZERO
token.cap = weiToDecimal(event.params.cap.toBigDecimal(), 18) token.cap = weiToDecimal(event.params.cap.toBigDecimal(), 18)
const globalStats = getGlobalStats()
globalStats.datatokenCount = globalStats.datatokenCount.plus(integer.ONE)
globalStats.save()
token.save() token.save()
addDatatoken()
} }

View File

@ -1,9 +1,11 @@
import { NewPool } from '../@types/FactoryRouter/FactoryRouter' import { NewPool } from '../@types/FactoryRouter/FactoryRouter'
import { Pool } from '../@types/schema' import { Pool } from '../@types/schema'
import { BPool } from '../@types/templates' import { BPool } from '../@types/templates'
import { addPool } from './utils/globalUtils'
export function handleNewPool(event: NewPool): void { export function handleNewPool(event: NewPool): void {
BPool.create(event.params.poolAddress) BPool.create(event.params.poolAddress)
const pool = new Pool(event.params.poolAddress.toHexString()) const pool = new Pool(event.params.poolAddress.toHexString())
pool.save() pool.save()
addPool()
} }

View File

@ -15,6 +15,7 @@ import {
} from '../@types/schema' } from '../@types/schema'
import { getFixedRateExchange, getUpdateOrSwapId } from './utils/fixedRateUtils' import { getFixedRateExchange, getUpdateOrSwapId } from './utils/fixedRateUtils'
import { weiToDecimal } from './utils/generic' import { weiToDecimal } from './utils/generic'
import { addFixedRateExchange } from './utils/globalUtils'
import { getToken } from './utils/tokenUtils' import { getToken } from './utils/tokenUtils'
import { getUser } from './utils/userUtils' import { getUser } from './utils/userUtils'
@ -40,6 +41,8 @@ export function handleExchangeCreated(event: ExchangeCreated): void {
fixedRateExchange.tx = event.transaction.hash.toHex() fixedRateExchange.tx = event.transaction.hash.toHex()
fixedRateExchange.block = event.block.number.toI32() fixedRateExchange.block = event.block.number.toI32()
fixedRateExchange.save() fixedRateExchange.save()
addFixedRateExchange()
} }
export function handleRateChange(event: ExchangeRateChanged): void { export function handleRateChange(event: ExchangeRateChanged): void {

View File

@ -6,7 +6,6 @@ import {
TokenURIUpdate TokenURIUpdate
} from '../@types/templates/ERC721Template/ERC721Template' } from '../@types/templates/ERC721Template/ERC721Template'
import { NftUpdateType } from './utils/constants' import { NftUpdateType } from './utils/constants'
function getId(tx: string, nftAddress: string): string { function getId(tx: string, nftAddress: string): string {
return `${tx}-${nftAddress}` return `${tx}-${nftAddress}`
} }

View File

@ -7,7 +7,12 @@ import {
import { Transfer } from '../@types/templates/BPool/BToken' import { Transfer } from '../@types/templates/BPool/BToken'
import { integer, PoolTransactionType, ZERO_ADDRESS } from './utils/constants' import { integer, PoolTransactionType, ZERO_ADDRESS } from './utils/constants'
import { weiToDecimal } from './utils/generic' import { weiToDecimal } from './utils/generic'
import { getGlobalStats } from './utils/globalUtils' import {
addLiquidity,
addPoolSwap,
getGlobalStats,
removeLiquidity
} from './utils/globalUtils'
import { import {
calcSpotPrice, calcSpotPrice,
getPool, getPool,
@ -50,6 +55,8 @@ export function handleJoin(event: LOG_JOIN): void {
poolSnapshot.baseTokenLiquidity.plus(ammount) poolSnapshot.baseTokenLiquidity.plus(ammount)
pool.baseTokenLiquidity = pool.baseTokenLiquidity.plus(ammount) pool.baseTokenLiquidity = pool.baseTokenLiquidity.plus(ammount)
addLiquidity(token.id, ammount)
} }
poolSnapshot.save() poolSnapshot.save()
@ -88,6 +95,7 @@ export function handleExit(event: LOG_EXIT): void {
poolSnapshot.baseTokenLiquidity.minus(ammount) poolSnapshot.baseTokenLiquidity.minus(ammount)
pool.baseTokenLiquidity.minus(ammount) pool.baseTokenLiquidity.minus(ammount)
removeLiquidity(token.id, ammount)
} }
poolSnapshot.save() poolSnapshot.save()
@ -126,6 +134,9 @@ export function handleSwap(event: LOG_SWAP): void {
poolSnapshot.baseTokenLiquidity = poolSnapshot.baseTokenLiquidity =
poolSnapshot.baseTokenLiquidity.minus(ammountOut) poolSnapshot.baseTokenLiquidity.minus(ammountOut)
addPoolSwap(tokenOut.id, ammountOut)
removeLiquidity(tokenOut.id, ammountOut)
} }
// update pool token in // update pool token in
@ -150,6 +161,9 @@ export function handleSwap(event: LOG_SWAP): void {
poolSnapshot.baseTokenLiquidity = poolSnapshot.baseTokenLiquidity =
poolSnapshot.baseTokenLiquidity.plus(ammountIn) poolSnapshot.baseTokenLiquidity.plus(ammountIn)
addLiquidity(tokenIn.id, ammountIn)
addPoolSwap(tokenIn.id, ammountIn)
} }
// update spot price // update spot price

View File

@ -4,7 +4,7 @@ export function getFixedRateExchange(exchangeId: string): FixedRateExchange {
let fixedRateExhange = FixedRateExchange.load(exchangeId) let fixedRateExhange = FixedRateExchange.load(exchangeId)
if (fixedRateExhange === null) { if (fixedRateExhange === null) {
fixedRateExhange = new FixedRateExchange(exchangeId) fixedRateExhange = new FixedRateExchange(exchangeId)
// TODO: get data from contract and fill in new fixed rate exchange // TODO: get data from contract and fill in new fixed rate exchange, this is just a worst case scenario. We shouldn't reach this code
fixedRateExhange.save() fixedRateExhange.save()
} }

View File

@ -1,16 +1,101 @@
import { BigDecimal } from '@graphprotocol/graph-ts' import { BigDecimal } from '@graphprotocol/graph-ts'
import { GlobalStats } from '../../@types/schema' import {
GlobalStatistic,
GlobalTotalFixedSwapPair,
GlobalTotalLiquidityPair,
GlobalTotalPoolSwapPair
} from '../../@types/schema'
const GLOBAL_ID = '1' const GLOBAL_ID = '1'
export function getGlobalStats(): GlobalStats { export function getGlobalStats(): GlobalStatistic {
let globalStats = GlobalStats.load(GLOBAL_ID) let globalStats = GlobalStatistic.load(GLOBAL_ID)
if (!globalStats) globalStats = new GlobalStats(GLOBAL_ID) if (!globalStats) {
globalStats = new GlobalStatistic(GLOBAL_ID)
globalStats.save()
}
return globalStats return globalStats
} }
export function addOrder(): void {
export function addSwap(tokenAddress: string, value: BigDecimal): void {
const globalStats = getGlobalStats() const globalStats = getGlobalStats()
globalStats.orderCount = globalStats.orderCount + 1
globalStats.save() globalStats.save()
} }
export function addDatatoken(): void {
const globalStats = getGlobalStats()
globalStats.datatokenCount = globalStats.datatokenCount + 1
globalStats.save()
}
export function addNft(): void {
const globalStats = getGlobalStats()
globalStats.nftCount = globalStats.nftCount + 1
globalStats.save()
}
export function addFixedRateExchange(): void {
const globalStats = getGlobalStats()
globalStats.fixedCount = globalStats.fixedCount + 1
globalStats.save()
}
export function addDispenser(): void {
const globalStats = getGlobalStats()
globalStats.dispenserCount = globalStats.dispenserCount + 1
globalStats.save()
}
export function addPool(): void {
const globalStats = getGlobalStats()
globalStats.poolCount = globalStats.poolCount + 1
globalStats.save()
}
export function addPoolSwap(tokenAddress: string, value: BigDecimal): void {
let poolSwapPair = GlobalTotalPoolSwapPair.load(tokenAddress)
if (!poolSwapPair) {
poolSwapPair = new GlobalTotalPoolSwapPair(tokenAddress)
poolSwapPair.globalStatistic = GLOBAL_ID
poolSwapPair.token = tokenAddress
}
poolSwapPair.value = poolSwapPair.value.plus(value)
poolSwapPair.save()
}
export function addFixedSwap(tokenAddress: string, value: BigDecimal): void {
let fixedSwapPair = GlobalTotalFixedSwapPair.load(tokenAddress)
if (!fixedSwapPair) {
fixedSwapPair = new GlobalTotalFixedSwapPair(tokenAddress)
fixedSwapPair.globalStatistic = GLOBAL_ID
fixedSwapPair.token = tokenAddress
}
fixedSwapPair.value = fixedSwapPair.value.plus(value)
fixedSwapPair.save()
}
export function addLiquidity(tokenAddress: string, value: BigDecimal): void {
let liquidityPair = GlobalTotalLiquidityPair.load(tokenAddress)
if (!liquidityPair) {
liquidityPair = new GlobalTotalLiquidityPair(tokenAddress)
liquidityPair.globalStatistic = GLOBAL_ID
liquidityPair.token = tokenAddress
}
liquidityPair.value = liquidityPair.value.plus(value)
liquidityPair.save()
}
export function removeLiquidity(tokenAddress: string, value: BigDecimal): void {
let liquidityPair = GlobalTotalLiquidityPair.load(tokenAddress)
if (!liquidityPair) {
liquidityPair = new GlobalTotalLiquidityPair(tokenAddress)
liquidityPair.globalStatistic = GLOBAL_ID
liquidityPair.token = tokenAddress
}
liquidityPair.value = liquidityPair.value.minus(value)
liquidityPair.save()
}

View File

@ -1,8 +1,6 @@
import { Address, log } from '@graphprotocol/graph-ts' import { Address, log } from '@graphprotocol/graph-ts'
import { Token } from '../../@types/schema' import { Token } from '../../@types/schema'
import { ERC20 } from '../../@types/templates/ERC20Template/ERC20' import { ERC20 } from '../../@types/templates/ERC20Template/ERC20'
import { integer } from './constants'
import { getGlobalStats } from './globalUtils'
export function createToken(address: string): Token { export function createToken(address: string): Token {
log.debug('started creating token with address: {}', [address]) log.debug('started creating token with address: {}', [address])
@ -13,10 +11,6 @@ export function createToken(address: string): Token {
token.address = address token.address = address
token.isDatatoken = false token.isDatatoken = false
token.decimals = contract.decimals() token.decimals = contract.decimals()
const globalStats = getGlobalStats()
globalStats.datatokenCount = globalStats.datatokenCount.plus(integer.ONE)
globalStats.save()
token.save() token.save()
return token return token
} }