diff --git a/packages/ERTP/src/paymentLedger.js b/packages/ERTP/src/paymentLedger.js index f55cd737499..a12d7a00b9b 100644 --- a/packages/ERTP/src/paymentLedger.js +++ b/packages/ERTP/src/paymentLedger.js @@ -291,7 +291,12 @@ export const makePaymentLedger = ( */ const withdraw = (currentBalance, updatePurseBalance, amount) => { amount = coerce(amount); + 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 { // COMMIT POINT diff --git a/packages/ERTP/test/unitTests/test-issuerObj.js b/packages/ERTP/test/unitTests/test-issuerObj.js index aa8ea53dfc1..ea89fee532a 100644 --- a/packages/ERTP/test/unitTests/test-issuerObj.js +++ b/packages/ERTP/test/unitTests/test-issuerObj.js @@ -129,6 +129,22 @@ test('issuer.makeEmptyPurse', async t => { .then(checkWithdrawal); }); +test('purse.withdraw overdrawn', async t => { + t.plan(1); + const { issuer, mint, brand } = makeIssuerKit('fungible'); + const purse = issuer.makeEmptyPurse(); + const purseBalance = AmountMath.make(brand, 103980n); + const payment = mint.mintPayment(purseBalance); + purse.deposit(payment); + + const tooMuch = AmountMath.make(brand, 103981n); + + t.throws(() => purse.withdraw(tooMuch), { + message: + 'Withdrawal of {"brand":"[Alleged: fungible brand]","value":"[103981n]"} failed because the purse only contained {"brand":"[Alleged: fungible brand]","value":"[103980n]"}', + }); +}); + test('purse.deposit', async t => { t.plan(7); const { issuer, mint, brand } = makeIssuerKit('fungible');