-
Notifications
You must be signed in to change notification settings - Fork 208
/
invitations.js
171 lines (157 loc) · 5.8 KB
/
invitations.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import { AmountMath } from '@agoric/ertp';
import { mustMatch } from '@agoric/store';
import { InvitationShape } from '@agoric/zoe/src/typeGuards.js';
import { Fail } from '@endo/errors';
import { E } from '@endo/far';
import { shape } from './typeGuards.js';
// A safety limit
const MAX_PIPE_LENGTH = 2;
/**
* @typedef {AgoricContractInvitationSpec
* | ContractInvitationSpec
* | PurseInvitationSpec
* | ContinuingInvitationSpec} InvitationSpec
* Specify how to produce an invitation. See each type in the union for details.
*/
/**
* @typedef {{
* source: 'agoricContract';
* instancePath: string[];
* callPipe: [methodName: string, methodArgs?: any[]][];
* }} AgoricContractInvitationSpec
* source of invitation is a chain of calls starting with an agoricName
*
* - the start of the pipe is a lookup of instancePath within agoricNames
* - each entry in the callPipe executes a call on the preceding result
* - the end of the pipe is expected to return an Invitation
*
*
* @typedef {{
* source: 'contract';
* instance: Instance;
* publicInvitationMaker: string;
* invitationArgs?: any[];
* }} ContractInvitationSpec
* source is a contract (in which case this takes an Instance to look up in zoe)
*
* @typedef {{
* source: 'purse';
* instance: Instance;
* description: string;
* }} PurseInvitationSpec
* the invitation is already in your Zoe "invitation" purse so we need to query
* it
*
* - use the find/query invitation by kvs thing
*
*
* @typedef {{
* source: 'continuing';
* previousOffer: import('./offers.js').OfferId;
* invitationMakerName: string;
* invitationArgs?: any[];
* }} ContinuingInvitationSpec
* continuing invitation in which the offer result from a previous invitation
* had an `invitationMakers` property
*/
/**
* @typedef {Pick<InvitationDetails, 'description' | 'instance'>} InvitationsPurseQuery
*/
/**
* @param {ERef<ZoeService>} zoe
* @param {ERef<import('@agoric/vats').NameHub>} agoricNames
* @param {Brand<'set'>} invitationBrand
* @param {Purse<'set', InvitationDetails>} invitationsPurse
* @param {(fromOfferId: string) => import('./types.js').InvitationMakers} getInvitationContinuation
*/
export const makeInvitationsHelper = (
zoe,
agoricNames,
invitationBrand,
invitationsPurse,
getInvitationContinuation,
) => {
const invitationGetters = /** @type {const} */ ({
/** @type {(spec: AgoricContractInvitationSpec) => Promise<Invitation>} */
async agoricContract(spec) {
mustMatch(spec, shape.AgoricContractInvitationSpec);
const { instancePath, callPipe } = spec;
instancePath.length >= 1 || Fail`instancePath path list empy`;
callPipe.length <= MAX_PIPE_LENGTH ||
Fail`callPipe longer than MAX_PIPE_LENGTH=${MAX_PIPE_LENGTH}`;
const instance = E(agoricNames).lookup('instance', ...instancePath);
let eref = E(zoe).getPublicFacet(instance);
for (const [methodName, methodArgs = []] of callPipe) {
eref = E(eref)[methodName](...methodArgs);
}
const invitation = await eref;
mustMatch(invitation, InvitationShape);
return invitation;
},
/** @type {(spec: ContractInvitationSpec) => Promise<Invitation>} */
contract(spec) {
mustMatch(spec, shape.ContractInvitationSpec);
const { instance, publicInvitationMaker, invitationArgs = [] } = spec;
const pf = E(zoe).getPublicFacet(instance);
return E(pf)[publicInvitationMaker](...invitationArgs);
},
/** @type {(spec: PurseInvitationSpec) => Promise<Invitation>} */
async purse(spec) {
mustMatch(spec, shape.PurseInvitationSpec);
const { instance, description } = spec;
(instance && description) || Fail`missing instance or description`;
const purseAmount = await E(invitationsPurse).getCurrentAmount();
const invitations = AmountMath.getValue(invitationBrand, purseAmount);
const matches = invitations.filter(
details =>
description === details.description && instance === details.instance,
);
if (matches.length === 0) {
// look up diagnostic info
const dCount = invitations.filter(
details => description === details.description,
).length;
const iCount = invitations.filter(
details => instance === details.instance,
).length;
assert.fail(
`no invitation match (${dCount} description and ${iCount} instance)`,
);
} else if (matches.length > 1) {
// TODO? allow further disambiguation
console.warn('multiple invitation matches, picking the first');
}
const match = matches[0];
const toWithDraw = AmountMath.make(invitationBrand, harden([match]));
console.log('.... ', { toWithDraw });
return E(invitationsPurse).withdraw(toWithDraw);
},
/** @type {(spec: ContinuingInvitationSpec) => Promise<Invitation>} */
continuing(spec) {
mustMatch(spec, shape.ContinuingInvitationSpec);
const { previousOffer, invitationArgs = [], invitationMakerName } = spec;
const makers = getInvitationContinuation(String(previousOffer));
if (!makers) {
Fail`invalid value stored for previous offer ${previousOffer}`;
}
return E(makers)[invitationMakerName](...invitationArgs);
},
});
/** @type {(spec: InvitationSpec) => ERef<Invitation>} */
const invitationFromSpec = spec => {
switch (spec.source) {
case 'agoricContract':
return invitationGetters.agoricContract(spec);
case 'contract':
return invitationGetters.contract(spec);
case 'purse':
return invitationGetters.purse(spec);
case 'continuing':
return invitationGetters.continuing(spec);
default:
throw Error('unrecognize invitation source');
}
};
return invitationFromSpec;
};
harden(makeInvitationsHelper);