I want to create a function to buy tokens from the contract and I use ERC20 standart to implement my code. I created 2 different contracts which are seperated as the first one is responsible for token creation and holding and the second one is for using the tokens to have some functionalities among these tokens.

What I want to do is creating a buy function which actually needs to transfer tokens from the main contract to the msg.sender since the msg.sender will be the one who will try to buy the token. Yet, when I write this code and run it, I face with an error that says "ERC20 Insufficient Amount" and I cannot make any transfer from the main contract to any account via the second contract.

What is the problem with the amount of tokens even if I approve it?

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

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

contract Token is ERC20{
    constructor() ERC20("MyTryToken","MTT"){

contract MyTryToken{

    ERC20 tokenControl;

    constructor(address tokenAddress){
        tokenControl = ERC20(tokenAddress);
    mapping(address=>uint) public balances;

    function buy(uint amount) public{


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.