0

I am getting Error: Returned error: VM Exception while processing transaction: revert when testing MyContract.deposit()

Here are simplified versions of my code to highlight where the issue is occuring (on token.transferFrom())

//MyContract.sol

contract IErc20 {
  function transferFrom(address, address, uint) public returns (bool);
}

contract MyContract {
  IErc20 public token;

  constructor(address _token) {
    token = IErc20(_token);
  }

  function deposit(uint _amount) public {
    require(token.transferFrom(msg.sender, address(this), _amount);
  }
}

Here is the unit test file, which I am running with truffle test on ganache-cli

//MyContract.test.js

const { BN } = require("openzeppelin-test-helpers");
const MyContract = artifacts.require("MyContract");
const ERC20Mock = artifacts.require("ERC20Mock");

contract("MyContract", function([_, adminRole, user]) {
  const dai = toBytes("DAI");
  let daiAddress;
  beforeEach(async function () {
    daiAddress = (await web3.eth.getAccounts())[5];
    this.daiToken = ERC20Mock.new(daiAddress, 100*(10**6));
    this.MyContract = await MyContract.new(
      this.daiToken.address
    ); 
    await this.daiToken.approve(user, amount, {from: daiAddress});
    await this.daiToken.transferFrom(daiAddress, user, amount);
  });

  describe("deposit()", function() {
    const amount = new BN(1);
    it("should deposit if tokens are approved by user", async function () {

       await this.daiToken.approve(this.MyContract.address, amount, {from: user});
       await this.MyContract.deposit(amount, {from: user});  //REVERTS!
    });
  });
});

Here is the migrations script:

// migrations/deploy_dai.js

const ERC20Mock = artifacts.require("ERC20Mock");

module.exports = async function(deployer, network, accounts) {
    if (network === "development") {
        console.log("Deploying MockDAI on Development");
        await deployer.deploy(ERC20Mock, accounts[5], 100*(10**6), {from: accounts[5]);
    }
}

This is the Mock ERC20 token, which I am deploying on the development blockchain:

//ERC20Mock.sol

pragma solidity ^0.5.0;

import "openzeppelin-solidity/contracts/token/ERC20/ERC20.sol";

// mock class using ERC20
contract ERC20Mock is ERC20 {
    constructor (address initialAccount, uint256 initialBalance) public {
        _mint(initialAccount, initialBalance);
    }

    function mint(address account, uint256 amount) public {
        _mint(account, amount);
    }

    function burn(address account, uint256 amount) public {
        _burn(account, amount);
    }

    function burnFrom(address account, uint256 amount) public {
        _burnFrom(account, amount);
    }
}
  • Clearly, token.transferFrom returns false. Have you checked why? – goodvibration Feb 20 at 17:18
  • Yes, I am trying to figure out why. Since my original post I have made a modification as I realised that the {from: user} account was never funded with dai to send to the MyContract.deposit but have since changed this. – isaacsultan Feb 20 at 17:37
  • @goodvibration still no luck – isaacsultan Feb 20 at 17:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.