hoophq / sequence

Immutable, scalable, and easy to use ledger service.
Apache License 2.0
493 stars 31 forks source link

How would one model an "auth-capture" scenario? #21

Open max-earnin opened 3 years ago

max-earnin commented 3 years ago

I'm exploring sequence and trying to apply it to a problem where an account may "promise" to pay up to X, and then end up paying Y (where typically Y < X). So if I start with 100 in my ledger, and promise 50, my balance is still 100 from one perspective, but I only have 50 left to promise. Then as that promise is resolved as 30 instead of 50, my balance is 70, and I have 70 left to promise.

Would this be best as two separate ledgers? One ledger with metadata distinguishing promises from movements (but balance computation becomes hard)?

Thanks!

sandyscoffable commented 3 years ago

AFAIK this is more of an accounting problem. You'd represent this with multiple accounts in an accrual accounting manner. So perhaps something like:

Starting state Bank Account: 100 Funds Available: 100

Auth Funds Available: -50 "I promise to pay" Account: +50

Capture Bank Account: -30 "Paid" Account: +30 "I promise to pay" Account: -50 Funds Available: +30

The advantage of this is you can see:

  1. How much money the bank account should have in it right now
  2. How much money has been paid out
  3. How much money has been ringfenced for payout
  4. How much money is remaining to be spent (i.e actual balance - future ringfenced amount)
andriosrobert commented 1 year ago

As @sandyscoffable mentioned, I wouldn't use separate ledgers; just create intermediate accounts for any money movement/state you need. This is why it is super important to have a ledger that has a simple API, doing this type of thing should be straightforward in a good ledger implementation.