Skip to content

Commit

Permalink
Batch transaction by collection
Browse files Browse the repository at this point in the history
  • Loading branch information
enjinabner committed Jun 27, 2023
1 parent 928ea1c commit e6b05b2
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 3 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('beam_batches', function (Blueprint $table) {
$table->string('collection_chain_id')->index()->nullable()->after('transaction_id');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('beam_batches', function (Blueprint $table) {
$table->dropColumn('collection_chain_id');
});
}
};
1 change: 1 addition & 0 deletions src/BeamServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function configurePackage(Package $package): void
->hasMigration('create_beam_claims_table')
->hasMigration('create_beam_scans_table')
->hasMigration('update_beams_table')
->hasMigration('add_collection_chain_id_to_beam_batches_table')
->hasRoute('enjin-platform-beam')
->hasTranslations();
}
Expand Down
2 changes: 2 additions & 0 deletions src/Commands/BatchProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Enjin\Platform\Beam\Enums\BeamType;
use Enjin\Platform\Beam\Enums\ClaimStatus;
use Enjin\Platform\Beam\Events\BeamBatchTransactionCreated;
use Enjin\Platform\Beam\Events\BeamClaimInProgress;
use Enjin\Platform\Beam\Models\BeamBatch;
use Enjin\Platform\Beam\Models\BeamClaim;
Expand Down Expand Up @@ -217,6 +218,7 @@ protected function processBatch(BeamType $type): int
'idempotency_key' => Str::uuid()->toString(),
], $this->account);
BeamBatch::where('id', $batchId)->update(['transaction_id' => $transaction->id]);
BeamBatchTransactionCreated::safeBroadcast($param['collectionId'], $transaction->id);
}

$this->updateStatus($claims, $reassignedClaims);
Expand Down
28 changes: 28 additions & 0 deletions src/Events/BeamBatchTransactionCreated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Enjin\Platform\Beam\Events;

use Enjin\Platform\Beam\Channels\PlatformBeamChannel;
use Enjin\Platform\Channels\PlatformAppChannel;
use Enjin\Platform\Events\PlatformBroadcastEvent;

class BeamBatchTransactionCreated extends PlatformBroadcastEvent
{
/**
* Creates a new event instance.
*/
public function __construct($collectionId, $transactionId)
{
parent::__construct();

$this->broadcastData = [
'collectionId' => $collectionId,
'transactionId' => $transactionId,
];

$this->broadcastChannels = [
new PlatformAppChannel(),
new PlatformBeamChannel(),
];
}
}
5 changes: 4 additions & 1 deletion src/Jobs/ClaimBeam.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ protected function buildRequiredClaimAttributes(BatchService $batchService, Mode
{
return [
...Arr::only($this->data, ['wallet_public_key', 'claimed_at', 'state', 'ip_address']),
'beam_batch_id' => $batchService->getNextBatchId(BeamType::getEnumCase($claim->type)),
'beam_batch_id' => $batchService->getNextBatchId(
BeamType::getEnumCase($claim->type),
$claim->beam->collection_chain_id
),
];
}
}
8 changes: 6 additions & 2 deletions src/Services/BatchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,13 @@ public function __construct()
/**
* Get next batch ID.
*/
public function getNextBatchId(BeamType $type): int
public function getNextBatchId(BeamType $type, string $collectionid): int
{
$batch = BeamBatch::firstOrNew(['completed_at' => null, 'beam_type' => $type->name]);
$batch = BeamBatch::firstOrNew([
'completed_at' => null,
'beam_type' => $type->name,
'collection_chain_id' => $collectionid,
]);
if (!$batch->exists) {
$batch->save();
}
Expand Down

0 comments on commit e6b05b2

Please sign in to comment.