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:

  1. Each vendor receives 95%, after fee deduction
  2. The system collects 5%, after fee deduction

Currently we do it manually like so:

  1. Create a new wallet in electrum with addresses that have balance
  2. Manually calculate how much each partner is going to receive
  3. Manually split the bitcoins for each partner
  4. Check how much is the estimated fee for that transaction
  5. Recalculate how much each partner is going to receive taking the fee out
  6. Manually sign the transaction
  7. 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?

1

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' => vendorABalance },
  { 'Vendor B wallet' => vendorBBalance },
  { 'Vendor C wallet' => vendorCBalance },
  { 'System wallet' => 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,...]
  • 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 May 8 at 10:57
  • Great question, I do not believe it makes the subtracted fee proportionate to the value of the output. See: github.com/bitcoin/bitcoin/blob/…. – chytrik May 8 at 11:25
  • Yeah, 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 May 8 at 12:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.