0

I have my ExampleToken contract from Openzeppelin:

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.7;

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

contract ExampleToken is ERC20 {

    address public owner;

    constructor() ERC20('ExampleToken', 'EXM') {
        _mint(msg.sender, 1000000 * 10**18);
        owner = msg.sender;
    }
}

And I want my other contract to manage those tokens. I was trying to create function which will send 100 of my ExampleToken to anyone who call this function. And even if remix output showed that transaction was correct, I couldn't see any balance changes either on my contract balance, or msg.sender balance.

Here is my other contract:

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.7;

import './ExampleToken.sol';

contract MyContract {

    address public owner;
    uint public EXMbalance;
    ExampleToken public exampleToken;

    constructor() {
        owner = msg.sender;
        exampleToken = new ExampleToken();
        EXMbalance = exampleToken.balanceOf(address(this));
    }

    function getBalanceOf(address adr) public view returns(uint) {
        return exampleToken.balanceOf(adr) / 10**18;
    }

    function contractBalance() public view returns(uint) {
        return address(this).balance ;
    }

    function requestEXM(uint quantity) public {
        require(quantity <= 100 && quantity > 0);

        exampleToken.approve(address(this), quantity);
        exampleToken.transferFrom(address(this), msg.sender, quantity);
        EXMbalance -= quantity;
    }

    function invest() external payable {

    }

}

Output in remix when I call requestEXM function: enter image description here

4
  • Take a look at his answer that explain how transfer and transferFrom work ethereum.stackexchange.com/a/46458
    – Ismael
    Jan 24, 2022 at 7:17
  • @Ismael I actually moved from transfer function to transferFrom cause it seemed like it's not working too, I couldn't see balance of my coin, it was still 0. Now I realized that I was sending 100 WEI and I was reading the balance converted to ETH (* 10**18). I moved back to transfer function, fixed the converting problem, and it's working fine. Thanks.
    – BlackH3art
    Jan 24, 2022 at 13:48
  • It would be nice if you could add the resulting fix as answer so the question can be marked as resolved.
    – Ismael
    Jan 25, 2022 at 6:20
  • Thank you for pointing that, I posted the answer.
    – BlackH3art
    Jan 25, 2022 at 12:37

2 Answers 2

1

My mistake was that function requestEXM is transfering 100 in WEI and my reading balance function getBalanceOf is returning value in ETHER, so that was the reason why I saw the 0 balance at first place using .transfer and then .transferFrom function of ERC20 contract.

As was mentioned above there's no need for approve function, and transferFrom function as it's not 3rd party transfer.

So the proper way of transferring tokens in this case could be like this:

function requestEXM(uint quantity) public {
        
        require(quantity <= 100 && quantity > 0);

        exampleToken.transfer(msg.sender, quantity * 10**18);
        EXMbalance -= quantity * 10**18;
    }
0

First the requestEXM function does not make sense. This line

exampleToken.approve(address(this), quantity);

Tries to approve your contract to spend quantity tokens which is not required. Your contract is the owner of it's tokens, your contract doesn't need any approval to spend it's own tokens.

1
  • Yes, I moved back to transfer function. It's working now, I was converting balance from WEI to ETHER with * 10**18, which was the reason why I didn't see the balance.
    – BlackH3art
    Jan 24, 2022 at 13:53

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.