@evefrontier/dapp-kit 0.1.7 → 0.1.8
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/graphql/client.ts +9 -6
- package/hooks/useSmartObject.ts +1 -1
- package/hooks/useSponsoredTransaction.ts +47 -4
- package/package.json +14 -13
- package/providers/NotificationProvider.tsx +13 -1
- package/providers/SmartObjectProvider.tsx +25 -12
- package/providers/VaultProvider.tsx +7 -5
- package/types/contexts.ts +14 -5
- package/types/types.ts +39 -0
- package/utils/__tests__/characterOwnedObjects.test.ts +4 -1
- package/utils/__tests__/datahub.test.ts +20 -10
- package/utils/__tests__/logger.test.ts +145 -0
- package/utils/__tests__/mapping.test.ts +9 -9
- package/utils/__tests__/utils.test.ts +299 -0
- package/utils/characterOwnedObjects.ts +4 -3
- package/utils/constants.ts +35 -16
- package/utils/index.ts +4 -4
- package/utils/logger.ts +300 -0
- package/utils/transforms.ts +4 -1
- package/utils/utils.ts +9 -15
package/utils/logger.ts
ADDED
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
CreateLoggerOptions,
|
|
3
|
+
GlobalProcess,
|
|
4
|
+
GlobalWithProcess,
|
|
5
|
+
Logger,
|
|
6
|
+
LoggerFn,
|
|
7
|
+
LogLevel,
|
|
8
|
+
StackFrame,
|
|
9
|
+
} from "../types";
|
|
10
|
+
|
|
11
|
+
const LOG_LEVELS: LogLevel[] = ["silent", "error", "warn", "info", "debug"];
|
|
12
|
+
const LOGGER_SOURCE_HINTS = ["logger.ts", "logger.js", "logger.mjs"];
|
|
13
|
+
|
|
14
|
+
const isLogLevel = (value?: string | null): value is LogLevel => {
|
|
15
|
+
if (!value) {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return LOG_LEVELS.includes(value.toLowerCase() as LogLevel);
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const getProcessObject = (): GlobalProcess | undefined => {
|
|
23
|
+
try {
|
|
24
|
+
return (globalThis as GlobalWithProcess).process;
|
|
25
|
+
} catch {
|
|
26
|
+
return undefined;
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/** Inputs for {@link resolveLogLevelFromEnv} (mirrors Vite `import.meta.env`). */
|
|
31
|
+
export type ResolveLogLevelFromEnvOptions = {
|
|
32
|
+
/** `import.meta.env.VITE_LOG_LEVEL` — when set to a valid level, overrides mode. */
|
|
33
|
+
viteLogLevel?: string | undefined;
|
|
34
|
+
/** `import.meta.env.MODE` — `production` → `error`, `test` → `warn`, else `debug`. */
|
|
35
|
+
viteMode?: string | undefined;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Resolves log level from `VITE_LOG_LEVEL` (if a valid level) and Vite `MODE`.
|
|
40
|
+
* Exported for unit tests; {@link createLogger} uses the snapshot at module load.
|
|
41
|
+
*/
|
|
42
|
+
export function resolveLogLevelFromEnv(
|
|
43
|
+
options: ResolveLogLevelFromEnvOptions,
|
|
44
|
+
): LogLevel {
|
|
45
|
+
const { viteLogLevel, viteMode } = options;
|
|
46
|
+
|
|
47
|
+
if (isLogLevel(viteLogLevel)) {
|
|
48
|
+
return viteLogLevel.toLowerCase() as LogLevel;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (viteMode === "production") {
|
|
52
|
+
return "error";
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (viteMode === "test") {
|
|
56
|
+
return "warn";
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return "debug";
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const resolveEnvLogLevel = (): LogLevel => {
|
|
63
|
+
return resolveLogLevelFromEnv({
|
|
64
|
+
viteLogLevel: import.meta.env.VITE_LOG_LEVEL as string | undefined,
|
|
65
|
+
viteMode: import.meta.env.MODE as string | undefined,
|
|
66
|
+
});
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const normalizeSeparators = (path: string): string => {
|
|
70
|
+
return path.replace(/\\/g, "/");
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const stripOrigin = (path: string): string => {
|
|
74
|
+
if (typeof window !== "undefined" && window.location?.origin) {
|
|
75
|
+
return path.replace(window.location.origin, "");
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return path;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
const stripWorkspaceRoot = (path: string): string => {
|
|
82
|
+
try {
|
|
83
|
+
const globalProcess = getProcessObject();
|
|
84
|
+
if (globalProcess && typeof globalProcess.cwd === "function") {
|
|
85
|
+
const cwd = normalizeSeparators(globalProcess.cwd());
|
|
86
|
+
if (path.startsWith(cwd)) {
|
|
87
|
+
return path.slice(cwd.length);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
} catch {
|
|
91
|
+
// Ignore if process is unavailable
|
|
92
|
+
}
|
|
93
|
+
return path;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
const sanitizeFilePath = (path: string): string => {
|
|
97
|
+
if (!path) {
|
|
98
|
+
return "";
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
let cleaned = normalizeSeparators(path);
|
|
102
|
+
cleaned = cleaned.replace(
|
|
103
|
+
/^webpack-internal:\/\/\/|^vite:\/\/|^file:\/\//,
|
|
104
|
+
"",
|
|
105
|
+
);
|
|
106
|
+
cleaned = stripOrigin(cleaned);
|
|
107
|
+
cleaned = stripWorkspaceRoot(cleaned);
|
|
108
|
+
cleaned = cleaned.split(/[?#]/)[0];
|
|
109
|
+
return cleaned;
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
const parseStackLine = (line: string): StackFrame | undefined => {
|
|
113
|
+
const match = line.match(/(?:\()?(.*?):(\d+):(\d+)\)?$/);
|
|
114
|
+
if (!match) {
|
|
115
|
+
return undefined;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const [, rawPath, lineNumber, columnNumber] = match;
|
|
119
|
+
return {
|
|
120
|
+
filePath: rawPath,
|
|
121
|
+
lineNumber: Number(lineNumber),
|
|
122
|
+
columnNumber: Number(columnNumber),
|
|
123
|
+
};
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
const captureStack = (): string | undefined => {
|
|
127
|
+
try {
|
|
128
|
+
return new Error().stack;
|
|
129
|
+
} catch {
|
|
130
|
+
return undefined;
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
const getCallerFrameFromStack = (stack?: string): StackFrame | undefined => {
|
|
135
|
+
if (!stack) {
|
|
136
|
+
return undefined;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const lines = stack.split("\n").slice(1);
|
|
140
|
+
for (const rawLine of lines) {
|
|
141
|
+
const line = rawLine.trim();
|
|
142
|
+
if (
|
|
143
|
+
!line ||
|
|
144
|
+
LOGGER_SOURCE_HINTS.some((hint) => line.includes(hint)) ||
|
|
145
|
+
line.includes("node:internal")
|
|
146
|
+
) {
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const frame = parseStackLine(line);
|
|
151
|
+
if (frame) {
|
|
152
|
+
return frame;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
return undefined;
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
const deriveScopeFromPath = (filePath?: string): string | undefined => {
|
|
160
|
+
if (!filePath) {
|
|
161
|
+
return undefined;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const cleaned = sanitizeFilePath(filePath);
|
|
165
|
+
const segments = cleaned.split("/").filter(Boolean);
|
|
166
|
+
|
|
167
|
+
if (segments.length === 0) {
|
|
168
|
+
return undefined;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const fileName = segments.pop();
|
|
172
|
+
if (!fileName) {
|
|
173
|
+
return undefined;
|
|
174
|
+
}
|
|
175
|
+
const parent = segments.pop();
|
|
176
|
+
const baseName = fileName.replace(/\.[^.]+$/, "");
|
|
177
|
+
|
|
178
|
+
return parent ? `${parent}/${baseName}` : baseName;
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
const getLocationLabel = (frame?: StackFrame): string | undefined => {
|
|
182
|
+
if (!frame?.filePath) {
|
|
183
|
+
return undefined;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const cleaned = sanitizeFilePath(frame.filePath);
|
|
187
|
+
const parts = cleaned.split("/").filter(Boolean);
|
|
188
|
+
const shortPath =
|
|
189
|
+
parts.length > 2 ? parts.slice(-2).join("/") : parts.join("/") || cleaned;
|
|
190
|
+
|
|
191
|
+
if (!shortPath) {
|
|
192
|
+
return undefined;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
if (frame.lineNumber) {
|
|
196
|
+
return `${shortPath}:${frame.lineNumber}`;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
return shortPath;
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
const resolveLoggerScope = (providedScope?: string): string | undefined => {
|
|
203
|
+
if (providedScope) {
|
|
204
|
+
return providedScope;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const frame = getCallerFrameFromStack(captureStack());
|
|
208
|
+
return deriveScopeFromPath(frame?.filePath);
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
const ENV_LOG_LEVEL = resolveEnvLogLevel();
|
|
212
|
+
|
|
213
|
+
const shouldLog = (
|
|
214
|
+
requestedLevel: LogLevel,
|
|
215
|
+
activeLevel: LogLevel,
|
|
216
|
+
): boolean => {
|
|
217
|
+
return LOG_LEVELS.indexOf(requestedLevel) <= LOG_LEVELS.indexOf(activeLevel);
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Create a logger scoped to the current module. If no scope is provided, the
|
|
222
|
+
* logger automatically derives the filename (plus parent folder) from the call
|
|
223
|
+
* site so logs stay consistent without manual strings.
|
|
224
|
+
*/
|
|
225
|
+
export const createLogger = ({
|
|
226
|
+
scope,
|
|
227
|
+
level = ENV_LOG_LEVEL,
|
|
228
|
+
}: CreateLoggerOptions = {}): Logger => {
|
|
229
|
+
const resolvedScope = resolveLoggerScope(scope);
|
|
230
|
+
|
|
231
|
+
const log =
|
|
232
|
+
(consoleMethod: keyof Console, logLevel: LogLevel): LoggerFn =>
|
|
233
|
+
(...args: unknown[]) => {
|
|
234
|
+
if (!shouldLog(logLevel, level)) {
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
const method = (console?.[consoleMethod] ?? console.log) as (
|
|
239
|
+
...args: unknown[]
|
|
240
|
+
) => void;
|
|
241
|
+
if (!method) {
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const frame = getCallerFrameFromStack(captureStack());
|
|
246
|
+
const locationScopeCandidate = deriveScopeFromPath(frame?.filePath);
|
|
247
|
+
const shouldShowResolvedScope =
|
|
248
|
+
resolvedScope &&
|
|
249
|
+
(!locationScopeCandidate || locationScopeCandidate !== resolvedScope);
|
|
250
|
+
|
|
251
|
+
const scopeLabel = shouldShowResolvedScope ? resolvedScope : undefined;
|
|
252
|
+
const locationLabel = getLocationLabel(frame);
|
|
253
|
+
|
|
254
|
+
const prefixParts: string[] = [];
|
|
255
|
+
if (scopeLabel) {
|
|
256
|
+
prefixParts.push(`[${scopeLabel}]`);
|
|
257
|
+
}
|
|
258
|
+
if (locationLabel) {
|
|
259
|
+
prefixParts.push(`(${locationLabel})`);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
const prefix = prefixParts.length > 0 ? prefixParts.join(" ") : "";
|
|
263
|
+
const finalArgs: unknown[] = (() => {
|
|
264
|
+
if (!prefix) {
|
|
265
|
+
return args;
|
|
266
|
+
}
|
|
267
|
+
if (args.length === 0) {
|
|
268
|
+
return [prefix];
|
|
269
|
+
}
|
|
270
|
+
// Merge prefix into the first argument when it's a string so the first
|
|
271
|
+
// arg remains the console format string (e.g. "%s …" still works).
|
|
272
|
+
if (typeof args[0] === "string") {
|
|
273
|
+
return [`${prefix} ${args[0]}`, ...args.slice(1)];
|
|
274
|
+
}
|
|
275
|
+
return [prefix, ...args];
|
|
276
|
+
})();
|
|
277
|
+
|
|
278
|
+
method.apply(console, finalArgs);
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
return {
|
|
282
|
+
debug: log("debug", "debug"),
|
|
283
|
+
info: log("info", "info"),
|
|
284
|
+
warn: log("warn", "warn"),
|
|
285
|
+
error: log("error", "error"),
|
|
286
|
+
child: (childScope: string) => {
|
|
287
|
+
const nestedScope =
|
|
288
|
+
resolvedScope && resolvedScope.length > 0
|
|
289
|
+
? `${resolvedScope}:${childScope}`
|
|
290
|
+
: childScope;
|
|
291
|
+
return createLogger({ scope: nestedScope, level });
|
|
292
|
+
},
|
|
293
|
+
};
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Default logger instance scoped to the application root. Prefer using
|
|
298
|
+
* module-level loggers (via `createLogger()`) for better call-site context.
|
|
299
|
+
*/
|
|
300
|
+
export const logger = createLogger();
|
package/utils/transforms.ts
CHANGED
|
@@ -17,6 +17,9 @@ import { getAssemblyType, parseStatus } from "./mapping";
|
|
|
17
17
|
import { getDatahubGameInfo } from "./datahub";
|
|
18
18
|
import { getEnergyConfig, getEnergyUsageForType } from "./config";
|
|
19
19
|
import { getObjectWithJson } from "../graphql";
|
|
20
|
+
import { createLogger } from "./logger";
|
|
21
|
+
|
|
22
|
+
const log = createLogger();
|
|
20
23
|
|
|
21
24
|
/**
|
|
22
25
|
* Transform CharacterInfo to DetailedSmartCharacterResponse
|
|
@@ -67,7 +70,7 @@ export async function transformToAssembly(
|
|
|
67
70
|
const rawData = moveObject.contents?.json as unknown as RawSuiObjectData;
|
|
68
71
|
|
|
69
72
|
if (!rawData) {
|
|
70
|
-
|
|
73
|
+
log.warn("[DappKit] transformToAssembly: No raw data found");
|
|
71
74
|
return null;
|
|
72
75
|
}
|
|
73
76
|
|
package/utils/utils.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
1
|
import { SuiChain } from "@mysten/wallet-standard";
|
|
3
2
|
import {
|
|
4
3
|
Assemblies,
|
|
@@ -7,8 +6,6 @@ import {
|
|
|
7
6
|
AssemblyType,
|
|
8
7
|
} from "../types";
|
|
9
8
|
|
|
10
|
-
import { ONE_M3 } from "./constants";
|
|
11
|
-
|
|
12
9
|
/**
|
|
13
10
|
* Abbreviate a Sui address or hex string for display.
|
|
14
11
|
*
|
|
@@ -129,9 +126,9 @@ export const getEnv = (env: string, fallback: string): string => {
|
|
|
129
126
|
};
|
|
130
127
|
|
|
131
128
|
/**
|
|
132
|
-
* Volume in m³ from unit quantity and volume per unit (
|
|
129
|
+
* Volume in m³ from unit quantity and volume per unit (litres, 10^3).
|
|
133
130
|
* @param quantity - Unit quantity of the item
|
|
134
|
-
* @param volume - Volume per unit in
|
|
131
|
+
* @param volume - Volume per unit in litres (10^3)
|
|
135
132
|
* @returns Volume in m³
|
|
136
133
|
* @category Utilities - Formatting
|
|
137
134
|
*/
|
|
@@ -141,21 +138,19 @@ export const getVolumeM3 = (quantity: bigint, volume: bigint): number => {
|
|
|
141
138
|
};
|
|
142
139
|
|
|
143
140
|
/**
|
|
144
|
-
* Format
|
|
145
|
-
*
|
|
146
|
-
* Converts from the on-chain representation (wei-like, 10^18) to m³.
|
|
141
|
+
* Format from cubic decimeters/litres (dm³\l) to cubic meters (m³).
|
|
147
142
|
*
|
|
148
143
|
* @category Utilities - Formatting
|
|
149
|
-
* @param quantity - The raw volume value as string or bigint
|
|
144
|
+
* @param quantity - The raw volume value as string or bigint in cubic decimeters/litres (dm³/l)
|
|
150
145
|
* @returns Volume in cubic meters
|
|
151
146
|
*
|
|
152
147
|
* @example
|
|
153
148
|
* ```typescript
|
|
154
|
-
* const m3 = formatM3(BigInt("
|
|
149
|
+
* const m3 = formatM3(BigInt("1000")); // 1.0 m³
|
|
155
150
|
* ```
|
|
156
151
|
*/
|
|
157
152
|
export const formatM3 = (quantity: string | bigint): number => {
|
|
158
|
-
return Number(quantity) /
|
|
153
|
+
return Number(quantity) / 1000;
|
|
159
154
|
};
|
|
160
155
|
|
|
161
156
|
/**
|
|
@@ -177,10 +172,9 @@ export const formatM3 = (quantity: string | bigint): number => {
|
|
|
177
172
|
export const getDappUrl = (assembly: AssemblyType<Assemblies>): string => {
|
|
178
173
|
if (!assembly?.dappURL) return "";
|
|
179
174
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
if (!pattern.test(url)) {
|
|
175
|
+
let url = assembly.dappURL.trim();
|
|
176
|
+
const hasProtocol = /^((http|https|ftp):\/\/)/i.test(url);
|
|
177
|
+
if (!hasProtocol) {
|
|
184
178
|
url = "https://" + url;
|
|
185
179
|
}
|
|
186
180
|
|