Swap

Swap

Swaps typically occur in two steps:

  • the first step involves pre-calculating the potential result of the current transaction;

  • the second step is to set the slippage based on the pre-calculated results, followed by executing the transaction.

After pre-calcualting then you can do swap via:

Swap after preswap

After pre-calcualting then swap. For a more detailed understanding of the pre-swap process and its intricacies, additional information is available here.

Function input param

Please refer to the original function for specific parameter types.

  • poolID: pool object id, you can get it by pre-calcualting.

  • coinTypeA: the coin type address about coinA.

  • coinTypeB: the coin type address about coinB.

  • a2b: swap direction, true means swap from coinA to coinB, false means swap from coinB to CoinA.

  • byAmountIn: true means fixed the amount of input, false means fixed the amount of output.

  • amount: the amount of input (byAmountIn = true) or output (byAmountIn = false).

  • amountLimit: the amount limit of coin what you get. There are two scenarios in amount limit, when by_amount_in equals true, amount limit means minimum number of outputs required to be obtained, when by_amount_in equals false, it means maximum number of input coin.

  • partner: The partner address. If you do not have a partner, simply leave the partner field unset.

    Notice:

    • This is the amount out of result after slippage adjustment. Use adjustForSlippage to calculate the limit of amount out.

    • If you set amount limit equal 0, when you trade during extremely volatile price fluctuations, you might end up with a very small trading outcome. The amountLimit will help prevent your assets from incurring losses.

    • You can get more details in these Partner swap parts.

Example

Swap without transfer coins

This methods support return two coins for user to build PTB.

Function input param

Please refer to the original function for specific parameter types.

  • poolID: pool object id, you can get it by pre-calcualting.

  • coinTypeA: the coin type address about coinA.

  • coinTypeB: the coin type address about coinB.

  • a2b: swap direction, true means swap from coinA to coinB, false means swap from coinB to CoinA.

  • byAmountIn: true means fixed the amount of input, false means fixed the amount of output.

  • amount: the amount of input (byAmountIn = true) or output (byAmountIn = false).

  • amountLimit: the amount limit of coin what you get. There are two scenarios in amount limit, when by_amount_in equals true, amount limit means minimum number of outputs required to be obtained, when by_amount_in equals false, it means maximum number of input coin.

  • partner: The partner address. If you do not have a partner, simply leave the partner field unset.

    Notice:

    • This is the amount out of result after slippage adjustment. Use adjustForSlippage to calculate the limit of amount out.

    • If you set amount limit equal 0, when you trade during extremely volatile price fluctuations, you might end up with a very small trading outcome. The amountLimit will help prevent your assets from incurring losses.

    • You can get more details in these Partner swap parts.

Example

Last updated