-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PLA-1734][PLA-1743] Fixes WhitelistedPallet, replaces IsFuelTankOwn…
…er by CanDispatch (#42)
- Loading branch information
1 parent
4624d5c
commit ddb5b18
Showing
13 changed files
with
219 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,4 +56,9 @@ public function toArray(): array | |
], | ||
]; | ||
} | ||
|
||
public function validate(string $value): bool | ||
{ | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,4 +53,9 @@ public function toArray(): array | |
], | ||
]; | ||
} | ||
|
||
public function validate(string $value): bool | ||
{ | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
<?php | ||
|
||
namespace Enjin\Platform\FuelTanks\Rules; | ||
|
||
use Closure; | ||
use Enjin\Platform\FuelTanks\Enums\DispatchRule; | ||
use Enjin\Platform\FuelTanks\Models\FuelTank; | ||
use Enjin\Platform\FuelTanks\Models\Substrate\MaxFuelBurnPerTransactionParams; | ||
use Enjin\Platform\FuelTanks\Models\Substrate\PermittedCallsParams; | ||
use Enjin\Platform\FuelTanks\Models\Substrate\PermittedExtrinsicsParams; | ||
use Enjin\Platform\FuelTanks\Models\Substrate\RequireTokenParams; | ||
use Enjin\Platform\FuelTanks\Models\Substrate\TankFuelBudgetParams; | ||
use Enjin\Platform\FuelTanks\Models\Substrate\UserFuelBudgetParams; | ||
use Enjin\Platform\FuelTanks\Models\Substrate\WhitelistedCallersParams; | ||
use Enjin\Platform\FuelTanks\Models\Substrate\WhitelistedCollectionsParams; | ||
use Enjin\Platform\FuelTanks\Models\Substrate\WhitelistedPalletsParams; | ||
use Enjin\Platform\Rules\Traits\HasDataAwareRule; | ||
use Enjin\Platform\Support\Account; | ||
use Enjin\Platform\Support\SS58Address; | ||
use Illuminate\Contracts\Validation\DataAwareRule; | ||
use Illuminate\Contracts\Validation\ValidationRule; | ||
use Illuminate\Support\Arr; | ||
|
||
class CanDispatch implements DataAwareRule, ValidationRule | ||
{ | ||
use HasDataAwareRule; | ||
|
||
/** | ||
* Run the validation rule. | ||
* | ||
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail | ||
*/ | ||
public function validate(string $attribute, mixed $value, Closure $fail): void | ||
{ | ||
$fuelTank = FuelTank::where('public_key', SS58Address::getPublicKey(Arr::get($this->data, 'tankId'))) | ||
->with('owner') | ||
->first(); | ||
|
||
if (!$fuelTank) { | ||
$fail(__('validation.exists', ['attribute' => $attribute]))->translate(); | ||
|
||
return; | ||
} | ||
|
||
$caller = SS58Address::getPublicKey(Arr::get($this->data, 'signingAccount') ?? Account::daemonPublicKey()); | ||
$ruleSetRules = $fuelTank->dispatchRules()->where('rule_set_id', Arr::get($this->data, 'ruleSetId'))->get(); | ||
|
||
if ($ruleSetRules->isEmpty()) { | ||
$fail(__('enjin-platform-fuel-tanks::validation.dispatch_rule_not_found'))->translate(); | ||
|
||
return; | ||
} | ||
|
||
$canDispatch = $ruleSetRules->filter(function ($ruleSetRule) use ($caller) { | ||
$ruleType = Arr::get($ruleSetRule, 'rule'); | ||
$value = Arr::get($ruleSetRule, 'value'); | ||
|
||
return $this->canDispatchWithRule($caller, $ruleType, $value); | ||
}); | ||
|
||
if ($canDispatch->count() < $ruleSetRules->count()) { | ||
$fail(__('enjin-platform-fuel-tanks::validation.dispatch_rule_requirements'))->translate(); | ||
} | ||
} | ||
|
||
protected function canDispatchWithRule(string $caller, string $ruleType, mixed $value): bool | ||
{ | ||
switch ($ruleType) { | ||
case DispatchRule::WHITELISTED_CALLERS->value: | ||
$dispatchRule = new WhitelistedCallersParams($value); | ||
|
||
return $dispatchRule->validate($caller); | ||
|
||
case DispatchRule::REQUIRE_TOKEN->value: | ||
$dispatchRule = new RequireTokenParams($value['collectionId'], $value['tokenId']); | ||
|
||
return $dispatchRule->validate($caller); | ||
|
||
case DispatchRule::WHITELISTED_PALLETS->value: | ||
$dispatchRule = new WhitelistedPalletsParams($value); | ||
|
||
return $dispatchRule->validate($this->data['dispatch']['call']); | ||
|
||
case DispatchRule::PERMITTED_CALLS->value: | ||
$dispatchRule = new PermittedCallsParams($value); | ||
|
||
// TODO: Not sure how to check the above yet | ||
return $dispatchRule->validate($caller); | ||
|
||
case DispatchRule::PERMITTED_EXTRINSICS->value: | ||
$dispatchRule = new PermittedExtrinsicsParams(array_map(function ($x) { | ||
$extrinsic = explode('.', $x); | ||
|
||
return [ | ||
$extrinsic[0] => [ | ||
$extrinsic[1] => null, | ||
], | ||
]; | ||
}, $value)); | ||
|
||
// TODO: Not sure how to check the above yet | ||
return $dispatchRule->validate($caller); | ||
|
||
case DispatchRule::WHITELISTED_COLLECTIONS->value: | ||
$dispatchRule = new WhitelistedCollectionsParams($value); | ||
|
||
// TODO: Not sure how to check the above yet | ||
return $dispatchRule->validate($caller); | ||
|
||
case DispatchRule::MAX_FUEL_BURN_PER_TRANSACTION->value: | ||
$dispatchRule = new MaxFuelBurnPerTransactionParams($value); | ||
|
||
// TODO: Not sure how to calculate the above yet | ||
return $dispatchRule->validate($caller); | ||
|
||
case DispatchRule::TANK_FUEL_BUDGET->value: | ||
$dispatchRule = new TankFuelBudgetParams($value['amount'], $value['resetPeriod']); | ||
|
||
// TODO: Not sure how to calculate the above yet | ||
return $dispatchRule->validate($caller); | ||
|
||
case DispatchRule::USER_FUEL_BUDGET->value: | ||
$dispatchRule = new UserFuelBudgetParams($value['amount'], $value['resetPeriod']); | ||
|
||
// TODO: Not sure how to calculate the above yet | ||
return $dispatchRule->validate($caller); | ||
|
||
default: | ||
return true; | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters