Usage

How to use the SDK to create quotes and bridge transactions

Setting up your Environment

In order to begin constructing bridge related transactions, developers need to set up their environment for the relevant chains. The first is configuring chains and providers.
//Set up providers (RPCs) for each chain desired
const arbitrumProvider: Provider = new etherProvider.JsonRpcProvider(
'https://arb1.arbitrum.io/rpc'
)
const avalancheProvider: Provider = new etherProvider.JsonRpcProvider(
'https://api.avax.network/ext/bc/C/rpc'
)
//Structure arguments properly
const chainIds = [42161, 43114]
const providers = [arbitrumProvider, avalancheProvider]
//Set up a SynapseSDK instance
const Synapse = new SynapseSDK(chainIds, providers)
In the code snippet above, we set up the different providers and format them, along with the Ids of the chains we are bridging from/to for our quotes.

bridgeQuote(): Getting a Quote

Getting a bridge quote returns all relevant information regarding a possible transaction. A quote returns important instructions for the bridge in a data type called "Query". The Query type is defined as follows.
  • swapAdapter (string): The 0x address of the swap adapter.
  • tokenOut (string): The 0x address of the outputted token on that chain.
  • minAmountOut (Ethers BigNumber): The min amount of value exiting the transaction.
  • deadline (Ethers BigNumber): The deadline for the potential transaction.
  • rawParams (string): The 0x params for the potential transaction.
For more information on the Query data type, refer to this page. To access these Queries along with a quote for the estimated amount out, we call the function bridgeQuote() with the relevant args.
The bridgeQuote function requires the following information
  • fromChain (number): The origin chain id.
  • toChain (number): The destination chain id.
  • fromToken (string): The 0x token address on the origin chain.
  • toToken (string): The 0x token address on the destination chain.
  • amount (Ethers BigNumber): The amount (with the correct amount of decimals specified by the token on the origin chain)
The bridgeQuote function returns the following information
  • feeAmount (Ethers BigNumber): The calculated amount of fee to be taken.
  • bridgeFee (number): The percentage of fee to be taken.
  • maxAmountOut (Ethers BigNumber): The maximum output amount resulting from the bridge transaction.
  • originQuery (Query Type): The query to be executed on the origin chain.
  • destQuery (Query Type): The query to be executed on the destination chain.
maxAmountOut is the value produced after fees
If the permutation of chains and tokens is not supported, the SDK will inform the user. Note that every bridge transaction requires this step.

Getting a Quote Example

const quotes = await Synapse.bridgeQuote(
42161, // Origin Chain
43114, // Destination Chain
'0xff970a61a04b1ca14834a43f5de4533ebddb5cc8', // Origin Token Address
'0xa7d7079b0fead91f3e65f86e8915cb59c1a4c664', // Destination Token Address
BigNumber.from('20000000') // Amount in
)

bridge(): Executing a Bridge Transaction

Once bridge quotes are retrieved, the Origin and Destination Queries can be used to execute the transaction.
The bridge() function requires the following information
  • toAddress (number): The 0x wallet address on the destination chain.
  • fromChain (number): The origin chain id.
  • toChain (number): The destination chain id.
  • fromToken (string): The 0x token address on the origin chain.
  • amount (Ethers BigNumber): The amount (with the correct amount of decimals specified by the token on the origin chain)
  • originQuery (Query Type): The query to be executed on the origin chain.
  • destQuery (Query Type): The query to be executed on the destination chain.
The bridge() function returns the following information
  • to (string): The 0x wallet address on the destination chain.
  • data (string): The output data in 0x hex format
Executing a bridge transaction example
await Synapse.bridge(
'0x0AF91FA049A7e1894F480bFE5bBa20142C6c29a9', // To Address
42161, // Origin Chain
43114, // Destination Chain
'0xff970a61a04b1ca14834a43f5de4533ebddb5cc8', // Origin Token Address
BigNumber.from('20000000'), // Amount
quote.originQuery, // Origin query from bridgeQuote()
quote.destQuery // Destination query from bridgeQuote()
)
Once this function is called the bridge transaction is initiated and the user should soon receive their funds on the destination chain. The allBridgeQuotes() function returns a list of all the possible bridge quotes, with the first item in the array being the cheapest route. This function allows for quotes to be returned from any of the bridge "types" such as RFQ or CCTP. Discover more here.
More information can be found in the synapsecns github repo