0

I want to send some amount of ether to a smart contract from metamask (without using web3) and want it to perform some specific tasks based on that amount. For example, if I send 0.9 ether, I want it to set a variable named planid to 1101 in the fallback function. The following piece of code is from my contract:

 function() external payable {
    
    if (msg.sender == bank_owner) {
        emit AdminDeposit(msg.value, bank_owner);
        //invest(msg.value, msg.sender, plan_id, _referrer);
    } else {
        address _referrer = bytesToAddress(msg.data);
        _init(msg.sender, msg.value, _referrer);
    }
}

function _init(address payable _investor, uint256 _investValue, address _referrer) private {
    bool doInvest = true;
    if (_investValue == PLAN_PRICE[0]) {
        planid = 1101;
    }
    else if (_investValue == PLAN_PRICE[1]) {
        planid = 1102;
    }
    else if (_investValue == PLAN_PRICE[2]) {
        planid = 1103;
    }
    else if (_investValue == PLAN_PRICE[3]) {
        planid = 1104;
    } 
    else{
        doInvest = false;
        emit TestEvent(planid);
    }

    if (doInvest == true){
        invest(_investValue, _investor, planid, _referrer);
    }else
    {
        revert("Incorrect value sent!");
    }
}

In the above piece of code, If someone (other than bank owner which is already defined in the constructor) send 0.9 ether from metamask to the contract (directly), the _init() function will be called. I expect that else part of the second if statement does not executes. However, it executes and transaction fails.

When I replace revert() with something else, everything is ok. I was wondering if you help me with this case. Thanks.

2
  • I think it's something related to the _referrer variable. Can you attach the full contract code so we can test? – Miroslav Nedelchev Oct 17 '20 at 5:35
  • @MiroslavNedelchev Thank you for your comment. As stated by by alireza, the problem was due to the low value of gasLimit. – Nima shiri Oct 17 '20 at 9:51
0

Transaction is failed because the gas limit you allocated in Metamask is less than what required. When we compare, _init() consumes gas more than revert(). So, you need to set gas limit of transcartion as much as necessary in the part indicated in the photo:

enter image description here

Good Luck.

3
  • I really appreciate your assistant. The problem solved. – Nima shiri Oct 17 '20 at 9:52
  • @NimsShiri, You're welcome. So, please mark the answer to if another has this question it can find the correct answer easily. – Alireza Oct 17 '20 at 9:55
  • Please send me a message through email for an industrial collaboration. Sorry for asking here. I couldn't find your contact information. My email: nimashiri2012@gmail.com. – Nima shiri Oct 17 '20 at 10:31

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.