I'm trying to test making some smart-contract that uses DAI ERC20 tokens on Kovan testnet (DAI contract on kovan is 0xc4375b7de8af5a38a93548eb8453a498222c4ff2). I developed this very simple contract that is meant to just:

  • getContractBalance() - Contract balance of DAI

  • getSenderBalance () - Sender balance of DAI

  • tranferTokenstoContract( uint _amount ) - transfer _amount DAI tokens from Sender to Contract

here is my code :

pragma solidity ^0.5.3;

    // Import OpenZeppelin's ERC20 interface defenition
    import "https://github.com/OpenZeppelin/openzeppelin-solidity/contracts/token/ERC20/IERC20.sol";
    import "https://github.com/OpenZeppelin/openzeppelin-solidity/contracts/token/ERC20/ERC20.sol";

    contract Example {
        IERC20 public associatedToken;

    // @param _token is the address of DAI in Kovan -> 0xc4375b7de8af5a38a93548eb8453a498222c4ff2 
        constructor(IERC20 _token) public {
            associatedToken = _token;

        function tranferTokenstoContract( uint256 _ammount) public returns (bool) {
            return associatedToken.transfer(address(this), _ammount);

          function getContractBalance() public view returns (uint256) {
            return associatedToken.balanceOf(address(this));

        function getSenderBalance() public view returns (uint256) {
            return associatedToken.balanceOf(address(msg.sender));


I'm using injected Web3 with metamask and I have 82 DAI tokens in it (Kovan testnet). Whenever I call getSenderBalance(), it works fine and returns 82000000000000000000 as expected. Although when I call tranferTokenstoContract() and try to send some tokens to the contract, I get a "Gas estimation errored" warning and my transaction always fails. I'm using Remix, but I also tried it on MyEtherWallet. Both unsuccessful. Also I tried moving around with the gas but I was not successful either.

Am I doing this right? Feel free to try it yourself. Just pass in 0xc4375b7de8af5a38a93548eb8453a498222c4ff2 to the constructor, which is the DAI contract on the Kovan network

I would appreciate any help.

1 Answer 1


When calling associatedToken.transfer(address(this), _ammount) the caller of this transaction is your contract, so it tries to transfer contract's 82 DAI to itself. To transfer DAI you should be the caller of the transaction with transfer() function call. You should upload DAI's code to Remix and use "At address" button and then call transfer() function at DAI's contract with your contract's address passed as a function argument (also, you can use etherscan to call DAI's functions).

Another option is to use approve() + transferFrom() functions:

  • Firstly, you need to approve your DAI to your contract. Thus, the contract will be able to use tokens on your behalf.
  • After that your contract can call transferFrom() function with your address passed as from argument and take your tokens.
  • Ok, that makes a lot of sense! I will re-develop this contract to implement aprove() and transferFrom() and try it again. I will let you know the result in a few minutes. Thank you in advance for the clarification. Commented Aug 30, 2019 at 10:29
  • Yup, it worked. Thanks a lot. Although now I have another question. By using a contract to use approve and transferFrom I have to pay gas.. Is there a way to avoid this? Commented Aug 30, 2019 at 11:19
  • There are some copmlex solutions that you can use if you do not want your users to have ether to use your contract. The main idea is to ask users to sign transactions, but not to send them to blockchain. After that you send the transaction for them, pay gas and take some fee from users (in tokens). Read more about OpenZeppelin's GSN Commented Aug 30, 2019 at 13:44
  • Alright .. that sounds reasonable. Thanks for all your help. Commented Aug 30, 2019 at 14:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.