Bridge and swap
Rhino also supports cross chain swaps using external swap aggregators.
Introduction
Cross chain swaps in general work the same way as a normal bridge:
- Fetch bridge config and additionally also a new config for supported swap tokens.
- Generate a special swap quote.
- Commit the quote.
- Deposit funds into the bridge contract.
- Rhino handles the swap and bridge logic under the hood and sends the funds to the recipient on the destination chain.
Making cross chain swaps
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.
Swap quotes
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 oftokenIn
that will be paidbridgePayAmountUsd
: The USD value of the pay amoutminReceiveAmount
: The minimum amount oftokenOut
that will be received on the destination chain after considering slippage.minReceiveAmountUsd
: The USD value of the minimum receive amountusdPriceTokenIn
: The current USD price oftokenIn
usdPriceTokenOut
: The current USD price oftokenOut
Failed swaps
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.