Skip to main content
iCrypto

Crypto news, costs and deadlines

How to integrate the SyncSwap SDK into a dapp

Integrate the SyncSwap SDK by quoting a route, authorizing bounded token spending, simulating the call and checking where the swap pays out.

iCrypto Editorial5 min read

How to integrate the SyncSwap SDK into a dapp

Integrate the SyncSwap SDK by building a route and transaction with a verified, pinned release, then asking the user’s wallet to authorize and submit each required transaction. Transaction construction and user authorization are separate stages. For a user making a standalone trade, swap tokens on syncswap through the exchange interface; that fits an external handoff when the dapp does not need to embed execution.

Prepare the SDK and wallet context

Before integrating the SyncSwap SDK, match the package, release and network to the deployed contracts you intend to call. Start with SyncSwap’s Project developer resources, then compare the published SDK README and package metadata. Pin the reviewed release in your lockfile; the README currently describes version 0.2.23, so an installation can specify `@syncswap/sdk@0.2.23`. Recheck the published release before adopting that version in a new project.

Initialize the provider separately from the connected wallet. The SDK README’s TypeScript pattern creates a provider for the target chain, initializes the SDK with that provider, then creates a `UserContext` with the wallet address and trade settings. Keep the wallet signer in the browser wallet flow. This separation lets the app prepare route and transaction data without signing for the user.

Before quoting, confirm the connected wallet is on the matching zkSync Era deployment and has the input ERC-20 token and enough native gas. Read token decimals from the token contract or a trusted token record. Convert the human-readable amount to base units with the token’s decimals; do not pass a display string such as “1.5” as an on-chain integer.

How do you build and submit a swap?

For an embedded swap, fetch candidate pools, calculate the route, authorize only the required input amount, then build and simulate the transaction before opening the wallet confirmation. The published SDK README names `fetchSyncSwapRouteData`, `calculateSyncSwapRoute`, `checkApproval`, `approve` and `swapExactInput` for this flow.

In TypeScript, the core sequence is: `const pools = await sdk.fetchSyncSwapRouteData(tokenIn, tokenOut, userContext); const route = await sdk.calculateSyncSwapRoute(pools, tokenIn, tokenOut, amountIn);`. Here, `amountIn` is already in base units. Calculate a minimum output from the quoted amount and the user’s slippage setting. Refresh the quote if it has gone stale before building the call.

Ask `checkApproval(tokenIn, amountIn, userContext)` whether the router can spend the input tokens. If it cannot, build approval data with `approve(tokenIn, amountIn, userContext)` and pass that transaction to the connected wallet. Wait for its receipt before continuing. The allowance should cover this trade, rather than an unlimited amount by default.

Next, create swap transaction data with `swapExactInput(route, null, userContext)`. The router reference defines the swap parameters as paths, `amountOutMin` and a deadline. Confirm the built call uses the intended recipient, minimum output and a deadline that limits how long the quote remains usable. Simulate the call with the provider using the connected address as `from`. Only after simulation succeeds should the dapp request wallet submission. The wallet prompt is the user’s chance to review and authorize the trade; route construction alone does not move funds.

How do you verify the payout and handle failures?

A successful swap is complete only when the receipt succeeds and the intended recipient has the expected payout. Await the wallet’s transaction receipt and check its status. Then check the recipient’s output-token balance or the relevant Vault balance, depending on the route’s payout mode.

That location matters on routes involving V1. In V1, `withdrawMode` 0 credits the recipient’s internal Vault balance instead of paying tokens directly to the recipient’s wallet. Configure the withdrawal mode for the payout the dapp promises, and make the result screen check that same destination. A successful receipt with an unchanged wallet balance may still mean the output arrived in the Vault. For the particular transaction, inspect its receipt and the recipient’s balances on the zkSync Era block explorer.

Two swaps can use the same token pair and amount but finish in different places because their V1 withdrawal modes differ. With mode 0, the recipient sees the output in the Vault. With a wallet payout configured, the recipient sees it in the wallet. The dapp should state which destination it expects and verify that destination after settlement.

Handle each failed stage explicitly. If the user rejects approval or the swap request, keep the trade pending only until the wallet returns the rejection, then let them retry. If allowance is short, request approval for the missing required spending and wait for confirmation. If simulation fails or the quote is stale, fetch pools and calculate a fresh route before asking for another signature. If execution reverts, show that the swap failed and do not display success based on the submission hash alone.

Choose between an embedded SDK flow and an external handoff based on who owns execution: the SDK path fits this dapp’s user-authorized trade, while the exchange interface fits someone trading outside the app. Once the receipt and intended payout are confirmed, show the settled amount and destination, then let the user continue from that completed state.