[Ethereum] Contract: Get address as input and send address to another contract

addressescontract-designcontract-invocationsolidity

Hey guys I'm curious about an idea i was having earlier. I am a total beginner so bare with me 😉
I want to create a contract that basically just sends ether to another contract. BUT the user should be able to input another address. This address and the ether will then be sent to another contract.

So in example User B sends a specific amount of Ether to Contract1. He also inputs another address he owns. Then, Contract1 sends the whole amount of Ether to Contract2, exporting the input address as well. I only want to get this far for now.

Is this even possible? And if so, can somebody write some example code? I can't find an address input option in the solidity documentation.

I'm not familiar with solidity so I'm just 'learning by doing' as they say.. 🙂

Thanks for your help!

Best Answer

How about something like that:

pragma solidity ^0.4.2;

contract Receiver {
    function receive(address benefactor) payable
        returns (bool) {
        // Receives msg.value from msg.sender, in your case ForwardExample
        // But understands benefactor is the one who "paid".
        return true;
    }
}

contract ForwardExample {
    // Keep the target contract in, instead of passing it as a parameter
    // To forward function.
    Receiver receiver;

    function ForwardExample(address receiverAddr) {
        receiver = Receiver(receiverAddr);
    }

    function forward(address myOtherAddress) payable
        returns (bool) {
        // Pass on the whole ether received.
        bool successful = receiver.receive.value(msg.value)(myOtherAddress);
        if (!successful) throw;
        return true;
    }
}
Related Topic