ElyraElyraOpen the app

Docs

Settlement

The call

settle(Solution s, Award award)   payable
Solution = (uint64 auctionId, address[] tokens, uint256[] prices, Trade[] trades, Interaction[] interactions, Asset[] inventory)
Asset    = (address token, uint256 amount)
Trade    = (Order order, uint8 scheme, bytes signature, bytes32[] proof, uint8 sellIndex, uint8 buyIndex, uint256 executedSell)
Award    = (uint64 auctionId, address solver, bytes32 solutionHash, uint64 deadline, bytes signature)

A solver that fills from its own stock lists it in inventory: those tokens are pulled from the solver through the relayer (native HYPE comes as the call's value). Only a registered, active solver can call settle. When an auctioneer is set, the call also needs an Award signed by it for this solver and exactly this solution (its hash), before its deadline, and each auction settles once.

Checks per trade

  1. The signature is valid for the scheme and the order is not cancelled.
  2. The current time is inside validFrom and validTo, and inside the open TWAP part.
  3. The condition, if any, is met by a usable value.
  4. executedSell is not more than what remains in this part, and equals it for fill-or-kill.
  5. The payout is executedSell x prices[sell] / prices[buy], minus the fee, and it is at least buyAmount x executedSell / sellAmount.

Prices are uniform per token in a batch: two traders on the same pair in the same batch get the same price.

Interactions

Between pulling the sell amounts and paying the receivers, the settlement runs the solver's interactions: calls to the Universal Router, the PoolManager, WHYPE, or the solver's own contracts. An interaction may not call the relayer, Permit2 or the settlement itself, so no interaction can reach a user's allowance. The whole call is guarded against reentrancy.

Unlock callback

A solver can swap directly on Elysium's PoolManager: an interaction calls PoolManager.unlock(data) and the PoolManager calls back unlockCallback on the settlement, which runs the listed swaps and settles the deltas from its own balance. The callback answers only the PoolManager, and only during a settle.

Nothing left behind

At the end of a batch, the settlement holds no more of any batch token than before it started, and a batch can never spend what the settlement held before it began (the call reverts with OpeningBalanceUsed). The protocol share of the fee goes straight to ElyraFees and the solver's share to the solver. Every fill emits Traded(owner, orderHash, sellToken, buyToken, sold, bought, fee, part).

Gas

Measured on a fork of Elysium with real pools: one order through the Universal Router 285k gas, through the PoolManager unlock 321k, two orders crossed as a coincidence of wants 352k, a mixed batch (a crossed pair plus a pool fill) 448k. At Elysium's base fee that is a fraction of a cent of HYPE, paid by the solver.