Skip to content

Commit

Permalink
ah2: gas optimize prices functions
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrai committed Oct 6, 2023
1 parent 1e98e14 commit 3b7e1ee
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 13 deletions.
10 changes: 6 additions & 4 deletions packages/nouns-contracts/.gas-snapshot
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
NounsAuctionHouseV2WarmedUp_GasSnapshot:test_createOneBid() (gas: 35090)
NounsAuctionHouseV2WarmedUp_GasSnapshot:test_createTwoBids() (gas: 93677)
NounsAuctionHouseV2WarmedUp_GasSnapshot:test_createOneBid() (gas: 34941)
NounsAuctionHouseV2WarmedUp_GasSnapshot:test_createTwoBids() (gas: 93379)
NounsAuctionHouseV2WarmedUp_GasSnapshot:test_settleCurrentAndCreateNewAuction() (gas: 232680)
NounsAuctionHouseV2_GasSnapshot:test_createOneBid() (gas: 35090)
NounsAuctionHouseV2_GasSnapshot:test_createTwoBids() (gas: 93677)
NounsAuctionHouseV2_GasSnapshot:test_createOneBid() (gas: 34941)
NounsAuctionHouseV2_GasSnapshot:test_createTwoBids() (gas: 93379)
NounsAuctionHouseV2_GasSnapshot:test_settleCurrentAndCreateNewAuction() (gas: 249780)
NounsAuctionHouseV2_HistoricPrices_GasSnapshot:test_prices_90() (gas: 385106)
NounsAuctionHouseV2_HistoricPrices_GasSnapshot:test_prices_range_90() (gas: 377540)
NounsAuctionHouse_GasSnapshot:test_createOneBid() (gas: 81474)
NounsAuctionHouse_GasSnapshot:test_createTwoBids() (gas: 142736)
NounsAuctionHouse_GasSnapshot:test_settleCurrentAndCreateNewAuction() (gas: 243008)
Expand Down
18 changes: 10 additions & 8 deletions packages/nouns-contracts/contracts/NounsAuctionHouseV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -368,17 +368,18 @@ contract NounsAuctionHouseV2 is
settlements = new Settlement[](auctionCount);
uint256 actualCount = 0;
while (actualCount < auctionCount && latestNounId > 0) {
SettlementState memory settlementState = settlementHistory[latestNounId];
// Skip Nouner reward Nouns, they have no price
// Also skips IDs with no price data
if (settlementHistory[latestNounId].winner == address(0)) {
if (settlementState.winner == address(0)) {
--latestNounId;
continue;
}

settlements[actualCount] = Settlement({
blockTimestamp: settlementHistory[latestNounId].blockTimestamp,
amount: uint64PriceToUint256(settlementHistory[latestNounId].amount),
winner: settlementHistory[latestNounId].winner,
blockTimestamp: settlementState.blockTimestamp,
amount: uint64PriceToUint256(settlementState.amount),
winner: settlementState.winner,
nounId: latestNounId
});
++actualCount;
Expand All @@ -404,17 +405,18 @@ contract NounsAuctionHouseV2 is
uint256 actualCount = 0;
uint256 currentId = startId;
while (currentId < endId) {
SettlementState memory settlementState = settlementHistory[currentId];
// Skip Nouner reward Nouns, they have no price
// Also skips IDs with no price data
if (settlementHistory[currentId].winner == address(0)) {
if (settlementState.winner == address(0)) {
++currentId;
continue;
}

settlements[actualCount] = Settlement({
blockTimestamp: settlementHistory[currentId].blockTimestamp,
amount: uint64PriceToUint256(settlementHistory[currentId].amount),
winner: settlementHistory[currentId].winner,
blockTimestamp: settlementState.blockTimestamp,
amount: uint64PriceToUint256(settlementState.amount),
winner: settlementState.winner,
nounId: currentId
});
++actualCount;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,7 @@ interface INounsAuctionHouseV2 {

function prices(uint256 auctionCount) external view returns (Settlement[] memory settlements);

function prices(uint256 startId, uint256 endId) external view returns (Settlement[] memory settlements);

function warmUpSettlementState(uint256[] calldata nounIds) external;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { AuctionHouseUpgrader } from './helpers/AuctionHouseUpgrader.sol';
import { NounsAuctionHouseProxy } from '../../contracts/proxies/NounsAuctionHouseProxy.sol';
import { NounsAuctionHouseProxyAdmin } from '../../contracts/proxies/NounsAuctionHouseProxyAdmin.sol';

contract NounsAuctionHouse_GasSnapshot is DeployUtils {
abstract contract NounsAuctionHouseBaseTest is DeployUtils {
INounsAuctionHouse auctionHouse;
INounsToken nouns;
address noundersDAO = makeAddr('noundersDAO');
Expand All @@ -31,7 +31,9 @@ contract NounsAuctionHouse_GasSnapshot is DeployUtils {
vm.prank(owner);
auctionHouse.unpause();
}
}

contract NounsAuctionHouse_GasSnapshot is NounsAuctionHouseBaseTest {
function test_createOneBid() public {
auctionHouse.createBid{ value: 1 ether }(1);
}
Expand Down Expand Up @@ -62,3 +64,39 @@ contract NounsAuctionHouseV2WarmedUp_GasSnapshot is NounsAuctionHouseV2_GasSnaps
INounsAuctionHouseV2(address(auctionHouse)).warmUpSettlementState(nounIds);
}
}

contract NounsAuctionHouseV2_HistoricPrices_GasSnapshot is NounsAuctionHouseBaseTest {
INounsAuctionHouseV2 auctionHouseV2;

function setUp() public virtual override {
super.setUp();
AuctionHouseUpgrader.upgradeAuctionHouse(owner, proxyAdmin, auctionHouseProxy);
auctionHouseV2 = INounsAuctionHouseV2(address(auctionHouse));

for (uint256 i = 1; i <= 200; ++i) {
address bidder = makeAddr(vm.toString(i));
bidAndWinCurrentAuction(bidder, i * 1e18);
}
}

function bidAndWinCurrentAuction(address bidder, uint256 bid) internal returns (uint256) {
uint128 nounId = auctionHouseV2.auction().nounId;
uint40 endTime = auctionHouseV2.auction().endTime;
vm.deal(bidder, bid);
vm.prank(bidder);
auctionHouseV2.createBid{ value: bid }(nounId);
vm.warp(endTime);
auctionHouseV2.settleCurrentAndCreateNewAuction();
return block.timestamp;
}

function test_prices_90() public {
INounsAuctionHouseV2.Settlement[] memory prices = auctionHouseV2.prices(90);
assertEq(prices.length, 90);
}

function test_prices_range_90() public {
INounsAuctionHouseV2.Settlement[] memory prices = auctionHouseV2.prices(1, 100);
assertEq(prices.length, 90);
}
}

0 comments on commit 3b7e1ee

Please sign in to comment.