1
1
import { Address , BigInt } from '@graphprotocol/graph-ts'
2
2
import {
3
3
Delegator ,
4
+ Delegation ,
4
5
Validator ,
5
6
Topup ,
6
7
StakingParams ,
7
8
GlobalDelegatorCounter ,
9
+ GlobalDelegationCounter ,
8
10
DelegatorUnbond ,
9
11
StakeUpdate as StakeUpdateEntity ,
10
12
} from '../../generated/schema'
@@ -277,6 +279,19 @@ function loadDelegatorUnbond(validatorId: BigInt, delegator: Address, nonce: Big
277
279
return entity as DelegatorUnbond
278
280
}
279
281
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
+
280
295
export function handleShareMinted ( event : ShareMinted ) : void {
281
296
let delegator = loadDelegator ( event . params . validatorId , event . params . user )
282
297
@@ -294,6 +309,24 @@ export function handleShareMinted(event: ShareMinted): void {
294
309
validator . delegatedStake = validator . delegatedStake . plus ( event . params . amount )
295
310
296
311
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 ( )
297
330
// -- Saving updation
298
331
}
299
332
0 commit comments