Skip to content

Commit

Permalink
chore: assert currentBalance greater than or equal to amount to be wi…
Browse files Browse the repository at this point in the history
…thdrawn
  • Loading branch information
katelynsills committed Sep 10, 2021
1 parent 71aaf84 commit c7fa39a
Showing 1 changed file with 5 additions and 15 deletions.
20 changes: 5 additions & 15 deletions packages/ERTP/src/paymentLedger.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,21 +291,11 @@ export const makePaymentLedger = (
*/
const withdraw = (currentBalance, updatePurseBalance, amount) => {
amount = coerce(amount);
let newPurseBalance;
try {
newPurseBalance = subtract(currentBalance, amount);
} catch (err) {
if (err instanceof RangeError) {
const withdrawalError = Error(
// @ts-ignore `X` (aka `details`) lazily constructs a
// string, but the types do not reflect that
X`Withdrawal of ${amount} failed because the purse only contained ${currentBalance}`,
);
assert.note(withdrawalError, X`Caused by: ${err}`);
throw withdrawalError;
}
throw err;
}
assert(
AmountMath.isGTE(currentBalance, amount),
X`Withdrawal of ${amount} failed because the purse only contained ${currentBalance}`,
);
const newPurseBalance = subtract(currentBalance, amount);

const payment = makePayment(allegedName, brand);
try {
Expand Down

0 comments on commit c7fa39a

Please sign in to comment.