0

I created two contracts which are Balance and TempDeposit. In the Balance contract, I wrote Mint function in order to mint certain number of token stored in the contract. Total token minted will be added into minter(msg.sender)'s balance.

Then I called fundNewContract function in TempDeposit contract to transfer user's balance into TempDeposit contract. But I failed to do so and got "Insufficient Amount" error message. The amount that I wanted to transfer to TempDeposit contract is much lower than user's balance in Balance contract. Anyone knows what is the issue here?

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

contract Balance {
    
    uint public totalSupply;
    mapping(address => uint) public balanceOf;

    function mint(uint amount) external {
        balanceOf[msg.sender] += amount;
        totalSupply += amount;
    }

    function transfer(address _to, uint amount) public {
        require(balanceOf[msg.sender] >= amount, "Insufficient Amount");
        balanceOf[msg.sender] -= amount;
        balanceOf[_to] += amount;
    }

    function viewBalance(address _address) public view returns (uint) {
        return balanceOf[_address];
    }

}

contract TempDeposit {

    Balance balanceContract;

    constructor(address _address) {
        balanceContract = Balance(_address);
    }

    function fundNewContract(uint amount) public {
        balanceContract.transfer(address(this), amount);
    }

    function viewUserBalance(address _address) public view returns(uint) {
        return balanceContract.viewBalance(_address);
    }

}

1 Answer 1

0

You should ask yourself if the minter and the one who is calling TempDeposit contract's fundNewContract function are the same or not?

Clearly your requirement in the transfer function of Balance contract is not meet up (require(balanceOf[msg.sender] >= amount, "Insufficient Amount")) and this error says that msg.sender (the one who is calling TempDeposit contract's fundNewContract function) has insufficient balance for this transfer.

Having said all of the above and after reading this beautiful question on "Who is msg.sender when calling a contract from a contract", you will understand the reason why the contract put that error. It is because of this fact that msg.sender in require(balanceOf[msg.sender] >= amount, "Insufficient Amount") is the TempDeposit contract's address, not the minter who you mapped your minted amount to its balance (balanceOf[msg.sender] += amount).

Possible solution to solve that (in case the minter and the caller of the fundNewContract function are the same): Simply pass msg.sender to the function:

function fundNewContract(uint amount) public {
    balanceContract.transfer(msg.sender, address(this), amount);
}

function transfer(address origin, address _to, uint amount) public {
    require(balanceOf[origin] >= amount, "Insufficient Amount");
    balanceOf[origin] -= amount;
    balanceOf[_to] += amount;
}

Hope this helps.

1
  • Thanks for your comment. It really helps me to understand the issue. By the way, I have another issue that I hope you can advise. I called fundNewContract function to send token to TempDeposit address. The transaction passed but the balance of TempDeposit still 0. Why is that? I literally sent it to address(this). Really appreciate your help here
    – normies
    Commented Mar 8, 2022 at 16:36

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.