@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.
- package/CHANGELOG.md +18 -0
- package/dist/cjs/client/client.d.ts +13 -12
- package/dist/cjs/client/client.js +189 -48
- package/dist/cjs/client/client.js.map +3 -3
- package/dist/cjs/experimental/cache.d.ts +1 -0
- package/dist/cjs/experimental/cache.js +9 -0
- package/dist/cjs/experimental/cache.js.map +2 -2
- package/dist/cjs/experimental/client.d.ts +2 -1
- package/dist/cjs/experimental/client.js +2 -1
- package/dist/cjs/experimental/client.js.map +2 -2
- package/dist/cjs/experimental/core.d.ts +6 -0
- package/dist/cjs/experimental/core.js +11 -3
- package/dist/cjs/experimental/core.js.map +2 -2
- package/dist/cjs/experimental/index.d.ts +2 -2
- package/dist/cjs/experimental/index.js.map +2 -2
- package/dist/cjs/experimental/mvr.d.ts +42 -0
- package/dist/cjs/experimental/mvr.js +355 -0
- package/dist/cjs/experimental/mvr.js.map +7 -0
- package/dist/cjs/experimental/transports/graphql.d.ts +4 -1
- package/dist/cjs/experimental/transports/graphql.js +17 -8
- package/dist/cjs/experimental/transports/graphql.js.map +2 -2
- package/dist/cjs/experimental/transports/jsonRPC.d.ts +5 -2
- package/dist/cjs/experimental/transports/jsonRPC.js +25 -17
- package/dist/cjs/experimental/transports/jsonRPC.js.map +3 -3
- package/dist/cjs/experimental/transports/utils.js +2 -2
- package/dist/cjs/experimental/transports/utils.js.map +2 -2
- package/dist/cjs/experimental/types.d.ts +43 -1
- package/dist/cjs/experimental/types.js.map +1 -1
- package/dist/cjs/graphql/client.d.ts +2 -1
- package/dist/cjs/graphql/client.js +6 -2
- package/dist/cjs/graphql/client.js.map +2 -2
- package/dist/cjs/transactions/index.d.ts +4 -1
- package/dist/cjs/transactions/index.js +2 -2
- package/dist/cjs/transactions/index.js.map +3 -3
- package/dist/cjs/transactions/plugins/NamedPackagesPlugin.d.ts +4 -3
- package/dist/cjs/transactions/plugins/NamedPackagesPlugin.js +38 -90
- package/dist/cjs/transactions/plugins/NamedPackagesPlugin.js.map +3 -3
- package/dist/cjs/transactions/resolve.js +6 -3
- package/dist/cjs/transactions/resolve.js.map +2 -2
- package/dist/cjs/version.d.ts +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/cjs/version.js.map +1 -1
- package/dist/esm/client/client.d.ts +13 -12
- package/dist/esm/client/client.js +188 -47
- package/dist/esm/client/client.js.map +2 -2
- package/dist/esm/experimental/cache.d.ts +1 -0
- package/dist/esm/experimental/cache.js +9 -0
- package/dist/esm/experimental/cache.js.map +2 -2
- package/dist/esm/experimental/client.d.ts +2 -1
- package/dist/esm/experimental/client.js +2 -1
- package/dist/esm/experimental/client.js.map +2 -2
- package/dist/esm/experimental/core.d.ts +6 -0
- package/dist/esm/experimental/core.js +11 -3
- package/dist/esm/experimental/core.js.map +2 -2
- package/dist/esm/experimental/index.d.ts +2 -2
- package/dist/esm/experimental/index.js.map +2 -2
- package/dist/esm/experimental/mvr.d.ts +42 -0
- package/dist/esm/experimental/mvr.js +340 -0
- package/dist/esm/experimental/mvr.js.map +7 -0
- package/dist/esm/experimental/transports/graphql.d.ts +4 -1
- package/dist/esm/experimental/transports/graphql.js +17 -8
- package/dist/esm/experimental/transports/graphql.js.map +2 -2
- package/dist/esm/experimental/transports/jsonRPC.d.ts +5 -2
- package/dist/esm/experimental/transports/jsonRPC.js +22 -14
- package/dist/esm/experimental/transports/jsonRPC.js.map +3 -3
- package/dist/esm/experimental/transports/utils.js +1 -1
- package/dist/esm/experimental/transports/utils.js.map +1 -1
- package/dist/esm/experimental/types.d.ts +43 -1
- package/dist/esm/graphql/client.d.ts +2 -1
- package/dist/esm/graphql/client.js +6 -2
- package/dist/esm/graphql/client.js.map +2 -2
- package/dist/esm/transactions/index.d.ts +4 -1
- package/dist/esm/transactions/index.js.map +2 -2
- package/dist/esm/transactions/plugins/NamedPackagesPlugin.d.ts +4 -3
- package/dist/esm/transactions/plugins/NamedPackagesPlugin.js +37 -95
- package/dist/esm/transactions/plugins/NamedPackagesPlugin.js.map +3 -3
- package/dist/esm/transactions/resolve.js +6 -3
- package/dist/esm/transactions/resolve.js.map +2 -2
- package/dist/esm/version.d.ts +1 -1
- package/dist/esm/version.js +1 -1
- package/dist/esm/version.js.map +1 -1
- package/dist/tsconfig.esm.tsbuildinfo +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +3 -3
- package/src/client/client.ts +239 -60
- package/src/experimental/cache.ts +14 -0
- package/src/experimental/client.ts +3 -1
- package/src/experimental/core.ts +20 -1
- package/src/experimental/index.ts +2 -1
- package/src/experimental/mvr.ts +477 -0
- package/src/experimental/transports/graphql.ts +22 -10
- package/src/experimental/transports/jsonRPC.ts +21 -11
- package/src/experimental/transports/utils.ts +1 -1
- package/src/experimental/types.ts +59 -1
- package/src/graphql/client.ts +7 -1
- package/src/transactions/index.ts +5 -1
- package/src/transactions/plugins/NamedPackagesPlugin.ts +46 -120
- package/src/transactions/resolve.ts +6 -3
- package/src/version.ts +1 -1
- package/dist/cjs/transactions/plugins/utils.d.ts +0 -31
- package/dist/cjs/transactions/plugins/utils.js +0 -144
- package/dist/cjs/transactions/plugins/utils.js.map +0 -7
- package/dist/esm/transactions/plugins/utils.d.ts +0 -31
- package/dist/esm/transactions/plugins/utils.js +0 -124
- package/dist/esm/transactions/plugins/utils.js.map +0 -7
- package/src/transactions/plugins/utils.ts +0 -215
|
@@ -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": "AAGA,SAA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AAU,QACV,gBAAgB;AAElB;",
|
|
6
|
-
"names": []
|
|
7
|
-
}
|
|
@@ -1,215 +0,0 @@
|
|
|
1
|
-
// Copyright (c) Mysten Labs, Inc.
|
|
2
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
|
|
4
|
-
import { isValidNamedPackage, isValidNamedType } from '../../utils/move-registry.js';
|
|
5
|
-
import { normalizeStructTag, parseStructTag } from '../../utils/sui-types.js';
|
|
6
|
-
import type { StructTag } from '../../utils/sui-types.js';
|
|
7
|
-
import type { TransactionDataBuilder } from '../TransactionData.js';
|
|
8
|
-
|
|
9
|
-
export type NamedPackagesPluginCache = {
|
|
10
|
-
packages: Record<string, string>;
|
|
11
|
-
types: Record<string, string>;
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
const NAME_SEPARATOR = '/';
|
|
15
|
-
|
|
16
|
-
export type NameResolutionRequest = {
|
|
17
|
-
id: number;
|
|
18
|
-
type: 'package' | 'moveType';
|
|
19
|
-
name: string;
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Looks up all `.move` names in a transaction block.
|
|
24
|
-
* Returns a list of all the names found.
|
|
25
|
-
*/
|
|
26
|
-
export function findNamesInTransaction(builder: TransactionDataBuilder): {
|
|
27
|
-
packages: string[];
|
|
28
|
-
types: string[];
|
|
29
|
-
} {
|
|
30
|
-
const packages: Set<string> = new Set();
|
|
31
|
-
const types: Set<string> = new Set();
|
|
32
|
-
|
|
33
|
-
for (const command of builder.commands) {
|
|
34
|
-
if (command.MakeMoveVec?.type) {
|
|
35
|
-
getNamesFromTypeList([command.MakeMoveVec.type]).forEach((type) => {
|
|
36
|
-
types.add(type);
|
|
37
|
-
});
|
|
38
|
-
continue;
|
|
39
|
-
}
|
|
40
|
-
if (!('MoveCall' in command)) continue;
|
|
41
|
-
const tx = command.MoveCall;
|
|
42
|
-
|
|
43
|
-
if (!tx) continue;
|
|
44
|
-
|
|
45
|
-
const pkg = tx.package.split('::')[0];
|
|
46
|
-
if (hasMvrName(pkg)) {
|
|
47
|
-
if (!isValidNamedPackage(pkg)) throw new Error(`Invalid package name: ${pkg}`);
|
|
48
|
-
packages.add(pkg);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
getNamesFromTypeList(tx.typeArguments ?? []).forEach((type) => {
|
|
52
|
-
types.add(type);
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
return {
|
|
57
|
-
packages: [...packages],
|
|
58
|
-
types: [...types],
|
|
59
|
-
};
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Extracts all first-level types from a list of types.
|
|
64
|
-
* E.g. for the input `['@mvr/demo::a::A<@mvr/demo::b::B>']`,
|
|
65
|
-
* the output will be `['@mvr/demo::a::A', '@mvr/demo::b::B']`.
|
|
66
|
-
*/
|
|
67
|
-
export function getFirstLevelNamedTypes(types: string[]) {
|
|
68
|
-
const results: Set<string> = new Set();
|
|
69
|
-
|
|
70
|
-
for (const type of types) {
|
|
71
|
-
findMvrNames(type).forEach((name) => results.add(name));
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
return results;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Extracts all named types from a given type.
|
|
79
|
-
*/
|
|
80
|
-
function findMvrNames(type: string | StructTag) {
|
|
81
|
-
const types: Set<string> = new Set();
|
|
82
|
-
|
|
83
|
-
if (typeof type === 'string' && !hasMvrName(type)) return types;
|
|
84
|
-
|
|
85
|
-
const tag = isStructTag(type) ? type : parseStructTag(type);
|
|
86
|
-
|
|
87
|
-
if (hasMvrName(tag.address)) types.add(`${tag.address}::${tag.module}::${tag.name}`);
|
|
88
|
-
|
|
89
|
-
for (const param of tag.typeParams) {
|
|
90
|
-
findMvrNames(param).forEach((name) => types.add(name));
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
return types;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
// /**
|
|
97
|
-
// * Allows partial replacements of known types with their resolved equivalents.
|
|
98
|
-
// * E.g. `@mvr/demo::a::A<@mvr/demo::b::B>` can be resolved, if we already have
|
|
99
|
-
// * the address for `@mvr/demo::b::B` and the address for `@mvr/demo::a::A`,
|
|
100
|
-
// * without the need to have the full type in the cache.
|
|
101
|
-
// *
|
|
102
|
-
// * Returns the fully composed resolved types (if any) in a `named-type -> normalized-type` map.
|
|
103
|
-
// */
|
|
104
|
-
export function populateNamedTypesFromCache(types: string[], typeCache: Record<string, string>) {
|
|
105
|
-
const composedTypes: Record<string, string> = {};
|
|
106
|
-
|
|
107
|
-
types.forEach((type) => {
|
|
108
|
-
const normalized = normalizeStructTag(findAndReplaceCachedTypes(type, typeCache));
|
|
109
|
-
composedTypes[type] = normalized;
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
return composedTypes;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Traverses a type, and replaces any found names with their resolved equivalents,
|
|
117
|
-
* based on the supplied type cache.
|
|
118
|
-
*/
|
|
119
|
-
function findAndReplaceCachedTypes(
|
|
120
|
-
tag: string | StructTag,
|
|
121
|
-
typeCache: Record<string, string>,
|
|
122
|
-
): StructTag {
|
|
123
|
-
const type = isStructTag(tag) ? tag : parseStructTag(tag);
|
|
124
|
-
|
|
125
|
-
const typeTag = `${type.address}::${type.module}::${type.name}`;
|
|
126
|
-
const cacheHit = typeCache[typeTag];
|
|
127
|
-
|
|
128
|
-
return {
|
|
129
|
-
...type,
|
|
130
|
-
address: cacheHit ? cacheHit.split('::')[0] : type.address,
|
|
131
|
-
typeParams: type.typeParams.map((param) => findAndReplaceCachedTypes(param, typeCache)),
|
|
132
|
-
};
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* Replace all names & types in a transaction block
|
|
137
|
-
* with their resolved names/types.
|
|
138
|
-
*/
|
|
139
|
-
export function replaceNames(builder: TransactionDataBuilder, cache: NamedPackagesPluginCache) {
|
|
140
|
-
for (const command of builder.commands) {
|
|
141
|
-
// Replacements for `MakeMoveVec` commands (that can include types)
|
|
142
|
-
if (command.MakeMoveVec?.type) {
|
|
143
|
-
if (!hasMvrName(command.MakeMoveVec.type)) continue;
|
|
144
|
-
if (!cache.types[command.MakeMoveVec.type])
|
|
145
|
-
throw new Error(`No resolution found for type: ${command.MakeMoveVec.type}`);
|
|
146
|
-
command.MakeMoveVec.type = cache.types[command.MakeMoveVec.type];
|
|
147
|
-
}
|
|
148
|
-
// Replacements for `MoveCall` commands (that can include packages & types)
|
|
149
|
-
const tx = command.MoveCall;
|
|
150
|
-
if (!tx) continue;
|
|
151
|
-
|
|
152
|
-
const nameParts = tx.package.split('::');
|
|
153
|
-
const name = nameParts[0];
|
|
154
|
-
|
|
155
|
-
if (hasMvrName(name) && !cache.packages[name])
|
|
156
|
-
throw new Error(`No address found for package: ${name}`);
|
|
157
|
-
|
|
158
|
-
// Replace package name with address.
|
|
159
|
-
if (hasMvrName(name)) {
|
|
160
|
-
nameParts[0] = cache.packages[name];
|
|
161
|
-
tx.package = nameParts.join('::');
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
const types = tx.typeArguments;
|
|
165
|
-
if (!types) continue;
|
|
166
|
-
|
|
167
|
-
for (let i = 0; i < types.length; i++) {
|
|
168
|
-
if (!hasMvrName(types[i])) continue;
|
|
169
|
-
|
|
170
|
-
if (!cache.types[types[i]]) throw new Error(`No resolution found for type: ${types[i]}`);
|
|
171
|
-
types[i] = cache.types[types[i]];
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
tx.typeArguments = types;
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
export function batch<T>(arr: T[], size: number): T[][] {
|
|
179
|
-
const batches = [];
|
|
180
|
-
for (let i = 0; i < arr.length; i += size) {
|
|
181
|
-
batches.push(arr.slice(i, i + size));
|
|
182
|
-
}
|
|
183
|
-
return batches;
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
/**
|
|
187
|
-
* Returns a list of unique types that include a name
|
|
188
|
-
* from the given list. This list is retrieved from the Transaction Data.
|
|
189
|
-
*/
|
|
190
|
-
function getNamesFromTypeList(types: string[]) {
|
|
191
|
-
const names = new Set<string>();
|
|
192
|
-
for (const type of types) {
|
|
193
|
-
if (hasMvrName(type)) {
|
|
194
|
-
if (!isValidNamedType(type)) throw new Error(`Invalid type with names: ${type}`);
|
|
195
|
-
names.add(type);
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
return names;
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
function hasMvrName(nameOrType: string) {
|
|
202
|
-
return (
|
|
203
|
-
nameOrType.includes(NAME_SEPARATOR) || nameOrType.includes('@') || nameOrType.includes('.sui')
|
|
204
|
-
);
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
function isStructTag(type: string | StructTag): type is StructTag {
|
|
208
|
-
return (
|
|
209
|
-
typeof type === 'object' &&
|
|
210
|
-
'address' in type &&
|
|
211
|
-
'module' in type &&
|
|
212
|
-
'name' in type &&
|
|
213
|
-
'typeParams' in type
|
|
214
|
-
);
|
|
215
|
-
}
|