Rebasely Beta: data may be purged

justintypescript-ultra-strict Public

Markdown · 1.2 KiB · at 355e324

Write transfer, which moves money from one account to another and returns the updated accounts.

Input:

  • accounts: a plain object mapping account id to { balanceCents, frozen }. Only own keys of accounts are accounts; inherited properties of plain objects are not.
  • from, to: account ids.
  • amountCents: the amount to move, in integer cents.

On success, return a new accounts object with the same keys, where from's balance went down by amountCents and to's went up by the same amount. A balance may reach exactly zero. The input object and the account objects inside it must never be modified (accounts not involved in the transfer may be shared with the input).

Failures, checked in exactly this order (report the first that applies):

  1. { kind: "invalid_amount" } when amountCents is not an integer or is not greater than zero.
  2. { kind: "same_account" } when from and to are the same id.
  3. { kind: "unknown_account", id } when from is not an account, then the same check for to.
  4. { kind: "frozen", id } when from is frozen, then the same check for to.
  5. { kind: "insufficient_funds", id, balanceCents, amountCents } when from's balance is less than amountCents (id is from, balanceCents its current balance).