@sentio/sdk 2.13.0-rc.5 → 2.13.0-rc.6

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 (58) hide show
  1. package/lib/aptos/aptos-chain-adapter.d.ts +4 -2
  2. package/lib/aptos/aptos-chain-adapter.js +9 -0
  3. package/lib/aptos/aptos-chain-adapter.js.map +1 -1
  4. package/lib/aptos/aptos-processor.d.ts +5 -4
  5. package/lib/aptos/aptos-processor.js +2 -2
  6. package/lib/aptos/aptos-processor.js.map +1 -1
  7. package/lib/aptos/codegen/codegen.js.map +1 -1
  8. package/lib/aptos/ext/aptos-dex.js +1 -1
  9. package/lib/aptos/ext/aptos-dex.js.map +1 -1
  10. package/lib/aptos/module-client.js +1 -1
  11. package/lib/aptos/module-client.js.map +1 -1
  12. package/lib/aptos/move-coder.d.ts +6 -6
  13. package/lib/aptos/move-coder.js +12 -6
  14. package/lib/aptos/move-coder.js.map +1 -1
  15. package/lib/move/abstract-codegen.d.ts +6 -6
  16. package/lib/move/abstract-codegen.js.map +1 -1
  17. package/lib/move/abstract-move-coder.d.ts +15 -20
  18. package/lib/move/abstract-move-coder.js +68 -51
  19. package/lib/move/abstract-move-coder.js.map +1 -1
  20. package/lib/move/chain-adapter.d.ts +3 -1
  21. package/lib/move/chain-adapter.js.map +1 -1
  22. package/lib/move/types.d.ts +6 -0
  23. package/lib/move/types.js +20 -0
  24. package/lib/move/types.js.map +1 -1
  25. package/lib/sui/builtin/0x2.d.ts +1 -4
  26. package/lib/sui/builtin/0x2.js +0 -5
  27. package/lib/sui/builtin/0x2.js.map +1 -1
  28. package/lib/sui/codegen/codegen.js +3 -0
  29. package/lib/sui/codegen/codegen.js.map +1 -1
  30. package/lib/sui/context.d.ts +1 -0
  31. package/lib/sui/context.js +2 -0
  32. package/lib/sui/context.js.map +1 -1
  33. package/lib/sui/move-coder.d.ts +9 -9
  34. package/lib/sui/move-coder.js +19 -28
  35. package/lib/sui/move-coder.js.map +1 -1
  36. package/lib/sui/sui-chain-adapter.d.ts +4 -2
  37. package/lib/sui/sui-chain-adapter.js +22 -1
  38. package/lib/sui/sui-chain-adapter.js.map +1 -1
  39. package/lib/sui/sui-processor.d.ts +4 -3
  40. package/lib/sui/sui-processor.js +2 -2
  41. package/lib/sui/sui-processor.js.map +1 -1
  42. package/package.json +4 -4
  43. package/src/aptos/aptos-chain-adapter.ts +13 -2
  44. package/src/aptos/aptos-processor.ts +8 -7
  45. package/src/aptos/codegen/codegen.ts +2 -2
  46. package/src/aptos/ext/aptos-dex.ts +4 -1
  47. package/src/aptos/module-client.ts +1 -1
  48. package/src/aptos/move-coder.ts +20 -12
  49. package/src/move/abstract-codegen.ts +6 -6
  50. package/src/move/abstract-move-coder.ts +77 -67
  51. package/src/move/chain-adapter.ts +4 -1
  52. package/src/move/types.ts +21 -0
  53. package/src/sui/builtin/0x2.ts +1 -4
  54. package/src/sui/codegen/codegen.ts +5 -2
  55. package/src/sui/context.ts +2 -0
  56. package/src/sui/move-coder.ts +34 -42
  57. package/src/sui/sui-chain-adapter.ts +32 -2
  58. package/src/sui/sui-processor.ts +6 -5
@@ -1,36 +1,31 @@
1
1
  import { TypeDescriptor } from './types.js';
2
2
  import { InternalMoveFunction, InternalMoveModule, InternalMoveStruct } from './internal-models.js';
3
3
  import { ChainAdapter } from './chain-adapter.js';
4
- export type StructWithTag<Base> = {
5
- type: string;
6
- data: Base;
7
- };
8
- export type DecodedStructWithTag<B, T> = StructWithTag<B> & {
4
+ export type DecodedStructWithTag<B, T> = B & {
9
5
  data_decoded: T;
10
6
  type_arguments: string[];
11
7
  };
12
8
  export declare abstract class AbstractMoveCoder<Network, ModuleType, StructType> {
13
- private moduleMapping;
9
+ protected moduleMapping: Map<string, InternalMoveModule>;
14
10
  private typeMapping;
15
11
  private funcMapping;
16
12
  network: Network;
17
- adapter: ChainAdapter<Network, ModuleType>;
13
+ adapter: ChainAdapter<Network, ModuleType, StructType>;
18
14
  protected constructor(network: Network);
19
15
  contains(account: string, name: string): boolean;
20
- protected toStructWithTag(val: StructType): StructWithTag<StructType>;
16
+ abstract load(module: ModuleType): InternalMoveModule;
21
17
  protected loadInternal(module: InternalMoveModule): void;
22
18
  protected decodeBigInt(data: any): bigint;
23
19
  protected encodeBigInt(data: bigint): any;
24
- getMoveStruct(type: string): InternalMoveStruct;
25
- getMoveFunction(type: string): InternalMoveFunction;
26
- decode(data: any, type: TypeDescriptor): any;
27
- encode(data: any, type: TypeDescriptor): any;
28
- decodeArray(entries: any[], types: TypeDescriptor[]): any[];
29
- encodeArray(entriesDecoded: any[], types: TypeDescriptor[]): any[];
30
- encodeCallArgs(args: any[], func: string): any[];
31
- decodeCallResult(res: any[], func: string): any[];
32
- protected filterAndDecodeStruct(typeQname: string, structs: StructType[]): any;
33
- private filterAndDecodeStructWithTags;
34
- protected decodedStruct<T>(typeStruct: StructType): DecodedStructWithTag<StructType, T> | undefined;
35
- private decodedStructWithTag;
20
+ private requestMap;
21
+ getMoveStruct(type: string): Promise<InternalMoveStruct>;
22
+ getMoveFunction(type: string): Promise<InternalMoveFunction>;
23
+ decode(data: any, type: TypeDescriptor): Promise<any>;
24
+ encode(data: any, type: TypeDescriptor): Promise<any>;
25
+ decodeArray(entries: any[], types: TypeDescriptor[]): Promise<any[]>;
26
+ encodeArray(entriesDecoded: any[], types: TypeDescriptor[]): Promise<any[]>;
27
+ encodeCallArgs(args: any[], func: string): Promise<any[]>;
28
+ decodeCallResult(res: any[], func: string): Promise<any[]>;
29
+ protected filterAndDecodeStruct<T>(typeMatcher: string, structsWithTags: StructType[]): Promise<DecodedStructWithTag<StructType, T>[]>;
30
+ protected decodedStruct<T>(typeStruct: StructType): Promise<DecodedStructWithTag<StructType, T> | undefined>;
36
31
  }
@@ -1,5 +1,5 @@
1
1
  import { moduleQname, SPLITTER, VECTOR_STR } from './utils.js';
2
- import { parseMoveType } from './types.js';
2
+ import { matchType, parseMoveType } from './types.js';
3
3
  import { bytesToBigInt } from '../utils/index.js';
4
4
  export class AbstractMoveCoder {
5
5
  moduleMapping = new Map();
@@ -13,10 +13,6 @@ export class AbstractMoveCoder {
13
13
  contains(account, name) {
14
14
  return this.moduleMapping.has(account + '::' + name);
15
15
  }
16
- // protected abstract getMeaningfulFunctionParams(params: TypeDescriptor[]): TypeDescriptor[]
17
- toStructWithTag(val) {
18
- return val;
19
- }
20
16
  loadInternal(module) {
21
17
  if (this.contains(module.address, module.name)) {
22
18
  return;
@@ -48,21 +44,50 @@ export class AbstractMoveCoder {
48
44
  encodeBigInt(data) {
49
45
  return '0x' + data.toString(16);
50
46
  }
51
- getMoveStruct(type) {
52
- const struct = this.typeMapping.get(type);
53
- if (!struct) {
54
- throw new Error('Failed to load type' + type + ' type are not imported anywhere');
47
+ requestMap = new Map();
48
+ async getMoveStruct(type) {
49
+ let struct = this.typeMapping.get(type);
50
+ if (struct) {
51
+ return struct;
52
+ }
53
+ const [account, module, typeName] = type.split(SPLITTER);
54
+ const key = account + SPLITTER + module;
55
+ let resp = this.requestMap.get(account + SPLITTER + module);
56
+ if (!resp) {
57
+ resp = this.adapter.fetchModule(account, module, this.network).then((m) => {
58
+ return this.load(m);
59
+ });
60
+ this.requestMap.set(key, resp);
61
+ }
62
+ await resp;
63
+ struct = this.typeMapping.get(type);
64
+ if (struct) {
65
+ return struct;
55
66
  }
56
- return struct;
67
+ throw new Error('Failed to load function ' + type + ' type are not imported anywhere');
57
68
  }
58
- getMoveFunction(type) {
59
- const func = this.funcMapping.get(type);
60
- if (!func) {
61
- throw new Error('Failed to load function ' + type + ' type are not imported anywhere');
69
+ async getMoveFunction(type) {
70
+ let func = this.funcMapping.get(type);
71
+ if (func) {
72
+ return func;
62
73
  }
63
- return func;
74
+ const [account, module, typeName] = type.split(SPLITTER);
75
+ const key = account + SPLITTER + module;
76
+ let resp = this.requestMap.get(account + SPLITTER + module);
77
+ if (!resp) {
78
+ resp = this.adapter.fetchModule(account, module, this.network).then((m) => {
79
+ return this.load(m);
80
+ });
81
+ this.requestMap.set(key, resp);
82
+ }
83
+ await resp;
84
+ func = this.funcMapping.get(type);
85
+ if (func) {
86
+ return func;
87
+ }
88
+ throw new Error('Failed to load function ' + type + ' type are not imported anywhere');
64
89
  }
65
- decode(data, type) {
90
+ async decode(data, type) {
66
91
  // process simple type
67
92
  if (type.reference) {
68
93
  return data;
@@ -95,12 +120,12 @@ export class AbstractMoveCoder {
95
120
  }
96
121
  const res = [];
97
122
  for (const entry of data) {
98
- res.push(this.decode(entry, type.typeArgs[0]));
123
+ res.push(await this.decode(entry, type.typeArgs[0]));
99
124
  }
100
125
  return res;
101
126
  }
102
127
  // Process complex type
103
- const struct = this.getMoveStruct(type.qname);
128
+ const struct = await this.getMoveStruct(type.qname);
104
129
  const typeCtx = new Map();
105
130
  for (const [idx, typeArg] of type.typeArgs.entries()) {
106
131
  typeCtx.set('T' + idx, typeArg);
@@ -109,12 +134,13 @@ export class AbstractMoveCoder {
109
134
  for (const field of struct.fields) {
110
135
  let filedType = field.type;
111
136
  filedType = filedType.applyTypeArgs(typeCtx);
112
- const value = this.decode(data[field.name], filedType);
137
+ const fieldValue = this.adapter.getData(data)[field.name];
138
+ const value = await this.decode(fieldValue, filedType);
113
139
  typedData[field.name] = value;
114
140
  }
115
141
  return typedData;
116
142
  }
117
- encode(data, type) {
143
+ async encode(data, type) {
118
144
  // process simple type
119
145
  if (type.reference) {
120
146
  return data;
@@ -156,7 +182,7 @@ export class AbstractMoveCoder {
156
182
  return res;
157
183
  }
158
184
  // Process complex type
159
- const struct = this.getMoveStruct(type.qname);
185
+ const struct = await this.getMoveStruct(type.qname);
160
186
  const typeCtx = new Map();
161
187
  for (const [idx, typeArg] of type.typeArgs.entries()) {
162
188
  typeCtx.set('T' + idx, typeArg);
@@ -165,18 +191,18 @@ export class AbstractMoveCoder {
165
191
  for (const field of struct.fields) {
166
192
  let filedType = field.type;
167
193
  filedType = filedType.applyTypeArgs(typeCtx);
168
- const value = this.encode(data[field.name], filedType);
194
+ const value = await this.encode(data[field.name], filedType);
169
195
  typedData[field.name] = value;
170
196
  }
171
197
  return typedData;
172
198
  }
173
- decodeArray(entries, types) {
199
+ async decodeArray(entries, types) {
174
200
  const entriesDecoded = [];
175
201
  for (const [idx, arg] of entries.entries()) {
176
202
  // TODO consider apply payload.type_arguments, but this might be hard since we don't code gen for them
177
203
  const argType = types[idx];
178
204
  try {
179
- entriesDecoded.push(this.decode(arg, argType));
205
+ entriesDecoded.push(await this.decode(arg, argType));
180
206
  }
181
207
  catch (e) {
182
208
  console.error(e, 'Decoding error for ', JSON.stringify(arg), 'using type', argType);
@@ -185,13 +211,13 @@ export class AbstractMoveCoder {
185
211
  }
186
212
  return entriesDecoded;
187
213
  }
188
- encodeArray(entriesDecoded, types) {
214
+ async encodeArray(entriesDecoded, types) {
189
215
  const entries = [];
190
216
  for (const [idx, arg] of entriesDecoded.entries()) {
191
217
  // TODO consider apply payload.type_arguments, but this might be hard since we don't code gen for them
192
218
  const argType = types[idx];
193
219
  try {
194
- entries.push(this.encode(arg, argType));
220
+ entries.push(await this.encode(arg, argType));
195
221
  }
196
222
  catch (e) {
197
223
  throw Error('Decoding error for ' + JSON.stringify(arg) + 'using type' + argType + e.toString());
@@ -199,51 +225,42 @@ export class AbstractMoveCoder {
199
225
  }
200
226
  return entries;
201
227
  }
202
- encodeCallArgs(args, func) {
203
- const f = this.getMoveFunction(func);
228
+ async encodeCallArgs(args, func) {
229
+ const f = await this.getMoveFunction(func);
204
230
  return this.encodeArray(args, this.adapter.getMeaningfulFunctionParams(f.params));
205
231
  }
206
- decodeCallResult(res, func) {
207
- const f = this.getMoveFunction(func);
232
+ async decodeCallResult(res, func) {
233
+ const f = await this.getMoveFunction(func);
208
234
  return this.decodeArray(res, f.return);
209
235
  }
210
- filterAndDecodeStruct(typeQname, structs) {
211
- if (!structs) {
212
- return [];
213
- }
214
- const withTags = structs.map((s) => this.toStructWithTag(s));
215
- return this.filterAndDecodeStructWithTags(typeQname, withTags);
216
- }
217
- filterAndDecodeStructWithTags(typeQname, structsWithTags) {
236
+ async filterAndDecodeStruct(typeMatcher, structsWithTags) {
218
237
  if (!structsWithTags) {
219
238
  return [];
220
239
  }
240
+ const typeMatcherDescriptor = parseMoveType(typeMatcher);
221
241
  const results = [];
222
242
  for (const resource of structsWithTags) {
223
- if (typeQname.includes('<')) {
224
- if (resource.type !== typeQname) {
225
- continue;
226
- }
227
- }
228
- else if (resource.type.split('<')[0] !== typeQname) {
243
+ const resourceType = this.adapter.getType(resource);
244
+ const resourceTypeDescriptor = parseMoveType(resourceType);
245
+ if (!matchType(typeMatcherDescriptor, resourceTypeDescriptor)) {
229
246
  continue;
230
247
  }
231
- const result = this.decodedStructWithTag(resource);
248
+ const result = await this.decodedStruct(resource);
232
249
  if (result) {
233
250
  results.push(result);
234
251
  }
252
+ else {
253
+ console.log('');
254
+ }
235
255
  }
236
256
  return results;
237
257
  }
238
- decodedStruct(typeStruct) {
239
- return this.decodedStructWithTag(this.toStructWithTag(typeStruct));
240
- }
241
- decodedStructWithTag(typeStruct) {
242
- const typeDescriptor = parseMoveType(typeStruct.type);
258
+ async decodedStruct(typeStruct) {
259
+ const typeDescriptor = parseMoveType(this.adapter.getType(typeStruct));
243
260
  const typeArguments = typeDescriptor.typeArgs.map((t) => t.getSignature());
244
261
  let dataTyped = undefined;
245
262
  try {
246
- dataTyped = this.decode(this.toStructWithTag(typeStruct.data), typeDescriptor);
263
+ dataTyped = await this.decode(typeStruct, typeDescriptor);
247
264
  }
248
265
  catch (e) {
249
266
  console.error('Decoding error for ', JSON.stringify(typeStruct), e);
@@ -1 +1 @@
1
- {"version":3,"file":"abstract-move-coder.js","sourceRoot":"","sources":["../../src/move/abstract-move-coder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AAC9D,OAAO,EAAE,aAAa,EAAkB,MAAM,YAAY,CAAA;AAE1D,OAAO,EAAE,aAAa,EAAE,MAAM,mBAA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AqC;QAErC,MAAM,cAAc,GAAG,aAAa,CAAC,UAAU,CAAC,IAAI,CAAC,CAAA;QACrD,MAAM,aAAa,GAAG,cAAc,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,YAAY,EAAE,CAAC,CAAA;QAE1E,IAAI,SAAS,GAAG,SAAS,CAAA;QACzB,IAAI;YACF,SAAS,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,eAAe,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,cAAc,CAAC,CAAA;SAC/E;QAAC,OAAO,CAAC,EAAE;YACV,OAAO,CAAC,KAAK,CAAC,qBAAqB,EAAE,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC,CAAA;YACnE,OAAO,SAAS,CAAA;SACjB;QACD,OAAO;YACL,GAAG,UAAU;YACb,YAAY,EAAE,SAAS;YACvB,cAAc,EAAE,aAAa;SACS,CAAA;IAC1C,CAAC;CACF","sourcesContent":["import { moduleQname, SPLITTER, VECTOR_STR } from './utils.js'\nimport { parseMoveType, TypeDescriptor } from './types.js'\nimport { InternalMoveFunction, InternalMoveModule, InternalMoveStruct } from './internal-models.js'\nimport { bytesToBigInt } from '../utils/index.js'\nimport { ChainAdapter } from './chain-adapter.js'\n\nexport type StructWithTag<Base> = {\n type: string\n data: Base\n}\n\nexport type DecodedStructWithTag<B, T> = StructWithTag<B> & {\n data_decoded: T\n type_arguments: string[]\n}\n\nexport abstract class AbstractMoveCoder<Network, ModuleType, StructType> {\n private moduleMapping = new Map<string, InternalMoveModule>()\n private typeMapping = new Map<string, InternalMoveStruct>()\n private funcMapping = new Map<string, InternalMoveFunction>()\n network: Network\n adapter: ChainAdapter<Network, ModuleType>\n\n protected constructor(network: Network) {\n this.network = network\n }\n\n contains(account: string, name: string) {\n return this.moduleMapping.has(account + '::' + name)\n }\n\n // protected abstract getMeaningfulFunctionParams(params: TypeDescriptor[]): TypeDescriptor[]\n\n protected toStructWithTag(val: StructType): StructWithTag<StructType> {\n return val as any\n }\n\n protected loadInternal(module: InternalMoveModule) {\n if (this.contains(module.address, module.name)) {\n return\n }\n this.moduleMapping.set(moduleQname(module), module)\n\n for (const struct of module.structs) {\n // TODO move to util\n const key = [module.address, module.name, struct.name].join(SPLITTER)\n this.typeMapping.set(key, struct)\n }\n\n for (const func of module.exposedFunctions) {\n // if (!func.isEntry) {\n // continue\n // }\n const key = [module.address, module.name, func.name].join(SPLITTER)\n this.funcMapping.set(key, func)\n }\n }\n\n protected decodeBigInt(data: any): bigint {\n if (Array.isArray(data)) {\n // Only sui function need this, strange\n const bytes = data as number[]\n return bytesToBigInt(new Uint8Array(bytes.slice().reverse()))\n } else {\n return BigInt(data)\n }\n }\n\n protected encodeBigInt(data: bigint): any {\n return '0x' + data.toString(16)\n }\n\n getMoveStruct(type: string): InternalMoveStruct {\n const struct = this.typeMapping.get(type)\n if (!struct) {\n throw new Error('Failed to load type' + type + ' type are not imported anywhere')\n }\n return struct\n }\n\n getMoveFunction(type: string): InternalMoveFunction {\n const func = this.funcMapping.get(type)\n if (!func) {\n throw new Error('Failed to load function ' + type + ' type are not imported anywhere')\n }\n return func\n }\n\n decode(data: any, type: TypeDescriptor): any {\n // process simple type\n if (type.reference) {\n return data\n }\n switch (type.qname) {\n case 'signer': // TODO check this, aptos only\n case 'address':\n case 'Address':\n case '0x1::string::String':\n case 'bool':\n case 'Bool':\n case 'u8':\n case 'U8':\n case 'u16':\n case 'U16':\n case 'u32':\n case 'U32':\n return data\n case 'u64':\n case 'U64':\n case 'u128':\n case 'U128':\n return this.decodeBigInt(data)\n }\n\n // process vector\n if (type.qname === VECTOR_STR) {\n // vector<u8> as hex string\n if (type.typeArgs[0].qname === 'u8' || type.typeArgs[0].qname === 'U8') {\n return data\n }\n\n const res = []\n for (const entry of data) {\n res.push(this.decode(entry, type.typeArgs[0]))\n }\n return res\n }\n\n // Process complex type\n const struct = this.getMoveStruct(type.qname)\n\n const typeCtx = new Map<string, TypeDescriptor>()\n for (const [idx, typeArg] of type.typeArgs.entries()) {\n typeCtx.set('T' + idx, typeArg)\n }\n\n const typedData: any = {}\n\n for (const field of struct.fields) {\n let filedType = field.type\n filedType = filedType.applyTypeArgs(typeCtx)\n const value = this.decode(data[field.name], filedType)\n typedData[field.name] = value\n }\n return typedData\n }\n\n encode(data: any, type: TypeDescriptor): any {\n // process simple type\n if (type.reference) {\n return data\n }\n switch (type.qname) {\n case 'signer': // TODO check this, aptos only\n case 'address':\n case 'Address':\n case '0x2::object::ID':\n case '0x2::coin::Coin':\n case '0x1::string::String':\n case 'bool':\n case 'Bool':\n case 'u8':\n case 'U8':\n case 'u16':\n case 'U16':\n case 'u32':\n case 'U32':\n return data\n case 'u64':\n case 'U64':\n case 'u128':\n case 'U128':\n case 'u256':\n case 'U256':\n return this.encodeBigInt(data)\n }\n\n // process vector\n if (type.qname === VECTOR_STR) {\n // vector<u8> as hex string\n if (type.typeArgs[0].qname === 'u8' || type.typeArgs[0].qname === 'U8') {\n return data\n }\n\n const res = []\n for (const entry of data) {\n res.push(this.encode(entry, type.typeArgs[0]))\n }\n return res\n }\n\n // Process complex type\n const struct = this.getMoveStruct(type.qname)\n\n const typeCtx = new Map<string, TypeDescriptor>()\n for (const [idx, typeArg] of type.typeArgs.entries()) {\n typeCtx.set('T' + idx, typeArg)\n }\n\n const typedData: any = {}\n\n for (const field of struct.fields) {\n let filedType = field.type\n filedType = filedType.applyTypeArgs(typeCtx)\n const value = this.encode(data[field.name], filedType)\n typedData[field.name] = value\n }\n return typedData\n }\n\n decodeArray(entries: any[], types: TypeDescriptor[]): any[] {\n const entriesDecoded: any[] = []\n for (const [idx, arg] of entries.entries()) {\n // TODO consider apply payload.type_arguments, but this might be hard since we don't code gen for them\n const argType = types[idx]\n try {\n entriesDecoded.push(this.decode(arg, argType))\n } catch (e) {\n console.error(e, 'Decoding error for ', JSON.stringify(arg), 'using type', argType)\n return entries\n }\n }\n return entriesDecoded\n }\n\n encodeArray(entriesDecoded: any[], types: TypeDescriptor[]): any[] {\n const entries: any[] = []\n for (const [idx, arg] of entriesDecoded.entries()) {\n // TODO consider apply payload.type_arguments, but this might be hard since we don't code gen for them\n const argType = types[idx]\n try {\n entries.push(this.encode(arg, argType))\n } catch (e) {\n throw Error('Decoding error for ' + JSON.stringify(arg) + 'using type' + argType + e.toString())\n }\n }\n return entries\n }\n\n encodeCallArgs(args: any[], func: string): any[] {\n const f = this.getMoveFunction(func)\n return this.encodeArray(args, this.adapter.getMeaningfulFunctionParams(f.params))\n }\n\n decodeCallResult(res: any[], func: string): any[] {\n const f = this.getMoveFunction(func)\n return this.decodeArray(res, f.return)\n }\n\n protected filterAndDecodeStruct(typeQname: string, structs: StructType[]): any {\n if (!structs) {\n return []\n }\n const withTags = structs.map((s) => this.toStructWithTag(s))\n return this.filterAndDecodeStructWithTags(typeQname, withTags)\n }\n\n private filterAndDecodeStructWithTags<T>(\n typeQname: string,\n structsWithTags: StructWithTag<StructType>[]\n ): DecodedStructWithTag<StructType, T>[] {\n if (!structsWithTags) {\n return []\n }\n const results: DecodedStructWithTag<StructType, T>[] = []\n for (const resource of structsWithTags) {\n if (typeQname.includes('<')) {\n if (resource.type !== typeQname) {\n continue\n }\n } else if (resource.type.split('<')[0] !== typeQname) {\n continue\n }\n const result = this.decodedStructWithTag(resource)\n if (result) {\n results.push(result as DecodedStructWithTag<StructType, T>)\n }\n }\n return results\n }\n\n protected decodedStruct<T>(typeStruct: StructType): DecodedStructWithTag<StructType, T> | undefined {\n return this.decodedStructWithTag(this.toStructWithTag(typeStruct)) as any\n }\n\n private decodedStructWithTag<T>(\n typeStruct: StructWithTag<StructType>\n ): DecodedStructWithTag<StructType, T> | undefined {\n const typeDescriptor = parseMoveType(typeStruct.type)\n const typeArguments = typeDescriptor.typeArgs.map((t) => t.getSignature())\n\n let dataTyped = undefined\n try {\n dataTyped = this.decode(this.toStructWithTag(typeStruct.data), typeDescriptor)\n } catch (e) {\n console.error('Decoding error for ', JSON.stringify(typeStruct), e)\n return undefined\n }\n return {\n ...typeStruct,\n data_decoded: dataTyped,\n type_arguments: typeArguments,\n } as DecodedStructWithTag<StructType, T>\n }\n}\n"]}
1
+ {"version":3,"file":"abstract-move-coder.js","sourceRoot":"","sources":["../../src/move/abstract-move-coder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AAC9D,OAAO,EAAE,SAAS,EAAE,aAAa,EAAkB,MAAM,YAAY,CAAA;AAErE,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAA;AAQjD,MAAM,OAAgB,iBAAiB;IAC3B,aAAa,GAAG,IAAI,GAAG,EAA8B,CAAA;IACvD,WAAW,GAAG,IAAI,GAAG,EAA8B,CAAA;IACnD,WAAW,GAAG,IAAI,GAAG,EAAgC,CAAA;IAC7D,OAAO,CAAS;IAChB,OAAO,CAA+C;IAEtD,YAAsB,OAAgB;QACpC,IAAI,CAAC,OAAO,GAAG,OAAO,CAAA;IACxB,CAAC;IAED,QAAQ,CAAC,OAAe,EAAE,IAAY;QACpC,OAAO,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,OAAO,GAAG,IAAI,GAAG,IAAI,CAAC,CAAA;IACtD,CAAC;IAIS,YAAY,CAAC,MAA0B;QAC/C,IAAI,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,OAAO,EAAE,MAAM,CAAC,IAAI,CAAC,EAAE;YAC9C,OAAM;SACP;QACD,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,WAAW,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC,CAAA;QAEnD,KAAK,MAAM,MAAM,IAAI,MAAM,CAAC,OAAO,EAAE;YACnC,oBAAoB;YACpB,MAAM,GAAG,GAAG,CAAC,MAAM,CAAC,OAAO,EAAE,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,QAAQ,CA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AI,eAAe,EAAE;YACtC,MAAM,YAAY,GAAG,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAA;YACnD,MAAM,sBAAsB,GAAG,aAAa,CAAC,YAAY,CAAC,CAAA;YAC1D,IAAI,CAAC,SAAS,CAAC,qBAAqB,EAAE,sBAAsB,CAAC,EAAE;gBAC7D,SAAQ;aACT;YAED,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAA;YACjD,IAAI,MAAM,EAAE;gBACV,OAAO,CAAC,IAAI,CAAC,MAA6C,CAAC,CAAA;aAC5D;iBAAM;gBACL,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC,CAAA;aAChB;SACF;QACD,OAAO,OAAO,CAAA;IAChB,CAAC;IAES,KAAK,CAAC,aAAa,CAAI,UAAsB;QACrD,MAAM,cAAc,GAAG,aAAa,CAAC,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC,CAAA;QACtE,MAAM,aAAa,GAAG,cAAc,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,YAAY,EAAE,CAAC,CAAA;QAE1E,IAAI,SAAS,GAAG,SAAS,CAAA;QACzB,IAAI;YACF,SAAS,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,UAAU,EAAE,cAAc,CAAC,CAAA;SAC1D;QAAC,OAAO,CAAC,EAAE;YACV,OAAO,CAAC,KAAK,CAAC,qBAAqB,EAAE,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC,CAAA;YACnE,OAAO,SAAS,CAAA;SACjB;QACD,OAAO;YACL,GAAG,UAAU;YACb,YAAY,EAAE,SAAS;YACvB,cAAc,EAAE,aAAa;SACS,CAAA;IAC1C,CAAC;CACF","sourcesContent":["import { moduleQname, SPLITTER, VECTOR_STR } from './utils.js'\nimport { matchType, parseMoveType, TypeDescriptor } from './types.js'\nimport { InternalMoveFunction, InternalMoveModule, InternalMoveStruct } from './internal-models.js'\nimport { bytesToBigInt } from '../utils/index.js'\nimport { ChainAdapter } from './chain-adapter.js'\n\nexport type DecodedStructWithTag<B, T> = B & {\n data_decoded: T\n type_arguments: string[]\n}\n\nexport abstract class AbstractMoveCoder<Network, ModuleType, StructType> {\n protected moduleMapping = new Map<string, InternalMoveModule>()\n private typeMapping = new Map<string, InternalMoveStruct>()\n private funcMapping = new Map<string, InternalMoveFunction>()\n network: Network\n adapter: ChainAdapter<Network, ModuleType, StructType>\n\n protected constructor(network: Network) {\n this.network = network\n }\n\n contains(account: string, name: string) {\n return this.moduleMapping.has(account + '::' + name)\n }\n\n abstract load(module: ModuleType): InternalMoveModule\n\n protected loadInternal(module: InternalMoveModule) {\n if (this.contains(module.address, module.name)) {\n return\n }\n this.moduleMapping.set(moduleQname(module), module)\n\n for (const struct of module.structs) {\n // TODO move to util\n const key = [module.address, module.name, struct.name].join(SPLITTER)\n this.typeMapping.set(key, struct)\n }\n\n for (const func of module.exposedFunctions) {\n // if (!func.isEntry) {\n // continue\n // }\n const key = [module.address, module.name, func.name].join(SPLITTER)\n this.funcMapping.set(key, func)\n }\n }\n\n protected decodeBigInt(data: any): bigint {\n if (Array.isArray(data)) {\n // Only sui function need this, strange\n const bytes = data as number[]\n return bytesToBigInt(new Uint8Array(bytes.slice().reverse()))\n } else {\n return BigInt(data)\n }\n }\n\n protected encodeBigInt(data: bigint): any {\n return '0x' + data.toString(16)\n }\n\n private requestMap = new Map<string, Promise<InternalMoveModule>>()\n\n async getMoveStruct(type: string): Promise<InternalMoveStruct> {\n let struct = this.typeMapping.get(type)\n if (struct) {\n return struct\n }\n const [account, module, typeName] = type.split(SPLITTER)\n const key = account + SPLITTER + module\n let resp = this.requestMap.get(account + SPLITTER + module)\n if (!resp) {\n resp = this.adapter.fetchModule(account, module, this.network).then((m) => {\n return this.load(m)\n })\n this.requestMap.set(key, resp)\n }\n await resp\n struct = this.typeMapping.get(type)\n if (struct) {\n return struct\n }\n throw new Error('Failed to load function ' + type + ' type are not imported anywhere')\n }\n\n async getMoveFunction(type: string): Promise<InternalMoveFunction> {\n let func = this.funcMapping.get(type)\n if (func) {\n return func\n }\n const [account, module, typeName] = type.split(SPLITTER)\n const key = account + SPLITTER + module\n let resp = this.requestMap.get(account + SPLITTER + module)\n if (!resp) {\n resp = this.adapter.fetchModule(account, module, this.network).then((m) => {\n return this.load(m)\n })\n this.requestMap.set(key, resp)\n }\n await resp\n func = this.funcMapping.get(type)\n if (func) {\n return func\n }\n throw new Error('Failed to load function ' + type + ' type are not imported anywhere')\n }\n\n async decode(data: any, type: TypeDescriptor): Promise<any> {\n // process simple type\n if (type.reference) {\n return data\n }\n switch (type.qname) {\n case 'signer': // TODO check this, aptos only\n case 'address':\n case 'Address':\n case '0x1::string::String':\n case 'bool':\n case 'Bool':\n case 'u8':\n case 'U8':\n case 'u16':\n case 'U16':\n case 'u32':\n case 'U32':\n return data\n case 'u64':\n case 'U64':\n case 'u128':\n case 'U128':\n return this.decodeBigInt(data)\n }\n\n // process vector\n if (type.qname === VECTOR_STR) {\n // vector<u8> as hex string\n if (type.typeArgs[0].qname === 'u8' || type.typeArgs[0].qname === 'U8') {\n return data\n }\n\n const res = []\n for (const entry of data) {\n res.push(await this.decode(entry, type.typeArgs[0]))\n }\n return res\n }\n\n // Process complex type\n const struct = await this.getMoveStruct(type.qname)\n\n const typeCtx = new Map<string, TypeDescriptor>()\n for (const [idx, typeArg] of type.typeArgs.entries()) {\n typeCtx.set('T' + idx, typeArg)\n }\n\n const typedData: any = {}\n\n for (const field of struct.fields) {\n let filedType = field.type\n filedType = filedType.applyTypeArgs(typeCtx)\n const fieldValue = this.adapter.getData(data)[field.name]\n const value = await this.decode(fieldValue, filedType)\n typedData[field.name] = value\n }\n return typedData\n }\n\n async encode(data: any, type: TypeDescriptor): Promise<any> {\n // process simple type\n if (type.reference) {\n return data\n }\n switch (type.qname) {\n case 'signer': // TODO check this, aptos only\n case 'address':\n case 'Address':\n case '0x2::object::ID':\n case '0x2::coin::Coin':\n case '0x1::string::String':\n case 'bool':\n case 'Bool':\n case 'u8':\n case 'U8':\n case 'u16':\n case 'U16':\n case 'u32':\n case 'U32':\n return data\n case 'u64':\n case 'U64':\n case 'u128':\n case 'U128':\n case 'u256':\n case 'U256':\n return this.encodeBigInt(data)\n }\n\n // process vector\n if (type.qname === VECTOR_STR) {\n // vector<u8> as hex string\n if (type.typeArgs[0].qname === 'u8' || type.typeArgs[0].qname === 'U8') {\n return data\n }\n\n const res = []\n for (const entry of data) {\n res.push(this.encode(entry, type.typeArgs[0]))\n }\n return res\n }\n\n // Process complex type\n const struct = await this.getMoveStruct(type.qname)\n\n const typeCtx = new Map<string, TypeDescriptor>()\n for (const [idx, typeArg] of type.typeArgs.entries()) {\n typeCtx.set('T' + idx, typeArg)\n }\n\n const typedData: any = {}\n\n for (const field of struct.fields) {\n let filedType = field.type\n filedType = filedType.applyTypeArgs(typeCtx)\n const value = await this.encode(data[field.name], filedType)\n typedData[field.name] = value\n }\n return typedData\n }\n\n async decodeArray(entries: any[], types: TypeDescriptor[]): Promise<any[]> {\n const entriesDecoded: any[] = []\n for (const [idx, arg] of entries.entries()) {\n // TODO consider apply payload.type_arguments, but this might be hard since we don't code gen for them\n const argType = types[idx]\n try {\n entriesDecoded.push(await this.decode(arg, argType))\n } catch (e) {\n console.error(e, 'Decoding error for ', JSON.stringify(arg), 'using type', argType)\n return entries\n }\n }\n return entriesDecoded\n }\n\n async encodeArray(entriesDecoded: any[], types: TypeDescriptor[]): Promise<any[]> {\n const entries: any[] = []\n for (const [idx, arg] of entriesDecoded.entries()) {\n // TODO consider apply payload.type_arguments, but this might be hard since we don't code gen for them\n const argType = types[idx]\n try {\n entries.push(await this.encode(arg, argType))\n } catch (e) {\n throw Error('Decoding error for ' + JSON.stringify(arg) + 'using type' + argType + e.toString())\n }\n }\n return entries\n }\n\n async encodeCallArgs(args: any[], func: string): Promise<any[]> {\n const f = await this.getMoveFunction(func)\n return this.encodeArray(args, this.adapter.getMeaningfulFunctionParams(f.params))\n }\n\n async decodeCallResult(res: any[], func: string): Promise<any[]> {\n const f = await this.getMoveFunction(func)\n return this.decodeArray(res, f.return)\n }\n\n protected async filterAndDecodeStruct<T>(\n typeMatcher: string,\n structsWithTags: StructType[]\n ): Promise<DecodedStructWithTag<StructType, T>[]> {\n if (!structsWithTags) {\n return [] as any\n }\n const typeMatcherDescriptor = parseMoveType(typeMatcher)\n const results: DecodedStructWithTag<StructType, T>[] = []\n for (const resource of structsWithTags) {\n const resourceType = this.adapter.getType(resource)\n const resourceTypeDescriptor = parseMoveType(resourceType)\n if (!matchType(typeMatcherDescriptor, resourceTypeDescriptor)) {\n continue\n }\n\n const result = await this.decodedStruct(resource)\n if (result) {\n results.push(result as DecodedStructWithTag<StructType, T>)\n } else {\n console.log('')\n }\n }\n return results\n }\n\n protected async decodedStruct<T>(typeStruct: StructType): Promise<DecodedStructWithTag<StructType, T> | undefined> {\n const typeDescriptor = parseMoveType(this.adapter.getType(typeStruct))\n const typeArguments = typeDescriptor.typeArgs.map((t) => t.getSignature())\n\n let dataTyped = undefined\n try {\n dataTyped = await this.decode(typeStruct, typeDescriptor)\n } catch (e) {\n console.error('Decoding error for ', JSON.stringify(typeStruct), e)\n return undefined\n }\n return {\n ...typeStruct,\n data_decoded: dataTyped,\n type_arguments: typeArguments,\n } as DecodedStructWithTag<StructType, T>\n }\n}\n"]}
@@ -1,9 +1,11 @@
1
1
  import { InternalMoveModule, InternalMoveStruct } from './internal-models.js';
2
2
  import { TypeDescriptor } from './types.js';
3
- export declare abstract class ChainAdapter<NetworkType, ModuleType> {
3
+ export declare abstract class ChainAdapter<NetworkType, ModuleType, StructType> {
4
4
  abstract fetchModule(account: string, module: string, network: NetworkType): Promise<ModuleType>;
5
5
  abstract fetchModules(account: string, network: NetworkType): Promise<ModuleType[]>;
6
6
  abstract toInternalModules(modules: ModuleType[]): InternalMoveModule[];
7
7
  abstract getEventStructs(module: InternalMoveModule): Map<string, InternalMoveStruct>;
8
8
  abstract getMeaningfulFunctionParams(params: TypeDescriptor[]): TypeDescriptor[];
9
+ abstract getType(base: StructType): string;
10
+ abstract getData<T>(base: StructType): any;
9
11
  }
@@ -1 +1 @@
1
- {"version":3,"file":"chain-adapter.js","sourceRoot":"","sources":["../../src/move/chain-adapter.ts"],"names":[],"mappings":"AAGA,MAAM,OAAgB,YAAY;CAUjC","sourcesContent":["import { InternalMoveModule, InternalMoveStruct } from './internal-models.js'\nimport { TypeDescriptor } from './types.js'\n\nexport abstract class ChainAdapter<NetworkType, ModuleType> {\n abstract fetchModule(account: string, module: string, network: NetworkType): Promise<ModuleType>\n\n abstract fetchModules(account: string, network: NetworkType): Promise<ModuleType[]>\n abstract toInternalModules(modules: ModuleType[]): InternalMoveModule[]\n // Get the structs that represent Events\n abstract getEventStructs(module: InternalMoveModule): Map<string, InternalMoveStruct>\n // Get the parameters that actually have arguments in runtime\n // Aptos first signer and Sui's last TxContext are no use\n abstract getMeaningfulFunctionParams(params: TypeDescriptor[]): TypeDescriptor[]\n}\n"]}
1
+ {"version":3,"file":"chain-adapter.js","sourceRoot":"","sources":["../../src/move/chain-adapter.ts"],"names":[],"mappings":"AAGA,MAAM,OAAgB,YAAY;CAajC","sourcesContent":["import { InternalMoveModule, InternalMoveStruct } from './internal-models.js'\nimport { TypeDescriptor } from './types.js'\n\nexport abstract class ChainAdapter<NetworkType, ModuleType, StructType> {\n abstract fetchModule(account: string, module: string, network: NetworkType): Promise<ModuleType>\n\n abstract fetchModules(account: string, network: NetworkType): Promise<ModuleType[]>\n abstract toInternalModules(modules: ModuleType[]): InternalMoveModule[]\n // Get the structs that represent Events\n abstract getEventStructs(module: InternalMoveModule): Map<string, InternalMoveStruct>\n // Get the parameters that actually have arguments in runtime\n // Aptos first signer and Sui's last TxContext are no use\n abstract getMeaningfulFunctionParams(params: TypeDescriptor[]): TypeDescriptor[]\n\n abstract getType(base: StructType): string\n abstract getData<T>(base: StructType): any\n}\n"]}
@@ -9,3 +9,9 @@ export declare class TypeDescriptor {
9
9
  dependedTypes(): string[];
10
10
  }
11
11
  export declare function parseMoveType(type: string): TypeDescriptor;
12
+ /**
13
+ *
14
+ * @param matcher
15
+ * @param type
16
+ */
17
+ export declare function matchType(matcher: TypeDescriptor, type: TypeDescriptor): boolean;
package/lib/move/types.js CHANGED
@@ -140,4 +140,24 @@ function adjustType(type) {
140
140
  type.qname = type.qname.slice(3);
141
141
  }
142
142
  }
143
+ /**
144
+ *
145
+ * @param matcher
146
+ * @param type
147
+ */
148
+ export function matchType(matcher, type) {
149
+ if (matcher.qname === 'any') {
150
+ return true;
151
+ }
152
+ if (matcher.qname !== type.qname) {
153
+ return false;
154
+ }
155
+ for (const [idx, matcherTarg] of matcher.typeArgs.entries()) {
156
+ const targ = type.typeArgs[idx];
157
+ if (!matchType(matcherTarg, targ)) {
158
+ return false;
159
+ }
160
+ }
161
+ return true;
162
+ }
143
163
  //# sourceMappingURL=types.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/move/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AAEjD,MAAM,OAAO,cAAc;IACzB,KAAK,CAAQ;IACb,SAAS,CAAS;IAClB,OAAO,CAAS;IAChB,QAAQ,CAAkB;IAE1B,YAAY,MAAc,EAAE,UAA6B;QACvD,IAAI,CAAC,KAAK,GAAG,MAAM,CAAA;QACnB,IAAI,CAAC,SAAS,GAAG,KAAK,CAAA;QACtB,IAAI,CAAC,OAAO,GAAG,KAAK,CAAA;QACpB,IAAI,CAAC,QAAQ,GAAG,UAAU,IAAI,EAAE,CAAA;IAClC,CAAC;IAED,YAAY;QACV,IAAI,IAAI,CAAC,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE;YAC5B,OAAO,IAAI,CAAC,KAAK,GAAG,GAAG,GAAG,IAAI,CAAC,QAAQ,CAAC,GAA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ED,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE;QACrB,KAAK,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC,CAAA;KAChD;IACD,UAAU,CAAC,KAAK,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAA;IAEnC,MAAM,GAAG,GAAG,KAAK,CAAC,GAAG,EAAE,CAAA;IACvB,IAAI,CAAC,GAAG,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE;QAC5B,MAAM,KAAK,CAAC,uBAAuB,CAAC,CAAA;KACrC;IACD,OAAO,GAAG,CAAA;AACZ,CAAC;AAED,SAAS,UAAU,CAAC,IAAoB;IACtC,IAAI,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE;QAC9B,IAAI,CAAC,SAAS,GAAG,IAAI,CAAA;QACrB,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAA;KACjC;IACD,IAAI,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,KAAK,CAAC,EAAE;QAChC,IAAI,CAAC,OAAO,GAAG,IAAI,CAAA;QACnB,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAA;KACjC;AACH,CAAC","sourcesContent":["import { SPLITTER, VECTOR_STR } from './utils.js'\n\nexport class TypeDescriptor {\n qname: string\n reference: boolean\n mutable: boolean\n typeArgs: TypeDescriptor[]\n\n constructor(symbol: string, typeParams?: TypeDescriptor[]) {\n this.qname = symbol\n this.reference = false\n this.mutable = false\n this.typeArgs = typeParams || []\n }\n\n getSignature(): string {\n if (this.typeArgs.length > 0) {\n return this.qname + '<' + this.typeArgs.map((t) => t.getSignature()).join(', ') + '>'\n }\n return this.qname\n }\n\n // Replace T0, T1 with more concrete type\n applyTypeArgs(ctx: Map<string, TypeDescriptor>): TypeDescriptor {\n const replace = ctx.get(this.qname)\n if (replace) {\n return replace\n }\n if (ctx.size === 0 || this.typeArgs.length === 0) {\n return this\n }\n\n const typeArgs: TypeDescriptor[] = []\n for (const arg of this.typeArgs) {\n const replace = ctx.get(arg.qname)\n if (replace) {\n typeArgs.push(replace)\n } else {\n typeArgs.push(arg.applyTypeArgs(ctx))\n }\n }\n return new TypeDescriptor(this.qname, typeArgs)\n }\n\n // all depended types including itself, not include system type\n dependedTypes(): string[] {\n if (this.qname.startsWith('&')) {\n console.error('Not expected &')\n return []\n }\n if (this.reference) {\n return []\n }\n switch (this.qname) {\n case 'signer':\n case 'address':\n case 'Address':\n case '0x1::string::String':\n case 'bool':\n case 'Bool':\n case 'u8':\n case 'U8':\n case 'u16':\n case 'U16':\n case 'u32':\n case 'U32':\n case 'u64':\n case 'U64':\n case 'u128':\n case 'U128':\n case 'u256':\n case 'U256':\n return []\n }\n\n // Type parameters are not depended\n if (this.qname.indexOf(SPLITTER) == -1) {\n if (this.qname.startsWith('T')) {\n return []\n }\n }\n\n const types = new Set<string>()\n for (const param of this.typeArgs) {\n param.dependedTypes().forEach((t) => types.add(t))\n }\n\n if (this.qname !== VECTOR_STR) {\n types.add(this.qname)\n }\n\n return Array.from(types)\n }\n}\n\nexport function parseMoveType(type: string): TypeDescriptor {\n const stack: TypeDescriptor[] = [new TypeDescriptor('')]\n let buffer = []\n\n // xxx:asdf<g1<a,<c,d>>, b, g2<a,b>, e>\n for (let i = 0; i < type.length; i++) {\n const ch = type[i]\n if (ch === ' ') {\n continue\n }\n if (ch === '<') {\n // const symbol = type.slice(symbolStart, i)\n // symbolStart =\n const symbol = buffer.join('')\n buffer = []\n stack[stack.length - 1].qname = symbol\n adjustType(stack[stack.length - 1])\n stack.push(new TypeDescriptor(''))\n continue\n }\n if (ch === '>' || ch === ',') {\n const typeParam = stack.pop()\n if (!typeParam) {\n throw Error('Unexpected stack size')\n }\n if (buffer.length > 0) {\n typeParam.qname = buffer.join('')\n buffer = []\n }\n adjustType(typeParam)\n stack[stack.length - 1].typeArgs.push(typeParam)\n if (ch === ',') {\n stack.push(new TypeDescriptor(''))\n }\n continue\n }\n buffer.push(ch)\n }\n\n if (buffer.length > 0) {\n stack[stack.length - 1].qname = buffer.join('')\n }\n adjustType(stack[stack.length - 1])\n\n const res = stack.pop()\n if (!res || stack.length > 0) {\n throw Error('Unexpected stack size')\n }\n return res\n}\n\nfunction adjustType(type: TypeDescriptor) {\n if (type.qname.startsWith('&')) {\n type.reference = true\n type.qname = type.qname.slice(1)\n }\n if (type.qname.startsWith('mut')) {\n type.mutable = true\n type.qname = type.qname.slice(3)\n }\n}\n"]}
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/move/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AAEjD,MAAM,OAAO,cAAc;IACzB,KAAK,CAAQ;IACb,SAAS,CAAS;IAClB,OAAO,CAAS;IAChB,QAAQ,CAAkB;IAE1B,YAAY,MAAc,EAAE,UAA6B;QACvD,IAAI,CAAC,KAAK,GAAG,MAAM,CAAA;QACnB,IAAI,CAAC,SAAS,GAAG,KAAK,CAAA;QACtB,IAAI,CAAC,OAAO,GAAG,KAAK,CAAA;QACpB,IAAI,CAAC,QAAQ,GAAG,UAAU,IAAI,EAAE,CAAA;IAClC,CAAC;IAED,YAAY;QACV,IAAI,IAAI,CAAC,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE;YAC5B,OAAO,IAAI,CAAC,KAAK,GAAG,GAAG,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,YAAY,EAAE,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,GAAG,CAAA;SACtF;QACD,OAAO,IAAI,CAAC,KAAK,CAAA;IACnB,CAAC;IAED,yCAAyC;IACzC,aAAa,CAAC,G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ECTOR_STR } from './utils.js'\n\nexport class TypeDescriptor {\n qname: string\n reference: boolean\n mutable: boolean\n typeArgs: TypeDescriptor[]\n\n constructor(symbol: string, typeParams?: TypeDescriptor[]) {\n this.qname = symbol\n this.reference = false\n this.mutable = false\n this.typeArgs = typeParams || []\n }\n\n getSignature(): string {\n if (this.typeArgs.length > 0) {\n return this.qname + '<' + this.typeArgs.map((t) => t.getSignature()).join(', ') + '>'\n }\n return this.qname\n }\n\n // Replace T0, T1 with more concrete type\n applyTypeArgs(ctx: Map<string, TypeDescriptor>): TypeDescriptor {\n const replace = ctx.get(this.qname)\n if (replace) {\n return replace\n }\n if (ctx.size === 0 || this.typeArgs.length === 0) {\n return this\n }\n\n const typeArgs: TypeDescriptor[] = []\n for (const arg of this.typeArgs) {\n const replace = ctx.get(arg.qname)\n if (replace) {\n typeArgs.push(replace)\n } else {\n typeArgs.push(arg.applyTypeArgs(ctx))\n }\n }\n return new TypeDescriptor(this.qname, typeArgs)\n }\n\n // all depended types including itself, not include system type\n dependedTypes(): string[] {\n if (this.qname.startsWith('&')) {\n console.error('Not expected &')\n return []\n }\n if (this.reference) {\n return []\n }\n switch (this.qname) {\n case 'signer':\n case 'address':\n case 'Address':\n case '0x1::string::String':\n case 'bool':\n case 'Bool':\n case 'u8':\n case 'U8':\n case 'u16':\n case 'U16':\n case 'u32':\n case 'U32':\n case 'u64':\n case 'U64':\n case 'u128':\n case 'U128':\n case 'u256':\n case 'U256':\n return []\n }\n\n // Type parameters are not depended\n if (this.qname.indexOf(SPLITTER) == -1) {\n if (this.qname.startsWith('T')) {\n return []\n }\n }\n\n const types = new Set<string>()\n for (const param of this.typeArgs) {\n param.dependedTypes().forEach((t) => types.add(t))\n }\n\n if (this.qname !== VECTOR_STR) {\n types.add(this.qname)\n }\n\n return Array.from(types)\n }\n}\n\nexport function parseMoveType(type: string): TypeDescriptor {\n const stack: TypeDescriptor[] = [new TypeDescriptor('')]\n let buffer = []\n\n // xxx:asdf<g1<a,<c,d>>, b, g2<a,b>, e>\n for (let i = 0; i < type.length; i++) {\n const ch = type[i]\n if (ch === ' ') {\n continue\n }\n if (ch === '<') {\n // const symbol = type.slice(symbolStart, i)\n // symbolStart =\n const symbol = buffer.join('')\n buffer = []\n stack[stack.length - 1].qname = symbol\n adjustType(stack[stack.length - 1])\n stack.push(new TypeDescriptor(''))\n continue\n }\n if (ch === '>' || ch === ',') {\n const typeParam = stack.pop()\n if (!typeParam) {\n throw Error('Unexpected stack size')\n }\n if (buffer.length > 0) {\n typeParam.qname = buffer.join('')\n buffer = []\n }\n adjustType(typeParam)\n stack[stack.length - 1].typeArgs.push(typeParam)\n if (ch === ',') {\n stack.push(new TypeDescriptor(''))\n }\n continue\n }\n buffer.push(ch)\n }\n\n if (buffer.length > 0) {\n stack[stack.length - 1].qname = buffer.join('')\n }\n adjustType(stack[stack.length - 1])\n\n const res = stack.pop()\n if (!res || stack.length > 0) {\n throw Error('Unexpected stack size')\n }\n return res\n}\n\nfunction adjustType(type: TypeDescriptor) {\n if (type.qname.startsWith('&')) {\n type.reference = true\n type.qname = type.qname.slice(1)\n }\n if (type.qname.startsWith('mut')) {\n type.mutable = true\n type.qname = type.qname.slice(3)\n }\n}\n\n/**\n *\n * @param matcher\n * @param type\n */\nexport function matchType(matcher: TypeDescriptor, type: TypeDescriptor): boolean {\n if (matcher.qname === 'any') {\n return true\n }\n if (matcher.qname !== type.qname) {\n return false\n }\n for (const [idx, matcherTarg] of matcher.typeArgs.entries()) {\n const targ = type.typeArgs[idx]\n if (!matchType(matcherTarg, targ)) {\n return false\n }\n }\n return true\n}\n"]}
@@ -12,10 +12,7 @@ export declare namespace bag {
12
12
  }
13
13
  }
14
14
  export declare namespace balance {
15
- class Balance<T0> {
16
- static TYPE_QNAME: string;
17
- value: bigint;
18
- }
15
+ type Balance<T> = string;
19
16
  class Supply<T0> {
20
17
  static TYPE_QNAME: string;
21
18
  value: bigint;
@@ -14,11 +14,6 @@ export var bag;
14
14
  })(bag || (bag = {}));
15
15
  export var balance;
16
16
  (function (balance) {
17
- class Balance {
18
- static TYPE_QNAME = "0x2::balance::Balance";
19
- value;
20
- }
21
- balance.Balance = Balance;
22
17
  class Supply {
23
18
  static TYPE_QNAME = "0x2::balance::Supply";
24
19
  value;