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.