Skip to content
LogoLogo

AsyncMatching

Git Source

Linked matching module operating on the hook's order and radix-book storage.

Public library calls use DELEGATECALL, so ERC-6909 escrow remains owned by Async Swap.

Constants

MAX_MATCHES_PER_CALL

uint8 public constant MAX_MATCHES_PER_CALL = 8;

POOL_LIQUIDITY_FILL_FEE

LP fee on a pool-liquidity fill, in v4 pips: 0.02%.

uint24 private constant POOL_LIQUIDITY_FILL_FEE = 200;

Functions

bestOrder

function bestOrder(AsyncFiller.State storage self, AsyncFiller.Book storage book, bool isBuy)
        public
        view
        returns (bool found, uint128 price, bytes32 orderId, address owner);

Parameters

NameTypeDescription
selfAsyncFiller.State
bookAsyncFiller.Book
isBuybool

Returns

NameTypeDescription
foundbool
priceuint128
orderIdbytes32
owneraddress

fill

function fill(AsyncFiller.State storage self, AsyncFiller.Book storage book, FillParams memory params) public;

Parameters

NameTypeDescription
selfAsyncFiller.State
bookAsyncFiller.Book
paramsFillParams

cancel

function cancel(AsyncFiller.State storage self, AsyncFiller.Book storage book, CancelParams memory params) public;

Parameters

NameTypeDescription
selfAsyncFiller.State
bookAsyncFiller.Book
paramsCancelParams

updatePrice

function updatePrice(AsyncFiller.State storage self, AsyncFiller.Book storage book, UpdateParams memory params)
        public;

Parameters

NameTypeDescription
selfAsyncFiller.State
bookAsyncFiller.Book
paramsUpdateParams

matchIncoming

function matchIncoming(
        AsyncFiller.State storage self,
        AsyncFiller.Book storage book,
        PoolKey calldata key,
        bytes32 incomingId
    ) public returns (uint8 matched);

Parameters

NameTypeDescription
selfAsyncFiller.State
bookAsyncFiller.Book
keyPoolKey
incomingIdbytes32

Returns

NameTypeDescription
matcheduint8

matchBook

function matchBook(
        AsyncFiller.State storage self,
        AsyncFiller.Book storage book,
        PoolKey calldata key,
        uint8 maxMatches
    ) public returns (uint8 matched);

Parameters

NameTypeDescription
selfAsyncFiller.State
bookAsyncFiller.Book
keyPoolKey
maxMatchesuint8

Returns

NameTypeDescription
matcheduint8

_bookPair

function _bookPair(AsyncFiller.State storage self, AsyncFiller.Book storage book)
        private
        view
        returns (bytes32 bidId, bytes32 askId);

Parameters

NameTypeDescription
selfAsyncFiller.State
bookAsyncFiller.Book

Returns

NameTypeDescription
bidIdbytes32
askIdbytes32

fillFromPoolLiquidity

Execute an order against active v4 LP positions if they meet the signed minimum.

Reverts atomically on insufficient liquidity or output; the caller catches this across a self-call so the resting order is preserved. Without a price limit the whole order must fill. With one (the best book level, when the pool goes first) the swap may stop there: the filled part must meet the maker's limit pro rata, and the rest stays on the book with the maker's own limit for the book to match.

function fillFromPoolLiquidity(
        AsyncFiller.State storage self,
        AsyncFiller.Book storage book,
        PoolKey calldata key,
        bytes32 id,
        address hook,
        uint160 sqrtPriceLimitX96
    ) public;

Parameters

NameTypeDescription
selfAsyncFiller.State
bookAsyncFiller.Book
keyPoolKey
idbytes32
hookaddress
sqrtPriceLimitX96uint160

_reduceAfterPoolFill

function _reduceAfterPoolFill(
        AsyncFiller.State storage self,
        AsyncFiller.Book storage book,
        PoolKey calldata key,
        bytes32 id,
        uint256 consumed
    ) private;

Parameters

NameTypeDescription
selfAsyncFiller.State
bookAsyncFiller.Book
keyPoolKey
idbytes32
consumeduint256

closeBelowMinimum

Close a live order left below the pool minimum and return its input as claims; its unfilled priority bid is spent, as for a book match that leaves dust.

function closeBelowMinimum(
        AsyncFiller.State storage self,
        AsyncFiller.Book storage book,
        PoolKey calldata key,
        bytes32 id
    ) public;

Parameters

NameTypeDescription
selfAsyncFiller.State
bookAsyncFiller.Book
keyPoolKey
idbytes32

_poolFillNet

Swap the order's input less the protocol's 0.01% (rounded up, paid to the vault), then take the dynamic fee (DynamicFeeOrdering) from the output. A swap stopped short at sqrtPriceLimitX96 pays the 0.01% on what it swapped. Returns the input swapped, the input used including the 0.01%, and the net output, reverting unless the output meets the maker's limit on the input used.

function _poolFillNet(
        AsyncFiller.State storage self,
        PoolKey calldata key,
        bytes32 id,
        address hook,
        uint160 sqrtPriceLimitX96
    ) private returns (uint256 swapped, uint256 consumed, uint256 amountOut);

Parameters

NameTypeDescription
selfAsyncFiller.State
keyPoolKey
idbytes32
hookaddress
sqrtPriceLimitX96uint160

Returns

NameTypeDescription
swappeduint256
consumeduint256
amountOutuint256

_chargeFees

Pay the 0.01% from the input's escrow to the vault and charge the dynamic fee on the output; returns the dynamic fee.

function _chargeFees(
        AsyncFiller.State storage self,
        PoolKey calldata key,
        bytes32 id,
        uint256 protocolFee,
        int24 tickBefore,
        uint256 amountOut
    ) private returns (uint256);

Parameters

NameTypeDescription
selfAsyncFiller.State
keyPoolKey
idbytes32
protocolFeeuint256
tickBeforeint24
amountOutuint256

Returns

NameTypeDescription
<none>uint256

_swapAgainstPool

function _swapAgainstPool(
        IPoolManager manager,
        PoolKey calldata key,
        bool zeroForOne,
        uint256 amountIn,
        address hook,
        uint160 sqrtPriceLimitX96
    ) private returns (uint256 swapped, uint256 amountOut, int24 tickBefore);

Parameters

NameTypeDescription
managerIPoolManager
keyPoolKey
zeroForOnebool
amountInuint256
hookaddress
sqrtPriceLimitX96uint160

Returns

NameTypeDescription
swappeduint256
amountOutuint256
tickBeforeint24

_matchPair

buyId comes from the bid tree and sellId from the ask tree (_bookPair), so their sides are fixed by construction.

function _matchPair(
        AsyncFiller.State storage self,
        AsyncFiller.Book storage book,
        PoolKey calldata key,
        bytes32 buyId,
        bytes32 sellId,
        ClaimTransfers.Batch memory batch
    ) internal returns (bool);

Parameters

NameTypeDescription
selfAsyncFiller.State
bookAsyncFiller.Book
keyPoolKey
buyIdbytes32
sellIdbytes32
batchClaimTransfers.Batch

Returns

NameTypeDescription
<none>bool

_matchAmounts

function _matchAmounts(AsyncFiller.OrderInfo memory sell, AsyncFiller.OrderInfo memory buy, bool restingBuy)
        internal
        pure
        returns (AsyncFiller.MatchAmounts memory amounts);

Parameters

NameTypeDescription
sellAsyncFiller.OrderInfo
buyAsyncFiller.OrderInfo
restingBuybool

Returns

NameTypeDescription
amountsAsyncFiller.MatchAmounts

_payouts

function _payouts(AsyncFiller.State storage self, PoolKey calldata key, bytes32 buyId, bytes32 sellId)
        private
        view
        returns (Payouts memory payouts);

Parameters

NameTypeDescription
selfAsyncFiller.State
keyPoolKey
buyIdbytes32
sellIdbytes32

Returns

NameTypeDescription
payoutsPayouts

_settleTransfers

Each side receives its proceeds less the 0.02% trade fee, which goes to the vault.

function _settleTransfers(
        IPoolManager manager,
        address feeVault,
        Payouts memory payouts,
        AsyncFiller.MatchAmounts memory amounts,
        ClaimTransfers.Batch memory batch
    ) internal;

Parameters

NameTypeDescription
managerIPoolManager
feeVaultaddress
payoutsPayouts
amountsAsyncFiller.MatchAmounts
batchClaimTransfers.Batch

_reduceMatchedOrders

function _reduceMatchedOrders(
        AsyncFiller.State storage self,
        AsyncFiller.Book storage book,
        PoolKey calldata key,
        bytes32 buyId,
        bytes32 sellId,
        AsyncFiller.MatchAmounts memory amounts,
        ClaimTransfers.Batch memory batch
    ) private returns (uint256 sellRefund, uint256 buyRefund);

Parameters

NameTypeDescription
selfAsyncFiller.State
bookAsyncFiller.Book
keyPoolKey
buyIdbytes32
sellIdbytes32
amountsAsyncFiller.MatchAmounts
batchClaimTransfers.Batch

Returns

NameTypeDescription
sellRefunduint256
buyRefunduint256

_transferEscrow

function _transferEscrow(
        IPoolManager manager,
        Currency currency,
        address recipient,
        uint256 amount,
        ClaimTransfers.Batch memory batch
    ) private;

Parameters

NameTypeDescription
managerIPoolManager
currencyCurrency
recipientaddress
amountuint256
batchClaimTransfers.Batch

_reduceAfterMatch

function _reduceAfterMatch(
        AsyncFiller.State storage self,
        AsyncFiller.Book storage book,
        PoolKey calldata key,
        ReductionParams memory params,
        ClaimTransfers.Batch memory batch,
        bool closeDust
    ) internal returns (uint256);

Parameters

NameTypeDescription
selfAsyncFiller.State
bookAsyncFiller.Book
keyPoolKey
paramsReductionParams
batchClaimTransfers.Batch
closeDustbool

Returns

NameTypeDescription
<none>uint256

_settleMatchedPriorityFee

function _settleMatchedPriorityFee(
        AsyncFiller.State storage self,
        PoolKey calldata key,
        bytes32 id,
        uint256 inputUsed,
        uint256 oldInput,
        ClaimTransfers.Batch memory batch
    ) private returns (uint256 unfilledPriorityFee);

Parameters

NameTypeDescription
selfAsyncFiller.State
keyPoolKey
idbytes32
inputUseduint256
oldInputuint256
batchClaimTransfers.Batch

Returns

NameTypeDescription
unfilledPriorityFeeuint256

_creditPriorityFee

function _creditPriorityFee(
        AsyncFiller.State storage self,
        PoolKey calldata key,
        bytes32 id,
        address maker,
        uint256 amount,
        ClaimTransfers.Batch memory batch
    ) internal;

Parameters

NameTypeDescription
selfAsyncFiller.State
keyPoolKey
idbytes32
makeraddress
amountuint256
batchClaimTransfers.Batch

_crosses

Decide whether a buy and a sell order cross, i.e. a price exists that satisfies both makers' limits.

Keys are the floor ticks of the exact limits, ordered by numeraire per asset, so two distinct prices can share a key. Strictly ordered keys decide the result directly, since a monotone key cannot invert a strict inequality. Equal keys are decided by a full-precision cross-multiply:

cross <=> buyIn/buyOut >= sellOut/sellIn <=> buyInsellIn >= sellOutbuyOut <=> floor(buyIn*sellIn / sellOut) >= buyOut (sellOut, buyOut are nonzero).

An exact tie counts as a cross. With equal keys the quotient is on the order of buyOut, so the mulDiv cannot overflow for escrowable amounts.

function _crosses(
        uint128 buyKey,
        uint128 sellKey,
        AsyncFiller.OrderInfo storage buy,
        AsyncFiller.OrderInfo storage sell
    ) internal view returns (bool);

Parameters

NameTypeDescription
buyKeyuint128
sellKeyuint128
buyAsyncFiller.OrderInfo
sellAsyncFiller.OrderInfo

Returns

NameTypeDescription
<none>bool

Events

AsyncOrdersMatched

event AsyncOrdersMatched(
        PoolId indexed poolId,
        bytes32 indexed sellOrderId,
        bytes32 indexed buyOrderId,
        uint256 assetAmount,
        uint256 numeraireAmount
    );

Parameters

NameTypeDescription
poolIdPoolId
sellOrderIdbytes32
buyOrderIdbytes32
assetAmountuint256
numeraireAmountuint256

AsyncOrderLiquidityFilled

event AsyncOrderLiquidityFilled(
        PoolId indexed poolId, bytes32 indexed orderId, address indexed maker, uint256 amountIn, uint256 amountOut
    );

Parameters

NameTypeDescription
poolIdPoolId
orderIdbytes32
makeraddress
amountInuint256
amountOutuint256

Errors

InvalidMatchBound

error InvalidMatchBound();

PartialLiquidityFill

error PartialLiquidityFill();

LimitPriceNotMet

error LimitPriceNotMet();

NoActiveLiquidity

error NoActiveLiquidity();

Structs

ReductionParams

struct ReductionParams {
        bytes32 id;
        uint256 inputUsed;
        uint256 outputReceived;
    }

Properties

NameTypeDescription
idbytes32
inputUseduint256
outputReceiveduint256

Payouts

Who receives each leg of a match, read before the matched orders are reduced.

struct Payouts {
        address sellOwner;
        address buyOwner;
        Currency sellInput;
        Currency buyInput;
        uint256 sellRefund; // input returned to an order left below the pool minimum
        uint256 buyRefund;
    }

Properties

NameTypeDescription
sellOwneraddress
buyOwneraddress
sellInputCurrency
buyInputCurrency
sellRefunduint256
buyRefunduint256

FillParams

struct FillParams {
        AsyncOrder order;
        bytes32 id;
        address hook;
        address executor;
        address filler;
        address feeVault;
        uint256 fee;
    }

Properties

NameTypeDescription
orderAsyncOrder
idbytes32
hookaddress
executoraddress
filleraddress
feeVaultaddress
feeuint256

CancelParams

struct CancelParams {
        AsyncOrder order;
        bytes32 id;
        address hook;
        address executor;
    }

Properties

NameTypeDescription
orderAsyncOrder
idbytes32
hookaddress
executoraddress

UpdateParams

struct UpdateParams {
        AsyncOrder order;
        bytes32 id;
        uint256 newAmountOutMin;
        address caller;
    }

Properties

NameTypeDescription
orderAsyncOrder
idbytes32
newAmountOutMinuint256
calleraddress