From f77e3a54dab9de2e0790e2fa7e4f761ecd12659d Mon Sep 17 00:00:00 2001 From: Dan Connolly Date: Thu, 9 Feb 2023 14:33:47 -0600 Subject: [PATCH] test: smart wallet recovers when only some withdrawals succeed --- .../test/smartWallet/test-psm-integration.js | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/packages/inter-protocol/test/smartWallet/test-psm-integration.js b/packages/inter-protocol/test/smartWallet/test-psm-integration.js index 7e804a45c3e5..3eed6e14b231 100644 --- a/packages/inter-protocol/test/smartWallet/test-psm-integration.js +++ b/packages/inter-protocol/test/smartWallet/test-psm-integration.js @@ -374,6 +374,57 @@ test.failing('deposit > 1 payment to unknown brand #6961', async t => { } }); +// XXX belongs in smart-wallet package, but needs lots of set-up that's handy here. +test('recover when some withdrawals succeed and others fail', async t => { + const { fromEntries } = Object; + const { make } = AmountMath; + const { anchor } = t.context; + const { agoricNames, bankManager } = t.context.consume; + const getBalance = (addr, brand) => { + const bank = E(bankManager).getBankForAddress(addr); + const purse = E(bank).getPurse(brand); + return E(purse).getCurrentAmount(); + }; + const namedBrands = kws => + Promise.all( + kws.map(kw => + E(agoricNames) + .lookup('brand', kw) + .then(b => [kw, b]), + ), + ).then(fromEntries); + + t.log('Johnny has 10 AUSD'); + const jAddr = 'addrForJohnny'; + const smartWallet = await t.context.simpleProvideWallet(jAddr); + await E(E(smartWallet).getDepositFacet()).receive( + // @ts-expect-error FarRef grumble + E(anchor.mint).mintPayment(make(anchor.brand, 10n)), + ); + t.deepEqual(await getBalance(jAddr, anchor.brand), make(anchor.brand, 10n)); + + t.log('He accidentally offers 10 BLD as well in a trade for IST'); + const instance = await E(agoricNames).lookup('instance', 'psm-IST-AUSD'); + const brand = await namedBrands(['BLD', 'IST']); + const proposal = harden({ + give: { Anchor: make(anchor.brand, 10n), Oops: make(brand.BLD, 10n) }, + want: { Proceeds: make(brand.IST, 1n) }, + }); + await E(smartWallet.getOffersFacet()).executeOffer({ + id: '1', + invitationSpec: { + source: 'contract', + instance, + publicInvitationMaker: 'makeWantMintedInvitation', + invitationArgs: [], + }, + proposal, + }); + + t.log('He still has 10 AUSD'); + t.deepEqual(await getBalance(jAddr, anchor.brand), make(anchor.brand, 10n)); +}); + test.todo('bad offer schema'); test.todo('not enough funds'); test.todo(