Skip to content

Commit

Permalink
fix: do not create an offer when the seller does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
levalleux-ludo committed Nov 27, 2023
1 parent f2ab29e commit cb617ac
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
7 changes: 7 additions & 0 deletions packages/subgraph/src/mappings/account-handler.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable @typescript-eslint/ban-types */
import { BigInt } from "@graphprotocol/graph-ts";
import {
SellerCreated,
SellerUpdatePending,
Expand Down Expand Up @@ -36,6 +38,11 @@ import { saveAccountEventLog } from "../entities/event-log";
import { saveSellerMetadata } from "../entities/metadata/handler";
import { getSellerMetadataEntityId } from "../entities/metadata/seller";

export function checkSellerExist(sellerId: BigInt): boolean {
const seller = Seller.load(sellerId.toString());
return !!seller;
}

export function handleSellerCreatedEventWithoutMetadataUri(
event: SellerCreatedLegacy
): void {
Expand Down
19 changes: 18 additions & 1 deletion packages/subgraph/src/mappings/offer-handler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BigInt } from "@graphprotocol/graph-ts";
import { BigInt, log } from "@graphprotocol/graph-ts";
import {
OfferCreated,
OfferVoided,
Expand All @@ -17,6 +17,7 @@ import {
saveDisputeResolutionTermsLegacy
} from "../entities/dispute-resolution";
import { saveOfferEventLog } from "../entities/event-log";
import { checkSellerExist } from "./account-handler";

export function handleOfferCreatedEvent(event: OfferCreated): void {
const offerId = event.params.offerId;
Expand All @@ -30,6 +31,14 @@ export function handleOfferCreatedEvent(event: OfferCreated): void {
const offerFeesStruct = event.params.offerFees;
const disputeResolutionTermsStruct = event.params.disputeResolutionTerms;

if (!checkSellerExist(offerStruct.sellerId)) {
log.warning(
"Offer '{}' won't be created because seller '{}' does not exist",
[offerId.toString(), offerStruct.sellerId.toString()]
);
return;
}

offer = new Offer(offerId.toString());
offer.createdAt = event.block.timestamp;
offer.price = offerStruct.price;
Expand Down Expand Up @@ -97,6 +106,14 @@ export function handleOfferCreatedEventLegacy(event: OfferCreatedLegacy): void {
const offerFeesStruct = event.params.offerFees;
const disputeResolutionTermsStruct = event.params.disputeResolutionTerms;

if (!checkSellerExist(offerStruct.sellerId)) {
log.warning(
"Offer '{}' won't be created because seller '{}' does not exist",
[offerId.toString(), offerStruct.sellerId.toString()]
);
return;
}

offer = new Offer(offerId.toString());
offer.createdAt = event.block.timestamp;
offer.price = offerStruct.price;
Expand Down

0 comments on commit cb617ac

Please sign in to comment.