Skip to content

Commit

Permalink
[PLA-1729] Adds CollectionTransferred event (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardocustodio authored Apr 17, 2024
1 parent 7b6f858 commit 5bc6b4e
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 6 deletions.
7 changes: 5 additions & 2 deletions src/Enums/Substrate/MultiTokensEventType.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Enjin\Platform\Services\Processor\Substrate\Events\Implementations\MultiTokens\CollectionCreated;
use Enjin\Platform\Services\Processor\Substrate\Events\Implementations\MultiTokens\CollectionDestroyed;
use Enjin\Platform\Services\Processor\Substrate\Events\Implementations\MultiTokens\CollectionMutated;
use Enjin\Platform\Services\Processor\Substrate\Events\Implementations\MultiTokens\CollectionTransferred;
use Enjin\Platform\Services\Processor\Substrate\Events\Implementations\MultiTokens\Freeze;
use Enjin\Platform\Services\Processor\Substrate\Events\Implementations\MultiTokens\Minted;
use Enjin\Platform\Services\Processor\Substrate\Events\Implementations\MultiTokens\Reserved;
Expand All @@ -32,6 +33,8 @@ enum MultiTokensEventType: string

case COLLECTION_CREATED = 'CollectionCreated';
case COLLECTION_DESTROYED = 'CollectionDestroyed';
case COLLECTION_MUTATED = 'CollectionMutated';
case COLLECTION_TRANSFERRED = 'CollectionTransferred';
case COLLECTION_ACCOUNT_CREATED = 'CollectionAccountCreated';
case COLLECTION_ACCOUNT_DESTROYED = 'CollectionAccountDestroyed';
case TOKEN_CREATED = 'TokenCreated';
Expand All @@ -48,7 +51,6 @@ enum MultiTokensEventType: string
case APPROVED = 'Approved';
case UNAPPROVED = 'Unapproved';
case TOKEN_MUTATED = 'TokenMutated';
case COLLECTION_MUTATED = 'CollectionMutated';

case RESERVED = 'Reserved';

Expand All @@ -62,6 +64,8 @@ public function getProcessor(): SubstrateEvent
return match ($this) {
self::COLLECTION_CREATED => new CollectionCreated(),
self::COLLECTION_DESTROYED => new CollectionDestroyed(),
self::COLLECTION_MUTATED => new CollectionMutated(),
self::COLLECTION_TRANSFERRED => new CollectionTransferred(),
self::COLLECTION_ACCOUNT_CREATED => new CollectionAccountCreated(),
self::COLLECTION_ACCOUNT_DESTROYED => new CollectionAccountDestroyed(),
self::TOKEN_CREATED => new TokenCreated(),
Expand All @@ -78,7 +82,6 @@ public function getProcessor(): SubstrateEvent
self::APPROVED => new Approved(),
self::UNAPPROVED => new Unapproved(),
self::TOKEN_MUTATED => new TokenMutated(),
self::COLLECTION_MUTATED => new CollectionMutated(),
self::RESERVED => new Reserved(),
self::UNRESERVED => new Unreserved(),
};
Expand Down
31 changes: 31 additions & 0 deletions src/Events/Substrate/MultiTokens/CollectionTransferred.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Enjin\Platform\Events\Substrate\MultiTokens;

use Enjin\Platform\Channels\PlatformAppChannel;
use Enjin\Platform\Events\PlatformBroadcastEvent;
use Illuminate\Broadcasting\Channel;
use Illuminate\Database\Eloquent\Model;

class CollectionTransferred extends PlatformBroadcastEvent
{
/**
* Create a new event instance.
*/
public function __construct(Model $collection, string $owner, ?Model $transaction = null)
{
parent::__construct();

$this->broadcastData = [
'idempotencyKey' => $transaction?->idempotency_key,
'collectionId' => $collection->collection_chain_id,
'owner' => $owner,
];

$this->broadcastChannels = [
new Channel("collection;{$this->broadcastData['collectionId']}"),
new PlatformAppChannel(),
new Channel($collection->owner->address),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Enjin\Platform\Services\Processor\Substrate\Codec\Polkadart\Events\MultiTokens;

use Enjin\Platform\Services\Processor\Substrate\Codec\Polkadart\Events\Event;
use Enjin\Platform\Services\Processor\Substrate\Codec\Polkadart\PolkadartEvent;
use Enjin\Platform\Support\Account;
use Illuminate\Support\Arr;

class CollectionTransferred extends Event implements PolkadartEvent
{
public readonly ?string $extrinsicIndex;
public readonly string $module;
public readonly string $name;
public readonly string $collectionId;
public readonly string $owner;

public static function fromChain(array $data): self
{
$self = new self();

$self->extrinsicIndex = Arr::get($data, 'phase.ApplyExtrinsic');
$self->module = array_key_first(Arr::get($data, 'event'));
$self->name = array_key_first(Arr::get($data, 'event.' . $self->module));
$self->collectionId = $self->getValue($data, ['collection_id', 'T::CollectionId']);
$self->owner = Account::parseAccount($self->getValue($data, ['new_owner', 'T::AccountId']));

return $self;
}

public function getPallet(): string
{
return $this->module;
}

public function getParams(): array
{
return [
['type' => 'collection_id', 'value' => $this->collectionId],
['type' => 'owner', 'value' => $this->owner],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ public function run(Event $event, Block $block, Codec $codec): void
$attributes = [];
$royalties = [];

if ($owner = $event->owner) {
$attributes['owner_wallet_id'] = $this->firstOrStoreAccount($owner)->id;
}

if ($event->royalty === 'SomeMutation') {
if ($beneficiary = $event->beneficiary) {
$attributes['royalty_wallet_id'] = $this->firstOrStoreAccount($beneficiary)->id;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Enjin\Platform\Services\Processor\Substrate\Events\Implementations\MultiTokens;

use Enjin\Platform\Events\Substrate\MultiTokens\CollectionTransferred as CollectionTransferredEvent;
use Enjin\Platform\Models\Laravel\Block;
use Enjin\Platform\Services\Processor\Substrate\Codec\Codec;
use Enjin\Platform\Services\Processor\Substrate\Codec\Polkadart\Events\MultiTokens\CollectionTransferred as CollectionTransferredPolkadart;
use Enjin\Platform\Services\Processor\Substrate\Codec\Polkadart\Events\Event;
use Enjin\Platform\Services\Processor\Substrate\Events\SubstrateEvent;
use Illuminate\Support\Facades\Log;

class CollectionTransferred extends SubstrateEvent
{
public function run(Event $event, Block $block, Codec $codec): void
{
if (!$event instanceof CollectionTransferredPolkadart) {
return;
}

if (!$this->shouldIndexCollection($event->collectionId)) {
return;
}

// Fails if collection is not found
$collection = $this->getCollection($event->collectionId);
$owner = $this->firstOrStoreAccount($event->owner);

$collection->owner_wallet_id = $owner->id;
$collection->save();

Log::info("Collection #{$event->collectionId} (id {$collection->id}) owner changed to {$owner->public_key} (id {$owner->id}).");

CollectionTransferredEvent::safeBroadcast(
$collection,
$owner->public_key,
$this->getTransaction($block, $event->extrinsicIndex),
);
}
}

0 comments on commit 5bc6b4e

Please sign in to comment.