hiro
hiro

Reputation: 41

PancakeSwap Contract / Swaping token for another token

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);
}

Upvotes: 4

Views: 2414

Answers (1)

n3onis
n3onis

Reputation: 105

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"].

Upvotes: 1

Related Questions