-
Notifications
You must be signed in to change notification settings - Fork 212
/
price-feed-proposal.js
339 lines (312 loc) · 8.79 KB
/
price-feed-proposal.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// @ts-nocheck -- lots of type errors. low prio b/c proposals are like scripts
import { E } from '@endo/far';
import { makeIssuerKit } from '@agoric/ertp';
import {
makeStorageNodeChild,
assertPathSegment,
} from '@agoric/vats/src/lib-chainStorage.js';
import { deeplyFulfilledObject } from '@agoric/internal';
import { unitAmount } from '@agoric/zoe/src/contractSupport/priceQuote.js';
import { reserveThenDeposit, reserveThenGetNames } from './utils.js';
import { makeTracer } from '../makeTracer.js';
const trace = makeTracer('RunPriceFeed');
/** @type {(name: string) => string} */
const sanitizePathSegment = name => {
const candidate = name.replace(/ /g, '_');
assertPathSegment(candidate);
return candidate;
};
/**
* @typedef {{
* brandIn?: ERef<Brand<'nat'> | undefined>,
* brandOut?: ERef<Brand<'nat'> | undefined>,
* IN_BRAND_NAME: string,
* IN_BRAND_DECIMALS: string,
* OUT_BRAND_NAME: string,
* OUT_BRAND_DECIMALS: string,
* }} PriceFeedOptions
*/
/**
* Create inert brands (no mint or issuer) referred to by price oracles.
*
* @param {ChainBootstrapSpace} space
* @param {{options: {priceFeedOptions: PriceFeedOptions}}} opt
* @returns {Promise<[Brand<'nat'>, Brand<'nat'>]>}
*/
export const ensureOracleBrands = async (
{ consume: { agoricNamesAdmin } },
{
options: {
priceFeedOptions: {
brandIn: rawBrandIn,
brandOut: rawBrandOut,
IN_BRAND_NAME,
IN_BRAND_DECIMALS,
OUT_BRAND_NAME,
OUT_BRAND_DECIMALS,
},
},
},
) => {
trace('ensureOracleBrands');
/** @type {Promise<import('@agoric/vats').NameAdmin>} */
const obAdmin = E(agoricNamesAdmin).lookupAdmin('oracleBrand');
/** @type {(brand: ERef<Brand<'nat'> | undefined>, name: string, decimals: string) => Promise<Brand<'nat'>>} */
const updateFreshBrand = async (brand, name, decimals) => {
const b = await brand;
if (b) {
// Don't update if it was already set.
return b;
}
const freshBrand = makeIssuerKit(
name,
undefined,
harden({ decimalPlaces: parseInt(decimals, 10) }),
).brand;
if (!name) {
// Don't update unnamed brands.
return freshBrand;
}
// Atomically update if not already set.
return E(obAdmin).default(name, freshBrand);
};
return Promise.all([
updateFreshBrand(rawBrandIn, IN_BRAND_NAME, IN_BRAND_DECIMALS),
updateFreshBrand(rawBrandOut, OUT_BRAND_NAME, OUT_BRAND_DECIMALS),
]);
};
/**
* @param {ChainBootstrapSpace} powers
* @param {{options: {priceFeedOptions: {AGORIC_INSTANCE_NAME: string, oracleAddresses: string[], contractTerms: unknown, IN_BRAND_NAME: string, OUT_BRAND_NAME: string}}}} config
*/
export const createPriceFeed = async (
{
consume: {
agoricNamesAdmin,
aggregators,
board,
chainStorage,
chainTimerService,
client,
namesByAddressAdmin,
priceAuthority,
priceAuthorityAdmin,
zoe,
},
produce: { aggregators: produceAggregators },
},
{
options: {
priceFeedOptions: {
AGORIC_INSTANCE_NAME,
oracleAddresses,
contractTerms,
IN_BRAND_NAME,
OUT_BRAND_NAME,
},
},
},
) => {
trace('createPriceFeed');
const STORAGE_PATH = 'priceFeed';
// Default to an empty Map and home.priceAuthority.
produceAggregators.resolve(new Map());
E(client).assignBundle([_addr => ({ priceAuthority })]);
const timer = await chainTimerService;
/**
* Values come from economy-template.json, which at this writing had IN:ATOM, OUT:USD
*
* @type {[[Brand<'nat'>, Brand<'nat'>], [Installation<import('@agoric/zoe/src/contracts/priceAggregator.js').start>]]}
*/
const [[brandIn, brandOut], [priceAggregator]] = await Promise.all([
reserveThenGetNames(E(agoricNamesAdmin).lookupAdmin('oracleBrand'), [
IN_BRAND_NAME,
OUT_BRAND_NAME,
]),
reserveThenGetNames(E(agoricNamesAdmin).lookupAdmin('installation'), [
'priceAggregator',
]),
]);
const unitAmountIn = await unitAmount(brandIn);
/** @type {import('@agoric/zoe/src/contracts/priceAggregator.js').PriceAggregatorContract['terms']} */
const terms = await deeplyFulfilledObject(
harden({
...contractTerms,
description: AGORIC_INSTANCE_NAME,
brandIn,
brandOut,
timer,
unitAmountIn,
}),
);
const storageNode = await makeStorageNodeChild(chainStorage, STORAGE_PATH);
const marshaller = E(board).getReadonlyMarshaller();
// Create the price feed.
const aggregator = await E(zoe).startInstance(
priceAggregator,
undefined,
terms,
{
storageNode: E(storageNode).makeChildNode(
sanitizePathSegment(AGORIC_INSTANCE_NAME),
),
marshaller,
},
);
E(aggregators).set(terms, { aggregator });
E(E(agoricNamesAdmin).lookupAdmin('instance')).update(
AGORIC_INSTANCE_NAME,
aggregator.instance,
);
// Publish price feed in home.priceAuthority.
const forceReplace = true;
E(priceAuthorityAdmin)
.registerPriceAuthority(
E(aggregator.publicFacet).getPriceAuthority(),
brandIn,
brandOut,
forceReplace,
)
.then(deleter => E(aggregators).set(terms, { aggregator, deleter }));
/**
* Send an invitation to one of the oracles.
*
* @param {string} addr
*/
const distributeInvitation = async addr => {
const invitation = await E(aggregator.creatorFacet).makeOracleInvitation(
addr,
);
await reserveThenDeposit(
`${AGORIC_INSTANCE_NAME} member ${addr}`,
namesByAddressAdmin,
addr,
[invitation],
);
};
trace('distributing invitations', oracleAddresses);
await Promise.all(oracleAddresses.map(distributeInvitation));
trace('createPriceFeed complete');
};
const t = 'priceFeed';
/**
* Add a price feed to a running chain, returning the manifest, installations, and options.
*
* @param {object} utils
* @param {(ref: unknown) => Promise<unknown>} [utils.restoreRef]
* @param {PriceFeedOptions} priceFeedOptions
*/
export const getManifestForPriceFeed = async (
{ restoreRef },
priceFeedOptions,
) => ({
manifest: {
[createPriceFeed.name]: {
consume: {
aggregators: t,
agoricNamesAdmin: t,
board: t,
chainStorage: t,
chainTimerService: t,
client: t,
namesByAddressAdmin: t,
priceAuthority: t,
priceAuthorityAdmin: t,
zoe: t,
},
produce: { aggregators: t },
},
[ensureOracleBrands.name]: {
consume: {
agoricNamesAdmin: t,
},
},
},
installations: {
priceAggregator: restoreRef(priceFeedOptions.priceAggregatorRef),
},
options: {
priceFeedOptions: {
brandIn:
priceFeedOptions.brandInRef && restoreRef(priceFeedOptions.brandInRef),
brandOut:
priceFeedOptions.brandOutRef &&
restoreRef(priceFeedOptions.brandeOutRef),
...priceFeedOptions,
},
},
});
/**
* @param {import('./econ-behaviors').EconomyBootstrapPowers} powers
* @param {object} [config]
* @param {object} [config.options]
* @param {string[]} [config.options.demoOracleAddresses]
*/
export const startPriceFeeds = async (
{
consume,
produce,
installation: {
consume: { priceAggregator },
},
},
{ options: { demoOracleAddresses = [] } = {} },
) => {
trace('startPriceFeeds');
// eventually this will have be parameterized. for now we just need one contract
// working well enough to build tooling around.
const inBrandName = 'ATOM';
const outBrandName = 'USD';
/** @type {ERef<NameAdmin>} */
const installAdmin = E(consume.agoricNamesAdmin).lookupAdmin('installation');
await E(installAdmin).update('priceAggregator', priceAggregator);
await ensureOracleBrands(
{ consume },
{
options: {
priceFeedOptions: {
IN_BRAND_NAME: inBrandName,
IN_BRAND_DECIMALS: '6',
OUT_BRAND_NAME: outBrandName,
OUT_BRAND_DECIMALS: '6',
},
},
},
);
await createPriceFeed(
{ consume, produce },
{
options: {
priceFeedOptions: {
AGORIC_INSTANCE_NAME: `${inBrandName}-${outBrandName} price feed`,
contractTerms: {
POLL_INTERVAL: 1n,
},
oracleAddresses: demoOracleAddresses,
IN_BRAND_NAME: inBrandName,
OUT_BRAND_NAME: outBrandName,
},
},
},
);
trace('startPriceFeeds complete');
};
harden(startPriceFeeds);
export const PRICE_FEEDS_MANIFEST = harden({
[startPriceFeeds.name]: {
consume: {
agoricNamesAdmin: true,
aggregators: true,
board: true,
chainStorage: true,
chainTimerService: true,
client: true,
namesByAddressAdmin: true,
priceAuthority: true,
priceAuthorityAdmin: true,
zoe: true,
},
produce: { aggregators: true },
installation: { consume: { priceAggregator: true } },
},
});