diff --git a/schema.graphql b/schema.graphql index f80774e..6fca687 100644 --- a/schema.graphql +++ b/schema.graphql @@ -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! } @@ -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 { diff --git a/src/mapping.ts b/src/mapping.ts index b6d2bd6..e13c67a 100644 --- a/src/mapping.ts +++ b/src/mapping.ts @@ -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, @@ -13,9 +16,9 @@ import { StakeData, Epoch, EpochStake, - ConfirmedOperator, Account, MinStakeAmount, + PREOperator, } from "../generated/schema" import { getDaoMetric, @@ -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 @@ -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) @@ -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() @@ -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( diff --git a/src/utils/epochs.ts b/src/utils/epochs.ts index d7202fa..82ea027 100644 --- a/src/utils/epochs.ts +++ b/src/utils/epochs.ts @@ -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 { @@ -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 } diff --git a/subgraph.yaml b/subgraph.yaml index 8b66d5c..90ec42d 100644 --- a/subgraph.yaml +++ b/subgraph.yaml @@ -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