Skip to content

Commit b868949

Browse files
Merge pull request #66 from maticnetwork/single-delegation
add: single single delegation transaction
2 parents ddedb04 + 6bdf5d9 commit b868949

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

root/schema.graphql

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,3 +251,20 @@ type MaticTransfer @entity {
251251
timestamp: BigInt!
252252
transactionHash: Bytes!
253253
}
254+
255+
type GlobalDelegationCounter @entity {
256+
id: ID!
257+
current: BigInt!
258+
}
259+
260+
type Delegation @entity {
261+
id: ID!
262+
counter: BigInt! # this field can be used for traversing through large number of delegations list
263+
validatorId: BigInt!
264+
address: Bytes!
265+
timestamp: BigInt!
266+
transactionHash: Bytes!
267+
# delegated amount in that transaction
268+
amount: BigInt!
269+
block: BigInt!
270+
}

root/src/mappings/staking-info.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { Address, BigInt } from '@graphprotocol/graph-ts'
22
import {
33
Delegator,
4+
Delegation,
45
Validator,
56
Topup,
67
StakingParams,
78
GlobalDelegatorCounter,
9+
GlobalDelegationCounter,
810
DelegatorUnbond,
911
StakeUpdate as StakeUpdateEntity,
1012
} from '../../generated/schema'
@@ -277,6 +279,19 @@ function loadDelegatorUnbond(validatorId: BigInt, delegator: Address, nonce: Big
277279
return entity as DelegatorUnbond
278280
}
279281

282+
function getGlobalDelegationCounter(): GlobalDelegationCounter {
283+
// Only one entry will be kept in this entity
284+
let id = 'global-delegation-counter'
285+
let entity = GlobalDelegationCounter.load(id)
286+
if (entity == null) {
287+
288+
entity = new GlobalDelegationCounter(id)
289+
entity.current = BigInt.fromI32(0)
290+
291+
}
292+
return entity as GlobalDelegationCounter
293+
}
294+
280295
export function handleShareMinted(event: ShareMinted): void {
281296
let delegator = loadDelegator(event.params.validatorId, event.params.user)
282297

@@ -294,6 +309,24 @@ export function handleShareMinted(event: ShareMinted): void {
294309
validator.delegatedStake = validator.delegatedStake.plus(event.params.amount)
295310

296311
validator.save()
312+
313+
// entity for single delegation
314+
let globalDelegationCounter = getGlobalDelegationCounter()
315+
let updated = globalDelegationCounter.current.plus(BigInt.fromI32(1))
316+
globalDelegationCounter.current = updated
317+
globalDelegationCounter.save()
318+
319+
let id = event.transaction.hash.toHexString()
320+
let delegation = new Delegation(id)
321+
delegation.counter = updated
322+
delegation.validatorId = event.params.validatorId
323+
delegation.address = event.params.user
324+
delegation.block = event.block.number
325+
delegation.timestamp = event.block.timestamp
326+
delegation.transactionHash = event.transaction.hash
327+
delegation.amount = event.params.amount
328+
329+
delegation.save()
297330
// -- Saving updation
298331
}
299332

0 commit comments

Comments
 (0)