WHAT I WANT TO DO: call another contract's constructor function, instantiate that contract, and subsequently send Ether to it in the same function.
WHAT HAPPENS: an instance of the other contract gets created, and Ether is transferred, but none of the functions on the newly created contract work. I get call to SmartGift.{function name} errored: Cannot read property 'length' of undefined
when I try to do anything on the new contract.
PROBLEM FUNCTION:
function createSmartGift(address _recipient, uint32 _expiry, string _donorMsg) public payable returns(address){
address newGift;
newGift = new SmartGift(_recipient, msg.sender, _expiry, _donorMsg);
giftExists[newGift] = true;
uint id = gifts.push(Gift(newGift, _donorMsg,false, _expiry));
giftToOwner[id-1] = _recipient;
recipientGiftCount[_recipient]++;
donorGiftCount[msg.sender]++;
newGift.transfer(address(this).balance); // I WANT TO THIS, BUT GET ERROR
return newGift;
}
OTHER NOTES: I've tried both address(this).balance
and msg.value
. And the target contract has a payable fallback function.
Also, everything works fine if I separate out the newGift.transfer() into a separate function that I have to call separately. That's my backup plan, but I want the customer to only click once, not twice!