@0xobelisk/sui-common 0.4.9

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/package.json ADDED
@@ -0,0 +1,71 @@
1
+ {
2
+ "name": "@0xobelisk/sui-common",
3
+ "version": "0.4.9",
4
+ "description": "Common low level logic shared between packages",
5
+ "keywords": [
6
+ "sui",
7
+ "obelisk labs",
8
+ "move",
9
+ "blockchain"
10
+ ],
11
+ "type": "module",
12
+ "author": "team@obelisk.build",
13
+ "homepage": "https://github.com/0xobelisk/obelisk-engine/tree/main/packages/sui-common#readme",
14
+ "bugs": "https://github.com/0xobelisk/obelisk-engine/issues",
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "https://github.com/0xobelisk/obelisk-engine.git"
18
+ },
19
+ "license": "Apache-2.0",
20
+ "publishConfig": {
21
+ "access": "public"
22
+ },
23
+ "engines": {
24
+ "node": ">=18"
25
+ },
26
+ "exports": {
27
+ ".": "./dist/index.js"
28
+ },
29
+ "typesVersions": {
30
+ "*": {
31
+ "index": [
32
+ "./src/index.ts"
33
+ ]
34
+ }
35
+ },
36
+ "types": "src/index.ts",
37
+ "dependencies": {
38
+ "@mysten/sui.js": "^0.41.0",
39
+ "chalk": "^5.0.1",
40
+ "dotenv": "^16.0.3",
41
+ "ejs": "^3.1.8",
42
+ "execa": "^7.0.0",
43
+ "find-up": "^6.3.0",
44
+ "glob": "^8.0.3",
45
+ "path": "^0.12.7",
46
+ "prettier": "^2.8.4",
47
+ "prettier-plugin-rust": "^0.1.9",
48
+ "prettier-plugin-solidity": "^1.1.2",
49
+ "typescript": "5.1.6",
50
+ "yargs": "^17.7.1",
51
+ "zod": "^3.22.3",
52
+ "zod-validation-error": "^1.3.0",
53
+ "esbuild": "^0.17.15"
54
+ },
55
+ "devDependencies": {
56
+ "@types/ejs": "^3.1.1",
57
+ "@types/glob": "^7.2.0",
58
+ "@types/node": "^18.15.11",
59
+ "@types/yargs": "^17.0.10",
60
+ "tsup": "^6.7.0",
61
+ "tsx": "^3.12.6"
62
+ },
63
+ "scripts": {
64
+ "build": "pnpm run build:js",
65
+ "build:js": "tsup && chmod +x ./dist/index.js",
66
+ "clean": "pnpm run clean:js",
67
+ "clean:js": "rimraf dist",
68
+ "dev": "tsup --watch",
69
+ "lint": "eslint . --ext .ts"
70
+ }
71
+ }
@@ -0,0 +1,3 @@
1
+ // import "./modules.d";
2
+ export * from "./utils";
3
+ export * from "./types";
@@ -0,0 +1 @@
1
+ declare module "prettier-plugin-rust";
@@ -0,0 +1,82 @@
1
+ export type BaseType =
2
+ | "string"
3
+ | "vector<string>"
4
+ | "address"
5
+ | "bool"
6
+ | "u8"
7
+ | "u64"
8
+ | "u128"
9
+ | "vector<address>"
10
+ | "vector<bool>"
11
+ | "vector<u8>"
12
+ | "vector<vector<u8>>"
13
+ | "vector<u64>"
14
+ | "vector<u128>";
15
+
16
+ type Address = string;
17
+ type Bool = boolean;
18
+ type U8 = number;
19
+ type U64 = number;
20
+ type U128 = number;
21
+ type Vector<T> = T[];
22
+
23
+ export type BaseValueType =
24
+ | String
25
+ | Address
26
+ | Bool
27
+ | U8
28
+ | U64
29
+ | U128
30
+ | Vector<Address>
31
+ | Vector<Bool>
32
+ | Vector<U8>
33
+ | Vector<Vector<U8>>
34
+ | Vector<U64>
35
+ | Vector<U128>;
36
+
37
+ export interface ValueType {
38
+ valueType: BaseType | Record<string, BaseType>;
39
+ ephemeral?: boolean;
40
+ defaultValue?: BaseValueType | Record<string, BaseValueType>;
41
+ }
42
+
43
+ export type SchemaMapType = BaseType | ValueType;
44
+
45
+ export type ObeliskConfig = {
46
+ name: string;
47
+ description: string;
48
+ systems: string[];
49
+ schemas: Record<string, SchemaMapType>;
50
+ };
51
+
52
+ export type MoveType =
53
+ | "string"
54
+ | "vector<string>"
55
+ | "String"
56
+ | "vector<String>"
57
+ | "address"
58
+ | "bool"
59
+ | "u8"
60
+ | "u64"
61
+ | "u128"
62
+ | "vector<address>"
63
+ | "vector<bool>"
64
+ | "vector<u8>"
65
+ | "vector<vector<u8>>"
66
+ | "vector<u64>"
67
+ | "vector<u128>";
68
+
69
+ export interface RenderSchemaOptions {
70
+ projectName: string;
71
+ systems: string[];
72
+ schemaName: string;
73
+ structName: string;
74
+ ephemeral: boolean;
75
+ singleton: boolean;
76
+ valueType: MoveType | Record<string, MoveType>; // move type
77
+ realType: BaseType | Record<string, BaseType>; // ts type
78
+ // structAttrs: string[];
79
+ // structTypes: string[];
80
+ defaultValue: BaseValueType | Record<string, BaseValueType> | undefined;
81
+ needImportString: boolean;
82
+ }
@@ -0,0 +1,68 @@
1
+ import { findUp } from "find-up";
2
+ import path from "path";
3
+ import esbuild from "esbuild";
4
+ import { NotInsideProjectError } from "./errors";
5
+ import { rmSync } from "fs";
6
+ import { pathToFileURL } from "url";
7
+ import os from "os";
8
+
9
+ // In order of preference files are checked
10
+ const configFiles = [
11
+ "obelisk.config.js",
12
+ "obelisk.config.mjs",
13
+ "obelisk.config.ts",
14
+ "obelisk.config.mts",
15
+ ];
16
+ const TEMP_CONFIG = "obelisk.config.example.mjs";
17
+
18
+ export async function loadConfig(configPath?: string): Promise<unknown> {
19
+ configPath = await resolveConfigPath(configPath);
20
+ try {
21
+ await esbuild.build({
22
+ entryPoints: [configPath],
23
+ format: "esm",
24
+ outfile: TEMP_CONFIG,
25
+ // https://esbuild.github.io/getting-started/#bundling-for-node
26
+ platform: "node",
27
+ // bundle local imports (otherwise it may error, js can't import ts)
28
+ bundle: true,
29
+ // avoid bundling external imports (it's unnecessary and esbuild can't handle all node features)
30
+ packages: "external",
31
+ });
32
+ configPath = await resolveConfigPath(TEMP_CONFIG, true);
33
+ // Node.js caches dynamic imports, so without appending a cache breaking
34
+ // param like `?update={Date.now()}` this import always returns the same config
35
+ // if called multiple times in a single process, like the `dev-contracts` cli
36
+ return (await import(configPath + `?update=${Date.now()}`)).obeliskConfig;
37
+ } finally {
38
+ rmSync(TEMP_CONFIG, { force: true });
39
+ }
40
+ }
41
+
42
+ export async function resolveConfigPath(
43
+ configPath: string | undefined,
44
+ toFileURL?: boolean
45
+ ) {
46
+ if (configPath === undefined) {
47
+ configPath = await getUserConfigPath();
48
+ } else {
49
+ if (!path.isAbsolute(configPath)) {
50
+ configPath = path.join(process.cwd(), configPath);
51
+ configPath = path.normalize(configPath);
52
+ }
53
+ }
54
+
55
+ // Add `file:///` for Windows support
56
+ // (see https://github.com/nodejs/node/issues/31710)
57
+ return toFileURL && os.platform() === "win32"
58
+ ? pathToFileURL(configPath).href
59
+ : configPath;
60
+ }
61
+
62
+ async function getUserConfigPath() {
63
+ const tsConfigPath = await findUp(configFiles);
64
+ if (tsConfigPath === undefined) {
65
+ throw new NotInsideProjectError();
66
+ }
67
+ return tsConfigPath;
68
+ }
@@ -0,0 +1,5 @@
1
+
2
+ export class NotInsideProjectError extends Error {
3
+ name = "NotInsideProjectError";
4
+ message = "You are not inside a Obelisk project";
5
+ }
@@ -0,0 +1,42 @@
1
+ // import chalk from "chalk";
2
+ import * as prettier from "prettier";
3
+ import * as rustPlugin from "prettier-plugin-rust";
4
+
5
+ export async function formatMove(
6
+ content: string,
7
+ prettierConfigPath?: string
8
+ ): Promise<string> {
9
+ let config;
10
+ if (prettierConfigPath) {
11
+ config = await prettier.resolveConfig(prettierConfigPath);
12
+ }
13
+ try {
14
+ return prettier.format(content, {
15
+ plugins: [rustPlugin],
16
+ // parser: "rustParse",
17
+
18
+ printWidth: 120,
19
+ semi: true,
20
+ tabWidth: 2,
21
+ useTabs: false,
22
+ bracketSpacing: true,
23
+
24
+ ...config,
25
+ });
26
+ } catch (error) {
27
+ let message;
28
+ if (error instanceof Error) {
29
+ message = error.message;
30
+ } else {
31
+ message = error;
32
+ }
33
+ console.log(`Error during output formatting: ${message}`);
34
+ return content;
35
+ }
36
+ }
37
+
38
+ export async function formatTypescript(content: string): Promise<string> {
39
+ return prettier.format(content, {
40
+ parser: "typescript",
41
+ });
42
+ }
@@ -0,0 +1,31 @@
1
+ import { mkdirSync, writeFileSync } from "fs";
2
+ import { dirname } from "path";
3
+ import { formatMove, formatTypescript } from "./format";
4
+
5
+ export async function formatAndWriteMove(
6
+ output: string,
7
+ fullOutputPath: string,
8
+ logPrefix?: string
9
+ ): Promise<void> {
10
+ // const formattedOutput = await formatMove(output);
11
+ // console.log(formattedOutput)
12
+ mkdirSync(dirname(fullOutputPath), { recursive: true });
13
+
14
+ writeFileSync(fullOutputPath, output);
15
+ if (logPrefix !== undefined) {
16
+ console.log(`${logPrefix}: ${fullOutputPath}`);
17
+ }
18
+ }
19
+
20
+ export async function formatAndWriteTypescript(
21
+ output: string,
22
+ fullOutputPath: string,
23
+ logPrefix: string
24
+ ): Promise<void> {
25
+ const formattedOutput = await formatTypescript(output);
26
+
27
+ mkdirSync(dirname(fullOutputPath), { recursive: true });
28
+
29
+ writeFileSync(fullOutputPath, formattedOutput);
30
+ console.log(`${logPrefix}: ${fullOutputPath}`);
31
+ }
@@ -0,0 +1,6 @@
1
+ export * from "./format";
2
+ export * from "./formatAndWrite";
3
+ export * from "./posixPath";
4
+ export * from "./renderMove/worldgen";
5
+ export * from "./renderMove/generateEps";
6
+ export * from "./config";
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Explicitly normalize a given path to a posix path (using `/` as separator).
3
+ * This should be used for generating Solidity files that will be consumed by solc,
4
+ * because solc expects `/` as path separator, but path.join produces `\` if the user is on windows.
5
+ */
6
+ export function posixPath(path: string): string {
7
+ return path.replace(/\\/g, "/");
8
+ }