From 9dc00eaaea096814897d5ab92f23b9518ab2697f Mon Sep 17 00:00:00 2001 From: Leonardo Custodio Date: Mon, 10 Jul 2023 22:48:54 -0300 Subject: [PATCH] Accept native currency on Make or Take (#2) --- .../Mutations/CreateListingMutation.php | 38 +++++++++++-------- src/Rules/TokenExistsInCollection.php | 6 ++- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/src/GraphQL/Mutations/CreateListingMutation.php b/src/GraphQL/Mutations/CreateListingMutation.php index c3bcff1..9a5f9e3 100644 --- a/src/GraphQL/Mutations/CreateListingMutation.php +++ b/src/GraphQL/Mutations/CreateListingMutation.php @@ -101,11 +101,29 @@ public function resolve( ); } + protected function makeOrTakeRule(?string $collectionId = null, ?bool $isMake = true): array + { + $makeOrTake = $isMake ? 'makeAssetId' : 'takeAssetId'; + + return $collectionId === '0' ? [] : [ + $makeOrTake . '.collectionId' => [ + 'bail', + 'required_with:' . $makeOrTake . '.tokenId', + new MinBigInt(), + new MaxBigInt(Hex::MAX_UINT128), + Rule::exists('collections', 'collection_chain_id'), + ], + ]; + } + /** * Get the mutation's request validation rules. */ protected function rules(array $args = []): array { + $makeRule = $this->makeOrTakeRule($makeCollection = Arr::get($args, 'makeAssetId.collectionId')); + $takeRule = $this->makeOrTakeRule($takeCollection = Arr::get($args, 'takeAssetId.collectionId'), false); + return [ 'account' => [ 'bail', @@ -113,24 +131,12 @@ protected function rules(array $args = []): array 'max:255', new ValidSubstrateAddress(), ], - 'makeAssetId.collectionId' => [ - 'bail', - 'required_with:makeAssetId.tokenId', - new MinBigInt(), - new MaxBigInt(Hex::MAX_UINT128), - Rule::exists('collections', 'collection_chain_id'), - ], + 'makeAssetId' => new TokenExistsInCollection($makeCollection), + ...$makeRule, ...$this->getTokenFieldRules('makeAssetId'), - 'makeAssetId' => new TokenExistsInCollection(Arr::get($args, 'makeAssetId.collectionId')), - 'takeAssetId.collectionId' => [ - 'bail', - 'required_with:takeAsset.tokenId', - new MinBigInt(), - new MaxBigInt(Hex::MAX_UINT128), - Rule::exists('collections', 'collection_chain_id'), - ], + 'takeAssetId' => new TokenExistsInCollection($takeCollection), + ...$takeRule, ...$this->getTokenFieldRules('takeAssetId'), - 'takeAssetId' => new TokenExistsInCollection(Arr::get($args, 'takeAssetId.collectionId')), 'amount' => [ 'bail', new MinBigInt(1), diff --git a/src/Rules/TokenExistsInCollection.php b/src/Rules/TokenExistsInCollection.php index 6d46c5c..a846040 100644 --- a/src/Rules/TokenExistsInCollection.php +++ b/src/Rules/TokenExistsInCollection.php @@ -25,7 +25,11 @@ public function __construct(protected ?string $collectionId) */ public function validate(string $attribute, mixed $value, Closure $fail): void { - if (!$this->collectionId) { + if (is_null($this->collectionId)) { + return; + } + + if ($this->collectionId == 0 && $this->encodeTokenId($value) == 0) { return; }