0

I'm trying to make a simple transferFrom within my contract using remix.

Contract code:

pragma solidity >=0.7.0 <0.9.0;

// Import OpenZeppelin's ERC20 interface defenition
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v4.0.0/contracts/token/ERC20/IERC20.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v4.0.0/contracts/token/ERC20/ERC20.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v4.0.0/contracts/token/ERC20/utils/SafeERC20.sol";



// Simple ERC20 token that mints the given initialSupply to the deployer
contract MYTOKEN is ERC20  {
    constructor()  ERC20("mytoken", "MYTOKEN") {
        _mint(msg.sender, 100000000 * (10 ** uint256(decimals())));
    }
    
   function transferFrom(address sender, address recipient, uint256 amount) public virtual override returns (bool) {

    }
}

contract SmartSub {
    using SafeERC20 for MYTOKEN;
    

    mapping(address => uint256) public bank;

    function deposit(uint amount) public {
        // The address is MYTOKEN address after i have deployed it
        MYTOKEN(0x0fC5025C764cE34df352757e82f7B5c4Df39A836).transferFrom(msg.sender, address(this), amount);
        bank[msg.sender] += amount;
    }

}

beforehand i approve 10000 tokens, where i allow the contract address to transfer MYTOKEN from my account (the one i created all contracts with). After calling allowance with contract address i see the amount i have allowed before as expected.

Then i head to deposit function, where i input 10. After executing it, i see success but when i check the balance of my contract address i still see 0.

Why are the tokens not being transferred? What am i doing wrong?

1

Why do you override transferFrom? By doing so the original implementation is ignored and transferFrom does nothing.

If you want to do something extra and keep the implementation you can call the parent by using super.

function transferFrom(address sender, address recipient, uint256 amount) public virtual override returns (bool) {

    // Prepare things here

    bool result = super.transferFrom(sender, recipient, amount);

    // Do something here

    return result;
}
1
  • ahhh, indeed i shouldn't be doing that, thanks removing transferFrom from the erc20 completely, fixed my issue. I was fooled by errors that have happened before hand and i thought that i need to define transferFrom on the erc20 as if it was an interface. – Arin Apr 17 at 5:48

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.