Skip to content

Commit

Permalink
Merge pull request #66 from maticnetwork/single-delegation
Browse files Browse the repository at this point in the history
add: single single delegation transaction
  • Loading branch information
rahuldamodar94 authored Jun 30, 2022
2 parents ddedb04 + 6bdf5d9 commit b868949
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
17 changes: 17 additions & 0 deletions root/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -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!
}
33 changes: 33 additions & 0 deletions root/src/mappings/staking-info.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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)

Expand All @@ -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
}

Expand Down

0 comments on commit b868949

Please sign in to comment.