💻
Developer Guide
  • Wallet SDK
    • Wallet Overview
    • Getting Started
      • 1. Start the RAILGUN Privacy Engine
      • 2. Build a persistent store for artifact downloads
      • 3. Load a Groth16 prover for each platform
      • 4. Add networks and RPC providers
      • 5. Set up a debug logger
    • Private Wallets
      • RAILGUN Wallets
      • View-Only Wallets
      • Encryption Keys
    • Private Balances
      • Balance and Sync Callbacks
      • Updating Balances
      • QuickSync
    • Transactions
      • Shielding
        • Shield ERC-20 tokens
        • Shield base token
        • Shield NFTs
      • Private Transfers
        • Private ERC-20 Transfers
        • Private NFT Transfers
      • Cross-Contract Calls
      • Unshielding
        • Unshield ERC-20 tokens
        • Unshield base token
        • Unshield NFTs
      • UX: Private Transactions
    • Broadcasters
  • Cookbook SDK
    • Cookbook Overview
    • Recipe Guide: Write a zkApp
      • "Step" — A smart contract call
      • "Recipe" — Steps in series
      • "Combo Meal" — 2+ Recipes
    • Use your zkApp privately
  • Engine SDK
    • Engine Overview
  • ZK Account Abstraction
    • Account Abstraction Overview
    • Getting started with the contracts
    • Wallets
    • State Structure
    • Example Primitives
Powered by GitBook
On this page
  1. Cookbook SDK

Use your zkApp privately

"Cook a Recipe" anonymously with RAILGUN Quickstart

Previous"Combo Meal" — 2+ RecipesNextEngine Overview

Last updated 1 year ago

Given a full Recipe and its inputs, will generate a and a final serialized transaction for the RAILGUN Relay Adapt contract.

This final transaction can be submitted to the blockchain by any wallet, including a .

cook-recipe-example.ts
import {
  RecipeERC20Info,
  ZeroXSwapRecipe,
  RecipeInput,
  RecipeOutput
} from '@railgun-community/cookbook';
import {
  NetworkName,
} from '@railgun-community/shared-models';

const sellERC20Info: RecipeERC20Info = {
  tokenAddress: '0x6B175474E89094C44Da98b954EedeAC495271d0F', // DAI
  decimals: BigInt(18)
}

const buyERC20Info: RecipeERC20Info = {
  tokenAddress: '0xdac17f958d2ee523a2206206994597c13d831ec7', // USDT
  decimals: BigInt(18)
}

const slippagePercentage = 0.05;

const swap = new ZeroXSwapRecipe(sellERC20Info, buyERC20Info, slippagePercentage);

// Inputs that will be unshielded from private balance.
const unshieldERC20Amounts = [
  { 
    ...sellERC20Info,
    amount: BigInt('0x10'), // hexadecimal amount
  }
];

const recipeInput: RecipeInput = { NetworkName.Ethereum, unshieldERC20Amounts };
const {
  crossContractCalls,
  erc20AmountRecipients,
  nftRecipients,
  feeERC20AmountRecipients,
  minGasLimit,
} = await swap.getRecipeOutput(recipeInput);

// Outputs to re-shield after the Recipe multicall.
const shieldERC20Addresses = erc20AmountRecipients.map(({tokenAddress}) => tokenAddress);

// RAILGUN Wallet will generate a [unshield -> call -> re-shield] 
// transaction enclosing the Recipe multicall.

const { gasEstimate } = await gasEstimateForUnprovenCrossContractCalls(
    ...
    unshieldERC20Amounts,
    ...
    shieldERC20Addresses,
    ...
    crossContractCalls,
    ...
)
await generateCrossContractCallsProof(
    ...
    unshieldERC20Amounts,
    ...
    shieldERC20Addresses,
    ...
    crossContractCalls,
    ...
)
const { transaction } = await populateProvedCrossContractCalls(
    ...
    unshieldERC20Amounts,
    ...
    shieldERC20Addresses,
    ...
    crossContractCalls,
    ...
);

// Submit transaction to RPC.
await wallet.sendTransaction(transaction);

// Note: use @railgun-community/waku-relayer-client to submit 
// through a Relayer instead of signing with your own wallet.
RAILGUN Wallet SDK
zero-knowledge proof
Relayer