Skip to content

Commit

Permalink
Simplify lsp7 orders based on order id
Browse files Browse the repository at this point in the history
  • Loading branch information
lykhonis committed Feb 13, 2024
1 parent ad9a56a commit 0c5fd64
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 33 deletions.
17 changes: 6 additions & 11 deletions artifacts/abi/marketplace/lsp7/LSP7Orders.json
Original file line number Diff line number Diff line change
Expand Up @@ -333,9 +333,9 @@
{
"inputs": [
{
"internalType": "address",
"name": "asset",
"type": "address"
"internalType": "uint256",
"name": "id",
"type": "uint256"
}
],
"name": "cancel",
Expand All @@ -346,20 +346,15 @@
{
"inputs": [
{
"internalType": "address",
"name": "asset",
"type": "address"
"internalType": "uint256",
"name": "id",
"type": "uint256"
},
{
"internalType": "address",
"name": "seller",
"type": "address"
},
{
"internalType": "address",
"name": "buyer",
"type": "address"
},
{
"internalType": "uint256",
"name": "itemCount",
Expand Down
9 changes: 4 additions & 5 deletions src/marketplace/lsp7/ILSP7Orders.sol
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,12 @@ interface ILSP7Orders {
function place(address asset, uint256 itemPrice, uint256 itemCount) external payable returns (uint256);

/// cancel an order by a buyer being a sender.
/// @param asset asset address
function cancel(address asset) external;
/// @param id order id
function cancel(uint256 id) external;

/// fill an order.
/// @param asset asset address
/// @param id order id
/// @param seller seller
/// @param buyer buyer
/// @param itemCount number of items
function fill(address asset, address seller, address buyer, uint256 itemCount) external;
function fill(uint256 id, address seller, uint256 itemCount) external;
}
2 changes: 1 addition & 1 deletion src/marketplace/lsp7/LSP7Marketplace.sol
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ contract LSP7Marketplace is Base {
function fillOrder(uint256 orderId, uint256 itemCount) external whenNotPaused nonReentrant {
address seller = msg.sender;
LSP7Order memory order = orders.getOrder(orderId);
orders.fill(order.asset, seller, order.buyer, itemCount);
orders.fill(orderId, seller, itemCount);
_executeSale(
order.asset,
itemCount,
Expand Down
23 changes: 13 additions & 10 deletions src/marketplace/lsp7/LSP7Orders.sol
Original file line number Diff line number Diff line change
Expand Up @@ -76,27 +76,30 @@ contract LSP7Orders is ILSP7Orders, Module {
return orderId;
}

function cancel(address asset) external override whenNotPaused nonReentrant {
function cancel(uint256 id) external override whenNotPaused nonReentrant {
address buyer = msg.sender;
LSP7Order memory order = orderOf(asset, buyer);
delete _orders[order.id];
delete _orderIds[asset][buyer];
LSP7Order memory order = getOrder(id);
if (order.buyer != buyer) {
revert NotPlacedOf(order.asset, buyer);
}
delete _orders[id];
delete _orderIds[order.asset][order.buyer];
uint256 totalValue = order.itemPrice * order.itemCount;
(bool success,) = buyer.call{value: totalValue}("");
(bool success,) = order.buyer.call{value: totalValue}("");
if (!success) {
revert Unpaid(buyer, totalValue);
revert Unpaid(order.buyer, totalValue);
}
emit Canceled(order.id, asset, buyer, order.itemPrice, order.itemCount);
emit Canceled(order.id, order.asset, order.buyer, order.itemPrice, order.itemCount);
}

function fill(address asset, address seller, address buyer, uint256 itemCount)
function fill(uint256 id, address seller, uint256 itemCount)
external
override
whenNotPaused
nonReentrant
onlyMarketplace
{
LSP7Order memory order = orderOf(asset, buyer);
LSP7Order memory order = getOrder(id);
if (itemCount > order.itemCount) {
revert InsufficientItemCount(order.itemCount, itemCount);
}
Expand All @@ -106,6 +109,6 @@ contract LSP7Orders is ILSP7Orders, Module {
if (!success) {
revert Unpaid(msg.sender, totalValue);
}
emit Filled(order.id, asset, seller, buyer, order.itemPrice, itemCount, order.itemCount);
emit Filled(order.id, order.asset, seller, order.buyer, order.itemPrice, itemCount, order.itemCount);
}
}
12 changes: 6 additions & 6 deletions test/marketplace/lsp7/LSP7Orders.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ contract LSP7OrdersTest is Test {
vm.expectRevert("Pausable: paused");
orders.place(address(asset), 1 ether, 1);
vm.expectRevert("Pausable: paused");
orders.cancel(address(asset));
orders.cancel(1);
vm.expectRevert("Pausable: paused");
orders.fill(address(asset), address(100), address(101), 100);
orders.fill(1, address(100), 100);
}

function testFuzz_NotPlacedOf(address someAsset, address buyer) public {
Expand Down Expand Up @@ -150,7 +150,7 @@ contract LSP7OrdersTest is Test {
vm.prank(address(alice));
vm.expectEmit();
emit Canceled(1, address(asset), address(alice), 0.5 ether, 2);
orders.cancel(address(asset));
orders.cancel(1);

assertFalse(orders.isPlacedOrder(1));
assertFalse(orders.isPlacedOrderOf(address(asset), address(alice)));
Expand All @@ -169,7 +169,7 @@ contract LSP7OrdersTest is Test {

vm.prank(buyer);
vm.expectRevert(abi.encodeWithSelector(LSP7Orders.NotPlacedOf.selector, address(asset), buyer));
orders.cancel(address(asset));
orders.cancel(1);

assertTrue(orders.isPlacedOrderOf(address(asset), address(alice)));
}
Expand Down Expand Up @@ -204,7 +204,7 @@ contract LSP7OrdersTest is Test {
vm.prank(marketplace);
vm.expectEmit();
emit Filled(1, address(asset), address(bob), address(alice), itemPrice, fillCount, itemCount);
orders.fill(address(asset), address(bob), address(alice), fillCount);
orders.fill(1, address(bob), fillCount);

assertEq(marketplace.balance, fillCount * itemPrice);

Expand Down Expand Up @@ -246,7 +246,7 @@ contract LSP7OrdersTest is Test {
vm.prank(marketplace);
vm.expectEmit();
emit Filled(1, address(asset), address(bob), address(alice), itemPrice, itemCount, itemCount);
orders.fill(address(asset), address(bob), address(alice), itemCount);
orders.fill(1, address(bob), itemCount);

assertEq(marketplace.balance, itemCount * itemPrice);
assertFalse(orders.isPlacedOrderOf(address(asset), address(alice)));
Expand Down

0 comments on commit 0c5fd64

Please sign in to comment.