0

So I am trying to implement a simple deposit contract deployed to the testnet of Binance smart chain. The code looks simple as following:

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

interface IBEP20 {
    function totalSupply() external view returns (uint256);
    function decimals() external view returns (uint8);
    function symbol() external view returns (string memory);
    function name() external view returns (string memory);
    function getOwner() external view returns (address);
    function balanceOf(address account) external view returns (uint256);
    function transfer(address recipient, uint256 amount) external returns (bool);
    function allowance(address _owner, address spender) external view returns (uint256);
    function approve(address spender, uint256 amount) external returns (bool); // This is a function we gonna use
    function transferFrom(address sender, address recipient, uint256 amount) external returns (bool); // This is a function we gonna use
    event Transfer(address indexed from, address indexed to, uint256 value);
    event Approval(address indexed owner, address indexed spender, uint256 value);
}

contract SimpleDeposit {
    function simpledeposit(address _tokenIn, uint256 _amountsIn) external {
        IBEP20(_tokenIn).approve(address(this), _amountsIn);
        IBEP20(_tokenIn).transferFrom(msg.sender, address(this), _amountsIn);
    }
}

I have some test BUSD in metamask wallet, so here msg.sender really means my wallet address. After deploying this contract, when I try to execute this function, the remix shows me the following errors:

Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending?
Internal JSON-RPC error. { "code": 3, "message": "execution reverted: BEP20: transfer amount exceeds allowance", "data": "0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002842455032303a207472616e7366657220616d6f756e74206578636565647320616c6c6f77616e6365000000000000000000000000000000000000000000000000" }

My question is how to properly fund a contract from my metamask wallet in this case? And also that the tranfer amount definitely not exceed the amounts of test BUSD in my test wallet.

3
  • approve does not work here, because the caller of the approve function is the contract and not the msg.sender
    – Majd TL
    Jul 31, 2021 at 12:33
  • Hello Majd, thx for your reply. Are you suggesting to change the arguments in approve() to msg.sender? I have tried this and not working.
    – CoolGas
    Jul 31, 2021 at 14:19
  • 1
    No. You are calling „approve“ from inside the contract = the contract (not you) is trying to approve his token to itself(make no sense) and then the contract try to transfer token from you to itself , but it has no approvement from u to do that
    – Majd TL
    Jul 31, 2021 at 18:06

1 Answer 1

0

You need to call the approve function directly from your wallet. You cant have your contract do that. So you'd need 2 transactions, one to the token contract that would be something like approve(yourContractAddress, amount) and one to your SimpleDeposit contract calling the simpleDeposit function with the parameters you want. The way you tried to do it by coding it wont work because the ERC20/BEP20 standard approve function simply doesnt work that way

8
  • Hi @Foxxxey, thx for your answer! To be 100% clear, are you suggesting that I should add one more function to the contract playing the role of approve? Or you are suggesting something else off chain?
    – CoolGas
    Jul 31, 2021 at 16:25
  • nop, sorry if i was unclear but you need to call the approve function of the token you want to deposit directly from your wallet (and not from your contract, it wont work if you try to do that, since approve relies on msg.sender) before interacting with your contract.
    – Foxxxey
    Jul 31, 2021 at 16:29
  • notice how whenever you want to fund a contract (such as a pancakeswap pool for example) for the first time, you have to make 2 separate transactions, thats why (the 1st transaction is a call to the token contract, approving the contract you're trying to interact with to spend your tokens on your behalf, and the 2nd transaction is a call to the contract you want to interact with, actually funding the contract).
    – Foxxxey
    Jul 31, 2021 at 16:34
  • Ahhh, I see. So what I need to do is first delete the line about approve in my code. Then I need to send a transection from my wallet to the contract of whatever token I want to deposit. Just one more question, how exactly from my metamask wallet send an approval transection?
    – CoolGas
    Jul 31, 2021 at 16:38
  • Thx, I figured out. You answer rocks!
    – CoolGas
    Jul 31, 2021 at 16:48

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.