Rhino also supports cross chain swaps using external swap aggregators.
Cross chain swaps in general work the same way as a normal bridge:
To make cross chain swaps you can use the same bridge
or prepareBridge
functions. The only difference is that the arguments are slightly different compared to normal bridges:
Instead of the token
field, a swap requires tokenIn
and tokenOut
fields as well as type: 'bridgeSwap'
to indicate that a swap should be performed.
When using swaps only the pay
mode is available. receive
mode is only supported for bridges without swaps.
When using the prepareBridge
function with swaps or using the checkQuote
hook, you will receive a quote object that contains some additional properties compared to the quote for normal bridges. You will need to compare the _tag
field against bridgeSwap
first to narrow the type down to the swap quote case to get access to those properties:
bridgePayAmount
: The amount of tokenIn
that will be paidbridgePayAmountUsd
: The USD value of the pay amoutminReceiveAmount
: The minimum amount of tokenOut
that will be received on the destination chain after considering slippage.minReceiveAmountUsd
: The USD value of the minimum receive amountusdPriceTokenIn
: The current USD price of tokenIn
usdPriceTokenOut
: The current USD price of tokenOut
Swaps can fail for a number of reasons, most commonly due to market volatility. Rhino will automatically refund failed swaps to the depositor address on the origin chain. The bridge function will in this case return an error with type SwapFailed
that contains metadata about the refund (refund chain, token, amount and transaction hash).
Additionally, the onBridgeStatusChange
hook will also first report swap-failed
and then follow up with failed-swap-refunded
once the refund has been processed. The latter update will contain the same metadata about the swap refund as the returned error.