Skip to content

Commit

Permalink
chore: added RewardsManager and track denylist
Browse files Browse the repository at this point in the history
  • Loading branch information
Maikol committed Jan 13, 2025
1 parent 2f363fb commit 68ccd2c
Show file tree
Hide file tree
Showing 10 changed files with 630 additions and 7 deletions.
413 changes: 413 additions & 0 deletions abis/RewardsManager.json

Large diffs are not rendered by default.

10 changes: 9 additions & 1 deletion networks.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
"SAODataEdge": {
"address": "0xeD16cEbd4fa74a0016E1149cc03563Db4B223aec",
"startBlock": 223927924
},
"RewardsManager": {
"address": "0x971B9d3d0Ae3ECa029CAB5eA1fB0F72c85e6a525",
"startBlock": 42449638
}
},
"arbitrum-sepolia": {
Expand All @@ -17,6 +21,10 @@
"SAODataEdge": {
"address": "0xB61AF143c79Cbdd68f179B657AaC86665CC2B469",
"startBlock": 49584061
},
"RewardsManager": {
"address": "0x1F49caE7669086c8ba53CC35d1E9f80176d67E79",
"startBlock": 570719
}
}
}
}
20 changes: 19 additions & 1 deletion schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ type GlobalState @entity {
id: ID!
oracles: [Oracle!]! @derivedFrom(field: "state")
activeOracles: [Oracle!]!
subgraphStates: [SubgraphState!]! @derivedFrom(field: "globalState")
}

type Oracle @entity {
Expand All @@ -19,6 +20,7 @@ type Oracle @entity {
type OracleVote @entity(immutable: true) {
id: ID!
oracle: Oracle!
subgraphState: SubgraphState!
subgraphDeploymentID: Bytes! # bytes32
deny: Boolean! # bool
timestamp: BigInt! # uint256
Expand All @@ -39,4 +41,20 @@ type OracleConfiguration @entity(immutable: true) {
subgraphAvailabilityManagerContract: String!
oracleIndex: String!
createdAt: BigInt!
}
}

type SubgraphState @entity {
id: Bytes! # subgraphDeploymentID
globalState: GlobalState!
currentVotes: [OracleVote!]! @derivedFrom(field: "subgraphState")
rewardsDenylist: [RewardsDenylist!]! @derivedFrom(field: "subgraphState") # If empty subgraph was never denylisted
lastUpdated: BigInt!
}

type RewardsDenylist @entity(immutable: true) {
id: Bytes!
subgraphDeploymentID: Bytes! # bytes32
subgraphState: SubgraphState!
sinceBlock: BigInt! # uint256
timestamp: BigInt!
}
26 changes: 26 additions & 0 deletions src/rewards-manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {
RewardsDenylistUpdated as RewardsDenylistUpdatedEvent,
} from "../generated/RewardsManager/RewardsManager"
import { StoreCache } from "./store-cache";

export function handleRewardsDenylistUpdated(
event: RewardsDenylistUpdatedEvent,
): void {
let subgraphDeploymentID = event.params.subgraphDeploymentID;
let id = event.transaction.hash.concatI32(event.logIndex.toI32());

let cache = new StoreCache();
let state = cache.getGlobalState();

let subgraphState = cache.getSubgraphState(subgraphDeploymentID);
subgraphState.globalState = state.id;
subgraphState.lastUpdated = event.block.timestamp;

let entity = cache.getRewardsDenylist(id);
entity.subgraphDeploymentID = subgraphDeploymentID;
entity.subgraphState = subgraphState.id;
entity.sinceBlock = event.params.sinceBlock;
entity.timestamp = event.block.timestamp;

cache.commitChanges();
}
38 changes: 37 additions & 1 deletion src/store-cache.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GlobalState, Oracle, OracleConfiguration, OracleVote } from "../generated/schema"
import { GlobalState, Oracle, OracleConfiguration, OracleVote, SubgraphState, RewardsDenylist } from "../generated/schema"
import { log, Bytes } from "@graphprotocol/graph-ts"

export class SafeMap<K, V> extends Map<K, V> {
Expand All @@ -12,6 +12,8 @@ export class StoreCache {
oracles: SafeMap<Bytes, Oracle>;
oraclesConfigs: SafeMap<String, OracleConfiguration>;
oracleVotes: SafeMap<String, OracleVote>;
subgraphStates: SafeMap<Bytes, SubgraphState>;
rewardsDenylists: SafeMap<Bytes, RewardsDenylist>;

constructor() {
let state = GlobalState.load("0");
Expand All @@ -25,6 +27,8 @@ export class StoreCache {
this.oracles = new SafeMap<Bytes, Oracle>();
this.oraclesConfigs = new SafeMap<String, OracleConfiguration>();
this.oracleVotes = new SafeMap<String, OracleVote>();
this.subgraphStates = new SafeMap<Bytes, SubgraphState>();
this.rewardsDenylists = new SafeMap<Bytes, RewardsDenylist>();
}

getGlobalState(): GlobalState {
Expand Down Expand Up @@ -66,6 +70,28 @@ export class StoreCache {
return this.oracleVotes.safeGet(id)!;
}

getSubgraphState(id: Bytes): SubgraphState {
if (this.subgraphStates.safeGet(id) == null) {
let state = SubgraphState.load(id);
if (state == null) {
state = new SubgraphState(id);
}
this.subgraphStates.set(id, state);
}
return this.subgraphStates.safeGet(id)!;
}

getRewardsDenylist(id: Bytes): RewardsDenylist {
if (this.rewardsDenylists.safeGet(id) == null) {
let denylist = RewardsDenylist.load(id);
if (denylist == null) {
denylist = new RewardsDenylist(id);
}
this.rewardsDenylists.set(id, denylist);
}
return this.rewardsDenylists.safeGet(id)!;
}

commitChanges(): void {
this.state.save();

Expand All @@ -83,5 +109,15 @@ export class StoreCache {
for (let i = 0; i < votes.length; i++) {
votes[i].save();
}

let states = this.subgraphStates.values();
for (let i = 0; i < states.length; i++) {
states[i].save();
}

let denylists = this.rewardsDenylists.values();
for (let i = 0; i < denylists.length; i++) {
denylists[i].save();
}
}
}
11 changes: 9 additions & 2 deletions src/subgraph-availability-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,25 @@ export function handleOracleSet(event: OracleSetEvent): void {
}

export function handleOracleVote(event: OracleVoteEvent): void {
let subgraphDeploymentID = event.params.subgraphDeploymentID.toHexString();
let subgraphDeploymentID = event.params.subgraphDeploymentID;
let oracleAddress = event.transaction.from.toHexString();
let timestamp = event.params.timestamp.toString();
let voteId = getOracleVoteId(subgraphDeploymentID, oracleAddress, timestamp);
let voteId = getOracleVoteId(subgraphDeploymentID.toHexString(), oracleAddress, timestamp);

let cache = new StoreCache();
let state = cache.getGlobalState();

let subgraphState = cache.getSubgraphState(subgraphDeploymentID);
subgraphState.globalState = state.id;
subgraphState.lastUpdated = event.block.timestamp;

let oracle = cache.getOracle(event.transaction.from);
let oracleVote = cache.getOracleVote(voteId);
oracleVote.subgraphDeploymentID = event.params.subgraphDeploymentID
oracleVote.deny = event.params.deny
oracleVote.oracle = oracle.id
oracleVote.timestamp = event.params.timestamp
oracleVote.subgraphState = subgraphState.id;

cache.commitChanges();
}
Expand Down
25 changes: 25 additions & 0 deletions subgraph.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,28 @@ dataSources:
- event: Log(bytes)
handler: handleLog
file: ./src/sao-data-edge.ts
- kind: ethereum
name: RewardsManager
network: arbitrum-one
source:
address: "0x971B9d3d0Ae3ECa029CAB5eA1fB0F72c85e6a525"
abi: RewardsManager
startBlock: 42449638
mapping:
kind: ethereum/events
apiVersion: 0.0.7
language: wasm/assemblyscript
entities:
- ContractSynced
- ParameterUpdated
- RewardsAssigned
- RewardsDenied
- RewardsDenylistUpdated
- SetController
abis:
- name: RewardsManager
file: ./abis/RewardsManager.json
eventHandlers:
- event: RewardsDenylistUpdated(indexed bytes32,uint256)
handler: handleRewardsDenylistUpdated
file: ./src/rewards-manager.ts
30 changes: 30 additions & 0 deletions tests/rewards-manager-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { newMockEvent } from "matchstick-as"
import { ethereum, Bytes, BigInt } from "@graphprotocol/graph-ts"
import {
RewardsDenylistUpdated
} from "../generated/RewardsManager/RewardsManager"

export function createRewardsDenylistUpdatedEvent(
subgraphDeploymentID: Bytes,
sinceBlock: BigInt
): RewardsDenylistUpdated {
let rewardsDenylistUpdatedEvent =
changetype<RewardsDenylistUpdated>(newMockEvent())

rewardsDenylistUpdatedEvent.parameters = new Array()

rewardsDenylistUpdatedEvent.parameters.push(
new ethereum.EventParam(
"subgraphDeploymentID",
ethereum.Value.fromFixedBytes(subgraphDeploymentID)
)
)
rewardsDenylistUpdatedEvent.parameters.push(
new ethereum.EventParam(
"sinceBlock",
ethereum.Value.fromUnsignedBigInt(sinceBlock)
)
)

return rewardsDenylistUpdatedEvent
}
50 changes: 50 additions & 0 deletions tests/rewards-manager.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import {
assert,
describe,
test,
clearStore,
afterEach
} from "matchstick-as/assembly/index"
import { Bytes, BigInt } from "@graphprotocol/graph-ts"
import { handleRewardsDenylistUpdated } from "../src/rewards-manager"
import { createRewardsDenylistUpdatedEvent } from "./rewards-manager-utils"

const subgraphDeploymentID = "0x0000000000000000000000000000000000000005"
const subgraphDeploymentBytes = Bytes.fromHexString(subgraphDeploymentID)

describe("RewardsManager", () => {
afterEach(() => {
clearStore()
})

test("RewardsDenylistUpdated created and stored", () => {
let newRewardsDenylistUpdatedEvent = createRewardsDenylistUpdatedEvent(
subgraphDeploymentBytes,
BigInt.fromI32(100)
)
handleRewardsDenylistUpdated(newRewardsDenylistUpdatedEvent)

assert.entityCount("RewardsDenylist", 1)
assert.fieldEquals(
"RewardsDenylist",
newRewardsDenylistUpdatedEvent.transaction.hash.concatI32(newRewardsDenylistUpdatedEvent.logIndex.toI32()).toHexString(),
"subgraphDeploymentID",
subgraphDeploymentID
)
assert.fieldEquals(
"RewardsDenylist",
newRewardsDenylistUpdatedEvent.transaction.hash.concatI32(newRewardsDenylistUpdatedEvent.logIndex.toI32()).toHexString(),
"sinceBlock",
"100"
)
// Verify SubgraphState relationship
assert.entityCount("SubgraphState", 1)
assert.fieldEquals("SubgraphState", subgraphDeploymentID, "id", subgraphDeploymentID)
assert.fieldEquals(
"SubgraphState",
subgraphDeploymentID,
"lastUpdated",
newRewardsDenylistUpdatedEvent.block.timestamp.toString()
)
})
})
14 changes: 12 additions & 2 deletions tests/subgraph-availability-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import {
clearStore,
afterEach
} from "matchstick-as/assembly/index"
import { Address, BigInt, log } from "@graphprotocol/graph-ts"
import { Address, BigInt, Bytes, log } from "@graphprotocol/graph-ts"
import { handleOracleSet, handleOracleVote } from "../src/subgraph-availability-manager"
import { createOracleSetEvent, createOracleVoteEvent } from "./subgraph-availability-manager-utils"
import { getOracleVoteId } from "../src/helpers"
import { Oracle, GlobalState } from "../generated/schema"
import { Oracle, GlobalState, SubgraphState } from "../generated/schema"

const oracleID = "0x0000000000000000000000000000000000000004"
const oracleAddress = Address.fromString(oracleID)
Expand Down Expand Up @@ -71,5 +71,15 @@ describe("ORACLE", () => {
assert.fieldEquals("OracleVote", oracleVoteID, "deny", "true")
assert.fieldEquals("OracleVote", oracleVoteID, "oracle", oracleID)
assert.fieldEquals("OracleVote", oracleVoteID, "timestamp", "300")

// Verify SubgraphState relationship
assert.entityCount("SubgraphState", 1)
assert.fieldEquals("SubgraphState", subgraphDeploymentID, "id", subgraphDeploymentID)
assert.fieldEquals(
"SubgraphState",
subgraphDeploymentID,
"lastUpdated",
newOracleVoteEvent.block.timestamp.toString()
)
})
})

0 comments on commit 68ccd2c

Please sign in to comment.