Skip to main content
How do you integrate Soroswap liquidity pools into your own DeFi protocol from a smart contract, rather than from a frontend or the API? You call the SoroswapRouter from your contract using a generated client. This is exactly what the aggregator’s Soroswap adapter does, and the pattern below is taken from contracts/adapters/soroswap/src/protocol_interface.rs in the aggregator repository.

Import the router client

Generate a typed client from the router’s WASM with contractimport!:
You need the router WASM in your build. Fetch it from soroswap/core or from the deployed contract.

Call it

The call returns the amount at each hop of path, not just the final output amount.

Authorization

The router calls to.require_auth() inside the swap. That means the address receiving the output has to authorize the call, so your contract cannot swap on a user’s behalf unless that user authorized the invocation, or to is your own contract address and your contract authorizes it. Design your flow around one of those two, not around holding user keys.

Getting the router address

Take it from Deployed Addresses, or read it at runtime from the API:
Store it as a constant or in contract storage rather than hardcoding it in several places.

The rest of the interface

swap_tokens_for_exact_tokens, add_liquidity and remove_liquidity are called the same way through the same client. Their signatures are on the SoroswapRouter page. If you only need a price and not an execution, router_get_amounts_out and router_quote are read-only and cost you no state.
If you are routing across more than just Soroswap, call the aggregator instead of the router. It has the same shape and splits the trade across every supported protocol.