Action: Swap
A Swap
action specifies two assets to convert between at the current market rate and contains a ciphertext that allows subsequent proof (in SwapClaim
) that a corresponding amount of each asset was burnt.
A Swap
action contains a trading pair, two transparent amounts and , a fee commitment, a swap ciphertext, a commitment to the hash of the fields of the corresponding swap plaintext, and a zero-knowledge proof relating these values. The swap ciphertext is authenticated and encrypted with ChaCha20Poly1305, and the corresponding swap plaintext contains another trading pair, amounts, a trans parent fee, an address (to redeem the swap at), and a nonce (rseed). It is expected in normal use that one of or is zero (since the amount of whichever is smaller could be deducted from both amounts to produce a Swap
with the same outcome), but this is not enforced.
Since and are transparent, swaps do not currently hide the values being swapped; this is intended to be solved by flow encryption in future work. Since there are no bounds on what range of exchange rates to accept, slippage may occur if liquidity positions are modified in the same block that the swap is included in.
Swap::check_stateless
verifies the zero-knowledge proof, which enforces that
the plaintext swap provided as a witness has the same hash as the swap commitment.
the transparent fee inside the plaintext swap matches the fee commitment.
the amounts in the plaintext swap are the same as the amounts in the action.
the balance commitment deducts the fee and values corresponding to the amounts of the specified assets in the trading pair.
Swap::check_stateful
unconditionally succeeds.
Swap::execute
records state relating to the swap:
Under
"/dex/swap_flows"
, it adds the amounts to be swapped to the trading pair to be processed at the end of the block.It adds swap commitment to the state commitment tree.
It adds the payload containing the swap ciphertext and swap commitment to
"dex/pending_payloads"
, to be stored in the compact block.