0

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!

Ave B
  • 1

1 Answers1

1

Let's define some names:

  • : the number of parties
  • : the fee coefficient (a coefficient between 0 and 1)
  • : the fee constant, applied after the coefficient is applied
  • : the initial amount
  • : the gross payment made in each transaction
  • : the net amount that every one will have in the end

We are asked to derive for given , , and with the following constraints:

  1. In a transaction, the received amount () is the paid amount () with fees applied:

    = (1 − ) −

  2. 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:

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>
trincot
  • 317,000
  • 35
  • 244
  • 286