Skip to main content

Cancel Bid

This guide will provide steps for cancelling a bid on a batch auction through direct integration with the contracts. This guide is designed to work with the EncryptedMarginalPrice auction format.

Setup

First, define the BatchAuctionHouse that will be used. This should correspond to the same BatchAuctionHouse in which the auction lot was created.

// Define the deployed BatchAuctionHouse
IBatchAuctionHouse auctionHouse = IBatchAuctionHouse(_batchAuctionHouse);

Inputs

We obtain from the LOT_ID environment variable the lot ID that identifies each auction lot. The lot ID is a uint96 number, so should be checked before being downcast.

// Obtain the lot from the environment variable
uint256 lotIdRaw = vm.envUint("LOT_ID");
if (lotIdRaw > type(uint96).max) {
revert("LOT_ID must be less than uint96 max");
}
uint96 lotId = uint96(lotIdRaw);

The lot ID is returned from the auction creation function. See the Create Auction Guide for more details.

We then obtain from the BID_ID environment variable the bid ID that identifies a single bid in an auction lot. The bid ID is a uint64 number, so should be checked before being downcast.

// Obtain the bid from the environment variable
uint64 bidId;
{
uint256 bidIdRaw = vm.envUint("BID_ID");
if (bidIdRaw > type(uint64).max) {
revert("BID_ID must be less than uint64 max");
}
bidId = uint64(bidIdRaw);
}

The bid ID is returned from the bid submission function. See the Submit Bid Guide for more details.

The last step in preparation for cancelling the bid is to determine the index of the bid. Determining the index of the bid on-chain would be expensive, so it is shifted off-chain. The code snippet iterates over bid ids to determine the appropriate index.

// Get the auction module
IBatchAuction empModule = IBatchAuction(address(auctionHouse.getModuleForId(lotId)));

// Determine the index of the bid
uint256 bidCount = empModule.getNumBids(lotId);
uint256 bidIndex;
bool bidIndexFound;
for (uint256 i = 0; i < bidCount; i++) {
if (empModule.getBidIdAtIndex(lotId, i) == bidId) {
bidIndex = i;
bidIndexFound = true;
break;
}
}
if (bidIndexFound == false) {
revert("Bid not found");
}

Contract Call

Lastly, we call the function to cancel the bid and obtain a refund. This must be performed as the bidder that was recorded on the original bid.

// Conduct the cancellation as the bidder
address bidder = address(0x10);

vm.prank(bidder);
auctionHouse.refundBid(lotId, bidId, bidIndex);

It is worthwhile noting that the refund may fail for the following reasons:

  • The caller is not the bidder
  • The lot ID or bid ID are invalid
  • The state of the auction lot does not allow for bid cancellation

Source Code

The source code for the guide is located in the bid-cancel.s.sol file.