Skip to content

Commit

Permalink
noracle: add function to return only prices
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrai committed Oct 6, 2023
1 parent 3b7e1ee commit 3b536b0
Show file tree
Hide file tree
Showing 5 changed files with 256 additions and 117 deletions.
18 changes: 10 additions & 8 deletions packages/nouns-contracts/.gas-snapshot
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
NounsAuctionHouseV2WarmedUp_GasSnapshot:test_createOneBid() (gas: 34941)
NounsAuctionHouseV2WarmedUp_GasSnapshot:test_createTwoBids() (gas: 93379)
NounsAuctionHouseV2WarmedUp_GasSnapshot:test_settleCurrentAndCreateNewAuction() (gas: 232680)
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)
NounsAuctionHouseV2WarmedUp_GasSnapshot:test_createOneBid() (gas: 34963)
NounsAuctionHouseV2WarmedUp_GasSnapshot:test_createTwoBids() (gas: 93423)
NounsAuctionHouseV2WarmedUp_GasSnapshot:test_settleCurrentAndCreateNewAuction() (gas: 232686)
NounsAuctionHouseV2_GasSnapshot:test_createOneBid() (gas: 34963)
NounsAuctionHouseV2_GasSnapshot:test_createTwoBids() (gas: 93423)
NounsAuctionHouseV2_GasSnapshot:test_settleCurrentAndCreateNewAuction() (gas: 249786)
NounsAuctionHouseV2_HistoricPrices_GasSnapshot:test_getPrices_90() (gas: 310063)
NounsAuctionHouseV2_HistoricPrices_GasSnapshot:test_getPrices_range_90() (gas: 300578)
NounsAuctionHouseV2_HistoricPrices_GasSnapshot:test_getSettlements_90() (gas: 385762)
NounsAuctionHouseV2_HistoricPrices_GasSnapshot:test_getSettlements_range_90() (gas: 378168)
NounsAuctionHouse_GasSnapshot:test_createOneBid() (gas: 81474)
NounsAuctionHouse_GasSnapshot:test_createTwoBids() (gas: 142736)
NounsAuctionHouse_GasSnapshot:test_settleCurrentAndCreateNewAuction() (gas: 243008)
Expand Down
85 changes: 78 additions & 7 deletions packages/nouns-contracts/contracts/NounsAuctionHouseV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -353,13 +353,13 @@ contract NounsAuctionHouseV2 is
}

/**
* @notice Get past auction prices.
* @dev Returns prices in reverse order, meaning settlements[0] will be the most recent auction price.
* @notice Get past auction settlements.
* @dev Returns settlements in reverse order, meaning settlements[0] will be the most recent auction price.
* @param auctionCount The number of price observations to get.
* @return settlements An array of type `Settlement`, where each Settlement includes a timestamp,
* the Noun ID of that auction, the winning bid amount, and the winner's addreess.
* the Noun ID of that auction, the winning bid amount, and the winner's address.
*/
function prices(uint256 auctionCount) external view returns (Settlement[] memory settlements) {
function getSettlements(uint256 auctionCount) external view returns (Settlement[] memory settlements) {
uint256 latestNounId = auctionStorage.nounId;
if (!auctionStorage.settled && latestNounId > 0) {
latestNounId -= 1;
Expand Down Expand Up @@ -395,12 +395,50 @@ contract NounsAuctionHouseV2 is
}

/**
* @notice Get a range of past auction prices.
* @dev Returns prices in chronological order, as opposed to `prices(count)` which returns prices in reverse order.
* @notice Get past auction prices.
* @dev Returns prices in reverse order, meaning prices[0] will be the most recent auction price.
* @param auctionCount The number of price observations to get.
* @return prices An array of uint256 prices.
*/
function getPrices(uint256 auctionCount) external view returns (uint256[] memory prices) {
uint256 latestNounId = auctionStorage.nounId;
if (!auctionStorage.settled && latestNounId > 0) {
latestNounId -= 1;
}

prices = new uint256[](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 (settlementState.winner == address(0)) {
--latestNounId;
continue;
}

prices[actualCount] = uint64PriceToUint256(settlementState.amount);
++actualCount;
--latestNounId;
}

if (auctionCount > actualCount) {
// this assembly trims the observations array, getting rid of unused cells
assembly {
mstore(prices, actualCount)
}
}
}

/**
* @notice Get a range of past auction settlements.
* @dev Returns prices in chronological order, as opposed to `getSettlements(count)` which returns prices in reverse order.
* @param startId the first Noun ID to get prices for.
* @param endId end Noun ID (up to, but not including).
* @return settlements An array of type `Settlement`, where each Settlement includes a timestamp,
* the Noun ID of that auction, the winning bid amount, and the winner's address.
*/
function prices(uint256 startId, uint256 endId) external view returns (Settlement[] memory settlements) {
function getSettlements(uint256 startId, uint256 endId) external view returns (Settlement[] memory settlements) {
settlements = new Settlement[](endId - startId);
uint256 actualCount = 0;
uint256 currentId = startId;
Expand Down Expand Up @@ -431,6 +469,39 @@ contract NounsAuctionHouseV2 is
}
}

/**
* @notice Get a range of past auction prices.
* @dev Returns prices in chronological order, as opposed to `getPrices(count)` which returns prices in reverse order.
* @param startId the first Noun ID to get prices for.
* @param endId end Noun ID (up to, but not including).
* @return prices An array of uint256 prices.
*/
function getPrices(uint256 startId, uint256 endId) external view returns (uint256[] memory prices) {
prices = new uint256[](endId - startId);
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 (settlementState.winner == address(0)) {
++currentId;
continue;
}

prices[actualCount] = uint64PriceToUint256(settlementState.amount);
++actualCount;
++currentId;
}

if (prices.length > actualCount) {
// this assembly trims the observations array, getting rid of unused cells
assembly {
mstore(prices, actualCount)
}
}
}

/**
* @dev Convert an ETH price of 256 bits with 18 decimals, to 64 bits with 10 decimals.
* Max supported value is 1844674407.3709551615 ETH.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,13 @@ interface INounsAuctionHouseV2 {

function auction() external view returns (AuctionV2 memory);

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

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

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

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

function warmUpSettlementState(uint256[] calldata nounIds) external;
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,23 @@ contract NounsAuctionHouseV2_HistoricPrices_GasSnapshot is NounsAuctionHouseBase
return block.timestamp;
}

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

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

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

function test_getPrices_range_90() public {
uint256[] memory prices = auctionHouseV2.getPrices(1, 100);
assertEq(prices.length, 90);
}
}
Loading

0 comments on commit 3b536b0

Please sign in to comment.