transferFrom fails with error 'Dai/insufficient-allowance'

Hello guys! I am trying to deposit kovan dai to my smart contract by using transferFrom of the ERC20 function. But I get an the ‘Dai/insufficient-allowance’.So I tried approving it explicitly and then trying to transfer… The approval works but this the transferFrom doesnt. Please help me. Just started with ERC 20.

The code:

function appDai(uint _amount) public {
    daitoken.approve(msg.sender, _amount);
}

function depDai(uint _amount) public {
    require(daitoken.balanceOf(msg.sender) >= _amount);
    daitoken.transferFrom(msg.sender, address(this), _amount);
    emit Deposit(msg.sender, _amount);
}
1 Like

I even tried increasing the allowance usinf increaseAllowance…this call too failed.
the code:

function incDai(uint _amount) public {
    daitoken.increaseAllowance(address(this), _amount);
}

function incDaiSend(uint _amount) public {
    daitoken.increaseAllowance(msg.sender, _amount);
}
1 Like

Hi @Harshak777,

Welcome to the community :wave:

The issue is that the token holder needs to call approve on the Token rather than your contract calling approve, as your contract can’t do it on their behalf.

Please see Example on how to use ERC20 token in another contract.