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

vstorage shop #8548

Closed
wants to merge 2 commits into from
Closed
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
74 changes: 74 additions & 0 deletions packages/vats/src/vstoreShop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// @ts-check

import { AmountMath } from '@agoric/ertp/src/amountMath.js';
import { E, Far } from '@endo/far';
import { M } from '@endo/patterns';

const { Fail } = assert;

/** @type {ContractMeta} */
export const meta = {
privateArgsShape: harden({ storageNode: M.remotable() }),
};

/**
* @param {ZCF<{
* basePrice: Amount<'nat'>;
* }>} zcf
* @param {{
* storageNode: StorageNode;
* }} privateArgs
*/
export const start = (zcf, privateArgs) => {
const { basePrice } = zcf.getTerms();
const { storageNode } = privateArgs;

const { zcfSeat: fees } = zcf.makeEmptySeatKit();

/**
* @param {ZCFSeat} seat
* @param {{ slug: string }} offerArgs
*/
const buyStorageHook = async (seat, { slug }) => {
assert.typeof(slug, 'string');

const { give } = seat.getProposal();
// IDEA: add entropy to name
// IDEA: add check digits a la board
// IDEA: higher price for lower entropy name
AmountMath.isGTE(give.Payment, basePrice) ||
Fail`Payment too low @@TODO detail`;

zcf.atomicRearrange([[seat, fees, give]]);

// IDEA: wrap storage node; charge on each call
// TODO: constrain slug
const goods = await E(storageNode).makeChildNode(slug);
seat.exit();

return goods;
};

const publicFacet = Far('VStoreShop API', {
makeBuyStorageInvitation: () =>
zcf.makeInvitation(
buyStorageHook,
'buy storage',
// TODO: offer shape
),
});

const collectFeesHook = async seat => {
const alloc = fees.getCurrentAllocation();
zcf.atomicRearrange([[fees, seat, alloc]]);
seat.exit();
return alloc;
};

const creatorFacet = Far('VSorageShop Admin', {
makeCollectFeesInvitation: () =>
zcf.makeInvitation(collectFeesHook, 'collect fees'),
});

return { publicFacet, creatorFacet };
};
79 changes: 79 additions & 0 deletions packages/vats/test/test-vstoreShop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// @ts-check
import { test as anyTest } from '@agoric/zoe/tools/prepare-test-env-ava.js';
import path from 'path';
import { unsafeMakeBundleCache } from '@agoric/swingset-vat/tools/bundleTool.js';
import { setUpZoeForTest } from '@agoric/zoe/tools/setup-zoe.js';
import { E } from '@endo/far';
import { makeIssuerKit } from '@agoric/ertp';
import { withAmountUtils } from '@agoric/inter-protocol/test/supports.js';

Check failure on line 8 in packages/vats/test/test-vstoreShop.js

View workflow job for this annotation

GitHub Actions / lint-primary

'@agoric/inter-protocol' should be listed in the project's dependencies. Run 'npm i -S @agoric/inter-protocol' to add it
import { makeFakeStorageKit } from '@agoric/internal/src/storage-test-utils.js';

/** @type {import('ava').TestFn<Awaited<ReturnType<makeTestContext>>>} */
const test = anyTest;

const pathname = new URL(import.meta.url).pathname;
const dirname = path.dirname(pathname);

const assets = {
vstoreShop: `${dirname}/../src/vstoreShop.js`,
};

const makeTestContext = async () => {
const bundleCache = await unsafeMakeBundleCache('bundles/');

const { zoe } = await setUpZoeForTest();

/** @type {Installation<import('../src/vstoreShop').start>} */
const installation = await E(zoe).install(
await bundleCache.load(assets.vstoreShop, 'vstoreShop'),
);

return { zoe, installation };
};

test.before(async t => (t.context = await makeTestContext()));

test('buy and write to storage', async t => {
const { zoe, installation } = t.context;

const money = withAmountUtils(makeIssuerKit('Money'));

const { rootNode, data } = makeFakeStorageKit('X');
const { publicFacet, instance: shopInstance } = await E(zoe).startInstance(
installation,
{ Payment: money.issuer },
{ basePrice: money.units(3) },
{ storageNode: rootNode },
);

/**
* @param {Instance} shop
* @param {Purse} purse
*/
const alice = async (shop, purse) => {
const toBuy = await E(publicFacet).makeBuyStorageInvitation();
const { basePrice } = await E(zoe).getTerms(shop);
const proposal = { give: { Payment: basePrice } };
const Payment = await E(purse).withdraw(proposal.give.Payment);
const seat = await E(zoe).offer(
toBuy,
proposal,
{ Payment },
{ slug: 'alice-info' },
);
/** @type {ERef<StorageNode>} */
const node = await E(seat).getOfferResult();
// TODO: get payouts; return extras to purse

await E(node).setValue('Hello, world!');
};

const ap = money.issuer.makeEmptyPurse();
ap.deposit(money.mint.mintPayment(money.units(10)));
await alice(shopInstance, ap);

t.deepEqual(
[...data.entries()],
[['X.alice-info', '{"blockHeight":"0","values":["Hello, world!"]}']],
);
});
Loading