3

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!

1

I'm not sure why that isn't working, but as a workaround, you can just make SmartGift's constructor payable and do this:

address newGift = (new SmartGift).value(address(this).balance)(_recipient,
    msg.sender, _expiry, _donorMsg);

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.