Railway DEX Swaps
Once the best trade route is found, the Railway 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.
Using an example of swapping DAI into WETH on Railway DEX, swaps take the following steps (this is also an example of how an external protocol might integrate the RAILGUN SDK):
- 1.User initiates the swap transaction on Railway DEX and specifies the amount of WETH that the DAI should be swapped into.
- 2.Adapt Module performs a contract multicall which executes the following actions:
- a) Calls
transact()function to unshield DAI from the private balance. As stated above, the
transact()function 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 0xAPI and Adapt Module calling the 0x smart contract address and passing parameters to the contract.
- c) Calls
shield()function 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.The contract merkletree is updated with the new notes from the
As swaps performed through the Railway DEX call the
shield()functions the swap transaction also updates the events emitted by the respective functions:
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.
This process gives Railway DEX 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. Therefore, users can not only enjoy complete privacy by using Railway DEX, but they also receive added sandwich attack and front-run protection versus trading on a DEX like Uniswap.