0

The following code is supposed to send 1 wei from the current contract to a newly created contract. The recipient contract gets created successfully and the sending contract has a positive balance, but the call to send() fails.

address payable this_addr = address (this);
require (this_addr.balance > 100000);

MyContract mc_new = new MyContract ();
address payable mc_new_addr = address (mc_new);
bool success = mc_new_addr.send (1);

Here is the relevant code from the contract being created. I can see that the contract never has a balance and neither recieve() nor fallback() is ever called.

contract MyContract
{
    bool receive_called;
    bool fallback_called;

    constructor () payable
    {
        receive_called = false;
        fallback_called = false;
    }

    receive () external payable
    {
        receive_called = true;
    }

    fallback () external payable
    {
        fallback_called = true;
    }
}

The transfer() function doesn't work either. What am I doing wrong?

Also, what triggers calls to receive() and fallback()? Can it only be done by using one of the assembly functions? Or is the a way to trigger these function calls in ordinary solidity?

  • You should first make sure you have that one wei in your contract (the one which implements the function which does MyContract mc_new = new MyContract (); address payable mc_new_addr = address (mc_new); bool success = mc_new_addr.send (1);). – goodvibration Sep 1 '20 at 20:24
  • BTW, as it stands, the constructor of MyContract doesn't need to be declared payable, since you're not passing any value to it when you call new MyContract(). – goodvibration Sep 1 '20 at 20:26
  • The contract does have at least 1 wei. It has several ether. – Zephyrus Sep 1 '20 at 20:34
  • Please prove it (add relevant on-chain and off-chain code). – goodvibration Sep 1 '20 at 20:35
  • 1
    Maybe setting those state variables from false to true causes your fallback/receive functions to exceed the gas stipend (2300 gas) allowed for them. Try to initialize those variables to true, and then set each one of them to false in the corresponding function. – goodvibration Sep 1 '20 at 21:45

Your Answer

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

Browse other questions tagged or ask your own question.