Sending ether from one contract to another via a function

smalltalksmalltalk Posts: 16
Is it possible to call from one contract to another via a function with arguments and give it ether. I can do it if the function has zero arguments using call on the contract address but how do it do it if i have parameters. The test example below shows how i can call register() with one ether but how do i call the other function register(date) with one ether?

contract test {
function() {
register();
}

function register() {
}

function register(uint date) {
}
}

The following will call the function register()
aContractAddress.call.gas(200000).value(1 ether)(bytes4(sha3("register()")));

or this will call the fallback function
aContractAddress.call.gas(200000).value(1 ether)();

Can i do the same thing but call register with the date parameter? If so how?




Comments

  • smalltalksmalltalk Posts: 16
    I have now figured it out and for completeness posted the solution.

    aContractAddress.register.value(1 ether) (date);

    This will run the register function passing in the parameter date and sending 1 ether


  • desktopdandesktopdan Posts: 15
    hi @smalltalk, I was trying the same thing. But not sure I quite understand what you are doing there. Could you expand?

    Say contract A and B are identical, and both have a

    function receiveSomething (_avariable ) {
    // do something with _avariable
    }

    and a

    function sendSomething (_address, _abiDef) {

    var contract = contract(_abiDdef).at(_address);
    contract.receiveSomething.sendTransaction("hello");
    //this is all made-up syntax, of course!
    }


    How can I call the function receiveSomething in contract B from the function sendSomething in contract A?
    IS that possible? And if so, what is the syntax in sendSomething that allows you to do that?

    Any pointers greatly appreciated!
  • tcimolitcimoli Cagliari - ItalyPosts: 35
    Sorry, why you do not use the send function in address?

    For instance,if the address of contractB is addrB, and if in ContractA you write:

    addrB.send(1 ether);

    this will send 1 ether from ContractA to contractB
  • desktopdandesktopdan Posts: 15
    well, because my understanding was that that was fine to send ether, but what if you want to send something else, i.e. pass in a parameter from contract A to contract B?
    Can you do that with the .send? If so, how?

    Thanks!
  • smalltalksmalltalk Posts: 16
    edited May 2016
    I didn't want to use the send function as i wanted the other contract to validate the payment before registering. That way i could check I sent the ether before registering the contract for the service.

    @desktopdan you can directly call the other contract you don't need the sendTransaction and you don't need to send the abi if you already know it; which you do from your code snippet.

    So for example you could define this in your contract A before the contract.

    // Only works if you know the ABI. Name of the contract does not matter as just used
    // internally but the function name must match the abi of contract B
    contract PayingContractInterface {
    function receiveSomething(string message) {}
    }

    contract A {
    function sendSomething (_address) {
    PayingContractInterface contract = PayingContractInterface(_address);
    contract.receiveSomething("hello");
    }
    }

    This should do the job but if you wanted to send ether as well you can do the following:

    contract.receiveSomething.value(1 ether)("hello");



  • desktopdandesktopdan Posts: 15
    brilliant! many thanks... I will try that and report back...
  • desktopdandesktopdan Posts: 15
    hi @smalltalk - just to say, had a go at this and it worked a treat!
    I have to admit I was struggling to understand it, but once I had a go at it, it made perfect sense: The way I understood it is that you are defining a contract class in contract A and then saying that this class is "located" at a certain _address (the address of contract B ).
    If you then call a method in this class, which happens to have the same name as the method in contract B, then the method in contract B gets called!
    Or something like that? :-)

    Anyway, thanks for that!
  • supere989supere989 Posts: 3
    Hi, Not sure i understand this, but does this mean to say that you can pull the class from an existing contract and import the contract like a resource header?
Sign In or Register to comment.