EVM (Ethereum)

DeFi Components

Uniswap V3 swap quotes and transaction building with Zafeguard DeFi components.

DeFi Components

Zafeguard includes native Uniswap V3 integration for token swaps — get a live quote, then build and broadcast the swap transaction.

Public RPC URLs enforce rate limits per IP address and can fail under concurrent load. Use a private endpoint (Alchemy, Infura, QuickNode, or self-hosted). See Connecting to any chain.


GET_UNISWAP_SWAP_QUOTE

GET_UNISWAP_SWAP_QUOTE DeFi Workflow Component

Fetch a live swap quote from Uniswap. Returns the best output amount across the configured protocol versions.

Config

FieldTypeDefaultDescription
version'v2' | 'v3' | 'v4' | 'all''all'Uniswap version(s) to query. 'all' returns the best quote across all supported versions

Inputs

FieldTypeDescription
jsonRpcUrlstringJSON-RPC endpoint for the target chain
tokenInstringInput token contract address
tokenOutstringOutput token contract address
amountInstringInput token amount in smallest unit (bigint string, e.g. "1000000000000000000" for 1 ETH)

Outputs

FieldTypeDescription
amountOutstringBest estimated output amount (bigint string)
totalFeenumberTotal pool fee in basis points (e.g. 30 = 0.3%)
gasEstimatestringEstimated gas cost for the swap (bigint string)

SDK example

import { WorkspaceClient, ComponentModule } from '@zafeguard/caller-sdk';
const workspace = new WorkspaceClient({ apiKey: process.env.ZAFEGUARD_API_KEY! });

const quote = await workspace.call(ComponentModule.GET_UNISWAP_SWAP_QUOTE, {
  jsonRpcUrl: 'https://eth-mainnet.g.alchemy.com/v2/KEY',
  chainId: 1,
  tokenIn: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',  // WETH
  tokenOut: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', // USDC
  amountIn: '1000000000000000000', // 1 ETH in wei
}).promise();

console.log(quote.amountOut);  // "1850000000" (1,850 USDC)
console.log(quote.totalFee);   // 30 (0.3%)

BUILD_EVM_UNISWAP_SWAP_CALLDATA

BUILD_EVM_UNISWAP_SWAP_CALLDATA DeFi Workflow Component

Build the encoded calldata for a Uniswap swap. Pipe the output into BUILD_EVM_TRANSACTION to produce an unsigned transaction ready for signing and broadcast.

Config

None.

Inputs

FieldTypeDescription
jsonRpcUrlstringJSON-RPC endpoint
chainIdnumberChain ID
tokenInstringInput token address
tokenOutstringOutput token address
recipientstringAddress to receive the output tokens
amountInnumberExact input amount
amountOutMinimumnumber?Minimum output (slippage protection). Defaults to 0 — always set this in production

Outputs

FieldTypeDescription
unsignedTransactionstringUnsigned swap transaction

SDK example

const swapTx = await workspace.call(ComponentModule.BUILD_EVM_UNISWAP_SWAP_CALLDATA, {
  jsonRpcUrl: 'https://eth-mainnet.g.alchemy.com/v2/KEY',
  chainId: 1,
  tokenIn: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
  tokenOut: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
  recipient: '0xMyWalletAddress',
  amountIn: 1_000_000_000_000_000_000,
  amountOutMinimum: 1_840_000_000, // min 1,840 USDC (1% slippage)
}).promise();

Complete swap workflow

STRING_CONSTANT (RPC URL)

        ├──▶ GET_EVM_CHAIN_ID
        │           │
        │           └──▶ GET_UNISWAP_SWAP_QUOTE
        │                         │ amountOut
        │                         ▼
        │                  (compute amountOutMinimum with slippage)

        └──▶ BUILD_EVM_UNISWAP_SWAP_CALLDATA
                        │ unsignedTransaction

             SIGN_WITH_KEY_SHARE ──▶ SIGN_EVM_TRANSACTION


                                   BROADCAST_EVM_TRANSACTION


                                   WAIT_FOR_EVM_TRANSACTION

Always set amountOutMinimum based on the quoted amountOut minus your acceptable slippage (e.g. 0.5–1%). Setting it to 0 exposes the transaction to sandwich attacks.

On this page