Skip to content

Commit

Permalink
vstorage market tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Pandelis Symeonidis committed Nov 8, 2023
1 parent 8d17e4d commit f7a4fc8
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 11 deletions.
57 changes: 55 additions & 2 deletions agoric/contract/test/swingsetTests/bootstrap/bootstrap-market.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { AmountMath } from '@agoric/ertp';
import { E } from '@endo/eventual-send';
import { flow } from '../flow.js';
import { makeCopyBag } from '@agoric/store';
import { makeCopyBag, mustMatch } from '@agoric/store';
import { addCharacterToContext, addItemToContext } from './utils.js';
import { makeKreadUser } from './make-bootstrap-users.js';
import { errors } from '../../../src/kreadV2/errors.js';
import { multiplyBy } from '@agoric/zoe/src/contractSupport/ratio.js';
import { defaultItems } from '../items.js';
import { TimerBrandShape } from '@agoric/time';

export async function setupMarketTests(context) {
await addCharacterToContext(context);
Expand Down Expand Up @@ -58,6 +59,10 @@ export async function sellCharacter(context) {
zoe,
users: { bob },
paymentAsset,
getFromVStorage,
royaltyRate,
platformFeeRate,
storageKit,
} = context;
const {
market: {
Expand Down Expand Up @@ -109,12 +114,27 @@ export async function sellCharacter(context) {
1,
'Character was not added to market',
);

assert.equal(
(await bob.getCharacters()).length,
0,
"Character is still in bob's wallet",
);
const vStorageCharacterMarket = getFromVStorage(
`kread.market-characters.character-${characterToSell.name}`,
);
//
mustMatch(vStorageCharacterMarket.asset, harden({...characterToSell, date: {...characterToSell.date, timerBrand: TimerBrandShape}}));
assert.equal(vStorageCharacterMarket.id, characterToSell.name);
assert.equal(vStorageCharacterMarket.askingPrice.value, priceAmount.value);
assert.equal(
vStorageCharacterMarket.royalty.value,
multiplyBy(priceAmount, royaltyRate).value,
);
assert.equal(
vStorageCharacterMarket.platformFee.value,
multiplyBy(priceAmount, platformFeeRate).value,
);
assert.equal(vStorageCharacterMarket.isFirstSale, false);
}

export async function buyCharacterOfferLessThanAskingPrice(context) {
Expand Down Expand Up @@ -188,6 +208,7 @@ export async function buyCharacter(context) {
platformFeePurse,
royaltyRate,
platformFeeRate,
getFromVStorage
} = context;

const {
Expand Down Expand Up @@ -265,6 +286,13 @@ export async function buyCharacter(context) {
platformFeePursePre +
multiplyBy(characterToBuy.askingPrice, platformFeeRate).value,
);
try {
getFromVStorage(
`kread.market-characters.character-${characterToBuy.asset.name}`,
);
} catch (error) {
assert.equal(error.message, `no data for "kread.market-characters.character-${characterToBuy.asset.name}"`)
}
}

export async function buyCharacterNotOnMarket(context) {
Expand Down Expand Up @@ -328,6 +356,9 @@ export async function sellItem(context) {
zoe,
users: { bob },
paymentAsset,
getFromVStorage,
royaltyRate,
platformFeeRate
} = context;

const itemToSellValue = (await bob.getItems()).find(
Expand Down Expand Up @@ -361,6 +392,19 @@ export async function sellItem(context) {
0,
"Item is still in bob's wallet",
);
const vStorageItemMarket = getFromVStorage(`kread.market-items.item-0`) // this is the first item on sale so we know it will be assigned id 0
mustMatch(vStorageItemMarket.asset, itemToSellValue);
assert.equal(vStorageItemMarket.id, 0);
assert.equal(vStorageItemMarket.askingPrice.value, priceAmount.value);
assert.equal(
vStorageItemMarket.royalty.value,
multiplyBy(priceAmount, royaltyRate).value,
);
assert.equal(
vStorageItemMarket.platformFee.value,
multiplyBy(priceAmount, platformFeeRate).value,
);
assert.equal(vStorageItemMarket.isFirstSale, false);
}

export async function buyItemOfferLessThanAskingPrice(context) {
Expand Down Expand Up @@ -421,6 +465,7 @@ export async function buyItem(context) {
contractAssets,
zoe,
users: { bob, alice },
getFromVStorage
} = context;

let itemsForSale = await E(publicFacet).getItemsForSale();
Expand Down Expand Up @@ -473,6 +518,14 @@ export async function buyItem(context) {

itemsForSale = await E(publicFacet).getItemsForSale();
assert.equal(itemsForSale.length, 0, 'Item was not removed from market');

try {
getFromVStorage(
`kread.market-items.item-0`,
);
} catch (error) {
assert.equal(error.message, `no data for "kread.market-items.item-0"`)
}
}

export async function buyItemNotOnMarket(context) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { Far, deeplyFulfilled } from '@endo/marshal';
import { Far, deeplyFulfilled, makeMarshal } from '@endo/marshal';
import { E } from '@endo/eventual-send';
import { makeFakeStorageKit } from '@agoric/internal/src/storage-test-utils';
import { buildManualTimer } from '@agoric/swingset-vat/tools/manual-timer';
import { makeFakeBoard } from '@agoric/vats/tools/board-utils';
import {
makeFakeBoard,
slotToBoardRemote,
} from '@agoric/vats/tools/board-utils';
import { makeTracer } from '@agoric/internal';
import { Fail, NonNullish } from '@agoric/assert';
import { makeIssuerKit } from '@agoric/ertp';
Expand Down Expand Up @@ -165,11 +168,20 @@ export const buildRootObject = async () => {
/**
* Reads the data from the vstorage at the given path
* Will throw an error if path doesnt exist
* @param {string} path
* @param {string} path
*/
const getFromVStorage = (path) => {
return unmarshalFromVstorage(storageKit.data, path, marshaller.fromCapData.bind(marshaller))
}
const { fromCapData } = makeMarshal(
undefined,
(slot, iface) => {
return Far((iface ?? '').replace(/^Alleged: /, ''), {});
},
{
serializeBodyFormat: 'smallcaps',
},
);
return unmarshalFromVstorage(storageKit.data, path, fromCapData);
};

const committeeName = 'KREAd Committee';

Expand Down Expand Up @@ -283,10 +295,7 @@ export const buildRootObject = async () => {
governorFacets.creatorFacet,
).getCreatorFacet();

await E(creatorFacet).initializeBaseAssets(
baseCharacters,
baseItems,
);
await E(creatorFacet).initializeBaseAssets(baseCharacters, baseItems);
await E(creatorFacet).initializeCharacterNamesEntries();
await E(creatorFacet).initializeMetrics();

Expand All @@ -297,6 +306,7 @@ export const buildRootObject = async () => {
} = terms;

context = {
storageKit,
getFromVStorage,
contractAssets: {
character: { issuer: characterIssuer, brand: characterBrand },
Expand Down

0 comments on commit f7a4fc8

Please sign in to comment.