@evefrontier/dapp-kit 0.1.8 → 0.2.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +28 -25
- package/dist/config/dapp-kit.d.ts +4 -0
- package/dist/config/dapp-kit.d.ts.map +1 -0
- package/dist/config/dapp-kit.js +14 -0
- package/dist/config/index.d.ts +2 -0
- package/dist/config/index.d.ts.map +1 -0
- package/dist/config/index.js +1 -0
- package/dist/graphql/client.d.ts +277 -0
- package/dist/graphql/client.d.ts.map +1 -0
- package/dist/graphql/client.js +346 -0
- package/dist/graphql/index.d.ts +4 -0
- package/dist/graphql/index.d.ts.map +1 -0
- package/dist/graphql/index.js +16 -0
- package/dist/graphql/queries.d.ts +103 -0
- package/dist/graphql/queries.d.ts.map +1 -0
- package/{graphql/queries.ts → dist/graphql/queries.js} +113 -223
- package/dist/graphql/types.d.ts +396 -0
- package/dist/graphql/types.d.ts.map +1 -0
- package/dist/graphql/types.js +4 -0
- package/dist/hooks/index.d.ts +10 -0
- package/dist/hooks/index.d.ts.map +1 -0
- package/dist/hooks/index.js +8 -0
- package/dist/hooks/useCharacterDirectory.d.ts +16 -0
- package/dist/hooks/useCharacterDirectory.d.ts.map +1 -0
- package/dist/hooks/useCharacterDirectory.js +23 -0
- package/dist/hooks/useCharacterSearch.d.ts +24 -0
- package/dist/hooks/useCharacterSearch.d.ts.map +1 -0
- package/dist/hooks/useCharacterSearch.js +24 -0
- package/dist/hooks/useConnection.d.ts +51 -0
- package/dist/hooks/useConnection.d.ts.map +1 -0
- package/{hooks/useConnection.ts → dist/hooks/useConnection.js} +8 -10
- package/dist/hooks/useEntityRole.d.ts +26 -0
- package/dist/hooks/useEntityRole.d.ts.map +1 -0
- package/dist/hooks/useEntityRole.js +91 -0
- package/dist/hooks/useNotification.d.ts +72 -0
- package/dist/hooks/useNotification.d.ts.map +1 -0
- package/{hooks/useNotification.ts → dist/hooks/useNotification.js} +8 -12
- package/dist/hooks/useSmartObject.d.ts +44 -0
- package/dist/hooks/useSmartObject.d.ts.map +1 -0
- package/dist/hooks/useSmartObject.js +50 -0
- package/dist/hooks/useSponsoredTransaction.d.ts +147 -0
- package/dist/hooks/useSponsoredTransaction.d.ts.map +1 -0
- package/dist/hooks/useSponsoredTransaction.js +240 -0
- package/dist/hooks/useTypeNames.d.ts +11 -0
- package/dist/hooks/useTypeNames.d.ts.map +1 -0
- package/dist/hooks/useTypeNames.js +24 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +29 -0
- package/dist/providers/EveFrontierProvider.d.ts +23 -0
- package/dist/providers/EveFrontierProvider.d.ts.map +1 -0
- package/dist/providers/EveFrontierProvider.js +23 -0
- package/dist/providers/NotificationProvider.d.ts +14 -0
- package/dist/providers/NotificationProvider.d.ts.map +1 -0
- package/dist/providers/NotificationProvider.js +66 -0
- package/dist/providers/SmartObjectProvider.d.ts +31 -0
- package/dist/providers/SmartObjectProvider.d.ts.map +1 -0
- package/dist/providers/SmartObjectProvider.js +80 -0
- package/dist/providers/VaultProvider.d.ts +10 -0
- package/dist/providers/VaultProvider.d.ts.map +1 -0
- package/dist/providers/VaultProvider.js +79 -0
- package/dist/providers/index.d.ts +5 -0
- package/dist/providers/index.d.ts.map +1 -0
- package/dist/providers/index.js +4 -0
- package/dist/providers/smartObject/useEntityData.d.ts +29 -0
- package/dist/providers/smartObject/useEntityData.d.ts.map +1 -0
- package/dist/providers/smartObject/useEntityData.js +135 -0
- package/dist/providers/smartObject/useEntitySubscription.d.ts +14 -0
- package/dist/providers/smartObject/useEntitySubscription.d.ts.map +1 -0
- package/dist/providers/smartObject/useEntitySubscription.js +58 -0
- package/dist/providers/smartObject/useObjectSelection.d.ts +11 -0
- package/dist/providers/smartObject/useObjectSelection.d.ts.map +1 -0
- package/dist/providers/smartObject/useObjectSelection.js +27 -0
- package/dist/providers/smartObject/usePollingBackstop.d.ts +18 -0
- package/dist/providers/smartObject/usePollingBackstop.d.ts.map +1 -0
- package/dist/providers/smartObject/usePollingBackstop.js +38 -0
- package/dist/types/character.d.ts +21 -0
- package/dist/types/character.d.ts.map +1 -0
- package/dist/types/character.js +1 -0
- package/dist/types/contexts.d.ts +61 -0
- package/dist/types/contexts.d.ts.map +1 -0
- package/dist/types/contexts.js +6 -0
- package/dist/types/entity.d.ts +245 -0
- package/dist/types/entity.d.ts.map +1 -0
- package/dist/types/entity.js +6 -0
- package/dist/types/index.d.ts +6 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +5 -0
- package/dist/types/sponsoredTransaction.d.ts +26 -0
- package/dist/types/sponsoredTransaction.d.ts.map +1 -0
- package/dist/types/sponsoredTransaction.js +10 -0
- package/dist/types/types.d.ts +110 -0
- package/dist/types/types.d.ts.map +1 -0
- package/dist/types/types.js +41 -0
- package/dist/types/worldApiReturnTypes.d.ts +68 -0
- package/dist/types/worldApiReturnTypes.d.ts.map +1 -0
- package/dist/types/worldApiReturnTypes.js +1 -0
- package/{utils/burnRate.ts → dist/utils/burnRate.d.ts} +4 -29
- package/dist/utils/burnRate.d.ts.map +1 -0
- package/dist/utils/burnRate.js +25 -0
- package/dist/utils/characters.d.ts +26 -0
- package/dist/utils/characters.d.ts.map +1 -0
- package/dist/utils/characters.js +84 -0
- package/dist/utils/constants.d.ts +153 -0
- package/dist/utils/constants.d.ts.map +1 -0
- package/dist/utils/constants.js +190 -0
- package/dist/utils/datahub.d.ts +40 -0
- package/dist/utils/datahub.d.ts.map +1 -0
- package/{utils/datahub.ts → dist/utils/datahub.js} +32 -25
- package/dist/utils/entity.d.ts +58 -0
- package/dist/utils/entity.d.ts.map +1 -0
- package/dist/utils/entity.js +386 -0
- package/dist/utils/errors.d.ts +18 -0
- package/dist/utils/errors.d.ts.map +1 -0
- package/dist/utils/errors.js +128 -0
- package/dist/utils/events/entityEvents.d.ts +44 -0
- package/dist/utils/events/entityEvents.d.ts.map +1 -0
- package/dist/utils/events/entityEvents.js +95 -0
- package/dist/utils/events/entitySubscription.d.ts +30 -0
- package/dist/utils/events/entitySubscription.d.ts.map +1 -0
- package/dist/utils/events/entitySubscription.js +84 -0
- package/dist/utils/events/graphqlEventStream.d.ts +90 -0
- package/dist/utils/events/graphqlEventStream.d.ts.map +1 -0
- package/dist/utils/events/graphqlEventStream.js +198 -0
- package/dist/utils/events/refetchScheduler.d.ts +30 -0
- package/dist/utils/events/refetchScheduler.d.ts.map +1 -0
- package/dist/utils/events/refetchScheduler.js +57 -0
- package/dist/utils/events/sseClient.d.ts +42 -0
- package/dist/utils/events/sseClient.d.ts.map +1 -0
- package/dist/utils/events/sseClient.js +97 -0
- package/dist/utils/fuel.d.ts +231 -0
- package/dist/utils/fuel.d.ts.map +1 -0
- package/dist/utils/fuel.js +652 -0
- package/dist/utils/grid.d.ts +115 -0
- package/dist/utils/grid.d.ts.map +1 -0
- package/dist/utils/grid.js +115 -0
- package/dist/utils/index.d.ts +15 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +22 -0
- package/dist/utils/inventory.d.ts +3 -0
- package/dist/utils/inventory.d.ts.map +1 -0
- package/dist/utils/inventory.js +12 -0
- package/dist/utils/logger.d.ts +25 -0
- package/dist/utils/logger.d.ts.map +1 -0
- package/dist/utils/logger.js +228 -0
- package/dist/utils/mapping.d.ts +30 -0
- package/dist/utils/mapping.d.ts.map +1 -0
- package/dist/utils/mapping.js +73 -0
- package/dist/utils/mvr/worldTypes.d.ts +35 -0
- package/dist/utils/mvr/worldTypes.d.ts.map +1 -0
- package/dist/utils/mvr/worldTypes.js +54 -0
- package/dist/utils/role.d.ts +116 -0
- package/dist/utils/role.d.ts.map +1 -0
- package/dist/utils/role.js +120 -0
- package/dist/utils/smartObject.d.ts +24 -0
- package/dist/utils/smartObject.d.ts.map +1 -0
- package/dist/utils/smartObject.js +24 -0
- package/dist/utils/utils.d.ts +133 -0
- package/dist/utils/utils.d.ts.map +1 -0
- package/dist/utils/utils.js +213 -0
- package/dist/wallet/features.d.ts +28 -0
- package/dist/wallet/features.d.ts.map +1 -0
- package/dist/wallet/features.js +22 -0
- package/dist/wallet/index.d.ts +9 -0
- package/dist/wallet/index.d.ts.map +1 -0
- package/dist/wallet/index.js +9 -0
- package/dist/wallet/utils.d.ts +71 -0
- package/dist/wallet/utils.d.ts.map +1 -0
- package/dist/wallet/utils.js +79 -0
- package/package.json +69 -35
- package/config/dapp-kit.ts +0 -20
- package/config/index.ts +0 -1
- package/graphql/client.ts +0 -578
- package/graphql/index.ts +0 -39
- package/graphql/types.ts +0 -468
- package/hooks/index.ts +0 -15
- package/hooks/useSmartObject.ts +0 -66
- package/hooks/useSponsoredTransaction.ts +0 -442
- package/index.ts +0 -88
- package/providers/EveFrontierProvider.tsx +0 -41
- package/providers/NotificationProvider.tsx +0 -108
- package/providers/SmartObjectProvider.tsx +0 -296
- package/providers/VaultProvider.tsx +0 -108
- package/providers/index.ts +0 -10
- package/types/contexts.ts +0 -67
- package/types/global.d.ts +0 -7
- package/types/index.ts +0 -4
- package/types/sponsoredTransaction.ts +0 -70
- package/types/types.ts +0 -198
- package/types/worldApiReturnTypes.ts +0 -144
- package/utils/__tests__/burnRate.test.ts +0 -121
- package/utils/__tests__/character.test.ts +0 -112
- package/utils/__tests__/characterOwnedObjects.test.ts +0 -225
- package/utils/__tests__/config.test.ts +0 -424
- package/utils/__tests__/datahub.test.ts +0 -58
- package/utils/__tests__/logger.test.ts +0 -145
- package/utils/__tests__/mapping.test.ts +0 -439
- package/utils/__tests__/utils.test.ts +0 -299
- package/utils/character.ts +0 -46
- package/utils/characterOwnedObjects.ts +0 -53
- package/utils/config.ts +0 -151
- package/utils/constants.ts +0 -254
- package/utils/errors.ts +0 -171
- package/utils/index.ts +0 -47
- package/utils/logger.ts +0 -300
- package/utils/mapping.ts +0 -118
- package/utils/transforms.ts +0 -263
- package/utils/utils.ts +0 -270
- package/wallet/features.ts +0 -136
- package/wallet/index.ts +0 -26
- package/wallet/utils.ts +0 -117
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
import { getEntity, getObjectOwner, getTableEntries } from '../graphql/client.js';
|
|
2
|
+
import { projectGrid } from './fuel.js';
|
|
3
|
+
import { createLogger } from './logger.js';
|
|
4
|
+
const log = createLogger();
|
|
5
|
+
const ENTITY_TYPE = /::entity::Entity$/;
|
|
6
|
+
const COMPONENT_INNER = /::component::Component<0x[0-9a-fA-F]+::(\w+)::(\w+)/;
|
|
7
|
+
function throwOnErrors(response) {
|
|
8
|
+
if (response.errors?.length) {
|
|
9
|
+
throw new Error(response.errors.map((e) => e.message).join('; '));
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
/** Reads a nested field such as `inner.sections.id` from component JSON. */
|
|
13
|
+
function readPath(json, path) {
|
|
14
|
+
return path
|
|
15
|
+
.split('.')
|
|
16
|
+
.reduce((value, key) => value?.[key], json);
|
|
17
|
+
}
|
|
18
|
+
/** Pages in the rest of a nested table once its inline first page is full. */
|
|
19
|
+
async function completeTable(ref, tableId) {
|
|
20
|
+
const page = ref?.asAddress?.addressAt?.dynamicFields;
|
|
21
|
+
if (!page)
|
|
22
|
+
return null;
|
|
23
|
+
const entries = [...page.nodes];
|
|
24
|
+
let after = page.pageInfo.hasNextPage ? page.pageInfo.endCursor : null;
|
|
25
|
+
if (!after)
|
|
26
|
+
return entries;
|
|
27
|
+
if (typeof tableId !== 'string') {
|
|
28
|
+
// Paging needs the table id; stopping here would return partial entries
|
|
29
|
+
throw new Error('Nested table has more pages but no table id in its JSON');
|
|
30
|
+
}
|
|
31
|
+
while (after) {
|
|
32
|
+
const response = await getTableEntries(tableId, { after });
|
|
33
|
+
throwOnErrors(response);
|
|
34
|
+
const next = response.data?.address?.dynamicFields;
|
|
35
|
+
entries.push(...(next?.nodes ?? []));
|
|
36
|
+
after = next?.pageInfo.hasNextPage ? next.pageInfo.endCursor : null;
|
|
37
|
+
}
|
|
38
|
+
return entries;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Fetches an object as a world entity, paging through all of its dynamic
|
|
42
|
+
* fields and any layout section or inventory item tables.
|
|
43
|
+
*
|
|
44
|
+
* Pages after the first are read at the latest version rather than pinned to
|
|
45
|
+
* `version`, so an entity that changes mid-fetch can mix fields from two
|
|
46
|
+
* versions. Polling callers converge on the next fetch, as the newer version
|
|
47
|
+
* no longer matches.
|
|
48
|
+
*
|
|
49
|
+
* @category Utilities - Entity
|
|
50
|
+
* @param objectId - The Sui object ID
|
|
51
|
+
* @returns The raw entity, or null if there is no Move object at that address
|
|
52
|
+
* @throws If a GraphQL response has errors, or a nested table cannot be paged
|
|
53
|
+
*/
|
|
54
|
+
export async function fetchEntity(objectId) {
|
|
55
|
+
let raw = null;
|
|
56
|
+
let cursor;
|
|
57
|
+
do {
|
|
58
|
+
const response = await getEntity(objectId, cursor ? { cursor } : {});
|
|
59
|
+
throwOnErrors(response);
|
|
60
|
+
const object = response.data?.object;
|
|
61
|
+
if (!object?.asMoveObject)
|
|
62
|
+
return null;
|
|
63
|
+
raw ?? (raw = {
|
|
64
|
+
version: response.data?.latestVersion.nodes[0]?.version ?? 0,
|
|
65
|
+
contents: object.asMoveObject.contents,
|
|
66
|
+
fields: [],
|
|
67
|
+
});
|
|
68
|
+
for (const { contents } of object.dynamicFields.nodes) {
|
|
69
|
+
const { sections, items, ...value } = contents.value;
|
|
70
|
+
const table = sections
|
|
71
|
+
? await completeTable(sections, readPath(value.json, 'inner.sections.id'))
|
|
72
|
+
: await completeTable(items, readPath(value.json, 'inner.items.balances.id'));
|
|
73
|
+
raw.fields.push({ name: contents.name, value, table });
|
|
74
|
+
}
|
|
75
|
+
const { pageInfo } = object.dynamicFields;
|
|
76
|
+
cursor = pageInfo.hasNextPage
|
|
77
|
+
? (pageInfo.endCursor ?? undefined)
|
|
78
|
+
: undefined;
|
|
79
|
+
} while (cursor);
|
|
80
|
+
return raw;
|
|
81
|
+
}
|
|
82
|
+
/** True when the raw object is a `core::entity::Entity`. @category Utilities - Entity */
|
|
83
|
+
export function isEntity(raw) {
|
|
84
|
+
return ENTITY_TYPE.test(raw.contents.type.repr);
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* The draw kind, checked rather than cast. An unknown variant (say, from a
|
|
88
|
+
* contract upgrade) is treated as Firm, the stricter rule, and logged.
|
|
89
|
+
*/
|
|
90
|
+
function parseDrawKind(variant, moduleId) {
|
|
91
|
+
if (variant === 'Firm' || variant === 'Elastic')
|
|
92
|
+
return variant;
|
|
93
|
+
log.warn(`[DappKit] Unknown draw kind "${variant}" on module ${moduleId}; treating it as Firm`);
|
|
94
|
+
return 'Firm';
|
|
95
|
+
}
|
|
96
|
+
function parseModules(json) {
|
|
97
|
+
const modules = {};
|
|
98
|
+
for (const { key, value } of json.contents) {
|
|
99
|
+
modules[key] = {
|
|
100
|
+
desiredOnline: value.desired_online,
|
|
101
|
+
activeDraw: value.active_draw_at_last_settled,
|
|
102
|
+
powerReq: value.module_spec.power_req,
|
|
103
|
+
drawKind: parseDrawKind(value.module_spec.draw_kind['@variant'], key),
|
|
104
|
+
priority: String(value.module_spec.priority),
|
|
105
|
+
lineLoss: String(value.module_spec.line_loss),
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
return modules;
|
|
109
|
+
}
|
|
110
|
+
function parsePoolsGrid(raw) {
|
|
111
|
+
const generators = {};
|
|
112
|
+
for (const { key, value } of raw.generators?.contents ?? []) {
|
|
113
|
+
generators[key] = {
|
|
114
|
+
online: value.online,
|
|
115
|
+
maxOutputMw: value.max_output_mw,
|
|
116
|
+
containmentReduction: '0',
|
|
117
|
+
baseFuelRate: '0',
|
|
118
|
+
fuelType: String(value.fuel_type),
|
|
119
|
+
impulseLimit: value.impulse_limit,
|
|
120
|
+
volatilityLimit: value.volatility_limit,
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
const bays = {};
|
|
124
|
+
const fuelSources = {};
|
|
125
|
+
let quantity = 0n;
|
|
126
|
+
let capacity = 0n;
|
|
127
|
+
let impulse = 0n;
|
|
128
|
+
let burden = 0n;
|
|
129
|
+
for (const { key, value } of raw.bays?.contents ?? []) {
|
|
130
|
+
const fuel = value.fuel;
|
|
131
|
+
bays[key] = {
|
|
132
|
+
capacity: value.capacity,
|
|
133
|
+
online: value.online,
|
|
134
|
+
fuel: {
|
|
135
|
+
fuelType: fuel.fuel_type == null ? null : String(fuel.fuel_type),
|
|
136
|
+
quantity: fuel.quantity,
|
|
137
|
+
impulse: fuel.impulse,
|
|
138
|
+
containmentBurden: fuel.containment_burden,
|
|
139
|
+
volatility: fuel.volatility,
|
|
140
|
+
},
|
|
141
|
+
};
|
|
142
|
+
fuelSources[key] = value.capacity;
|
|
143
|
+
if (value.online) {
|
|
144
|
+
// `online_fuel`: online bays, stats weighted by quantity
|
|
145
|
+
const held = BigInt(fuel.quantity);
|
|
146
|
+
quantity += held;
|
|
147
|
+
capacity += BigInt(value.capacity);
|
|
148
|
+
impulse += held * BigInt(fuel.impulse);
|
|
149
|
+
burden += held * BigInt(fuel.containment_burden);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
const grid = {
|
|
153
|
+
model: 'pools',
|
|
154
|
+
on: raw.on_at_last_settled,
|
|
155
|
+
fuelQuantity: quantity.toString(),
|
|
156
|
+
fuelCapacity: capacity.toString(),
|
|
157
|
+
capacityMw: '0',
|
|
158
|
+
lastSettledMs: raw.last_settled_ms,
|
|
159
|
+
fuelImpulse: quantity > 0n ? (impulse / quantity).toString() : '0',
|
|
160
|
+
fuelContainmentBurden: quantity > 0n ? (burden / quantity).toString() : '0',
|
|
161
|
+
modules: parseModules(raw.modules),
|
|
162
|
+
generators,
|
|
163
|
+
fuelSources,
|
|
164
|
+
bays,
|
|
165
|
+
};
|
|
166
|
+
// `capacity_mw`: the generators' output as of the last settle
|
|
167
|
+
grid.capacityMw = projectGrid(grid, Number(raw.last_settled_ms)).capacityMw.toString();
|
|
168
|
+
return grid;
|
|
169
|
+
}
|
|
170
|
+
function parsePowerGrid(raw) {
|
|
171
|
+
if ('on_at_last_settled' in raw)
|
|
172
|
+
return parsePoolsGrid(raw);
|
|
173
|
+
const inner = raw;
|
|
174
|
+
const modules = parseModules(inner.modules);
|
|
175
|
+
const generators = {};
|
|
176
|
+
for (const { key, value } of inner.generators.contents) {
|
|
177
|
+
generators[key] = {
|
|
178
|
+
online: value.online,
|
|
179
|
+
maxOutputMw: value.max_output_mw,
|
|
180
|
+
containmentReduction: value.containment_reduction,
|
|
181
|
+
baseFuelRate: value.base_fuel_rate,
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
const fuelSources = {};
|
|
185
|
+
for (const { key, value } of inner.fuel_sources.contents) {
|
|
186
|
+
fuelSources[key] = value;
|
|
187
|
+
}
|
|
188
|
+
return {
|
|
189
|
+
on: inner.on,
|
|
190
|
+
fuelQuantity: inner.fuel_quantity_at_last_settled,
|
|
191
|
+
fuelCapacity: inner.fuel_capacity,
|
|
192
|
+
capacityMw: inner.capacity_mw,
|
|
193
|
+
lastSettledMs: inner.last_settled_ms,
|
|
194
|
+
fuelImpulse: inner.fuel_impulse,
|
|
195
|
+
fuelContainmentBurden: inner.fuel_containment_burden,
|
|
196
|
+
modules,
|
|
197
|
+
generators,
|
|
198
|
+
fuelSources,
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
function parseActions(json, scope) {
|
|
202
|
+
return json.contents.map(({ key, value }) => ({
|
|
203
|
+
name: key,
|
|
204
|
+
scope,
|
|
205
|
+
requirements: value.requirements.map((requirement) => ({
|
|
206
|
+
typeName: requirement.type_name,
|
|
207
|
+
componentId: requirement.component_id,
|
|
208
|
+
})),
|
|
209
|
+
}));
|
|
210
|
+
}
|
|
211
|
+
function parseLayout(inner, table) {
|
|
212
|
+
const moduleIds = new Map((table ?? []).map((entry) => [
|
|
213
|
+
String(entry.name.json),
|
|
214
|
+
entry.value?.json?.module_ids.contents ?? [],
|
|
215
|
+
]));
|
|
216
|
+
return {
|
|
217
|
+
shellId: inner.shell?.shell_id ?? null,
|
|
218
|
+
// Keep the layout's own section order
|
|
219
|
+
sections: inner.section_ids.contents.map((id) => ({
|
|
220
|
+
id,
|
|
221
|
+
moduleIds: moduleIds.get(id) ?? [],
|
|
222
|
+
})),
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
function parseInventory(component, inner, table) {
|
|
226
|
+
return {
|
|
227
|
+
componentId: component.id,
|
|
228
|
+
name: component.name,
|
|
229
|
+
typeId: inner.type_id,
|
|
230
|
+
capacity: inner.capacity,
|
|
231
|
+
used: inner.used,
|
|
232
|
+
// LinkedTable nodes: { prev, next, value: Balance { quantity, volume } }.
|
|
233
|
+
// Entries without a readable balance are skipped rather than failing the entity.
|
|
234
|
+
items: (table ?? []).flatMap((entry) => {
|
|
235
|
+
const balance = entry.value?.json
|
|
236
|
+
?.value;
|
|
237
|
+
return balance
|
|
238
|
+
? [
|
|
239
|
+
{
|
|
240
|
+
typeId: String(entry.name.json),
|
|
241
|
+
quantity: balance.quantity,
|
|
242
|
+
volume: balance.volume,
|
|
243
|
+
},
|
|
244
|
+
]
|
|
245
|
+
: [];
|
|
246
|
+
}),
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Parses a fetched entity into typed components. Components without a
|
|
251
|
+
* parser still appear in `components`.
|
|
252
|
+
*
|
|
253
|
+
* @category Utilities - Entity
|
|
254
|
+
* @param raw - The entity from {@link fetchEntity}
|
|
255
|
+
* @returns The parsed entity
|
|
256
|
+
*/
|
|
257
|
+
export function parseEntity(raw) {
|
|
258
|
+
const json = raw.contents.json;
|
|
259
|
+
const entity = {
|
|
260
|
+
id: json.id,
|
|
261
|
+
version: raw.version,
|
|
262
|
+
key: { id: json.key.id, tenant: json.key.tenant },
|
|
263
|
+
accessCapId: json.access_cap_id ?? null,
|
|
264
|
+
inFlight: false,
|
|
265
|
+
metadata: null,
|
|
266
|
+
identity: null,
|
|
267
|
+
powerGrid: null,
|
|
268
|
+
layout: null,
|
|
269
|
+
inventories: [],
|
|
270
|
+
genericModules: [],
|
|
271
|
+
controllers: null,
|
|
272
|
+
components: [],
|
|
273
|
+
adminActions: [],
|
|
274
|
+
ownerActions: [],
|
|
275
|
+
actions: [],
|
|
276
|
+
};
|
|
277
|
+
for (const { name, value, table } of raw.fields) {
|
|
278
|
+
const keyType = name.type.repr;
|
|
279
|
+
if (keyType.endsWith('::entity::AdminActionsKey')) {
|
|
280
|
+
const actions = parseActions(value.json, 'admin');
|
|
281
|
+
entity.adminActions = actions.map((action) => action.name);
|
|
282
|
+
entity.actions.push(...actions);
|
|
283
|
+
continue;
|
|
284
|
+
}
|
|
285
|
+
if (keyType.endsWith('::entity::OwnerActionsKey')) {
|
|
286
|
+
const actions = parseActions(value.json, 'owner');
|
|
287
|
+
entity.ownerActions = actions.map((action) => action.name);
|
|
288
|
+
entity.actions.push(...actions);
|
|
289
|
+
continue;
|
|
290
|
+
}
|
|
291
|
+
if (keyType.endsWith('::entity::InFlight')) {
|
|
292
|
+
// The lock is `df::exists(InFlight())`; the stored bool is a placeholder
|
|
293
|
+
entity.inFlight = true;
|
|
294
|
+
continue;
|
|
295
|
+
}
|
|
296
|
+
if (!keyType.endsWith('::entity::ComponentKey'))
|
|
297
|
+
continue;
|
|
298
|
+
const match = COMPONENT_INNER.exec(value.type.repr);
|
|
299
|
+
const wrapper = value.json;
|
|
300
|
+
const component = {
|
|
301
|
+
id: name.json.pos0,
|
|
302
|
+
name: wrapper.name,
|
|
303
|
+
kind: match ? `${match[1]}::${match[2]}` : value.type.repr,
|
|
304
|
+
type: value.type.repr,
|
|
305
|
+
json: wrapper.inner,
|
|
306
|
+
};
|
|
307
|
+
entity.components.push(component);
|
|
308
|
+
const inner = wrapper.inner;
|
|
309
|
+
switch (component.kind) {
|
|
310
|
+
case 'metadata::Metadata': {
|
|
311
|
+
const { name, description, url } = inner;
|
|
312
|
+
entity.metadata = { name, description, url };
|
|
313
|
+
break;
|
|
314
|
+
}
|
|
315
|
+
case 'identity::Identity': {
|
|
316
|
+
const { tribe_id, owner } = inner;
|
|
317
|
+
entity.identity = { tribeId: Number(tribe_id), owner };
|
|
318
|
+
break;
|
|
319
|
+
}
|
|
320
|
+
case 'power_grid::PowerGrid':
|
|
321
|
+
entity.powerGrid = parsePowerGrid(inner);
|
|
322
|
+
break;
|
|
323
|
+
case 'layout::Layout':
|
|
324
|
+
entity.layout = parseLayout(inner, table);
|
|
325
|
+
break;
|
|
326
|
+
case 'inventory::Inventory':
|
|
327
|
+
entity.inventories.push(parseInventory(component, inner, table));
|
|
328
|
+
break;
|
|
329
|
+
case 'generic_module::GenericModule':
|
|
330
|
+
entity.genericModules.push({
|
|
331
|
+
componentId: component.id,
|
|
332
|
+
name: component.name,
|
|
333
|
+
typeId: inner.type_id,
|
|
334
|
+
});
|
|
335
|
+
break;
|
|
336
|
+
case 'access_list::AccessList':
|
|
337
|
+
entity.controllers = inner.controllers.contents;
|
|
338
|
+
break;
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
return entity;
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
* Resolves who controls an entity from the holder of its AccessCap. A cap
|
|
345
|
+
* parked on a Character is held by the Character's object id; its wallet is
|
|
346
|
+
* the Character's `Identity.owner`. See {@link EntityOwner} `kind` for the
|
|
347
|
+
* cases.
|
|
348
|
+
*
|
|
349
|
+
* @category Utilities - Entity
|
|
350
|
+
* @param accessCapId - The entity's `accessCapId`
|
|
351
|
+
* @param previous - The last owner resolved for this cap; returned as-is when
|
|
352
|
+
* the holder hasn't changed, skipping the Character fetch
|
|
353
|
+
* @returns The holder, its wallet, and the Character entity when there is one
|
|
354
|
+
* @throws If a GraphQL response has errors
|
|
355
|
+
*/
|
|
356
|
+
export async function fetchEntityOwner(accessCapId, previous) {
|
|
357
|
+
const response = await getObjectOwner(accessCapId);
|
|
358
|
+
throwOnErrors(response);
|
|
359
|
+
const owner = response.data?.object?.owner;
|
|
360
|
+
const holder = owner && 'address' in owner ? owner.address.address : null;
|
|
361
|
+
if (!owner || !holder) {
|
|
362
|
+
return { kind: 'none', holder: null, walletAddress: null, character: null };
|
|
363
|
+
}
|
|
364
|
+
// An ObjectOwner holder is a dynamic-field wrapper, not the Character
|
|
365
|
+
if (owner.__typename === 'ObjectOwner') {
|
|
366
|
+
return { kind: 'object', holder, walletAddress: null, character: null };
|
|
367
|
+
}
|
|
368
|
+
if (previous && previous.kind !== 'object' && previous.holder === holder) {
|
|
369
|
+
return previous;
|
|
370
|
+
}
|
|
371
|
+
// A wallet has no object at its address; an object holder should be a Character
|
|
372
|
+
const raw = await fetchEntity(holder);
|
|
373
|
+
if (!raw) {
|
|
374
|
+
return { kind: 'wallet', holder, walletAddress: holder, character: null };
|
|
375
|
+
}
|
|
376
|
+
if (!isEntity(raw)) {
|
|
377
|
+
return { kind: 'object', holder, walletAddress: null, character: null };
|
|
378
|
+
}
|
|
379
|
+
const character = parseEntity(raw);
|
|
380
|
+
return {
|
|
381
|
+
kind: 'character',
|
|
382
|
+
holder,
|
|
383
|
+
walletAddress: character.identity?.owner ?? null,
|
|
384
|
+
character,
|
|
385
|
+
};
|
|
386
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/** @category Types */
|
|
2
|
+
export type ErrorType = {
|
|
3
|
+
code: number;
|
|
4
|
+
name: string;
|
|
5
|
+
patterns: string[];
|
|
6
|
+
message: string;
|
|
7
|
+
};
|
|
8
|
+
/** @category Utilities - Error helpers */
|
|
9
|
+
export declare const ERRORS: Record<number | string, ErrorType>;
|
|
10
|
+
/** @category Constants */
|
|
11
|
+
export declare const ERROR_MESSAGES: Record<number, string>;
|
|
12
|
+
/** @category Utilities - Error helpers */
|
|
13
|
+
export declare const parseErrorFromMessage: (errorMessage: string) => {
|
|
14
|
+
code: number;
|
|
15
|
+
name: string;
|
|
16
|
+
patterns: string[];
|
|
17
|
+
};
|
|
18
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../utils/errors.ts"],"names":[],"mappings":"AAAA,sBAAsB;AACtB,MAAM,MAAM,SAAS,GAAG;IACtB,IAAI,EAAE,MAAM,CAAA;IACZ,IAAI,EAAE,MAAM,CAAA;IACZ,QAAQ,EAAE,MAAM,EAAE,CAAA;IAClB,OAAO,EAAE,MAAM,CAAA;CAChB,CAAA;AAoGD,0CAA0C;AAC1C,eAAO,MAAM,MAAM,EAAE,MAAM,CAAC,MAAM,GAAG,MAAM,EAAE,SAAS,CAMrD,CAAA;AAED,0BAA0B;AAC1B,eAAO,MAAM,cAAc,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAIjD,CAAA;AAED,0CAA0C;AAC1C,eAAO,MAAM,qBAAqB,iBAClB,MAAM,KACnB;IACD,IAAI,EAAE,MAAM,CAAA;IACZ,IAAI,EAAE,MAAM,CAAA;IACZ,QAAQ,EAAE,MAAM,EAAE,CAAA;CAkBnB,CAAA"}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/** @category Utilities - Error helpers */
|
|
2
|
+
const NUMERIC_ERRORS = {
|
|
3
|
+
1001: {
|
|
4
|
+
code: 1001,
|
|
5
|
+
name: 'Unknown Error',
|
|
6
|
+
patterns: ['unknown error'],
|
|
7
|
+
message: 'An unknown error occurred.',
|
|
8
|
+
},
|
|
9
|
+
1002: {
|
|
10
|
+
code: 1002,
|
|
11
|
+
name: 'Network Error',
|
|
12
|
+
patterns: ['network error', 'connection failed', 'failed to fetch'],
|
|
13
|
+
message: '',
|
|
14
|
+
},
|
|
15
|
+
1003: {
|
|
16
|
+
code: 1003,
|
|
17
|
+
name: 'Invalid Input Error',
|
|
18
|
+
patterns: ['invalid input', 'bad request', 'malformed'],
|
|
19
|
+
message: '',
|
|
20
|
+
},
|
|
21
|
+
2001: {
|
|
22
|
+
code: 2001,
|
|
23
|
+
name: 'Contract Call Error',
|
|
24
|
+
patterns: ['contract call error', 'execution reverted', 'out of gas'],
|
|
25
|
+
message: 'Error calling the smart contract.',
|
|
26
|
+
},
|
|
27
|
+
2002: {
|
|
28
|
+
code: 2002,
|
|
29
|
+
name: 'Contract Deployment Error',
|
|
30
|
+
patterns: ['contract deployment error', 'contract creation failed'],
|
|
31
|
+
message: '',
|
|
32
|
+
},
|
|
33
|
+
2003: {
|
|
34
|
+
code: 2003,
|
|
35
|
+
name: 'World Resource Not Found Error',
|
|
36
|
+
patterns: ['Error: World_ResourceNotFound'],
|
|
37
|
+
message: 'World resource not found.',
|
|
38
|
+
},
|
|
39
|
+
2008: {
|
|
40
|
+
code: 2008,
|
|
41
|
+
name: 'Contract Revert Error',
|
|
42
|
+
patterns: ['contract revert', 'reverted'],
|
|
43
|
+
message: 'Function simulation reverted',
|
|
44
|
+
},
|
|
45
|
+
3001: {
|
|
46
|
+
code: 3001,
|
|
47
|
+
name: 'Insufficient Gas',
|
|
48
|
+
patterns: ['insufficient gas', 'not enough gas', 'balance too low'],
|
|
49
|
+
message: 'Insufficient funds for GAS.',
|
|
50
|
+
},
|
|
51
|
+
3002: {
|
|
52
|
+
code: 3002,
|
|
53
|
+
name: 'Insufficient Eve',
|
|
54
|
+
patterns: ['insufficient EVE', 'not enough EVE'],
|
|
55
|
+
message: 'Insufficient EVE.',
|
|
56
|
+
},
|
|
57
|
+
3003: {
|
|
58
|
+
code: 3003,
|
|
59
|
+
name: 'User Denied Transaction',
|
|
60
|
+
patterns: [
|
|
61
|
+
'User denied transaction signature.',
|
|
62
|
+
'transaction rejected',
|
|
63
|
+
'User rejected the request',
|
|
64
|
+
],
|
|
65
|
+
message: 'User rejected the transaction.',
|
|
66
|
+
},
|
|
67
|
+
3004: {
|
|
68
|
+
code: 3004,
|
|
69
|
+
name: 'Transaction Timeout',
|
|
70
|
+
patterns: ['transaction timeout', 'timeout exceeded'],
|
|
71
|
+
message: '',
|
|
72
|
+
},
|
|
73
|
+
4001: {
|
|
74
|
+
code: 4001,
|
|
75
|
+
name: 'Unauthorized Access',
|
|
76
|
+
patterns: ['unauthorized access', 'permission denied'],
|
|
77
|
+
message: '',
|
|
78
|
+
},
|
|
79
|
+
4002: {
|
|
80
|
+
code: 4002,
|
|
81
|
+
name: 'User Not Logged In',
|
|
82
|
+
patterns: ['user not logged in', 'authentication required'],
|
|
83
|
+
message: '',
|
|
84
|
+
},
|
|
85
|
+
4003: {
|
|
86
|
+
code: 4003,
|
|
87
|
+
name: 'Chain Mismatch',
|
|
88
|
+
patterns: ['does not match the target chain'],
|
|
89
|
+
message: 'Please check the chain your wallet is connected to.',
|
|
90
|
+
},
|
|
91
|
+
5001: {
|
|
92
|
+
code: 5001,
|
|
93
|
+
name: 'Lens Unavailable',
|
|
94
|
+
patterns: ['no lens'],
|
|
95
|
+
message: 'There are no lenses available here',
|
|
96
|
+
},
|
|
97
|
+
};
|
|
98
|
+
/** @category Utilities - Error helpers */
|
|
99
|
+
export const ERRORS = {
|
|
100
|
+
...NUMERIC_ERRORS,
|
|
101
|
+
UNKNOWN_ERROR: NUMERIC_ERRORS[1001],
|
|
102
|
+
CONTRACT_CALL: NUMERIC_ERRORS[2001],
|
|
103
|
+
INSUFFICIENT_EVE: NUMERIC_ERRORS[3002],
|
|
104
|
+
LENS_UNAVAILABLE: NUMERIC_ERRORS[5001],
|
|
105
|
+
};
|
|
106
|
+
/** @category Constants */
|
|
107
|
+
export const ERROR_MESSAGES = Object.fromEntries(Object.entries(ERRORS)
|
|
108
|
+
.filter(([key]) => /^\d+$/.test(key))
|
|
109
|
+
.map(([key, value]) => [Number(key), value.message]));
|
|
110
|
+
/** @category Utilities - Error helpers */
|
|
111
|
+
export const parseErrorFromMessage = (errorMessage) => {
|
|
112
|
+
for (const error of Object.values(ERRORS)) {
|
|
113
|
+
for (const pattern of error.patterns) {
|
|
114
|
+
if (new RegExp(pattern, 'i').test(errorMessage)) {
|
|
115
|
+
return {
|
|
116
|
+
code: +error.code,
|
|
117
|
+
name: error.name,
|
|
118
|
+
patterns: error.patterns,
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return {
|
|
124
|
+
code: 1001,
|
|
125
|
+
name: 'Unknown Error',
|
|
126
|
+
patterns: ['unknown error'],
|
|
127
|
+
};
|
|
128
|
+
};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { EventTypeMatcher, StreamEvent } from './graphqlEventStream.js';
|
|
2
|
+
/**
|
|
3
|
+
* The world v1 events that change an entity's inventories or power grid, as
|
|
4
|
+
* `module::Struct` by world package. Each carries the entity's `entity_id`
|
|
5
|
+
* (`InventoryMigrated` carries `from_entity_id` and `to_entity_id`).
|
|
6
|
+
*
|
|
7
|
+
* @category Utilities - Events
|
|
8
|
+
*/
|
|
9
|
+
export declare const ENTITY_EVENT_NAMES: {
|
|
10
|
+
readonly inventory: readonly ["item::ItemMinted", "item::ItemBurned", "item::ItemDeposited", "item::ItemWithdrawn", "item::ItemsDestroyed", "item::ItemsWithdrawn", "item::ItemsDeposited", "inventory::InventoryInstalled", "inventory::InventoryUninstalled", "inventory::FuelItemDeposited", "inventory::InventoryMigrated"];
|
|
11
|
+
readonly power: readonly ["power_grid::PowerToggled", "power_grid::DrawChanged", "grid_fuel::FuelAdded", "grid_fuel::FuelWithdrawn", "grid_fuel::FuelSiphoned", "grid_fuel::FuelBayToggled", "grid_generator::GeneratorToggled", "grid_load::DesiredStateChanged"];
|
|
12
|
+
};
|
|
13
|
+
type EventPackage = keyof typeof ENTITY_EVENT_NAMES;
|
|
14
|
+
/**
|
|
15
|
+
* The full event types to subscribe to: each name under its world package's
|
|
16
|
+
* id for the tenant's tier. The subscription opens one stream per package.
|
|
17
|
+
*
|
|
18
|
+
* Events are typed by the package version that defined them. An event added
|
|
19
|
+
* in a later upgrade of a package carries that version's id, which the
|
|
20
|
+
* per-package filter does not match; the polling backstop still catches the
|
|
21
|
+
* change.
|
|
22
|
+
*
|
|
23
|
+
* @param tenant - Tenant whose tier to resolve against
|
|
24
|
+
* @param packageId - Resolves a world package's id; `getWorldPackageId` by default
|
|
25
|
+
* @category Utilities - Events
|
|
26
|
+
*/
|
|
27
|
+
export declare function entityEventTypes(tenant?: string, packageId?: (pkg: EventPackage, tenant?: string) => string): string[];
|
|
28
|
+
/**
|
|
29
|
+
* Match delivered types by `::module::Struct`, whatever the package id. The
|
|
30
|
+
* server already filters by package, so this only widens matching to a type
|
|
31
|
+
* from an upgraded version that the filter let through.
|
|
32
|
+
*
|
|
33
|
+
* @category Utilities - Events
|
|
34
|
+
*/
|
|
35
|
+
export declare function entityEventMatcher(eventTypes: readonly string[]): EventTypeMatcher;
|
|
36
|
+
/**
|
|
37
|
+
* Whether an event is about the entity `entityId`: its `entity_id`, or either
|
|
38
|
+
* side of an `InventoryMigrated`.
|
|
39
|
+
*
|
|
40
|
+
* @category Utilities - Events
|
|
41
|
+
*/
|
|
42
|
+
export declare function isEventForEntity(event: StreamEvent, entityId: string): boolean;
|
|
43
|
+
export {};
|
|
44
|
+
//# sourceMappingURL=entityEvents.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"entityEvents.d.ts","sourceRoot":"","sources":["../../../utils/events/entityEvents.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,gBAAgB,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAA;AAEzE;;;;;;GAMG;AACH,eAAO,MAAM,kBAAkB;;;CAwBsC,CAAA;AAErE,KAAK,YAAY,GAAG,MAAM,OAAO,kBAAkB,CAAA;AAEnD;;;;;;;;;;;;GAYG;AACH,wBAAgB,gBAAgB,CAC9B,MAAM,CAAC,EAAE,MAAM,EACf,SAAS,GAAE,CAAC,GAAG,EAAE,YAAY,EAAE,MAAM,CAAC,EAAE,MAAM,KAAK,MAA0B,GAC5E,MAAM,EAAE,CAKV;AASD;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAChC,UAAU,EAAE,SAAS,MAAM,EAAE,GAC5B,gBAAgB,CAGlB;AAWD;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAC9B,KAAK,EAAE,WAAW,EAClB,QAAQ,EAAE,MAAM,GACf,OAAO,CAST"}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { normalizeSuiAddress } from '@mysten/sui/utils';
|
|
2
|
+
import { getWorldPackageId } from '../mvr/worldTypes.js';
|
|
3
|
+
/**
|
|
4
|
+
* The world v1 events that change an entity's inventories or power grid, as
|
|
5
|
+
* `module::Struct` by world package. Each carries the entity's `entity_id`
|
|
6
|
+
* (`InventoryMigrated` carries `from_entity_id` and `to_entity_id`).
|
|
7
|
+
*
|
|
8
|
+
* @category Utilities - Events
|
|
9
|
+
*/
|
|
10
|
+
export const ENTITY_EVENT_NAMES = {
|
|
11
|
+
inventory: [
|
|
12
|
+
'item::ItemMinted',
|
|
13
|
+
'item::ItemBurned',
|
|
14
|
+
'item::ItemDeposited',
|
|
15
|
+
'item::ItemWithdrawn',
|
|
16
|
+
'item::ItemsDestroyed',
|
|
17
|
+
'item::ItemsWithdrawn',
|
|
18
|
+
'item::ItemsDeposited',
|
|
19
|
+
'inventory::InventoryInstalled',
|
|
20
|
+
'inventory::InventoryUninstalled',
|
|
21
|
+
'inventory::FuelItemDeposited',
|
|
22
|
+
'inventory::InventoryMigrated',
|
|
23
|
+
],
|
|
24
|
+
power: [
|
|
25
|
+
'power_grid::PowerToggled',
|
|
26
|
+
'power_grid::DrawChanged',
|
|
27
|
+
'grid_fuel::FuelAdded',
|
|
28
|
+
'grid_fuel::FuelWithdrawn',
|
|
29
|
+
'grid_fuel::FuelSiphoned',
|
|
30
|
+
'grid_fuel::FuelBayToggled',
|
|
31
|
+
'grid_generator::GeneratorToggled',
|
|
32
|
+
'grid_load::DesiredStateChanged',
|
|
33
|
+
],
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* The full event types to subscribe to: each name under its world package's
|
|
37
|
+
* id for the tenant's tier. The subscription opens one stream per package.
|
|
38
|
+
*
|
|
39
|
+
* Events are typed by the package version that defined them. An event added
|
|
40
|
+
* in a later upgrade of a package carries that version's id, which the
|
|
41
|
+
* per-package filter does not match; the polling backstop still catches the
|
|
42
|
+
* change.
|
|
43
|
+
*
|
|
44
|
+
* @param tenant - Tenant whose tier to resolve against
|
|
45
|
+
* @param packageId - Resolves a world package's id; `getWorldPackageId` by default
|
|
46
|
+
* @category Utilities - Events
|
|
47
|
+
*/
|
|
48
|
+
export function entityEventTypes(tenant, packageId = getWorldPackageId) {
|
|
49
|
+
return Object.keys(ENTITY_EVENT_NAMES).flatMap((pkg) => {
|
|
50
|
+
const id = packageId(pkg, tenant);
|
|
51
|
+
return ENTITY_EVENT_NAMES[pkg].map((name) => `${id}::${name}`);
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
/** `::module::Struct` of a type, without its package or type arguments. */
|
|
55
|
+
function typeSuffix(type) {
|
|
56
|
+
const base = type.split('<')[0] ?? type;
|
|
57
|
+
const start = base.indexOf('::');
|
|
58
|
+
return start === -1 ? base : base.slice(start);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Match delivered types by `::module::Struct`, whatever the package id. The
|
|
62
|
+
* server already filters by package, so this only widens matching to a type
|
|
63
|
+
* from an upgraded version that the filter let through.
|
|
64
|
+
*
|
|
65
|
+
* @category Utilities - Events
|
|
66
|
+
*/
|
|
67
|
+
export function entityEventMatcher(eventTypes) {
|
|
68
|
+
const suffixes = new Set(eventTypes.map(typeSuffix));
|
|
69
|
+
return (type) => suffixes.has(typeSuffix(type));
|
|
70
|
+
}
|
|
71
|
+
const sameId = (a, b) => {
|
|
72
|
+
if (typeof a !== 'string' || a === '')
|
|
73
|
+
return false;
|
|
74
|
+
try {
|
|
75
|
+
return normalizeSuiAddress(a) === normalizeSuiAddress(b);
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
/**
|
|
82
|
+
* Whether an event is about the entity `entityId`: its `entity_id`, or either
|
|
83
|
+
* side of an `InventoryMigrated`.
|
|
84
|
+
*
|
|
85
|
+
* @category Utilities - Events
|
|
86
|
+
*/
|
|
87
|
+
export function isEventForEntity(event, entityId) {
|
|
88
|
+
const json = event.parsedJson;
|
|
89
|
+
if (typeof json !== 'object' || json === null)
|
|
90
|
+
return false;
|
|
91
|
+
const fields = json;
|
|
92
|
+
return (sameId(fields['entity_id'], entityId) ||
|
|
93
|
+
sameId(fields['from_entity_id'], entityId) ||
|
|
94
|
+
sameId(fields['to_entity_id'], entityId));
|
|
95
|
+
}
|