From 98f942832f9c927a250a126081abac2c7da54bba Mon Sep 17 00:00:00 2001 From: Dan Connolly Date: Wed, 21 Feb 2024 17:22:20 -0600 Subject: [PATCH] chore: postalSvc 3d899ff --- contract/src/postalSvc.js | 64 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 contract/src/postalSvc.js diff --git a/contract/src/postalSvc.js b/contract/src/postalSvc.js new file mode 100644 index 00000000..045d8374 --- /dev/null +++ b/contract/src/postalSvc.js @@ -0,0 +1,64 @@ +// @ts-check +import { E, Far } from '@endo/far'; +import { M, mustMatch } from '@endo/patterns'; +import { withdrawFromSeat } from '@agoric/zoe/src/contractSupport/zoeHelpers.js'; + +const { keys, values } = Object; + +/** + * @typedef {object} PostalSvcTerms + * @property {import('@agoric/vats').NameHub} namesByAddress + */ + +/** @param {ZCF} zcf */ +export const start = zcf => { + const { namesByAddress, issuers } = zcf.getTerms(); + mustMatch(namesByAddress, M.remotable('namesByAddress')); + console.log('postalSvc issuers', Object.keys(issuers)); + + /** + * @param {string} addr + * @returns {ERef} + */ + const getDepositFacet = addr => { + assert.typeof(addr, 'string'); + return E(namesByAddress).lookup(addr, 'depositFacet'); + }; + + /** + * @param {string} addr + * @param {Payment} pmt + */ + const sendTo = (addr, pmt) => E(getDepositFacet(addr)).receive(pmt); + + /** @param {string} recipient */ + const makeSendInvitation = recipient => { + assert.typeof(recipient, 'string'); + + /** @type {OfferHandler} */ + const handleSend = async seat => { + const { give } = seat.getProposal(); + const depositFacet = await getDepositFacet(recipient); + const payouts = await withdrawFromSeat(zcf, seat, give); + + // XXX partial failure? return payments? + await Promise.all( + values(payouts).map(pmtP => + Promise.resolve(pmtP).then(pmt => E(depositFacet).receive(pmt)), + ), + ); + seat.exit(); + return `sent ${keys(payouts).join(', ')}`; + }; + + return zcf.makeInvitation(handleSend, 'send'); + }; + + const publicFacet = Far('postalSvc', { + lookup: (...path) => E(namesByAddress).lookup(...path), + getDepositFacet, + sendTo, + makeSendInvitation, + }); + return { publicFacet }; +};