1
We have an instance of bitcoin core running a watch only wallet. This wallet receives a lot of transactions that we need to (once a day) sweep and split into other personal wallets.
The distribution is a full sweep as follows:
- Each vendor receives 95%, after fee deduction
- The system collects 5%, after fee deduction
Currently we do it manually like so:
- Create a new wallet in electrum with addresses that have balance
- Manually calculate how much each partner is going to receive
- Manually split the bitcoins for each partner
- Check how much is the estimated fee for that transaction
- Recalculate how much each partner is going to receive taking the fee out
- Manually sign the transaction
- Broadcast it
Example:
Inputs:
Vendor A: 2 BTC in 5 inputs
Vendor B: 1 BTC in 2 inputs
Vendor C: 7 BTC in 20 inputs
Total input transactions: 10 BTC
Assuming fee: 0.0002
We send to each party's personal wallet:
Vendor A: 1.89996200
Vendor B: 0.94998100
Vendor C: 6.64986700
System: 0.49999000
Total sent: 10 BTC
I'm trying to automate the calculation of the distribution with the following script: (please assume Bitcoin.rpc is the same as bitcoin-cli)
balance = Bitcoin.rpc.getbalance '*', 1, true
estimatedFee = ???
vendorABalance = vendorAIncome * (balance - estimatedFee)/balance * 95%
vendorBBalance = vendorBIncome * (balance - estimatedFee)/balance * 95%
vendorCBalance = vendorCIncome * (balance - estimatedFee)/balance * 95%
systemBalance = balance - estimatedFee - vendorABalance - vendorBBalance - vendorCBalance
rtx = Bitcoin.rpc.createrawtransaction [], [
{ 'Vendor A wallet' => vendorABalance },
{ 'Vendor B wallet' => vendorBBalance },
{ 'Vendor C wallet' => vendorCBalance },
{ 'System wallet' => systemBalance },
]
Bitcoin.rpc.fundrawtransaction rtx, { includeWatching: true, conf_target: 6 }
# On a offline machine
rtx = bitcoin-cli signrawtransactionwithkey rtx ['private keys']
# Back on the online machine
Bitcoin.rpc.sendrawtransaction rtx
In theory this script does exactly what I need. If the estimatedFee variable gets the correct value
But I can't seem to find the sweet spot that will allow for the sweep of the wallet, I get Insufficient funds or there will be some leftovers.
What can I do?
Before I use this solution I have to ask.
equally from all specified outputs, In the example I gave, everyone will share the same amount to pay the fee, vendorB (1 btc) and vendorC (7 btc) will contribute the same way to the tx fee. Is there no way to have them contribute by "shares" or participation? – Nicos Karalis – 2019-05-08T10:57:34.597Great question, I do not believe it makes the subtracted fee proportionate to the value of the output. See: https://github.com/bitcoin/bitcoin/blob/24dfcf3a56f90b101bc208f48ccdb7813fa08b83/src/wallet/wallet.cpp#L2761.
– chytrik – 2019-05-08T11:25:44.200Yeah, subtractFeeFromOutputs just splits the fee equally. It is important to us because on a very large transaction (100 inputs) a vendor with very small balance (< $10) will be hit hard (even with segwit) – Nicos Karalis – 2019-05-08T12:42:09.763