-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PLA-1729] Adds CollectionTransferred event (#158)
- Loading branch information
1 parent
7b6f858
commit 5bc6b4e
Showing
5 changed files
with
119 additions
and
6 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
31 changes: 31 additions & 0 deletions
31
src/Events/Substrate/MultiTokens/CollectionTransferred.php
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,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), | ||
]; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...Services/Processor/Substrate/Codec/Polkadart/Events/MultiTokens/CollectionTransferred.php
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,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], | ||
]; | ||
} | ||
} |
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
40 changes: 40 additions & 0 deletions
40
...Services/Processor/Substrate/Events/Implementations/MultiTokens/CollectionTransferred.php
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,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), | ||
); | ||
} | ||
} |