|
| 1 | +# Bungee Bridge Provider Integration |
| 2 | + |
| 3 | +> **Prerequisites:** This document assumes you've read [bridge-provider-summary.md](./bridge-provider-summary.md) and are familiar with the Actions SDK BridgeProvider pattern. |
| 4 | +
|
| 5 | +## Overview |
| 6 | + |
| 7 | +Bungee will serve as the recommended custom bridge provider for Actions SDK, enabling L2 ↔ L2 transfers that the native Optimism bridge doesn't support. |
| 8 | + |
| 9 | +**Integration Approach:** Actions SDK provides a `BungeeAPIAdapter` that wraps Bungee's REST API and implements the `BridgeClient` interface. |
| 10 | + |
| 11 | +--- |
| 12 | + |
| 13 | +## BridgeClient Interface |
| 14 | + |
| 15 | +The Actions SDK defines this interface for all bridge providers: |
| 16 | + |
| 17 | +```typescript |
| 18 | +interface BridgeClient { |
| 19 | + getQuote(params: BridgeQuoteParams): Promise<BridgeQuote> |
| 20 | + buildTransaction(params: any): Promise<BridgeTransactionData> |
| 21 | + getSupportedRoutes?(): BridgeRoute[] |
| 22 | +} |
| 23 | +``` |
| 24 | + |
| 25 | +### Actions SDK Types |
| 26 | + |
| 27 | +```typescript |
| 28 | +interface BridgeQuoteParams { |
| 29 | + asset: Asset |
| 30 | + amount: number // Human-readable |
| 31 | + fromChainId: number |
| 32 | + toChainId: number |
| 33 | + to?: Address // Recipient (optional, defaults to sender) |
| 34 | +} |
| 35 | + |
| 36 | +interface BridgeQuote { |
| 37 | + amountIn: bigint // Wei |
| 38 | + amountOut: bigint // Wei (minAmountOut) |
| 39 | + fee: bigint // Wei |
| 40 | + feePercent: number // 0.001 = 0.1% |
| 41 | + estimatedTime: number // Seconds |
| 42 | + gasEstimate?: bigint // Wei |
| 43 | + provider: string // "bungee" |
| 44 | + meta?: any // Provider-specific data (e.g., quoteId) |
| 45 | +} |
| 46 | + |
| 47 | +interface BridgeTransactionData { |
| 48 | + txTarget: Address // Contract to call |
| 49 | + txData: string // Calldata |
| 50 | + value?: string // ETH value |
| 51 | + approvalData?: { |
| 52 | + approvalTokenAddress: Address |
| 53 | + allowanceTarget: Address |
| 54 | + approvalAmount: string |
| 55 | + } |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +--- |
| 60 | + |
| 61 | +## API Mapping |
| 62 | + |
| 63 | +### 1. `getQuote()` → `/api/v1/bungee/quote` |
| 64 | + |
| 65 | +**Parameter Mapping:** |
| 66 | + |
| 67 | +| Actions SDK | Bungee API | Transform | |
| 68 | +|-------------|------------|-----------| |
| 69 | +| `fromChainId` | `originChainId` | `number` → `string` | |
| 70 | +| `toChainId` | `destinationChainId` | `number` → `string` | |
| 71 | +| `asset` | `inputToken` + `outputToken` | Get token address per chain | |
| 72 | +| `amount` | `inputAmount` | Human → wei → `string` | |
| 73 | +| `to` | `receiverAddress` | Pass through | |
| 74 | +| N/A | `userAddress` | From wallet context | |
| 75 | + |
| 76 | +**Response Mapping:** |
| 77 | + |
| 78 | +```typescript |
| 79 | +{ |
| 80 | + amountIn: BigInt(result.input.amount), |
| 81 | + amountOut: BigInt(result.autoRoute.output.minAmountOut), |
| 82 | + fee: BigInt(result.input.amount) - BigInt(result.autoRoute.output.amount), |
| 83 | + feePercent: (fee / amountIn) * 100, |
| 84 | + estimatedTime: result.autoRoute.estimatedTime, |
| 85 | + gasEstimate: BigInt(result.autoRoute.gasFee.estimatedFee), |
| 86 | + provider: 'bungee', |
| 87 | + meta: { |
| 88 | + quoteId: result.autoRoute.quoteId, |
| 89 | + routeDetails: result.autoRoute.routeDetails |
| 90 | + } |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +### 2. `buildTransaction()` → `/api/v1/bungee/build-tx` |
| 95 | + |
| 96 | +**Input:** Takes `quoteId` from `meta` in quote response |
| 97 | + |
| 98 | +**Response Mapping:** |
| 99 | + |
| 100 | +```typescript |
| 101 | +{ |
| 102 | + txTarget: result.txData.to, |
| 103 | + txData: result.txData.data, |
| 104 | + value: result.txData.value, |
| 105 | + approvalData: result.approvalData ? { |
| 106 | + approvalTokenAddress: result.approvalData.tokenAddress, |
| 107 | + allowanceTarget: result.approvalData.spenderAddress, |
| 108 | + approvalAmount: result.approvalData.amount |
| 109 | + } : undefined |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +### 3. `getSupportedRoutes()` → `/api/v1/supported-chains` |
| 114 | + |
| 115 | +Derive `BridgeRoute[]` by combining chains that support sending and receiving: |
| 116 | + |
| 117 | +```typescript |
| 118 | +interface BridgeRoute { |
| 119 | + asset: Asset |
| 120 | + fromChainId: number |
| 121 | + toChainId: number |
| 122 | + provider: string // "bungee" |
| 123 | +} |
| 124 | +``` |
| 125 | + |
| 126 | +--- |
| 127 | + |
| 128 | +## Implementation Example |
| 129 | + |
| 130 | +```typescript |
| 131 | +// packages/sdk/src/bridge/adapters/BungeeAPIAdapter.ts |
| 132 | + |
| 133 | +export class BungeeAPIAdapter implements BridgeClient { |
| 134 | + constructor( |
| 135 | + private config: { |
| 136 | + apiKey?: string |
| 137 | + baseURL?: string |
| 138 | + affiliateAddress?: string |
| 139 | + } |
| 140 | + ) { |
| 141 | + this.baseURL = config.baseURL || 'https://dedicated-backend.bungee.exchange' |
| 142 | + } |
| 143 | + |
| 144 | + async getQuote(params: BridgeQuoteParams): Promise<BridgeQuote> { |
| 145 | + const { asset, amount, fromChainId, toChainId, to } = params |
| 146 | + |
| 147 | + // Convert to Bungee format |
| 148 | + const queryParams = { |
| 149 | + userAddress: this.walletAddress, |
| 150 | + originChainId: fromChainId.toString(), |
| 151 | + destinationChainId: toChainId.toString(), |
| 152 | + inputToken: getAssetAddress(asset, fromChainId), |
| 153 | + outputToken: getAssetAddress(asset, toChainId), |
| 154 | + inputAmount: parseUnits(amount, asset.decimals).toString(), |
| 155 | + receiverAddress: to || this.walletAddress, |
| 156 | + } |
| 157 | + |
| 158 | + const response = await fetch( |
| 159 | + `${this.baseURL}/api/v1/bungee/quote?${new URLSearchParams(queryParams)}`, |
| 160 | + { |
| 161 | + headers: { |
| 162 | + 'x-api-key': this.config.apiKey || '', |
| 163 | + 'affiliate': this.config.affiliateAddress || '', |
| 164 | + }, |
| 165 | + } |
| 166 | + ) |
| 167 | + |
| 168 | + const data = await response.json() |
| 169 | + if (!data.success) { |
| 170 | + throw new BridgeQuoteError(data.message) |
| 171 | + } |
| 172 | + |
| 173 | + return transformQuoteResponse(data.result) |
| 174 | + } |
| 175 | + |
| 176 | + async buildTransaction(params: { quoteId: string }): Promise<BridgeTransactionData> { |
| 177 | + const response = await fetch( |
| 178 | + `${this.baseURL}/api/v1/bungee/build-tx?quoteId=${params.quoteId}`, |
| 179 | + { |
| 180 | + headers: { 'x-api-key': this.config.apiKey || '' }, |
| 181 | + } |
| 182 | + ) |
| 183 | + |
| 184 | + const data = await response.json() |
| 185 | + if (!data.success) { |
| 186 | + throw new BridgeBuildError(data.message) |
| 187 | + } |
| 188 | + |
| 189 | + return transformBuildTxResponse(data.result) |
| 190 | + } |
| 191 | +} |
| 192 | +``` |
| 193 | + |
| 194 | +--- |
| 195 | + |
| 196 | +## Developer Setup |
| 197 | + |
| 198 | +Developers configure Bungee as a custom bridge provider: |
| 199 | + |
| 200 | +```typescript |
| 201 | +import { createActions } from '@eth-optimism/actions-sdk' |
| 202 | +import { BungeeAPIAdapter } from '@eth-optimism/actions-sdk/bridge/adapters' |
| 203 | + |
| 204 | +const bungeeClient = new BungeeAPIAdapter({ |
| 205 | + apiKey: process.env.BUNGEE_API_KEY!, |
| 206 | + baseURL: 'https://dedicated-backend.bungee.exchange', |
| 207 | +}) |
| 208 | + |
| 209 | +const actions = createActions({ |
| 210 | + wallet: { /* ... */ }, |
| 211 | + chains: [ |
| 212 | + { chainId: 10, rpcUrl: '...' }, |
| 213 | + { chainId: 8453, rpcUrl: '...' }, |
| 214 | + ], |
| 215 | + bridge: { |
| 216 | + type: 'custom', |
| 217 | + client: bungeeClient, |
| 218 | + }, |
| 219 | +}) |
| 220 | + |
| 221 | +// L2 ↔ L2 bridging now works |
| 222 | +await wallet.send({ |
| 223 | + amount: 100, |
| 224 | + asset: USDC, |
| 225 | + to: '0x...', |
| 226 | + fromChainId: 8453, // Base |
| 227 | + toChainId: 10, // OP Mainnet |
| 228 | +}) |
| 229 | +``` |
| 230 | + |
| 231 | +--- |
| 232 | + |
| 233 | +## Integration Requirements |
| 234 | + |
| 235 | +### 1. API Authentication |
| 236 | +- Production: Dedicated backend with API key via `x-api-key` header |
| 237 | +- Testing: Public sandbox (no auth) |
| 238 | + |
| 239 | +### 2. Native Token Handling |
| 240 | +- Use `0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE` for native ETH |
| 241 | + |
| 242 | +### 3. Error Handling |
| 243 | +- Check `data.success` in responses |
| 244 | +- Handle `statusCode` and `message` fields |
| 245 | +- Quote expiry validation via `quoteExpiry` timestamp |
| 246 | + |
| 247 | +### 4. Transaction Execution |
| 248 | +- Actions SDK handles approval + bridge tx automatically |
| 249 | +- Supports batched transactions (approval + bridge in one user signature) |
| 250 | + |
| 251 | +### 5. Optional Features |
| 252 | +The adapter can support additional Bungee features: |
| 253 | +- `refuel`: Enable gas refuel on destination |
| 254 | +- `feeBps`: Collect affiliate fees |
| 255 | +- `enableManual`: Use manual routing |
| 256 | + |
| 257 | +--- |
| 258 | + |
| 259 | +## Comparison: Native vs Bungee |
| 260 | + |
| 261 | +| Feature | Native Bridge | Bungee | |
| 262 | +|---------|---------------|--------| |
| 263 | +| **Routes** | L1 ↔ L2 only | L1 ↔ L2 + L2 ↔ L2 | |
| 264 | +| **L2→L2 Support** | ❌ | ✅ | |
| 265 | +| **Bridge Fees** | 0% | ~0.05-0.5% | |
| 266 | +| **Speed (L2→L1)** | ~7 days | ~1-15 min | |
| 267 | +| **Setup** | Default | API key required | |
| 268 | + |
| 269 | +**Strategy:** |
| 270 | +- Native bridge (default) for L1 ↔ L2 (lowest cost) |
| 271 | +- Bungee for L2 ↔ L2 and fast L2 → L1 |
| 272 | + |
| 273 | +--- |
| 274 | + |
| 275 | +## Resources |
| 276 | + |
| 277 | +- **Bungee API Docs:** https://docs.bungee.exchange |
| 278 | +- **Actions SDK Repo:** https://github.com/ethereum-optimism/actions |
| 279 | +- **Bridge Provider Spec:** [bridge-provider-summary.md](./bridge-provider-summary.md) |
0 commit comments