Skip to main content

AmAmm

AmAmm is an implementation of the Auction-Managed Automated Market Maker (am-AMM) system.

In Bunni v2 the AmAmm contract is inherited by BunniHook, so function calls should be made on the BunniHook contract. The token used to pay for rent is the LP token of the specific Bunni v2 pool.

The source code for AmAmm can be found in the Biddog repo.

Structs

Bid

struct Bid {
address manager;
uint40 epoch;
bytes7 payload;
uint128 rent;
uint128 deposit;
}

Represents a bid in the am-AMM system.

FieldTypeDescription
manageraddressThe address of the bid manager
epochuint40The epoch when the bid was created or last charged rent
payloadbytes7Payload specifying parameters the manager wants (e.g., swap fee)
rentuint128Rent per hour
deposituint128Rent deposit amount

BunniHook Payload Format

The BunniHook bid payload is a bytes7 value that encodes three parameters used to configure pool behavior.

Payload Encoding

The bytes7 payload is encoded as follows:

| swapFee0For1 (3 bytes) | swapFee1For0 (3 bytes) | enableSurgeFee (1 byte) |
  • swapFee0For1 (3 bytes): A uint24 value representing the swap fee for token0 -> token1 swaps. 6 decimals.
  • swapFee1For0 (3 bytes): A uint24 value representing the swap fee for token1 -> token0 swaps. 6 decimals.
  • enableSurgeFee (1 byte): A uint8 value (0 or 1) indicating whether the surge fee is enabled.

Important Notes

  1. The swap fees are represented as uint24 values, allowing for a wide range of fee settings.
  2. The surge fee is enabled if the last byte of the payload is non-zero.
  3. When encoding the payload, ensure that the swap fee values do not exceed the maximum value for swap fees (1e6).

Example Usage

Here's an example of how to encode/decode the payload:

function encodeAmAmmPayload(
uint24 swapFee0For1,
uint24 swapFee1For0,
bool enableSurgeFee
) pure returns (bytes7) {
return bytes7(
abi.encodePacked(swapFee0For1, swapFee1For0, enableSurgeFee)
);
}

function decodeAmAmmPayload(bytes7 payload)
pure
returns (uint24 swapFee0For1, uint24 swapFee1For0, bool enableSurgeFee)
{
swapFee0For1 = uint24(bytes3(payload));
swapFee1For0 = uint24(bytes3(payload << 24));
enableSurgeFee = uint8(payload[6]) != 0;
}

Functions

bid

function bid(
PoolId id,
address manager,
bytes7 payload,
uint128 rent,
uint128 deposit
) external;

Places a bid to become the manager of a pool.

Will transfer deposit LP tokens of the Bunni v2 pool specified by id from msg.sender, so ERC-20 approval should have been given prior to calling this function.

Parameters:

NameTypeDescription
idPoolIdThe unique identifier of the pool
manageraddressThe address of the account that will manage the pool if bid wins
payloadbytes7Encoded data specifying parameters the manager wants (e.g., swap fee)
rentuint128The amount of rent per hour the manager is willing to pay
deposituint128The initial deposit amount, must be a multiple of rent and cover rent for ≥K epochs

depositIntoTopBid

function depositIntoTopBid(PoolId id, uint128 amount) external;

Adds deposit to the top bid. Only callable by the current top bid manager.

Will transfer amount LP tokens of the Bunni v2 pool specified by id from msg.sender, so ERC-20 approval should have been given prior to calling this function.

Parameters:

NameTypeDescription
idPoolIdThe unique identifier of the pool
amountuint128The amount to deposit, must be a multiple of rent

withdrawFromTopBid

function withdrawFromTopBid(
PoolId id,
uint128 amount,
address recipient
) external;

Withdraws from the deposit of the top bid. Only callable by the current top bid manager. Reverts if deposit / rent < K after the function returns

Parameters:

NameTypeDescription
idPoolIdThe unique identifier of the pool
amountuint128The amount to withdraw, must be a multiple of rent and leave deposit / rent ≥ K
recipientaddressThe address that will receive the withdrawn amount

depositIntoNextBid

function depositIntoNextBid(PoolId id, uint128 amount) external;

Adds deposit to the next bid. Only callable by the next bid manager.

Will transfer amount LP tokens of the Bunni v2 pool specified by id from msg.sender, so ERC-20 approval should have been given prior to calling this function.

Parameters:

NameTypeDescription
idPoolIdThe unique identifier of the pool
amountuint128The amount to deposit, must be a multiple of rent

withdrawFromNextBid

function withdrawFromNextBid(
PoolId id,
uint128 amount,
address recipient
) external;

Withdraws from the deposit of the next bid. Only callable by the next bid manager. Reverts if deposit / rent < K after the function returns.

Parameters:

NameTypeDescription
idPoolIdThe unique identifier of the pool
amountuint128The amount to withdraw, must be a multiple of rent and leave deposit / rent ≥ K
recipientaddressThe address that will receive the withdrawn amount

cancelNextBid

function cancelNextBid(PoolId id, address recipient) external returns (uint256 refund);

Cancels the next bid. Only callable by the next bid manager.

Parameters:

NameTypeDescription
idPoolIdThe unique identifier of the pool
recipientaddressThe address that will receive the refunded deposit

Returns:

TypeDescription
uint256The amount of deposit refunded

claimRefund

function claimRefund(PoolId id, address recipient) external returns (uint256 refund);

Claims the refundable deposit of a pool owed to the caller.

Parameters:

NameTypeDescription
idPoolIdThe unique identifier of the pool
recipientaddressThe address that will receive the refund

Returns:

TypeDescription
uint256The amount of refund claimed

claimFees

function claimFees(Currency currency, address recipient) external returns (uint256 fees);

Claims the accrued fees of the caller for a specific currency.

Parameters:

NameTypeDescription
currencyCurrencyThe currency of the fees to be claimed
recipientaddressThe address that will receive the fees

Returns:

TypeDescription
uint256The amount of fees claimed

setBidPayload

function setBidPayload(PoolId id, bytes7 payload, bool topBid) external;

Sets the payload of a pool. Only callable by the manager of either the top bid or the next bid.

Parameters:

NameTypeDescription
idPoolIdThe unique identifier of the pool
payloadbytes7The new payload specifying parameters (e.g., swap fee)
topBidboolTrue if setting for the top bid, false if setting for the next bid

getTopBid

function getTopBid(PoolId id) external view returns (Bid memory);

Retrieves the current top bid for a pool.

Parameters:

NameTypeDescription
idPoolIdThe unique identifier of the pool

Returns:

TypeDescription
BidA struct containing the details of the top bid

getTopBidWrite

function getTopBidWrite(PoolId id) external returns (Bid memory);

Updates the am-AMM state of a pool and then retrieves the top bid.

Parameters:

NameTypeDescription
idPoolIdThe unique identifier of the pool

Returns:

TypeDescription
BidA struct containing the details of the top bid

getNextBid

function getNextBid(PoolId id) external view returns (Bid memory);

Retrieves the current next bid for a pool.

Parameters:

NameTypeDescription
idPoolIdThe unique identifier of the pool

Returns:

TypeDescription
BidA struct containing the details of the next bid

getNextBidWrite

function getNextBidWrite(PoolId id) external returns (Bid memory);

Updates the am-AMM state of a pool and then retrieves the next bid.

Parameters:

NameTypeDescription
idPoolIdThe unique identifier of the pool

Returns:

TypeDescription
BidA struct containing the details of the next bid

getRefund

function getRefund(address manager, PoolId id) external view returns (uint256);

Retrieves the refundable deposit amount for a specific manager in a pool.

Parameters:

NameTypeDescription
manageraddressThe address of the manager
idPoolIdThe unique identifier of the pool

Returns:

TypeDescription
uint256The amount of refundable deposit

getRefundWrite

function getRefundWrite(address manager, PoolId id) external returns (uint256);

Updates the am-AMM state of a pool and then retrieves the refundable deposit amount for a specific manager.

Parameters:

NameTypeDescription
manageraddressThe address of the manager
idPoolIdThe unique identifier of the pool

Returns:

TypeDescription
uint256The amount of refundable deposit

getFees

function getFees(address manager, Currency currency) external view returns (uint256);

Retrieves the amount of fees accrued by a manager for a specific currency.

Parameters:

NameTypeDescription
manageraddressThe address of the manager
currencyCurrencyThe currency of the fees

Returns:

TypeDescription
uint256The amount of accrued fees

updateStateMachine

function updateStateMachine(PoolId id) external;

Triggers a state machine update for the given pool.

Parameters:

NameTypeDescription
idPoolIdThe unique identifier of the pool

Events

SubmitBid

event SubmitBid(
PoolId indexed id,
address indexed manager,
uint40 indexed epoch,
bytes7 payload,
uint128 rent,
uint128 deposit
)

Emitted when a new bid is submitted.

Parameters:

NameTypeIndexedDescription
idPoolIdYesThe ID of the pool
manageraddressYesThe address of the manager submitting the bid
epochuint40YesThe epoch when the bid was submitted
payloadbytes7NoThe payload specifying bid parameters
rentuint128NoThe rent amount per hour
deposituint128NoThe deposit amount for the bid

DepositIntoTopBid

event DepositIntoTopBid(PoolId indexed id, address indexed manager, uint128 amount)

Emitted when a deposit is made into the top bid.

Parameters:

NameTypeIndexedDescription
idPoolIdYesThe ID of the pool
manageraddressYesThe address of the manager making the deposit
amountuint128NoThe amount deposited

WithdrawFromTopBid

event WithdrawFromTopBid(PoolId indexed id, address indexed manager, address indexed recipient, uint128 amount)

Emitted when a withdrawal is made from the top bid.

Parameters:

NameTypeIndexedDescription
idPoolIdYesThe ID of the pool
manageraddressYesThe address of the manager making the withdrawal
recipientaddressYesThe address receiving the withdrawn amount
amountuint128NoThe amount withdrawn

DepositIntoNextBid

event DepositIntoNextBid(PoolId indexed id, address indexed manager, uint128 amount)

Emitted when a deposit is made into the next bid.

Parameters:

NameTypeIndexedDescription
idPoolIdYesThe ID of the pool
manageraddressYesThe address of the manager making the deposit
amountuint128NoThe amount deposited

WithdrawFromNextBid

event WithdrawFromNextBid(PoolId indexed id, address indexed manager, address indexed recipient, uint128 amount)

Emitted when a withdrawal is made from the next bid.

Parameters:

NameTypeIndexedDescription
idPoolIdYesThe ID of the pool
manageraddressYesThe address of the manager making the withdrawal
recipientaddressYesThe address receiving the withdrawn amount
amountuint128NoThe amount withdrawn

CancelNextBid

event CancelNextBid(PoolId indexed id, address indexed manager, address indexed recipient, uint256 refund)

Emitted when the next bid is cancelled.

Parameters:

NameTypeIndexedDescription
idPoolIdYesThe ID of the pool
manageraddressYesThe address of the manager cancelling the bid
recipientaddressYesThe address receiving the refund
refunduint256NoThe amount refunded

ClaimRefund

event ClaimRefund(PoolId indexed id, address indexed manager, address indexed recipient, uint256 refund)

Emitted when a refund is claimed.

Parameters:

NameTypeIndexedDescription
idPoolIdYesThe ID of the pool
manageraddressYesThe address of the manager claiming the refund
recipientaddressYesThe address receiving the refund
refunduint256NoThe amount refunded

ClaimFees

event ClaimFees(Currency indexed currency, address indexed manager, address indexed recipient, uint256 fees)

Emitted when fees are claimed.

Parameters:

NameTypeIndexedDescription
currencyCurrencyYesThe currency of the claimed fees
manageraddressYesThe address of the manager claiming the fees
recipientaddressYesThe address receiving the fees
feesuint256NoThe amount of fees claimed

SetBidPayload

event SetBidPayload(PoolId indexed id, address indexed manager, bytes7 payload, bool topBid)

Emitted when a bid payload is set.

Parameters:

NameTypeIndexedDescription
idPoolIdYesThe ID of the pool
manageraddressYesThe address of the manager setting the payload
payloadbytes7NoThe new payload
topBidboolNoWhether this is for the top bid or next bid

Errors

The interface defines several custom errors:

  • AmAmm__BidLocked()
  • AmAmm__InvalidBid()
  • AmAmm__NotEnabled()
  • AmAmm__Unauthorized()
  • AmAmm__InvalidDepositAmount()

These errors are used to provide more specific information about failures in the am-AMM system.