0

I am trying to create a pair of contracts, let's say C1 and C2. I want to add an if...else in payable function function() public payable{} of C2, which works on the bool value returned by contract C1, when send(), is called from C1.

How will I be able to do it?

Maybe this code snippet helps somewhere to understand it better

contract target.sol{
    bool private flag=false;    
    function set0(int val) public returns(bool){
        if (val % 10 == 0) {flag0 = true;}
    }


    function transact() public returns(bool){
        if(!flag){
            // heavy computation
        }
        else{
            // light computation
        }
    }
}


contract attacker(){



    function() public payable {
        if(flag == true){
            // Do some heavy computation to generate uncomplete send    
        }

        else {
            // Do some light computation to let send pass
        }
    }

}

Here I want to send a transaction to contract attacker from contract target, with a bool value in the message, so that when the control lands on the payable function of the contract attacker, it checks for the bool value, if true, the computationally heavy portion gets executed, otherwise lighter one gets.

Hope that helps with the description.

Note: I have control over target contract only. I can run no functions from attacker contract.

  • Sorry, I don't understand the question. What do you mean with "works on the bool value"? You can pass any value from a contract to another by calling some function, if you want – Lauri Peltonen May 24 at 4:31
  • @LauriPeltonen, in very short, I want is there anyway, I can use send() to pass some bool value (true/false) to the payable function of C2. So that I can use that bool value to either do a Computationally heavy transaction and eventually fail the transaction or perform a light transaction for a successful transaction. Hope that helps. – Aman Pandey May 24 at 6:28
0

It's hard to tell from your description, but maybe you want something like this? Here C1 sends ether to C2, and C2 then calls back into C1 to read a boolean value.

contract C1 {
    function pay(address whoToPay) external payable {
        require(whoToPay.send(msg.value)); // just because you said "send" instead of "transfer"

        // for gas reasons, you may instead need to use whoToPay.call.value(msg.value)("")
    }

    function getBoolValue() external pure returns (bool) {
        return true;
    }
}

contract C2 {
    function() external payable {
        if (C1(msg.sender).getBoolValue()) { ... }
    }
}

If this isn't what you meant, please improve your question.

  • Hey, @smarx. Hope the added description helps more in understanding the question. – Aman Pandey May 24 at 6:09
  • in very short, what I want is there anyway, I can use send() to pass some bool value (true/false) to the payable function of C2. So that I can use that bool value to either do a Computationally heavy transaction and eventually fail the transaction or perform a light transaction for a successful transaction. – Aman Pandey May 24 at 6:29

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.