Skip to content

Commit

Permalink
chore: postalSvc 3d899ff
Browse files Browse the repository at this point in the history
  • Loading branch information
dckc committed Feb 21, 2024
1 parent 142b481 commit 74a9f6d
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions contract/src/postalSvc.js
Original file line number Diff line number Diff line change
@@ -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<PostalSvcTerms>} 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<DepositFacet>}
*/
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 };
};

0 comments on commit 74a9f6d

Please sign in to comment.