Debugging pancake swap API request

Job ID: 34471320

Budget: €8 – €40 EUR

I'm currently facing issues with pancake swap API request, I'm using ethers js librabry.
When trying to swap tokens I get sometimes error with error code CALL_EXCEPTION.
I need someone who understands this API and can pin point what is missing or needs to be changed in order to make every token swap successful.

Here is the script that I'm running:

async function buy_coin() {

const ethers = require("ethers");

const WBNB = "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c";
const TokenOut = "0x748ed23b57726617069823dc1e6267c8302d4e76";

const router = "0x10ED43C718714eb63d5aA57B78B54704E256024E";

const recipient = "wallet_address";

const provider = new ethers.providers.JsonRpcProvider("https://bsc.getblock.io/mainnet/?api_key=key");

const mnemonic = "phrase";

const wallet = new ethers.Wallet.fromMnemonic(mnemonic);

const signer = wallet.connect(provider);

const routerContract = new ethers.Contract(
router,
[
'function getAmountsOut(uint amountIn, address[] memory path) public view returns (uint[] memory amounts)',
'function swapExactTokensForTokens(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline) external returns (uint[] memory amounts)'
],
signer
);

const WBNBamountIn = ethers.utils.parseUnits("0.01", "ether");
let amounts = await routerContract.getAmountsOut(WBNBamountIn, [WBNB, TokenOut]);
const TokenOutamountOutMin = amounts[1].sub(amounts[1].div(10));

const swapTx = await routerContract.swapExactTokensForTokens(
WBNBamountIn,
TokenOutamountOutMin,
[WBNB, TokenOut],
recipient,
Date.now() + 1000 * 60 * 10,
{gasLimit: 350000}
)

let receipt = await swapTx.wait();
console.log(receipt);
}
buy_coin();
Related categories: Node.js Blockchain Ethereum Solidity Smart Contracts