@mysten/sui 1.31.0 → 1.33.0

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.
Files changed (106) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/dist/cjs/client/client.d.ts +13 -12
  3. package/dist/cjs/client/client.js +189 -48
  4. package/dist/cjs/client/client.js.map +3 -3
  5. package/dist/cjs/experimental/cache.d.ts +1 -0
  6. package/dist/cjs/experimental/cache.js +9 -0
  7. package/dist/cjs/experimental/cache.js.map +2 -2
  8. package/dist/cjs/experimental/client.d.ts +2 -1
  9. package/dist/cjs/experimental/client.js +2 -1
  10. package/dist/cjs/experimental/client.js.map +2 -2
  11. package/dist/cjs/experimental/core.d.ts +6 -0
  12. package/dist/cjs/experimental/core.js +11 -3
  13. package/dist/cjs/experimental/core.js.map +2 -2
  14. package/dist/cjs/experimental/index.d.ts +2 -2
  15. package/dist/cjs/experimental/index.js.map +2 -2
  16. package/dist/cjs/experimental/mvr.d.ts +42 -0
  17. package/dist/cjs/experimental/mvr.js +355 -0
  18. package/dist/cjs/experimental/mvr.js.map +7 -0
  19. package/dist/cjs/experimental/transports/graphql.d.ts +4 -1
  20. package/dist/cjs/experimental/transports/graphql.js +17 -8
  21. package/dist/cjs/experimental/transports/graphql.js.map +2 -2
  22. package/dist/cjs/experimental/transports/jsonRPC.d.ts +5 -2
  23. package/dist/cjs/experimental/transports/jsonRPC.js +25 -17
  24. package/dist/cjs/experimental/transports/jsonRPC.js.map +3 -3
  25. package/dist/cjs/experimental/transports/utils.js +2 -2
  26. package/dist/cjs/experimental/transports/utils.js.map +2 -2
  27. package/dist/cjs/experimental/types.d.ts +43 -1
  28. package/dist/cjs/experimental/types.js.map +1 -1
  29. package/dist/cjs/graphql/client.d.ts +2 -1
  30. package/dist/cjs/graphql/client.js +6 -2
  31. package/dist/cjs/graphql/client.js.map +2 -2
  32. package/dist/cjs/transactions/index.d.ts +4 -1
  33. package/dist/cjs/transactions/index.js +2 -2
  34. package/dist/cjs/transactions/index.js.map +3 -3
  35. package/dist/cjs/transactions/plugins/NamedPackagesPlugin.d.ts +4 -3
  36. package/dist/cjs/transactions/plugins/NamedPackagesPlugin.js +38 -90
  37. package/dist/cjs/transactions/plugins/NamedPackagesPlugin.js.map +3 -3
  38. package/dist/cjs/transactions/resolve.js +6 -3
  39. package/dist/cjs/transactions/resolve.js.map +2 -2
  40. package/dist/cjs/version.d.ts +1 -1
  41. package/dist/cjs/version.js +1 -1
  42. package/dist/cjs/version.js.map +1 -1
  43. package/dist/esm/client/client.d.ts +13 -12
  44. package/dist/esm/client/client.js +188 -47
  45. package/dist/esm/client/client.js.map +2 -2
  46. package/dist/esm/experimental/cache.d.ts +1 -0
  47. package/dist/esm/experimental/cache.js +9 -0
  48. package/dist/esm/experimental/cache.js.map +2 -2
  49. package/dist/esm/experimental/client.d.ts +2 -1
  50. package/dist/esm/experimental/client.js +2 -1
  51. package/dist/esm/experimental/client.js.map +2 -2
  52. package/dist/esm/experimental/core.d.ts +6 -0
  53. package/dist/esm/experimental/core.js +11 -3
  54. package/dist/esm/experimental/core.js.map +2 -2
  55. package/dist/esm/experimental/index.d.ts +2 -2
  56. package/dist/esm/experimental/index.js.map +2 -2
  57. package/dist/esm/experimental/mvr.d.ts +42 -0
  58. package/dist/esm/experimental/mvr.js +340 -0
  59. package/dist/esm/experimental/mvr.js.map +7 -0
  60. package/dist/esm/experimental/transports/graphql.d.ts +4 -1
  61. package/dist/esm/experimental/transports/graphql.js +17 -8
  62. package/dist/esm/experimental/transports/graphql.js.map +2 -2
  63. package/dist/esm/experimental/transports/jsonRPC.d.ts +5 -2
  64. package/dist/esm/experimental/transports/jsonRPC.js +22 -14
  65. package/dist/esm/experimental/transports/jsonRPC.js.map +3 -3
  66. package/dist/esm/experimental/transports/utils.js +1 -1
  67. package/dist/esm/experimental/transports/utils.js.map +1 -1
  68. package/dist/esm/experimental/types.d.ts +43 -1
  69. package/dist/esm/graphql/client.d.ts +2 -1
  70. package/dist/esm/graphql/client.js +6 -2
  71. package/dist/esm/graphql/client.js.map +2 -2
  72. package/dist/esm/transactions/index.d.ts +4 -1
  73. package/dist/esm/transactions/index.js.map +2 -2
  74. package/dist/esm/transactions/plugins/NamedPackagesPlugin.d.ts +4 -3
  75. package/dist/esm/transactions/plugins/NamedPackagesPlugin.js +37 -95
  76. package/dist/esm/transactions/plugins/NamedPackagesPlugin.js.map +3 -3
  77. package/dist/esm/transactions/resolve.js +6 -3
  78. package/dist/esm/transactions/resolve.js.map +2 -2
  79. package/dist/esm/version.d.ts +1 -1
  80. package/dist/esm/version.js +1 -1
  81. package/dist/esm/version.js.map +1 -1
  82. package/dist/tsconfig.esm.tsbuildinfo +1 -1
  83. package/dist/tsconfig.tsbuildinfo +1 -1
  84. package/package.json +3 -3
  85. package/src/client/client.ts +239 -60
  86. package/src/experimental/cache.ts +14 -0
  87. package/src/experimental/client.ts +3 -1
  88. package/src/experimental/core.ts +20 -1
  89. package/src/experimental/index.ts +2 -1
  90. package/src/experimental/mvr.ts +477 -0
  91. package/src/experimental/transports/graphql.ts +22 -10
  92. package/src/experimental/transports/jsonRPC.ts +21 -11
  93. package/src/experimental/transports/utils.ts +1 -1
  94. package/src/experimental/types.ts +59 -1
  95. package/src/graphql/client.ts +7 -1
  96. package/src/transactions/index.ts +5 -1
  97. package/src/transactions/plugins/NamedPackagesPlugin.ts +46 -120
  98. package/src/transactions/resolve.ts +6 -3
  99. package/src/version.ts +1 -1
  100. package/dist/cjs/transactions/plugins/utils.d.ts +0 -31
  101. package/dist/cjs/transactions/plugins/utils.js +0 -144
  102. package/dist/cjs/transactions/plugins/utils.js.map +0 -7
  103. package/dist/esm/transactions/plugins/utils.d.ts +0 -31
  104. package/dist/esm/transactions/plugins/utils.js +0 -124
  105. package/dist/esm/transactions/plugins/utils.js.map +0 -7
  106. package/src/transactions/plugins/utils.ts +0 -215
@@ -52,6 +52,7 @@ export interface SuiGraphQLClientOptions<Queries extends Record<string, GraphQLD
52
52
  headers?: Record<string, string>;
53
53
  queries?: Queries;
54
54
  network?: Experimental_SuiClientTypes.Network;
55
+ mvr?: Experimental_SuiClientTypes.MvrOptions;
55
56
  }
56
57
 
57
58
  export class SuiGraphQLRequestError extends Error {}
@@ -64,7 +65,7 @@ export class SuiGraphQLClient<
64
65
  #queries: Queries;
65
66
  #headers: Record<string, string>;
66
67
  #fetch: typeof fetch;
67
- core: GraphQLTransport = new GraphQLTransport(this);
68
+ core: GraphQLTransport;
68
69
 
69
70
  constructor({
70
71
  url,
@@ -72,6 +73,7 @@ export class SuiGraphQLClient<
72
73
  headers = {},
73
74
  queries = {} as Queries,
74
75
  network = 'unknown',
76
+ mvr,
75
77
  }: SuiGraphQLClientOptions<Queries>) {
76
78
  super({
77
79
  network,
@@ -80,6 +82,10 @@ export class SuiGraphQLClient<
80
82
  this.#queries = queries;
81
83
  this.#headers = headers;
82
84
  this.#fetch = (...args) => fetchFn(...args);
85
+ this.core = new GraphQLTransport({
86
+ graphqlClient: this,
87
+ mvr,
88
+ });
83
89
  }
84
90
 
85
91
  async query<Result = Record<string, unknown>, Variables = Record<string, unknown>>(
@@ -1,6 +1,8 @@
1
1
  // Copyright (c) Mysten Labs, Inc.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
+ import type { NamedPackagesOverrides } from '../experimental/mvr.js';
5
+
4
6
  export { normalizedTypeToMoveTypeSignature, getPureBcsSchema } from './serializer.js';
5
7
 
6
8
  export { Inputs } from './Inputs.js';
@@ -53,6 +55,8 @@ export {
53
55
  type NamedPackagesPluginOptions,
54
56
  } from './plugins/NamedPackagesPlugin.js';
55
57
 
56
- export { type NamedPackagesPluginCache } from './plugins/utils.js';
58
+ export type { NamedPackagesOverrides };
59
+ /** @deprecated Use NamedPackagesOverrides instead */
60
+ export type NamedPackagesPluginCache = NamedPackagesOverrides;
57
61
 
58
62
  export { isArgument } from './utils.js';
@@ -1,17 +1,12 @@
1
1
  // Copyright (c) Mysten Labs, Inc.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
- import { parseStructTag } from '../../utils/sui-types.js';
4
+ import { ClientCache } from '../../experimental/cache.js';
5
+ import { MvrClient } from '../../experimental/mvr.js';
5
6
  import type { BuildTransactionOptions } from '../resolve.js';
6
7
  import type { TransactionDataBuilder } from '../TransactionData.js';
7
- import type { NamedPackagesPluginCache } from './utils.js';
8
- import {
9
- batch,
10
- findNamesInTransaction,
11
- getFirstLevelNamedTypes,
12
- populateNamedTypesFromCache,
13
- replaceNames,
14
- } from './utils.js';
8
+ import { findNamesInTransaction, replaceNames } from '../../experimental/mvr.js';
9
+ import type { NamedPackagesOverrides } from '../../experimental/mvr.js';
15
10
 
16
11
  export type NamedPackagesPluginOptions = {
17
12
  /**
@@ -40,9 +35,14 @@ export type NamedPackagesPluginOptions = {
40
35
  * }
41
36
  *
42
37
  */
43
- overrides?: NamedPackagesPluginCache;
38
+ overrides?: NamedPackagesOverrides;
44
39
  };
45
40
 
41
+ // The original versions of the mvr plugin cached lookups by mutating overrides.
42
+ // We don't want to mutate the options, but we can link our cache to the provided overrides object
43
+ // This preserves the caching across transactions while removing the mutation side effects
44
+ const cacheMap = new WeakMap<object, ClientCache>();
45
+
46
46
  /**
47
47
  * @experimental This plugin is in experimental phase and there might be breaking changes in the future
48
48
  *
@@ -57,129 +57,55 @@ export type NamedPackagesPluginOptions = {
57
57
  *
58
58
  * You can also define `overrides` to pre-populate name resolutions locally (removes the GraphQL request).
59
59
  */
60
- export const namedPackagesPlugin = ({
61
- url,
62
- pageSize = 50,
63
- overrides = { packages: {}, types: {} },
64
- }: NamedPackagesPluginOptions) => {
65
- // validate that types are first-level only.
66
- Object.keys(overrides.types).forEach((type) => {
67
- if (parseStructTag(type).typeParams.length > 0)
68
- throw new Error(
69
- 'Type overrides must be first-level only. If you want to supply generic types, just pass each type individually.',
70
- );
71
- });
72
-
73
- const cache = overrides;
60
+ export const namedPackagesPlugin = (options?: NamedPackagesPluginOptions) => {
61
+ let mvrClient: MvrClient | undefined;
62
+
63
+ if (options) {
64
+ const overrides = options.overrides ?? {
65
+ packages: {},
66
+ types: {},
67
+ };
68
+
69
+ if (!cacheMap.has(overrides)) {
70
+ cacheMap.set(overrides, new ClientCache());
71
+ }
72
+
73
+ mvrClient = new MvrClient({
74
+ cache: cacheMap.get(overrides)!,
75
+ url: options.url,
76
+ pageSize: options.pageSize,
77
+ overrides: overrides,
78
+ });
79
+ }
74
80
 
75
81
  return async (
76
82
  transactionData: TransactionDataBuilder,
77
- _buildOptions: BuildTransactionOptions,
83
+ buildOptions: BuildTransactionOptions,
78
84
  next: () => Promise<void>,
79
85
  ) => {
80
86
  const names = findNamesInTransaction(transactionData);
81
87
 
82
- const [packages, types] = await Promise.all([
83
- resolvePackages(
84
- names.packages.filter((x) => !cache.packages[x]),
85
- url,
86
- pageSize,
87
- ),
88
- resolveTypes(
89
- [...getFirstLevelNamedTypes(names.types)].filter((x) => !cache.types[x]),
90
- url,
91
- pageSize,
92
- ),
93
- ]);
88
+ if (names.types.length === 0 && names.packages.length === 0) {
89
+ return next();
90
+ }
94
91
 
95
- // save first-level mappings to cache.
96
- Object.assign(cache.packages, packages);
97
- Object.assign(cache.types, types);
98
-
99
- const composedTypes = populateNamedTypesFromCache(names.types, cache.types);
100
-
101
- // when replacing names, we also need to replace the "composed" types collected above.
102
- replaceNames(transactionData, {
103
- packages: { ...cache.packages },
104
- // we include the "composed" type cache too.
105
- types: composedTypes,
92
+ const resolved = await (mvrClient || getClient(buildOptions).core.mvr).resolve({
93
+ types: names.types,
94
+ packages: names.packages,
106
95
  });
107
96
 
97
+ replaceNames(transactionData, resolved);
98
+
108
99
  await next();
109
100
  };
101
+ };
110
102
 
111
- async function resolvePackages(packages: string[], apiUrl: string, pageSize: number) {
112
- if (packages.length === 0) return {};
113
-
114
- const batches = batch(packages, pageSize);
115
- const results: Record<string, string> = {};
116
-
117
- await Promise.all(
118
- batches.map(async (batch) => {
119
- const response = await fetch(`${apiUrl}/v1/resolution/bulk`, {
120
- method: 'POST',
121
- headers: { 'Content-Type': 'application/json' },
122
- body: JSON.stringify({
123
- names: batch,
124
- }),
125
- });
126
-
127
- if (!response.ok) {
128
- const errorBody = await response.json().catch(() => ({}));
129
- throw new Error(`Failed to resolve packages: ${errorBody?.message}`);
130
- }
131
-
132
- const data = await response.json();
133
-
134
- if (!data?.resolution) return;
135
-
136
- for (const pkg of Object.keys(data?.resolution)) {
137
- const pkgData = data.resolution[pkg]?.package_id;
138
-
139
- if (!pkgData) continue;
140
-
141
- results[pkg] = pkgData;
142
- }
143
- }),
103
+ export function getClient(options: BuildTransactionOptions) {
104
+ if (!options.client) {
105
+ throw new Error(
106
+ `No sui client passed to Transaction#build, but transaction data was not sufficient to build offline.`,
144
107
  );
145
-
146
- return results;
147
108
  }
148
109
 
149
- async function resolveTypes(types: string[], apiUrl: string, pageSize: number) {
150
- if (types.length === 0) return {};
151
-
152
- const batches = batch(types, pageSize);
153
- const results: Record<string, string> = {};
154
-
155
- await Promise.all(
156
- batches.map(async (batch) => {
157
- const response = await fetch(`${apiUrl}/v1/struct-definition/bulk`, {
158
- method: 'POST',
159
- headers: { 'Content-Type': 'application/json' },
160
- body: JSON.stringify({
161
- types: batch,
162
- }),
163
- });
164
-
165
- if (!response.ok) {
166
- const errorBody = await response.json().catch(() => ({}));
167
- throw new Error(`Failed to resolve types: ${errorBody?.message}`);
168
- }
169
-
170
- const data = await response.json();
171
-
172
- if (!data?.resolution) return;
173
-
174
- for (const type of Object.keys(data?.resolution)) {
175
- const typeData = data.resolution[type]?.type_tag;
176
- if (!typeData) continue;
177
-
178
- results[type] = typeData;
179
- }
180
- }),
181
- );
182
-
183
- return results;
184
- }
185
- };
110
+ return options.client;
111
+ }
@@ -8,6 +8,7 @@ import type { TransactionDataBuilder } from './TransactionData.js';
8
8
  import type { BcsType } from '@mysten/bcs';
9
9
  import { Inputs } from './Inputs.js';
10
10
  import { bcs } from '../bcs/index.js';
11
+ import { namedPackagesPlugin } from './plugins/NamedPackagesPlugin.js';
11
12
 
12
13
  export interface BuildTransactionOptions {
13
14
  client?: ClientWithCoreApi;
@@ -59,9 +60,11 @@ export async function resolveTransactionPlugin(
59
60
  const client = getClient(options);
60
61
  const plugin = client.core.resolveTransactionPlugin();
61
62
 
62
- return plugin(transactionData, options, async () => {
63
- await validate(transactionData);
64
- await next();
63
+ return namedPackagesPlugin()(transactionData, options, async () => {
64
+ await plugin(transactionData, options, async () => {
65
+ await validate(transactionData);
66
+ await next();
67
+ });
65
68
  });
66
69
  }
67
70
 
package/src/version.ts CHANGED
@@ -3,5 +3,5 @@
3
3
 
4
4
  // This file is generated by genversion.mjs. Do not edit it directly.
5
5
 
6
- export const PACKAGE_VERSION = '1.31.0';
6
+ export const PACKAGE_VERSION = '1.33.0';
7
7
  export const TARGETED_RPC_VERSION = '1.51.0';
@@ -1,31 +0,0 @@
1
- import type { TransactionDataBuilder } from '../TransactionData.js';
2
- export type NamedPackagesPluginCache = {
3
- packages: Record<string, string>;
4
- types: Record<string, string>;
5
- };
6
- export type NameResolutionRequest = {
7
- id: number;
8
- type: 'package' | 'moveType';
9
- name: string;
10
- };
11
- /**
12
- * Looks up all `.move` names in a transaction block.
13
- * Returns a list of all the names found.
14
- */
15
- export declare function findNamesInTransaction(builder: TransactionDataBuilder): {
16
- packages: string[];
17
- types: string[];
18
- };
19
- /**
20
- * Extracts all first-level types from a list of types.
21
- * E.g. for the input `['@mvr/demo::a::A<@mvr/demo::b::B>']`,
22
- * the output will be `['@mvr/demo::a::A', '@mvr/demo::b::B']`.
23
- */
24
- export declare function getFirstLevelNamedTypes(types: string[]): Set<string>;
25
- export declare function populateNamedTypesFromCache(types: string[], typeCache: Record<string, string>): Record<string, string>;
26
- /**
27
- * Replace all names & types in a transaction block
28
- * with their resolved names/types.
29
- */
30
- export declare function replaceNames(builder: TransactionDataBuilder, cache: NamedPackagesPluginCache): void;
31
- export declare function batch<T>(arr: T[], size: number): T[][];
@@ -1,144 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var utils_exports = {};
20
- __export(utils_exports, {
21
- batch: () => batch,
22
- findNamesInTransaction: () => findNamesInTransaction,
23
- getFirstLevelNamedTypes: () => getFirstLevelNamedTypes,
24
- populateNamedTypesFromCache: () => populateNamedTypesFromCache,
25
- replaceNames: () => replaceNames
26
- });
27
- module.exports = __toCommonJS(utils_exports);
28
- var import_move_registry = require("../../utils/move-registry.js");
29
- var import_sui_types = require("../../utils/sui-types.js");
30
- const NAME_SEPARATOR = "/";
31
- function findNamesInTransaction(builder) {
32
- const packages = /* @__PURE__ */ new Set();
33
- const types = /* @__PURE__ */ new Set();
34
- for (const command of builder.commands) {
35
- if (command.MakeMoveVec?.type) {
36
- getNamesFromTypeList([command.MakeMoveVec.type]).forEach((type) => {
37
- types.add(type);
38
- });
39
- continue;
40
- }
41
- if (!("MoveCall" in command)) continue;
42
- const tx = command.MoveCall;
43
- if (!tx) continue;
44
- const pkg = tx.package.split("::")[0];
45
- if (hasMvrName(pkg)) {
46
- if (!(0, import_move_registry.isValidNamedPackage)(pkg)) throw new Error(`Invalid package name: ${pkg}`);
47
- packages.add(pkg);
48
- }
49
- getNamesFromTypeList(tx.typeArguments ?? []).forEach((type) => {
50
- types.add(type);
51
- });
52
- }
53
- return {
54
- packages: [...packages],
55
- types: [...types]
56
- };
57
- }
58
- function getFirstLevelNamedTypes(types) {
59
- const results = /* @__PURE__ */ new Set();
60
- for (const type of types) {
61
- findMvrNames(type).forEach((name) => results.add(name));
62
- }
63
- return results;
64
- }
65
- function findMvrNames(type) {
66
- const types = /* @__PURE__ */ new Set();
67
- if (typeof type === "string" && !hasMvrName(type)) return types;
68
- const tag = isStructTag(type) ? type : (0, import_sui_types.parseStructTag)(type);
69
- if (hasMvrName(tag.address)) types.add(`${tag.address}::${tag.module}::${tag.name}`);
70
- for (const param of tag.typeParams) {
71
- findMvrNames(param).forEach((name) => types.add(name));
72
- }
73
- return types;
74
- }
75
- function populateNamedTypesFromCache(types, typeCache) {
76
- const composedTypes = {};
77
- types.forEach((type) => {
78
- const normalized = (0, import_sui_types.normalizeStructTag)(findAndReplaceCachedTypes(type, typeCache));
79
- composedTypes[type] = normalized;
80
- });
81
- return composedTypes;
82
- }
83
- function findAndReplaceCachedTypes(tag, typeCache) {
84
- const type = isStructTag(tag) ? tag : (0, import_sui_types.parseStructTag)(tag);
85
- const typeTag = `${type.address}::${type.module}::${type.name}`;
86
- const cacheHit = typeCache[typeTag];
87
- return {
88
- ...type,
89
- address: cacheHit ? cacheHit.split("::")[0] : type.address,
90
- typeParams: type.typeParams.map((param) => findAndReplaceCachedTypes(param, typeCache))
91
- };
92
- }
93
- function replaceNames(builder, cache) {
94
- for (const command of builder.commands) {
95
- if (command.MakeMoveVec?.type) {
96
- if (!hasMvrName(command.MakeMoveVec.type)) continue;
97
- if (!cache.types[command.MakeMoveVec.type])
98
- throw new Error(`No resolution found for type: ${command.MakeMoveVec.type}`);
99
- command.MakeMoveVec.type = cache.types[command.MakeMoveVec.type];
100
- }
101
- const tx = command.MoveCall;
102
- if (!tx) continue;
103
- const nameParts = tx.package.split("::");
104
- const name = nameParts[0];
105
- if (hasMvrName(name) && !cache.packages[name])
106
- throw new Error(`No address found for package: ${name}`);
107
- if (hasMvrName(name)) {
108
- nameParts[0] = cache.packages[name];
109
- tx.package = nameParts.join("::");
110
- }
111
- const types = tx.typeArguments;
112
- if (!types) continue;
113
- for (let i = 0; i < types.length; i++) {
114
- if (!hasMvrName(types[i])) continue;
115
- if (!cache.types[types[i]]) throw new Error(`No resolution found for type: ${types[i]}`);
116
- types[i] = cache.types[types[i]];
117
- }
118
- tx.typeArguments = types;
119
- }
120
- }
121
- function batch(arr, size) {
122
- const batches = [];
123
- for (let i = 0; i < arr.length; i += size) {
124
- batches.push(arr.slice(i, i + size));
125
- }
126
- return batches;
127
- }
128
- function getNamesFromTypeList(types) {
129
- const names = /* @__PURE__ */ new Set();
130
- for (const type of types) {
131
- if (hasMvrName(type)) {
132
- if (!(0, import_move_registry.isValidNamedType)(type)) throw new Error(`Invalid type with names: ${type}`);
133
- names.add(type);
134
- }
135
- }
136
- return names;
137
- }
138
- function hasMvrName(nameOrType) {
139
- return nameOrType.includes(NAME_SEPARATOR) || nameOrType.includes("@") || nameOrType.includes(".sui");
140
- }
141
- function isStructTag(type) {
142
- return typeof type === "object" && "address" in type && "module" in type && "name" in type && "typeParams" in type;
143
- }
144
- //# sourceMappingURL=utils.js.map
@@ -1,7 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../../../../src/transactions/plugins/utils.ts"],
4
- "sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { isValidNamedPackage, isValidNamedType } from '../../utils/move-registry.js';\nimport { normalizeStructTag, parseStructTag } from '../../utils/sui-types.js';\nimport type { StructTag } from '../../utils/sui-types.js';\nimport type { TransactionDataBuilder } from '../TransactionData.js';\n\nexport type NamedPackagesPluginCache = {\n\tpackages: Record<string, string>;\n\ttypes: Record<string, string>;\n};\n\nconst NAME_SEPARATOR = '/';\n\nexport type NameResolutionRequest = {\n\tid: number;\n\ttype: 'package' | 'moveType';\n\tname: string;\n};\n\n/**\n * Looks up all `.move` names in a transaction block.\n * Returns a list of all the names found.\n */\nexport function findNamesInTransaction(builder: TransactionDataBuilder): {\n\tpackages: string[];\n\ttypes: string[];\n} {\n\tconst packages: Set<string> = new Set();\n\tconst types: Set<string> = new Set();\n\n\tfor (const command of builder.commands) {\n\t\tif (command.MakeMoveVec?.type) {\n\t\t\tgetNamesFromTypeList([command.MakeMoveVec.type]).forEach((type) => {\n\t\t\t\ttypes.add(type);\n\t\t\t});\n\t\t\tcontinue;\n\t\t}\n\t\tif (!('MoveCall' in command)) continue;\n\t\tconst tx = command.MoveCall;\n\n\t\tif (!tx) continue;\n\n\t\tconst pkg = tx.package.split('::')[0];\n\t\tif (hasMvrName(pkg)) {\n\t\t\tif (!isValidNamedPackage(pkg)) throw new Error(`Invalid package name: ${pkg}`);\n\t\t\tpackages.add(pkg);\n\t\t}\n\n\t\tgetNamesFromTypeList(tx.typeArguments ?? []).forEach((type) => {\n\t\t\ttypes.add(type);\n\t\t});\n\t}\n\n\treturn {\n\t\tpackages: [...packages],\n\t\ttypes: [...types],\n\t};\n}\n\n/**\n * Extracts all first-level types from a list of types.\n * E.g. for the input `['@mvr/demo::a::A<@mvr/demo::b::B>']`,\n * the output will be `['@mvr/demo::a::A', '@mvr/demo::b::B']`.\n */\nexport function getFirstLevelNamedTypes(types: string[]) {\n\tconst results: Set<string> = new Set();\n\n\tfor (const type of types) {\n\t\tfindMvrNames(type).forEach((name) => results.add(name));\n\t}\n\n\treturn results;\n}\n\n/**\n * Extracts all named types from a given type.\n */\nfunction findMvrNames(type: string | StructTag) {\n\tconst types: Set<string> = new Set();\n\n\tif (typeof type === 'string' && !hasMvrName(type)) return types;\n\n\tconst tag = isStructTag(type) ? type : parseStructTag(type);\n\n\tif (hasMvrName(tag.address)) types.add(`${tag.address}::${tag.module}::${tag.name}`);\n\n\tfor (const param of tag.typeParams) {\n\t\tfindMvrNames(param).forEach((name) => types.add(name));\n\t}\n\n\treturn types;\n}\n\n// /**\n// * Allows partial replacements of known types with their resolved equivalents.\n// * E.g. `@mvr/demo::a::A<@mvr/demo::b::B>` can be resolved, if we already have\n// * the address for `@mvr/demo::b::B` and the address for `@mvr/demo::a::A`,\n// * without the need to have the full type in the cache.\n// *\n// * Returns the fully composed resolved types (if any) in a `named-type -> normalized-type` map.\n// */\nexport function populateNamedTypesFromCache(types: string[], typeCache: Record<string, string>) {\n\tconst composedTypes: Record<string, string> = {};\n\n\ttypes.forEach((type) => {\n\t\tconst normalized = normalizeStructTag(findAndReplaceCachedTypes(type, typeCache));\n\t\tcomposedTypes[type] = normalized;\n\t});\n\n\treturn composedTypes;\n}\n\n/**\n * Traverses a type, and replaces any found names with their resolved equivalents,\n * based on the supplied type cache.\n */\nfunction findAndReplaceCachedTypes(\n\ttag: string | StructTag,\n\ttypeCache: Record<string, string>,\n): StructTag {\n\tconst type = isStructTag(tag) ? tag : parseStructTag(tag);\n\n\tconst typeTag = `${type.address}::${type.module}::${type.name}`;\n\tconst cacheHit = typeCache[typeTag];\n\n\treturn {\n\t\t...type,\n\t\taddress: cacheHit ? cacheHit.split('::')[0] : type.address,\n\t\ttypeParams: type.typeParams.map((param) => findAndReplaceCachedTypes(param, typeCache)),\n\t};\n}\n\n/**\n * Replace all names & types in a transaction block\n * with their resolved names/types.\n */\nexport function replaceNames(builder: TransactionDataBuilder, cache: NamedPackagesPluginCache) {\n\tfor (const command of builder.commands) {\n\t\t// Replacements for `MakeMoveVec` commands (that can include types)\n\t\tif (command.MakeMoveVec?.type) {\n\t\t\tif (!hasMvrName(command.MakeMoveVec.type)) continue;\n\t\t\tif (!cache.types[command.MakeMoveVec.type])\n\t\t\t\tthrow new Error(`No resolution found for type: ${command.MakeMoveVec.type}`);\n\t\t\tcommand.MakeMoveVec.type = cache.types[command.MakeMoveVec.type];\n\t\t}\n\t\t// Replacements for `MoveCall` commands (that can include packages & types)\n\t\tconst tx = command.MoveCall;\n\t\tif (!tx) continue;\n\n\t\tconst nameParts = tx.package.split('::');\n\t\tconst name = nameParts[0];\n\n\t\tif (hasMvrName(name) && !cache.packages[name])\n\t\t\tthrow new Error(`No address found for package: ${name}`);\n\n\t\t// Replace package name with address.\n\t\tif (hasMvrName(name)) {\n\t\t\tnameParts[0] = cache.packages[name];\n\t\t\ttx.package = nameParts.join('::');\n\t\t}\n\n\t\tconst types = tx.typeArguments;\n\t\tif (!types) continue;\n\n\t\tfor (let i = 0; i < types.length; i++) {\n\t\t\tif (!hasMvrName(types[i])) continue;\n\n\t\t\tif (!cache.types[types[i]]) throw new Error(`No resolution found for type: ${types[i]}`);\n\t\t\ttypes[i] = cache.types[types[i]];\n\t\t}\n\n\t\ttx.typeArguments = types;\n\t}\n}\n\nexport function batch<T>(arr: T[], size: number): T[][] {\n\tconst batches = [];\n\tfor (let i = 0; i < arr.length; i += size) {\n\t\tbatches.push(arr.slice(i, i + size));\n\t}\n\treturn batches;\n}\n\n/**\n * Returns a list of unique types that include a name\n * from the given list. This list is retrieved from the Transaction Data.\n */\nfunction getNamesFromTypeList(types: string[]) {\n\tconst names = new Set<string>();\n\tfor (const type of types) {\n\t\tif (hasMvrName(type)) {\n\t\t\tif (!isValidNamedType(type)) throw new Error(`Invalid type with names: ${type}`);\n\t\t\tnames.add(type);\n\t\t}\n\t}\n\treturn names;\n}\n\nfunction hasMvrName(nameOrType: string) {\n\treturn (\n\t\tnameOrType.includes(NAME_SEPARATOR) || nameOrType.includes('@') || nameOrType.includes('.sui')\n\t);\n}\n\nfunction isStructTag(type: string | StructTag): type is StructTag {\n\treturn (\n\t\ttypeof type === 'object' &&\n\t\t'address' in type &&\n\t\t'module' in type &&\n\t\t'name' in type &&\n\t\t'typeParams' in type\n\t);\n}\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,2BAAsD;AACtD,uBAAmD;AASnD,MAAM,iBAAiB;AAYhB,SAAS,uBAAuB,SAGrC;AACD,QAAM,WAAwB,oBAAI,IAAI;AACtC,QAAM,QAAqB,oBAAI,IAAI;AAEnC,aAAW,WAAW,QAAQ,UAAU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ClC,SAAG,UAAU,UAAU,KAAK,IAAI;AAAA,IACjC;AAEA,UAAM,QAAQ,GAAG;AACjB,QAAI,CAAC,MAAO;AAEZ,aAAS,IAAI,GAAG,IAAI,MAAM,QAAQ,KAAK;AACtC,UAAI,CAAC,WAAW,MAAM,CAAC,CAAC,EAAG;AAE3B,UAAI,CAAC,MAAM,MAAM,MAAM,CAAC,CAAC,EAAG,OAAM,IAAI,MAAM,iCAAiC,MAAM,CAAC,CAAC,EAAE;AACvF,YAAM,CAAC,IAAI,MAAM,MAAM,MAAM,CAAC,CAAC;AAAA,IAChC;AAEA,OAAG,gBAAgB;AAAA,EACpB;AACD;AAEO,SAAS,MAAS,KAAU,MAAqB;AACvD,QAAM,UAAU,CAAC;AACjB,WAAS,IAAI,GAAG,IAAI,IAAI,QAAQ,KAAK,MAAM;AAC1C,YAAQ,KAAK,IAAI,MAAM,GAAG,IAAI,IAAI,CAAC;AAAA,EACpC;AACA,SAAO;AACR;AAMA,SAAS,qBAAqB,OAAiB;AAC9C,QAAM,QAAQ,oBAAI,IAAY;AAC9B,aAAW,QAAQ,OAAO;AACzB,QAAI,WAAW,IAAI,GAAG;AACrB,UAAI,KAAC,uCAAiB,IAAI,EAAG,OAAM,IAAI,MAAM,4BAA4B,IAAI,EAAE;AAC/E,YAAM,IAAI,IAAI;AAAA,IACf;AAAA,EACD;AACA,SAAO;AACR;AAEA,SAAS,WAAW,YAAoB;AACvC,SACC,WAAW,SAAS,cAAc,KAAK,WAAW,SAAS,GAAG,KAAK,WAAW,SAAS,MAAM;AAE/F;AAEA,SAAS,YAAY,MAA6C;AACjE,SACC,OAAO,SAAS,YAChB,aAAa,QACb,YAAY,QACZ,UAAU,QACV,gBAAgB;AAElB;",
6
- "names": []
7
- }
@@ -1,31 +0,0 @@
1
- import type { TransactionDataBuilder } from '../TransactionData.js';
2
- export type NamedPackagesPluginCache = {
3
- packages: Record<string, string>;
4
- types: Record<string, string>;
5
- };
6
- export type NameResolutionRequest = {
7
- id: number;
8
- type: 'package' | 'moveType';
9
- name: string;
10
- };
11
- /**
12
- * Looks up all `.move` names in a transaction block.
13
- * Returns a list of all the names found.
14
- */
15
- export declare function findNamesInTransaction(builder: TransactionDataBuilder): {
16
- packages: string[];
17
- types: string[];
18
- };
19
- /**
20
- * Extracts all first-level types from a list of types.
21
- * E.g. for the input `['@mvr/demo::a::A<@mvr/demo::b::B>']`,
22
- * the output will be `['@mvr/demo::a::A', '@mvr/demo::b::B']`.
23
- */
24
- export declare function getFirstLevelNamedTypes(types: string[]): Set<string>;
25
- export declare function populateNamedTypesFromCache(types: string[], typeCache: Record<string, string>): Record<string, string>;
26
- /**
27
- * Replace all names & types in a transaction block
28
- * with their resolved names/types.
29
- */
30
- export declare function replaceNames(builder: TransactionDataBuilder, cache: NamedPackagesPluginCache): void;
31
- export declare function batch<T>(arr: T[], size: number): T[][];
@@ -1,124 +0,0 @@
1
- import { isValidNamedPackage, isValidNamedType } from "../../utils/move-registry.js";
2
- import { normalizeStructTag, parseStructTag } from "../../utils/sui-types.js";
3
- const NAME_SEPARATOR = "/";
4
- function findNamesInTransaction(builder) {
5
- const packages = /* @__PURE__ */ new Set();
6
- const types = /* @__PURE__ */ new Set();
7
- for (const command of builder.commands) {
8
- if (command.MakeMoveVec?.type) {
9
- getNamesFromTypeList([command.MakeMoveVec.type]).forEach((type) => {
10
- types.add(type);
11
- });
12
- continue;
13
- }
14
- if (!("MoveCall" in command)) continue;
15
- const tx = command.MoveCall;
16
- if (!tx) continue;
17
- const pkg = tx.package.split("::")[0];
18
- if (hasMvrName(pkg)) {
19
- if (!isValidNamedPackage(pkg)) throw new Error(`Invalid package name: ${pkg}`);
20
- packages.add(pkg);
21
- }
22
- getNamesFromTypeList(tx.typeArguments ?? []).forEach((type) => {
23
- types.add(type);
24
- });
25
- }
26
- return {
27
- packages: [...packages],
28
- types: [...types]
29
- };
30
- }
31
- function getFirstLevelNamedTypes(types) {
32
- const results = /* @__PURE__ */ new Set();
33
- for (const type of types) {
34
- findMvrNames(type).forEach((name) => results.add(name));
35
- }
36
- return results;
37
- }
38
- function findMvrNames(type) {
39
- const types = /* @__PURE__ */ new Set();
40
- if (typeof type === "string" && !hasMvrName(type)) return types;
41
- const tag = isStructTag(type) ? type : parseStructTag(type);
42
- if (hasMvrName(tag.address)) types.add(`${tag.address}::${tag.module}::${tag.name}`);
43
- for (const param of tag.typeParams) {
44
- findMvrNames(param).forEach((name) => types.add(name));
45
- }
46
- return types;
47
- }
48
- function populateNamedTypesFromCache(types, typeCache) {
49
- const composedTypes = {};
50
- types.forEach((type) => {
51
- const normalized = normalizeStructTag(findAndReplaceCachedTypes(type, typeCache));
52
- composedTypes[type] = normalized;
53
- });
54
- return composedTypes;
55
- }
56
- function findAndReplaceCachedTypes(tag, typeCache) {
57
- const type = isStructTag(tag) ? tag : parseStructTag(tag);
58
- const typeTag = `${type.address}::${type.module}::${type.name}`;
59
- const cacheHit = typeCache[typeTag];
60
- return {
61
- ...type,
62
- address: cacheHit ? cacheHit.split("::")[0] : type.address,
63
- typeParams: type.typeParams.map((param) => findAndReplaceCachedTypes(param, typeCache))
64
- };
65
- }
66
- function replaceNames(builder, cache) {
67
- for (const command of builder.commands) {
68
- if (command.MakeMoveVec?.type) {
69
- if (!hasMvrName(command.MakeMoveVec.type)) continue;
70
- if (!cache.types[command.MakeMoveVec.type])
71
- throw new Error(`No resolution found for type: ${command.MakeMoveVec.type}`);
72
- command.MakeMoveVec.type = cache.types[command.MakeMoveVec.type];
73
- }
74
- const tx = command.MoveCall;
75
- if (!tx) continue;
76
- const nameParts = tx.package.split("::");
77
- const name = nameParts[0];
78
- if (hasMvrName(name) && !cache.packages[name])
79
- throw new Error(`No address found for package: ${name}`);
80
- if (hasMvrName(name)) {
81
- nameParts[0] = cache.packages[name];
82
- tx.package = nameParts.join("::");
83
- }
84
- const types = tx.typeArguments;
85
- if (!types) continue;
86
- for (let i = 0; i < types.length; i++) {
87
- if (!hasMvrName(types[i])) continue;
88
- if (!cache.types[types[i]]) throw new Error(`No resolution found for type: ${types[i]}`);
89
- types[i] = cache.types[types[i]];
90
- }
91
- tx.typeArguments = types;
92
- }
93
- }
94
- function batch(arr, size) {
95
- const batches = [];
96
- for (let i = 0; i < arr.length; i += size) {
97
- batches.push(arr.slice(i, i + size));
98
- }
99
- return batches;
100
- }
101
- function getNamesFromTypeList(types) {
102
- const names = /* @__PURE__ */ new Set();
103
- for (const type of types) {
104
- if (hasMvrName(type)) {
105
- if (!isValidNamedType(type)) throw new Error(`Invalid type with names: ${type}`);
106
- names.add(type);
107
- }
108
- }
109
- return names;
110
- }
111
- function hasMvrName(nameOrType) {
112
- return nameOrType.includes(NAME_SEPARATOR) || nameOrType.includes("@") || nameOrType.includes(".sui");
113
- }
114
- function isStructTag(type) {
115
- return typeof type === "object" && "address" in type && "module" in type && "name" in type && "typeParams" in type;
116
- }
117
- export {
118
- batch,
119
- findNamesInTransaction,
120
- getFirstLevelNamedTypes,
121
- populateNamedTypesFromCache,
122
- replaceNames
123
- };
124
- //# sourceMappingURL=utils.js.map