From fd1c890588adea167ef62401e5e333856e44591f Mon Sep 17 00:00:00 2001 From: Leonardo Custodio Date: Tue, 15 Oct 2024 16:10:57 -0300 Subject: [PATCH] Add new event --- src/Enums/Substrate/MarketplaceEventType.php | 3 + .../Marketplace/ListingCancelled.php | 4 +- .../ListingRemovedUnderMinimum.php | 32 ++++++++ .../ListingRemovedUnderMinimum.php | 74 +++++++++++++++++++ 4 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 src/Events/Substrate/Marketplace/ListingRemovedUnderMinimum.php create mode 100644 src/Services/Processor/Substrate/Events/Implementations/Marketplace/ListingRemovedUnderMinimum.php diff --git a/src/Enums/Substrate/MarketplaceEventType.php b/src/Enums/Substrate/MarketplaceEventType.php index 2cceb6c..e756c6b 100644 --- a/src/Enums/Substrate/MarketplaceEventType.php +++ b/src/Enums/Substrate/MarketplaceEventType.php @@ -7,6 +7,7 @@ use Enjin\Platform\Marketplace\Services\Processor\Substrate\Events\Implementations\Marketplace\ListingCancelled; use Enjin\Platform\Marketplace\Services\Processor\Substrate\Events\Implementations\Marketplace\ListingCreated; use Enjin\Platform\Marketplace\Services\Processor\Substrate\Events\Implementations\Marketplace\ListingFilled; +use Enjin\Platform\Marketplace\Services\Processor\Substrate\Events\Implementations\Marketplace\ListingRemovedUnderMinimum; use Enjin\Platform\Services\Processor\Substrate\Events\SubstrateEvent; use Enjin\Platform\Traits\EnumExtensions; @@ -19,6 +20,7 @@ enum MarketplaceEventType: string case LISTING_CANCELLED = 'ListingCancelled'; case LISTING_CREATED = 'ListingCreated'; case LISTING_FILLED = 'ListingFilled'; + case LISTING_REMOVED_UNDER_MINIMUM = 'ListingRemovedUnderMinimum'; /** * Get the processor for the event. @@ -31,6 +33,7 @@ public function getProcessor($event, $block, $codec): SubstrateEvent self::LISTING_CANCELLED => new ListingCancelled($event, $block, $codec), self::LISTING_CREATED => new ListingCreated($event, $block, $codec), self::LISTING_FILLED => new ListingFilled($event, $block, $codec), + self::LISTING_REMOVED_UNDER_MINIMUM => new ListingRemovedUnderMinimum($event, $block, $codec), }; } } diff --git a/src/Events/Substrate/Marketplace/ListingCancelled.php b/src/Events/Substrate/Marketplace/ListingCancelled.php index 8805cec..9300cf0 100644 --- a/src/Events/Substrate/Marketplace/ListingCancelled.php +++ b/src/Events/Substrate/Marketplace/ListingCancelled.php @@ -23,8 +23,8 @@ public function __construct(ListingCancelledPolkadart $event, ?Model $transactio $this->broadcastChannels = [ new Channel("listing;{$event->listingId}"), - new Channel("collection;{$extra['collectionId']}"), - new Channel("token;{$extra['collection_id']}-{$extra['tokenId']}"), + new Channel("collection;{$extra['collection_id']}"), + new Channel("token;{$extra['collection_id']}-{$extra['token_id']}"), new Channel($extra['seller']), new PlatformAppChannel(), ]; diff --git a/src/Events/Substrate/Marketplace/ListingRemovedUnderMinimum.php b/src/Events/Substrate/Marketplace/ListingRemovedUnderMinimum.php new file mode 100644 index 0000000..7bc27ef --- /dev/null +++ b/src/Events/Substrate/Marketplace/ListingRemovedUnderMinimum.php @@ -0,0 +1,32 @@ +broadcastData = $event->toBroadcast([ + 'idempotencyKey' => $transaction?->idempotency_key, + ]); + + $this->broadcastChannels = [ + new Channel("listing;{$event->listingId}"), + new Channel("collection;{$extra['collection_id']}"), + new Channel("token;{$extra['collection_id']}-{$extra['token_id']}"), + new Channel($extra['seller']), + new PlatformAppChannel(), + ]; + } +} diff --git a/src/Services/Processor/Substrate/Events/Implementations/Marketplace/ListingRemovedUnderMinimum.php b/src/Services/Processor/Substrate/Events/Implementations/Marketplace/ListingRemovedUnderMinimum.php new file mode 100644 index 0000000..f6a701f --- /dev/null +++ b/src/Services/Processor/Substrate/Events/Implementations/Marketplace/ListingRemovedUnderMinimum.php @@ -0,0 +1,74 @@ +getListing($this->event->listingId); + $seller = Wallet::find($listing->seller_wallet_id); + + MarketplaceState::create([ + 'marketplace_listing_id' => $listing->id, + 'state' => ListingState::CANCELLED->name, + 'height' => $this->block->number, + 'created_at' => $now = Carbon::now(), + 'updated_at' => $now, + ]); + + $this->extra = [ + 'collection_id' => $listing->make_collection_chain_id, + 'token_id' => $listing->make_token_chain_id, + 'seller' => $seller->public_key, + ]; + } catch (\Throwable) { + Log::error( + sprintf( + 'Listing %s was removed but could not be found in the database.', + $this->event->listingId, + ) + ); + } + } + + #[\Override] + public function log(): void + { + Log::debug( + sprintf( + 'Listing %s was removed because of royalties change.', + $this->event->listingId, + ) + ); + } + + #[\Override] + public function broadcast(): void + { + ListingRemovedUnderMinimumEvent::safeBroadcast( + $this->event, + $this->getTransaction($this->block, $this->event->extrinsicIndex), + $this->extra, + ); + } +}