I am creating a smart contract to deposit and withdraw USDT token. But I am getting error while depositing tokens saying that "ERC20: insufficient allowance"
function depositTokens(uint256 amount, string memory symbol) external {
ERC20(whitelistedTokens[symbol]).approve(address(this),amount);
accountBalances[msg.sender][symbol] += amount;
ERC20(whitelistedTokens[symbol]).transferFrom(msg.sender, address(this), amount);
}
I have even tried with IERC20 token too.
function depositTokens(string memory token,uint _amount) external payable{
// Check the user's allowance for the specified token.
uint256 allowance = ERC20(whitelistedTokens[token]).allowance(msg.sender, address(this));
// If the user's allowance is less than the amount they are trying to deposit, revert.
if (allowance < _amount) {
revert("Insufficient allowance");
}
// Transfers the amount of USDT/USDC from user to smart contract.
ERC20(whitelistedTokens[token]).transferFrom(msg.sender, address(this), _amount);
//Adding collateral amount to user's colateral account balance.
userCollateralBalance[msg.sender][token] += calculateAmountOfINRDfromUSD(_amount);
}