-
Notifications
You must be signed in to change notification settings - Fork 215
/
contractHelper.js
282 lines (259 loc) · 10 KB
/
contractHelper.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import { Fail } from '@endo/errors';
import { Far } from '@endo/marshal';
import { makeStoredPublisherKit } from '@agoric/notifier';
import { getMethodNames, objectMap } from '@agoric/internal';
import { ignoreContext, prepareExo } from '@agoric/vat-data';
import { M } from '@agoric/store';
import { AmountShape, BrandShape } from '@agoric/ertp';
import { RelativeTimeRecordShape, TimestampRecordShape } from '@agoric/time';
import { E } from '@endo/eventual-send';
import { makeParamManagerFromTerms } from './contractGovernance/typedParamManager.js';
import { GovernorFacetShape } from './typeGuards.js';
import { CONTRACT_ELECTORATE } from './contractGovernance/governParam.js';
/**
* @import {VoteCounterCreatorFacet, VoteCounterPublicFacet, QuestionSpec, OutcomeRecord, AddQuestion, AddQuestionReturn, GovernanceSubscriptionState, GovernanceTerms, GovernedApis, GovernedCreatorFacet, GovernedPublicFacet} from './types.js';
*/
export const GOVERNANCE_STORAGE_KEY = 'governance';
const publicMixinAPI = harden({
getSubscription: M.call().returns(M.remotable('StoredSubscription')),
getGovernedParams: M.call().returns(M.or(M.record(), M.promise())),
getAmount: M.call().returns(AmountShape),
getBrand: M.call().returns(BrandShape),
getInstance: M.call().returns(M.remotable('Instance')),
getInstallation: M.call().returns(M.remotable('Installation')),
getInvitationAmount: M.call().returns(M.promise()),
getNat: M.call().returns(M.bigint()),
getRatio: M.call().returns(M.record()),
getString: M.call().returns(M.string()),
getTimestamp: M.call().returns(TimestampRecordShape),
getRelativeTime: M.call().returns(RelativeTimeRecordShape),
getUnknown: M.call().returns(M.any()),
});
/**
* Verify that the electorate is represented by a live invitation.
*
* @param {ZCF<GovernanceTerms<{}> & {}>} zcf
* @param {import('./contractGovernance/typedParamManager.js').TypedParamManager<any>} paramManager
*/
export const validateElectorate = (zcf, paramManager) => {
const invitation = paramManager.getInternalParamValue(CONTRACT_ELECTORATE);
return E.when(
E(zcf.getInvitationIssuer()).isLive(invitation),
isLive => isLive || Fail`Electorate invitation is not live.`,
);
};
harden(validateElectorate);
/**
* Utility function for `makeParamGovernance`.
*
* @template {import('./contractGovernance/typedParamManager.js').ParamTypesMap} T
* @param {ZCF<GovernanceTerms<{}> & {}>} zcf
* @param {import('./contractGovernance/typedParamManager.js').TypedParamManager<T>} paramManager
*/
const facetHelpers = (zcf, paramManager) => {
// validate async to wait for params to be finished
// UNTIL https://github.com/Agoric/agoric-sdk/issues/4343
void validateElectorate(zcf, paramManager);
const typedAccessors = {
getAmount: paramManager.getAmount,
getBrand: paramManager.getBrand,
getInstance: paramManager.getInstance,
getInstallation: paramManager.getInstallation,
getInvitationAmount: paramManager.getInvitationAmount,
getNat: paramManager.getNat,
getRatio: paramManager.getRatio,
getString: paramManager.getString,
getTimestamp: paramManager.getTimestamp,
getRelativeTime: paramManager.getRelativeTime,
getUnknown: paramManager.getUnknown,
};
const commonPublicMethods = {
getSubscription: () => paramManager.getSubscription(),
getGovernedParams: () => paramManager.getParams(),
};
/**
* Add required methods to publicFacet
*
* @template {{}} PF public facet
* @param {PF} originalPublicFacet
* @returns {GovernedPublicFacet<PF>}
*/
const augmentPublicFacet = originalPublicFacet => {
return Far('publicFacet', {
...originalPublicFacet,
...commonPublicMethods,
...typedAccessors,
});
};
/**
* Add required methods to publicFacet, for a virtual/durable contract
*
* @param {OPF} originalPublicFacet
* @template {{}} OPF
*/
const augmentVirtualPublicFacet = originalPublicFacet => {
return Far('publicFacet', {
...originalPublicFacet,
...commonPublicMethods,
...objectMap(typedAccessors, ignoreContext),
});
};
/**
* @template {{}} CF
* @param {CF} limitedCreatorFacet
* @param {Record<string, (...any) => unknown>} [governedApis]
* @returns {GovernedCreatorFacet<CF>}
*/
const makeFarGovernorFacet = (limitedCreatorFacet, governedApis = {}) => {
const governorFacet = Far('governorFacet', {
getParamMgrRetriever: () =>
Far('paramRetriever', { get: () => paramManager }),
getInvitation: name => paramManager.getInternalParamValue(name),
getLimitedCreatorFacet: () => limitedCreatorFacet,
// The contract provides a facet with the APIs that can be invoked by
// governance
/** @type {() => GovernedApis} */
getGovernedApis: () => Far('governedAPIs', governedApis),
// The facet returned by getGovernedApis is Far, so we can't see what
// methods it has. There's no clean way to have contracts specify the APIs
// without also separately providing their names.
getGovernedApiNames: () => Object.keys(governedApis),
setOfferFilter: strings => zcf.setOfferFilter(strings),
});
// exclusively for contractGovernor, which only reveals limitedCreatorFacet
return governorFacet;
};
/**
* @template {{}} CF
* @param {CF} originalCreatorFacet
* @param {{}} [governedApis]
* @returns {GovernedCreatorFacet<CF>}
*/
const makeGovernorFacet = (originalCreatorFacet, governedApis = {}) => {
return makeFarGovernorFacet(originalCreatorFacet, governedApis);
};
/**
* Add required methods to a creatorFacet for a durable contract.
*
* @see {makeDurableGovernorFacet}
*
* @template {{ [methodName: string]: (context?: unknown, ...rest: unknown[]) => unknown}} LCF
* @param {LCF} limitedCreatorFacet
*/
const makeVirtualGovernorFacet = limitedCreatorFacet => {
/** @type {import('@agoric/swingset-liveslots').FunctionsPlusContext<unknown, GovernedCreatorFacet<limitedCreatorFacet>>} */
const governorFacet = harden({
getParamMgrRetriever: () =>
Far('paramRetriever', { get: () => paramManager }),
getInvitation: (_context, /** @type {string} */ name) =>
paramManager.getInternalParamValue(name),
getLimitedCreatorFacet: ({ facets }) => facets.limitedCreatorFacet,
// The contract provides a facet with the APIs that can be invoked by
// governance
getGovernedApis: ({ facets }) => facets.governedApis,
// The facet returned by getGovernedApis is Far, so we can't see what
// methods it has. There's no clean way to have contracts specify the APIs
// without also separately providing their names.
getGovernedApiNames: ({ facets }) =>
getMethodNames(facets.governedApis || {}),
setOfferFilter: (_context, strings) => zcf.setOfferFilter(strings),
});
return { governorFacet, limitedCreatorFacet };
};
/**
* Add required methods to a creatorFacet for a durable contract.
*
* @see {makeVirtualGovernorFacet}
*
* @template CF
* @param {import('@agoric/vat-data').Baggage} baggage
* @param {CF} limitedCreatorFacet
* @param {Record<string, (...any) => unknown>} [governedApis]
*/
const makeDurableGovernorFacet = (
baggage,
limitedCreatorFacet,
governedApis = {},
) => {
const governorFacet = prepareExo(
baggage,
'governorFacet',
M.interface('governorFacet', GovernorFacetShape),
{
getParamMgrRetriever: () =>
Far('paramRetriever', { get: () => paramManager }),
getInvitation: name => paramManager.getInternalParamValue(name),
getLimitedCreatorFacet: () => limitedCreatorFacet,
// The contract provides a facet with the APIs that can be invoked by
// governance
/** @type {() => GovernedApis} */
getGovernedApis: () => Far('governedAPIs', governedApis),
// The facet returned by getGovernedApis is Far, so we can't see what
// methods it has. There's no clean way to have contracts specify the APIs
// without also separately providing their names.
getGovernedApiNames: () => Object.keys(governedApis || {}),
setOfferFilter: strings => zcf.setOfferFilter(strings),
},
);
return { governorFacet, limitedCreatorFacet };
};
return harden({
publicMixin: {
...commonPublicMethods,
...typedAccessors,
},
augmentPublicFacet,
augmentVirtualPublicFacet,
makeGovernorFacet,
makeFarGovernorFacet,
makeVirtualGovernorFacet,
makeDurableGovernorFacet,
params: paramManager.readonly(),
});
};
/**
* Helper for the 90% of contracts that will have only a single set of
* parameters. Using this for managed parameters, a contract only has to
*
* - Define the parameter template, which includes name, type and value
* - Add any methods needed in the public and creator facets.
*
* It's also crucial that the governed contract not interact with the product of
* makeGovernorFacet(). The wrapped creatorFacet has the power to change
* parameter values, and the governance guarantees only hold if they're not used
* directly by the governed contract.
*
* @template {import('./contractGovernance/typedParamManager.js').ParamTypesMap} M
* Map of types of custom governed terms
* @param {ZCF<GovernanceTerms<M>>} zcf
* @param {Invitation} initialPoserInvitation
* @param {M} paramTypesMap
* @param {ERef<StorageNode>} [storageNode]
* @param {ERef<Marshaller>} [marshaller]
* @param {object} [overrides]
*/
const handleParamGovernance = (
zcf,
initialPoserInvitation,
paramTypesMap,
storageNode,
marshaller,
overrides,
) => {
/** @type {import('@agoric/notifier').StoredPublisherKit<GovernanceSubscriptionState>} */
const publisherKit = makeStoredPublisherKit(
storageNode,
marshaller,
GOVERNANCE_STORAGE_KEY,
);
const paramManager = makeParamManagerFromTerms(
publisherKit,
zcf,
{ Electorate: initialPoserInvitation },
paramTypesMap,
overrides,
);
return facetHelpers(zcf, paramManager);
};
harden(handleParamGovernance);
export { handleParamGovernance, publicMixinAPI };