Skip to content

Commit 7b5841c

Browse files
authored
fix!: improve error message when a purse is overdrawn (#3811)
* fix!: improve error message when a purse is overdrawn
1 parent cca39d9 commit 7b5841c

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

packages/ERTP/src/paymentLedger.js

+5
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,12 @@ export const makePaymentLedger = (
291291
*/
292292
const withdraw = (currentBalance, updatePurseBalance, amount) => {
293293
amount = coerce(amount);
294+
assert(
295+
AmountMath.isGTE(currentBalance, amount),
296+
X`Withdrawal of ${amount} failed because the purse only contained ${currentBalance}`,
297+
);
294298
const newPurseBalance = subtract(currentBalance, amount);
299+
295300
const payment = makePayment(allegedName, brand);
296301
try {
297302
// COMMIT POINT

packages/ERTP/test/unitTests/test-issuerObj.js

+16
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,22 @@ test('issuer.makeEmptyPurse', async t => {
129129
.then(checkWithdrawal);
130130
});
131131

132+
test('purse.withdraw overdrawn', async t => {
133+
t.plan(1);
134+
const { issuer, mint, brand } = makeIssuerKit('fungible');
135+
const purse = issuer.makeEmptyPurse();
136+
const purseBalance = AmountMath.make(brand, 103980n);
137+
const payment = mint.mintPayment(purseBalance);
138+
purse.deposit(payment);
139+
140+
const tooMuch = AmountMath.make(brand, 103981n);
141+
142+
t.throws(() => purse.withdraw(tooMuch), {
143+
message:
144+
'Withdrawal of {"brand":"[Alleged: fungible brand]","value":"[103981n]"} failed because the purse only contained {"brand":"[Alleged: fungible brand]","value":"[103980n]"}',
145+
});
146+
});
147+
132148
test('purse.deposit', async t => {
133149
t.plan(7);
134150
const { issuer, mint, brand } = makeIssuerKit('fungible');

0 commit comments

Comments
 (0)