Skip to content

Commit

Permalink
[PLA-1461] Changes to AccountAdded event (#28)
Browse files Browse the repository at this point in the history
Signed-off-by: Leonardo Custodio <[email protected]>
Co-authored-by: Simon Evans <[email protected]>
  • Loading branch information
leonardocustodio and v16Studios authored Nov 29, 2023
1 parent 32c987f commit 2c0fa5f
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class () extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('fuel_tank_accounts', function (Blueprint $table) {
$table->string('total_received')->default('0')->after('user_deposit');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('fuel_tank_accounts', function (Blueprint $table) {
$table->dropColumn('total_received');
});
}
};
3 changes: 2 additions & 1 deletion src/FuelTanksServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public function configurePackage(Package $package): void
->hasMigrations(
'create_fuel_tanks_table',
'create_fuel_tank_accounts_table',
'create_fuel_tank_rules_table'
'create_fuel_tank_rules_table',
'add_total_received_to_accounts_table'
)
->hasTranslations();
}
Expand Down
10 changes: 9 additions & 1 deletion src/GraphQL/Mutations/ScheduleMutateFreezeStateMutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function resolve(
Closure $getSelectFields,
SerializationServiceInterface $serializationService
) {
$encodedData = $serializationService->encode($this->getMutationName(), static::getEncodableParams(...$args));
$encodedData = $serializationService->encode($this->getMethodName(), static::getEncodableParams(...$args));

return Transaction::lazyLoadSelectFields(
DB::transaction(fn () => $this->storeTransaction($args, $encodedData)),
Expand All @@ -111,6 +111,14 @@ public static function getEncodableParams(...$params): array
];
}

/**
* Get the serialization service method name.
*/
public function getMethodName(): string
{
return 'MutateFreezeState';
}

/**
* Get the mutation's request validation rules.
*/
Expand Down
3 changes: 3 additions & 0 deletions src/Models/Laravel/FuelTankAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ class FuelTankAccount extends BaseModel
protected $fillable = [
'fuel_tank_id',
'wallet_id',
'tank_deposit',
'user_deposit',
'total_received',
];

/**
Expand Down
1 change: 1 addition & 0 deletions src/Services/Processor/Substrate/Codec/Decoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public function fuelTankAccountStorageData(string $data): array
return [
'tankDeposit' => gmp_strval(Arr::get($decoded, 'tankDeposit')),
'userDeposit' => gmp_strval(Arr::get($decoded, 'userDeposit')),
'totalReceived' => gmp_strval(Arr::get($decoded, 'totalReceived')),
'ruleDataSets' => '', //TODO: Implement
];
}
Expand Down
2 changes: 1 addition & 1 deletion src/Services/Processor/Substrate/Codec/Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Encoder extends BaseEncoder
'RemoveRuleSet' => 'FuelTanks.remove_rule_set',
'RemoveAccountRuleData' => 'FuelTanks.remove_account_rule_data',
'MutateFuelTank' => 'FuelTanks.mutate_fuel_tank',
'ScheduleMutateFreezeState' => 'FuelTanks.schedule_mutate_freeze_state',
'MutateFreezeState' => 'FuelTanks.mutate_freeze_state',
'Dispatch' => 'FuelTanks.dispatch',
'DispatchAndTouch' => 'FuelTanks.dispatch_and_touch',
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public function run(PolkadartEvent $event, Block $block, Codec $codec): void
$fuelTankAccount = FuelTankAccount::create([
'fuel_tank_id' => $fuelTank->id,
'wallet_id' => $account->id,
'tank_deposit' => $event->tankDeposit,
'user_deposit' => $event->userDeposit,
'total_received' => $event->totalReceived,
]);

$extrinsic = $block->extrinsics[$event->extrinsicIndex];
Expand Down

This file was deleted.

1 change: 1 addition & 0 deletions src/Services/Processor/Substrate/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public function accountsStorages(array $data): void
'wallet_id' => $userWallet->id,
'tank_deposit' => $accountData['tankDeposit'],
'user_deposit' => $accountData['userDeposit'],
'total_received' => $accountData['totalReceived'],
'created_at' => $now = Carbon::now(),
'updated_at' => $now,
];
Expand Down
30 changes: 18 additions & 12 deletions tests/Feature/GraphQL/Mutations/ScheduleMutateFreezeStateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@

class ScheduleMutateFreezeStateTest extends TestCaseGraphQL
{
/**
* The graphql mutation.
*/
protected string $mutation = 'ScheduleMutateFreezeState';

/**
* The graphql method.
*/
protected string $method = 'ScheduleMutateFreezeState';
protected string $method = 'MutateFreezeState';

/**
* The fuel tank.
Expand All @@ -36,16 +41,17 @@ protected function setUp(): void
public function test_it_can_remove_rule_set(): void
{
$response = $this->graphql(
$this->method,
$params = $this->generateParams(),
$this->mutation,
$params = $this->generateParams()
);

$this->assertEquals(
$response['encodedData'],
TransactionSerializer::encode($this->method, ScheduleMutateFreezeStateMutation::getEncodableParams(...$params))
);

$params['tankId'] = SS58Address::encode($params['tankId']);
$response = $this->graphql($this->method, $params);
$response = $this->graphql($this->mutation, $params);
$this->assertEquals(
$response['encodedData'],
TransactionSerializer::encode($this->method, ScheduleMutateFreezeStateMutation::getEncodableParams(...$params))
Expand All @@ -57,7 +63,7 @@ public function test_it_will_fail_with_invalid_parameter_tank_id(): void
$pubicKey = resolve(SubstrateProvider::class)->public_key();
$data = $this->generateParams();
$response = $this->graphql(
$this->method,
$this->mutation,
array_merge($data, ['tankId' => $pubicKey]),
true
);
Expand All @@ -68,7 +74,7 @@ public function test_it_will_fail_with_invalid_parameter_tank_id(): void


$response = $this->graphql(
$this->method,
$this->mutation,
array_merge($data, ['tankId' => Str::random(300)]),
true
);
Expand All @@ -78,7 +84,7 @@ public function test_it_will_fail_with_invalid_parameter_tank_id(): void
);

$response = $this->graphql(
$this->method,
$this->mutation,
array_merge($data, ['tankId' => 'Invalid']),
true
);
Expand All @@ -99,7 +105,7 @@ public function test_it_will_fail_with_invalid_parameter_tank_id(): void
);
$tank->forceFill(['owner_wallet_id' => $wallet->id])->save();
$response = $this->graphql(
$this->method,
$this->mutation,
array_merge($data, ['tankId' => $tank->public_key]),
true
);
Expand All @@ -113,7 +119,7 @@ public function test_it_will_fail_with_invalid_parameter_rule_set_id(): void
{
$data = $this->generateParams();
$response = $this->graphql(
$this->method,
$this->mutation,
array_merge($data, ['ruleSetId' => 'Invalid']),
true
);
Expand All @@ -123,14 +129,14 @@ public function test_it_will_fail_with_invalid_parameter_rule_set_id(): void
);

$response = $this->graphql(
$this->method,
$this->mutation,
array_merge($data, ['ruleSetId' => null]),
true
);
$this->assertNotEmpty($response);

$response = $this->graphql(
$this->method,
$this->mutation,
array_merge($data, ['tankId' => $this->tank->public_key, 'ruleSetId' => Hex::MAX_UINT128]),
true
);
Expand All @@ -140,7 +146,7 @@ public function test_it_will_fail_with_invalid_parameter_rule_set_id(): void
);

$response = $this->graphql(
$this->method,
$this->mutation,
array_merge($data, ['ruleSetId' => fake()->numberBetween(5000, 10000)]),
true
);
Expand Down
8 changes: 4 additions & 4 deletions tests/Unit/EncodingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,12 +365,12 @@ public function test_it_can_encode_create_fuel_tank_with_tank_fuel_budget()

public function test_it_can_encode_schedule_mutate_freeze_state_without_rule_set_id()
{
$data = TransactionSerializer::encode('ScheduleMutateFreezeState', ScheduleMutateFreezeStateMutation::getEncodableParams(
$data = TransactionSerializer::encode('MutateFreezeState', ScheduleMutateFreezeStateMutation::getEncodableParams(
tankId: '0x18353dcf7a6eb053b6f0c01774d1f8cfe0c15963780f6935c49a9fd4f50b893c',
isFrozen: true,
));

$callIndex = $this->codec->encoder()->getCallIndex('FuelTanks.schedule_mutate_freeze_state', true);
$callIndex = $this->codec->encoder()->getCallIndex('FuelTanks.mutate_freeze_state', true);
$this->assertEquals(
"0x{$callIndex}0018353dcf7a6eb053b6f0c01774d1f8cfe0c15963780f6935c49a9fd4f50b893c0001",
$data
Expand All @@ -379,13 +379,13 @@ public function test_it_can_encode_schedule_mutate_freeze_state_without_rule_set

public function test_it_can_encode_schedule_mutate_freeze_state_with_rule_set_id()
{
$data = TransactionSerializer::encode('ScheduleMutateFreezeState', ScheduleMutateFreezeStateMutation::getEncodableParams(
$data = TransactionSerializer::encode('MutateFreezeState', ScheduleMutateFreezeStateMutation::getEncodableParams(
tankId: '0x18353dcf7a6eb053b6f0c01774d1f8cfe0c15963780f6935c49a9fd4f50b893c',
isFrozen: true,
ruleSetId: '255',
));

$callIndex = $this->codec->encoder()->getCallIndex('FuelTanks.schedule_mutate_freeze_state', true);
$callIndex = $this->codec->encoder()->getCallIndex('FuelTanks.mutate_freeze_state', true);
$this->assertEquals(
"0x{$callIndex}0018353dcf7a6eb053b6f0c01774d1f8cfe0c15963780f6935c49a9fd4f50b893c01ff00000001",
$data
Expand Down

0 comments on commit 2c0fa5f

Please sign in to comment.