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, 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. 1.
    User starts the swap transaction on the RAILGUN integrated wallet and specifies the amount of WETH to swap.
  2. 2.
    Adapt Module performs a contract multicall which executes the following actions:
    • a) Calls transact() to unshield DAI from the private balance. As stated above, 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 transaction, then both WETH and any DAI leftover will be shielded into the private balance.
  3. 3.
    The contract merkletree 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 transaction types on RAILGUN, to an outside observer, the transaction appears to originate from a Relayer address. As Step 2b has an external touchpoint outside of the RAILGUN system, the amount of the assets swapped and the asset 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 transactions are linked.
This process gives RAIGLUN 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 transactions through simulating the contract, it is markedly more difficult than reading the data fields in a standard DEX transaction.