1

I want my contract to use my token. So I coded it like this. first my token code

pragma solidity ^0.8.0;

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

contract Elena is ERC20{
    constructor(string memory name, string memory symbol) ERC20(name,symbol){
        // mint 1000 token
        _mint(msg.sender, 100000*10**18);
    }
}

and my contract

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;

import "./myToken.sol";

contract FundingTest{
    uint public constant MINIMUM_AMOUNT = 1 ether;
    address myToken;
    IERC20 private _currencyContract;

    constructor(address _myToken){
        myToken = _myToken;
        _currencyContract = IERC20(myToken);
    }


    function funding(uint sendMoney) public payable {
        _currencyContract.approve(msg.sender, sendMoney*10**18);
        _currencyContract.approve(address(this),_currencyContract.totalSupply());
        _currencyContract.transferFrom(msg.sender, address(this), sendMoney*10**18);
    }

    function currentBalance() public view returns(uint256){
        return _currencyContract.balanceOf(address(this));
    }
}

Token issuance is confirmed. But this kind of error happens enter image description here

please help me

1 Answer 1

0

First, you need to authorize your FundingTest to spend your tokens, by calling the approve function of the Elena token directly, with the address of the FundingTest contract and the amount you want to allow it to spend. Then, the FundingTest contract will be able to spend the tokens, which is what the funding() is trying to do.

Also, you could send all your tokens to the FundingTest contract directly by calling the transfer function in the Elena contract, indicating the address of the FundingTest as the recipient, and the amount you want to send it.

Calling the approve function on _currencyContract.approve(msg.sender, sendMoney*10**18);, the contract is the one approving the sender to spend the contract's token. This is not what you want. You want to allow the contract to spend the sender's tokens. And you cannot do that from the contract. You need to do it directly, calling the Elena's approve function directly.

Look at the following screenshot from Remix where I deployed both contracts. I need to call the approve function of the Elena contract directly, with the address of the FundingTest token and the amount I'm allowing it to spend. The FundingTest cannot approve itself as the spender of your tokens, so you cannot do that from the FundingTest contract. You need to do it directly from the Elena contract.

enter image description here

Notice how the FundingTest contract is deployed at address: 0xFDDbd99fCf252dB25CCC89CC209c0218C5c5aB6D

While the Elena token is deployed at address: 0xe701Cc3Bdcc2D86Bd5Bb161eE5F929eA4Dd31e84

So, I call the approve function of the Elena token with the FundingTest contract address like this:

Elena.approve(FundingTestAddress, amountToAllowToSpend);

Like:

Elena.approve(0xFDDbd99fCf252dB25CCC89CC209c0218C5c5aB6D, 100000000000000000000000);

Since you are calling the Elena contract directly (not from the FundingTest contract), then the msg.sender in the Elena.approve function will be you, your address, and thus it will authorize the FundingTest to spend your tokens.

After that, you can then call the FundingTest.funding() function and it will work fine.

5
  • 1
    Thank you. But I cat`t understand about approve function. th function request argument amount. What is this?
    – 이건희
    Commented Sep 7, 2022 at 1:16
  • The ERC20 tokens have an approve function to allow someone else to spend your tokens on your behalf. Your Elena token inherits that approve function. Just call it. docs.openzeppelin.com/contracts/2.x/api/token/erc20 Commented Sep 7, 2022 at 1:25
  • 1
    You are angel. Thank you
    – 이건희
    Commented Sep 7, 2022 at 1:28
  • 1
    I took your advice and modified the funding() function. But the same error occurs. Why?You can check the modified code in the revised article. I'm sorry to bother you
    – 이건희
    Commented Sep 7, 2022 at 1:48
  • Check the update of my answer, and get your code back to what it was before. Commented Sep 7, 2022 at 3:00

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.