Skip to content

Commit

Permalink
[PLA-2058] Add anyone can infuse (#283)
Browse files Browse the repository at this point in the history
  • Loading branch information
enjinabner authored Oct 28, 2024
1 parent bc72fb9 commit 227386a
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Enjin\Platform\GraphQL\Schemas\Primary\Substrate\Mutations;

use Closure;
use Enjin\BlockchainTools\HexConverter;
use Enjin\Platform\GraphQL\Base\Mutation;
use Enjin\Platform\GraphQL\Schemas\Primary\Substrate\Traits\HasEncodableTokenId;
use Enjin\Platform\GraphQL\Schemas\Primary\Substrate\Traits\InPrimarySubstrateSchema;
Expand Down Expand Up @@ -99,7 +100,9 @@ public function resolve(
collectionId: $args['collectionId'],
tokenId: $this->encodeTokenId($args),
behavior: $blockchainService->getMutateTokenBehavior(Arr::get($args, 'mutation')),
listingForbidden: Arr::get($args, 'mutation.listingForbidden')
listingForbidden: Arr::get($args, 'mutation.listingForbidden'),
anyoneCanInfuse: Arr::get($args, 'mutation.anyoneCanInfuse'),
name: Arr::get($args, 'mutation.name'),
));

return Transaction::lazyLoadSelectFields(
Expand Down Expand Up @@ -128,8 +131,8 @@ public static function getEncodableParams(...$params): array
'mutation' => [
'behavior' => is_array($behavior) ? ['NoMutation' => null] : ['SomeMutation' => $behavior?->toEncodable()],
'listingForbidden' => Arr::get($params, 'listingForbidden'),
'anyoneCanInfuse' => null, // TODO: Add this when the mutation is changed
'name' => null, // TODO: Add this when the mutation is changed
'anyoneCanInfuse' => Arr::get($params, 'anyoneCanInfuse'),
'name' => ($name = Arr::get($params, 'name')) ? HexConverter::stringToHexPrefixed($name) : null,
],
];
}
Expand All @@ -142,11 +145,13 @@ protected function rulesCommon(array $args): array
$isBehaviorEmpty = Arr::get($args, 'mutation.behavior') === [];

return [
'mutation.behavior' => $isBehaviorEmpty ? [] : ['required_without:mutation.listingForbidden'],
'mutation.behavior' => $isBehaviorEmpty ? [] : ['required_without_all:mutation.listingForbidden,mutation.anyoneCanInfuse,mutation.name'],
'mutation.behavior.hasRoyalty.beneficiary' => ['nullable', 'bail', 'required_with:mutation.behavior.hasRoyalty.percentage', new ValidSubstrateAccount()],
'mutation.behavior.hasRoyalty.percentage' => ['required_with:mutation.behavior.hasRoyalty.beneficiary', new ValidRoyaltyPercentage()],
'mutation.behavior.isCurrency' => ['sometimes', 'accepted'],
'mutation.listingForbidden' => $isBehaviorEmpty ? [] : ['required_without:mutation.behavior'],
'mutation.listingForbidden' => $isBehaviorEmpty ? [] : ['required_without_all:mutation.behavior,mutation.anyoneCanInfuse,mutation.name'],
'mutation.anyoneCanInfuse' => $isBehaviorEmpty ? [] : ['required_without_all:mutation.behavior,mutation.listingForbidden,mutation.name'],
'mutation.name' => $isBehaviorEmpty ? [] : ['required_without_all:mutation.behavior,mutation.listingForbidden,mutation.anyoneCanInfuse'],
];
}

Expand Down
8 changes: 8 additions & 0 deletions src/GraphQL/Types/Input/Substrate/TokenMutationInputType.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ public function fields(): array
'type' => GraphQL::type('Boolean'),
'description' => __('enjin-platform::input_type.token_mutation.field.listingForbidden'),
],
'anyoneCanInfuse' => [
'type' => GraphQL::type('Boolean'),
'description' => __('enjin-platform::type.token.field.anyoneCanInfuse'),
],
'name' => [
'type' => GraphQL::type('String'),
'description' => __('enjin-platform::type.token.field.name'),
],
];
}
}
82 changes: 79 additions & 3 deletions tests/Feature/GraphQL/Mutations/MutateTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,82 @@ public function test_it_can_mutate_a_token_with_listing_forbidden(): void
Event::assertDispatched(TransactionCreated::class);
}

public function test_it_can_mutate_a_token_with_anyone_can_infuse(): void
{
$encodedData = TransactionSerializer::encode($this->method, MutateTokenMutation::getEncodableParams(
collectionId: $collectionId = $this->collection->collection_chain_id,
tokenId: $this->tokenIdEncoder->encode(),
anyoneCanInfuse: $anyoneCanInfuse = fake()->boolean(),
));

$response = $this->graphql($this->method, [
'collectionId' => $collectionId,
'tokenId' => $this->tokenIdEncoder->toEncodable(),
'mutation' => [
'anyoneCanInfuse' => $anyoneCanInfuse,
],
'nonce' => $nonce = fake()->numberBetween(),
]);

$this->assertArraySubset([
'method' => $this->method,
'state' => TransactionState::PENDING->name,
'encodedData' => $encodedData,
'signingPayload' => Substrate::getSigningPayload($encodedData, [
'nonce' => $nonce,
'tip' => '0',
]),
'wallet' => null,
], $response);

$this->assertDatabaseHas('transactions', [
'id' => $response['id'],
'method' => $this->method,
'state' => TransactionState::PENDING->name,
'encoded_data' => $encodedData,
]);

Event::assertDispatched(TransactionCreated::class);
}

public function test_it_can_mutate_a_token_with_name(): void
{
$encodedData = TransactionSerializer::encode($this->method, MutateTokenMutation::getEncodableParams(
collectionId: $collectionId = $this->collection->collection_chain_id,
tokenId: $this->tokenIdEncoder->encode(),
name: $name = fake()->name(),
));

$response = $this->graphql($this->method, [
'collectionId' => $collectionId,
'tokenId' => $this->tokenIdEncoder->toEncodable(),
'mutation' => [
'name' => $name,
],
'nonce' => $nonce = fake()->numberBetween(),
]);

$this->assertArraySubset([
'method' => $this->method,
'state' => TransactionState::PENDING->name,
'encodedData' => $encodedData,
'signingPayload' => Substrate::getSigningPayload($encodedData, [
'nonce' => $nonce,
'tip' => '0',
]),
'wallet' => null,
], $response);

$this->assertDatabaseHas('transactions', [
'id' => $response['id'],
'method' => $this->method,
'state' => TransactionState::PENDING->name,
'encoded_data' => $encodedData,
]);

Event::assertDispatched(TransactionCreated::class);
}

public function test_it_can_mutate_a_token_with_ss58_signing_account(): void
{
$encodedData = TransactionSerializer::encode($this->method, MutateTokenMutation::getEncodableParams(
Expand Down Expand Up @@ -679,7 +755,7 @@ public function test_it_will_fail_with_empty_mutation(): void
], true);

$this->assertArraySubset(
['mutation.behavior' => ['The mutation.behavior field is required when mutation.listing forbidden is not present.']],
['mutation.behavior' => ['The mutation.behavior field is required when none of mutation.listing forbidden / mutation.anyone can infuse / mutation.name are present.']],
$response['error']
);

Expand All @@ -697,7 +773,7 @@ public function test_it_will_fail_with_only_listing_forbidden_equals_null(): voi
], true);

$this->assertArraySubset(
['mutation.behavior' => ['The mutation.behavior field is required when mutation.listing forbidden is not present.']],
['mutation.behavior' => ['The mutation.behavior field is required when none of mutation.listing forbidden / mutation.anyone can infuse / mutation.name are present.']],
$response['error']
);

Expand Down Expand Up @@ -733,7 +809,7 @@ public function test_it_will_fail_with_only_behavior_equals_null(): void
], true);

$this->assertArraySubset(
['mutation.behavior' => ['The mutation.behavior field is required when mutation.listing forbidden is not present.']],
['mutation.behavior' => ['The mutation.behavior field is required when none of mutation.listing forbidden / mutation.anyone can infuse / mutation.name are present.']],
$response['error']
);

Expand Down

0 comments on commit 227386a

Please sign in to comment.