0

I am trying to build a smart contract that is using my custom token. Below you can find the sample of smart contract that I have created that showcases the approve function.

// SPDX-License-Identifier: GPL-3.0-only
pragma solidity ^0.8.11;

import "IERC20.sol";

contract Contract {

address public contractOwner;
IERC20 token;

constructor() {
    token = IERC20(0x000000000000); //your token here
    contractOwner = msg.sender;
}

function Approvetokens(uint256 _tokenamount) public returns(bool){
   token.approve(address(this), _tokenamount);
   return true;
}

}

Then, in my react application I am using a function to call the contract like so:

async addTokens() {
let abc = process.env.myPrice // where I have tried with either 1 or 1*1000000000 (as my token has 9 digits)
await this.state.myContract.methods.Approvetokens(abc).send({
  from:this.state.account
});
}

The contract gets called and metamask is opening the transaction, but the price to pay is HUGE, please see the screenshot below. I am connected to the fantom test network, but replicated this in Rinkeby as well.

enter image description here

I do not understand what I am doing wrong here...

5
  • Do you have enough tokens in the first place to make the approval?
    – pbsh
    Commented Jan 27, 2022 at 8:04
  • Yes, I am sending 1 token from the custom token contract and I own 100k as again it is a testnet contract that I have deployed. For the testnet FTM, I own 20FTM, the usual transaction is at around 0.05 something FTM.
    – FBK
    Commented Jan 27, 2022 at 8:07
  • Also have you replaced this like with your actual token's address? token = IERC20(0x000000000000);
    – pbsh
    Commented Jan 27, 2022 at 8:08
  • Yes of course, I redacted it when I posted here.
    – FBK
    Commented Jan 27, 2022 at 8:23
  • I guess my question is, is the contract Approvetokens function written correctly? I am fairly new to solidity.
    – FBK
    Commented Jan 27, 2022 at 8:24

1 Answer 1

1

The issue here is that the code you had mentioned is trying to approve from the contract's address.

token.approve(address(this), _tokenamount);

Approve function in ERC approves Approvetokens functions, it'll call the approve function in the ERC20 contract. For that call, msg.sender will be your contarct, not you. Since the contract doesn't have sufficient balance, it'll fail with Insufficient balance.

To approve your contract to spend your tokens, you'll need to call the token's approve function from your address. Hope this clarifies your question.

2
  • Oh, I thought that address(this) will take the address of the submitter. That makes sense. I will re-try and come back if any questions. Thank you :)
    – FBK
    Commented Jan 27, 2022 at 8:36
  • I have used this tutorial and tried with both address(this) and msg.sender and the balance does not update, it still shows 0. And of course afterwards the rest of the contract fails because i have 0 tokens... ethereum.stackexchange.com/questions/46318/…
    – FBK
    Commented Jan 27, 2022 at 13:35

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.