Skip to content

Commit

Permalink
feat: add createdAt timestamp to subgraphs and global statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
martines3000 committed Jun 17, 2024
1 parent 421a5ef commit de5fc84
Show file tree
Hide file tree
Showing 4 changed files with 394 additions and 17 deletions.
20 changes: 20 additions & 0 deletions packages/subgraphs/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ type Donation @entity(immutable: true) {
from: Account!
to: Account!
amount: BigInt!
createdAtTimestamp: BigInt!
}

type Endorsement @entity(immutable: true) {
Expand All @@ -13,12 +14,14 @@ type Endorsement @entity(immutable: true) {
endorsementType: String!
easUid: Bytes!
donationAmount: BigInt!
createdAtTimestamp: BigInt!
}

type Withdrawal @entity(immutable: true) {
id: Bytes!
address: Account!
amount: BigInt!
createdAtTimestamp: BigInt!
}

type FeeWithdrawal @entity(immutable: true) {
Expand All @@ -30,6 +33,7 @@ type FeeWithdrawal @entity(immutable: true) {
}

# Aggregated entities
# Single account information
type Account @entity(immutable: false) {
id: Bytes! # address

Expand All @@ -45,12 +49,28 @@ type Account @entity(immutable: false) {
totalEndorsementsReceived: BigInt!
totalDonationsSent: BigInt!
totalEndorsementsSent: BigInt!

# Other information
firstEndorsementReceivedTimestamp: BigInt
firstDonationReceivedTimestamp: BigInt
firstEndorsementSentTimestamp: BigInt
firstDonationSentTimestamp: BigInt
}

# User pair information (from - to)
type AggregatedInformation @entity(immutable: false) {
id: Bytes! # from - to
from: Account!
to: Account!
donationAmount: BigInt!
endorsementCount: BigInt!
}

# Global statistics
type GlobalStatistics @entity(immutable: false) {
id: Bytes!
totalEndorsements: BigInt!
totalDonations: BigInt!
totalDonationAmount: BigInt!
totalWithdrawnAmount: BigInt!
}
74 changes: 74 additions & 0 deletions packages/subgraphs/src/EESCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
Donation,
Endorsement,
FeeWithdrawal,
GlobalStatistics,
Withdrawal,
} from '../generated/schema';

Expand Down Expand Up @@ -50,6 +51,23 @@ const createOrLoadAggregatedInformation = (
return aggregatedInformation;
};

const createOrLoadGlobalStatistics = (): GlobalStatistics => {
const id = Bytes.fromUTF8('GlobalStatistics');

let globalStatistics = GlobalStatistics.load(id);

if (globalStatistics == null) {
globalStatistics = new GlobalStatistics(id);
globalStatistics.totalEndorsements = BigInt.fromI32(0);
globalStatistics.totalDonations = BigInt.fromI32(0);
globalStatistics.totalDonationAmount = BigInt.fromI32(0);
globalStatistics.totalWithdrawnAmount = BigInt.fromI32(0);
globalStatistics.save();
}

return globalStatistics;
};

export function handleDonate(event: DonateEvent): void {
// Create or load accounts
const fromAccount = createOrLoadAccount(event.params.from);
Expand All @@ -63,19 +81,32 @@ export function handleDonate(event: DonateEvent): void {
donation.from = fromAccount.id;
donation.to = toAccount.id;
donation.amount = event.params.amount;
donation.createdAtTimestamp = event.block.timestamp;

donation.save();

// Update account donations sent
fromAccount.totalDonationsSent = fromAccount.totalDonationsSent.plus(
donation.amount
);

// Check if this is the first donation sent by this account
if (fromAccount.firstDonationSentTimestamp === null) {
fromAccount.firstDonationSentTimestamp = event.block.timestamp;
}

fromAccount.save();

// Update account donations received
toAccount.totalDonationsReceived = toAccount.totalDonationsReceived.plus(
donation.amount
);

// Check if this is the first donation received by this account
if (toAccount.firstDonationReceivedTimestamp === null) {
toAccount.firstDonationReceivedTimestamp = event.block.timestamp;
}

toAccount.save();

// Update aggregated information
Expand All @@ -86,7 +117,19 @@ export function handleDonate(event: DonateEvent): void {

aggregatedInformation.donationAmount =
aggregatedInformation.donationAmount.plus(donation.amount);

aggregatedInformation.save();

// Update global statistics
const globalStatistics = createOrLoadGlobalStatistics();

globalStatistics.totalDonations = globalStatistics.totalDonations.plus(
BigInt.fromI32(1)
);
globalStatistics.totalDonationAmount =
globalStatistics.totalDonationAmount.plus(donation.amount);

globalStatistics.save();
}

export function handleEndorse(event: EndorseEvent): void {
Expand All @@ -104,18 +147,31 @@ export function handleEndorse(event: EndorseEvent): void {
endorsement.endorsementType = event.params.endorsementType;
endorsement.easUid = event.params.uid;
endorsement.donationAmount = event.params.donationAmount;
endorsement.createdAtTimestamp = event.block.timestamp;

endorsement.save();

// Update account endorsements sent
fromAccount.totalEndorsementsSent = fromAccount.totalEndorsementsSent.plus(
BigInt.fromI32(1)
);

// Check if this is the first endorsement sent by this account
if (fromAccount.firstEndorsementSentTimestamp === null) {
fromAccount.firstEndorsementSentTimestamp = event.block.timestamp;
}

fromAccount.save();

// Update account endorsements received
toAccount.totalEndorsementsReceived =
toAccount.totalEndorsementsReceived.plus(BigInt.fromI32(1));

// Check if this is the first endorsement received by this account
if (toAccount.firstEndorsementReceivedTimestamp === null) {
toAccount.firstEndorsementReceivedTimestamp = event.block.timestamp;
}

toAccount.save();

// Update aggregated information
Expand All @@ -127,6 +183,15 @@ export function handleEndorse(event: EndorseEvent): void {
aggregatedInformation.endorsementCount =
aggregatedInformation.endorsementCount.plus(BigInt.fromI32(1));
aggregatedInformation.save();

// Update global statistics
const globalStatistics = createOrLoadGlobalStatistics();

globalStatistics.totalEndorsements = globalStatistics.totalEndorsements.plus(
BigInt.fromI32(1)
);

globalStatistics.save();
}

export function handleWithdraw(event: WithdrawEvent): void {
Expand All @@ -140,8 +205,17 @@ export function handleWithdraw(event: WithdrawEvent): void {

withdrawal.address = account.id;
withdrawal.amount = event.params.amount;
withdrawal.createdAtTimestamp = event.block.timestamp;

withdrawal.save();

// Update global statistics
const globalStatistics = createOrLoadGlobalStatistics();

globalStatistics.totalWithdrawnAmount =
globalStatistics.totalWithdrawnAmount.plus(withdrawal.amount);

globalStatistics.save();
}

export function handleWithdrawFees(event: WithdrawFeesEvent): void {
Expand Down
16 changes: 14 additions & 2 deletions packages/subgraphs/tests/ees-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export function createEndorseEvent(
endorsementType: string,
easUid: Bytes,
logIndex: BigInt,
donationAmount: BigInt
donationAmount: BigInt,
timestamp: BigInt
): EndorseEvent {
const mockEvent = newMockEvent();
const event = new EndorseEvent(
Expand All @@ -27,6 +28,9 @@ export function createEndorseEvent(
mockEvent.receipt
);

// Set block timestamp
event.block.timestamp = timestamp;

event.parameters = new Array();

event.parameters.push(
Expand Down Expand Up @@ -62,6 +66,7 @@ export function createDonateEvent(
from: Address,
to: Address,
amount: BigInt,
timestamp: BigInt,
logIndex: BigInt = BigInt.fromI32(1)
): DonateEvent {
const mockEvent = newMockEvent();
Expand All @@ -76,6 +81,9 @@ export function createDonateEvent(
mockEvent.receipt
);

// Set block timestamp
event.block.timestamp = timestamp;

event.parameters = new Array();

event.parameters.push(
Expand All @@ -95,7 +103,8 @@ export function createDonateEvent(

export function createWithdrawEvent(
from: Address,
amount: BigInt
amount: BigInt,
timestamp: BigInt
): WithdrawEvent {
const mockEvent = newMockEvent();
const event = new WithdrawEvent(
Expand All @@ -109,6 +118,9 @@ export function createWithdrawEvent(
mockEvent.receipt
);

// Set block timestamp
event.block.timestamp = timestamp;

event.parameters = new Array();

event.parameters.push(
Expand Down
Loading

0 comments on commit de5fc84

Please sign in to comment.