Skip to content

Commit 0d481ef

Browse files
committed
Improve message bus
1 parent febd69d commit 0d481ef

13 files changed

+159
-24
lines changed

.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,6 @@ PERCENT_FEES=2.0
3535
###> project/auctionx ###
3636
IMX_API_ENV=dev
3737
IMX_ESCROW_WALLET=
38+
IMX_FEES_WALLET=
3839
BUS_API_KEY=ChangeMe
3940
###< project/auctionx ###

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ RUN set -eux; \
3838
gmp \
3939
xml \
4040
pgsql \
41+
bcmath \
4142
; \
4243
pecl install \
4344
apcu-${APCU_VERSION} \

config/services.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ services:
3232
$auctionRepository: '@App\Repository\AuctionRepository'
3333
$messageService: '@App\Service\MessageService'
3434
$percentFees: '%env(PERCENT_FEES)%'
35+
$feesWallet: '%env(IMX_FEES_WALLET)%'
3536

3637
App\Controller\Bus\MessageController:
3738
arguments:

migrations/Version20220718131849.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DoctrineMigrations;
6+
7+
use Doctrine\DBAL\Schema\Schema;
8+
use Doctrine\Migrations\AbstractMigration;
9+
10+
/**
11+
* Auto-generated Migration: Please modify to your needs!
12+
*/
13+
final class Version20220718131849 extends AbstractMigration
14+
{
15+
public function getDescription(): string
16+
{
17+
return '';
18+
}
19+
20+
public function up(Schema $schema): void
21+
{
22+
// this up() migration is auto-generated, please modify it to your needs
23+
$this->addSql('ALTER TABLE message ADD auction_id INT DEFAULT NULL');
24+
$this->addSql('ALTER TABLE message ADD bid_id INT DEFAULT NULL');
25+
$this->addSql('ALTER TABLE message ADD CONSTRAINT FK_B6BD307F57B8F0DE FOREIGN KEY (auction_id) REFERENCES auction (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
26+
$this->addSql('ALTER TABLE message ADD CONSTRAINT FK_B6BD307F4D9866B8 FOREIGN KEY (bid_id) REFERENCES bid (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
27+
$this->addSql('CREATE UNIQUE INDEX UNIQ_B6BD307F57B8F0DE ON message (auction_id)');
28+
$this->addSql('CREATE UNIQUE INDEX UNIQ_B6BD307F4D9866B8 ON message (bid_id)');
29+
}
30+
31+
public function down(Schema $schema): void
32+
{
33+
// this down() migration is auto-generated, please modify it to your needs
34+
$this->addSql('CREATE SCHEMA public');
35+
$this->addSql('ALTER TABLE message DROP CONSTRAINT FK_B6BD307F57B8F0DE');
36+
$this->addSql('ALTER TABLE message DROP CONSTRAINT FK_B6BD307F4D9866B8');
37+
$this->addSql('DROP INDEX UNIQ_B6BD307F57B8F0DE');
38+
$this->addSql('DROP INDEX UNIQ_B6BD307F4D9866B8');
39+
$this->addSql('ALTER TABLE message DROP auction_id');
40+
$this->addSql('ALTER TABLE message DROP bid_id');
41+
}
42+
}

src/Command/AuctionUpdateStatusCommand.php

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use App\Entity\Asset;
88
use App\Entity\Auction;
99
use App\Entity\Bid;
10+
use App\Entity\Message;
1011
use App\Repository\AuctionRepository;
1112
use App\Service\MessageService;
1213
use Symfony\Component\Console\Attribute\AsCommand;
@@ -23,7 +24,8 @@ class AuctionUpdateStatusCommand extends Command
2324
public function __construct(
2425
private readonly AuctionRepository $auctionRepository,
2526
private readonly MessageService $messageService,
26-
private readonly float $percentFees
27+
private readonly float $percentFees,
28+
private readonly string $feesWallet
2729
) {
2830
parent::__construct();
2931
}
@@ -43,30 +45,48 @@ protected function execute(InputInterface $input, OutputInterface $output): int
4345
if ($lastBid instanceof Bid) {
4446
// transfer NFT to higher bidder
4547
$this->messageService->transferNFT(
48+
Message::TASK_TRANSFER_NFT,
4649
$auction->getAsset()->getInternalId(),
4750
$auction->getAsset()->getTokenId(),
4851
$auction->getAsset()->getTokenAddress(),
49-
$lastBid->getOwner()
52+
$lastBid->getOwner(),
53+
$auction
5054
);
5155

52-
$quantity = bcmul($lastBid->getQuantity(), (string)(1 - ($this->percentFees / 100)));
56+
$quantityToPay = bcmul($lastBid->getQuantity(), (string)(1 - ($this->percentFees / 100)));
5357

5458
// transfer token to seller
5559
$this->messageService->transferToken(
60+
Message::TASK_PAYMENT,
5661
$auction->getTokenType(),
57-
$quantity,
62+
$quantityToPay,
5863
$lastBid->getDecimals(),
59-
$auction->getOwner()
64+
$auction->getOwner(),
65+
$lastBid
66+
);
67+
68+
$quantityFees = bcsub($lastBid->getQuantity(), $quantityToPay);
69+
70+
// transfer fees to wallet
71+
$this->messageService->transferToken(
72+
Message::TASK_PAYMENT_FEES,
73+
$auction->getTokenType(),
74+
$quantityFees,
75+
$lastBid->getDecimals(),
76+
$this->feesWallet,
77+
$lastBid
6078
);
6179

6280
$auction->setStatus(Auction::STATUS_FILLED);
6381
} else {
6482
// even, return NFT to the seller
6583
$this->messageService->transferNFT(
84+
Message::TASK_REFUND_NFT,
6685
$auction->getAsset()->getInternalId(),
6786
$auction->getAsset()->getTokenId(),
6887
$auction->getAsset()->getTokenAddress(),
69-
$auction->getOwner()
88+
$auction->getOwner(),
89+
$auction
7090
);
7191

7292
$auction->setStatus(Auction::STATUS_EXPIRED);

src/Controller/Api/AuctionController.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use App\Entity\Asset;
88
use App\Entity\Auction;
9+
use App\Entity\Message;
910
use App\Form\AddAuctionType;
1011
use App\Form\CancelAuctionType;
1112
use App\Form\Filters\FilterAuctionsType;
@@ -327,10 +328,12 @@ public function cancel(
327328
// add to queue
328329
if ($auction->getAsset() instanceof Asset) {
329330
$messageService->transferNFT(
331+
Message::TASK_REFUND_NFT,
330332
$auction->getAsset()->getInternalId(),
331333
$auction->getAsset()->getTokenId(),
332334
$auction->getAsset()->getTokenAddress(),
333-
$auction->getOwner()
335+
$auction->getOwner(),
336+
$auction
334337
);
335338
}
336339

src/Controller/Api/BidController.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use App\Entity\Auction;
88
use App\Entity\Bid;
9+
use App\Entity\Message;
910
use App\Form\AddBidType;
1011
use App\Form\CancelBidType;
1112
use App\Form\Filters\FilterAuctionsType;
@@ -355,10 +356,12 @@ public function cancel(
355356
// add to queue
356357
if ($bid->getAuction() instanceof Auction) {
357358
$messageService->transferToken(
359+
Message::TASK_REFUND_BID,
358360
$bid->getAuction()->getTokenType(),
359361
$bid->getQuantity(),
360362
$bid->getDecimals(),
361-
$bid->getOwner()
363+
$bid->getOwner(),
364+
$bid
362365
);
363366
}
364367

src/Entity/Auction.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#[OA\Schema(description: 'Auction linked to an asset', required: [
1818
'type', 'status', 'transferId', 'quantity', 'decimals', 'tokenType', 'endAt'
1919
])]
20-
class Auction
20+
class Auction implements MessageableInterface
2121
{
2222
public const STATUS_ACTIVE = 'active';
2323
public const STATUS_FILLED = 'filled';

src/Entity/Bid.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#[OA\Schema(description: 'Bids linked to an asset', required: [
1515
'transferId', 'quantity', 'decimals', 'tokenType'
1616
])]
17-
class Bid
17+
class Bid implements MessageableInterface
1818
{
1919
public const STATUS_ACTIVE = 'active';
2020
public const STATUS_OVERPAID = 'overpaid';

src/Entity/Message.php

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,18 @@ class Message
2020
self::STATUS_ERROR,
2121
];
2222

23-
public const TASK_TRANSFER_TOKEN = 'task-transfer-token';
23+
public const TASK_REFUND_BID = 'task-refund-bid';
24+
public const TASK_REFUND_NFT = 'task-refund-nft';
2425
public const TASK_TRANSFER_NFT = 'task-transfer-nft';
26+
public const TASK_PAYMENT = 'task-payment';
27+
public const TASK_PAYMENT_FEES = 'task-payment-fees';
2528

2629
public const TASKS = [
27-
self::TASK_TRANSFER_TOKEN,
30+
self::TASK_REFUND_BID,
31+
self::TASK_REFUND_NFT,
2832
self::TASK_TRANSFER_NFT,
33+
self::TASK_PAYMENT,
34+
self::TASK_PAYMENT_FEES,
2935
];
3036

3137
#[ORM\Id]
@@ -54,6 +60,12 @@ class Message
5460
#[ORM\Column(type: 'datetime_immutable', nullable: true)]
5561
private ?\DateTimeImmutable $processedAt;
5662

63+
#[ORM\OneToOne(cascade: ['persist', 'remove'])]
64+
private ?Auction $auction = null;
65+
66+
#[ORM\OneToOne(cascade: ['persist', 'remove'])]
67+
private ?Bid $bid = null;
68+
5769
public function getId(): ?int
5870
{
5971
return $this->id;
@@ -146,4 +158,28 @@ public function setTask(string $task): self
146158

147159
return $this;
148160
}
161+
162+
public function getAuction(): ?Auction
163+
{
164+
return $this->auction;
165+
}
166+
167+
public function setAuction(?Auction $auction): self
168+
{
169+
$this->auction = $auction;
170+
171+
return $this;
172+
}
173+
174+
public function getBid(): ?Bid
175+
{
176+
return $this->bid;
177+
}
178+
179+
public function setBid(?Bid $bid): self
180+
{
181+
$this->bid = $bid;
182+
183+
return $this;
184+
}
149185
}

src/Entity/MessageableInterface.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Entity;
6+
7+
interface MessageableInterface
8+
{
9+
}

src/Service/ImmutableService.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use App\Entity\Asset;
99
use App\Entity\Auction;
1010
use App\Entity\Bid;
11+
use App\Entity\Message;
1112
use App\Helper\TokenHelper;
1213
use App\Repository\AssetRepository;
1314
use App\Repository\BidRepository;
@@ -92,10 +93,12 @@ public function checkBidDeposit(Bid $bid, Auction $auction): void
9293
if ($token !== $auction->getTokenType()) {
9394
// we refund bid
9495
$this->messageService->transferToken(
96+
Message::TASK_REFUND_BID,
9597
$token,
9698
$transfer['token']['data']['quantity'],
9799
$transfer['token']['data']['decimals'],
98-
$transfer['user']
100+
$transfer['user'],
101+
$bid
99102
);
100103
throw new BadBidException(
101104
sprintf('Invalid bid currency: %s sent, %s excepted. Refund in progress.', $token, $auction->getTokenType())
@@ -106,10 +109,12 @@ public function checkBidDeposit(Bid $bid, Auction $auction): void
106109
if ($transfer['token']['data']['quantity'] < $auction->getQuantity()) {
107110
// we refund bid
108111
$this->messageService->transferToken(
112+
Message::TASK_REFUND_BID,
109113
$token,
110114
$transfer['token']['data']['quantity'],
111115
$transfer['token']['data']['decimals'],
112-
$transfer['user']
116+
$transfer['user'],
117+
$bid
113118
);
114119
throw new BadBidException('Bid should be superior to auction price. Refund in progress.');
115120
}
@@ -122,10 +127,12 @@ public function checkBidDeposit(Bid $bid, Auction $auction): void
122127
if ($bid->getQuantity() <= $previousBid->getQuantity()) {
123128
// we have to refund bid
124129
$this->messageService->transferToken(
130+
Message::TASK_REFUND_BID,
125131
$auction->getTokenType(),
126132
$bid->getQuantity(),
127133
$bid->getDecimals(),
128-
$bid->getOwner()
134+
$bid->getOwner(),
135+
$bid
129136
);
130137

131138
throw new BadBidException('Bid should be superior to previous bid. Refund in progress.');
@@ -136,10 +143,12 @@ public function checkBidDeposit(Bid $bid, Auction $auction): void
136143

137144
// we refund previous bid
138145
$this->messageService->transferToken(
146+
Message::TASK_REFUND_BID,
139147
$auction->getTokenType(),
140148
$previousBid->getQuantity(),
141149
$previousBid->getDecimals(),
142-
$previousBid->getOwner()
150+
$previousBid->getOwner(),
151+
$previousBid
143152
);
144153
}
145154
}

0 commit comments

Comments
 (0)