Skip to content

Commit

Permalink
[PLA-2059] Add RequireAccount to InsertRuleSet (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardocustodio authored Oct 28, 2024
1 parent 34bd702 commit 3732797
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/GraphQL/Mutations/InsertRuleSetMutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ public function args(): array
'type' => GraphQL::type('DispatchRuleInputType!'),
'description' => __('enjin-platform-fuel-tanks::input_type.dispatch_rule.description'),
],
'requireAccount' => [
'type' => GraphQL::type('Boolean'),
'description' => __('enjin-platform-fuel-tanks::mutation.insert_rule_set.args.requireAccount'),
'defaultValue' => false,
],
...$this->getSigningAccountField(),
...$this->getIdempotencyField(),
...$this->getSimulateField(),
Expand All @@ -102,7 +107,8 @@ public function resolve(
static::getEncodableParams(
tankId: $args['tankId'],
ruleSetId: $args['ruleSetId'],
dispatchRules: $dispatchRules
dispatchRules: $dispatchRules,
requireAccount: $args['requireAccount'],
)
);

Expand All @@ -122,6 +128,7 @@ public static function getEncodableParams(...$params): array
$tankId = Arr::get($params, 'tankId', Account::daemonPublicKey());
$ruleSetId = Arr::get($params, 'ruleSetId', 0);
$rules = Arr::get($params, 'dispatchRules', new DispatchRulesParams())->toEncodable();
$requireAccount = Arr::get($params, 'requireAccount', false);

return [
'tankId' => [
Expand All @@ -130,7 +137,7 @@ public static function getEncodableParams(...$params): array
'ruleSetId' => $ruleSetId,
'ruleSet' => [
'rules' => $rules,
'requireAccount' => false,
'requireAccount' => $requireAccount,
],
];
}
Expand Down
22 changes: 22 additions & 0 deletions tests/Feature/GraphQL/Mutations/InsertRuleSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,28 @@ public function test_it_can_insert_rule_set(): void
);
}

public function test_it_can_insert_rule_set_with_require_account(): void
{
$params = $this->generateRuleSet();
$params['requireAccount'] = true;

$response = $this->graphql(
$this->method,
$params
);

$params['dispatchRules'] = resolve(Substrate::class)->getDispatchRulesParams($params['dispatchRules']);

$encodedData = TransactionSerializer::encode($this->method, InsertRuleSetMutation::getEncodableParams(...$params));
$encodedData = Str::take($encodedData, Str::length($encodedData) - 4);
$encodedData .= Arr::get($params['dispatchRules']->permittedExtrinsics->toEncodable(), 'PermittedExtrinsics.extrinsics');

$this->assertEquals(
$response['encodedData'],
$encodedData
);
}

public function test_it_can_skip_validation(): void
{
$response = $this->graphql(
Expand Down
2 changes: 2 additions & 0 deletions tests/Feature/GraphQL/Resources/InsertRuleSet.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ mutation InsertRuleSet(
$tankId: String!
$ruleSetId: BigInt!
$dispatchRules: DispatchRuleInputType!
$requireAccount: Boolean
$skipValidation: Boolean
) {
InsertRuleSet(
tankId: $tankId
ruleSetId: $ruleSetId
dispatchRules: $dispatchRules
requireAccount: $requireAccount
skipValidation: $skipValidation
) {
id
Expand Down
23 changes: 23 additions & 0 deletions tests/Unit/EncodingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,29 @@ public function test_it_can_encode_insert_or_update_rule_set()
);
}

public function test_it_can_encode_insert_rule_set_with_require_account()
{
$dispatchRules = new DispatchRulesParams(
userFuelBudget: new UserFuelBudgetParams(
amount: '1000000000',
resetPeriod: '500000',
),
);

$data = $this->substrate->encode('InsertRuleSet', InsertRuleSetMutation::getEncodableParams(
tankId: '0x18353dcf7a6eb053b6f0c01774d1f8cfe0c15963780f6935c49a9fd4f50b893c',
ruleSetId: '1',
dispatchRules: $dispatchRules,
requireAccount: true,
));

$callIndex = $this->codec->encoder()->getCallIndex('FuelTanks.insert_rule_set', true);
$this->assertEquals(
"0x{$callIndex}0018353dcf7a6eb053b6f0c01774d1f8cfe0c15963780f6935c49a9fd4f50b893c01000000040302286bee20a1070001",
$data
);
}

public function test_it_can_encode_insert_or_update_rule_with_permitted_extrinsics()
{
$dispatchRules = new DispatchRulesParams(
Expand Down

0 comments on commit 3732797

Please sign in to comment.