-
Notifications
You must be signed in to change notification settings - Fork 212
/
contractHelper.js
249 lines (227 loc) · 8.57 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
import { Far } from '@endo/marshal';
import { makeStoredPublisherKit } from '@agoric/notifier';
import { getMethodNames, objectMap } from '@agoric/internal';
import { ignoreContext } from '@agoric/vat-data';
import { keyEQ, M } from '@agoric/store';
import { AmountShape, BrandShape } from '@agoric/ertp';
import { RelativeTimeRecordShape, TimestampRecordShape } from '@agoric/time';
import { assertElectorateMatches } from './contractGovernance/paramManager.js';
import { makeParamManagerFromTerms } from './contractGovernance/typedParamManager.js';
const { Fail, quote: q } = assert;
export const GOVERNANCE_STORAGE_KEY = 'governance';
const publicMixinAPI = harden({
getSubscription: M.call().returns(M.remotable('StoredSubscription')),
getContractGovernor: M.call().returns(M.remotable('Instance')),
getGovernedParams: M.call().returns(M.record()),
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()),
});
/**
* Utility function for `makeParamGovernance`.
*
* @template T
* @param {ZCF<GovernanceTerms<{}> & {}>} zcf
* @param {import('./contractGovernance/typedParamManager').TypedParamManager<T>} paramManager
*/
const facetHelpers = (zcf, paramManager) => {
const terms = zcf.getTerms();
const { governedParams } = terms;
keyEQ(governedParams, paramManager.getParams()) ||
Fail`Terms must include ${q(paramManager.getParams())}, but were ${q(
governedParams,
)}`;
assertElectorateMatches(paramManager, governedParams);
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 { electionManager } = terms;
const commonPublicMethods = {
getSubscription: () => paramManager.getSubscription(),
getContractGovernor: () => electionManager,
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 creator facet
* @param {CF} originalCreatorFacet
* @returns {LimitedCreatorFacet<CF>}
*/
const makeLimitedCreatorFacet = originalCreatorFacet => {
return Far('governedContract creator facet', {
...originalCreatorFacet,
getContractGovernor: () => electionManager,
});
};
/**
* Add required methods to a creatorFacet
*
* @template {{}} CF creator facet
* @param {CF} originalCreatorFacet
* @param {Record<string, (...args: any[]) => any>} governedApis
* @returns {GovernedCreatorFacet<CF>}
*/
/**
* @template {{}} CF
* @param {CF} limitedCreatorFacet
* @param {Record<string, (...any) => unknown>} [governedApis]
* @returns {GovernorFacet<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 {GovernorFacet<CF>}
*/
const makeGovernorFacet = (originalCreatorFacet, governedApis = {}) => {
const limitedCreatorFacet = makeLimitedCreatorFacet(originalCreatorFacet);
return makeFarGovernorFacet(limitedCreatorFacet, governedApis);
};
/**
* Add required methods to a creatorFacet for a virtual/durable contract.
*
* @param {{ [methodName: string]: (context?: unknown, ...rest: unknown[]) => unknown}} originalCreatorFacet
*/
const makeVirtualGovernorFacet = originalCreatorFacet => {
const limitedCreatorFacet = makeLimitedCreatorFacet(originalCreatorFacet);
/** @type {import('@agoric/vat-data/src/types.js').FunctionsPlusContext<unknown, GovernorFacet<originalCreatorFacet>>} */
const governorFacet = harden({
getParamMgrRetriever: () =>
Far('paramRetriever', { get: () => paramManager }),
getInvitation: (_context, 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 };
};
return harden({
publicMixin: {
...commonPublicMethods,
...typedAccessors,
},
creatorMixin: {
getContractGovernor: () => electionManager,
},
augmentPublicFacet,
augmentVirtualPublicFacet,
makeGovernorFacet,
makeVirtualGovernorFacet,
makeFarGovernorFacet,
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').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]
*/
const handleParamGovernance = async (
zcf,
initialPoserInvitation,
paramTypesMap,
storageNode,
marshaller,
) => {
/** @type {import('@agoric/notifier').StoredPublisherKit<GovernanceSubscriptionState>} */
const publisherKit = makeStoredPublisherKit(
storageNode,
marshaller,
GOVERNANCE_STORAGE_KEY,
);
const paramManager = await makeParamManagerFromTerms(
publisherKit,
zcf,
{ Electorate: initialPoserInvitation },
paramTypesMap,
);
return facetHelpers(zcf, paramManager);
};
harden(handleParamGovernance);
export { handleParamGovernance, publicMixinAPI };