Skip to content

Commit

Permalink
Accept native currency on Make or Take (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardocustodio authored Jul 11, 2023
1 parent 306aabb commit 9dc00ea
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
38 changes: 22 additions & 16 deletions src/GraphQL/Mutations/CreateListingMutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,36 +101,42 @@ 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',
'filled',
'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),
Expand Down
6 changes: 5 additions & 1 deletion src/Rules/TokenExistsInCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit 9dc00ea

Please sign in to comment.