[Ethereum] How to send from multiple accounts to the one account in the contract

accountsbalancescontract-developmentfeestransactions

For example, I have 3 accounts with balances:

1 account – 5 ether

2 account – 15 ether

3 account – 7 ether

I want to send 20 ether, so it will be used 1 and 2 account.
I understand, how to make through just simple transactions, but for this will need to pay fee for every transaction. And for every transaction, before send, I need to unlock account. Contract can't unlock account, so I don't understand how will ether withdraw from this accounts and how it could have a permission to do this. Please help.

Best Answer

Short: You cannot send from multiple accounts in one transaction.

Long: Every account is controlled by its associated private key. That means all Ether that leave this account need to be signed by that key and then the signed transaction broadcasted to the entire network. Therefore you cannot just have one transaction that sends Ether from three private key controlled accounts. What you could do is have a multi-signature smart contract. This contract would hold all Ether and you could, e.g. have any one of the three accounts control it. This allows you to still send funds if you have lost the key to one account. Note that at the same time it means that all funds are at risk if an attacker gains access to any of those three private keys. Therefore you might be better of setting up a 2-of-3 multisig and need 2 out of all 3 private keys signing a transaction. Read here more on multisig smart contracts for Ethereum.