I am new to solidity. My MSHIB contract collects MSHIB tokens from tax for every transaction. Once the number of tokens reaches a particular threshold it should swap those tokens to BNB. I also have function that allows the owner to trigger this manually.
//This function swaps a {tokenAmount} of MSHIB tokens for BNB and returns the total amount of BNB received
function swapTokensForBNB(uint256 tokenAmount) public returns(uint256) {
uint256 initialBalance = address(this).balance;
// Generate pair for MSHIB -> WBNB
address[] memory path = new address[](2);
path[0] = address(this);
path[1] = _pancakeswapV2Router.WETH();
approve(address(this), tokenAmount);
// Swap
_pancakeswapV2Router.swapExactTokensForETHSupportingFeeOnTransferTokens(tokenAmount, 0, path, address(this), block.timestamp + 360);
// Return the amount received
return address(this).balance - initialBalance;
}
I am calling this function manually but it throws an error saying "insufficient liquidity". I am wondering how it can have an insufficient liquidity when i am able to buy and sell the tokens through pancakeswap testnet (https://pcs.nhancv.com/#/swap). Is it not swapping because i am on testnet? Do i need to provide the liquidity Pair address to the router? Can anyone please help me on this? Thank you in advance.