-
Notifications
You must be signed in to change notification settings - Fork 251
Expand file tree
/
Copy pathutils.js
More file actions
200 lines (183 loc) · 5.43 KB
/
utils.js
File metadata and controls
200 lines (183 loc) · 5.43 KB
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
import { Fail } from '@endo/errors';
import { E } from '@endo/far';
import { WalletName } from '@agoric/internal';
import { getCopyMapEntries, makeCopyMap } from '@agoric/store';
import { assertPathSegment } from '@agoric/internal/src/lib-chainStorage.js';
import { makeScalarBigMapStore } from '@agoric/vat-data';
/**
* @import {CopyMap} from '@endo/patterns';
* @import {MapStore, SetStore} from '@agoric/store';
* @import {NameAdmin, Producer} from '@agoric/vats';
* @import {ScratchPad} from '@agoric/internal/src/scratch.js';
* @import {Bundle} from '@agoric/swingset-vat';
*/
/**
* @param {ERef<NameAdmin>} nameAdmin
* @param {string[][]} paths
*/
export const reserveThenGetNamePaths = async (nameAdmin, paths) => {
/**
* @param {ERef<NameAdmin>} nextAdmin
* @param {string[]} path
* @returns {Promise<unknown>}
*/
const nextPath = async (nextAdmin, path) => {
const [nextName, ...rest] = path;
assert.typeof(nextName, 'string');
// Ensure we wait for the next name until it exists.
await E(nextAdmin).reserve(nextName);
if (rest.length === 0) {
// Now return the readonly lookup of the name.
const nameHub = E(nextAdmin).readonly();
return E(nameHub).lookup(nextName);
}
// Wait until the next admin is resolved.
const restAdmin = await E(nextAdmin).lookupAdmin(nextName);
return nextPath(restAdmin, rest);
};
return Promise.all(
paths.map(async path => {
Array.isArray(path) || Fail`path ${path} is not an array`;
return nextPath(nameAdmin, path);
}),
);
};
/**
* @param {ERef<NameAdmin>} nameAdmin
* @param {string[]} names
* @returns {Promise<any[]>}
*/
export const reserveThenGetNames = async (nameAdmin, names) =>
reserveThenGetNamePaths(
nameAdmin,
names.map(name => [name]),
);
/**
* @param {string} debugName
* @param {ERef<NameAdmin>} namesByAddressAdmin
* @param {string} addr
* @param {ERef<Payment>[]} payments
*/
export const reserveThenDeposit = async (
debugName,
namesByAddressAdmin,
addr,
payments,
) => {
console.info('awaiting depositFacet for', debugName);
/** @type {any} */
const [depositFacet] = await reserveThenGetNamePaths(namesByAddressAdmin, [
[addr, WalletName.depositFacet],
]);
console.info('depositing to', debugName);
await Promise.allSettled(
payments.map(async (paymentP, i) => {
const payment = await paymentP;
await E(depositFacet).receive(payment);
console.info(
`confirmed deposit ${i + 1}/${payments.length} for`,
debugName,
);
}),
);
};
/**
* @type {<T>(
* store: ERef<Map<string, T> | ScratchPad>,
* key: string,
* make: () => T,
* ) => Promise<T>}
*/
const provideWhen = async (store, key, make) => {
const found = await E(store).get(key);
if (found) {
return found;
}
const value = make();
await E(store).set(key, value);
return value;
};
/**
* @param {Promise<{
* scratch: ERef<ScratchPad>;
* }>} homeP
* @param {object} opts
* @param {(specifier: string) => Promise<{ default: Bundle }>} opts.loadBundle
* @param {string} [opts.installCacheKey]
*/
export const makeInstallCache = async (
homeP,
{ installCacheKey = 'installCache', loadBundle },
) => {
/**
* @type {CopyMap<
* string,
* { installation: Installation; boardId: string; path?: string }
* >}
*/
const initial = await provideWhen(E.get(homeP).scratch, installCacheKey, () =>
makeCopyMap([]),
);
// ISSUE: getCopyMapEntries of CopyMap<K, V> loses K, V.
/**
* @type {Map<
* string,
* { installation: Installation; boardId: string; path?: string }
* >}
*/
const working = new Map(getCopyMapEntries(initial));
const saveCache = async () => {
const final = makeCopyMap(working);
assert.equal(final.payload.keys.length, working.size);
await E(E.get(homeP).scratch).set(installCacheKey, final);
console.log({
initial: initial.payload.keys.length,
total: working.size,
});
};
const wrapInstall = install => async (mPath, bPath, opts) => {
const bundle = await loadBundle(bPath).then(m => m.default);
assert(
'endoZipBase64Sha512' in bundle,
'bundle must be EndoZipBase64Bundle',
);
const { endoZipBase64Sha512: sha512 } = bundle;
const detail = await provideWhen(working, sha512, () =>
install(mPath, bPath, opts).then(installation => ({
installation,
sha512,
path: bPath,
})),
);
return detail.installation;
};
return { wrapInstall, saveCache };
};
export const oracleBrandFeedName = (inBrandName, outBrandName) =>
`${inBrandName}-${outBrandName} price feed`;
export const scaledPriceFeedName = issuerName =>
`scaledPriceAuthority-${issuerName}`;
/** @type {(name: string) => string} */
export const sanitizePathSegment = name => {
const candidate = name.replace(/ /g, '_');
assertPathSegment(candidate);
return candidate;
};
/**
* Idempotently provide an empty MapStore for the `retiredContractInstances`
* value in promise space
*
* @param {Promise<MapStore>} consume
* @param {Producer<MapStore>} produce
* @returns {Promise<MapStore>}
*/
export const provideRetiredInstances = async (consume, produce) => {
// Promise space has no way to look for an existing value other than awaiting a promise,
// but it does allow extra production so it's safe to do this redundantly.
produce.resolve(
makeScalarBigMapStore('retiredContractInstances', {
durable: true,
}),
);
return consume;
};