Error with BEP20/ERC20 "Fail with error 'BEP20: transfer amount exceeds allowance''

This is a betting Smart contract using erc20 token source:https://github.com/laronlineworld/betERC20/blob/main/beterc20.sol

I encountered this error when betting

 IERC20 token = IERC20(0x9d47e0bd606b4b208C035D1ccA619c5562875BB1);
    function bet(uint8 _teamSelected, uint256 amount) public  {
            require(bettingActive);
            //The first require is used to check if the player already exist
            require(!checkPlayerExists(msg.sender));
            //The second one is used to see if the value sended by the player is
            //Higher than the minimum value
            require(amount >= minimumBet);
    
    
            //To roll in the Token, this line of code is executed on the condition that the user has approved a contract to use his Token
            //IERC20 is Token
            token.transferFrom(msg.sender,address(this),amount);
    
            //We set the player informations : amount of the bet and selected team
            playerInfo[msg.sender].amountBet = amount;
            playerInfo[msg.sender].teamSelected = _teamSelected;
    
            //then we add the address of the player to the players array
            players.push(msg.sender);
    
            //at the end, we increment the stakes of the team selected with the player bet
            if ( _teamSelected == 1){
                totalBetsOne += amount;
            }
            else{
                totalBetsTwo += amount;
            }
        }

and also when distributing prize

// Generates a number between 1 and 10 that will be the winner
    function distributePrizes(uint16 teamWinner) public onlyOwner {
        require(bettingActive == false);
        address[1000] memory winners;
        //We have to create a temporary in memory array with fixed size
        //Let's choose 1000
        uint256 count = 0; // This is the count for the array of winners
        uint256 LoserBet = 0; //This will take the value of all losers bet
        uint256 WinnerBet = 0; //This will take the value of all winners bet

        //We loop through the player array to check who selected the winner team
        for(uint256 i = 0; i < players.length; i++){
            address playerAddress = players[i];

            //If the player selected the winner team
            //We add his address to the winners array
            if(playerInfo[playerAddress].teamSelected == teamWinner){
                winners[count] = playerAddress;
                count++;
            }
        }

        //We define which bet sum is the Loser one and which one is the winner
        if ( teamWinner == 1){
            LoserBet = totalBetsTwo;
            WinnerBet = totalBetsOne;
        }
        else{
            LoserBet = totalBetsOne;
            WinnerBet = totalBetsTwo;
        }


        //We loop through the array of winners, to give ethers to the winners
        for(uint256 j = 0; j < count; j++){
            // Check that the address in this fixed array is not empty
            if(winners[j] != address(0))
             address add = winners[j];
             uint256 bet = playerInfo[add].amountBet;
            //Transfer the money to the user
//            winners[j].transfer(    (bet*(10000+(LoserBet*devFee /WinnerBet)))/10000 );

            token.transferFrom(address(this), winners[j],  (bet*(10000+(LoserBet*devFee /WinnerBet)))/10000 );

        }
        delete playerInfo[playerAddress]; // Delete all the players
        players.length = 0; // Delete all the players array
        LoserBet = 0; //reinitialize the bets
        WinnerBet = 0;
        totalBetsOne = 0;
        totalBetsTwo = 0;
    }

Note: I already try to approve ang increase the allowance of the contract but still get the same error "BEP20/ERC20: transfer amount exceeds allowance"

If you check out the OZ implementation of the transferFrom function, you'll see that it checks that currentAllowance of the sender is greater than the sending amount, even if the sender is the owner.

So, if you are getting "transfer amount exceeds allowance" when transferring funds from the custody of the contract to the recipient, it's because you are not calling allow earlier in your distributePrizes function to allow the contract to spend its own funds with the transferFrom function. So, you might just try replacing

token.transferFrom(address(this), winners[j],  (bet*(10000+(LoserBet*devFee /WinnerBet)))/10000 );

with

token.transfer(winners[j],  (bet*(10000+(LoserBet*devFee /WinnerBet)))/10000 );

if distributePrizes is the specific function you are having problems with.

How about in bettting

 IERC20 token = IERC20(0x9d47e0bd606b4b208C035D1ccA619c5562875BB1);
    function bet(uint8 _teamSelected, uint256 amount) public  {
            require(bettingActive);
            //The first require is used to check if the player already exist
            require(!checkPlayerExists(msg.sender));
            //The second one is used to see if the value sended by the player is
            //Higher than the minimum value
            require(amount >= minimumBet);
    
    
            //To roll in the Token, this line of code is executed on the condition that the user has approved a contract to use his Token
            //IERC20 is Token
            token.transfer(msg.sender,address(this),amount);
    
            //We set the player informations : amount of the bet and selected team
            playerInfo[msg.sender].amountBet = amount;
            playerInfo[msg.sender].teamSelected = _teamSelected;
    
            //then we add the address of the player to the players array
            players.push(msg.sender);
    
            //at the end, we increment the stakes of the team selected with the player bet
            if ( _teamSelected == 1){
                totalBetsOne += amount;
            }
            else{
                totalBetsTwo += amount;
            }
        }

I got this error in

token.transfer(msg.sender,address(this),amount);
    

Error:TypeError: Member "transfer" not found or not visible after argument-dependent lookup in contract IERC20
token.transfer(msg.sender,address(this),amount);

Right, leave the bet function the way you had it using transferFrom (because the contract is pulling funds from the use's wallet) and change distributePrizes to just transfer (since in this case it is the contract itself transferring its own funds to a recipient.

Note: Just to be clear, before a user makes a transaction calling bet, the user must first make a transaction on their ERC20 token they are using that calls erc20token.approve(<bettingContractAddress>,amount), otherwise you will still get "BEP20/ERC20: transfer amount exceeds allowance

1 Like

I did all the instructions, but I get the same error "transfer amount exceeds allowance'

if you've got a github repo you're working out of or a test script you are running to reproduce your error, you can paste it here and I can could take a look.

https://github.com/laronlineworld/betERC20/blob/main/beterc20.sol

This is my github repo mate, thank you.