Skip to content

Commit

Permalink
Fix validation
Browse files Browse the repository at this point in the history
  • Loading branch information
enjinabner committed Sep 9, 2024
1 parent f9f3d89 commit 43129a0
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 97 deletions.
7 changes: 5 additions & 2 deletions src/GraphQL/Traits/HasTokenInputRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use Illuminate\Support\Arr;
use Illuminate\Validation\Rule;
use Enjin\Platform\Beam\Rules\BeamPackExistInBeam;
use Enjin\Platform\Beam\Rules\BeamPackMaxTokenCount;
use Enjin\Platform\Beam\Rules\BeamPackMaxTokenSupply;

trait HasTokenInputRules
{
Expand Down Expand Up @@ -152,16 +154,17 @@ public function packTokenRules(array $args, ?string $collectionId = null, bool $
'filled',
'integer',
'min:1',
new MaxTokenSupply($collectionId),
new BeamPackMaxTokenSupply($collectionId),
],
'packs.*.tokens.*.claimQuantity' => [
'prohibited',
],
'packs.claimQuantity' => [
'packs.*.claimQuantity' => [
'bail',
'integer',
'min:1',
'max:1000',
new BeamPackMaxTokenCount($collectionId),
],
];
}
Expand Down
13 changes: 13 additions & 0 deletions src/Rules/BeamPackMaxTokenCount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Enjin\Platform\Beam\Rules;

use Illuminate\Support\Arr;

class BeamPackMaxTokenCount extends MaxTokenCount
{
protected function getInputTokens(): array
{
return collect(Arr::get($this->data, 'packs'))->flatMap(fn ($row) => Arr::get($row, 'tokens', []))->toArray();
}
}
87 changes: 2 additions & 85 deletions src/Rules/BeamPackMaxTokenSupply.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,95 +2,12 @@

namespace Enjin\Platform\Beam\Rules;

use Closure;
use Enjin\Platform\Beam\Enums\BeamType;
use Enjin\Platform\Beam\Models\BeamClaim;
use Enjin\Platform\Models\Collection;
use Enjin\Platform\Models\TokenAccount;
use Illuminate\Support\Arr;

class BeamPackMaxTokenSupply extends MaxTokenSupply
{
/**
* Determine if the validation rule passes.
*
* @param Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
protected function getInputTokens(): array
{
/**
* The total circulating supply of tokens must not exceed the collection's maximum token supply.
* For example, if the maximum token count is 10 and the maximum token supply is 10,
* the total circulating supply must not exceed 100.
*/
if ($this->collectionId
&& ($collection = Collection::firstWhere(['collection_chain_id' => $this->collectionId]))
&& ! is_null($this->limit = $collection->max_token_supply)
) {
if ((Arr::get($this->data, str_replace('tokenQuantityPerClaim', 'type', $attribute)) == BeamType::MINT_ON_DEMAND->name
&& !$collection->max_token_supply >= $value)
|| $this->limit == 0
) {
$fail($this->maxTokenSupplyMessage)->translate(['limit' => $this->limit]);

return;
}

if ($collection->max_token_count == 0) {
$fail('enjin-platform-beam::validation.max_token_count')->translate(['limit' => $this->limit]);

return;
}

$this->limit = $collection->max_token_supply * ($collection->max_token_count ?? 1);

$balanceCount = TokenAccount::where('token_accounts.collection_id', $collection->id)->sum('balance');
$claimCount = BeamClaim::where('type', BeamType::MINT_ON_DEMAND->name)
->whereHas('beam', fn ($query) => $query->where('collection_chain_id', $this->collectionId)->where('end', '>', now()))
->claimable()
->sum('quantity');

$tokenCount = 0;
$tokenCount = collect($this->data['tokens'])
->reduce(function ($carry, $token) {

if (Arr::get($token, 'tokenIds')) {
return collect($token['tokenIds'])->reduce(function ($val, $tokenId) use ($token) {
$range = $this->integerRange($tokenId);
$claimQuantity = Arr::get($token, 'claimQuantity', 1);
$quantityPerClaim = Arr::get($token, 'tokenQuantityPerClaim', 1);

return $val + (
$range === false
? $claimQuantity * $quantityPerClaim
: (($range[1] - $range[0]) + 1) * $claimQuantity * $quantityPerClaim
);
}, $carry);
}

if (Arr::get($token, 'tokenIdDataUpload')) {
$total = 0;
$handle = fopen($token['tokenIdDataUpload']->getPathname(), 'r');
while (($line = fgets($handle)) !== false) {
$range = $this->integerRange(trim($line));
$claimQuantity = Arr::get($token, 'claimQuantity', 1);
$quantityPerClaim = Arr::get($token, 'tokenQuantityPerClaim', 1);
$total += (
$range === false
? $claimQuantity * $quantityPerClaim
: (($range[1] - $range[0]) + 1) * $claimQuantity * $quantityPerClaim
);
}
fclose($handle);

return $total;
}

}, $tokenCount);

if ($this->limit < $balanceCount + $claimCount + $tokenCount) {
$fail($this->maxTokenSupplyMessage)->translate(['limit' => $this->limit]);
}
}
return collect(Arr::get($this->data, 'packs'))->flatMap(fn ($row) => Arr::get($row, 'tokens', []))->toArray();
}
}
11 changes: 8 additions & 3 deletions src/Rules/MaxTokenCount.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function validate(string $attribute, mixed $value, Closure $fail): void
* The sum of all unique tokens (including existing tokens, tokens in beams, and tokens to be created)
* must not exceed the collection's maximum token count.
*/
if (!Arr::get($this->data, 'tokens')) {
if (!$inputTokens = $this->getInputTokens()) {
return;
}

Expand All @@ -65,12 +65,12 @@ public function validate(string $attribute, mixed $value, Closure $fail): void
->groupBy('token_chain_id')
->count();

$tokens = collect($this->data['tokens'])
$tokens = collect($inputTokens)
->filter(fn ($data) => !empty(Arr::get($data, 'tokenIds')))
->pluck('tokenIds')
->flatten();

collect($this->data['tokens'])
collect($inputTokens)
->filter(fn ($data) => !empty(Arr::get($data, 'tokenIdDataUpload')))
->map(function ($data) use ($tokens) {
$handle = fopen($data['tokenIdDataUpload']->getPathname(), 'r');
Expand Down Expand Up @@ -137,4 +137,9 @@ public function validate(string $attribute, mixed $value, Closure $fail): void
}
}
}

protected function getInputTokens(): array
{
return Arr::get($this->data, 'tokens', []);
}
}
9 changes: 7 additions & 2 deletions src/Rules/MaxTokenSupply.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function validate(string $attribute, mixed $value, Closure $fail): void
* For example, if the maximum token count is 10 and the maximum token supply is 10,
* the total circulating supply must not exceed 100.
*/
if (!Arr::get($this->data, 'tokens')) {
if (!$inputTokens = $this->getInputTokens()) {
return;
}

Expand Down Expand Up @@ -81,7 +81,7 @@ public function validate(string $attribute, mixed $value, Closure $fail): void
->sum('quantity');

$tokenCount = 0;
$tokenCount = collect($this->data['tokens'])
$tokenCount = collect($inputTokens)
->reduce(function ($carry, $token) {

if (Arr::get($token, 'tokenIds')) {
Expand Down Expand Up @@ -123,4 +123,9 @@ public function validate(string $attribute, mixed $value, Closure $fail): void
}
}
}

protected function getInputTokens(): array
{
return Arr::get($this->data, 'tokens', []);
}
}
10 changes: 5 additions & 5 deletions tests/Feature/GraphQL/Mutations/CreateBeamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -537,11 +537,11 @@ public function test_it_will_fail_with_invalid_token_quantity_per_claim(): void
$response['error']
);

// $response = $this->graphql($this->method, $this->generateBeamPackData(), true);
// $this->assertArraySubset(
// ['packs.0.tokens.0.tokenQuantityPerClaim' => ['The packs.0.tokens.0.tokenQuantityPerClaim is invalid, the amount provided is bigger than the token account balance.']],
// $response['error']
// );
$response = $this->graphql($this->method, $this->generateBeamPackData(), true);
$this->assertArraySubset(
['packs.0.tokens.0.tokenQuantityPerClaim' => ['The packs.0.tokens.0.tokenQuantityPerClaim exceeded the maximum supply limit of 0 for unique tokens for this collection.']],
$response['error']
);
}

/**
Expand Down

0 comments on commit 43129a0

Please sign in to comment.