diff --git a/root/schema.graphql b/root/schema.graphql index 52cc9f4..e16061c 100644 --- a/root/schema.graphql +++ b/root/schema.graphql @@ -251,3 +251,20 @@ type MaticTransfer @entity { timestamp: BigInt! transactionHash: Bytes! } + +type GlobalDelegationCounter @entity { + id: ID! + current: BigInt! +} + +type Delegation @entity { + id: ID! + counter: BigInt! # this field can be used for traversing through large number of delegations list + validatorId: BigInt! + address: Bytes! + timestamp: BigInt! + transactionHash: Bytes! + # delegated amount in that transaction + amount: BigInt! + block: BigInt! +} diff --git a/root/src/mappings/staking-info.ts b/root/src/mappings/staking-info.ts index 39feb38..6c27921 100644 --- a/root/src/mappings/staking-info.ts +++ b/root/src/mappings/staking-info.ts @@ -1,10 +1,12 @@ import { Address, BigInt } from '@graphprotocol/graph-ts' import { Delegator, + Delegation, Validator, Topup, StakingParams, GlobalDelegatorCounter, + GlobalDelegationCounter, DelegatorUnbond, StakeUpdate as StakeUpdateEntity, } from '../../generated/schema' @@ -277,6 +279,19 @@ function loadDelegatorUnbond(validatorId: BigInt, delegator: Address, nonce: Big return entity as DelegatorUnbond } +function getGlobalDelegationCounter(): GlobalDelegationCounter { + // Only one entry will be kept in this entity + let id = 'global-delegation-counter' + let entity = GlobalDelegationCounter.load(id) + if (entity == null) { + + entity = new GlobalDelegationCounter(id) + entity.current = BigInt.fromI32(0) + + } + return entity as GlobalDelegationCounter +} + export function handleShareMinted(event: ShareMinted): void { let delegator = loadDelegator(event.params.validatorId, event.params.user) @@ -294,6 +309,24 @@ export function handleShareMinted(event: ShareMinted): void { validator.delegatedStake = validator.delegatedStake.plus(event.params.amount) validator.save() + + // entity for single delegation + let globalDelegationCounter = getGlobalDelegationCounter() + let updated = globalDelegationCounter.current.plus(BigInt.fromI32(1)) + globalDelegationCounter.current = updated + globalDelegationCounter.save() + + let id = event.transaction.hash.toHexString() + let delegation = new Delegation(id) + delegation.counter = updated + delegation.validatorId = event.params.validatorId + delegation.address = event.params.user + delegation.block = event.block.number + delegation.timestamp = event.block.timestamp + delegation.transactionHash = event.transaction.hash + delegation.amount = event.params.amount + + delegation.save() // -- Saving updation }