Quickstart
Installation
npm i @zerodev/smart-routing-addressUsage
Smart Routing Address has a beautifully simple API: just createSmartRoutingAddress and then send funds to it. That's really it.
Code Example
We recommend that you take a look at our code example (even better if you try running it) as you follow along the docs.
Creating a smart routing address
In the following example, we are going to create a smart routing address that, when receiving funds on any chain, will transfer the funds to a specific address on Base. This is helpful when, for instance, you want to create a deposit address for your user, so that they can send funds to the address on any chain (possibly from a CEX), and then they will receive funds in their wallet on the chain that your app runs on.
First import some types and functions:
import {
createSmartRoutingAddress,
createCall,
FLEX,
} from "@zerodev/smart-routing-address";Then set up some values (explained below):
// Replace with your own address
const owner = "0x244b33858733aab5307B0919D31f73878cAF617B";
const destChain = base;
const slippage = 5000;
// This is the call that will be executed on the destination chain
const call = createCall({
target: FLEX.TOKEN_ADDRESS, // any tokens
value: 0n,
abi: erc20Abi,
functionName: "transfer",
args: [owner, FLEX.AMOUNT], // any amount
});Finally, create a smart routing address:
const { smartRoutingAddress } = await createSmartRoutingAddress({
owner,
destChain,
// Source tokens (any ERC20 on arbitrum, ETH on mainnet, USDC on optimism)
srcTokens: [
{
tokenType: "ERC20",
chain: arbitrum,
},
{
tokenType: "NATIVE",
chain: mainnet,
},
{
tokenType: "USDC",
chain: optimism,
},
],
actions: {
USDC: {
action: [call],
fallBack: [],
},
},
slippage,
});The options are:
-
owneris an address that is authorized to recover funds from the smart routing address, in case the smart routing address fails to execute the target action for whatever reason. Typically you would set this to your user's EOA wallet, but you could also set it to your own address if you want to recover funds for users. -
destChainis the chain on which theactionsare supposed to happen. This is presumably the chain that your app runs on. -
srcTokensis a list of tokens that the smart routing address should be able to receive. Only tokens listed insrcTokenswill trigger actions on the destination. The other tokens sent to the address will have to be manually recovered by theowner. -
actionsis a map that records which action will be triggered by which token. In the example above, we specified an action that will be triggered when the smart routing address receivesUSDC. -
slippageis the maximum slippage that the user can expect.slippageis an integer, where 1 is equal to 0.01% (so 100 would mean 1% slippage). You can skip this param if you are unsure what to set, and the SDK will estimate a reasonable slippage based on your other settings.
Once you have created a smart routing address, you can send tokens to it on any of the chains specified in srcTokens, and the actions will happen on the destChain. It's really that simple.