Skip to content

Commit

Permalink
feat: handle any issuer in postal service
Browse files Browse the repository at this point in the history
  • Loading branch information
samsiegart committed May 29, 2024
1 parent fd97f3b commit d8099bc
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 26 deletions.
20 changes: 16 additions & 4 deletions contract/src/postal-service.contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@ export const meta = harden({
// compatibility with an earlier contract metadata API
export const { customTermsShape } = meta;

let issuerNumber = 1;
const IssuerShape = M.remotable('Issuer');

/**
* @typedef {object} PostalSvcTerms
* @property {import('@agoric/vats').NameHub} namesByAddress
*/

/** @param {ZCF<PostalSvcTerms>} zcf */
export const start = zcf => {
const { namesByAddress, issuers } = zcf.getTerms();
const { namesByAddress } = zcf.getTerms();
mustMatch(namesByAddress, M.remotable('namesByAddress'));
console.log('postal-service issuers', Object.keys(issuers));

/**
* @param {string} addr
Expand All @@ -38,9 +40,19 @@ export const start = zcf => {
*/
const sendTo = (addr, pmt) => E(getDepositFacet(addr)).receive(pmt);

/** @param {string} recipient */
const makeSendInvitation = recipient => {
/**
* @param {string} recipient
* @param {Issuer[]} issuers
*/
const makeSendInvitation = (recipient, issuers) => {
assert.typeof(recipient, 'string');
mustMatch(issuers, M.arrayOf(IssuerShape));

for (const i of issuers) {
if (!Object.values(zcf.getTerms().issuers).includes(i)) {
zcf.saveIssuer(i, `Issuer${(issuerNumber += 1)}`);
}
}

/** @type {OfferHandler} */
const handleSend = async seat => {
Expand Down
14 changes: 2 additions & 12 deletions contract/src/postal-service.proposal.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@
*/
// @ts-check

import { E } from '@endo/far';
import { fixHub } from './fixHub.js';
import {
installContract,
startContract,
} from './platform-goals/start-contract.js';
import { allValues } from './objectTools.js';

const { Fail } = assert;

Expand All @@ -23,17 +21,15 @@ const contractName = 'postalService';
* @param {BootstrapPowers} powers
* @param {{ options?: { postalService: {
* bundleID: string;
* issuerNames?: string[];
* }}}} [config]
*/
export const startPostalService = async (powers, config) => {
const {
consume: { namesByAddressAdmin, agoricNames },
consume: { namesByAddressAdmin },
} = powers;
const {
// must be supplied by caller or template-replaced
bundleID = Fail`no bundleID`,
issuerNames = ['IST', 'Invitation', 'BLD', 'ATOM'],
} = config?.options?.[contractName] ?? {};

const installation = await installContract(powers, {
Expand All @@ -44,15 +40,9 @@ export const startPostalService = async (powers, config) => {
const namesByAddress = await fixHub(namesByAddressAdmin);
const terms = harden({ namesByAddress });

const issuerKeywordRecord = await allValues(
Object.fromEntries(
issuerNames.map(n => [n, E(agoricNames).lookup('issuer', n)]),
),
);

await startContract(powers, {
name: contractName,
startArgs: { installation, issuerKeywordRecord, terms },
startArgs: { installation, terms },
});
};

Expand Down
7 changes: 4 additions & 3 deletions contract/test/market-actors.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@ const { entries, fromEntries, keys } = Object;
* }} mine
* @param {{
* rxAddr: string,
* toSend: AmountKeywordRecord;
* toSend: AmountKeywordRecord,
* issuers: Issuer[]
* }} shared
*/
export const payerPete = async (
t,
{ wallet, queryTool },
{ rxAddr, toSend },
{ rxAddr, toSend, issuers },
) => {
const hub = await makeAgoricNames(queryTool);
/** @type {WellKnown} */
Expand All @@ -55,7 +56,7 @@ export const payerPete = async (
source: 'contract',
instance,
publicInvitationMaker: 'makeSendInvitation',
invitationArgs: [rxAddr],
invitationArgs: [rxAddr, issuers],
},
proposal: { give: toSend },
};
Expand Down
3 changes: 3 additions & 0 deletions contract/test/snapshots/test-postalSvc.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ Generated by [AVA](https://avajs.dev).
instance: Object @Alleged: InstanceHandle {},
invitationArgs: [
'agoric1aap7m84dt0rwhhfw49d4kv2gqetzl56vn8aaxj',
[
Object @Alleged: ATOM issuer {},
],
],
publicInvitationMaker: 'makeSendInvitation',
source: 'contract',
Expand Down
Binary file modified contract/test/snapshots/test-postalSvc.js.snap
Binary file not shown.
5 changes: 3 additions & 2 deletions contract/test/test-postalSvc.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ test.serial('deploy contract with core eval: postalService / send', async t => {
behavior: startPostalService,
entryFile: scriptRoots.postalService,
config: {
options: { postalService: { bundleID, issuerNames: ['ATOM', 'Item'] } },
options: { postalService: { bundleID } },
},
});

Expand Down Expand Up @@ -160,6 +160,7 @@ test.serial('deliver payment using offer', async t => {
toSend: {
Pmt: amt(await agoricNames.brand.ATOM, 3n),
},
issuers: [await agoricNames.issuer.ATOM],
};

const wallet = {
Expand Down Expand Up @@ -192,7 +193,7 @@ test('send invitation* from contract using publicFacet of postalService', async
const postalPowers = extract(permit, powers);
await startPostalService(postalPowers, {
options: {
postalService: { bundleID, issuerNames: ['IST', 'Invitation'] },
postalService: { bundleID },
},
});

Expand Down
13 changes: 12 additions & 1 deletion ui/src/components/pay/Pay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useContext, useEffect, useState } from 'react';
import type { Amount } from '@agoric/web-components';
import { useDisplayInfo } from '../../store/displayInfo';
import { NotificationContext } from '../../context/NotificationContext';
import { queryIssuers } from '../../utils/queryIssuers';

const Pay = () => {
const { addNotification } = useContext(NotificationContext);
Expand Down Expand Up @@ -49,12 +50,22 @@ const Pay = () => {
}, [chainStorageWatcher, recipientAddr]);

const sendOffer = async () => {
assert(chainStorageWatcher && makeOffer);

assert(chainStorageWatcher && makeOffer);
try {
const brandPetnameToIssuer = await queryIssuers(chainStorageWatcher);
const issuers = new Set(
[...myAmounts].map(amount => {
const { petname } = brandToDisplayInfo.get(amount.brand)!;
return brandPetnameToIssuer.get(petname);
}),
);

const invitationSpec = {
source: 'agoricContract',
instancePath: ['postalService'],
callPipe: [['makeSendInvitation', [recipientAddr]]],
callPipe: [['makeSendInvitation', [recipientAddr, [...issuers]]]],
};

const gives = myAmounts.map(amount => {
Expand Down
10 changes: 6 additions & 4 deletions ui/src/components/swap/Swap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,12 @@ const Swap = () => {
try {
const brandPetnameToIssuer = await queryIssuers(chainStorageWatcher);
const issuers = new Set(
[...myAmounts, ...recipientAmounts].map(amount => {
const { petname } = brandToDisplayInfo.get(amount.brand)!;
return brandPetnameToIssuer.get(petname);
}),
[...myAmounts, ...recipientAmounts, ...(fee ? [fee] : [])].map(
amount => {
const { petname } = brandToDisplayInfo.get(amount.brand)!;
return brandPetnameToIssuer.get(petname);
},
),
);

const invitationSpec = {
Expand Down

0 comments on commit d8099bc

Please sign in to comment.