Reputation: 1
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!
Upvotes: 0
Views: 195
Reputation: 350760
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
Here is a little runnable code, where you can input the parameters and see the result:
const roundCents = x => Math.round(x * 100) / 100;
// Apply formula, but rounded to the cent
function solve (n, r, c, a) {
const g = roundCents((a + c) / (n - r));
const p1 = roundCents(g * (1 - r) - c);
const p2 = roundCents(a - g * (n - 1));
return [g, p1, p2];
}
// I/O management
document.addEventListener("input", refresh);
const inputs = document.querySelectorAll("input");
const outputs = document.querySelectorAll("span");
function refresh() {
const [n, pct, c, a] = Array.from(inputs, input => +input.value);
const results = solve(n, pct/100, c, a);
outputs.forEach((output, i) => output.textContent = results[i].toFixed(2));
}
refresh();
input { width: 5em}
Number of parties: <input type="number" value="5" min="2"><br>
Fee percentage: <input type="number" value="1.9" min="0" max="100" step="0.1"><br>
Fee constant: <input type="number" value="0.10" min="0" step="0.01"><br>
Initial amount: <input type="number" value="1000" min="0" step="0.01"><br>
<hr>
Gross payments: $<span></span><br>
Net received: $<span></span><br>
Net remaining: $<span></span><br>
Upvotes: 1