🟧
JS SDK Cross-chain Swap
Symbiosis JS SDK Cross-chain Swap
A cross-chain swap is an exchange of one token for another token. The exchanging tokens reside on different blockchains.
If you want to understand what happens under the hood during cross-chain swaps, please refer to Metarouter V3 | Symbiosis
Examples You can find full-featured examples of ANY to ANY token cross-chain swaps as well as exchanging for BTC in the directory of examples
JS SDK implements one method for cross-chain swapping:
Swapping.exactIn
that takes the following parameters for input:Property | Description |
---|---|
tokenAmountIn | An object of TokenAmount type. An amount of tokens the user gives away on the source blockchain. |
tokenOut | The token the user expects to get on the destination blockchain. |
from | The sender's address. This address receives tokens in case of a reverting operation. |
to | The recipient's address. Usually, it is identical to the sender's address but can differ. |
revertableAddress | The address that can revert this operation if the operation gets stuck. Usually, it is identical to the sender's address but can differ.
Please check out Emergencies for more information on stuck operations. |
slippage | The slippage tolerance that is acceptable for the user. The cross-chain swap will automatically cancel if the price change for this swap exceeds the specified value. The value is in a percent value multiplied by 100. For example, 0.8% * 100 = 80 |
deadline | The value in seconds that the cross-chain swap is considered valid. The cross-chain swap will automatically cancel if the processing time exceeds the specified value. |
use1Inch |
const swapping = symbiosis.newSwapping()
const { execute, fee, tokenAmountOut, route, priceImpact } = await swapping.exactIn(
tokenAmountIn,
tokenOut,
from,
to,
revertableAddress,
slippage,
deadline,
use1Inch
)
const { response, waitForMined } = await execute(signer)
const { receipt, waitForComplete } = await waitForMined()
const log = await waitForComplete()
The
exactIn
method returns:Property | Description |
---|---|
execute | A function to execute the cross-chain swap operation. Calling this function triggers the transaction signing and sends the signed transaction to the source blockchain. Accepts an object of Signer type . |
fee | An object of TokenAmount type. The amount of tokens to pay the fee for bridging: the fee for relayers network work.
Please refer to Relayers Network | Symbiosis for more information on relayers. |
tokenAmountOut | |
route | Token[] A sequence of intermediate swaps leading to the best price for this cross-chain swap. |
priceImpact | An object of Percent type. An effect of the cross-chain swap amount on the resulting transaction value. |
transactionRequest |
Calling the
execute
function returns: - A function
waitForMined
is a function of waiting for the transaction to be mined.
Calling the
waitForMined
function returns: - A function
waitForComplete
is a function of waiting for getting tokens on the destination blockchain.
The stateless mode (since JS SDK v2.3)
const swapping = symbiosis.newSwapping()
// transactionRequest contains everything you need to send a transaction by yourself
const { transactionRequest } = await swapping.exactIn(...)
// Send the transaction and get a receipt
const receipt = ...
// Wait for the transaction to be completed on the destination chain
const log = await swapping.waitForComplete(receipt)
Last modified 3mo ago