-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add some tests for the swap helper
move setupZCFTest to a common location and share it. use makeOffer helper suggested by Kate
- Loading branch information
1 parent
12f5911
commit 2a8b0fc
Showing
4 changed files
with
124 additions
and
188 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { E } from '@agoric/eventual-send'; | ||
import bundleSource from '@agoric/bundle-source'; | ||
|
||
// noinspection ES6PreferShortImport | ||
import { makeZoe } from '../../../src/zoeService/zoe'; | ||
import { makeFakeVatAdmin } from '../contracts/fakeVatAdmin'; | ||
|
||
const contractRoot = `${__dirname}/zcfTesterContract`; | ||
|
||
export const setupZCFTest = async (issuerKeywordRecord, terms) => { | ||
/** @type ContractFacet */ | ||
let zcf; | ||
const setZCF = jig => { | ||
zcf = jig.zcf; | ||
}; | ||
// The contract provides the `zcf` via `setTestJig` upon `start`. | ||
const zoe = makeZoe(makeFakeVatAdmin(setZCF)); | ||
const bundle = await bundleSource(contractRoot); | ||
const installation = await E(zoe).install(bundle); | ||
const { creatorFacet, instance } = await E(zoe).startInstance( | ||
installation, | ||
issuerKeywordRecord, | ||
terms, | ||
); | ||
return { zoe, zcf, instance, installation, creatorFacet }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
import '@agoric/install-ses'; | ||
import { E } from '@agoric/eventual-send'; | ||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
import test from 'ava'; | ||
|
||
import { setup } from '../setupBasicMints'; | ||
import { swap } from '../../../src/contractSupport'; | ||
import { assertPayoutAmount } from '../../zoeTestHelpers'; | ||
import { setupZCFTest } from './setupZcfTest'; | ||
|
||
const makeOffer = async (zoe, zcf, proposal, payments) => { | ||
let zcfSeat; | ||
const getSeat = seat => { | ||
zcfSeat = seat; | ||
}; | ||
const invitation = await zcf.makeInvitation(getSeat, 'seat'); | ||
const userSeat = await E(zoe).offer(invitation, proposal, payments); | ||
return { zcfSeat, userSeat }; | ||
}; | ||
|
||
test(`zoeHelper with zcf - swap`, async t => { | ||
const { | ||
moolaIssuer, | ||
moola, | ||
moolaMint, | ||
simoleanIssuer, | ||
simoleanMint, | ||
simoleans, | ||
} = setup(); | ||
const issuerKeywordRecord = { A: moolaIssuer, B: simoleanIssuer }; | ||
const { zoe, zcf } = await setupZCFTest(issuerKeywordRecord); | ||
|
||
const { zcfSeat: aZcfSeat, userSeat: aUserSeat } = await makeOffer( | ||
zoe, | ||
zcf, | ||
harden({ want: { A: moola(3) }, give: { B: simoleans(7) } }), | ||
{ B: simoleanMint.mintPayment(simoleans(7)) }, | ||
); | ||
const { zcfSeat: bZcfSeat, userSeat: bUserSeat } = await makeOffer( | ||
zoe, | ||
zcf, | ||
harden({ want: { B: simoleans(3) }, give: { A: moola(5) } }), | ||
{ A: moolaMint.mintPayment(moola(5)) }, | ||
); | ||
const message = await swap(zcf, aZcfSeat, bZcfSeat); | ||
t.is( | ||
message, | ||
'The offer has been accepted. Once the contract has been completed, please check your payout', | ||
); | ||
assertPayoutAmount(t, moolaIssuer, await aUserSeat.getPayout('A'), moola(3)); | ||
const seat1PayoutB = await aUserSeat.getPayout('B'); | ||
assertPayoutAmount(t, simoleanIssuer, seat1PayoutB, simoleans(4)); | ||
const seat2PayoutB = await bUserSeat.getPayout('B'); | ||
assertPayoutAmount(t, simoleanIssuer, seat2PayoutB, simoleans(3)); | ||
assertPayoutAmount(t, moolaIssuer, await bUserSeat.getPayout('A'), moola(2)); | ||
}); | ||
|
||
test(`zoeHelper with zcf - swap no match`, async t => { | ||
const { | ||
moolaIssuer, | ||
moola, | ||
moolaMint, | ||
simoleanIssuer, | ||
simoleanMint, | ||
simoleans, | ||
} = setup(); | ||
const issuerKeywordRecord = { A: moolaIssuer, B: simoleanIssuer }; | ||
const { zoe, zcf } = await setupZCFTest(issuerKeywordRecord); | ||
|
||
const { zcfSeat: aZcfSeat, userSeat: aUserSeat } = await makeOffer( | ||
zoe, | ||
zcf, | ||
harden({ want: { A: moola(20) }, give: { B: simoleans(3) } }), | ||
{ B: simoleanMint.mintPayment(simoleans(3)) }, | ||
); | ||
const { zcfSeat: bZcfSeat, userSeat: bUserSeat } = await makeOffer( | ||
zoe, | ||
zcf, | ||
harden({ want: { B: simoleans(43) }, give: { A: moola(5) } }), | ||
{ A: moolaMint.mintPayment(moola(5)) }, | ||
); | ||
t.throws( | ||
() => swap(zcf, aZcfSeat, bZcfSeat), | ||
{ | ||
message: | ||
'The trade between left [object Object] and right [object Object] failed. Please check the log for more information', | ||
}, | ||
'mismatched offers', | ||
); | ||
assertPayoutAmount(t, moolaIssuer, await aUserSeat.getPayout('A'), moola(0)); | ||
const seat1PayoutB = await aUserSeat.getPayout('B'); | ||
assertPayoutAmount(t, simoleanIssuer, seat1PayoutB, simoleans(3)); | ||
const seat2PayoutB = await bUserSeat.getPayout('B'); | ||
assertPayoutAmount(t, simoleanIssuer, seat2PayoutB, simoleans(0)); | ||
assertPayoutAmount(t, moolaIssuer, await bUserSeat.getPayout('A'), moola(5)); | ||
}); |