4️⃣Swap Execution

Before swap, you should calculate a trade object

Swap for both On-Chain and Cross-Chain trade are pretty similar and very easy to use. You just have to call swap method in trade object (instantTrade or crossChain).

On-Chain trade example

const trades = await sdk.onChainManager.calculateTrade(...);
const bestTrade = trades[0] instanceof OnChainTrade && trades[0];
const receipt = await bestTrade.swap();

Cross-Chain trade example

const trade = await sdk.crossChain.calculateTrade(tradeParams);
const bestTrade = trades[0].trade.swap();
const transactionHash = await bestTrade.swap();

After swap execution you will get the transaction receipt or hash in case of cross-chain swap.

Well done!

Swap options

Swap method takes optional params with options:

interface SwapOptions {    
  /**
   * Callback to be called, when user confirm swap transaction.
   * @param hash Transaction hash.
   */
  onConfirm?: (hash: string) => void;

  /**
   * Callback to be called, when user confirm approve transaction.
   * @param hash Transaction hash.
   */
  onApprove?: (hash: string | null) => void;
  
  /**
   * Tokens receiver address.
   */
  receiverAddress?: string;

  /**
   * Transaction gas price options.
   */
  gasPriceOptions?: EIP1559Gas | SingleGasPrice;

  /**
   * Approve transaction gas limit.
   * Will be used for approve transaction, if it is called before swap.
   */
  approveGasLimit?: string;
  
  /**
   * Approve tron-transaction fee limit.
   * Will be used for approve transaction, if it is called before swap.
   */
  approveFeeLimit?: number;
}
 

With these parameters, you can specify your gas data or actions which will be executed right after transaction signing.

Last updated