Accurately calculate fees to sweep all coins 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' =&gt; vendorABalance },
  { 'Vendor B wallet' =&gt; vendorBBalance },
  { 'Vendor C wallet' =&gt; vendorCBalance },
  { 'System wallet' =&gt; 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?
It seems there is some discrepancy between your estimatedFee variable and the feerate being used in the fundrawtransaction step. 

I think you could try simplifying your code to not use the estimatedFee during the vendorXBalance calculation, and then when calling fundrawtransaction, use the subtractFeeFromOutputs option to just have the fee deducted equally from all specified outputs. For example:

balance = Bitcoin.rpc.getbalance '*', 1, true

vendorABalance = vendorAIncome * 95%
vendorBBalance = vendorBIncome * 95%
vendorCBalance = vendorCIncome * 95%

systemBalance = balance - vendorABalance - vendorBBalance - vendorCBalance

rtx = Bitcoin.rpc.createrawtransaction [], [
  { 'Vendor A wallet' =&gt; vendorABalance },
  { 'Vendor B wallet' =&gt; vendorBBalance },
  { 'Vendor C wallet' =&gt; vendorCBalance },
  { 'System wallet' =&gt; systemBalance },
]
Bitcoin.rpc.fundrawtransaction rtx, { includeWatching: true, conf_target: 6, subtractFeeFromOutputs: [vout_index,...] }

# On a offline machine
rtx = bitcoin-cli signrawtransactionwithkey rtx ['private keys']

# Back on the online machine
Bitcoin.rpc.sendrawtransaction rtx


Here is some relevant text from bitcoin-cli help fundrawtransaction (v0.17.0.1): 

"subtractFeeFromOutputs" (array, optional) A json array of integers.
     The fee will be equally deducted from the amount of each specified output.
     The outputs are specified by their zero-based index, before any change 
     output is added.
     Those recipients will receive less bitcoins than you enter in their 
     corresponding amount field. 
     If no outputs are specified here, the sender pays the fee.
       [vout_index,...]