diff --git a/root/schema.graphql b/root/schema.graphql index e856bdc..73a6911 100644 --- a/root/schema.graphql +++ b/root/schema.graphql @@ -38,6 +38,7 @@ type StateRegistration @entity { type PlasmaExit @entity { # id always created using `plasma-exit-${exitId}` id: ID! + counter: BigInt! # this field can be used to know where the exitStarted transaction happened. exitId: BigInt! exitInitiator: Bytes exitCompleter: Bytes @@ -108,6 +109,12 @@ type GlobalDelegatorCounter @entity { current: BigInt! } +# This can be used to get the count of the plasma exits and till where have the process exits been processed. +type GlobalPlasmaExitCounter @entity { + id: ID! + current: BigInt! +} + type Delegator @entity { id: ID! counter: BigInt! # this field can be used for traversing through large number of delegator list diff --git a/root/src/mappings/staking-info.ts b/root/src/mappings/staking-info.ts index 1e952a6..5a29506 100644 --- a/root/src/mappings/staking-info.ts +++ b/root/src/mappings/staking-info.ts @@ -198,7 +198,6 @@ export function handleUpdateCommissionRate(event: UpdateCommissionRate): void { // Sole purpose of this entity is to keep a global state variable // which can be used when new delegator comes into picture function getGlobalDelegatorCounter(): GlobalDelegatorCounter { - // Only one entry will be kept in this entity let id = 'global-delegator-counter' let entity = GlobalDelegatorCounter.load(id) @@ -208,9 +207,7 @@ function getGlobalDelegatorCounter(): GlobalDelegatorCounter { entity.current = BigInt.fromI32(0) } - return entity as GlobalDelegatorCounter - } function loadDelegator(validatorId: BigInt, delegator: Address): Delegator { diff --git a/root/src/mappings/withdraw-manager.ts b/root/src/mappings/withdraw-manager.ts index 61b3c2a..f40e580 100644 --- a/root/src/mappings/withdraw-manager.ts +++ b/root/src/mappings/withdraw-manager.ts @@ -1,14 +1,36 @@ +import { BigInt } from '@graphprotocol/graph-ts' import { ExitStarted, ExitCancelled, Withdraw } from '../../generated/WithdrawManager/WithdrawManager' -import { PlasmaExit } from '../../generated/schema' +import { PlasmaExit, GlobalPlasmaExitCounter } from '../../generated/schema' +function getGlobalPlasmaExitCounter(): GlobalPlasmaExitCounter { + // Only one entry will be kept in this entity + let id = 'global-plasma-exit-counter' + let entity = GlobalPlasmaExitCounter.load(id) + if (entity == null) { + + entity = new GlobalPlasmaExitCounter(id) + entity.current = BigInt.fromI32(0) + + } + return entity as GlobalPlasmaExitCounter +} export function handleExitStarted(event: ExitStarted): void { let id = 'plasma-exit-' + event.params.exitId.toHexString() + + // Try to get what's current global plasma counter's state + // when called for very first time, it'll be `0` + let counter = getGlobalPlasmaExitCounter() + let updated = counter.current.plus(BigInt.fromI32(1)) + + // Updating global counter's state + counter.current = updated + counter.save() // this is first time this entity being created i.e. during plasma // exit start step let entity = new PlasmaExit(id) - + entity.counter = updated entity.exitId = event.params.exitId entity.exitInitiator = event.params.exitor entity.token = event.params.token