|
| 1 | +--- |
| 2 | +title: Swaps (Alpha) |
| 3 | +slug: wallets/transactions/swap-tokens |
| 4 | +--- |
| 5 | + |
| 6 | +The Swap API is the easiest way to integrate single-chain token swaps in your app. Cross-chain swaps are coming soon! |
| 7 | + |
| 8 | +Key benefits: |
| 9 | + |
| 10 | +- **Single-step flows:** Batch approve + swap + post-swap action (ex: deposits, transfers, contract calls) into a single transaction - made possible by smart wallets |
| 11 | +- **Low latency:** |
| 12 | + - Built natively into Smart Wallets. Enables preparing gasless swaps with a single API call |
| 13 | + - No need to make calls to two providers to perform a swap |
| 14 | +- **Simplified stack:** No need for third-party swap integrations |
| 15 | + |
| 16 | +Key use cases: |
| 17 | + |
| 18 | +- Native token swaps: Enable users to do gasless swaps without ever leaving your app |
| 19 | +- Transact with any token: Let users transact with any token they have in their wallet (ex: ETH, WLD), while your contracts still receive your preferred token(ex: USDC). |
| 20 | + |
| 21 | +<Tip> |
| 22 | + The Swaps API is in early access. The service is production ready, however there may be changes in the future to simplify the endpoint. We will let you know if/when that happens. |
| 23 | + |
| 24 | + [Request early access!](https://alchemyapi.typeform.com/to/pkjSr57v) |
| 25 | + |
| 26 | +</Tip> |
| 27 | + |
| 28 | +# The Swap flow |
| 29 | + |
| 30 | +## **Flow** |
| 31 | + |
| 32 | +1. Request a swap quote |
| 33 | +2. Sign the prepared calls (swap + post swap action) |
| 34 | +3. Send prepared calls (swap + post swap action) |
| 35 | +4. Poll for confirmation |
| 36 | + |
| 37 | +## **Swap options** |
| 38 | + |
| 39 | +```tsx |
| 40 | +// Mode 1: Swap exact input amount |
| 41 | +{ |
| 42 | + fromAmount: "0x2710"; |
| 43 | +} // Swap exactly 0.01 USDC (10000 in hex, 6 decimals) |
| 44 | + |
| 45 | +// Mode 2: Get minimum output amount |
| 46 | +{ |
| 47 | + minimumToAmount: "0x5AF3107A4000"; |
| 48 | +} // Get at least 0.0001 ETH (18 decimals). We calculate how much USDC you need to spend to get at least your desired ETH amount! |
| 49 | +``` |
| 50 | + |
| 51 | +<Tabs> |
| 52 | + <Tab title="React"></Tab> |
| 53 | + <Tab title="Other JS"></Tab> |
| 54 | + <Tab title="APIs"></Tab> |
| 55 | +</Tabs> |
| 56 | + |
| 57 | +# FAQs |
| 58 | + |
| 59 | +## What chains are supported? |
| 60 | + |
| 61 | +Chains supported (for now) are: Ethereum, Arbitrum, Base, Berachain, BSC/BNB, Ink, Monad, Optimism, Polygon, Unichain and World Chain mainnets. |
| 62 | + |
| 63 | +## Does the Swap API support cross-chain swaps? |
| 64 | + |
| 65 | +Currently, the Swap API supports only single-chain swaps. Cross-chain swaps are coming soon! |
| 66 | + |
| 67 | +## How do you encode values? |
| 68 | + |
| 69 | +Values are simply passed as hexadecimal strings. The Swap API doesn't add complexity to consider decimals, so 0x01 is always the smallest amount of a given asset. |
| 70 | +1 ETH, or DAI (18 decimals) is `0xDE0B6B3A7640000` |
| 71 | +1 USDC (6 decimals) is `0xF4240` |
| 72 | +This removes any ambiguity— if it’s numerical, it’s a hex. |
| 73 | + |
| 74 | +## What is the expiry? |
| 75 | + |
| 76 | +The expiry is an informational indicator of when you can expect to be able to process the swap request. If you’re at/near the expiry, it might be a good time to request a new quote. |
| 77 | + |
| 78 | +## What if I’m using React? |
| 79 | + |
| 80 | +Use the `@account-kit/react` package to access a `smartWalletClient`. This package automatically uses the Signer for user authentication & signing. [Check out the docs for getting started with React to learn more](/wallets/react/quickstart). |
0 commit comments