Skip to content

Commit

Permalink
fix: do not create an offer when the seller does not exist (#614)
Browse files Browse the repository at this point in the history
* fix: do not create an offer when the seller does not exist

* fix subgraph tests

* fix subgraph test
  • Loading branch information
levalleux-ludo authored Nov 28, 2023
1 parent f2ab29e commit ad49a79
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 54 deletions.
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
46 changes: 16 additions & 30 deletions packages/subgraph/tests/account-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,19 @@ import {
mockIpfsFile
} from "matchstick-as/assembly/index";
import {
handleSellerCreatedEvent,
handleSellerUpdatedEvent,
handleBuyerCreatedEvent,
handleSellerCreatedEventWithoutMetadataUri,
handleSellerUpdateAppliedEvent
} from "../src/mappings/account-handler";
import {
createSellerCreatedEvent,
createSellerUpdatedEvent,
createBuyerCreatedEvent,
mockBosonVoucherContractCalls,
createSellerCreatedEventLegacy,
createSellerUpdateAppliedEvent,
mockCreateProduct
mockCreateProduct,
createSeller
} from "./mocks";
import { getSellerMetadataEntityId } from "../src/entities/metadata/seller";
import {
Expand All @@ -31,7 +30,6 @@ import {
Offer,
ProductV1Product,
SalesChannel,
SalesChannelDeployment,
SellerMetadata
} from "../generated/schema";
import { getMetadataEntityId } from "../src/entities/metadata/utils";
Expand Down Expand Up @@ -76,30 +74,6 @@ test("handle legacy SellerCreatedEvent", () => {
);
});

function createSeller(
sellerId: i32,
sellerAddress: string,
sellerMetadataFilepath: string
): string {
mockBosonVoucherContractCalls(voucherCloneAddress, "ipfs://", 0);
mockIpfsFile(sellerMetadataHash, sellerMetadataFilepath);
const sellerCreatedEvent = createSellerCreatedEvent(
sellerId,
sellerAddress,
sellerAddress,
sellerAddress,
sellerAddress,
voucherCloneAddress,
0,
0,
sellerAddress,
"ipfs://" + sellerMetadataHash
);

handleSellerCreatedEvent(sellerCreatedEvent);
return sellerId.toString();
}

function updateSellerMetadata(
sellerId: i32,
sellerMetadataFilepath: string
Expand All @@ -123,7 +97,13 @@ function updateSellerMetadata(
}

test("handle SellerCreatedEvent", () => {
const sellerId = createSeller(1, sellerAddress, "tests/metadata/seller.json");
const sellerId = createSeller(
1,
sellerAddress,
"tests/metadata/seller.json",
voucherCloneAddress,
sellerMetadataHash
);

assert.fieldEquals("Seller", sellerId, "id", sellerId);
assert.fieldEquals(
Expand Down Expand Up @@ -229,7 +209,13 @@ test("handle BuyerCreatedEvent", () => {
});

test("add/remove product salesChannels", () => {
const sellerId = createSeller(1, sellerAddress, "tests/metadata/seller.json");
const sellerId = createSeller(
1,
sellerAddress,
"tests/metadata/seller.json",
voucherCloneAddress,
sellerMetadataHash
);

// mock creation of ProductV1Product for Product_A and Product_B
let productA = mockCreateProduct(sellerId, "Product_A", 1);
Expand Down
31 changes: 9 additions & 22 deletions packages/subgraph/tests/funds-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,21 @@ import {
beforeEach,
test,
assert,
clearStore,
mockIpfsFile
clearStore
} from "matchstick-as/assembly/index";
import {
handleFundsDepositedEvent,
handleFundsWithdrawnEvent,
handleFundsEncumberedEvent,
handleFundsReleasedEvent
} from "../src/mappings/funds-handler";
import {
handleSellerCreatedEvent,
handleSellerCreatedEventWithoutMetadataUri
} from "../src/mappings/account-handler";
import { handleSellerCreatedEventWithoutMetadataUri } from "../src/mappings/account-handler";
import {
createFundsDepositedEvent,
createFundsEncumberedEvent,
createFundsReleasedEvent,
createFundsWithdrawnEvent,
createSellerCreatedEvent,
createSeller,
createSellerCreatedEventLegacy,
mockBosonVoucherContractCalls
} from "./mocks";
Expand Down Expand Up @@ -109,21 +105,12 @@ test("handle legacy FundsEncumberedEvent", () => {
});

test("handle FundsEncumberedEvent", () => {
mockBosonVoucherContractCalls(voucherCloneAddress, "ipfs://", 1);
mockIpfsFile(sellerMetadataHash, "tests/metadata/seller.json");
handleSellerCreatedEvent(
createSellerCreatedEvent(
sellerId,
sellerAddress,
sellerAddress,
sellerAddress,
sellerAddress,
voucherCloneAddress,
0,
0,
sellerAddress,
"ipfs://" + sellerMetadataHash
)
createSeller(
sellerId,
sellerAddress,
"tests/metadata/seller.json",
voucherCloneAddress,
sellerMetadataHash
);
const fundsDepositedEvent = createFundsDepositedEvent(
sellerId,
Expand Down
30 changes: 29 additions & 1 deletion packages/subgraph/tests/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ import {
} from "../generated/BosonFundsHandler/IBosonFundsHandler";
import {
newMockEvent,
createMockedFunction
createMockedFunction,
mockIpfsFile
} from "matchstick-as/assembly/index";
import { ethereum, Address, BigInt } from "@graphprotocol/graph-ts";
import {
Expand All @@ -39,6 +40,7 @@ import {
import { SellerUpdateApplied } from "../generated/BosonAccountHandler/IBosonAccountHandler";
import { getProductId } from "../src/entities/metadata/product-v1/product";
import { ProductV1Media, ProductV1Product } from "../generated/schema";
import { handleSellerCreatedEvent } from "../src/mappings/account-handler";

export function createOfferCreatedEvent(
offerId: i32,
Expand Down Expand Up @@ -887,3 +889,29 @@ export function mockCreateProduct(
product.save();
return product;
}

export function createSeller(
sellerId: i32,
sellerAddress: string,
sellerMetadataFilepath: string,
voucherCloneAddress: string,
sellerMetadataHash: string
): string {
mockBosonVoucherContractCalls(voucherCloneAddress, "ipfs://", 0);
mockIpfsFile(sellerMetadataHash, sellerMetadataFilepath);
const sellerCreatedEvent = createSellerCreatedEvent(
sellerId,
sellerAddress,
sellerAddress,
sellerAddress,
sellerAddress,
voucherCloneAddress,
0,
0,
sellerAddress,
"ipfs://" + sellerMetadataHash
);

handleSellerCreatedEvent(sellerCreatedEvent);
return sellerId.toString();
}
25 changes: 25 additions & 0 deletions packages/subgraph/tests/offer-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import {
createOfferCreatedEvent,
createOfferVoidedEvent,
createSeller,
mockExchangeTokenContractCalls
} from "./mocks";

Expand All @@ -24,6 +25,9 @@ const metadataHash = "QmPK1s3pNYLi9ERiq3BDxKa4XosgWwFRQUydHUtz4YgpqB";

const offerId = 1;
const sellerId = 1;
const sellerAddress = "0x89205A3A3b2A69De6Dbf7f01ED13B2108B2c43e7";
const voucherCloneAddress = "0x123456789a123456789a123456789a123456789a";
const sellerMetadataHash = "QmZffs1Uv6pmf4649UpMqinDord9QBerJaWcwRgdenAto1";
const price = 1;
const sellerDeposit = 1;
const protocolFee = 1;
Expand Down Expand Up @@ -86,6 +90,13 @@ test("handle OfferCreatedEvent with BASE metadata", () => {
exchangeTokenSymbol
);
mockIpfsFile(metadataHash, "tests/metadata/base.json");
createSeller(
1,
sellerAddress,
"tests/metadata/seller.json",
voucherCloneAddress,
sellerMetadataHash
);

handleOfferCreatedEvent(offerCreatedEvent);

Expand Down Expand Up @@ -119,6 +130,13 @@ test("handle OfferCreatedEvent with PRODUCT_V1 metadata", () => {
exchangeTokenSymbol
);
mockIpfsFile(metadataHash, "tests/metadata/product-v1-full.json");
createSeller(
1,
sellerAddress,
"tests/metadata/seller.json",
voucherCloneAddress,
sellerMetadataHash
);

handleOfferCreatedEvent(offerCreatedEvent);

Expand Down Expand Up @@ -157,6 +175,13 @@ test("handle OfferVoidedEvent", () => {
exchangeTokenSymbol
);
mockIpfsFile(metadataHash, "tests/metadata/base.json");
createSeller(
1,
sellerAddress,
"tests/metadata/seller.json",
voucherCloneAddress,
sellerMetadataHash
);
handleOfferCreatedEvent(offerCreatedEvent);

const offerVoidedEvent = createOfferVoidedEvent(1, 1, exchangeTokenAddress);
Expand Down

0 comments on commit ad49a79

Please sign in to comment.