Cross-Contract Calls
Cross-Contract Calls require you to "Generate Proof" and, optionally, make use of "Relayers": See UX for Private Transactions UX for Private Transactions
Cross Contract calls are the primary method for RAILGUN to access external DeFi protocols and smart contracts. Any number of ordered contract calls can be wired together and executed against a private balance. These cross-contract interactions occur in the generalized RAILGUN Relay Adapt contract, which facilitates a number of serial multi-calls in a single block.
How it Works
First, (1) an unshield is executed against the private balance, moving tokens temporarily into the Relay Adapt contract. Then, (2) a multi-call processes any number of contract calls against the balance. Finally, (3) the result is shielded back into the user’s private balance. This all occurs in a single block and one transaction.
For example, Railway Wallet has integrated 0x API to facilitate private swaps. This is done through two serial cross-contract calls: First, the exact swap token amount is approved for spending. Second, the 0x token swap is performed. The contract spend approval is a failsafe operation on each swap transaction. It ensures that the swap amount is guaranteed to be approved during the execution block.
In the case of a contract call failure, the entire call will revert.
RAILGUN Cookbook and Recipes
The RAILGUN Cookbook simplifies forming these cross-contract transactions. Please see our guide on RAILGUN Cookbook and check the verified recipes that are available in the Cookbook SDK (Github).
The "Recipes" in the Cookbook contain structured calls that integrate directly into the Wallet SDK. Contributors are working on verified Recipes for DEX Swaps, Asset Vaults, Liquidity Provisioning (LP), Stablecoin Minting, Liquid Staking, and more.
Call Structure
The structure of these transactions is very similar to RAILGUN transfers and unshields, with 3 new fields.
crossContractCallsSerialized: {to, data, value}
fields make up our array of crossContractCalls, which interact with external smart contracts through a series of ordered calls.
relayAdaptUnshieldERC20Amounts: The exact amounts that should be unshielded to process in this transaction. Make sure only to unshield the exact token amount that is required, as there is an unshield fee associated with this value (like any unshield transaction).
relayAdaptShieldERC20Addresses: A list of token addresses that are the result of the cross contract calls, to shield into private balance after the calls are complete. If its balance is 0, nothing will be shielded. These are the only tokens that will be shielded back, so make sure the list is complete. Any tokens that are not listed in this array and shielded during the transaction will not be recoverable.
NOTE: If using an existing Cookbook Recipe or writing your own for custom cross-contract calls, the Recipe will output these fields for you.
Example: Format 0x API private swap for cross-contract calls with Cookbook
Example: Format 0x API private swap for cross-contract calls without Cookbook
Approvals
By default, you should assume that the Relay Adapt contract is not approved to spend any tokens. Because anyone can execute transactions against this contract, a user could potentially reset all spend allowances to 0 in any transaction.
However, because we execute cross-contract transactions inside a multi-call, we're safe from outside interactions during the execution block.
It's recommended to always approve the exact amount that you require in your transaction, as the initial step of the cross-contract calls. (As in the example above).
NOTE: When using the Cookbook, all Recipes will include the necessary approval call, so there is no additional code needed.
Impact of Unshield Fee
Because of the unshield fee, the amount of tokens in the transaction will be 0.25% less than the amount unshielded. This is very important if transacting a specific value, like for a swap.
For example, if we want to swap 100 DAI for wETH, the transaction will look like this:
Unshield 100 DAI (
relayAdaptUnshieldERC20Amounts
), with a 0.25 DAI fee.Cross contract call: approve 99.75 DAI for swap spending.
Cross contract call: swap 99.75 DAI for wETH.
Shield wETH and any leftover DAI dust into private balance (
relayAdaptShieldERC20Addresses
).
NOTE: The Cookbook automatically calculates the unshield fees when using an existing Recipe or you write your own cross-contract calls as a Recipe.
Gas Estimate
Proof Generation
Populate Transaction
Last updated