Unrecognized Contract Error: VM Exception while processing transaction: reverted with reason string 'ERC20: transfer amount exceeds balance'

This is the full error I'm getting:

Error: VM Exception while processing transaction: reverted with reason string 'ERC20: transfer amount exceeds balance'
at . (0x5fbdb2315678afecb367f032d93f642f64180aa3)
at Dex.depositToken (contracts/Wallet.sol:42)
at HardhatNode._mineBlockWithPendingTxs (node_modules\hardhat\src\internal\hardhat-network\provider\node.ts:1650:23)
at HardhatNode.mineBlock (node_modules\hardhat\src\internal\hardhat-network\provider\node.ts:459:16)
at EthModule._sendTransactionAndReturnHash (node_modules\hardhat\src\internal\hardhat-network\provider\modules\eth.ts:1496:18)
at HardhatNetworkProvider.request (node_modules\hardhat\src\internal\hardhat-network\provider\provider.ts:117:18)
at EthersProviderWrapper.send (node_modules@nomiclabs\hardhat-ethers\src\internal\ethers-provider-wrapper.ts:13:20)

This is the code:

describe('Wallet', () =>{

    let eth;

    let dai;

    let dex;

    let accounts;

    beforeEach( async () =>{

        const ethContract = await hre.ethers.getContractFactory('ETH', {

            value: ethers.utils.parseEther("0.1")

        });

        const daiContract = await hre.ethers.getContractFactory('Dai');

        const dexContract = await hre.ethers.getContractFactory('Dex', {

            value: ethers.utils.parseEther("0.1")

        });

        eth = await ethContract.deploy();

        dai = await daiContract.deploy();

        dex = await dexContract.deploy();

        await eth.deployed();

        await dai.deployed();

        await dex.deployed();

        accounts = await hre.ethers.getSigners();

        ethSymbol = ethers.utils.formatBytes32String(await eth.symbol());

        daiSymbol = ethers.utils.formatBytes32String("DAI");

        await eth.mint(accounts[1].address, 500);

        await eth.mint(accounts[0].address, 500);

        await eth.mint(accounts[2].address, 50);

        await eth.mint(accounts[3].address, 50);

        // totalSupply 1000

        await dex.addToken(ethSymbol, eth.address);

        await eth.approve(dex.address, 500);

        await eth.connect(accounts[1]).approve(dex.address, 500);

        await dex.connect(accounts[1]).depositToken(500, ethSymbol);

        await eth.approve(accounts[2].address, 50);

        await eth.connect(accounts[2]).approve(dex.address, 50);

        await dex.connect(accounts[2]).depositToken(500, ethSymbol);

    });

And the depositToken code:

function depositToken(uint256 amount, bytes32 ticker) tokenExist(ticker) external payable{

        require(amount > 0, "MasterChef::depositToken can not send non-negative value");

        m_totalBalance[ticker] += amount;

        IERC20(m_tokens[ticker].tokenAddress).transferFrom(msg.sender, address(this), amount);

        m_traderBalances[msg.sender][ticker] = m_traderBalances[msg.sender][ticker].add(amount);

        emit Deposit(address(this), amount);

    }

The error:

await eth.approve(accounts[2].address, 50);
        await eth.connect(accounts[2]).approve(dex.address, 50);
        await dex.connect(accounts[2]).depositToken(500, ethSymbol);

IT WORKED COMPLETELY FINE, when I connect to accounts[1] but not accounts[2]

But it still gives me an error. I have no clue why.

1 Like

Hi, welcome! :wave:

It seems like your approval amount is 50, but you want to deposit 500, so maybe you can increase your approval amount and then have a try.

1 Like

This is the full error I'm getting:

Error: VM Exception while processing transaction: reverted with reason string 'ERC20: transfer amount exceeds balance'
at . (0x5fbdb2315678afecb367f032d93f642f64180aa3)
at Dex.depositToken (contracts/Wallet.sol:42)
at HardhatNode._mineBlockWithPendingTxs (node_modules\hardhat\src\internal\hardhat-network\provider\node.ts:1650:23)
at HardhatNode.mineBlock (node_modules\hardhat\src\internal\hardhat-network\provider\node.ts:459:16)
at EthModule._sendTransactionAndReturnHash (node_modules\hardhat\src\internal\hardhat-network\provider\modules\eth.ts:1496:18)
at HardhatNetworkProvider.request (node_modules\hardhat\src\internal\hardhat-network\provider\provider.ts:117:18)
at EthersProviderWrapper.send (node_modules@nomiclabs\hardhat-ethers\src\internal\ethers-provider-wrapper.ts:13:20)

This is the code:

describe('Wallet', () =>{

    let eth;

    let dai;

    let dex;

    let accounts;

    beforeEach( async () =>{

        const ethContract = await hre.ethers.getContractFactory('ETH', {

            value: ethers.utils.parseEther("0.1")

        });

        const daiContract = await hre.ethers.getContractFactory('Dai');

        const dexContract = await hre.ethers.getContractFactory('Dex', {

            value: ethers.utils.parseEther("0.1")

        });

        eth = await ethContract.deploy();

        dai = await daiContract.deploy();

        dex = await dexContract.deploy();

        await eth.deployed();

        await dai.deployed();

        await dex.deployed();

        accounts = await hre.ethers.getSigners();

        ethSymbol = ethers.utils.formatBytes32String(await eth.symbol());

        daiSymbol = ethers.utils.formatBytes32String("DAI");

        await eth.mint(accounts[1].address, 500);

        await eth.mint(accounts[0].address, 500);

        await eth.mint(accounts[2].address, 50);

        await eth.mint(accounts[3].address, 50);

        // totalSupply 1100

        await dex.addToken(ethSymbol, eth.address);

        await eth.approve(dex.address, 500);

        await eth.connect(accounts[1]).approve(dex.address, 500);

        await dex.connect(accounts[1]).depositToken(500, ethSymbol);

        await eth.approve(dex.address, 50);

        await eth.connect(accounts[2]).approve(dex.address, 50);

        await dex.connect(accounts[2]).depositToken(500, ethSymbol);

    });

And the depositToken code:

function depositToken(uint256 amount, bytes32 ticker) tokenExist(ticker) external payable{

        require(amount > 0, "MasterChef::depositToken can not send non-negative value");

        m_totalBalance[ticker] += amount;

        IERC20(m_tokens[ticker].tokenAddress).transferFrom(msg.sender, address(this), amount);

        m_traderBalances[msg.sender][ticker] = m_traderBalances[msg.sender][ticker].add(amount);

        emit Deposit(address(this), amount);

    }

The error:

await eth.approve(dex.address, 50);
        await eth.connect(accounts[2]).approve(dex.address, 50);
        await dex.connect(accounts[2]).depositToken(500, ethSymbol);

When I connect with accounts[1], IT WORKED COMPLETELY FINE.

But if I connect to accounts[2], IT DOESN'T WORK.

I am approve() the dex contract while connected to accounts[2] before I call transferFrom().

But it still gives me an error. I have no clue why.

Using hardhat, chai and openZeppelin extension.

1 Like

Please do not ask the same question for many times, I have answered at the previous one, you can have a look: Unrecognized Contract Error: VM Exception while processing transaction: reverted with reason string 'ERC20: transfer amount exceeds balance' - Support - OpenZeppelin Community

I will close this duplicate post.