Skip to content

Commit

Permalink
feat(billboard): revise clearAuction(s) funcs to return prices and taxes
Browse files Browse the repository at this point in the history
  • Loading branch information
robertu7 committed Jan 26, 2024
1 parent 8ce7758 commit 46c633f
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 42 deletions.
50 changes: 25 additions & 25 deletions .gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ ACLManagerTest:testRenounceRole() (gas: 27841)
ACLManagerTest:testRoles() (gas: 15393)
ACLManagerTest:testTransferRole() (gas: 21528)
BillboardTest:testAddToWhitelist() (gas: 35114)
BillboardTest:testApproveAndTransfer() (gas: 162534)
BillboardTest:testApproveAndTransfer() (gas: 162468)
BillboardTest:testCalculateTax() (gas: 21760)
BillboardTest:testCannnotWithdrawTaxIfSmallAmount(uint8) (runs: 256, μ: 519125, ~: 524489)
BillboardTest:testCannnotWithdrawTaxIfZero() (gas: 490314)
BillboardTest:testCannnotWithdrawTaxIfSmallAmount(uint8) (runs: 256, μ: 519318, ~: 524555)
BillboardTest:testCannnotWithdrawTaxIfZero() (gas: 490374)
BillboardTest:testCannotAddToWhitelistByAttacker() (gas: 9037)
BillboardTest:testCannotApproveByAttacker() (gas: 130326)
BillboardTest:testCannotClearAuctionIfAuctionNotEnded() (gas: 699798)
BillboardTest:testCannotClearAuctionOnNewBoard() (gas: 136208)
BillboardTest:testCannotApproveByAttacker() (gas: 130281)
BillboardTest:testCannotClearAuctionIfAuctionNotEnded() (gas: 700985)
BillboardTest:testCannotClearAuctionOnNewBoard() (gas: 136261)
BillboardTest:testCannotMintBoardByAttacker() (gas: 13321)
BillboardTest:testCannotPlaceBidByAttacker() (gas: 246293)
BillboardTest:testCannotPlaceBidTwice(uint96) (runs: 256, μ: 749648, ~: 754816)
BillboardTest:testCannotPlaceBidTwice(uint96) (runs: 256, μ: 749802, ~: 754906)
BillboardTest:testCannotRemoveToWhitelistByAttacker() (gas: 9104)
BillboardTest:testCannotSafeTransferByAttacker() (gas: 127438)
BillboardTest:testCannotSetBoardProprtiesByAttacker() (gas: 157292)
Expand All @@ -29,34 +29,34 @@ BillboardTest:testCannotSetTaxRateByAttacker() (gas: 9006)
BillboardTest:testCannotTransferByOperator() (gas: 132771)
BillboardTest:testCannotTransferToZeroAddress() (gas: 128258)
BillboardTest:testCannotUpgradeRegistryByAttacker() (gas: 9128)
BillboardTest:testCannotWithBidTwice(uint96) (runs: 256, μ: 1079596, ~: 1079596)
BillboardTest:testCannotWithdrawBidIfAuctionNotCleared(uint96) (runs: 256, μ: 910976, ~: 910976)
BillboardTest:testCannotWithdrawBidIfAuctionNotEnded(uint96) (runs: 256, μ: 725669, ~: 725669)
BillboardTest:testCannotWithdrawBidIfNotFound() (gas: 427992)
BillboardTest:testCannotWithdrawBidIfWon(uint96) (runs: 256, μ: 833971, ~: 833971)
BillboardTest:testCannotWithBidTwice(uint96) (runs: 256, μ: 1079907, ~: 1079907)
BillboardTest:testCannotWithdrawBidIfAuctionNotCleared(uint96) (runs: 256, μ: 911089, ~: 911089)
BillboardTest:testCannotWithdrawBidIfAuctionNotEnded(uint96) (runs: 256, μ: 725782, ~: 725782)
BillboardTest:testCannotWithdrawBidIfNotFound() (gas: 428052)
BillboardTest:testCannotWithdrawBidIfWon(uint96) (runs: 256, μ: 834282, ~: 834282)
BillboardTest:testCannotWithdrawTaxByAttacker() (gas: 16677)
BillboardTest:testClearAuctionIfAuctionEnded() (gas: 743955)
BillboardTest:testClearAuctionsIfAuctionEnded() (gas: 1376419)
BillboardTest:testGetBids(uint8,uint8,uint8) (runs: 256, μ: 4688527, ~: 2075169)
BillboardTest:testClearAuctionIfAuctionEnded(uint96) (runs: 256, μ: 837581, ~: 837581)
BillboardTest:testClearAuctionsIfAuctionEnded() (gas: 1379962)
BillboardTest:testGetBids(uint8,uint8,uint8) (runs: 256, μ: 4580822, ~: 2075259)
BillboardTest:testGetTokenURI() (gas: 154936)
BillboardTest:testMintBoard() (gas: 225541)
BillboardTest:testMintBoardByWhitelist() (gas: 154942)
BillboardTest:testMintBoardIfOpened() (gas: 145715)
BillboardTest:testPlaceBidByWhitelist() (gas: 579119)
BillboardTest:testPlaceBidIfAuctionEnded() (gas: 1090527)
BillboardTest:testPlaceBidOnNewBoard(uint96) (runs: 256, μ: 619217, ~: 635029)
BillboardTest:testPlaceBidWithHigherPrice(uint96) (runs: 256, μ: 905056, ~: 913141)
BillboardTest:testPlaceBidWithSamePrices(uint96) (runs: 256, μ: 908042, ~: 917915)
BillboardTest:testPlaceBidZeroPrice() (gas: 376887)
BillboardTest:testPlaceBidByWhitelist() (gas: 579179)
BillboardTest:testPlaceBidIfAuctionEnded() (gas: 1090700)
BillboardTest:testPlaceBidOnNewBoard(uint96) (runs: 256, μ: 618577, ~: 635089)
BillboardTest:testPlaceBidWithHigherPrice(uint96) (runs: 256, μ: 905402, ~: 913254)
BillboardTest:testPlaceBidWithSamePrices(uint96) (runs: 256, μ: 908177, ~: 918050)
BillboardTest:testPlaceBidZeroPrice() (gas: 376947)
BillboardTest:testRemoveToWhitelist() (gas: 23188)
BillboardTest:testSafeTransferByOperator() (gas: 141171)
BillboardTest:testSafeTransferByOperator() (gas: 141193)
BillboardTest:testSetBoardProperties() (gas: 305972)
BillboardTest:testSetBoardPropertiesAfterTransfer() (gas: 335477)
BillboardTest:testSetIsOpened() (gas: 22661)
BillboardTest:testSetTaxRate() (gas: 22887)
BillboardTest:testUpgradeRegistry() (gas: 3038450)
BillboardTest:testWithdrawBid(uint96) (runs: 256, μ: 1081104, ~: 1081104)
BillboardTest:testWithdrawTax(uint96) (runs: 256, μ: 597578, ~: 597578)
BillboardTest:testUpgradeRegistry() (gas: 3131297)
BillboardTest:testWithdrawBid(uint96) (runs: 256, μ: 1081415, ~: 1081415)
BillboardTest:testWithdrawTax(uint96) (runs: 256, μ: 597638, ~: 597638)
CurationTest:testCannotCurateERC20CurateZeroAmount() (gas: 12194)
CurationTest:testCannotCurateERC20EmptyURI() (gas: 15797)
CurationTest:testCannotCurateERC20IfNotApproval() (gas: 21624)
Expand Down
30 changes: 22 additions & 8 deletions src/Billboard/Billboard.sol
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ contract Billboard is IBillboard {
}

/// @inheritdoc IBillboard
function clearAuction(uint256 tokenId_) public {
function clearAuction(uint256 tokenId_) public returns (uint256 price, uint256 tax) {
// revert if board not found
IBillboardRegistry.Board memory _board = registry.getBoard(tokenId_);
require(_board.creator != address(0), "Board not found");
Expand All @@ -160,25 +160,37 @@ contract Billboard is IBillboard {
address _prevOwner = registry.ownerOf(tokenId_);
if (_nextAuction.startAt == 0 && _prevOwner != _board.creator) {
registry.safeTransferByOperator(_prevOwner, _board.creator, tokenId_);
return;
return (0, 0);
}

_clearAuction(tokenId_, _board.creator, _nextAuctionId);
return _clearAuction(tokenId_, _board.creator, _nextAuctionId);
}

/// @inheritdoc IBillboard
function clearAuctions(uint256[] calldata tokenIds_) external {
for (uint256 i = 0; i < tokenIds_.length; i++) {
clearAuction(tokenIds_[i]);
function clearAuctions(
uint256[] calldata tokenIds_
) external returns (uint256[] memory prices, uint256[] memory taxes) {
uint256 _size = tokenIds_.length;
uint256[] memory _prices = new uint256[](_size);
uint256[] memory _taxes = new uint256[](_size);

for (uint256 i = 0; i < _size; i++) {
(_prices[i], _taxes[i]) = clearAuction(tokenIds_[i]);
}

return (_prices, _taxes);
}

function _clearAuction(uint256 tokenId_, address boardCreator_, uint256 nextAuctionId_) private {
function _clearAuction(
uint256 tokenId_,
address boardCreator_,
uint256 nextAuctionId_
) private returns (uint256 price, uint256 tax) {
IBillboardRegistry.Auction memory _nextAuction = registry.getAuction(tokenId_, nextAuctionId_);

// skip if auction is already cleared
if (_nextAuction.leaseEndAt != 0) {
return;
return (0, 0);
}

address _prevOwner = registry.ownerOf(tokenId_);
Expand Down Expand Up @@ -211,6 +223,8 @@ contract Billboard is IBillboard {

// emit AuctionCleared
registry.emitAuctionCleared(tokenId_, nextAuctionId_, _nextAuction.highestBidder, leaseStartAt, leaseEndAt);

return (_highestBid.price, _highestBid.tax);
}

/// @inheritdoc IBillboard
Expand Down
6 changes: 4 additions & 2 deletions src/Billboard/IBillboard.sol
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,16 @@ interface IBillboard {
*
* @param tokenId_ Token ID of a board.
*/
function clearAuction(uint256 tokenId_) external;
function clearAuction(uint256 tokenId_) external returns (uint256 price, uint256 tax);

/**
* @notice Clear the next auction of mutiple boards.
*
* @param tokenIds_ Token IDs of boards.
*/
function clearAuctions(uint256[] calldata tokenIds_) external;
function clearAuctions(
uint256[] calldata tokenIds_
) external returns (uint256[] memory prices, uint256[] memory taxes);

/**
* @notice Place bid for the next auction of a board.
Expand Down
25 changes: 18 additions & 7 deletions src/test/Billboard/BillboardTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -590,15 +590,20 @@ contract BillboardTest is BillboardTestBase {
operator.placeBid(_tokenId, _amount);
}

function testClearAuctionIfAuctionEnded() public {
function testClearAuctionIfAuctionEnded(uint96 _amount) public {
vm.assume(_amount > 0.001 ether);

uint256 _tax = operator.calculateTax(_amount);
uint256 _total = _amount + _tax;

(uint256 _tokenId, uint256 _prevAuctionId) = _mintBoardAndPlaceBid();
uint64 _placedAt = uint64(block.number);
uint64 _clearedAt = uint64(block.number) + registry.leaseTerm() + 1;

// place a bid
vm.startPrank(USER_A);
deal(address(usdt), USER_A, 0);
operator.placeBid(_tokenId, 0);
deal(address(usdt), USER_A, _total);
operator.placeBid(_tokenId, _amount);

// clear auction
vm.expectEmit(true, true, true, true);
Expand All @@ -611,7 +616,9 @@ contract BillboardTest is BillboardTestBase {
);

vm.roll(_clearedAt);
operator.clearAuction(_tokenId);
(uint256 _price1, uint256 _tax1) = operator.clearAuction(_tokenId);
assertEq(_price1, _amount);
assertEq(_tax1, _tax);

// check auction
uint256 _nextAuctionId = registry.nextBoardAuctionId(_tokenId);
Expand All @@ -624,8 +631,8 @@ contract BillboardTest is BillboardTestBase {

// check bid
IBillboardRegistry.Bid memory _bid = registry.getBid(_tokenId, _nextAuctionId, USER_A);
assertEq(_bid.price, 0);
assertEq(_bid.tax, 0);
assertEq(_bid.price, _amount);
assertEq(_bid.tax, _tax);
assertEq(_bid.placedAt, _placedAt);
assertEq(_bid.isWon, true);
assertEq(_bid.isWithdrawn, false);
Expand Down Expand Up @@ -670,7 +677,11 @@ contract BillboardTest is BillboardTestBase {
uint256[] memory _tokenIds = new uint256[](2);
_tokenIds[0] = _tokenId;
_tokenIds[1] = _tokenId2;
operator.clearAuctions(_tokenIds);
(uint256[] memory prices, uint256[] memory taxes) = operator.clearAuctions(_tokenIds);
assertEq(prices[0], 0);
assertEq(prices[1], 0);
assertEq(taxes[0], 0);
assertEq(taxes[1], 0);

// check auction
uint256 _nextAuctionId = registry.nextBoardAuctionId(_tokenId);
Expand Down

0 comments on commit 46c633f

Please sign in to comment.