Skip to content

Commit

Permalink
mappings: fix movements ids
Browse files Browse the repository at this point in the history
  • Loading branch information
facuspagnuolo committed Dec 13, 2019
1 parent 94c4ac0 commit 9d5d6ab
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 30 deletions.
5 changes: 5 additions & 0 deletions helpers/id.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { EthereumEvent } from '@graphprotocol/graph-ts'

export function buildId(event: EthereumEvent): string {
return event.transaction.hash.toHex() + event.logIndex.toString()
}
38 changes: 17 additions & 21 deletions src/JurorsRegistry.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { buildId } from '../helpers/id'
import { Juror, ANJMovement } from '../types/schema'
import { EthereumEvent, Address, BigInt } from '@graphprotocol/graph-ts'
import {
Expand Down Expand Up @@ -26,27 +27,27 @@ let SLASH = 'Slash'

export function handleStaked(event: Staked): void {
updateJuror(event.params.user, event)
createANJMovementForEvent(event.params.user, STAKE, event.params.amount, event.block.timestamp, event)
createANJMovementForEvent(event.params.user, STAKE, event.params.amount, event)
}

export function handleUnstaked(event: Unstaked): void {
updateJuror(event.params.user, event)
createANJMovementForEvent(event.params.user, UNSTAKE, event.params.amount, event.block.timestamp, event)
createANJMovementForEvent(event.params.user, UNSTAKE, event.params.amount, event)
}

export function handleJurorActivated(event: JurorActivated): void {
updateJuror(event.params.juror, event)
createANJMovementForTerm(event.params.juror, ACTIVATION, event.params.amount, event.block.timestamp, event.params.fromTermId)
createANJMovementForTerm(event.params.juror, ACTIVATION, event.params.amount, event.params.fromTermId, event)
}

export function handleJurorDeactivationRequested(event: JurorDeactivationRequested): void {
updateJuror(event.params.juror, event)
createANJMovementForTerm(event.params.juror, DEACTIVATION, event.params.amount, event.block.timestamp, event.params.availableTermId)
createANJMovementForTerm(event.params.juror, DEACTIVATION, event.params.amount, event.params.availableTermId, event)
}

export function handleJurorDeactivationUpdated(event: JurorDeactivationUpdated): void {
updateJuror(event.params.juror, event)
createANJMovementForTerm(event.params.juror, DEACTIVATION, event.params.amount, event.block.timestamp, event.params.availableTermId)
createANJMovementForTerm(event.params.juror, DEACTIVATION, event.params.amount, event.params.availableTermId, event)
}

export function handleJurorDeactivationProcessed(event: JurorDeactivationProcessed): void {
Expand All @@ -55,27 +56,27 @@ export function handleJurorDeactivationProcessed(event: JurorDeactivationProcess

export function handleJurorBalanceLocked(event: JurorBalanceLocked): void {
updateJuror(event.params.juror, event)
createANJMovementForEvent(event.params.juror, LOCK, event.params.amount, event.block.timestamp, event)
createANJMovementForEvent(event.params.juror, LOCK, event.params.amount, event)
}

export function handleJurorBalanceUnlocked(event: JurorBalanceUnlocked): void {
updateJuror(event.params.juror, event)
createANJMovementForEvent(event.params.juror, UNLOCK, event.params.amount, event.block.timestamp, event)
createANJMovementForEvent(event.params.juror, UNLOCK, event.params.amount, event)
}

export function handleJurorTokensAssigned(event: JurorTokensAssigned): void {
updateJuror(event.params.juror, event)
createANJMovementForEvent(event.params.juror, REWARD, event.params.amount, event.block.timestamp, event)
createANJMovementForEvent(event.params.juror, REWARD, event.params.amount, event)
}

export function handleJurorTokensCollected(event: JurorTokensCollected): void {
updateJuror(event.params.juror, event)
createANJMovementForTerm(event.params.juror, SLASH, event.params.amount, event.block.timestamp, event.params.effectiveTermId)
createANJMovementForTerm(event.params.juror, SLASH, event.params.amount, event.params.effectiveTermId, event)
}

export function handleJurorSlashed(event: JurorSlashed): void {
updateJuror(event.params.juror, event)
createANJMovementForTerm(event.params.juror, SLASH, event.params.amount, event.block.timestamp, event.params.effectiveTermId)
createANJMovementForTerm(event.params.juror, SLASH, event.params.amount, event.params.effectiveTermId, event)
}

function loadOrCreateJuror(jurorAddress: Address, event: EthereumEvent): Juror | null {
Expand All @@ -102,15 +103,14 @@ function updateJuror(jurorAddress: Address, event: EthereumEvent): void {
juror.save()
}

function createANJMovementForEvent(juror: Address, type: string, amount: BigInt, createdAt: BigInt, event: EthereumEvent): void {
let eventId = event.transaction.hash.toHex() + event.logIndex.toString()
let id = buildANJMovementId(juror, type, eventId)
createANJMovement(id, juror, type, amount, createdAt)
function createANJMovementForEvent(juror: Address, type: string, amount: BigInt, event: EthereumEvent): void {
let id = buildId(event)
createANJMovement(id, juror, type, amount, event.block.timestamp)
}

function createANJMovementForTerm(juror: Address, type: string, amount: BigInt, createdAt: BigInt, termId: BigInt): void {
let id = buildANJMovementId(juror, type, termId.toString())
createANJMovement(id, juror, type, amount, createdAt, termId)
function createANJMovementForTerm(juror: Address, type: string, amount: BigInt, termId: BigInt, event: EthereumEvent): void {
let id = buildId(event)
createANJMovement(id, juror, type, amount, event.block.timestamp, termId)
}

function createANJMovement(id: string, juror: Address, type: string, amount: BigInt, createdAt: BigInt, termId: BigInt | null = null): void {
Expand All @@ -122,7 +122,3 @@ function createANJMovement(id: string, juror: Address, type: string, amount: Big
movement.createdAt = createdAt
movement.save()
}

function buildANJMovementId(juror: Address, type: string, id: string): string {
return juror.toHex() + '-' + type + '-' + id
}
13 changes: 4 additions & 9 deletions src/Treasury.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { buildId } from '../helpers/id'
import { FeeMovement } from '../types/schema'
import { FeesClaimed } from '../types/templates/Subscriptions/Subscriptions'
import { Assign, Withdraw } from '../types/templates/Treasury/Treasury'
import { Address, EthereumEvent } from '@graphprotocol/graph-ts'

let ASSIGN = 'Assign'
let WITHDRAW = 'Withdraw'
let SUBSCRIPTIONS = 'Subscriptions'

export function handleAssign(event: Assign): void {
let id = buildFeeMovementId(event.params.to, ASSIGN, event)
let id = buildId(event)
let movement = new FeeMovement(id)
movement.type = ASSIGN
movement.juror = event.params.to.toHex()
Expand All @@ -18,7 +18,7 @@ export function handleAssign(event: Assign): void {
}

export function handleWithdraw(event: Withdraw): void {
let id = buildFeeMovementId(event.params.to, WITHDRAW, event)
let id = buildId(event)
let movement = new FeeMovement(id)
movement.type = WITHDRAW
movement.juror = event.params.to.toHex()
Expand All @@ -28,16 +28,11 @@ export function handleWithdraw(event: Withdraw): void {
}

export function handleSubscriptionPaid(event: FeesClaimed): void {
let id = buildFeeMovementId(event.params.juror, SUBSCRIPTIONS, event)
let id = buildId(event)
let movement = new FeeMovement(id)
movement.type = SUBSCRIPTIONS
movement.juror = event.params.juror.toHex()
movement.amount = event.params.jurorShare
movement.createdAt = event.block.timestamp
movement.save()
}

function buildFeeMovementId(juror: Address, type: string, event: EthereumEvent): string {
let eventId = event.transaction.hash.toHex() + event.logIndex.toString()
return juror.toHex() + '-' + type + '-' + eventId
}

0 comments on commit 9d5d6ab

Please sign in to comment.