Skip to content

Commit

Permalink
test(billboard): add tests for auction & bids
Browse files Browse the repository at this point in the history
  • Loading branch information
robertu7 committed Jul 3, 2024
1 parent f84f906 commit b0aae9c
Show file tree
Hide file tree
Showing 4 changed files with 213 additions and 273 deletions.
51 changes: 28 additions & 23 deletions .gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,34 @@ ACLManagerTest:testGrantRole() (gas: 23547)
ACLManagerTest:testRenounceRole() (gas: 27841)
ACLManagerTest:testRoles() (gas: 15393)
ACLManagerTest:testTransferRole() (gas: 21528)
BillboardTest:testAddToWhitelist() (gas: 252070)
BillboardTest:testApproveAndTransfer() (gas: 252645)
BillboardTest:testCannotAddToWhitelistByAttacker() (gas: 225643)
BillboardTest:testCannotApproveByAttacker() (gas: 221634)
BillboardTest:testCannotPlaceBidByAttacker() (gas: 459506)
BillboardTest:testCannotPlaceBidIfAuctionEnded() (gas: 598942)
BillboardTest:testCannotRemoveToWhitelistByAttacker() (gas: 225773)
BillboardTest:testCannotSafeTransferByAttacker() (gas: 218748)
BillboardTest:testCannotSetBoardByAttacker() (gas: 226787)
BillboardTest:testCannotSetBoardByOwner() (gas: 358929)
BillboardTest:testCannotTransferByOperator() (gas: 224012)
BillboardTest:testCannotTransferToZeroAddress() (gas: 219434)
BillboardTest:testCannotUpgradeRegistryByAttacker() (gas: 9016)
BillboardTest:testGetTokenURI() (gas: 388561)
BillboardTest:testMintBoard() (gas: 417198)
BillboardTest:testPlaceBid(uint96) (runs: 256, μ: 599051, ~: 601384)
BillboardTest:testPlaceBidWithHigherPrice(uint96) (runs: 256, μ: 953951, ~: 953956)
BillboardTest:testPlaceBidWithSamePrices(uint96) (runs: 256, μ: 846964, ~: 850463)
BillboardTest:testPlaceBidZeroPrice() (gas: 416316)
BillboardTest:testRemoveToWhitelist() (gas: 238710)
BillboardTest:testSafeTransferByOperator() (gas: 232457)
BillboardTest:testSetBoardByCreator() (gas: 339338)
BillboardTest:testUpgradeRegistry() (gas: 3319574)
BillboardTest:testAddToWhitelist() (gas: 252168)
BillboardTest:testApproveAndTransfer() (gas: 252840)
BillboardTest:testCannotAddToWhitelistByAttacker() (gas: 225830)
BillboardTest:testCannotApproveByAttacker() (gas: 221688)
BillboardTest:testCannotClearAuctionIfAuctionNotEnded() (gas: 247623)
BillboardTest:testCannotClearAuctionIfNoBid() (gas: 255406)
BillboardTest:testCannotPlaceBidByAttacker() (gas: 459863)
BillboardTest:testCannotPlaceBidIfAuctionEnded() (gas: 270366)
BillboardTest:testCannotRemoveToWhitelistByAttacker() (gas: 225871)
BillboardTest:testCannotSafeTransferByAttacker() (gas: 218868)
BillboardTest:testCannotSetBoardByAttacker() (gas: 226929)
BillboardTest:testCannotSetBoardByOwner() (gas: 359191)
BillboardTest:testCannotTransferByOperator() (gas: 224110)
BillboardTest:testCannotTransferToZeroAddress() (gas: 219554)
BillboardTest:testCannotUpgradeRegistryByAttacker() (gas: 9038)
BillboardTest:testClearAuction(uint96) (runs: 256, μ: 712150, ~: 712150)
BillboardTest:testClearAuctions() (gas: 1278587)
BillboardTest:testGetBids(uint8,uint8,uint8) (runs: 256, μ: 4938705, ~: 2145225)
BillboardTest:testGetTokenURI() (gas: 388777)
BillboardTest:testMintBoard() (gas: 417504)
BillboardTest:testPlaceBid(uint96) (runs: 256, μ: 798108, ~: 800441)
BillboardTest:testPlaceBidWithHigherPrice(uint96) (runs: 256, μ: 975862, ~: 975867)
BillboardTest:testPlaceBidWithSamePrices(uint96) (runs: 256, μ: 865951, ~: 869450)
BillboardTest:testPlaceBidZeroPrice() (gas: 416622)
BillboardTest:testRemoveToWhitelist() (gas: 238830)
BillboardTest:testSafeTransferByOperator() (gas: 232599)
BillboardTest:testSetBoardByCreator() (gas: 339600)
BillboardTest:testUpgradeRegistry() (gas: 3326886)
CurationTest:testCannotCurateERC20CurateZeroAmount() (gas: 12194)
CurationTest:testCannotCurateERC20EmptyURI() (gas: 15797)
CurationTest:testCannotCurateERC20IfNotApproval() (gas: 21624)
Expand Down
55 changes: 25 additions & 30 deletions src/Billboard/Billboard.sol
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ contract Billboard is IBillboard {
require(_board.creator != address(0), "Board not found");

uint256 _endedAt = this.getBlockFromEpoch(epoch_ + 1, _board.epochInterval);
require(_endedAt >= block.number, "Auction ended");
require(block.number < _endedAt, "Auction ended");

IBillboardRegistry.Bid memory _bid = registry.getBid(tokenId_, epoch_, msg.sender);

Expand Down Expand Up @@ -189,36 +189,14 @@ contract Billboard is IBillboard {

// revert if auction is still running
uint256 _endedAt = this.getBlockFromEpoch(epoch_ + 1, _board.epochInterval);
require(block.number < _endedAt, "Auction not ended");
require(block.number >= _endedAt, "Auction not ended");

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

/// @inheritdoc IBillboard
function clearAuctions(
uint256[] calldata tokenIds_,
uint256[] calldata epochs_
) external returns (address[] memory highestBidders, uint256[] memory prices, uint256[] memory taxes) {
uint256 _size = tokenIds_.length;
address[] memory _highestBidders = new address[](_size);
uint256[] memory _prices = new uint256[](_size);
uint256[] memory _taxes = new uint256[](_size);

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

return (_highestBidders, _prices, _taxes);
}

function _clearAuction(
uint256 tokenId_,
address boardCreator_,
uint256 epoch_
) private returns (address highestBidder, uint256 price, uint256 tax) {
address _highestBidder = registry.highestBidder(tokenId_, epoch_);
IBillboardRegistry.Bid memory _highestBid = registry.getBid(tokenId_, epoch_, _highestBidder);

// revert if no bid
require(_highestBid.createdAt != 0, "No bid");

// skip if auction is already cleared
if (_highestBid.isWon) {
return (address(0), 0, 0);
Expand All @@ -231,8 +209,8 @@ contract Billboard is IBillboard {
registry.transferCurrencyByOperator(_prevOwner, _highestBid.price);

// transfer bid tax to board creator's tax treasury
(uint256 _taxAccumulated, uint256 _taxWithdrawn) = registry.taxTreasury(boardCreator_);
registry.setTaxTreasury(boardCreator_, _taxAccumulated + _highestBid.tax, _taxWithdrawn);
(uint256 _taxAccumulated, uint256 _taxWithdrawn) = registry.taxTreasury(_board.creator);
registry.setTaxTreasury(_board.creator, _taxAccumulated + _highestBid.tax, _taxWithdrawn);
}

// transfer ownership
Expand All @@ -247,6 +225,23 @@ contract Billboard is IBillboard {
return (_highestBidder, _highestBid.price, _highestBid.tax);
}

/// @inheritdoc IBillboard
function clearAuctions(
uint256[] calldata tokenIds_,
uint256[] calldata epochs_
) external returns (address[] memory highestBidders, uint256[] memory prices, uint256[] memory taxes) {
uint256 _size = tokenIds_.length;
address[] memory _highestBidders = new address[](_size);
uint256[] memory _prices = new uint256[](_size);
uint256[] memory _taxes = new uint256[](_size);

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

return (_highestBidders, _prices, _taxes);
}

/// @inheritdoc IBillboard
function getBid(
uint256 tokenId_,
Expand Down Expand Up @@ -294,7 +289,7 @@ contract Billboard is IBillboard {

// revert if auction is not ended
uint256 _endedAt = this.getBlockFromEpoch(epoch_ + 1, _board.epochInterval);
require(block.number < _endedAt, "Auction not ended");
require(block.number >= _endedAt, "Auction not ended");

// revert if auction is not cleared
address _highestBidder = registry.highestBidder(tokenId_, epoch_);
Expand Down
Loading

0 comments on commit b0aae9c

Please sign in to comment.