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"
type TokenValuePair @entity {
"address of the token"
id : ID!
token : Token!
value : BigDecimal!
@ -387,23 +388,58 @@ type PoolSnapshot @entity {
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!
"total liquidity for each base token"
totalLiquidity: [TokenValuePair!]!
"total swap volume for each base token. pools and fre"
totalSwapVolume: [TokenValuePair!]!
"number of total consumes, pools + fre+ free"
orderCount: BigInt!
"total liquidity for each base token in pools"
totalLiquidity: [GlobalTotalLiquidityPair!]! @derivedFrom(field: "globalStatistic")
"total swap volume for each base token in pools"
totalPoolSwapVolume: [GlobalTotalPoolSwapPair!]! @derivedFrom(field: "globalStatistic")
"total nfts created"
nftCount: BigInt!
"total datatokens created"
datatokenCount:BigInt!
"total swap volume for each base token in fixed rate exchanges"
totalFixedSwapVolume: [GlobalTotalFixedSwapPair!] @derivedFrom(field: "globalStatistic")
"number of total orders. pool orders + fixed rate exchange orders + dispenser orders"
orderCount: Int!
"total nfts(erc721) created"
nftCount: Int!
"total datatokens (tokens with isDatatoken = true) created"
datatokenCount:Int!
"number of pools"
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 { getDispenser } from './utils/dispenserUtils'
import { weiToDecimal } from './utils/generic'
import { addDispenser } from './utils/globalUtils'
import { getToken } from './utils/tokenUtils'
import { getUser } from './utils/userUtils'
@ -34,6 +35,8 @@ export function handleNewDispenser(event: DispenserCreated): void {
dispenser.tx = event.transaction.hash.toHex()
dispenser.block = event.block.number.toI32()
dispenser.save()
addDispenser()
}
export function handleActivate(event: DispenserActivated): void {

View File

@ -7,7 +7,7 @@ import {
import { integer } from './utils/constants'
import { weiToDecimal } from './utils/generic'
import { getGlobalStats } from './utils/globalUtils'
import { addOrder } from './utils/globalUtils'
import { getToken } from './utils/tokenUtils'
import { getUser } from './utils/userUtils'
@ -55,12 +55,9 @@ export function handleOrderStarted(event: OrderStarted): void {
order.tx = event.transaction.hash.toHex()
order.block = event.block.number.toI32()
const globalStats = getGlobalStats()
globalStats.orderCount = globalStats.orderCount.plus(integer.ONE)
globalStats.save()
order.save()
token.save()
addOrder()
}
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 { Nft, Token } from '../@types/schema'
import { ERC20Template, ERC721Template } from '../@types/templates'
import { decimal, integer } from './utils/constants'
import { decimal } from './utils/constants'
import { weiToDecimal } from './utils/generic'
import { getGlobalStats } from './utils/globalUtils'
import { addDatatoken, addNft } from './utils/globalUtils'
import { getUser } from './utils/userUtils'
export function handleNftCreated(event: NFTCreated): void {
@ -21,10 +21,7 @@ export function handleNftCreated(event: NFTCreated): void {
nft.tx = event.transaction.hash.toHex()
nft.block = event.block.number.toI32()
const globalStats = getGlobalStats()
globalStats.nftCount = globalStats.nftCount.plus(integer.ONE)
globalStats.save()
addNft()
nft.save()
}
@ -45,9 +42,6 @@ export function handleNewToken(event: TokenCreated): void {
token.supply = decimal.ZERO
token.cap = weiToDecimal(event.params.cap.toBigDecimal(), 18)
const globalStats = getGlobalStats()
globalStats.datatokenCount = globalStats.datatokenCount.plus(integer.ONE)
globalStats.save()
token.save()
addDatatoken()
}

View File

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

View File

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

View File

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

View File

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

View File

@ -4,7 +4,7 @@ export function getFixedRateExchange(exchangeId: string): FixedRateExchange {
let fixedRateExhange = FixedRateExchange.load(exchangeId)
if (fixedRateExhange === null) {
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()
}

View File

@ -1,16 +1,101 @@
import { BigDecimal } from '@graphprotocol/graph-ts'
import { GlobalStats } from '../../@types/schema'
import {
GlobalStatistic,
GlobalTotalFixedSwapPair,
GlobalTotalLiquidityPair,
GlobalTotalPoolSwapPair
} from '../../@types/schema'
const GLOBAL_ID = '1'
export function getGlobalStats(): GlobalStats {
let globalStats = GlobalStats.load(GLOBAL_ID)
if (!globalStats) globalStats = new GlobalStats(GLOBAL_ID)
export function getGlobalStats(): GlobalStatistic {
let globalStats = GlobalStatistic.load(GLOBAL_ID)
if (!globalStats) {
globalStats = new GlobalStatistic(GLOBAL_ID)
globalStats.save()
}
return globalStats
}
export function addSwap(tokenAddress: string, value: BigDecimal): void {
export function addOrder(): void {
const globalStats = getGlobalStats()
globalStats.orderCount = globalStats.orderCount + 1
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 { Token } from '../../@types/schema'
import { ERC20 } from '../../@types/templates/ERC20Template/ERC20'
import { integer } from './constants'
import { getGlobalStats } from './globalUtils'
export function createToken(address: string): Token {
log.debug('started creating token with address: {}', [address])
@ -13,10 +11,6 @@ export function createToken(address: string): Token {
token.address = address
token.isDatatoken = false
token.decimals = contract.decimals()
const globalStats = getGlobalStats()
globalStats.datatokenCount = globalStats.datatokenCount.plus(integer.ONE)
globalStats.save()
token.save()
return token
}