Skip to content

Commit 8494641

Browse files
authored
Global stats (#319)
* partial global stats * fix integer * fixes * liquidty and swap
1 parent 5f67254 commit 8494641

11 files changed

+167
-40
lines changed

schema.graphql

+47-11
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ type Token @entity {
5959

6060
"utility type"
6161
type TokenValuePair @entity {
62+
"address of the token"
6263
id : ID!
6364
token : Token!
6465
value : BigDecimal!
@@ -387,23 +388,58 @@ type PoolSnapshot @entity {
387388
datatokenLiquidity: BigDecimal!
388389

389390
}
390-
type GlobalStats @entity {
391+
392+
"utility type"
393+
type GlobalTotalLiquidityPair @entity {
394+
"address of the token"
395+
id : ID!
396+
globalStatistic: GlobalStatistic!
397+
token : Token!
398+
value : BigDecimal!
399+
}
400+
401+
"utility type"
402+
type GlobalTotalPoolSwapPair @entity {
403+
"address of the token"
404+
id : ID!
405+
globalStatistic: GlobalStatistic!
406+
token : Token!
407+
value : BigDecimal!
408+
}
409+
"utility type"
410+
type GlobalTotalFixedSwapPair @entity {
411+
"address of the token"
412+
id : ID!
413+
globalStatistic: GlobalStatistic!
414+
token : Token!
415+
value : BigDecimal!
416+
}
417+
type GlobalStatistic @entity {
391418
id: ID!
392419

393-
"total liquidity for each base token"
394-
totalLiquidity: [TokenValuePair!]!
395-
"total swap volume for each base token. pools and fre"
396-
totalSwapVolume: [TokenValuePair!]!
420+
"total liquidity for each base token in pools"
421+
totalLiquidity: [GlobalTotalLiquidityPair!]! @derivedFrom(field: "globalStatistic")
422+
"total swap volume for each base token in pools"
423+
totalPoolSwapVolume: [GlobalTotalPoolSwapPair!]! @derivedFrom(field: "globalStatistic")
424+
425+
"total swap volume for each base token in fixed rate exchanges"
426+
totalFixedSwapVolume: [GlobalTotalFixedSwapPair!] @derivedFrom(field: "globalStatistic")
397427

398-
"number of total consumes, pools + fre+ free"
399-
orderCount: BigInt!
428+
"number of total orders. pool orders + fixed rate exchange orders + dispenser orders"
429+
orderCount: Int!
400430

401-
"total nfts created"
402-
nftCount: BigInt!
403-
"total datatokens created"
404-
datatokenCount:BigInt!
431+
"total nfts(erc721) created"
432+
nftCount: Int!
433+
"total datatokens (tokens with isDatatoken = true) created"
434+
datatokenCount:Int!
405435
"number of pools"
406436
poolCount: Int!
437+
438+
"number of fixed rate exchanges"
439+
fixedCount: Int!
440+
441+
"number of dispensers created"
442+
dispenserCount: Int!
407443
}
408444

409445

src/mappings/dispenser.ts

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { Dispenser, DispenserTransaction } from '../@types/schema'
1010
import { decimal } from './utils/constants'
1111
import { getDispenser } from './utils/dispenserUtils'
1212
import { weiToDecimal } from './utils/generic'
13+
import { addDispenser } from './utils/globalUtils'
1314
import { getToken } from './utils/tokenUtils'
1415
import { getUser } from './utils/userUtils'
1516

@@ -34,6 +35,8 @@ export function handleNewDispenser(event: DispenserCreated): void {
3435
dispenser.tx = event.transaction.hash.toHex()
3536
dispenser.block = event.block.number.toI32()
3637
dispenser.save()
38+
39+
addDispenser()
3740
}
3841

3942
export function handleActivate(event: DispenserActivated): void {

src/mappings/erc20Templates.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77

88
import { integer } from './utils/constants'
99
import { weiToDecimal } from './utils/generic'
10-
import { getGlobalStats } from './utils/globalUtils'
10+
import { addOrder } from './utils/globalUtils'
1111
import { getToken } from './utils/tokenUtils'
1212
import { getUser } from './utils/userUtils'
1313

@@ -55,12 +55,9 @@ export function handleOrderStarted(event: OrderStarted): void {
5555
order.tx = event.transaction.hash.toHex()
5656
order.block = event.block.number.toI32()
5757

58-
const globalStats = getGlobalStats()
59-
globalStats.orderCount = globalStats.orderCount.plus(integer.ONE)
60-
61-
globalStats.save()
6258
order.save()
6359
token.save()
60+
addOrder()
6461
}
6562

6663
export function handleNewPaymentCollector(event: NewPaymentCollector): void {}

src/mappings/erc721Factory.ts

+4-10
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import { log } from '@graphprotocol/graph-ts'
22
import { NFTCreated, TokenCreated } from '../@types/ERC721Factory/ERC721Factory'
33
import { Nft, Token } from '../@types/schema'
44
import { ERC20Template, ERC721Template } from '../@types/templates'
5-
import { decimal, integer } from './utils/constants'
5+
import { decimal } from './utils/constants'
66
import { weiToDecimal } from './utils/generic'
7-
import { getGlobalStats } from './utils/globalUtils'
7+
import { addDatatoken, addNft } from './utils/globalUtils'
88
import { getUser } from './utils/userUtils'
99

1010
export function handleNftCreated(event: NFTCreated): void {
@@ -21,10 +21,7 @@ export function handleNftCreated(event: NFTCreated): void {
2121
nft.tx = event.transaction.hash.toHex()
2222
nft.block = event.block.number.toI32()
2323

24-
const globalStats = getGlobalStats()
25-
globalStats.nftCount = globalStats.nftCount.plus(integer.ONE)
26-
27-
globalStats.save()
24+
addNft()
2825
nft.save()
2926
}
3027

@@ -45,9 +42,6 @@ export function handleNewToken(event: TokenCreated): void {
4542
token.supply = decimal.ZERO
4643
token.cap = weiToDecimal(event.params.cap.toBigDecimal(), 18)
4744

48-
const globalStats = getGlobalStats()
49-
globalStats.datatokenCount = globalStats.datatokenCount.plus(integer.ONE)
50-
51-
globalStats.save()
5245
token.save()
46+
addDatatoken()
5347
}

src/mappings/factoryRouter.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { NewPool } from '../@types/FactoryRouter/FactoryRouter'
22
import { Pool } from '../@types/schema'
33
import { BPool } from '../@types/templates'
4+
import { addPool } from './utils/globalUtils'
45

56
export function handleNewPool(event: NewPool): void {
67
BPool.create(event.params.poolAddress)
78
const pool = new Pool(event.params.poolAddress.toHexString())
89
pool.save()
10+
addPool()
911
}

src/mappings/fixedRateExchange.ts

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
} from '../@types/schema'
1616
import { getFixedRateExchange, getUpdateOrSwapId } from './utils/fixedRateUtils'
1717
import { weiToDecimal } from './utils/generic'
18+
import { addFixedRateExchange } from './utils/globalUtils'
1819
import { getToken } from './utils/tokenUtils'
1920
import { getUser } from './utils/userUtils'
2021

@@ -40,6 +41,8 @@ export function handleExchangeCreated(event: ExchangeCreated): void {
4041
fixedRateExchange.tx = event.transaction.hash.toHex()
4142
fixedRateExchange.block = event.block.number.toI32()
4243
fixedRateExchange.save()
44+
45+
addFixedRateExchange()
4346
}
4447

4548
export function handleRateChange(event: ExchangeRateChanged): void {

src/mappings/nftUpdate.ts

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
TokenURIUpdate
77
} from '../@types/templates/ERC721Template/ERC721Template'
88
import { NftUpdateType } from './utils/constants'
9-
109
function getId(tx: string, nftAddress: string): string {
1110
return `${tx}-${nftAddress}`
1211
}

src/mappings/pool.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ import {
77
import { Transfer } from '../@types/templates/BPool/BToken'
88
import { integer, PoolTransactionType, ZERO_ADDRESS } from './utils/constants'
99
import { weiToDecimal } from './utils/generic'
10-
import { getGlobalStats } from './utils/globalUtils'
10+
import {
11+
addLiquidity,
12+
addPoolSwap,
13+
getGlobalStats,
14+
removeLiquidity
15+
} from './utils/globalUtils'
1116
import {
1217
calcSpotPrice,
1318
getPool,
@@ -50,6 +55,8 @@ export function handleJoin(event: LOG_JOIN): void {
5055
poolSnapshot.baseTokenLiquidity.plus(ammount)
5156

5257
pool.baseTokenLiquidity = pool.baseTokenLiquidity.plus(ammount)
58+
59+
addLiquidity(token.id, ammount)
5360
}
5461

5562
poolSnapshot.save()
@@ -88,6 +95,7 @@ export function handleExit(event: LOG_EXIT): void {
8895
poolSnapshot.baseTokenLiquidity.minus(ammount)
8996

9097
pool.baseTokenLiquidity.minus(ammount)
98+
removeLiquidity(token.id, ammount)
9199
}
92100

93101
poolSnapshot.save()
@@ -126,6 +134,9 @@ export function handleSwap(event: LOG_SWAP): void {
126134

127135
poolSnapshot.baseTokenLiquidity =
128136
poolSnapshot.baseTokenLiquidity.minus(ammountOut)
137+
138+
addPoolSwap(tokenOut.id, ammountOut)
139+
removeLiquidity(tokenOut.id, ammountOut)
129140
}
130141

131142
// update pool token in
@@ -150,6 +161,9 @@ export function handleSwap(event: LOG_SWAP): void {
150161

151162
poolSnapshot.baseTokenLiquidity =
152163
poolSnapshot.baseTokenLiquidity.plus(ammountIn)
164+
165+
addLiquidity(tokenIn.id, ammountIn)
166+
addPoolSwap(tokenIn.id, ammountIn)
153167
}
154168

155169
// update spot price

src/mappings/utils/fixedRateUtils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export function getFixedRateExchange(exchangeId: string): FixedRateExchange {
44
let fixedRateExhange = FixedRateExchange.load(exchangeId)
55
if (fixedRateExhange === null) {
66
fixedRateExhange = new FixedRateExchange(exchangeId)
7-
// TODO: get data from contract and fill in new fixed rate exchange
7+
// 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
88
fixedRateExhange.save()
99
}
1010

src/mappings/utils/globalUtils.ts

+90-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,101 @@
11
import { BigDecimal } from '@graphprotocol/graph-ts'
2-
import { GlobalStats } from '../../@types/schema'
2+
import {
3+
GlobalStatistic,
4+
GlobalTotalFixedSwapPair,
5+
GlobalTotalLiquidityPair,
6+
GlobalTotalPoolSwapPair
7+
} from '../../@types/schema'
38

49
const GLOBAL_ID = '1'
510

6-
export function getGlobalStats(): GlobalStats {
7-
let globalStats = GlobalStats.load(GLOBAL_ID)
8-
if (!globalStats) globalStats = new GlobalStats(GLOBAL_ID)
11+
export function getGlobalStats(): GlobalStatistic {
12+
let globalStats = GlobalStatistic.load(GLOBAL_ID)
13+
if (!globalStats) {
14+
globalStats = new GlobalStatistic(GLOBAL_ID)
15+
globalStats.save()
16+
}
917
return globalStats
1018
}
19+
export function addOrder(): void {
20+
const globalStats = getGlobalStats()
21+
globalStats.orderCount = globalStats.orderCount + 1
22+
globalStats.save()
23+
}
24+
25+
export function addDatatoken(): void {
26+
const globalStats = getGlobalStats()
27+
globalStats.datatokenCount = globalStats.datatokenCount + 1
28+
globalStats.save()
29+
}
30+
31+
export function addNft(): void {
32+
const globalStats = getGlobalStats()
33+
globalStats.nftCount = globalStats.nftCount + 1
34+
globalStats.save()
35+
}
36+
37+
export function addFixedRateExchange(): void {
38+
const globalStats = getGlobalStats()
39+
globalStats.fixedCount = globalStats.fixedCount + 1
40+
globalStats.save()
41+
}
1142

12-
export function addSwap(tokenAddress: string, value: BigDecimal): void {
43+
export function addDispenser(): void {
1344
const globalStats = getGlobalStats()
45+
globalStats.dispenserCount = globalStats.dispenserCount + 1
46+
globalStats.save()
47+
}
1448

49+
export function addPool(): void {
50+
const globalStats = getGlobalStats()
51+
globalStats.poolCount = globalStats.poolCount + 1
1552
globalStats.save()
1653
}
54+
55+
export function addPoolSwap(tokenAddress: string, value: BigDecimal): void {
56+
let poolSwapPair = GlobalTotalPoolSwapPair.load(tokenAddress)
57+
if (!poolSwapPair) {
58+
poolSwapPair = new GlobalTotalPoolSwapPair(tokenAddress)
59+
poolSwapPair.globalStatistic = GLOBAL_ID
60+
poolSwapPair.token = tokenAddress
61+
}
62+
poolSwapPair.value = poolSwapPair.value.plus(value)
63+
64+
poolSwapPair.save()
65+
}
66+
67+
export function addFixedSwap(tokenAddress: string, value: BigDecimal): void {
68+
let fixedSwapPair = GlobalTotalFixedSwapPair.load(tokenAddress)
69+
if (!fixedSwapPair) {
70+
fixedSwapPair = new GlobalTotalFixedSwapPair(tokenAddress)
71+
fixedSwapPair.globalStatistic = GLOBAL_ID
72+
fixedSwapPair.token = tokenAddress
73+
}
74+
fixedSwapPair.value = fixedSwapPair.value.plus(value)
75+
76+
fixedSwapPair.save()
77+
}
78+
79+
export function addLiquidity(tokenAddress: string, value: BigDecimal): void {
80+
let liquidityPair = GlobalTotalLiquidityPair.load(tokenAddress)
81+
if (!liquidityPair) {
82+
liquidityPair = new GlobalTotalLiquidityPair(tokenAddress)
83+
liquidityPair.globalStatistic = GLOBAL_ID
84+
liquidityPair.token = tokenAddress
85+
}
86+
liquidityPair.value = liquidityPair.value.plus(value)
87+
88+
liquidityPair.save()
89+
}
90+
91+
export function removeLiquidity(tokenAddress: string, value: BigDecimal): void {
92+
let liquidityPair = GlobalTotalLiquidityPair.load(tokenAddress)
93+
if (!liquidityPair) {
94+
liquidityPair = new GlobalTotalLiquidityPair(tokenAddress)
95+
liquidityPair.globalStatistic = GLOBAL_ID
96+
liquidityPair.token = tokenAddress
97+
}
98+
liquidityPair.value = liquidityPair.value.minus(value)
99+
100+
liquidityPair.save()
101+
}

src/mappings/utils/tokenUtils.ts

-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { Address, log } from '@graphprotocol/graph-ts'
22
import { Token } from '../../@types/schema'
33
import { ERC20 } from '../../@types/templates/ERC20Template/ERC20'
4-
import { integer } from './constants'
5-
import { getGlobalStats } from './globalUtils'
64

75
export function createToken(address: string): Token {
86
log.debug('started creating token with address: {}', [address])
@@ -13,10 +11,6 @@ export function createToken(address: string): Token {
1311
token.address = address
1412
token.isDatatoken = false
1513
token.decimals = contract.decimals()
16-
const globalStats = getGlobalStats()
17-
globalStats.datatokenCount = globalStats.datatokenCount.plus(integer.ONE)
18-
19-
globalStats.save()
2014
token.save()
2115
return token
2216
}

0 commit comments

Comments
 (0)