From a4ec8e435c647aacb691912de5af24c9af496b90 Mon Sep 17 00:00:00 2001 From: Aditya Kumar Singh Date: Sat, 29 Oct 2022 14:30:03 +0530 Subject: [PATCH 1/3] Update DutchAuction.sol Optimized the smart contract using the custom error with revert statements. This significantly reduces the gas cost in smart contract. --- contracts/Auction/DutchAuction.sol | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/contracts/Auction/DutchAuction.sol b/contracts/Auction/DutchAuction.sol index 00acedd..fda1139 100644 --- a/contracts/Auction/DutchAuction.sol +++ b/contracts/Auction/DutchAuction.sol @@ -10,6 +10,10 @@ interface IERC721 { ) external; } +error AuctionExpired(); +error AuctionFinished(); +InsufficientValue(uint256 receivedAmount, uint256 price requiredAmount); + contract DutchAuction { event Buy(address winner, uint256 amount); @@ -41,15 +45,21 @@ contract DutchAuction { function buy() external payable { // Check for the timestamp before buy - require(block.timestamp < expiresAt, "auction expired"); + if(block.timestamp >= expiresAt){ + revert AuctionExpired(); + } - require(winner == address(0), "auction finished"); + if(winner != address(0)) { + revert AuctionFinished(); + } uint256 timeElapsed = block.timestamp - startAt; uint256 deduction = priceDeductionRate * timeElapsed; uint256 price = startingPrice - deduction; - require(msg.value >= price, "ETH < price"); + if(msg.value < price){ + revert InsufficientValue(msg.value, price); + } winner = msg.sender; nft.transferFrom(seller, msg.sender, nftId); From 0eccf41ee87d3039925119e8f229800013e82de3 Mon Sep 17 00:00:00 2001 From: Aditya Kumar Singh Date: Sat, 29 Oct 2022 14:42:07 +0530 Subject: [PATCH 2/3] Update DutchAuction.sol fixed breaking changes in the contract --- contracts/Auction/DutchAuction.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/Auction/DutchAuction.sol b/contracts/Auction/DutchAuction.sol index fda1139..683c32b 100644 --- a/contracts/Auction/DutchAuction.sol +++ b/contracts/Auction/DutchAuction.sol @@ -12,7 +12,7 @@ interface IERC721 { error AuctionExpired(); error AuctionFinished(); -InsufficientValue(uint256 receivedAmount, uint256 price requiredAmount); +error InsufficientValue(uint256 receivedAmount, uint256 price requiredAmount); contract DutchAuction { event Buy(address winner, uint256 amount); From 201bdee3e89f274452ca522511d36f24757f9ea3 Mon Sep 17 00:00:00 2001 From: Aditya Kumar Singh Date: Sat, 29 Oct 2022 14:44:00 +0530 Subject: [PATCH 3/3] fixed breaking changes --- contracts/Auction/DutchAuction.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/Auction/DutchAuction.sol b/contracts/Auction/DutchAuction.sol index 683c32b..18f5906 100644 --- a/contracts/Auction/DutchAuction.sol +++ b/contracts/Auction/DutchAuction.sol @@ -12,7 +12,7 @@ interface IERC721 { error AuctionExpired(); error AuctionFinished(); -error InsufficientValue(uint256 receivedAmount, uint256 price requiredAmount); +error InsufficientValue(uint256 receivedAmount, uint256 requiredAmount); contract DutchAuction { event Buy(address winner, uint256 amount);