Skip to content

Commit

Permalink
[PLA-1735] Adds skipValidation to blockchain transactions (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardocustodio authored Apr 21, 2024
1 parent fbb5ca4 commit f48f221
Show file tree
Hide file tree
Showing 15 changed files with 276 additions and 34 deletions.
23 changes: 21 additions & 2 deletions src/GraphQL/Mutations/CancelListingMutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
use Enjin\BlockchainTools\HexConverter;
use Enjin\Platform\Facades\TransactionSerializer;
use Enjin\Platform\GraphQL\Schemas\Primary\Substrate\Traits\StoresTransactions;
use Enjin\Platform\GraphQL\Schemas\Primary\Traits\HasSkippableRules;
use Enjin\Platform\GraphQL\Schemas\Primary\Traits\HasTransactionDeposit;
use Enjin\Platform\GraphQL\Types\Input\Substrate\Traits\HasIdempotencyField;
use Enjin\Platform\GraphQL\Types\Input\Substrate\Traits\HasSigningAccountField;
use Enjin\Platform\GraphQL\Types\Input\Substrate\Traits\HasSimulateField;
use Enjin\Platform\Interfaces\PlatformBlockchainTransaction;
use Enjin\Platform\Marketplace\Rules\ListingNotCancelled;
use Enjin\Platform\Models\Transaction;
use Enjin\Platform\Rules\ValidHex;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\Type;
use Illuminate\Support\Arr;
Expand All @@ -24,6 +26,7 @@ class CancelListingMutation extends Mutation implements PlatformBlockchainTransa
use HasIdempotencyField;
use HasSigningAccountField;
use HasSimulateField;
use HasSkippableRules;
use HasTransactionDeposit;
use StoresTransactions;

Expand Down Expand Up @@ -59,6 +62,7 @@ public function args(): array
...$this->getSigningAccountField(),
...$this->getIdempotencyField(),
...$this->getSimulateField(),
...$this->getSkipValidationField(),
];
}

Expand Down Expand Up @@ -88,9 +92,9 @@ public static function getEncodableParams(...$params): array
}

/**
* Get the mutation's request validation rules.
* Get the mutation's validation rules.
*/
protected function rules(array $args = []): array
protected function rulesWithValidation(array $args): array
{
return [
'listingId' => [
Expand All @@ -101,4 +105,19 @@ protected function rules(array $args = []): array
],
];
}

/**
* Get the mutation's validation rules without DB rules.
*/
protected function rulesWithoutValidation(array $args): array
{
return [
'listingId' => [
'bail',
'filled',
'max:255',
new ValidHex(32),
],
];
}
}
82 changes: 68 additions & 14 deletions src/GraphQL/Mutations/CreateListingMutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Enjin\BlockchainTools\HexConverter;
use Enjin\Platform\Facades\TransactionSerializer;
use Enjin\Platform\GraphQL\Schemas\Primary\Substrate\Traits\StoresTransactions;
use Enjin\Platform\GraphQL\Schemas\Primary\Traits\HasSkippableRules;
use Enjin\Platform\GraphQL\Schemas\Primary\Traits\HasTokenIdFieldRules;
use Enjin\Platform\GraphQL\Schemas\Primary\Traits\HasTransactionDeposit;
use Enjin\Platform\GraphQL\Types\Input\Substrate\Traits\HasIdempotencyField;
Expand Down Expand Up @@ -34,6 +35,7 @@ class CreateListingMutation extends Mutation implements PlatformBlockchainTransa
use HasIdempotencyField;
use HasSigningAccountField;
use HasSimulateField;
use HasSkippableRules;
use HasTokenIdFieldRules;
use HasTransactionDeposit;
use StoresTransactions;
Expand Down Expand Up @@ -90,6 +92,7 @@ public function args(): array
...$this->getSigningAccountField(),
...$this->getIdempotencyField(),
...$this->getSimulateField(),
...$this->getSkipValidationField(),
];
}

Expand Down Expand Up @@ -145,7 +148,7 @@ public static function getEncodableParams(...$params): array
];
}

protected function makeOrTakeRule(?string $collectionId = null, ?bool $isMake = true): array
protected function makeOrTakeRuleExist(?string $collectionId = null, ?bool $isMake = true): array
{
$makeOrTake = $isMake ? 'makeAssetId' : 'takeAssetId';

Expand All @@ -164,13 +167,49 @@ function (string $attribute, mixed $value, Closure $fail) {
];
}

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),
],
];
}

/**
* Get the common rules.
*/
protected function rulesCommon(array $args): array
{
return [
'price' => [
'bail',
new MinBigInt(),
new MaxBigInt(),
],
'salt' => ['bail', 'filled', 'max:255'],
'auctionData.endBlock' => [
'bail',
'required_with:auctionData.startBlock',
new MinBigInt(),
new MaxBigInt(Hex::MAX_UINT32),
'gt:auctionData.startBlock',
],
];
}

/**
* Get the mutation's request validation rules.
* Get the mutation's validation rules.
*/
protected function rules(array $args = []): array
protected function rulesWithValidation(array $args): array
{
$makeRule = $this->makeOrTakeRule($makeCollection = Arr::get($args, 'makeAssetId.collectionId'));
$takeRule = $this->makeOrTakeRule($takeCollection = Arr::get($args, 'takeAssetId.collectionId'), false);
$makeRule = $this->makeOrTakeRuleExist($makeCollection = Arr::get($args, 'makeAssetId.collectionId'));
$takeRule = $this->makeOrTakeRuleExist($takeCollection = Arr::get($args, 'takeAssetId.collectionId'), false);

return [
'makeAssetId' => new TokenExistsInCollection($makeCollection),
Expand All @@ -185,12 +224,6 @@ protected function rules(array $args = []): array
new MaxBigInt(),
new EnoughTokenSupply(),
],
'price' => [
'bail',
new MinBigInt(),
new MaxBigInt(),
],
'salt' => ['bail', 'filled', 'max:255'],
'auctionData.startBlock' => [
'bail',
'required_with:auctionData.endBlock',
Expand All @@ -199,12 +232,33 @@ protected function rules(array $args = []): array
new FutureBlock(),
'lte:auctionData.endBlock',
],
'auctionData.endBlock' => [
];
}

/**
* Get the mutation's validation rules without DB rules.
*/
protected function rulesWithoutValidation(array $args): array
{
$makeRule = $this->makeOrTakeRule(Arr::get($args, 'makeAssetId.collectionId'));
$takeRule = $this->makeOrTakeRule(Arr::get($args, 'takeAssetId.collectionId'), false);

return [
...$makeRule,
...$this->getTokenFieldRules('makeAssetId'),
...$takeRule,
...$this->getTokenFieldRules('takeAssetId'),
'amount' => [
'bail',
'required_with:auctionData.startBlock',
new MinBigInt(1),
new MaxBigInt(),
],
'auctionData.startBlock' => [
'bail',
'required_with:auctionData.endBlock',
new MinBigInt(),
new MaxBigInt(Hex::MAX_UINT32),
'gt:auctionData.startBlock',
'lte:auctionData.endBlock',
],
];
}
Expand Down
38 changes: 33 additions & 5 deletions src/GraphQL/Mutations/FillListingMutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Enjin\BlockchainTools\HexConverter;
use Enjin\Platform\Facades\TransactionSerializer;
use Enjin\Platform\GraphQL\Schemas\Primary\Substrate\Traits\StoresTransactions;
use Enjin\Platform\GraphQL\Schemas\Primary\Traits\HasSkippableRules;
use Enjin\Platform\GraphQL\Schemas\Primary\Traits\HasTransactionDeposit;
use Enjin\Platform\GraphQL\Types\Input\Substrate\Traits\HasIdempotencyField;
use Enjin\Platform\GraphQL\Types\Input\Substrate\Traits\HasSigningAccountField;
Expand All @@ -15,6 +16,7 @@
use Enjin\Platform\Models\Transaction;
use Enjin\Platform\Rules\MaxBigInt;
use Enjin\Platform\Rules\MinBigInt;
use Enjin\Platform\Rules\ValidHex;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\Type;
use Illuminate\Support\Arr;
Expand All @@ -26,6 +28,7 @@ class FillListingMutation extends Mutation implements PlatformBlockchainTransact
use HasIdempotencyField;
use HasSigningAccountField;
use HasSimulateField;
use HasSkippableRules;
use HasTransactionDeposit;
use StoresTransactions;

Expand Down Expand Up @@ -65,6 +68,7 @@ public function args(): array
...$this->getSigningAccountField(),
...$this->getIdempotencyField(),
...$this->getSimulateField(),
...$this->getSkipValidationField(),
];
}

Expand Down Expand Up @@ -95,9 +99,23 @@ public static function getEncodableParams(...$params): array
}

/**
* Get the mutation's request validation rules.
* Get the common rules.
*/
protected function rules(array $args = []): array
protected function rulesCommon(array $args): array
{
return [
'amount' => [
'bail',
new MinBigInt(1),
new MaxBigInt(),
],
];
}

/**
* Get the mutation's validation rules.
*/
protected function rulesWithValidation(array $args): array
{
return [
'listingId' => [
Expand All @@ -106,10 +124,20 @@ protected function rules(array $args = []): array
'max:255',
new ListingNotCancelled(),
],
'amount' => [
];
}

/**
* Get the mutation's validation rules without DB rules.
*/
protected function rulesWithoutValidation(array $args): array
{
return [
'listingId' => [
'bail',
new MinBigInt(1),
new MaxBigInt(),
'filled',
'max:255',
new ValidHex(32),
],
];
}
Expand Down
23 changes: 21 additions & 2 deletions src/GraphQL/Mutations/FinalizeAuctionMutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
use Enjin\BlockchainTools\HexConverter;
use Enjin\Platform\Facades\TransactionSerializer;
use Enjin\Platform\GraphQL\Schemas\Primary\Substrate\Traits\StoresTransactions;
use Enjin\Platform\GraphQL\Schemas\Primary\Traits\HasSkippableRules;
use Enjin\Platform\GraphQL\Schemas\Primary\Traits\HasTransactionDeposit;
use Enjin\Platform\GraphQL\Types\Input\Substrate\Traits\HasIdempotencyField;
use Enjin\Platform\GraphQL\Types\Input\Substrate\Traits\HasSigningAccountField;
use Enjin\Platform\GraphQL\Types\Input\Substrate\Traits\HasSimulateField;
use Enjin\Platform\Interfaces\PlatformBlockchainTransaction;
use Enjin\Platform\Marketplace\Rules\ListingNotCancelled;
use Enjin\Platform\Models\Transaction;
use Enjin\Platform\Rules\ValidHex;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\Type;
use Illuminate\Support\Arr;
Expand All @@ -24,6 +26,7 @@ class FinalizeAuctionMutation extends Mutation implements PlatformBlockchainTran
use HasIdempotencyField;
use HasSigningAccountField;
use HasSimulateField;
use HasSkippableRules;
use HasTransactionDeposit;
use StoresTransactions;

Expand Down Expand Up @@ -59,6 +62,7 @@ public function args(): array
...$this->getSigningAccountField(),
...$this->getIdempotencyField(),
...$this->getSimulateField(),
...$this->getSkipValidationField(),
];
}

Expand Down Expand Up @@ -88,9 +92,9 @@ public static function getEncodableParams(...$params): array
}

/**
* Get the mutation's request validation rules.
* Get the mutation's validation rules.
*/
protected function rules(array $args = []): array
protected function rulesWithValidation(array $args): array
{
return [
'listingId' => [
Expand All @@ -101,4 +105,19 @@ protected function rules(array $args = []): array
],
];
}

/**
* Get the mutation's validation rules without DB rules.
*/
protected function rulesWithoutValidation(array $args): array
{
return [
'listingId' => [
'bail',
'filled',
'max:255',
new ValidHex(32),
],
];
}
}
Loading

0 comments on commit f48f221

Please sign in to comment.