Skip to content

Commit

Permalink
Add tokens pack
Browse files Browse the repository at this point in the history
  • Loading branch information
enjinabner committed Aug 5, 2024
1 parent b50dc3b commit befef64
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 106 deletions.
88 changes: 19 additions & 69 deletions src/GraphQL/Mutations/AddTokensMutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,21 @@
namespace Enjin\Platform\Beam\GraphQL\Mutations;

use Closure;
use Enjin\Platform\Beam\Enums\BeamType;
use Enjin\Platform\Beam\GraphQL\Traits\HasBeamCommonFields;
use Enjin\Platform\Beam\GraphQL\Traits\HasTokenInputRules;
use Enjin\Platform\Beam\Models\Beam;
use Enjin\Platform\Beam\Rules\BeamExists;
use Enjin\Platform\Beam\Rules\MaxTokenCount;
use Enjin\Platform\Beam\Rules\MaxTokenSupply;
use Enjin\Platform\Beam\Rules\TokensDoNotExistInBeam;
use Enjin\Platform\Beam\Rules\TokensDoNotExistInCollection;
use Enjin\Platform\Beam\Rules\TokensExistInCollection;
use Enjin\Platform\Beam\Rules\TokenUploadExistInCollection;
use Enjin\Platform\Beam\Rules\TokenUploadNotExistInBeam;
use Enjin\Platform\Beam\Rules\TokenUploadNotExistInCollection;
use Enjin\Platform\Beam\Rules\UniqueTokenIds;
use Enjin\Platform\Beam\Services\BeamService;
use Enjin\Platform\Rules\DistinctAttributes;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\Type;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\Rule;
use Rebing\GraphQL\Support\Facades\GraphQL;

class AddTokensMutation extends Mutation
{
use HasBeamCommonFields;
use HasTokenInputRules;

/**
* Get the mutation's attributes.
Expand Down Expand Up @@ -59,9 +49,13 @@ public function args(): array
'description' => __('enjin-platform-beam::mutation.claim_beam.args.code'),
],
'tokens' => [
'type' => GraphQL::type('[ClaimToken!]!'),
'type' => GraphQL::type('[ClaimToken!]'),
'description' => __('enjin-platform-beam::input_type.claim_token.description'),
],
'packs' => [
'type' => GraphQL::type('[BeamPackInput!]'),
'description' => __('enjin-platform-beam::input_type.beam_pack.description'),
],
];
}

Expand All @@ -76,7 +70,13 @@ public function resolve(
Closure $getSelectFields,
BeamService $beam
) {
return DB::transaction(fn () => $beam->addTokens($args['code'], $args['tokens']));
return DB::transaction(
fn () => $beam->addTokens(
$args['code'],
Arr::get($args, 'tokens', []),
Arr::get($args, 'packs', [])
)
);
}

/**
Expand All @@ -92,61 +92,11 @@ protected function rules(array $args = []): array
'max:1024',
new BeamExists(),
],
'tokens' => ['bail', 'array', 'min:1', new UniqueTokenIds()],
'tokens.*.attributes' => Rule::forEach(function ($value, $attribute) use ($args) {
if (empty($value)) {
return [];
}

return [
'nullable',
'bail',
'array',
'min:1',
'max:10',
new DistinctAttributes(),
Rule::prohibitedIf(BeamType::getEnumCase(Arr::get($args, str_replace('attributes', 'type', $attribute))) == BeamType::TRANSFER_TOKEN),
];
}),
'tokens.*.attributes.*.key' => 'max:255',
'tokens.*.attributes.*.value' => 'max:1000',
'tokens.*.tokenIds' => Rule::forEach(function ($value, $attribute) use ($args, $beam) {
return [
'bail',
'required_without:tokens.*.tokenIdDataUpload',
'prohibits:tokens.*.tokenIdDataUpload',
'distinct',
BeamType::getEnumCase(Arr::get($args, str_replace('tokenIds', 'type', $attribute))) == BeamType::TRANSFER_TOKEN
? new TokensExistInCollection($beam?->collection_chain_id)
: new TokensDoNotExistInCollection($beam?->collection_chain_id),
new TokensDoNotExistInBeam($beam),
];
}),
'tokens.*.tokenIdDataUpload' => Rule::forEach(function ($value, $attribute) use ($args, $beam) {
return [
'bail',
'required_without:tokens.*.tokenIds',
'prohibits:tokens.*.tokenIds',
BeamType::getEnumCase(Arr::get($args, str_replace('tokenIdDataUpload', 'type', $attribute))) == BeamType::TRANSFER_TOKEN
? new TokenUploadExistInCollection($beam?->collection_chain_id)
: new TokenUploadNotExistInCollection($beam?->collection_chain_id),
new TokenUploadNotExistInBeam($beam),
];
}),
'tokens.*.tokenQuantityPerClaim' => [
'bail',
'filled',
'integer',
'min:1',
new MaxTokenSupply($beam?->collection_chain_id),
],
'tokens.*.claimQuantity' => [
'bail',
'filled',
'integer',
'min:1',
new MaxTokenCount($beam?->collection_chain_id),
],
...match (true) {
!$beam => [],
!$beam?->is_pack => $this->tokenRules($args, $beam?->collection_chain_id),
default => $this->packTokenRules($args, $beam?->collection_chain_id),
},
];
}
}
4 changes: 2 additions & 2 deletions src/GraphQL/Mutations/CreateBeamMutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ function (string $attribute, mixed $value, Closure $fail) {
new IsCollectionOwnerOrApproved(),
],
'flags.*.flag' => ['required', 'distinct'],
...$this->tokenRules($args, $args['collectionId']),
...$this->packTokenRules($args, $args['collectionId']),
...$this->tokenRules($args, $args['collectionId'], true),
...$this->packTokenRules($args, $args['collectionId'], true),
];
}
}
4 changes: 2 additions & 2 deletions src/GraphQL/Mutations/UpdateBeamMutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ protected function rules(array $args = []): array
'end' => ['filled', 'date', new IsEndDateValid()],
...match (true) {
!$beam => [],
!$beam?->is_pack => $this->tokenRules($args, $beam?->collection_chain_id, false),
default => $this->packTokenRules($args, $beam?->collection_chain_id, false),
!$beam?->is_pack => $this->tokenRules($args, $beam?->collection_chain_id),
default => $this->packTokenRules($args, $beam?->collection_chain_id),
},
];
}
Expand Down
4 changes: 2 additions & 2 deletions src/GraphQL/Traits/HasTokenInputRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

trait HasTokenInputRules
{
public function tokenRules(array $args, ?string $collectionId = null, bool $withPacks = true): array
public function tokenRules(array $args, ?string $collectionId = null, bool $withPacks = false): array
{
return [
'tokens' => [
Expand Down Expand Up @@ -85,7 +85,7 @@ public function tokenRules(array $args, ?string $collectionId = null, bool $with
];
}

public function packTokenRules(array $args, ?string $collectionId = null, bool $withTokens = true): array
public function packTokenRules(array $args, ?string $collectionId = null, bool $withTokens = false): array
{
return [
'packs' => [
Expand Down
19 changes: 12 additions & 7 deletions src/Services/BeamService.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public function updateByCode(string $code, array $values): Model

if ($beam->fill($values)->save()) {
if ($beam->is_pack && ($packs = Arr::get($values, 'packs', []))) {
$this->createPackClaims($beam, $packs, false);
$this->createPackClaims($beam, $packs);
} elseif ($tokens = Arr::get($values, 'tokens', [])) {
Cache::increment(
self::key($beam->code),
Expand All @@ -205,14 +205,19 @@ public function updateByCode(string $code, array $values): Model
/**
* Update beam by code.
*/
public function addTokens(string $code, array $tokens): bool
public function addTokens(string $code, ?array $tokens = [], ?array $packs = []): bool
{
$beam = Beam::whereCode($code)->firstOrFail();
Cache::increment(
self::key($beam->code),
$this->createClaims($beam, $tokens)
);
TokensAdded::safeBroadcast(event: ['beamCode' => $beam->code, 'code' => $code, 'tokenIds' => collect($tokens)->pluck('tokenIds')->all()]);

if ($beam->is_pack && $packs) {
$this->createPackClaims($beam, $packs);
} elseif ($tokens) {
Cache::increment(
self::key($beam->code),
$this->createClaims($beam, $tokens)
);
TokensAdded::safeBroadcast(event: ['beamCode' => $beam->code, 'code' => $code, 'tokenIds' => collect($tokens)->pluck('tokenIds')->all()]);
}

return true;
}
Expand Down
Loading

0 comments on commit befef64

Please sign in to comment.