Skip to content

Commit

Permalink
fix: handle multiple user praises
Browse files Browse the repository at this point in the history
  • Loading branch information
bashybaranaba committed Jan 12, 2025
1 parent 2288237 commit 55a7997
Showing 1 changed file with 84 additions and 82 deletions.
166 changes: 84 additions & 82 deletions graph/abraham/src/abraham.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BigInt, Bytes } from "@graphprotocol/graph-ts";
import { BigInt, Bytes, log } from "@graphprotocol/graph-ts";
import {
Approval as ApprovalEvent,
BoughtManna as BoughtMannaEvent,
Expand All @@ -18,11 +18,15 @@ import {
CreationAdded,
OwnershipTransferred,
Praised,
PraiseCount,
SoldManna,
Transfer,
Unpraised,
} from "../generated/schema";

// -------------------------------------
// handleApproval
// -------------------------------------
export function handleApproval(event: ApprovalEvent): void {
let entity = new Approval(
event.transaction.hash.concatI32(event.logIndex.toI32())
Expand All @@ -38,6 +42,9 @@ export function handleApproval(event: ApprovalEvent): void {
entity.save();
}

// -------------------------------------
// handleBoughtManna
// -------------------------------------
export function handleBoughtManna(event: BoughtMannaEvent): void {
let entity = new BoughtManna(
event.transaction.hash.concatI32(event.logIndex.toI32())
Expand All @@ -52,63 +59,41 @@ export function handleBoughtManna(event: BoughtMannaEvent): void {
entity.save();
}

// export function handleConvictionUpdated(event: ConvictionUpdatedEvent): void {
// let entity = new ConvictionUpdated(
// event.transaction.hash.concatI32(event.logIndex.toI32())
// );
// entity.creationId = event.params.creationId;
// entity.newConviction = event.params.newConviction;

// entity.blockNumber = event.block.number;
// entity.blockTimestamp = event.block.timestamp;
// entity.transactionHash = event.transaction.hash;

// entity.save();
// }
// -------------------------------------
// handleConvictionUpdated
// -------------------------------------
export function handleConvictionUpdated(event: ConvictionUpdatedEvent): void {
// 1. Save the ConvictionUpdated event entity (optional)
// 1. Save the ConvictionUpdated event (immutable log)
let convictionEntity = new ConvictionUpdated(
event.transaction.hash.concatI32(event.logIndex.toI32())
);
convictionEntity.creationId = event.params.creationId;
convictionEntity.newConviction = event.params.newConviction;

convictionEntity.blockNumber = event.block.number;
convictionEntity.blockTimestamp = event.block.timestamp;
convictionEntity.transactionHash = event.transaction.hash;
convictionEntity.save();

// 2. Update the Creation entity
let creation = Creation.load(event.params.creationId.toString());
let creationIdString = event.params.creationId.toString();
let creation = Creation.load(creationIdString);
if (creation !== null) {
creation.conviction = event.params.newConviction;
creation.updatedAt = event.block.timestamp;

creation.save();
}
}

// export function handleCreationAdded(event: CreationAddedEvent): void {
// let entity = new CreationAdded(
// event.transaction.hash.concatI32(event.logIndex.toI32())
// )
// entity.creationId = event.params.creationId
// entity.metadataUri = event.params.metadataUri

// entity.blockNumber = event.block.number
// entity.blockTimestamp = event.block.timestamp
// entity.transactionHash = event.transaction.hash

// entity.save()
// }

// -------------------------------------
// handleCreationAdded
// -------------------------------------
export function handleCreationAdded(event: CreationAddedEvent): void {
let creationId = event.params.creationId.toString();
let creationIdString = event.params.creationId.toString();

// Load existing or create new Creation entity
let creation = Creation.load(creationId);
// 1. Create or load Creation entity
let creation = Creation.load(creationIdString);
if (creation == null) {
creation = new Creation(creationId);
creation = new Creation(creationIdString);
creation.creationId = event.params.creationId;
creation.metadataUri = event.params.metadataUri;
creation.totalStaked = BigInt.zero();
Expand All @@ -118,8 +103,22 @@ export function handleCreationAdded(event: CreationAddedEvent): void {
creation.updatedAt = event.block.timestamp;
creation.save();
}

// 2. Save the immutable event
let eventEntity = new CreationAdded(
event.transaction.hash.concatI32(event.logIndex.toI32())
);
eventEntity.creationId = event.params.creationId;
eventEntity.metadataUri = event.params.metadataUri;
eventEntity.blockNumber = event.block.number;
eventEntity.blockTimestamp = event.block.timestamp;
eventEntity.transactionHash = event.transaction.hash;
eventEntity.save();
}

// -------------------------------------
// handleOwnershipTransferred
// -------------------------------------
export function handleOwnershipTransferred(
event: OwnershipTransferredEvent
): void {
Expand All @@ -136,121 +135,124 @@ export function handleOwnershipTransferred(
entity.save();
}

// export function handlePraised(event: PraisedEvent): void {
// let entity = new Praised(
// event.transaction.hash.concatI32(event.logIndex.toI32())
// );
// entity.creationId = event.params.creationId;
// entity.user = event.params.user;
// entity.pricePaid = event.params.pricePaid;
// entity.unitsPraised = event.params.unitsPraised;

// entity.blockNumber = event.block.number;
// entity.blockTimestamp = event.block.timestamp;
// entity.transactionHash = event.transaction.hash;

// entity.save();
// }

// -------------------------------------
// handlePraised
// -------------------------------------
export function handlePraised(event: PraisedEvent): void {
// Existing logic to save Praised event
// 1. Save the Praised event (immutable)
let praisedEntity = new Praised(
event.transaction.hash.concatI32(event.logIndex.toI32())
);

praisedEntity.creationId = event.params.creationId;
praisedEntity.user = event.params.user;
praisedEntity.pricePaid = event.params.pricePaid;
praisedEntity.unitsPraised = event.params.unitsPraised;

praisedEntity.blockNumber = event.block.number;
praisedEntity.blockTimestamp = event.block.timestamp;
praisedEntity.transactionHash = event.transaction.hash;
praisedEntity.save();

// 2. Update the Creation entity
let creation = Creation.load(event.params.creationId.toString());
let creationIdString = event.params.creationId.toString();
let creation = Creation.load(creationIdString);
if (creation !== null) {
creation.totalStaked = creation.totalStaked.plus(event.params.unitsPraised);
creation.praisePool = creation.praisePool.plus(event.params.pricePaid);
creation.updatedAt = event.block.timestamp;

creation.save();
}

// 3. Update the aggregated PraiseCount
let praiseCountId = creationIdString + "-" + event.params.user.toHexString();
let praiseCount = PraiseCount.load(praiseCountId);
if (praiseCount == null) {
praiseCount = new PraiseCount(praiseCountId);
// This must match the Creation's id exactly
praiseCount.creation = creationIdString;
praiseCount.userAddress = event.params.user;
praiseCount.noOfPraises = event.params.unitsPraised;
} else {
praiseCount.noOfPraises = praiseCount.noOfPraises.plus(
event.params.unitsPraised
);
}
praiseCount.save();
}

// -------------------------------------
// handleSoldManna
// -------------------------------------
export function handleSoldManna(event: SoldMannaEvent): void {
let entity = new SoldManna(
event.transaction.hash.concatI32(event.logIndex.toI32())
);
entity.seller = event.params.seller;
entity.mannaAmount = event.params.mannaAmount;
entity.ethAmount = event.params.ethAmount;

entity.blockNumber = event.block.number;
entity.blockTimestamp = event.block.timestamp;
entity.transactionHash = event.transaction.hash;

entity.save();
}

// -------------------------------------
// handleTransfer
// -------------------------------------
export function handleTransfer(event: TransferEvent): void {
let entity = new Transfer(
event.transaction.hash.concatI32(event.logIndex.toI32())
);
entity.from = event.params.from;
entity.to = event.params.to;
entity.value = event.params.value;

entity.blockNumber = event.block.number;
entity.blockTimestamp = event.block.timestamp;
entity.transactionHash = event.transaction.hash;

entity.save();
}

// export function handleUnpraised(event: UnpraisedEvent): void {
// let entity = new Unpraised(
// event.transaction.hash.concatI32(event.logIndex.toI32())
// );
// entity.creationId = event.params.creationId;
// entity.user = event.params.user;
// entity.unitsUnpraised = event.params.unitsUnpraised;
// entity.mannaRefunded = event.params.mannaRefunded;
// entity.unpraiseCost = event.params.unpraiseCost;

// entity.blockNumber = event.block.number;
// entity.blockTimestamp = event.block.timestamp;
// entity.transactionHash = event.transaction.hash;

// entity.save();
// }
// -------------------------------------
// handleUnpraised
// -------------------------------------
export function handleUnpraised(event: UnpraisedEvent): void {
// Existing logic to save Unpraised event
// 1. Save the Unpraised event (immutable)
let unpraisedEntity = new Unpraised(
event.transaction.hash.concatI32(event.logIndex.toI32())
);

unpraisedEntity.creationId = event.params.creationId;
unpraisedEntity.user = event.params.user;
unpraisedEntity.unitsUnpraised = event.params.unitsUnpraised;
unpraisedEntity.mannaRefunded = event.params.mannaRefunded;
unpraisedEntity.unpraiseCost = event.params.unpraiseCost;

unpraisedEntity.blockNumber = event.block.number;
unpraisedEntity.blockTimestamp = event.block.timestamp;
unpraisedEntity.transactionHash = event.transaction.hash;
unpraisedEntity.save();

// 2. Update the Creation entity
let creation = Creation.load(event.params.creationId.toString());
let creationIdString = event.params.creationId.toString();
let creation = Creation.load(creationIdString);
if (creation !== null) {
creation.totalStaked = creation.totalStaked.minus(
event.params.unitsUnpraised
);
creation.praisePool = creation.praisePool.minus(event.params.mannaRefunded);
creation.updatedAt = event.block.timestamp;

creation.save();
}

// 3. Update the aggregated PraiseCount
let praiseCountId = creationIdString + "-" + event.params.user.toHexString();
let praiseCount = PraiseCount.load(praiseCountId);
if (praiseCount !== null) {
praiseCount.noOfPraises = praiseCount.noOfPraises.minus(
event.params.unitsUnpraised
);

// Avoid negative values (just a safety check)
if (praiseCount.noOfPraises < BigInt.zero()) {
praiseCount.noOfPraises = BigInt.zero();
}
praiseCount.save();
}
}

0 comments on commit 55a7997

Please sign in to comment.