Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix!: improve error message when a purse is overdrawn #3811

Merged
merged 2 commits into from
Sep 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/ERTP/src/paymentLedger.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
katelynsills marked this conversation as resolved.
Show resolved Hide resolved
);
const newPurseBalance = subtract(currentBalance, amount);

const payment = makePayment(allegedName, brand);
try {
// COMMIT POINT
Expand Down
16 changes: 16 additions & 0 deletions packages/ERTP/test/unitTests/test-issuerObj.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]"}',
katelynsills marked this conversation as resolved.
Show resolved Hide resolved
});
});

test('purse.deposit', async t => {
t.plan(7);
const { issuer, mint, brand } = makeIssuerKit('fungible');
Expand Down