PancakeSwap Contract / Swaping token for another token

2.2k views Asked by At

I want to swap some testnet bep20 tokens to busd(bep20) with pancakeswap testnet contarct through web3.

I looked into the contract but I can't get what "amountOutMin" and "path" mean.

How can I swap them with this contract?

Pancakeswap testnet contract

function swapExactTokensForTokens(
    uint amountIn,
    uint amountOutMin,
    address[] calldata path,
    address to,
    uint deadline
) external virtual override ensure(deadline) returns (uint[] memory amounts) {
    amounts = PancakeLibrary.getAmountsOut(factory, amountIn, path);
    require(amounts[amounts.length - 1] >= amountOutMin, 'PancakeRouter: INSUFFICIENT_OUTPUT_AMOUNT');
    TransferHelper.safeTransferFrom(
        path[0], msg.sender, PancakeLibrary.pairFor(factory, path[0], path[1]), amounts[0]
    );
    _swap(amounts, path, to);
}
1

There are 1 answers

0
n3onis On

amountOutMin is the minimum amount you want to receive. If the amount is less than that, the transaction will be reverted. It's like setting the slippage on PancakeSwap.

Path is the path that the swap takes. If there is a direct path, e.g. Token -> WBNB, the path would simply be ["TOKEN_CONTRACT_ADDRESS", "WBNB_CONTRACT_ADDRESS"].