Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 64 additions & 1 deletion src/lib/contract/shared.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Address, BigDecimal, BigInt, Bytes } from "@graphprotocol/graph-ts";
import { REFERENCE_TOKEN } from "../../uniswap/common/chain";
import { Dlp, User, Token, Grantee } from "../../../generated/schema";
import { Dlp, User, Token, Grantee, Server, Epoch, Refiner } from "../../../generated/schema";
import { getOrCreateTotals, getTotalsDlpId } from "../entity/totals";

export function getOrCreateUser(userId: string): User {
Expand Down Expand Up @@ -47,6 +47,69 @@ export function getOrCreateGrantee(granteeId: string): Grantee {
return grantee;
}

export function getOrCreateServer(serverId: string): Server {
let server = Server.load(serverId);
if (server == null) {
// Create a placeholder server that will be populated by ServerRegistered event
server = new Server(serverId);

// Initialize with placeholder values
// These will be overwritten when the actual ServerRegistered event is processed
server.owner = "";
server.serverAddress = Bytes.empty();
server.publicKey = Bytes.empty();
server.url = "";
server.registeredAtBlock = BigInt.zero();
server.registeredAtTimestamp = BigInt.zero();
server.transactionHash = Bytes.empty();

server.save();
}
return server;
}

export function getOrCreateEpoch(epochId: string): Epoch {
let epoch = Epoch.load(epochId);
if (epoch == null) {
// Create a placeholder epoch that will be populated by EpochCreated event
epoch = new Epoch(epochId);

// Initialize with placeholder values
// These will be overwritten when the actual EpochCreated event is processed
epoch.startBlock = BigInt.zero();
epoch.endBlock = BigInt.zero();
epoch.reward = BigInt.zero();
epoch.createdAt = BigInt.zero();
epoch.createdTxHash = Bytes.empty();
epoch.createdAtBlock = BigInt.zero();
epoch.logIndex = BigInt.zero();
epoch.isFinalized = false;
epoch.dlpIds = [];

epoch.save();
}
return epoch;
}

export function getOrCreateRefiner(refinerId: string): Refiner {
let refiner = Refiner.load(refinerId);
if (refiner == null) {
// Create a placeholder refiner that will be populated by RefinerRegistered event
refiner = new Refiner(refinerId);

// Initialize with placeholder values
// These will be overwritten when the actual RefinerRegistered event is processed
refiner.dlp = "";
refiner.owner = Bytes.empty();
refiner.name = "";
refiner.schemaDefinitionUrl = "";
refiner.refinementInstructionUrl = "";

refiner.save();
}
return refiner;
}

export function getTokenAmountInVana(
tokenAddress: Bytes,
amount: BigInt,
Expand Down
3 changes: 3 additions & 0 deletions src/lib/contract/shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@ export {
getOrCreateUser,
getOrCreateDlp,
getOrCreateGrantee,
getOrCreateServer,
getOrCreateEpoch,
getOrCreateRefiner,
getTokenAmountInVana,
} from "../shared";
8 changes: 3 additions & 5 deletions src/lib/contract/v3/data-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { getDlpEpochUserId } from "../../entity/dlpEpochUser";
import {
getOrCreateDlp,
getOrCreateUser,
getOrCreateEpoch,
createFileFromEvent,
logDataRegistryEvent,
createDataRegistryProof,
Expand Down Expand Up @@ -96,12 +97,9 @@ function updateDlpEpochUser(
userId: string,
dlpId: string,
): void {
const epoch = Epoch.load(epochId);
// Get or create epoch and dlp (handles race condition when events are processed out of order)
const epoch = getOrCreateEpoch(epochId);
const dlp = getOrCreateDlp(dlpId);
if (!epoch) {
log.error("No epoch found for ID {}", [epochId]);
return;
}

if (!dlp.verificationBlockNumber) {
return;
Expand Down
6 changes: 3 additions & 3 deletions src/lib/contract/v4/data-refiner-registry.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { log } from "@graphprotocol/graph-ts";
import { Refiner } from "../../../../generated/schema";
import { RefinerAdded } from "../../../../generated/DataRefinerRegistryImplementation/DataRefinerRegistryImplementation";
import { getOrCreateUser } from "../shared";
import { getOrCreateUser, getOrCreateRefiner } from "../shared";

export function handleRefinerAddedV4(event: RefinerAdded): void {
log.info("Handling RefinerAdded with transaction hash: {}", [
Expand All @@ -11,8 +11,8 @@ export function handleRefinerAddedV4(event: RefinerAdded): void {
const ownerAddress = event.transaction.from;
getOrCreateUser(ownerAddress.toHex());

// Create new Refiner entity
const refiner = new Refiner(event.params.refinerId.toString());
// Get or create Refiner entity (may already exist if PaymentReceived was processed first)
const refiner = getOrCreateRefiner(event.params.refinerId.toString());
refiner.dlp = event.params.dlpId.toString();
refiner.owner = ownerAddress;
refiner.name = event.params.name;
Expand Down
13 changes: 3 additions & 10 deletions src/lib/contract/v4/query-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,15 @@ import {
getOrCreateTotalsGlobal,
getTotalsDlpId,
} from "../../entity/totals";
import { getTokenAmountInVana } from "../shared";
import { getTokenAmountInVana, getOrCreateRefiner } from "../shared";

export function handlePaymentReceived(event: PaymentReceivedEvent): void {
// Create unique ID from transaction hash and log index
const id = `${event.transaction.hash.toHexString()}-${event.logIndex.toString()}`;

// Check if refiner exists
// Get or create refiner (handles race condition when events are processed out of order)
const refinerId = event.params.refinerId.toString();
const refiner = Refiner.load(refinerId);
if (!refiner) {
log.error(
"Payment received for unknown refiner: {}. TX: {}. This payment will not be tracked!",
[refinerId, event.transaction.hash.toHexString()],
);
return;
}
const refiner = getOrCreateRefiner(refinerId);

// Create new PaymentReceived entity
const payment = new PaymentReceived(id);
Expand Down
17 changes: 4 additions & 13 deletions src/lib/contract/v5/dlp-performance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { BigInt as GraphBigInt, log } from "@graphprotocol/graph-ts";
import { EpochDlpPerformancesSaved as EpochDlpPerformancesSavedEvent } from "../../../../generated/DLPPerformanceImplementationV5/DLPPerformanceImplementationV5";

import { Dlp, Epoch, DlpPerformance } from "../../../../generated/schema";
import { getOrCreateDlp, getOrCreateEpoch } from "../shared";

export function handleEpochDlpPerformancesSavedV5(
event: EpochDlpPerformancesSavedEvent,
Expand All @@ -13,19 +14,9 @@ export function handleEpochDlpPerformancesSavedV5(
const epochId = event.params.epochId.toString();
const dlpId = event.params.dlpId.toString();

// Load the epoch and dlp
const epoch = Epoch.load(epochId);
const dlp = Dlp.load(dlpId);

if (epoch == null) {
log.error("Epoch not found for performance metrics: {}", [epochId]);
return;
}

if (dlp == null) {
log.error("DLP not found for performance metrics: {}", [dlpId]);
return;
}
// Get or create epoch and dlp (handles race condition when events are processed out of order)
const epoch = getOrCreateEpoch(epochId);
const dlp = getOrCreateDlp(dlpId);

// Create unique performance ID
const performanceId = `${epochId}-${dlpId}`;
Expand Down
31 changes: 10 additions & 21 deletions src/lib/contract/v6/data-portability-servers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
ServerUntrusted,
} from "../../../../generated/DataPortabilityServersImplementation/DataPortabilityServersImplementation";
import { Server, UserServer } from "../../../../generated/schema";
import { getOrCreateUser } from "../shared";
import { getOrCreateUser, getOrCreateServer } from "../shared";

export function handleServerRegistered(event: ServerRegistered): void {
log.info(
Expand All @@ -20,11 +20,9 @@ export function handleServerRegistered(event: ServerRegistered): void {
);

const serverId = event.params.serverId.toString();
let server = Server.load(serverId);

if (server == null) {
server = new Server(serverId);
}
// Get or create server (may already exist if ServerTrusted/ServerUpdated was processed first)
const server = getOrCreateServer(serverId);

// Get or create the owner user
const owner = getOrCreateUser(event.params.owner.toHex());
Expand All @@ -47,17 +45,12 @@ export function handleServerUpdated(event: ServerUpdated): void {
]);

const serverId = event.params.serverId.toString();
const server = Server.load(serverId);

if (server) {
server.url = event.params.url;
server.save();
} else {
log.warning(
"Received update event for a server not found in subgraph: {}",
[serverId],
);
}
// Get or create server (handles race condition when ServerUpdated is processed before ServerRegistered)
const server = getOrCreateServer(serverId);

server.url = event.params.url;
server.save();
}

export function handleServerTrusted(event: ServerTrusted): void {
Expand All @@ -70,12 +63,8 @@ export function handleServerTrusted(event: ServerTrusted): void {
const serverId = event.params.serverId.toString();
const compositeId = `${user.id}-${serverId}`;

// Ensure server exists
const server = Server.load(serverId);
if (server == null) {
log.error("Server with id {} not found for trust relationship", [serverId]);
return;
}
// Get or create server (handles race condition when ServerTrusted is processed before ServerRegistered)
const server = getOrCreateServer(serverId);

let userServer = UserServer.load(compositeId);
if (userServer == null) {
Expand Down
6 changes: 3 additions & 3 deletions src/lib/contract/v6/data-refiner-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
RefinerAdded,
SchemaAdded,
} from "../../../../generated/DataRefinerRegistryImplementationV6/DataRefinerRegistryImplementationV6";
import { getOrCreateUser } from "../shared";
import { getOrCreateUser, getOrCreateRefiner } from "../shared";

export function handleSchemaAdded(event: SchemaAdded): void {
log.info("Handling SchemaAdded with transaction hash: {} and schemaId: {}", [
Expand Down Expand Up @@ -36,8 +36,8 @@ export function handleRefinerAddedV6(event: RefinerAdded): void {
const ownerAddress = event.transaction.from;
getOrCreateUser(ownerAddress.toHex());

// Create new Refiner entity
const refiner = new Refiner(event.params.refinerId.toString());
// Get or create Refiner entity (may already exist if PaymentReceived was processed first)
const refiner = getOrCreateRefiner(event.params.refinerId.toString());
refiner.dlp = event.params.dlpId.toString();
refiner.owner = ownerAddress;
refiner.name = event.params.name;
Expand Down
Loading