# Is there a way for contracts to share balances?

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 Answer

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
{"ft_lang_label":"__label__en","ft_lang_prob":0.876609,"math_prob":0.6920626,"size":1642,"snap":"2021-21-2021-25","text_gpt3_token_len":407,"char_repetition_ratio":0.16483517,"word_repetition_ratio":0.08921933,"special_character_ratio":0.2630938,"punctuation_ratio":0.1629393,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9668328,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-06-20T07:10:09Z\",\"WARC-Record-ID\":\"<urn:uuid:29ebfbe9-469e-468f-8f69-1edfc088cb20>\",\"Content-Length\":\"169438\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:5edfd0f0-1219-43d0-bf69-9994fff75fa5>\",\"WARC-Concurrent-To\":\"<urn:uuid:23656711-afe8-40e9-a860-e63928364bbe>\",\"WARC-IP-Address\":\"151.101.193.69\",\"WARC-Target-URI\":\"https://ethereum.stackexchange.com/questions/9697/is-there-a-way-for-contracts-to-share-balances/9699\",\"WARC-Payload-Digest\":\"sha1:2KQYHLPEKVEE7RXW3CARPLAF3PSNTDWF\",\"WARC-Block-Digest\":\"sha1:4NQPDAFQKGO7M7JZ7VBMDKD2AZ7BMM6E\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-25/CC-MAIN-2021-25_segments_1623487658814.62_warc_CC-MAIN-20210620054240-20210620084240-00353.warc.gz\"}"}