diff --git a/graph/abraham/src/abraham.ts b/graph/abraham/src/abraham.ts index db1f4a0..e31ab76 100644 --- a/graph/abraham/src/abraham.ts +++ b/graph/abraham/src/abraham.ts @@ -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, @@ -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()) @@ -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()) @@ -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(); @@ -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 { @@ -136,49 +135,53 @@ 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()) @@ -186,14 +189,15 @@ export function handleSoldManna(event: SoldMannaEvent): void { 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()) @@ -201,56 +205,54 @@ export function handleTransfer(event: TransferEvent): void { 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(); + } }