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.