Skip to content
This repository has been archived by the owner on Feb 16, 2023. It is now read-only.

Commit

Permalink
Merge pull request #370 from humanprotocol/feat/subgraph-daily-updates
Browse files Browse the repository at this point in the history
feat: add eventDayDatas
  • Loading branch information
m00n620 authored Sep 29, 2022
2 parents 9327282 + a06cc23 commit 80d37ce
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
9 changes: 9 additions & 0 deletions hmt_subgraph/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,12 @@ type HMTokenStatistics @entity {
totalValueTransfered: BigInt!
token: Bytes! # token address
}

type EventDayData @entity {
id: ID!
timestamp: Int!
dailyBulkTransferEvents: BigInt!
dailyIntermediateStorageEvents: BigInt!
dailyPendingEvents: BigInt!
dailyEscrowAmounts: BigInt!
}
12 changes: 12 additions & 0 deletions hmt_subgraph/src/mapping/Escrow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import {
PEvent,
} from "../../generated/schema";
import { BigInt } from "@graphprotocol/graph-ts";
import {
updateIntermediateStorageEventDayData,
updatePendingEventDayData,
updateBulkTransferEventDayData,
} from "./utils/dayUpdates";
export const STATISTICS_ENTITY_ID = "escrow-statistics-id";

export function constructStatsEntity(): EscrowStatistics {
Expand All @@ -23,6 +28,7 @@ export function constructStatsEntity(): EscrowStatistics {

return entity;
}

export function handleIntermediateStorage(event: IntermediateStorage): void {
// Entities can be loaded from the store using a string ID; this ID
// needs to be unique across all entities of the same type
Expand Down Expand Up @@ -51,6 +57,8 @@ export function handleIntermediateStorage(event: IntermediateStorage): void {

statsEntity.save();
entity.save();

updateIntermediateStorageEventDayData(event);
}

export function handlePending(event: Pending): void {
Expand Down Expand Up @@ -82,6 +90,8 @@ export function handlePending(event: Pending): void {

entity.save();
statsEntity.save();

updatePendingEventDayData(event);
}

export function handleBulkTransfer(event: BulkTransfer): void {
Expand Down Expand Up @@ -112,4 +122,6 @@ export function handleBulkTransfer(event: BulkTransfer): void {

statsEntity.save();
entity.save();

updateBulkTransferEventDayData(event);
}
3 changes: 3 additions & 0 deletions hmt_subgraph/src/mapping/EscrowFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Launched } from "../../generated/EscrowFactory/EscrowFactory";
import { EscrowStatistics, LaunchedEscrow } from "../../generated/schema";
import { Escrow } from "../../generated/templates";
import { constructStatsEntity, STATISTICS_ENTITY_ID } from "./Escrow";
import { updateEscrowAmountDayData } from "./utils/dayUpdates";

export function handleLaunched(event: Launched): void {
// Entities only exist after they have been saved to the store;
Expand All @@ -27,4 +28,6 @@ export function handleLaunched(event: Launched): void {
// Entities can be written to the store with `.save()`
entity.save();
Escrow.create(event.params.escrow);

updateEscrowAmountDayData(event);
}
68 changes: 68 additions & 0 deletions hmt_subgraph/src/mapping/utils/dayUpdates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/* eslint-disable prefer-const */
import { BigInt, ethereum } from "@graphprotocol/graph-ts";
import { Launched } from "../../../generated/EscrowFactory/EscrowFactory";
import { EventDayData } from "../../../generated/schema";
import {
IntermediateStorage,
Pending,
BulkTransfer,
} from "../../../generated/templates/Escrow/Escrow";

const ZERO_BI = BigInt.fromI32(0);

function getEventDayData(event: ethereum.Event): EventDayData {
let timestamp = event.block.timestamp.toI32();
let dayID = timestamp / 86400;
let dayStartTimestamp = dayID * 86400;

let eventDayData = EventDayData.load(dayID.toString());
if (eventDayData === null) {
eventDayData = new EventDayData(dayID.toString());
eventDayData.timestamp = dayStartTimestamp;
eventDayData.dailyBulkTransferEvents = ZERO_BI;
eventDayData.dailyIntermediateStorageEvents = ZERO_BI;
eventDayData.dailyPendingEvents = ZERO_BI;
eventDayData.dailyEscrowAmounts = ZERO_BI;
}
return eventDayData;
}

export function updateIntermediateStorageEventDayData(
event: IntermediateStorage
): EventDayData {
const eventDayData = getEventDayData(event);
//@ts-ignore
eventDayData.dailyIntermediateStorageEvents += BigInt.fromI32(1);
eventDayData.save();

return eventDayData;
}

export function updatePendingEventDayData(event: Pending): EventDayData {
const eventDayData = getEventDayData(event);
//@ts-ignore
eventDayData.dailyPendingEvents += BigInt.fromI32(1);
eventDayData.save();

return eventDayData;
}

export function updateBulkTransferEventDayData(
event: BulkTransfer
): EventDayData {
const eventDayData = getEventDayData(event);
//@ts-ignore
eventDayData.dailyBulkTransferEvents += BigInt.fromI32(1);
eventDayData.save();

return eventDayData;
}

export function updateEscrowAmountDayData(event: Launched): EventDayData {
const eventDayData = getEventDayData(event);
//@ts-ignore
eventDayData.dailyEscrowAmounts += BigInt.fromI32(1);
eventDayData.save();

return eventDayData;
}

0 comments on commit 80d37ce

Please sign in to comment.