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?
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:24MyContract
doesn't need to be declaredpayable
, since you're not passing any value to it when you callnew MyContract()
. – goodvibration Sep 1 '20 at 20:26false
totrue
causes your fallback/receive functions to exceed the gas stipend (2300 gas) allowed for them. Try to initialize those variables totrue
, and then set each one of them tofalse
in the corresponding function. – goodvibration Sep 1 '20 at 21:45