Error : ERC20: transfer amount exceeds balance

contract TransferToken
    ERC20 public  Tokens;
    constructor(address _address) {
        Tokens = ERC20(_address);
    function transfer(address recipient, uint256 amount) public {
        Tokens.approve(address(this), 10000);
        Tokens.allowance(msg.sender, address(this));
        Tokens.transferFrom(msg.sender, recipient, amount);
1 Like

I am not understanding. Which is the aim of this contract?

You understood approve wrongly. Here's a simple example:

A - 100 tokens
B - 50 tokens
Smart contract (SC)

If A wants to transfer B 20 tokens, A can just do token.transfer(token, 20 * 10**decimals);.
If A wants to use the SC to transfer 20 tokens to B, A needs to do 2 things:

  1. A needs to interact with the token contract and call approve(SC.address, 20 * 10**decimals);
  2. in the SC's code, you can call token.transferFrom(A, B, 20*10**decimals);

hello mate , i tried with the SC like you referred but it is still showing the error.
I have updated the code

i just wanted to swap the tokens mate

@STYJ said another thing. You have to interact directly with the token contract and call there the APPROVE. You can't use a smartcontract to do this.
So search the token on bsc/etherscan and go on write section. On "approve" paste the TransferToken contract address on first field and on second field put the desired amount (with decimals)

yeah, that is working but it's just , my logic was to create the smart contract to ensure the token contract transaction ..

Unfortunately there is no way to approve from the contract. You should use a dapp

1 Like