Skip to content

Commit

Permalink
Add getLastWonBid method in billboard.sol
Browse files Browse the repository at this point in the history
  • Loading branch information
zeckli committed Nov 27, 2023
1 parent 363c16b commit 188688f
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 43 deletions.
85 changes: 43 additions & 42 deletions .gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -10,54 +10,55 @@ ACLManagerTest:testRenounceRole() (gas: 27841)
ACLManagerTest:testRoles() (gas: 15393)
ACLManagerTest:testTransferRole() (gas: 21528)
BillboardTest:testAddToWhitelist() (gas: 35114)
BillboardTest:testApproveAndTransfer() (gas: 162512)
BillboardTest:testCalculateTax() (gas: 22822)
BillboardTest:testCannnotWithdrawTaxIfSmallAmount(uint8) (runs: 256, μ: 410878, ~: 424702)
BillboardTest:testCannnotWithdrawTaxIfZero() (gas: 377863)
BillboardTest:testApproveAndTransfer() (gas: 162535)
BillboardTest:testCalculateTax() (gas: 22778)
BillboardTest:testCannnotWithdrawTaxIfSmallAmount(uint8) (runs: 256, μ: 411584, ~: 424725)
BillboardTest:testCannnotWithdrawTaxIfZero() (gas: 377886)
BillboardTest:testCannotAddToWhitelistByAttacker() (gas: 9037)
BillboardTest:testCannotApproveByAttacker() (gas: 130271)
BillboardTest:testCannotClearAuctionIfAuctionNotEnded() (gas: 578827)
BillboardTest:testCannotClearAuctionOnNewBoard() (gas: 136253)
BillboardTest:testCannotMintBoardByAttacker() (gas: 13321)
BillboardTest:testCannotPlaceBidByAttacker() (gas: 139222)
BillboardTest:testCannotPlaceBidTwice(uint96) (runs: 256, μ: 624744, ~: 630975)
BillboardTest:testCannotApproveByAttacker() (gas: 130294)
BillboardTest:testCannotClearAuctionIfAuctionNotEnded() (gas: 578872)
BillboardTest:testCannotClearAuctionOnNewBoard() (gas: 136276)
BillboardTest:testCannotMintBoardByAttacker() (gas: 13344)
BillboardTest:testCannotPlaceBidByAttacker() (gas: 139245)
BillboardTest:testCannotPlaceBidTwice(uint96) (runs: 256, μ: 624767, ~: 630998)
BillboardTest:testCannotRemoveToWhitelistByAttacker() (gas: 9104)
BillboardTest:testCannotSafeTransferByAttacker() (gas: 127438)
BillboardTest:testCannotSetBoardProprtiesByAttacker() (gas: 157292)
BillboardTest:testCannotSafeTransferByAttacker() (gas: 127461)
BillboardTest:testCannotSetBoardProprtiesByAttacker() (gas: 157361)
BillboardTest:testCannotSetIsOpenedByAttacker() (gas: 8994)
BillboardTest:testCannotSetTaxRateByAttacker() (gas: 9006)
BillboardTest:testCannotTransferByOperator() (gas: 132771)
BillboardTest:testCannotTransferToZeroAddress() (gas: 128258)
BillboardTest:testCannotUpgradeRegistryByAttacker() (gas: 9128)
BillboardTest:testCannotWithBidTwice(uint96) (runs: 256, μ: 903306, ~: 903306)
BillboardTest:testCannotWithdrawBidIfAuctionNotCleared(uint96) (runs: 256, μ: 743847, ~: 743847)
BillboardTest:testCannotWithdrawBidIfAuctionNotEnded(uint96) (runs: 256, μ: 619025, ~: 619025)
BillboardTest:testCannotWithdrawBidIfNotFound() (gas: 413160)
BillboardTest:testCannotWithdrawBidIfWon(uint96) (runs: 256, μ: 710721, ~: 710721)
BillboardTest:testCannotSetTaxRateByAttacker() (gas: 8962)
BillboardTest:testCannotTransferByOperator() (gas: 132794)
BillboardTest:testCannotTransferToZeroAddress() (gas: 128281)
BillboardTest:testCannotUpgradeRegistryByAttacker() (gas: 9084)
BillboardTest:testCannotWithBidTwice(uint96) (runs: 256, μ: 903329, ~: 903329)
BillboardTest:testCannotWithdrawBidIfAuctionNotCleared(uint96) (runs: 256, μ: 743870, ~: 743870)
BillboardTest:testCannotWithdrawBidIfAuctionNotEnded(uint96) (runs: 256, μ: 619048, ~: 619048)
BillboardTest:testCannotWithdrawBidIfNotFound() (gas: 413183)
BillboardTest:testCannotWithdrawBidIfWon(uint96) (runs: 256, μ: 710744, ~: 710744)
BillboardTest:testCannotWithdrawTaxByAttacker() (gas: 16677)
BillboardTest:testClearAuctionIfAuctionEnded() (gas: 622677)
BillboardTest:testClearAuctionsIfAuctionEnded() (gas: 1156582)
BillboardTest:testGetBids(uint8,uint8,uint8) (runs: 256, μ: 2773780, ~: 1419565)
BillboardTest:testGetTokenURI() (gas: 154980)
BillboardTest:testMintBoard() (gas: 225541)
BillboardTest:testMintBoardByWhitelist() (gas: 154942)
BillboardTest:testMintBoardIfOpened() (gas: 145715)
BillboardTest:testPlaceBidByWhitelist() (gas: 461423)
BillboardTest:testPlaceBidIfAuctionEnded() (gas: 906529)
BillboardTest:testPlaceBidOnNewBoard(uint96) (runs: 256, μ: 510724, ~: 520900)
BillboardTest:testPlaceBidWithHigherPrice(uint96) (runs: 256, μ: 739847, ~: 747077)
BillboardTest:testPlaceBidWithSamePrices(uint96) (runs: 256, μ: 738385, ~: 750061)
BillboardTest:testPlaceBidZeroPrice() (gas: 354275)
BillboardTest:testClearAuctionIfAuctionEnded() (gas: 622700)
BillboardTest:testClearAuctionsIfAuctionEnded() (gas: 1156628)
BillboardTest:testGetBids(uint8,uint8,uint8) (runs: 256, μ: 2715677, ~: 1414075)
BillboardTest:testGetLastWonBid() (gas: 1179284)
BillboardTest:testGetTokenURI() (gas: 155026)
BillboardTest:testMintBoard() (gas: 225587)
BillboardTest:testMintBoardByWhitelist() (gas: 154988)
BillboardTest:testMintBoardIfOpened() (gas: 145738)
BillboardTest:testPlaceBidByWhitelist() (gas: 461446)
BillboardTest:testPlaceBidIfAuctionEnded() (gas: 906552)
BillboardTest:testPlaceBidOnNewBoard(uint96) (runs: 256, μ: 510747, ~: 520923)
BillboardTest:testPlaceBidWithHigherPrice(uint96) (runs: 256, μ: 739892, ~: 747122)
BillboardTest:testPlaceBidWithSamePrices(uint96) (runs: 256, μ: 738408, ~: 750084)
BillboardTest:testPlaceBidZeroPrice() (gas: 354298)
BillboardTest:testRemoveToWhitelist() (gas: 23207)
BillboardTest:testSafeTransferByOperator() (gas: 141237)
BillboardTest:testSetBoardProperties() (gas: 305883)
BillboardTest:testSetBoardPropertiesAfterTransfer() (gas: 335509)
BillboardTest:testSafeTransferByOperator() (gas: 141260)
BillboardTest:testSetBoardProperties() (gas: 305952)
BillboardTest:testSetBoardPropertiesAfterTransfer() (gas: 335624)
BillboardTest:testSetIsOpened() (gas: 22661)
BillboardTest:testSetTaxRate() (gas: 22909)
BillboardTest:testSomethin() (gas: 1626769)
BillboardTest:testUpgradeRegistry() (gas: 2968149)
BillboardTest:testWithdrawBid(uint96) (runs: 256, μ: 904808, ~: 904808)
BillboardTest:testWithdrawTax(uint96) (runs: 256, μ: 500265, ~: 500265)
BillboardTest:testSetTaxRate() (gas: 22821)
BillboardTest:testSomethin() (gas: 1626838)
BillboardTest:testUpgradeRegistry() (gas: 3153089)
BillboardTest:testWithdrawBid(uint96) (runs: 256, μ: 904853, ~: 904853)
BillboardTest:testWithdrawTax(uint96) (runs: 256, μ: 500288, ~: 500288)
CurationTest:testCannotCurateERC20CurateZeroAmount() (gas: 12194)
CurationTest:testCannotCurateERC20EmptyURI() (gas: 15797)
CurationTest:testCannotCurateERC20IfNotApproval() (gas: 21624)
Expand Down
19 changes: 19 additions & 0 deletions src/Billboard/Billboard.sol
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,25 @@ contract Billboard is IBillboard {
return (_total, limit_, offset_, _bids);
}

/// @inheritdoc IBillboard
function getLastWonBid(uint256 tokenId_) external view returns (IBillboardRegistry.Bid memory bid) {
uint256 _auctionId = registry.nextBoardAuctionId(tokenId_);
require(_auctionId > 0, "Last cleared auction not found");

IBillboardRegistry.Auction memory auction = registry.getAuction(tokenId_, _auctionId);
bid = registry.getBid(tokenId_, _auctionId, auction.highestBidder);

// check if the auction is ended
if (bid.isWon == true) {
return bid;
}

// get previous auction
uint256 _lastAuctionId = _auctionId - 1;
auction = registry.getAuction(tokenId_, _lastAuctionId);
bid = registry.getBid(tokenId_, _lastAuctionId, auction.highestBidder);
}

//////////////////////////////
/// Tax & Withdraw
//////////////////////////////
Expand Down
7 changes: 7 additions & 0 deletions src/Billboard/IBillboard.sol
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,13 @@ interface IBillboard {
uint256 offset_
) external view returns (uint256 total, uint256 limit, uint256 offset, IBillboardRegistry.Bid[] memory bids);

/**
* @notice Get the last won bid of a board.
*
* @param tokenId_ Token ID of a board.
*/
function getLastWonBid(uint256 tokenId_) external view returns (IBillboardRegistry.Bid memory bid);

//////////////////////////////
/// Tax & Withdraw
//////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion src/Billboard/IBillboardRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ interface IBillboardRegistry is IERC721 {
* @notice Get an auction
*
* @param tokenId_ Token ID of a board.
* @param auctionId_ Token ID of a board.
* @param auctionId_ Auction ID of a auction.
*/
function getAuction(uint256 tokenId_, uint256 auctionId_) external view returns (Auction memory auction);

Expand Down
57 changes: 57 additions & 0 deletions src/test/Billboard/BillboardTest.t.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.20;

import "forge-std/console.sol";
import "./BillboardTestBase.t.sol";
import "@openzeppelin/contracts/token/ERC721/IERC721.sol";

Expand Down Expand Up @@ -791,6 +792,62 @@ contract BillboardTest is BillboardTestBase {
}
}

function testGetLastWonBid() public {
(uint256 _tokenId, ) = _mintBoardAndPlaceBid();

// check the first won bid by ADMIN
IBillboardRegistry.Bid memory _bid = operator.getLastWonBid(_tokenId);
assertEq(_bid.price, 0);

// place a bid by USER_A during the second auction
uint256 _amount_a = 3 ether;
uint256 _tax_a = operator.calculateTax(_amount_a);
uint256 _total_a = _amount_a + _tax_a;
vm.startPrank(USER_A);
vm.deal(USER_A, _total_a);
operator.placeBid{value: _total_a}(_tokenId, _amount_a);

// place a bid by USER_B during the second auction
uint256 _amount_b = 1 ether;
uint256 _tax_b = operator.calculateTax(_amount_b);
uint256 _total_b = _amount_b + _tax_b;
vm.startPrank(USER_B);
vm.deal(USER_B, _total_b);
operator.placeBid{value: _total_b}(_tokenId, _amount_b);

// check the last won bid is still the first bid before clearing
_bid = operator.getLastWonBid(_tokenId);
assertEq(_bid.price, 0);

// clear auction
vm.warp(block.timestamp + registry.leaseTerm() + 1 minutes);
operator.clearAuction(_tokenId);

// check the second won bid
_bid = operator.getLastWonBid(_tokenId);
assertEq(_bid.price, 3 ether);

// place a bid by USER_A during the thrid auction
_amount_a = 5 ether;
_tax_a = operator.calculateTax(_amount_a);
_total_a = _amount_a + _tax_a;
vm.startPrank(USER_A);
vm.deal(USER_A, _total_a);
operator.placeBid{value: _total_a}(_tokenId, _amount_a);

// check last won bid is still the second bid before clearing
_bid = operator.getLastWonBid(_tokenId);
assertEq(_bid.price, 3 ether);

// clear auction
vm.warp(block.timestamp + registry.leaseTerm() + 1 minutes);
operator.clearAuction(_tokenId);

// check the third won bid
_bid = operator.getLastWonBid(_tokenId);
assertEq(_bid.price, 5 ether);
}

//////////////////////////////
/// Tax & Withdraw
//////////////////////////////
Expand Down

0 comments on commit 188688f

Please sign in to comment.