Skip to content

Commit

Permalink
Modify PRE operator schema's entity
Browse files Browse the repository at this point in the history
- Entity name changed to PREOperator, which is more descriptive.
- Added OperatorBonded event handler, so PREOperator instance is
created when a new operator is bonded.
- Added confirmationTimestamp field, which will store the time in which
the operator is confirmed.
- Update getEpochStakeId function to accept epoch ID. If no argument
passed, the function takes the current epoch count.
  • Loading branch information
manumonti committed Jun 8, 2022
1 parent eeeac19 commit c2834fa
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 22 deletions.
9 changes: 5 additions & 4 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type StakeData @entity {
type EpochStake @entity {
# ID is the staking prov. add. + epoch counter
id: ID!
stakingProvider: Bytes! # address
stakingProvider: Bytes!
amount: BigInt!
}

Expand Down Expand Up @@ -77,10 +77,11 @@ type TokenholderDelegation implements Delegation @entity {
delegators: [Account!] @derivedFrom(field: "delegatee")
}

type ConfirmedOperator @entity {
type PREOperator @entity {
# ID is the staking provider address
id: ID!
stakingProvider: Bytes! # address
operator: Bytes! # address
operator: Bytes!
confirmationTimestamp: BigInt
}

type DAOMetric @entity {
Expand Down
32 changes: 20 additions & 12 deletions src/mapping.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { crypto, ByteArray, BigInt, log } from "@graphprotocol/graph-ts"
import { OperatorConfirmed } from "../generated/SimplePREApplication/SimplePREApplication"
import {
OperatorBonded,
OperatorConfirmed,
} from "../generated/SimplePREApplication/SimplePREApplication"
import {
Staked,
ToppedUp,
Expand All @@ -13,9 +16,9 @@ import {
StakeData,
Epoch,
EpochStake,
ConfirmedOperator,
Account,
MinStakeAmount,
PREOperator,
} from "../generated/schema"
import {
getDaoMetric,
Expand Down Expand Up @@ -68,7 +71,7 @@ export function handleStaked(event: Staked): void {
lastEpoch.save()

const epochStakes = populateNewEpochStakes(lastEpoch.stakes)
const epochStakeId = getEpochStakeId(stakingProvider.toHexString())
const epochStakeId = getEpochStakeId(stakingProvider)
const epochStake = new EpochStake(epochStakeId)
epochStake.stakingProvider = stakingProvider
epochStake.amount = stakeAmount
Expand Down Expand Up @@ -138,7 +141,7 @@ export function handleToppedUp(event: ToppedUp): void {
lastEpoch.save()

const epochStakes = populateNewEpochStakes(lastEpoch.stakes)
const epochStakeId = getEpochStakeId(stakingProvider.toHexString())
const epochStakeId = getEpochStakeId(stakingProvider)
let epochStake = EpochStake.load(epochStakeId)
if (!epochStake) {
epochStake = new EpochStake(epochStakeId)
Expand Down Expand Up @@ -222,7 +225,7 @@ export function handleUnstaked(event: Unstaked): void {
lastEpoch.save()

const epochStakes = populateNewEpochStakes(lastEpoch.stakes)
const epochStakeId = getEpochStakeId(stakingProvider.toHexString())
const epochStakeId = getEpochStakeId(stakingProvider)
const epochStake = EpochStake.load(epochStakeId)
epochStake!.amount = epochStake!.amount.minus(amount)
epochStake!.save()
Expand Down Expand Up @@ -272,16 +275,21 @@ export function handleDelegateVotesChanged(event: DelegateVotesChanged): void {
daoMetric.save()
}

export function handleOperatorBonded(event: OperatorBonded): void {
const preOperator = new PREOperator(
event.params.stakingProvider.toHexString()
)
preOperator.operator = event.params.operator
preOperator.save()
}

export function handleOperatorConfirmed(event: OperatorConfirmed): void {
let operator = ConfirmedOperator.load(
const preOperator = new PREOperator(
event.params.stakingProvider.toHexString()
)
if (!operator) {
operator = new ConfirmedOperator(event.params.stakingProvider.toHexString())
}
operator.stakingProvider = event.params.stakingProvider
operator.operator = event.params.operator
operator.save()
preOperator.operator = event.params.operator
preOperator.confirmationTimestamp = event.block.timestamp
preOperator.save()
}

export function handleMinStakeAmountChanged(
Expand Down
14 changes: 9 additions & 5 deletions src/utils/epochs.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { BigInt } from "@graphprotocol/graph-ts"
import { Address, BigInt } from "@graphprotocol/graph-ts"
import { Epoch, EpochStake, EpochCounter } from "../../generated/schema"

const STAKING_CONTRACT_DEPLOY_TIMESTAMP = 1643633638
const EPOCH_COUNTER_ID = "epoch-counter"

export function getEpochStakeId(stakingProvider: string): string {
const epochCount = getEpochCount()
return `${stakingProvider}-${epochCount}`
export function getEpochStakeId(
stakingProvider: Address,
epoch: i32 = -1
): string {
const epochCount = epoch == -1 ? getEpochCount() : epoch
return `${stakingProvider.toHexString()}-${epochCount}`
}

export function getOrCreateLastEpoch(): Epoch {
Expand All @@ -27,7 +30,8 @@ export function getOrCreateLastEpoch(): Epoch {
export function populateNewEpochStakes(stakes: string[]): string[] {
for (let i = 0; i < stakes.length; i++) {
const epochStake = EpochStake.load(stakes[i])
epochStake!.id = getEpochStakeId(epochStake!.stakingProvider.toHexString())
const stakingProvider = Address.fromBytes(epochStake!.stakingProvider)
epochStake!.id = getEpochStakeId(stakingProvider)
epochStake!.save()
stakes[i] = epochStake!.id
}
Expand Down
4 changes: 3 additions & 1 deletion subgraph.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,13 @@ dataSources:
apiVersion: 0.0.5
language: wasm/assemblyscript
entities:
- Operator
- PREOperator
abis:
- name: SimplePREApplication
file: ./abis/SimplePREApplication.json
eventHandlers:
- event: OperatorConfirmed(indexed address,indexed address)
handler: handleOperatorConfirmed
- event: OperatorBonded(indexed address,indexed address,uint256)
handler: handleOperatorBonded
file: ./src/mapping.ts

0 comments on commit c2834fa

Please sign in to comment.