21

I want one contract to collect a certain amount of finney before sending it to another contract, but I can't simply send in with C2.send(thisMuch).

function() {
    Dividend m = Dividend(dividendAddr);
    if (this.balance >= 70 finney) {
        uint sendProfit = this.balance;
    }
    m.Enter.send(sendProfit);
}

This is what I have so far, but I'm completely lost when it comes to sending finney from one contract to the next. How can I do that?

I have made two contracts on how I think it should work. Contract one sends it to contract two.

contract one {

    address public deployer;
    address public targetAddress;


    modifier execute {
        if (msg.sender == deployer) {
            _
        }
    }


    function one(address _targetAddress) {
        deployer = msg.sender;
        targetAddress = _targetAddress;
    }


    function forward() {
        two m = two(targetAddress);
        m.pay();
        targetAddress.send(this.balance);
    }


    function() {
        forward();
    }


    function sendBack() execute {
        deployer.send(this.balance);
    }


}

Contract two sends it back to me.

contract two {

    address public deployer;

    function two() {
        deployer = msg.sender;
    }

    function pay() {
        deployer.send(this.balance);
    }

    function() {
        pay();
    }

}

Is this how it should work?

eth
  • 85,679
  • 53
  • 285
  • 406
Sileniced
  • 537
  • 1
  • 5
  • 10

4 Answers4

18

In order to send Ether to another contract while specifying the amount of gas, use the call function.

targetAddress.call.gas(200000).value(this.balance)(); will call the fallback function.

targetAddress.call.gas(200000).value(this.balance)(bytes4(sha3("pay()"))); will call the pay function.

Tjaden Hess
  • 37,046
  • 10
  • 91
  • 118
  • It didn't work unfortunately http://etherscan.io/address/0x845f74aac51148bdd4ef6aed29822940c025a3a2 – Sileniced Apr 14 '16 at 00:34
  • You set the gas to 200,000 but you only gave the transaction 134968. That's what caused the error – Tjaden Hess Apr 14 '16 at 00:43
  • It worked, but I didn't see at first - in mist - that you can change the amount of gas in the same window as where you put your password in. – Sileniced Apr 16 '16 at 14:08
  • Is there a return value handling for this mechanism? As it doesn't know about the type of the return value? For example send() returns bool if the transaction succeeded. – Mikko Ohtamaa Jul 20 '16 at 04:31
  • 3
    No, you can't get a return value from a low-level call. If you know the return type, you should use the myContract.myMethod.value(1 ether).gas(30000)(arg1,arg2). Fallback functions don't return values anyway – Tjaden Hess Jul 20 '16 at 04:33
2

It should be possible to specify the transmitted value and used gas for each external function (and constructor) call:

contract Fa { function fa(uint _a) {} }

contract Fb {
  function fb(address a) {
     Fa b= Fa(a);// or Fa b = New Fa.value(2).gas(200)(a);
    b.fa.value(3).gas(1500)(50);
  }
}

f.gas(x).value(20)() calls the modified function f and thereby sending 20 Wei (or 20 ether by value(20 ether)) and limiting the gas to x (so this function call will most likely go out of gas and return your 20 Wei).

NB : send() should actually use min gas. If we want the code to be executed, we should use call()

Badr Bellaj
  • 18,780
  • 4
  • 58
  • 75
1

Calling external functions is documented here. To the best of my knowledge, there's no way to send to the default function with a user-specified amount of gas; you have to call a named function.

Nick Johnson
  • 8,144
  • 1
  • 28
  • 35
0

Functions that send Ether shall be marked as payable: function name(args) payable { //do something }