2

I am looking for a way for people to be able to send ethers to two contracts (two different address). But I need the balance between these two contracts to be shared. So that one contract can use the other's fund and vice-versa. Is that possible to implement?

Alternatively, would it be possible for the same contract to have two different address that users can send money to that will perform different tasks?

1

Both alternatives are possible with the use of additional contracts.

Option 1

A transaction can only have one destination, to, account. So the idea is that there would be an additional contract at this to address, and it sends half the ethers to your two contracts (c1 and c2). Both contracts then need functions that allows the other access to its ethers.

pragma solidity ^0.4.0;
contract D {
  function deposit(address c1, address c2) payable {
     uint amount1 = msg.value / 2;
     uint amount2 = msg.value - amount1;
     // if (amount1 != amount2) throw;  uncomment this if you want this guarantee
     if (!c1.send(amount1)) throw;
     if (!c2.send(amount2)) throw;
  }
}

Option 2

Contracts only have 1 address. What can be done is to have 2 contracts, at address a1 and a2, and to have these contracts forward the ethers they receive to your main contract M (and perform their different tasks).

pragma solidity ^0.4.0;
contract A1 {
  address addressOfM;
  function A1(address _m) {
      addressOfM = _m;
  }
  function forward() payable {
     if (!addressOfM.send(msg.value)) throw;
     // code for task1
  }
}

contract A2 {
  address addressOfM;
  function A2(address _m) {
      addressOfM = _m;
  }
  function forward() payable {
     if (!addressOfM.send(msg.value)) throw;
     // code for task2
  }
}

contract M {
    function() payable {}
}

Note that forward functions are used in contracts A1 and A2, instead of fallback functions, because they are doing tasks, and fallback functions are not recommended for doing tasks, primarily because other contracts that invoke a1.send will run out of gas: How much computation can be done in a fallback function? Part of the reason why a1.forward is recommended.

  • Is it possible to use the fallback function in contacts A1 and A2 to forward the function to the main contract where the tasks will be done? How can I send ethers to the main contract and at the same time call a function in the main contract from either contract A1 or A2? – Pabi Oct 29 '16 at 20:48
  • Thanks for asking ethereum.stackexchange.com/questions/9705/… – eth Oct 31 '16 at 0:55

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.