To perform a cross chain swapOutToken() in solidity, here is an example:
For the native gas fee required for swapOutToken() you need to call getNativeFee() to get the amount you should send as msg.value.
getNativeFee() interface;
functiongetNativeFee(address_token,//bridge tokenuint256_gasLimit,// call gas limit on target chainuint256_toChain// target chain id ) externalviewreturns (uint256);
swapOutToken() interface
functionswapOutToken(address_sender,// user account send this transationaddress_token,// src tokenbytesmemory_to,// receiver account (if _swapData not empty _to must contract who implement IButterReceiver)uint256_amount,// token amountuint256_toChain,// target chain idbytescalldata_swapData ) externalpayablereturns (bytes32 orderId);
First determine the swap fee by call getNativeFee(), then call swapOutToken() to transfer the asset to the destination chain.
// get native feeuint256 value =IButterBridgeV3(bridgeAddress).getNativeFee(tokenοΌgasLimitοΌtoChain);// perform a butter swapOutToken() in a solidity smart contract functionIButterBridgeV3(bridgeAddress).swapOutToken{value:value}( msg.sender,// user account send this transatio token,// bridge token (zero address for native token) toAddress,// target chain receiver address amount,// bridge token amount toChain,// target chain id swapDat // encode swap Data);
To perform a swapOutToken() using ethers via a frontend, use the abi and call swapOutToken() on the Router contract instance:
let [wallet] =awaitethers.getSigners();let nativeFee =awaitbridge.getNativeFee(token.address,gasLimit,tochain);// if token if ERC20 tokenawait(awaittoken.approve(bridge.address,amount)).wait()let BridgeParam = { gasLimit: gasLimit,// gas limit called by IButterReceiver refundAddress:wallet.address,// for src token is OmniToken to receiver refund native fee on target chain swapData: swapData,// IButterReceiver -> onReceived -> _payload };let swapData =ethers.utils.defaultAbiCoder.encode( ["tuple(uint256,bytes,bytes)"], [[BridgeParam.gasLimit,BridgeParam.refundAddress,BridgeParam.swapData]] );let tx =awaitbridge.swapOutToken(wallet.address,token.address,to,amount,swapData,{value:nativeFee})
BridgeParam.swapData if need call contract on target chain otherwise set '0x' for it
need call contract on target chain ? must make sure parameter _to is a contract and implement IButterReceiver.
IButterReceiver
interface IButterReceiver {//_srcToken received token (wtoken or erc20 token)functiononReceived(bytes32_orderId,// order Idaddress_srcToken,// received tokenuint256_amount,// received token amountuint256_fromChain,// from chainbytescalldata_from,// from accountbytescalldata_payload// call data ) external;}