Example - DEX Swaps

Independent RAILGUN-integrated wallets, Railway Wallet, Terminal Wallet, and others have a built-in swap integration with the 0x API (the same tech as Matcha.xyz) to enable private DEX trading.

Once the best trade route is found, the wallet generates a proof to extract the token that is being sold from the user’s private balance, then swaps them using the 0x API, and shields the resulting tokens back into the private balance.

A swap of DAI into WETH through a private swap takes the following steps:

  1. User starts the swap interaction on the RAILGUN integrated wallet and specifies the amount of WETH to swap.

  2. Adapt Module performs a contract multicall which executes the following actions:

    • a) Calls transact() to unshield DAI from the private balance. As previously stated, the transact() will verify that the UTXO has not been nullified and that the user has sufficient funds to execute the swap.

    • b) Swap from DAI into WETH is executed using the 0x API and Adapt Module, calling the 0x smart contract address and passing parameters to the contract.

    • c) Calls shield()to shield the WETH back into the private balance. If the price of WETH has gone down since the start of the interaction, then both WETH and any DAI leftover will be shielded into the private balance.

  3. The contract Merkle Tree is updated with the new notes from shield().

As swaps performed through the Railway DEX call the transact()and shield() functions, the swap transaction also updates the events emitted by the respective functions:

  • Transact

  • Shield

  • Nullifiers

As with all other interaction types on RAILGUN, to an outside observer, the interaction appears to originate from a Broadcaster address. As Step 2b has an external touchpoint outside of the RAILGUN system, the number of tokens swapped and the token type will be visible but as it does not interact with the user’s 0x address in any way, the user remains anonymous, and no interactions are linked.

This process gives RAILGUN users full privacy and executes an immediate swap against their RAILGUN balance without a rollup delay. The encrypted 0x API call is also much harder to front-run as it’s passed as a cross-contract call to the RAILGUN Relay-Adapt contract. Whilst it is still possible to front-run Railway DEX interactions through simulating the contract, it is markedly more difficult than reading the data fields in a standard DEX interaction.

Last updated