1

I want to transfer Dai tokens from account1 to account2. My account1 has 50 DAI tokens.
Kovan Dai contract address given by makerdao is: 0x4F96Fe3b7A6Cf9725f59d353F723c1bDb64CA6Aa.

What I am doing is,
1. I have signed in metamask by account1.
2. approve balance by a method called approve(address(this), balance).
- address(this) is my deployed smart contract address.

function approve(uint _value) public {
    _daiToken.approve(address(this), _value);
}

  1. Transfer balance to account2 using transferFrom(address(this), account2, balance) => which is not working.
function transferFrom(address account2, uint _value) public {
    _daiToken.transferFrom(address(this), account2, _value);
}

But it gets an error,
Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending? The execution failed due to an exception. Reverted

I search for this error but it didn't satisfy to solve the problem. I don't know I am sending wrong address in approve and transferFrom or doing something anything wrong.

Full Code snippet
- I deployed My Contract smart contract using DAI smart contract address (0x4F96Fe3b7A6Cf9725f59d353F723c1bDb64CA6Aa).

pragma solidity ^0.5.0;

interface IERC20 {
    function totalSupply() external view returns (uint supply);
    function balanceOf(address _owner) external view returns (uint balance);
    function transfer(address _to, uint _value) external returns (bool success);
    function transferFrom(address _from, address _to, uint _value) external returns (bool success);
    function approve(address _spender, uint _value) external returns (bool success);
    function allowance(address _owner, address _spender) external view returns (uint remaining);
    function decimals() external view returns(uint digits);
    event Approval(address indexed _owner, address indexed _spender, uint _value);
}

contract owned {
    IERC20 daitoken;
    address owner;

    constructor(address addr) public{
        owner = msg.sender;
        daitoken = IERC20(addr);
    }

    modifier onlyOwner {
        require(msg.sender == owner,
                "Only the contract owner can call this function");
        _;
    }
}

contract MyContract is owned{

    address owner;

    constructor(address _daiToken)
    public 
    owned(_daiToken)
    {}

    function approves(uint256 _value) public {
        daitoken.approve(address(this), _value);
    }

    function allowance(address _owner, address _spender) public view returns(uint256) {
        return daitoken.allowance(_owner, _spender);
    }

    function transferFroms(address _to, uint256 _value) public {
        daitoken.transferFrom(address(this), _to, _value);
    }

    function transfers(address _addr, uint256 _value) external {
        require(daitoken.transfer(_addr, _value));
    }

    function balanceOf(address _addr) public view returns(uint256){
        return daitoken.balanceOf(_addr);
    }
}

6
  • Please elaborate on "I am calling allowance and it is zero" (i.e., explain how you are calling approve and how you are checking allowance). Apr 3 '20 at 5:43
  • I am calling approve using (spender, value) parameters and I am taking address(this) parameter for the spender. Then I am calling allowance using (owner, spender). I am taking address(this) parameter for both owner and spender. So, Now approve and allowance are working. But, Now I want to transfer tokens to any(e.g. _to) address. So, I am using transferFrom(address(this), _to, value). But transferFrom is not working Apr 4 '20 at 12:27
  • Please elaborate on this within your question, in a more readable (and properly formatted) manner. Apr 4 '20 at 12:31
  • I want to use remote DAI ERC20 functionality in my smart contract. I want to transfer dai tokens from accounts1 to account2. So, How can I achieve this? First I am approving (e.g. approve(address(this), value)) balance for address(this). And then I am transfering token using transferFrom method (e.g transferFrom(address(this), account2, value))) but it is not working. Apr 4 '20 at 14:07
  • Please - within your question, in a more readable (and properly formatted) manner. Apr 4 '20 at 15:06
0

You are approving the smart contract to transfer its own DAI token.

You must call the approve method first on the Kovan DAI contract address :

https://kovan.etherscan.io/address/0x4f96fe3b7a6cf9725f59d353f723c1bdb64ca6aa

This call is done by Web3, either using MetaMask or your own Web3 calls.
Using etherscan, you can go to Write Contract section and click on approve with valid parameters (you must be connected as the DAI token owner).

Parameters should be address of your smart contract deployed on Kovan and the amount you approve to be transfered.

Second, you can use your smart contract in order to call the transferFrom method.

Your Answer

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

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