0

This is my following Code. I am using the Rinkeby network.

//SPDX-License-Identifier: MIT
pragma solidity ^0.8;

import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/IERC20.sol";

contract TransferDAI{
    IERC20 public dai;
   
    constructor() {
        dai = IERC20(0x5592EC0cfb4dbc12D3aB100b257153436a1f0FEa);
    }
    
    
    function Transfer(uint _amount) external {
        dai.transferFrom(msg.sender, address(this), _amount);
    }
   
    // Some extra functions for now...
    function getDaiBalance(address _address) external view returns(uint) {
        return dai.balanceOf(_address);
    }
}

I am not able to transfer Dai to the contract address. Although I can view getDaiBalance and see the correct amount of DAIs.

Any lead will be helpful. I tried using dai.approve(msg.sender,_amount) it wasn't helpful.

1 Answer 1

0

dai.approve(msg.sender, _amount) lets msg.sender spend _amount of tokens that are in the contract balance. Not the other way around. You need to have your wallet call approve directly on the DAI contract address, with your contract address and amount as inputs.

4
  • Thank you for your response... does it mean that in the Transfer function, it should be dai.approve(address(this), _amount)? Aug 12, 2021 at 19:55
  • Actually i answered almost the same question a little while back, here it is ethereum.stackexchange.com/questions/106390/…
    – Foxxxey
    Aug 12, 2021 at 23:10
  • Thank you, but I still struggling hard to understand how things are working. If you could prove me with the code, it will be very helpful and quite instant to understand. Thank you!. "You need to have your wallet call approve directly on the DAI contract address", Does it mean that I need to send transaction approve like, dai.approve(address(this), _amount).? Aug 13, 2021 at 10:54
  • thank you again!! After reading thoroughly, I was able to solve this issue.. Thank you for providing the external link. It was very helpful. Aug 13, 2021 at 13:00

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.