I’m trying to figure out a code for determining how to split payments equally when there is a transaction fee involved in paying the parties.
Assuming there are 5 parties, and 1 of them receives $1000 that needs to be split out equally between the 5. How much should party 1 send the remaining 4 people accounting for a hypothetical 1.9% + $0.1 fee, such that each of the 5 people had the same balance at the end. In this scenario, party 1 can only make N-1 transactions, i.e, 4 transactions.
Any help would be greatly appreciated!
Let's define some names:
We are asked to derive for given , , and with the following constraints:
In a transaction, the received amount () is the paid amount () with fees applied:
= (1 − ) −
After making 4 transactions, the payer is left with the same amount as the receivers have ():
= − ( − 1)
This set of equalities can be resolved as follows:
(1 − ) − = − ( − 1)
⇔ (1 − ) + ( − 1) = +
⇔ ( − ) = +
⇔ = ( + ) / ( − )
For the given example, we have this input:
= 5
= 0.019
= 0.10
= 1000
The result is thus:
= ( + ) / ( − ) = (1000 + 0.10) / (5 − 0.019) = 200.78297530616342...
Verification of constraints:
= (1 − ) − = 200.78297530616342 * (1 − 0.019) − 0.10 = 196.8680987753463...
= − ( − 1) = 1000 − 200.78297530616342 * (5 − 1) = 196.8680987753463...
As we deal with dollars, we must round to the cent, and so there will be slight divergence.
The first party will pay 200.78 in 4 transactions and after these, each party will have 196.87, except the first party; they will have one cent more: 196.88
Snippet
Here is a little runnable code, where you can input the parameters and see the result: