diff --git a/src/lib/marketplace/EOACreateOrderStrategy.ts b/src/lib/marketplace/EOACreateOrderStrategy.ts index a8dbfa9..3449699 100644 --- a/src/lib/marketplace/EOACreateOrderStrategy.ts +++ b/src/lib/marketplace/EOACreateOrderStrategy.ts @@ -1,5 +1,6 @@ import { HypercertExchangeClient, + OrderValidatorCode, utils, } from "@hypercerts-org/marketplace-sdk"; import { verifyTypedData } from "ethers"; @@ -61,7 +62,8 @@ export default class EOACreateOrderStrategy extends MarketplaceStrategy { id: "temporary", }, ]); - if (!validationResult.valid) { + if (!this.evaluateOrderValidationResult(validationResult)) { + // Check if only error code is TOO_EARLY_TO_EXECUTE_ORDER throw new Errors.InvalidOrder(validationResult); } @@ -117,4 +119,25 @@ export default class EOACreateOrderStrategy extends MarketplaceStrategy { : null, ); } + + evaluateOrderValidationResult(validationResult: { + valid: boolean; + validatorCodes: OrderValidatorCode[]; + }) { + if (validationResult.valid) { + return true; + } + + if ( + validationResult.validatorCodes + .filter( + (code) => code !== OrderValidatorCode.TOO_EARLY_TO_EXECUTE_ORDER, + ) + .every((code) => code === OrderValidatorCode.ORDER_EXPECTED_TO_BE_VALID) + ) { + return true; + } + + return false; + } } diff --git a/src/lib/marketplace/MarketplaceStrategy.ts b/src/lib/marketplace/MarketplaceStrategy.ts index 46d5612..d55112f 100644 --- a/src/lib/marketplace/MarketplaceStrategy.ts +++ b/src/lib/marketplace/MarketplaceStrategy.ts @@ -1,8 +1,7 @@ +import { OrderValidatorCode } from "@hypercerts-org/marketplace-sdk"; import { DataResponse } from "../../types/api.js"; export abstract class MarketplaceStrategy { - constructor() {} - abstract executeCreate(): Promise>; protected returnSuccess( @@ -11,4 +10,9 @@ export abstract class MarketplaceStrategy { ): DataResponse { return { success: true, message, data }; } + + abstract evaluateOrderValidationResult(validationResult: { + valid: boolean; + validatorCodes: OrderValidatorCode[]; + }): boolean; } diff --git a/src/lib/marketplace/MultisigCreateOrderStrategy.ts b/src/lib/marketplace/MultisigCreateOrderStrategy.ts index bec153b..9ebbc4a 100644 --- a/src/lib/marketplace/MultisigCreateOrderStrategy.ts +++ b/src/lib/marketplace/MultisigCreateOrderStrategy.ts @@ -135,30 +135,11 @@ export default class MultisigCreateOrderStrategy extends MarketplaceStrategy { const [validationResult] = await hec.checkOrdersValidity([orderToValidate]); - if (!validationResult.valid) { - const errorCodes = validationResult.validatorCodes || []; - - // Check if error codes follow the expected pattern. Everything needs to be 0 (valid), - // except for the signature validation error. This is because when this request is - // made, the message is missing one or more signatures. - // The signature will be validated in the command. It's only skipped for now. - // TODO: get the command name when ready - const isValidErrorPattern = errorCodes.every((code, index) => { - if (index === 3) { - return ( - code === - OrderValidatorCode.MISSING_IS_VALID_SIGNATURE_FUNCTION_EIP1271 || - code === OrderValidatorCode.ORDER_EXPECTED_TO_BE_VALID - ); - } - return code === 0; - }); - - // Only proceed if it's the expected signature validation error pattern - if (!isValidErrorPattern) { - throw new Errors.InvalidOrder(validationResult); - } + // Only proceed if it's the expected signature validation error pattern + if (!this.evaluateOrderValidationResult(validationResult)) { + throw new Errors.InvalidOrder(validationResult); } + const tokenIds = orderDetails.itemIds.map( (id) => `${this.request.chainId}-${orderDetails.collection}-${id}`, ); @@ -209,4 +190,27 @@ export default class MultisigCreateOrderStrategy extends MarketplaceStrategy { timestamp: Math.floor(Date.now() / 1000), }); } + + evaluateOrderValidationResult(validationResult: { + valid: boolean; + validatorCodes: OrderValidatorCode[]; + }): boolean { + const errorCodes = validationResult.validatorCodes || []; + + // Check if error codes follow the expected pattern. Everything needs to be 0 (valid), + // except for the signature validation error. This is because when this request is + // made, the message is missing one or more signatures. + // The signature will be validated in the command. It's only skipped for now. + // TODO: get the command name when ready + return errorCodes.every((code, index) => { + if (index === 3) { + return ( + code === + OrderValidatorCode.MISSING_IS_VALID_SIGNATURE_FUNCTION_EIP1271 || + code === OrderValidatorCode.ORDER_EXPECTED_TO_BE_VALID + ); + } + return code === OrderValidatorCode.ORDER_EXPECTED_TO_BE_VALID; + }); + } }