exporttypeSwapRequestParam= { fromAddress:string; fromToken:BaseCurrency; toAddress:string; toToken:BaseCurrency; amountIn:string; // in minimal uint swapRouteStr:string; // cross-chain swap route, in string format. slippage?:number; // in bps, e.g. 100 = 1% options:ButterTransactionOption;};
swapRouteStr: optimal cross-chain swap route based on token and amount provided. Please see how to get the best route
ButterTransactionOption contains all the necessary information required to complete a transaction:
exporttypeButterTransactionOption= { signerOrProvider?: Signer | Provider | Eth; // When source chain is EVM provide Ethers.js Signer/Provider or Web3.js Eth info
nearProvider?:NearProviderType; // mandatory when src chain is near gas?:string; gasPrice?:string;};// when send transaction from Near ProtocoltypeNearProviderType=NearNetworkConfig|WalletConnection;
signerOrProvider: Butter supports both ethers.js and web3.js. If you are using ethers.js, provider the Signer object. If your application choose to use web3.js, please provide Eth object in order to send a transaction.
nearProvider: Whenever send a transaction from Near Protocol, you have to provide NearNetworkConfig with keystore provided or WalletConnection object
import {ButterTransactionResponse} from"./responseTypes";import {PromiEvent} from"web3-core";// create a Butter swap instance.constbutterSwap:ButterSwap=newButterSwap();// assemble swap request parametersconstswapRequest:SwapRequestParam= { fromAddress:'0x...', fromToken:BNB_NATIVE, toAddress:'0x...', toToken:MATIC_NATIVE, amountIn:ethers.utils.parseEther('1'), swapRouteStr:'{}',// too long will omit here for readability slippage:100,// 1% splippage options: { signerOrProvider:web3.eth,// here we use web3.js as example }};constresponse:ButterTransactionResponse=awaitbutterSwap.omnichainSwap( swapRequest);constpromiReceipt:PromiEvent<TransactionReceipt> =response.promiReceipt!;await promiReceipt.on('transactionHash',function (hash:string) {console.log('hash', hash); }).on('receipt',function (receipt:any) {console.log('receipt', receipt); });
Example 2: Swap 1 BNB for Matic using ethers.jsโ
import {ButterTransactionReceipt, ButterTransactionResponse} from"./responseTypes";import {PromiEvent} from"web3-core";constbutterSwap:ButterSwap=newButterSwap();// assemble swap request parametersconstswapRequest:SwapRequestParam= { fromAddress:'0x...', fromToken:BNB_NATIVE, toAddress:'0x...', toToken:MATIC_NATIVE, amountIn:ethers.utils.parseEther('1'), swapRouteStr:'{}',// too long will omit here for readability slippage:150,// 1.5% splippage options: { signerOrProvider:ethers.signer,// here we use web3.js as example },};constresponse:ButterTransactionResponse=awaitbutterSwap.omnichainSwap( swapRequest);console.log("transaction hash",response.hash!)constreceipt:ButterTransactionReceipt=awaitresponse.wait!();console.log('receipt', receipt)