StargateComposed.sol
Leverage Stargate to perform additional smart contract logic on the destination chain!
This contract is an example of the power of composability. A contract on the source chain composes an AMM and Stargate, and a destination contract which has access to an AMM implements sgReceive() receives the tokens and a payload to perform additional logic.
swapNativeForNative()
The StargateComposed.sol contract can swap native on the source chain for native on the destination chain. To swap tokens to another chain using Stargate this contract must use the StargateComposer.sol to interact with Stargate. This code snippet shows how the contract uses it to swap native on the source chain for native on the destination chain.
Warning: When composing, Do Not swap() real funds to a contract address that does not implement sgReceive() or your *will* lose those funds.
/// @param dstChainId The message ordering nonce
/// @param bridgeToken The remote Bridge address
/// @param srcPoolId The token contract on the local chain
/// @param dstPoolId The qty of local _token contract tokens
/// @param nativeAmountIn The amount of native token coming in on source
/// @param to The address to send the destination tokens to
/// @param amountOutMin The minimum amount of stargatePoolId token to get out of amm router
/// @param amountOutMinSg The minimum amount of stargatePoolId token to get out on destination chain
/// @param amountOutMinDest The minimum amount of native token to receive on destination
/// @param deadline The overall deadline
/// @param destStargateComposed The destination contract address that must implement sgReceive()
function swapNativeForNative(
uint16 dstChainId,
address bridgeToken,
uint16 srcPoolId,
uint16 dstPoolId,
uint nativeAmountIn,
address to,
uint amountOutMin,
uint amountOutMinSg,
uint amountOutMinDest,
uint deadline,
address destStargateComposed
)Use the AMM Router
Using the amm router, swap native into the Stargate pool token, sending the output token to this contract.
StargateComposer.swap()
Call StargateComposer swap() to send the tokens to the destination chain.
sgReceive()
StargateComposed.sol implements IStargateReceiver so it can implement the sgReceive function to receive the tokens and payload.
Unpack payload, approve, get pre balance of toAddress
Unpack the payload to get _tokenOut,_deadline,_amountOutMin,_toAddr. Approve the amm router so it can swap our tokens. Get the pre balance of the _toAddr to emit an event of exact amount sent.
If _tokenOut is the Zero Address they will get native token
Use the amm router to swap the incoming bridge token into native token
Else they will get ERC20 token
Use the amm router to swap the incoming bridge token into an ERC20 token
Last updated
