> For the complete documentation index, see [llms.txt](https://clanker.gitbook.io/documentation/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://clanker.gitbook.io/documentation/references/core-contracts/v4/mev-modules/clankersniperauctionv0.md).

# ClankerSniperAuctionV0

**ClankerSniperAuctionV0** is a MEV module that lets snipers bid for the right to make the next swap on a freshly deployed pool, with the proceeds going mostly to the token's reward recipients.

## Overview

At deployment, up to 5 auctions can run per pool, one every 2 blocks. If an auction block passes without any valid bids, the auction is disabled and normal swaps can go through. The original design doc is on [HackMD](https://hackmd.io/@lobstermindset/rkwlyMpkgl).

These contracts are part of the setup:

* [`ClankerSniperAuctionV0.sol`](https://github.com/clanker-devco/v4-contracts/blob/main/src/mev-modules/ClankerSniperAuctionV0.sol): The `IClankerMevModule` instance running Clanker's first version of a sniper auction.
* [`ClankerSniperUtilV0.sol`](https://github.com/clanker-devco/v4-contracts/blob/main/src/mev-modules/sniper-utils/ClankerSniperUtilV0.sol): An auxiliary helper contract that shows how snipers can interact with the auction.

## How it works

The auction relies on Base using priority ordering, which places transactions with higher gas fees first. Each time the auction resets, the module records the gas price and uses an inflated version as the auction's starting gas price. The WETH payment is a multiple of the difference between the winning transaction's gas price and the inflated gas price. The inflated gas price, next auction block and current auction round can be read from the contract and are easy to replicate off-chain.

Auction payments are split 80/20 between the token's reward recipients and the Clanker factory.

### Auction configuration

These auction configuration variables matter to snipers:

| Variable                                 | Description                                                               |
| ---------------------------------------- | ------------------------------------------------------------------------- |
| `blocksBetweenDeploymentAndFirstAuction` | The number of blocks between the pool's deployment and the first auction. |
| `blocksBetweenAuction`                   | The number of blocks between subsequent auctions.                         |
| `maxRounds`                              | The maximum number of auctions that can run per pool.                     |
| `paymentPerGasUnit`                      | The amount of WETH to pay per gas unit above the gas peg for an auction.  |

All of these can be queried on the `ClankerSniperAuctionV0` contract, and the Clanker team can change them with advance notice.

### Auction state

These auction state variables matter to snipers and can be queried per `PoolId`:

| Variable           | Description                                              |
| ------------------ | -------------------------------------------------------- |
| `round`            | The current auction round.                               |
| `nextAuctionBlock` | The block number in which the next auction accepts bids. |
| `gasPeg`           | The gas price the next auction round starts at.          |

### Auction events

These events matter to snipers and are emitted by the `ClankerSniperAuctionV0` contract:

```solidity
// emitted when a new auction is initialized with the first round's parameters
event AuctionInitialized(
    PoolId indexed poolId, uint256 gasPeg, uint256 indexed auctionBlock, uint256 round
);
// emitted when an auction is reset, with the next round's parameters
event AuctionReset(
    PoolId indexed poolId, uint256 gasPeg, uint256 indexed auctionBlock, uint256 round
);
// emitted when a sniper wins an auction
event AuctionWon(PoolId indexed poolId, address indexed payee, uint256 paymentAmount, uint256 round);

// emitted when the gas payment expectation is updated
// note: advanced warning will be given, will not be updated often
event SetPaymentPerGasUnit(uint256 oldPaymentPerGasUnit, uint256 newPaymentPerGasUnit);
```

## Example auction round

A pool is deployed in block 1 with a base fee of 10 gwei. The first auction round happens in block 3 with a starting auction gas price of 12 gwei. Three snipers try to snipe:

* **Sniper A** sets their transaction gas price to 13 gwei, approves a WETH payment of 0.0001 ether and lands in block 3.
* **Sniper B** sets their transaction gas price to 15 gwei, approves a WETH payment of 0.0003 ether and lands in block 3.
* **Sniper C** sets their transaction gas price to 17 gwei, approves a WETH payment of 0.0005 ether and lands in block 4.

**Result:** Sniper B wins, because Sniper C landed in the wrong block. The auction module uses block 3's actual gas price of 11 gwei to set round 2's starting gas price to 13 gwei, with bids accepted in block 5.

The first auction parameters set by the Clanker team put 2 blocks between deployment and the first auction, and 2 blocks between subsequent auctions. These may change as Clanker gathers more data on the auction's performance.

## ClankerSniperUtilV0 contract

The `ClankerSniperUtilV0` contract is a shared utility for snipers to bid in auctions, and a user-facing example of how bidding works. It handles approval spending, round management and encoding the auction swap's hook data. Snipers are welcome to rewrite and redeploy the utility themselves, since it's auxiliary to the auction.

### Example usage

```solidity
// addresses of the sniper util and auction
ClankerSniperAuctionV0 sniperAuction;
ClankerSniperUtilV0 sniperUtil;

// addresses of the deployed clanker token, paired token, and associated poolKey
// (can be found on the TokenCreated event)
address clankerToken;
PoolKey memory clankerPoolKey;
address pairedToken;

// amount of the input token to swap
uint256 amountIn = AMOUNT_TOKEN_TO_SWAP;
// amount of eth to pay as bid (needs to be multiple of IClankerSniperAuctionV0.PAYMENT_PER_GAS_UNIT())
uint256 bidAmount = AMOUNT_TO_BID;

// approve the sniper util to pull in the swap's input token
IERC20(pairedToken).approve(address(sniperUtil), amountIn);

// build the desired swap to run if the bid is winning
IV4Router.ExactInputSingleParams memory swapParams = IV4Router.ExactInputSingleParams({
    poolKey: poolKey,
    amountIn: uint128(amountIn),
    amountOutMinimum: 0,
    zeroForOne: pairedToken > clankerToken,
    // util will fill out, needs to be address to pull WETH bid payment from
    hookData: abi.encode("")
});

// get token's gas peg
uint256 gasPeg = sniperAuction.gasPeg(poolKey.toId());
// get next auction block
uint256 nextAuctionBlock = sniperAuction.nextAuctionBlock(poolKey.toId());
// get current round
uint256 round = sniperAuction.round(poolKey.toId());

// determine gas price that needs to be used to match desired payment amount
uint256 txGasPrice = sniperUtil.getTxGasPriceForBidAmount(gasPeg, bidAmount);

// bid in auction
// NOTE: you need to land this in the correct block with the correct gas price.
// This is the hardest part of using the auction, because transactions on Base
// can't specify which block they land in.
sniperUtil.bidInAuction{value: bidAmount}(swapParams, round);
```

## Approving `ClankerSniperAuctionV0` to spend WETH

{% hint style="danger" %}
Clanker does not recommend approving `ClankerSniperAuctionV0` to spend WETH unless an auxiliary contract handles the approval and reverts if the bid doesn't win. For example, if an EOA approves the auction contract to spend WETH, anyone can spend that EOA's WETH by passing the EOA's address as the `auctionData` parameter to `ClankerSniperAuctionV0::beforeSwap`. The `ClankerSniperUtilV0` contract shows how to handle this safely.
{% endhint %}

## Disclaimer

Clanker reserves the right to upgrade to other auction versions depending on how V0 is used, and to change V0's configuration. Clanker gives advance warning of upgrades, but may change the number of blocks between auctions and the payment multiplier.
