Skip to content

Commit b376cb3

Browse files
authored
add more veOcean stats (#655)
* add more veOcean stats
1 parent 81b43ec commit b376cb3

File tree

4 files changed

+69
-1
lines changed

4 files changed

+69
-1
lines changed

schema.graphql

+4
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,9 @@ type GlobalStatistic @entity {
338338
"number of dispensers created"
339339
dispenserCount: Int!
340340

341+
"total ocean locked in veOcean"
342+
totalOceanLocked:BigDecimal!
343+
341344
"current version"
342345
version: String
343346
}
@@ -504,6 +507,7 @@ type VeDeposit @entity {
504507
block: Int!
505508
tx: String!
506509
veOcean: VeOCEAN!
510+
totalOceanLocked:BigDecimal!
507511
}
508512

509513

src/mappings/utils/globalUtils.ts

+12
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export function getGlobalStats(): GlobalStatistic {
1919
globalStats.datatokenCount = 0
2020
globalStats.dispenserCount = 0
2121
globalStats.nftCount = 0
22+
globalStats.totalOceanLocked = BigDecimal.zero()
2223
globalStats.save()
2324
}
2425
return globalStats
@@ -33,6 +34,11 @@ export function getOPC(): OPC {
3334
return globalStats
3435
}
3536

37+
export function getTotalOceanLocked(): BigDecimal {
38+
const globalStats = getGlobalStats()
39+
return globalStats.totalOceanLocked
40+
}
41+
3642
export function getTemplates(): Template {
3743
let templates = Template.load(GLOBAL_ID)
3844
if (!templates) {
@@ -42,6 +48,12 @@ export function getTemplates(): Template {
4248
return templates
4349
}
4450

51+
export function updateTotalOceanLocked(amount: BigDecimal): void {
52+
const globalStats = getGlobalStats()
53+
globalStats.totalOceanLocked = amount
54+
globalStats.save()
55+
}
56+
4557
export function addOrder(): void {
4658
const globalStats = getGlobalStats()
4759
globalStats.orderCount = globalStats.orderCount + 1

src/mappings/veOCEAN.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@ import { BigDecimal, BigInt } from '@graphprotocol/graph-ts'
22
import { Deposit, Supply, Withdraw } from '../@types/veOCEAN/veOCEAN'
33
import { weiToDecimal } from './utils/generic'
44
import { getDeposit, getveOCEAN } from './utils/veUtils'
5+
import {
6+
getTotalOceanLocked,
7+
updateTotalOceanLocked
8+
} from './utils/globalUtils'
59

610
export function handleDeposit(event: Deposit): void {
711
const provider = event.params.provider
812
const value = event.params.value
913
const locktime = event.params.locktime
1014
const type = event.params.type
1115
const ts = event.params.ts
12-
16+
const totalOceanLocked = getTotalOceanLocked()
1317
const veOCEAN = getveOCEAN(provider.toHex())
1418
// Create new Deposit entity
1519
const deposit = getDeposit(
@@ -28,6 +32,10 @@ export function handleDeposit(event: Deposit): void {
2832
deposit.tx = event.transaction.hash.toHex()
2933
deposit.sender = event.transaction.from.toHex()
3034
deposit.veOcean = veOCEAN.id
35+
36+
deposit.totalOceanLocked = totalOceanLocked.plus(deposit.value)
37+
updateTotalOceanLocked(deposit.totalOceanLocked)
38+
3139
deposit.save()
3240
// --------------------------------------------
3341

@@ -39,6 +47,7 @@ export function handleDeposit(event: Deposit): void {
3947
}
4048
export function handleSupply(event: Supply): void {}
4149
export function handleWithdraw(event: Withdraw): void {
50+
const totalOceanLocked = getTotalOceanLocked()
4251
const provider = event.params.provider
4352
const value = event.params.value
4453
const ts = event.params.ts
@@ -61,6 +70,8 @@ export function handleWithdraw(event: Withdraw): void {
6170
deposit.tx = event.transaction.hash.toHex()
6271
deposit.sender = event.transaction.from.toHex()
6372
deposit.veOcean = veOCEAN.id
73+
deposit.totalOceanLocked = totalOceanLocked.plus(deposit.value) // it's already negated above
74+
updateTotalOceanLocked(deposit.totalOceanLocked)
6475
deposit.save()
6576
// --------------------------------------------
6677

test/integration/VeOcean.test.ts

+41
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,23 @@ function evmIncreaseTime(seconds) {
7070
})
7171
}
7272

73+
async function getTotalLockedOcean() {
74+
const initialQuery = {
75+
query: `query{
76+
globalStatistics{
77+
totalOceanLocked
78+
}
79+
}`
80+
}
81+
const initialResponse = await fetch(subgraphUrl, {
82+
method: 'POST',
83+
body: JSON.stringify(initialQuery)
84+
})
85+
const data = (await initialResponse.json()).data.globalStatistics
86+
if (data.length == 0) return 0
87+
88+
return data[0].totalOceanLocked
89+
}
7390
const minAbi = [
7491
{
7592
constant: false,
@@ -144,6 +161,7 @@ describe('veOcean tests', async () => {
144161
it('Alice should lock 100 Ocean', async () => {
145162
// since we can only lock once, we test if tx fails or not
146163
// so if there is already a lock, skip it
164+
const totalOceanLockedBefore = await getTotalLockedOcean()
147165
let currentBalance = await veOcean.getLockedAmount(Alice)
148166
let currentLock = await veOcean.lockEnd(Alice)
149167
const amount = '100'
@@ -172,6 +190,19 @@ describe('veOcean tests', async () => {
172190
currentBalance = await veOcean.getLockedAmount(Alice)
173191
currentLock = await veOcean.lockEnd(Alice)
174192
await sleep(2000)
193+
const totalOceanLockedAfter = await getTotalLockedOcean()
194+
assert(
195+
parseFloat(totalOceanLockedAfter) > parseFloat(totalOceanLockedBefore),
196+
'After (' +
197+
totalOceanLockedAfter +
198+
') shold be higher then ' +
199+
totalOceanLockedBefore
200+
)
201+
assert(
202+
parseFloat(totalOceanLockedAfter) ==
203+
parseFloat(totalOceanLockedBefore + amount),
204+
'Invalid totalOceanLockedAfter (' + totalOceanLockedAfter + ')'
205+
)
175206
const initialQuery = {
176207
query: `query {
177208
veOCEANs(id:"${Alice.toLowerCase()}"){
@@ -550,7 +581,17 @@ describe('veOcean tests', async () => {
550581
})
551582
it('Alice should withdraw locked tokens', async () => {
552583
await evmIncreaseTime(60 * 60 * 24 * 7)
584+
const totalOceanLockedBefore = await getTotalLockedOcean()
553585
await veOcean.withdraw(Alice)
586+
await sleep(2000)
587+
const totalOceanLockedAfter = await getTotalLockedOcean()
588+
assert(
589+
parseFloat(totalOceanLockedAfter) < parseFloat(totalOceanLockedBefore),
590+
'After (' +
591+
totalOceanLockedAfter +
592+
') shold be lower then ' +
593+
totalOceanLockedBefore
594+
)
554595
})
555596

556597
it('Alice should lock 100 Ocean and Delegate them to Bob', async () => {

0 commit comments

Comments
 (0)