-
Notifications
You must be signed in to change notification settings - Fork 1
/
postalSvc.js
64 lines (54 loc) · 1.75 KB
/
postalSvc.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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 };
};