Skip to content

Commit

Permalink
Enable spot buy with different recipient
Browse files Browse the repository at this point in the history
  • Loading branch information
lykhonis committed Aug 24, 2023
1 parent 5924c29 commit 378dfaf
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 19 deletions.
5 changes: 5 additions & 0 deletions artifacts/abi/marketplace/lsp7/LSP7Marketplace.json
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,11 @@
"internalType": "uint256",
"name": "itemCount",
"type": "uint256"
},
{
"internalType": "address",
"name": "recipient",
"type": "address"
}
],
"name": "buy",
Expand Down
5 changes: 5 additions & 0 deletions artifacts/abi/marketplace/lsp8/LSP8Marketplace.json
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,11 @@
"internalType": "uint256",
"name": "listingId",
"type": "uint256"
},
{
"internalType": "address",
"name": "recipient",
"type": "address"
}
],
"name": "buy",
Expand Down
4 changes: 2 additions & 2 deletions src/marketplace/lsp7/LSP7Marketplace.sol
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ contract LSP7Marketplace is Base {
offers = offers_;
}

function buy(uint256 listingId, uint256 itemCount) external payable whenNotPaused nonReentrant {
function buy(uint256 listingId, uint256 itemCount, address recipient) external payable whenNotPaused nonReentrant {
LSP7Listing memory listing = listings.getListing(listingId);
if (listing.itemPrice * itemCount != msg.value) {
revert InsufficientFunds(listing.itemPrice * itemCount, msg.value);
}
listings.deduct(listingId, itemCount);
_executeSale(listingId, listing.asset, itemCount, listing.owner, msg.sender, msg.value);
_executeSale(listingId, listing.asset, itemCount, listing.owner, recipient, msg.value);
}

function acceptOffer(uint256 listingId, address buyer) external whenNotPaused nonReentrant {
Expand Down
4 changes: 2 additions & 2 deletions src/marketplace/lsp8/LSP8Marketplace.sol
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ contract LSP8Marketplace is IPageNameMarketplace, Base {
return _lastPurchasePrice[buyer][asset][tokenId];
}

function buy(uint256 listingId) external payable whenNotPaused nonReentrant {
function buy(uint256 listingId, address recipient) external payable whenNotPaused nonReentrant {
if (auctions.isIssued(listingId)) {
revert Auctioned(listingId);
}
Expand All @@ -76,7 +76,7 @@ contract LSP8Marketplace is IPageNameMarketplace, Base {
revert InsufficientFunds(listing.price, msg.value);
}
listings.unlist(listingId);
_executeSale(listingId, listing.asset, listing.tokenId, listing.owner, msg.sender, msg.value);
_executeSale(listingId, listing.asset, listing.tokenId, listing.owner, recipient, msg.value);
}

function acceptOffer(uint256 listingId, address buyer) external whenNotPaused nonReentrant {
Expand Down
14 changes: 7 additions & 7 deletions test/marketplace/lsp7/LSP7Marketplace.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ contract LSP7MarketplaceTest is Test {
vm.prank(owner);
marketplace.pause();
vm.expectRevert("Pausable: paused");
marketplace.buy(1, 1);
marketplace.buy(1, 1, address(100));
vm.expectRevert("Pausable: paused");
marketplace.acceptOffer(1, address(100));
}
Expand Down Expand Up @@ -159,7 +159,7 @@ contract LSP7MarketplaceTest is Test {
vm.prank(address(bob));
vm.expectEmit(address(marketplace));
emit Sale(1, address(asset), itemCount, address(alice), address(bob), totalPrice);
marketplace.buy{value: totalPrice}(1, itemCount);
marketplace.buy{value: totalPrice}(1, itemCount, address(bob));

assertFalse(listings.isListed(1));
assertEq(address(alice).balance, totalPrice);
Expand Down Expand Up @@ -196,7 +196,7 @@ contract LSP7MarketplaceTest is Test {
}
vm.expectEmit(address(marketplace));
emit Sale(1, address(asset), itemCount, address(alice), address(bob), totalPrice);
marketplace.buy{value: totalPrice}(1, itemCount);
marketplace.buy{value: totalPrice}(1, itemCount, address(bob));

assertFalse(listings.isListed(1));
assertEq(address(marketplace).balance, feeAmount);
Expand Down Expand Up @@ -237,7 +237,7 @@ contract LSP7MarketplaceTest is Test {
}
vm.expectEmit(address(marketplace));
emit Sale(1, address(asset), 10, address(alice), address(bob), totalPrice);
marketplace.buy{value: totalPrice}(1, 10);
marketplace.buy{value: totalPrice}(1, 10, address(bob));

assertFalse(listings.isListed(1));
assertEq(address(marketplace).balance, feeAmount - discountFeeAmount);
Expand Down Expand Up @@ -296,7 +296,7 @@ contract LSP7MarketplaceTest is Test {
}
vm.expectEmit(address(marketplace));
emit Sale(1, address(asset), itemCount, address(alice), address(bob), totalPrice);
marketplace.buy{value: totalPrice}(1, itemCount);
marketplace.buy{value: totalPrice}(1, itemCount, address(bob));

assertFalse(listings.isListed(1));
assertEq(address(marketplace).balance, 0);
Expand Down Expand Up @@ -327,7 +327,7 @@ contract LSP7MarketplaceTest is Test {
emit FeePaid(1, address(asset), 10, 1 ether);
vm.expectEmit(address(marketplace));
emit Sale(1, address(asset), 10, address(alice), address(bob), 10 ether);
marketplace.buy{value: 10 ether}(1, 10);
marketplace.buy{value: 10 ether}(1, 10, address(bob));

assertFalse(listings.isListed(1));
assertEq(address(marketplace).balance, 1 ether);
Expand Down Expand Up @@ -365,7 +365,7 @@ contract LSP7MarketplaceTest is Test {
vm.expectRevert(
abi.encodeWithSelector(Base.RoyaltiesExceedThreshold.selector, 1, 10 ether, Points.realize(10 ether, 2))
);
marketplace.buy{value: 10 ether}(1, 10);
marketplace.buy{value: 10 ether}(1, 10, address(bob));
}

function testFuzz_AcceptOffer(uint256 itemCount, uint256 itemPrice, uint256 totalPrice) public {
Expand Down
16 changes: 8 additions & 8 deletions test/marketplace/lsp8/LSP8Marketplace.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ contract LSP8MarketplaceTest is Test {
vm.prank(owner);
marketplace.pause();
vm.expectRevert("Pausable: paused");
marketplace.buy(1);
marketplace.buy(1, address(100));
vm.expectRevert("Pausable: paused");
marketplace.acceptOffer(1, address(100));
vm.expectRevert("Pausable: paused");
Expand Down Expand Up @@ -172,7 +172,7 @@ contract LSP8MarketplaceTest is Test {
vm.prank(address(bob));
vm.expectEmit(address(marketplace));
emit Sale(1, address(asset), tokenId, address(alice), address(bob), price);
marketplace.buy{value: price}(1);
marketplace.buy{value: price}(1, address(bob));

assertFalse(listings.isListed(1));
assertEq(address(alice).balance, price);
Expand Down Expand Up @@ -207,7 +207,7 @@ contract LSP8MarketplaceTest is Test {
}
vm.expectEmit(address(marketplace));
emit Sale(1, address(asset), tokenId, address(alice), address(bob), price);
marketplace.buy{value: price}(1);
marketplace.buy{value: price}(1, address(bob));

assertFalse(listings.isListed(1));
assertEq(address(marketplace).balance, feeAmount);
Expand Down Expand Up @@ -249,7 +249,7 @@ contract LSP8MarketplaceTest is Test {
}
vm.expectEmit(address(marketplace));
emit Sale(1, address(asset), tokenId, address(alice), address(bob), totalPrice);
marketplace.buy{value: totalPrice}(1);
marketplace.buy{value: totalPrice}(1, address(bob));

assertFalse(listings.isListed(1));
assertEq(address(marketplace).balance, feeAmount - discountFeeAmount);
Expand Down Expand Up @@ -301,7 +301,7 @@ contract LSP8MarketplaceTest is Test {
}
vm.expectEmit(address(marketplace));
emit Sale(1, address(asset), tokenId, address(alice), address(bob), price);
marketplace.buy{value: price}(1);
marketplace.buy{value: price}(1, address(bob));

assertFalse(listings.isListed(1));
assertEq(address(marketplace).balance, 0);
Expand Down Expand Up @@ -333,7 +333,7 @@ contract LSP8MarketplaceTest is Test {
emit FeePaid(1, address(asset), tokenId, 0.1 ether);
vm.expectEmit(address(marketplace));
emit Sale(1, address(asset), tokenId, address(alice), address(bob), 1 ether);
marketplace.buy{value: 1 ether}(1);
marketplace.buy{value: 1 ether}(1, address(bob));

assertFalse(listings.isListed(1));
assertEq(address(marketplace).balance, 0.1 ether);
Expand Down Expand Up @@ -371,7 +371,7 @@ contract LSP8MarketplaceTest is Test {
vm.expectRevert(
abi.encodeWithSelector(Base.RoyaltiesExceedThreshold.selector, 1, 1 ether, Points.realize(1 ether, 2))
);
marketplace.buy{value: 1 ether}(1);
marketplace.buy{value: 1 ether}(1, address(bob));
}

function testFuzz_AcceptOffer(uint256 price, uint256 totalPrice) public {
Expand Down Expand Up @@ -468,7 +468,7 @@ contract LSP8MarketplaceTest is Test {
vm.deal(address(bob), 1 ether);
vm.prank(address(bob));
vm.expectRevert(abi.encodeWithSelector(LSP8Marketplace.Auctioned.selector, 1));
marketplace.buy{value: 1 ether}(1);
marketplace.buy{value: 1 ether}(1, address(bob));
}

function test_Revert_AcceptOfferIfAuctioned() public {
Expand Down

0 comments on commit 378dfaf

Please sign in to comment.