Skip to content

Commit

Permalink
refactor: follow X.contract.js pattern; spell out postalService
Browse files Browse the repository at this point in the history
  • Loading branch information
dckc committed Mar 5, 2024
1 parent 630be97 commit 2863950
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const { keys, values } = Object;
export const start = zcf => {
const { namesByAddress, issuers } = zcf.getTerms();
mustMatch(namesByAddress, M.remotable('namesByAddress'));
console.log('postalSvc issuers', Object.keys(issuers));
console.log('postal-service issuers', Object.keys(issuers));

/**
* @param {string} addr
Expand Down
42 changes: 21 additions & 21 deletions contract/src/start-postalSvc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @file core eval script* to start the postalSvc contract.
* @file core eval script* to start the postalService contract.
*
* * see rollup.config.mjs to make a script from this file.
*
Expand All @@ -10,53 +10,53 @@
import { E } from '@endo/far';
import { fixHub } from './fixHub.js';

const trace = (...args) => console.log('start-postalSvc', ...args);
const trace = (...args) => console.log('start-postalService', ...args);

const { Fail } = assert;

/**
* @typedef { typeof import('../src/postalSvc.js').start } PostalSvcFn
* @typedef { typeof import('./postal-service.contract.js').start } PostalServiceFn
*
* @typedef {{
* produce: { postalSvcKit: Producer<unknown> },
* produce: { postalServiceKit: Producer<unknown> },
* installation: {
* consume: { postalSvc: Promise<Installation<PostalSvcFn>> },
* produce: { postalSvc: Producer<Installation<PostalSvcFn>> },
* consume: { postalService: Promise<Installation<PostalServiceFn>> },
* produce: { postalService: Producer<Installation<PostalServiceFn>> },
* }
* instance: {
* consume: { postalSvc: Promise<StartedInstanceKit<PostalSvcFn>['instance']> },
* produce: { postalSvc: Producer<StartedInstanceKit<PostalSvcFn>['instance']> },
* consume: { postalService: Promise<StartedInstanceKit<PostalServiceFn>['instance']> },
* produce: { postalService: Producer<StartedInstanceKit<PostalServiceFn>['instance']> },
* }
* }} PostalSvcPowers
* }} PostalServicePowers
*/

/**
* @param {BootstrapPowers} powers
* @param {{ options?: { postalSvc: {
* @param {{ options?: { postalService: {
* bundleID: string;
* issuerNames?: string[];
* }}}} [config]
*/
export const startPostalSvc = async (powers, config) => {
/** @type { BootstrapPowers & PostalSvcPowers} */
export const startPostalService = async (powers, config) => {
/** @type { BootstrapPowers & PostalServicePowers} */
// @ts-expect-error bootstrap powers evolve with BLD staker governance
const postalPowers = powers;
const {
consume: { zoe, namesByAddressAdmin, agoricNames },
installation: {
produce: { postalSvc: produceInstallation },
produce: { postalService: produceInstallation },
},
instance: {
produce: { postalSvc: produceInstance },
produce: { postalService: produceInstance },
},
} = postalPowers;
const {
// separate line for bundling
bundleID = Fail`no bundleID`,
issuerNames = ['IST', 'Invitation', 'BLD', 'ATOM'],
} = config?.options?.postalSvc ?? {};
} = config?.options?.postalService ?? {};

/** @type {Installation<PostalSvcFn>} */
/** @type {Installation<PostalServiceFn>} */
const installation = await E(zoe).installBundleID(bundleID);
produceInstallation.resolve(installation);

Expand All @@ -71,26 +71,26 @@ export const startPostalSvc = async (powers, config) => {
});
produceInstance.resolve(instance);

trace('postalSvc started');
trace('postalService started');
};

export const manifest = /** @type {const} */ ({
[startPostalSvc.name]: {
[startPostalService.name]: {
consume: {
agoricNames: true,
namesByAddress: true,
namesByAddressAdmin: true,
zoe: true,
},
installation: {
produce: { postalSvc: true },
produce: { postalService: true },
},
instance: {
produce: { postalSvc: true },
produce: { postalService: true },
},
},
});

export const permit = Object.values(manifest)[0];

export const main = startPostalSvc;
export const main = startPostalService;
10 changes: 5 additions & 5 deletions contract/test/market-actors.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const payerPete = async (
/** @type {WellKnown} */
const agoricNames = makeNameProxy(hub);

const instance = await agoricNames.instance.postalSvc;
const instance = await agoricNames.instance.postalService;

t.log('Pete offers to send to', rxAddr, 'via contract', instance);
/** @type {import('@agoric/smart-wallet/src/offers.js').OfferSpec} */
Expand Down Expand Up @@ -142,25 +142,25 @@ export const receiverRex = async (t, { wallet }, { toSend }) => {

export const senderContract = async (
t,
{ zoe, terms: { postalSvc: instance, destAddr: addr1 } },
{ zoe, terms: { postalService: instance, destAddr: addr1 } },
) => {
const iIssuer = await E(zoe).getInvitationIssuer();
const iBrand = await E(iIssuer).getBrand();
const postalSvc = E(zoe).getPublicFacet(instance);
const postalService = E(zoe).getPublicFacet(instance);
const purse = await E(iIssuer).makeEmptyPurse();

const noInvitations = AmountMath.make(iBrand, harden([]));
const pmt1 = await E(purse).withdraw(noInvitations);

t.log(
'senderContract: E(',
getInterfaceOf(await postalSvc),
getInterfaceOf(await postalService),
').sendTo(',
addr1,
',',
noInvitations,
')',
);
const sent = await E(postalSvc).sendTo(addr1, pmt1);
const sent = await E(postalService).sendTo(addr1, pmt1);
t.deepEqual(sent, noInvitations);
};
38 changes: 20 additions & 18 deletions contract/test/test-postalSvc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { createRequire } from 'module';

import { E, passStyleOf } from '@endo/far';
import { AmountMath } from '@agoric/ertp/src/amountMath.js';
import { startPostalSvc } from '../src/start-postalSvc.js';
import { startPostalService } from '../src/start-postalSvc.js';
import { bootAndInstallBundles, makeMockTools } from './boot-tools.js';
import { makeBundleCacheContext, getBundleId } from './bundle-tools.js';
import { mockWalletFactory } from './wallet-tools.js';
Expand Down Expand Up @@ -58,7 +58,7 @@ test.serial('well-known brand (ATOM) is available', async t => {
t.is(passStyleOf(brand.ATOM), 'remotable');
});

test.serial('install bundle: postalSvc / send', async t => {
test.serial('install bundle: postalService / send', async t => {
const { installBundles } = t.context;
console.time('installBundles');
console.timeLog('installBundles', Object.keys(bundleRoots).length, 'todo');
Expand All @@ -67,27 +67,27 @@ test.serial('install bundle: postalSvc / send', async t => {
);
console.timeEnd('installBundles');

const id = getBundleId(bundles.postalSvc);
const id = getBundleId(bundles.postalService);
const shortId = id.slice(0, 8);
t.log('postalSvc', shortId);
t.log('postalService', shortId);
t.is(id.length, 3 + 128, 'bundleID length');
t.regex(id, /^b1-.../);

Object.assign(t.context.shared, { bundles });
});

test.serial('deploy contract with core eval: postalSvc / send', async t => {
test.serial('deploy contract with core eval: postalService / send', async t => {
const { runCoreEval } = t.context;
const { bundles } = t.context.shared;
const bundleID = getBundleId(bundles.postalSvc);
const bundleID = getBundleId(bundles.postalService);

const name = 'send';
const result = await runCoreEval({
name,
behavior: startPostalSvc,
entryFile: scriptRoots.postalSvc,
behavior: startPostalService,
entryFile: scriptRoots.postalService,
config: {
options: { postalSvc: { bundleID, issuerNames: ['ATOM', 'Item'] } },
options: { postalService: { bundleID, issuerNames: ['ATOM', 'Item'] } },
},
});

Expand All @@ -100,12 +100,12 @@ test.serial('deploy contract with core eval: postalSvc / send', async t => {
});
});

test.serial('agoricNames.instances has contract: postalSvc', async t => {
test.serial('agoricNames.instances has contract: postalService', async t => {
const { makeQueryTool } = t.context;
const hub0 = makeAgoricNames(makeQueryTool());
const agoricNames = makeNameProxy(hub0);
await null;
const instance = await agoricNames.instance.postalSvc;
const instance = await agoricNames.instance.postalService;
t.log(instance);
t.is(passStyleOf(instance), 'remotable');
});
Expand Down Expand Up @@ -151,12 +151,14 @@ test.serial('deliver payment using offer', async t => {

test.todo('E2E: send using publicFacet using contract');

test('send invitation* from contract using publicFacet of postalSvc', async t => {
test('send invitation* from contract using publicFacet of postalService', async t => {
const { powers, bundles } = await bootAndInstallBundles(t, bundleRoots);

const bundleID = getBundleId(bundles.postalSvc);
await startPostalSvc(powers, {
options: { postalSvc: { bundleID, issuerNames: ['IST', 'Invitation'] } },
const bundleID = getBundleId(bundles.postalService);
await startPostalService(powers, {
options: {
postalService: { bundleID, issuerNames: ['IST', 'Invitation'] },
},
});

const { zoe, namesByAddressAdmin } = powers.consume;
Expand All @@ -171,10 +173,10 @@ test('send invitation* from contract using publicFacet of postalSvc', async t =>
{ zoe, namesByAddressAdmin },
smartWalletIssuers,
);
/** @type {import('../src/start-postalSvc.js').PostalSvcPowers} */
/** @type {import('../src/start-postalSvc.js').PostalServicePowers} */
// @ts-expect-error cast
const postalSpace = powers;
const instance = await postalSpace.instance.consume.postalSvc;
const instance = await postalSpace.instance.consume.postalService;

const shared = {
rxAddr: 'agoric1receiverRex',
Expand All @@ -187,7 +189,7 @@ test('send invitation* from contract using publicFacet of postalSvc', async t =>
};

const wallet = await walletFactory.makeSmartWallet(shared.rxAddr);
const terms = { postalSvc: instance, destAddr: shared.rxAddr };
const terms = { postalService: instance, destAddr: shared.rxAddr };
await Promise.all([
senderContract(t, { zoe, terms }),
receiverRex(t, { wallet }, shared),
Expand Down

0 comments on commit 2863950

Please sign in to comment.