Skip to content

Commit

Permalink
Add new event
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardocustodio committed Oct 15, 2024
1 parent 6b70a7b commit fd1c890
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/Enums/Substrate/MarketplaceEventType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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.
Expand All @@ -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),
};
}
}
4 changes: 2 additions & 2 deletions src/Events/Substrate/Marketplace/ListingCancelled.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
];
Expand Down
32 changes: 32 additions & 0 deletions src/Events/Substrate/Marketplace/ListingRemovedUnderMinimum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Enjin\Platform\Marketplace\Events\Substrate\Marketplace;

use Enjin\Platform\Channels\PlatformAppChannel;
use Enjin\Platform\Events\PlatformBroadcastEvent;
use Illuminate\Broadcasting\Channel;
use Illuminate\Database\Eloquent\Model;
use Enjin\Platform\Services\Processor\Substrate\Codec\Polkadart\Events\Marketplace\ListingRemovedUnderMinimum as ListingRemovedUnderMinimumPolkadart;

class ListingRemovedUnderMinimum extends PlatformBroadcastEvent
{
/**
* Create a new event instance.
*/
public function __construct(ListingRemovedUnderMinimumPolkadart $event, ?Model $transaction = null, ?array $extra = null)
{
parent::__construct();

$this->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(),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

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

use Carbon\Carbon;
use Enjin\Platform\Marketplace\Enums\ListingState;
use Enjin\Platform\Marketplace\Events\Substrate\Marketplace\ListingRemovedUnderMinimum as ListingRemovedUnderMinimumEvent;
use Enjin\Platform\Marketplace\Models\Laravel\Wallet;
use Enjin\Platform\Marketplace\Models\MarketplaceState;
use Enjin\Platform\Marketplace\Services\Processor\Substrate\Events\Implementations\MarketplaceSubstrateEvent;
use Enjin\Platform\Services\Processor\Substrate\Codec\Polkadart\Events\Marketplace\ListingRemovedUnderMinimum as ListingRemovedUnderMinimumPolkadart;
use Enjin\Platform\Services\Processor\Substrate\Codec\Polkadart\Events\Event;
use Illuminate\Support\Facades\Log;

class ListingRemovedUnderMinimum extends MarketplaceSubstrateEvent
{
/** @var ListingRemovedUnderMinimumPolkadart */
protected Event $event;

/**
* Handles the listing cancelled event.
*/
#[\Override]
public function run(): void
{
try {
// Fails if the listing is not found
$listing = $this->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,
);
}
}

0 comments on commit fd1c890

Please sign in to comment.