@sentio/sdk 2.11.6-rc.2 → 2.11.6-rc.3

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.
@@ -12,16 +12,16 @@ export class MoveCoder extends AbstractMoveCoder {
12
12
  this.loadInternal(toInternalModule(module));
13
13
  }
14
14
  decodeEvent(event) {
15
- return this.decodedInternal(event);
15
+ return this.decodedStruct(event);
16
16
  }
17
17
  filterAndDecodeEvents(typeQname, resources) {
18
- return this.filterAndDecodeInternal(typeQname, resources);
18
+ return this.filterAndDecodeStruct(typeQname, resources);
19
19
  }
20
20
  decodeResource(res) {
21
- return this.decodedInternal(res);
21
+ return this.decodedStruct(res);
22
22
  }
23
23
  filterAndDecodeResources(typeQname, resources) {
24
- return this.filterAndDecodeInternal(typeQname, resources);
24
+ return this.filterAndDecodeStruct(typeQname, resources);
25
25
  }
26
26
  getMeaningfulFunctionParams(params) {
27
27
  return getMeaningfulFunctionParams(params);
@@ -1 +1 @@
1
- {"version":3,"file":"move-coder.js","sourceRoot":"","sources":["../../src/aptos/move-coder.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,gBAAgB,GAEjB,MAAM,iBAAiB,CAAA;AAGxB,OAAO,EAAE,2BAA2B,EAAE,MAAM,YAAY,CAAA;AACxD,OAAO,EAAE,iBAAiB,EAAE,MAAM,gCAAgC,CAAA;AAGlE,MAAM,OAAO,SAAU,SAAQ,iBAAuC;IACpE,IAAI,CAAC,MAA0B;QAC7B,IAAI,CAAC,MAAM,CAAC,GAAG,EAAE;YACf,MAAM,KAAK,CAAC,oBAAoB,CAAC,CAAA;SAClC;QACD,IAAI,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,GAAG,CAAC,OAAO,EAAE,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE;YACtD,OAAM;SACP;QACD,IAAI,CAAC,YAAY,CAAC,gBAAgB,CAAC,MAAM,CAAC,CAAC,CAAA;IAC7C,CAAC;IAED,WAAW,CAAI,KAAY;QACzB,OAAO,IAAI,CAAC,eAAe,CAAI,KAAK,CAA0B,CAAA;IAChE,CAAC;IACD,qBAAqB,CAAI,SAAiB,EAAE,SAAkB;QAC5D,OAAO,IAAI,CAAC,uBAAuB,CAAC,SAAS,EAAE,SAAS,CAA4B,CAAA;IACtF,CAAC;IACD,cAAc,CAAI,GAAiB;QACjC,OAAO,IAAI,CAAC,eAAe,CAAI,GAAG,CAAC,CAAA;IACrC,CAAC;IACD,wBAAwB,CAAI,SAAiB,EAAE,SAAyB;QACtE,OAAO,IAAI,CAAC,uBAAuB,CAAC,SAAS,EAAE,SAAS,CAAC,CAAA;IAC3D,CAAC;IAED,2BAA2B,CAAC,MAAwB;QAClD,OAAO,2BAA2B,CAAC,MAAM,CAAC,CAAA;IAC5C,CAAC;IAED,qBAAqB,CAAC,OAAgD;QACpE,MAAM,IAAI,GAAG,IAAI,CAAC,eAAe,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAA;QACnD,MAAM,MAAM,GAAG,2BAA2B,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;QACvD,MAAM,gBAAgB,GAAG,IAAI,CAAC,WAAW,CAAC,OAAO,CAAC,SAAS,EAAE,MAAM,CAAC,CAAA;QAEpE,OAAO;YACL,GAAG,OAAO;YACV,iBAAiB,EAAE,gBAAgB;SACP,CAAA;IAChC,CAAC;CACF;AAED,MAAM,CAAC,MAAM,UAAU,GAAG,IAAI,SAAS,EAAE,CAAA;AAEzC,MAAM,UAAU,gBAAgB;IAC9B,OAAO,UAAU,CAAA;AACnB,CAAC","sourcesContent":["import {\n Event,\n MoveModuleBytecode,\n MoveResource,\n toInternalModule,\n TransactionPayload_EntryFunctionPayload,\n} from './move-types.js'\n\nimport { TypedEventInstance, TypedMoveResource, TypedFunctionPayload } from './models.js'\nimport { getMeaningfulFunctionParams } from './utils.js'\nimport { AbstractMoveCoder } from '../move/abstract-move-coder.js'\nimport { TypeDescriptor } from '../move/index.js'\n\nexport class MoveCoder extends AbstractMoveCoder<Event | MoveResource> {\n load(module: MoveModuleBytecode) {\n if (!module.abi) {\n throw Error('Module without abi')\n }\n if (this.contains(module.abi.address, module.abi.name)) {\n return\n }\n this.loadInternal(toInternalModule(module))\n }\n\n decodeEvent<T>(event: Event): TypedEventInstance<T> | undefined {\n return this.decodedInternal<T>(event) as TypedEventInstance<T>\n }\n filterAndDecodeEvents<T>(typeQname: string, resources: Event[]): TypedEventInstance<T>[] {\n return this.filterAndDecodeInternal(typeQname, resources) as TypedEventInstance<T>[]\n }\n decodeResource<T>(res: MoveResource): TypedMoveResource<T> | undefined {\n return this.decodedInternal<T>(res)\n }\n filterAndDecodeResources<T>(typeQname: string, resources: MoveResource[]): TypedMoveResource<T>[] {\n return this.filterAndDecodeInternal(typeQname, resources)\n }\n\n getMeaningfulFunctionParams(params: TypeDescriptor[]): TypeDescriptor[] {\n return getMeaningfulFunctionParams(params)\n }\n\n decodeFunctionPayload(payload: TransactionPayload_EntryFunctionPayload): TransactionPayload_EntryFunctionPayload {\n const func = this.getMoveFunction(payload.function)\n const params = getMeaningfulFunctionParams(func.params)\n const argumentsDecoded = this.decodeArray(payload.arguments, params)\n\n return {\n ...payload,\n arguments_decoded: argumentsDecoded,\n } as TypedFunctionPayload<any>\n }\n}\n\nexport const MOVE_CODER = new MoveCoder()\n\nexport function defaultMoveCoder(): MoveCoder {\n return MOVE_CODER\n}\n"]}
1
+ {"version":3,"file":"move-coder.js","sourceRoot":"","sources":["../../src/aptos/move-coder.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,gBAAgB,GAEjB,MAAM,iBAAiB,CAAA;AAGxB,OAAO,EAAE,2BAA2B,EAAE,MAAM,YAAY,CAAA;AACxD,OAAO,EAAE,iBAAiB,EAAE,MAAM,gCAAgC,CAAA;AAGlE,MAAM,OAAO,SAAU,SAAQ,iBAAuC;IACpE,IAAI,CAAC,MAA0B;QAC7B,IAAI,CAAC,MAAM,CAAC,GAAG,EAAE;YACf,MAAM,KAAK,C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cesContent":["import {\n Event,\n MoveModuleBytecode,\n MoveResource,\n toInternalModule,\n TransactionPayload_EntryFunctionPayload,\n} from './move-types.js'\n\nimport { TypedEventInstance, TypedMoveResource, TypedFunctionPayload } from './models.js'\nimport { getMeaningfulFunctionParams } from './utils.js'\nimport { AbstractMoveCoder } from '../move/abstract-move-coder.js'\nimport { TypeDescriptor } from '../move/index.js'\n\nexport class MoveCoder extends AbstractMoveCoder<Event | MoveResource> {\n load(module: MoveModuleBytecode) {\n if (!module.abi) {\n throw Error('Module without abi')\n }\n if (this.contains(module.abi.address, module.abi.name)) {\n return\n }\n this.loadInternal(toInternalModule(module))\n }\n\n decodeEvent<T>(event: Event): TypedEventInstance<T> | undefined {\n return this.decodedStruct<T>(event) as TypedEventInstance<T>\n }\n filterAndDecodeEvents<T>(typeQname: string, resources: Event[]): TypedEventInstance<T>[] {\n return this.filterAndDecodeStruct(typeQname, resources) as TypedEventInstance<T>[]\n }\n decodeResource<T>(res: MoveResource): TypedMoveResource<T> | undefined {\n return this.decodedStruct<T>(res)\n }\n filterAndDecodeResources<T>(typeQname: string, resources: MoveResource[]): TypedMoveResource<T>[] {\n return this.filterAndDecodeStruct(typeQname, resources) as any\n }\n\n getMeaningfulFunctionParams(params: TypeDescriptor[]): TypeDescriptor[] {\n return getMeaningfulFunctionParams(params)\n }\n\n decodeFunctionPayload(payload: TransactionPayload_EntryFunctionPayload): TransactionPayload_EntryFunctionPayload {\n const func = this.getMoveFunction(payload.function)\n const params = getMeaningfulFunctionParams(func.params)\n const argumentsDecoded = this.decodeArray(payload.arguments, params)\n\n return {\n ...payload,\n arguments_decoded: argumentsDecoded,\n } as TypedFunctionPayload<any>\n }\n}\n\nexport const MOVE_CODER = new MoveCoder()\n\nexport function defaultMoveCoder(): MoveCoder {\n return MOVE_CODER\n}\n"]}
@@ -1,8 +1,8 @@
1
1
  import { TypeDescriptor } from './types.js';
2
2
  import { InternalMoveFunction, InternalMoveModule, InternalMoveStruct } from './internal-models.js';
3
- export type StructWithTag<Base> = Base & {
3
+ export type StructWithTag<Base> = {
4
4
  type: string;
5
- data: any;
5
+ data: Base;
6
6
  };
7
7
  export type DecodedStructWithTag<B, T> = StructWithTag<B> & {
8
8
  data_decoded: T;
@@ -14,6 +14,7 @@ export declare abstract class AbstractMoveCoder<StructType> {
14
14
  private funcMapping;
15
15
  contains(account: string, name: string): boolean;
16
16
  protected abstract getMeaningfulFunctionParams(params: TypeDescriptor[]): TypeDescriptor[];
17
+ protected toStructWithTag(val: StructType): StructWithTag<StructType>;
17
18
  protected loadInternal(module: InternalMoveModule): void;
18
19
  protected decodeBigInt(data: any): bigint;
19
20
  protected encodeBigInt(data: bigint): any;
@@ -25,6 +26,8 @@ export declare abstract class AbstractMoveCoder<StructType> {
25
26
  encodeArray(entriesDecoded: any[], types: TypeDescriptor[]): any[];
26
27
  encodeCallArgs(args: any[], func: string): any[];
27
28
  decodeCallResult(res: any[], func: string): any[];
28
- protected filterAndDecodeInternal<T>(typeQname: string, structs: StructWithTag<StructType>[]): DecodedStructWithTag<StructType, T>[];
29
- protected decodedInternal<T>(typeStruct: StructWithTag<StructType>): DecodedStructWithTag<StructType, T> | undefined;
29
+ protected filterAndDecodeStruct(typeQname: string, structs: StructType[]): any;
30
+ private filterAndDecodeStructWithTags;
31
+ protected decodedStruct<T>(typeStruct: StructType): DecodedStructWithTag<StructType, T> | undefined;
32
+ private decodedStructWithTag;
30
33
  }
@@ -8,6 +8,9 @@ export class AbstractMoveCoder {
8
8
  contains(account, name) {
9
9
  return this.moduleMapping.has(account + '::' + name);
10
10
  }
11
+ toStructWithTag(val) {
12
+ return val;
13
+ }
11
14
  loadInternal(module) {
12
15
  if (this.contains(module.address, module.name)) {
13
16
  return;
@@ -198,12 +201,19 @@ export class AbstractMoveCoder {
198
201
  const f = this.getMoveFunction(func);
199
202
  return this.decodeArray(res, f.return);
200
203
  }
201
- filterAndDecodeInternal(typeQname, structs) {
204
+ filterAndDecodeStruct(typeQname, structs) {
202
205
  if (!structs) {
203
206
  return [];
204
207
  }
208
+ const withTags = structs.map((s) => this.toStructWithTag(s));
209
+ return this.filterAndDecodeStructWithTags(typeQname, withTags);
210
+ }
211
+ filterAndDecodeStructWithTags(typeQname, structsWithTags) {
212
+ if (!structsWithTags) {
213
+ return [];
214
+ }
205
215
  const results = [];
206
- for (const resource of structs) {
216
+ for (const resource of structsWithTags) {
207
217
  if (typeQname.includes('<')) {
208
218
  if (resource.type !== typeQname) {
209
219
  continue;
@@ -212,19 +222,22 @@ export class AbstractMoveCoder {
212
222
  else if (resource.type.split('<')[0] !== typeQname) {
213
223
  continue;
214
224
  }
215
- const result = this.decodedInternal(resource);
225
+ const result = this.decodedStructWithTag(resource);
216
226
  if (result) {
217
227
  results.push(result);
218
228
  }
219
229
  }
220
230
  return results;
221
231
  }
222
- decodedInternal(typeStruct) {
232
+ decodedStruct(typeStruct) {
233
+ return this.decodedStructWithTag(this.toStructWithTag(typeStruct));
234
+ }
235
+ decodedStructWithTag(typeStruct) {
223
236
  const typeDescriptor = parseMoveType(typeStruct.type);
224
237
  const typeArguments = typeDescriptor.typeArgs.map((t) => t.getSignature());
225
238
  let dataTyped = undefined;
226
239
  try {
227
- dataTyped = this.decode(typeStruct.data, typeDescriptor);
240
+ dataTyped = this.decode(this.toStructWithTag(typeStruct.data), typeDescriptor);
228
241
  }
229
242
  catch (e) {
230
243
  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,mBAAmB,CAAA;AAYjD,MAAM,OAAgB,iBAAiB;IAC7B,aAAa,GAAG,IAAI,GAAG,EAA8B,CAAA;IACrD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,CAAA;QACzD,KAAK,MAAM,QAAQ,IAAI,OAAO,EAAE;YAC9B,IAAI,SAAS,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE;gBAC3B,IAAI,QAAQ,CAAC,IAAI,KAAK,SAAS,EAAE;oBAC/B,SAAQ;iBACT;aACF;iBAAM,IAAI,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,SAAS,EAAE;gBACpD,SAAQ;aACT;YACD,MAAM,MAAM,GAAG,IAAI,CAAC,eAAe,CAAC,QAAQ,CAAC,CAAA;YAC7C,IAAI,MAAM,EAAE;gBACV,OAAO,CAAC,IAAI,CAAC,MAA6C,CAAC,CAAA;aAC5D;SACF;QACD,OAAO,OAAO,CAAA;IAChB,CAAC;IAES,eAAe,CAAI,UAAqC;QAChE,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,UAAU,CAAC,IAAI,EAAE,cAAc,CAAC,CAAA;SACzD;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'\n\nexport type StructWithTag<Base> = Base & {\n type: string\n data: any\n}\n\nexport type DecodedStructWithTag<B, T> = StructWithTag<B> & {\n data_decoded: T\n type_arguments: string[]\n}\n\nexport abstract class AbstractMoveCoder<StructType> {\n private moduleMapping = new Map<string, InternalMoveModule>()\n private typeMapping = new Map<string, InternalMoveStruct>()\n private funcMapping = new Map<string, InternalMoveFunction>()\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 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.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 filterAndDecodeInternal<T>(\n typeQname: string,\n structs: StructWithTag<StructType>[]\n ): DecodedStructWithTag<StructType, T>[] {\n if (!structs) {\n return []\n }\n const results: DecodedStructWithTag<StructType, T>[] = []\n for (const resource of structs) {\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.decodedInternal(resource)\n if (result) {\n results.push(result as DecodedStructWithTag<StructType, T>)\n }\n }\n return results\n }\n\n protected decodedInternal<T>(typeStruct: StructWithTag<StructType>): 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(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,aAAa,EAAkB,MAAM,YAAY,CAAA;AAE1D,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAA;AAYjD,MAAM,OAAgB,iBAAiB;IAC7B,aAAa,GAAG,IAAI,GAAG,EAA8B,CAAA;IACrD,WAAW,GAAG,IAAI,GAAG,EAA8B,CAAA;IACnD,WAAW,GAAG,IAAI,GAAG,EAAgC,CAAA;IAE7D,QAAQ,CAAC,OAAe,EAAE,IAAY;QACpC,OAAO,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,OAAO,GAAG,IAAI,GAAG,IAAI,CAAC,CAAA;IACtD,CAAC;IAIS,eAAe,CAAC,GAAe;QACvC,OAAO,GAAU,CAAA;IACnB,CAAC;IAES,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,CAAC,CAAA;YACrE,IAAI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AAC,CAAA;YAClD,IAAI,MAAM,EAAE;gBACV,OAAO,CAAC,IAAI,CAAC,MAA6C,CAAC,CAAA;aAC5D;SACF;QACD,OAAO,OAAO,CAAA;IAChB,CAAC;IAES,aAAa,CAAI,UAAsB;QAC/C,OAAO,IAAI,CAAC,oBAAoB,CAAC,IAAI,CAAC,eAAe,CAAC,UAAU,CAAC,CAAQ,CAAA;IAC3E,CAAC;IAEO,oBAAoB,CAC1B,UAAqC;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'\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<StructType> {\n private moduleMapping = new Map<string, InternalMoveModule>()\n private typeMapping = new Map<string, InternalMoveStruct>()\n private funcMapping = new Map<string, InternalMoveFunction>()\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.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,11 +1,12 @@
1
1
  import { TypedEventInstance, TypedSuiMoveObject } from './models.js';
2
- import { AbstractMoveCoder } from '../move/abstract-move-coder.js';
2
+ import { AbstractMoveCoder, StructWithTag } from '../move/abstract-move-coder.js';
3
3
  import { MoveCallSuiTransaction, SuiEvent, SuiMoveNormalizedModule, SuiCallArg, SuiMoveObject } from '@mysten/sui.js';
4
4
  import { TypeDescriptor } from '../move/index.js';
5
5
  import { dynamic_field } from './builtin/0x2.js';
6
- export declare class MoveCoder extends AbstractMoveCoder<SuiEvent> {
6
+ export declare class MoveCoder extends AbstractMoveCoder<SuiEvent | SuiMoveObject> {
7
7
  load(module: SuiMoveNormalizedModule): void;
8
8
  decode(data: any, type: TypeDescriptor): any;
9
+ toStructWithTag(val: SuiEvent | SuiMoveObject): StructWithTag<SuiEvent>;
9
10
  decodeEvent<T>(event: SuiEvent): TypedEventInstance<T> | undefined;
10
11
  filterAndDecodeEvents<T>(typeQname: string, resources: SuiEvent[]): TypedEventInstance<T>[];
11
12
  getObjectsFromDynamicFields<Name, Value>(objects: SuiMoveObject[]): dynamic_field.Field<Name, Value>[];
@@ -1,4 +1,5 @@
1
1
  import { AbstractMoveCoder } from '../move/abstract-move-coder.js';
2
+ import { SuiEvent, SuiRawData, } from '@mysten/sui.js';
2
3
  import { toInternalModule } from './move-types.js';
3
4
  import { SPLITTER } from '../move/index.js';
4
5
  import { getMeaningfulFunctionParams } from './utils.js';
@@ -20,33 +21,32 @@ export class MoveCoder extends AbstractMoveCoder {
20
21
  return super.decode(data, type);
21
22
  }
22
23
  }
24
+ toStructWithTag(val) {
25
+ if (SuiEvent.is(val)) {
26
+ return {
27
+ ...val,
28
+ data: val.parsedJson,
29
+ };
30
+ }
31
+ if (SuiRawData.is(val)) {
32
+ return {
33
+ ...val,
34
+ data: val.fields,
35
+ };
36
+ }
37
+ return val;
38
+ }
23
39
  decodeEvent(event) {
24
- const res = this.decodedInternal({ ...event, data: event.parsedJson });
25
- return { ...event, data_decoded: res?.data_decoded, type_arguments: res?.type_arguments || [] };
40
+ return this.decodedStruct(event);
26
41
  }
27
42
  filterAndDecodeEvents(typeQname, resources) {
28
- const resp = this.filterAndDecodeInternal(typeQname, resources.map((event) => {
29
- return { ...event, data: event.parsedJson, type: event.type };
30
- }));
31
- return resp.map((res) => {
32
- delete res.data;
33
- const event = res;
34
- return { ...event, data_decoded: res?.data_decoded, type_arguments: res?.type_arguments || [] };
35
- });
43
+ return this.filterAndDecodeStruct(typeQname, resources);
36
44
  }
37
45
  getObjectsFromDynamicFields(objects) {
38
46
  return this.filterAndDecodeObjects(`0x2::dynamic_field::Field`, objects).map((o) => o.data_decoded);
39
47
  }
40
48
  filterAndDecodeObjects(typeQname, objects) {
41
- const structs = objects.map((obj) => {
42
- return { ...obj, data: obj.fields, type: obj.type };
43
- });
44
- const resp = this.filterAndDecodeInternal(typeQname, structs);
45
- return resp.map((res) => {
46
- delete res.data;
47
- const event = res;
48
- return { ...event, data_decoded: res?.data_decoded, type_arguments: res?.type_arguments || [] };
49
- });
49
+ return this.filterAndDecodeStruct(typeQname, objects);
50
50
  }
51
51
  getMeaningfulFunctionParams(params) {
52
52
  return getMeaningfulFunctionParams(params);
@@ -1 +1 @@
1
- {"version":3,"file":"move-coder.js","sourceRoot":"","sources":["../../src/sui/move-coder.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAiB,MAAM,gCAAgC,CAAA;AAEjF,OAAO,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAA;AA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CAAC,MAAM,UAAU,GAAG,IAAI,SAAS,EAAE,CAAA;AAEzC,MAAM,UAAU,gBAAgB;IAC9B,OAAO,UAAU,CAAA;AACnB,CAAC","sourcesContent":["import { TypedEventInstance, TypedFunctionPayload, TypedSuiMoveObject } from './models.js'\nimport { AbstractMoveCoder, StructWithTag } from '../move/abstract-move-coder.js'\nimport { MoveCallSuiTransaction, SuiEvent, SuiMoveNormalizedModule, SuiCallArg, SuiMoveObject } from '@mysten/sui.js'\nimport { toInternalModule } from './move-types.js'\nimport { SPLITTER, TypeDescriptor } from '../move/index.js'\nimport { getMeaningfulFunctionParams } from './utils.js'\nimport { dynamic_field } from './builtin/0x2.js'\n\nexport class MoveCoder extends AbstractMoveCoder<SuiEvent> {\n load(module: SuiMoveNormalizedModule) {\n if (this.contains(module.address, module.name)) {\n return\n }\n this.loadInternal(toInternalModule(module))\n }\n\n decode(data: any, type: TypeDescriptor): any {\n switch (type.qname) {\n case '0x2::object::ID':\n case '0x2::coin::Coin':\n return data\n case '0x1::option::Option':\n return this.decode(data, type.typeArgs[0])\n default:\n return super.decode(data, type)\n }\n }\n\n decodeEvent<T>(event: SuiEvent): TypedEventInstance<T> | undefined {\n const res = this.decodedInternal<T>({ ...event, data: event.parsedJson })\n return { ...event, data_decoded: res?.data_decoded as T, type_arguments: res?.type_arguments || [] }\n }\n filterAndDecodeEvents<T>(typeQname: string, resources: SuiEvent[]): TypedEventInstance<T>[] {\n const resp = this.filterAndDecodeInternal(\n typeQname,\n resources.map((event) => {\n return { ...event, data: event.parsedJson, type: event.type }\n })\n )\n return resp.map((res) => {\n delete res.data\n const event = res as SuiEvent\n return { ...event, data_decoded: res?.data_decoded as T, type_arguments: res?.type_arguments || [] }\n })\n }\n\n getObjectsFromDynamicFields<Name, Value>(objects: SuiMoveObject[]): dynamic_field.Field<Name, Value>[] {\n return this.filterAndDecodeObjects(`0x2::dynamic_field::Field`, objects).map((o) => o.data_decoded) as any\n }\n\n filterAndDecodeObjects<T>(typeQname: string, objects: SuiMoveObject[]): TypedSuiMoveObject<T>[] {\n const structs = objects.map((obj) => {\n return { ...obj, data: obj.fields, type: obj.type }\n }) as StructWithTag<any>\n const resp = this.filterAndDecodeInternal(typeQname, structs)\n return resp.map((res) => {\n delete res.data\n const event = res as any as SuiMoveObject\n return { ...event, data_decoded: res?.data_decoded as T, type_arguments: res?.type_arguments || [] }\n })\n }\n\n getMeaningfulFunctionParams(params: TypeDescriptor[]): TypeDescriptor[] {\n return getMeaningfulFunctionParams(params)\n }\n\n decodeFunctionPayload(payload: MoveCallSuiTransaction, inputs: SuiCallArg[]): MoveCallSuiTransaction {\n const functionType = [payload.package, payload.module, payload.function].join(SPLITTER)\n const func = this.getMoveFunction(functionType)\n const params = getMeaningfulFunctionParams(func.params)\n const args = []\n for (const value of payload.arguments || []) {\n const argValue = value as any\n if ('Input' in (argValue as any)) {\n const idx = argValue.Input\n const arg = inputs[idx]\n if (arg.type === 'pure') {\n args.push(arg.value)\n } else if (arg.type === 'object') {\n args.push(arg.objectId)\n } else {\n console.error('unexpected function arg value')\n args.push(undefined)\n }\n // args.push(arg) // TODO check why ts not work using arg.push(arg)\n } else {\n args.push(undefined)\n }\n }\n\n const argumentsTyped = this.decodeArray(args, params)\n return {\n ...payload,\n arguments_decoded: argumentsTyped,\n } as TypedFunctionPayload<any>\n }\n}\n\nexport const MOVE_CODER = new MoveCoder()\n\nexport function defaultMoveCoder(): MoveCoder {\n return MOVE_CODER\n}\n"]}
1
+ {"version":3,"file":"move-coder.js","sourceRoot":"","sources":["../../src/sui/move-coder.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAiB,MAAM,gCAAgC,CAAA;AACjF,OAAO,EAEL,QAAQ,EAIR,UAAU,GACX,MAAM,gBAAgB,CAAA;AACvB,OAAO,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAA;AAClD,OAAO,EAAE,QAAQ,EAAkB,MAAM,kBAAkB,CAAA;AAC3D,OAAO,EAAE,2BAA2B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AC,CAAA;oBAC9C,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,CAAA;iBACrB;gBACD,mEAAmE;aACpE;iBAAM;gBACL,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,CAAA;aACrB;SACF;QAED,MAAM,cAAc,GAAG,IAAI,CAAC,WAAW,CAAC,IAAI,EAAE,MAAM,CAAC,CAAA;QACrD,OAAO;YACL,GAAG,OAAO;YACV,iBAAiB,EAAE,cAAc;SACL,CAAA;IAChC,CAAC;CACF;AAED,MAAM,CAAC,MAAM,UAAU,GAAG,IAAI,SAAS,EAAE,CAAA;AAEzC,MAAM,UAAU,gBAAgB;IAC9B,OAAO,UAAU,CAAA;AACnB,CAAC","sourcesContent":["import { TypedEventInstance, TypedFunctionPayload, TypedSuiMoveObject } from './models.js'\nimport { AbstractMoveCoder, StructWithTag } from '../move/abstract-move-coder.js'\nimport {\n MoveCallSuiTransaction,\n SuiEvent,\n SuiMoveNormalizedModule,\n SuiCallArg,\n SuiMoveObject,\n SuiRawData,\n} from '@mysten/sui.js'\nimport { toInternalModule } from './move-types.js'\nimport { SPLITTER, TypeDescriptor } from '../move/index.js'\nimport { getMeaningfulFunctionParams } from './utils.js'\nimport { dynamic_field } from './builtin/0x2.js'\n\nexport class MoveCoder extends AbstractMoveCoder<SuiEvent | SuiMoveObject> {\n load(module: SuiMoveNormalizedModule) {\n if (this.contains(module.address, module.name)) {\n return\n }\n this.loadInternal(toInternalModule(module))\n }\n\n decode(data: any, type: TypeDescriptor): any {\n switch (type.qname) {\n case '0x2::object::ID':\n case '0x2::coin::Coin':\n return data\n case '0x1::option::Option':\n return this.decode(data, type.typeArgs[0])\n default:\n return super.decode(data, type)\n }\n }\n\n toStructWithTag(val: SuiEvent | SuiMoveObject): StructWithTag<SuiEvent> {\n if (SuiEvent.is(val)) {\n return {\n ...val,\n data: val.parsedJson as any,\n }\n }\n if (SuiRawData.is(val)) {\n return {\n ...val,\n data: val.fields as any,\n }\n }\n return val as any\n }\n\n decodeEvent<T>(event: SuiEvent): TypedEventInstance<T> | undefined {\n return this.decodedStruct<T>(event) as any\n }\n filterAndDecodeEvents<T>(typeQname: string, resources: SuiEvent[]): TypedEventInstance<T>[] {\n return this.filterAndDecodeStruct(typeQname, resources)\n }\n\n getObjectsFromDynamicFields<Name, Value>(objects: SuiMoveObject[]): dynamic_field.Field<Name, Value>[] {\n return this.filterAndDecodeObjects(`0x2::dynamic_field::Field`, objects).map((o) => o.data_decoded) as any\n }\n\n filterAndDecodeObjects<T>(typeQname: string, objects: SuiMoveObject[]): TypedSuiMoveObject<T>[] {\n return this.filterAndDecodeStruct(typeQname, objects)\n }\n\n getMeaningfulFunctionParams(params: TypeDescriptor[]): TypeDescriptor[] {\n return getMeaningfulFunctionParams(params)\n }\n\n decodeFunctionPayload(payload: MoveCallSuiTransaction, inputs: SuiCallArg[]): MoveCallSuiTransaction {\n const functionType = [payload.package, payload.module, payload.function].join(SPLITTER)\n const func = this.getMoveFunction(functionType)\n const params = getMeaningfulFunctionParams(func.params)\n const args = []\n for (const value of payload.arguments || []) {\n const argValue = value as any\n if ('Input' in (argValue as any)) {\n const idx = argValue.Input\n const arg = inputs[idx]\n if (arg.type === 'pure') {\n args.push(arg.value)\n } else if (arg.type === 'object') {\n args.push(arg.objectId)\n } else {\n console.error('unexpected function arg value')\n args.push(undefined)\n }\n // args.push(arg) // TODO check why ts not work using arg.push(arg)\n } else {\n args.push(undefined)\n }\n }\n\n const argumentsTyped = this.decodeArray(args, params)\n return {\n ...payload,\n arguments_decoded: argumentsTyped,\n } as TypedFunctionPayload<any>\n }\n}\n\nexport const MOVE_CODER = new MoveCoder()\n\nexport function defaultMoveCoder(): MoveCoder {\n return MOVE_CODER\n}\n"]}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@sentio/sdk",
3
3
  "license": "Apache-2.0",
4
- "version": "2.11.6-rc.2",
4
+ "version": "2.11.6-rc.3",
5
5
  "type": "module",
6
6
  "scripts": {
7
7
  "compile": "tsc && cp src/utils/*.csv lib/utils && cp src/tsup.config.ts lib",
@@ -23,8 +23,8 @@
23
23
  "@project-serum/anchor": "^0.26.0",
24
24
  "@sentio/bigdecimal": "^9.1.1-patch.3",
25
25
  "@sentio/ethers-v6": "^1.0.28",
26
- "@sentio/protos": "^2.11.6-rc.2",
27
- "@sentio/runtime": "^2.11.6-rc.2",
26
+ "@sentio/protos": "^2.11.6-rc.3",
27
+ "@sentio/runtime": "^2.11.6-rc.3",
28
28
  "@solana/web3.js": "^1.74.0",
29
29
  "@types/prettier": "^2.7.2",
30
30
  "aptos-sdk": "npm:aptos@^1.7.2",
@@ -87,5 +87,5 @@
87
87
  "engines": {
88
88
  "node": ">=16"
89
89
  },
90
- "gitHead": "00032f65df12d3e50ac446bc9e5c865a110264ba"
90
+ "gitHead": "8e1193e39095aa024d21bf517f808f4e1e20c080"
91
91
  }
@@ -23,16 +23,16 @@ export class MoveCoder extends AbstractMoveCoder<Event | MoveResource> {
23
23
  }
24
24
 
25
25
  decodeEvent<T>(event: Event): TypedEventInstance<T> | undefined {
26
- return this.decodedInternal<T>(event) as TypedEventInstance<T>
26
+ return this.decodedStruct<T>(event) as TypedEventInstance<T>
27
27
  }
28
28
  filterAndDecodeEvents<T>(typeQname: string, resources: Event[]): TypedEventInstance<T>[] {
29
- return this.filterAndDecodeInternal(typeQname, resources) as TypedEventInstance<T>[]
29
+ return this.filterAndDecodeStruct(typeQname, resources) as TypedEventInstance<T>[]
30
30
  }
31
31
  decodeResource<T>(res: MoveResource): TypedMoveResource<T> | undefined {
32
- return this.decodedInternal<T>(res)
32
+ return this.decodedStruct<T>(res)
33
33
  }
34
34
  filterAndDecodeResources<T>(typeQname: string, resources: MoveResource[]): TypedMoveResource<T>[] {
35
- return this.filterAndDecodeInternal(typeQname, resources)
35
+ return this.filterAndDecodeStruct(typeQname, resources) as any
36
36
  }
37
37
 
38
38
  getMeaningfulFunctionParams(params: TypeDescriptor[]): TypeDescriptor[] {
@@ -3,9 +3,9 @@ import { parseMoveType, TypeDescriptor } from './types.js'
3
3
  import { InternalMoveFunction, InternalMoveModule, InternalMoveStruct } from './internal-models.js'
4
4
  import { bytesToBigInt } from '../utils/index.js'
5
5
 
6
- export type StructWithTag<Base> = Base & {
6
+ export type StructWithTag<Base> = {
7
7
  type: string
8
- data: any
8
+ data: Base
9
9
  }
10
10
 
11
11
  export type DecodedStructWithTag<B, T> = StructWithTag<B> & {
@@ -24,6 +24,10 @@ export abstract class AbstractMoveCoder<StructType> {
24
24
 
25
25
  protected abstract getMeaningfulFunctionParams(params: TypeDescriptor[]): TypeDescriptor[]
26
26
 
27
+ protected toStructWithTag(val: StructType): StructWithTag<StructType> {
28
+ return val as any
29
+ }
30
+
27
31
  protected loadInternal(module: InternalMoveModule) {
28
32
  if (this.contains(module.address, module.name)) {
29
33
  return
@@ -236,15 +240,23 @@ export abstract class AbstractMoveCoder<StructType> {
236
240
  return this.decodeArray(res, f.return)
237
241
  }
238
242
 
239
- protected filterAndDecodeInternal<T>(
243
+ protected filterAndDecodeStruct(typeQname: string, structs: StructType[]): any {
244
+ if (!structs) {
245
+ return []
246
+ }
247
+ const withTags = structs.map((s) => this.toStructWithTag(s))
248
+ return this.filterAndDecodeStructWithTags(typeQname, withTags)
249
+ }
250
+
251
+ private filterAndDecodeStructWithTags<T>(
240
252
  typeQname: string,
241
- structs: StructWithTag<StructType>[]
253
+ structsWithTags: StructWithTag<StructType>[]
242
254
  ): DecodedStructWithTag<StructType, T>[] {
243
- if (!structs) {
255
+ if (!structsWithTags) {
244
256
  return []
245
257
  }
246
258
  const results: DecodedStructWithTag<StructType, T>[] = []
247
- for (const resource of structs) {
259
+ for (const resource of structsWithTags) {
248
260
  if (typeQname.includes('<')) {
249
261
  if (resource.type !== typeQname) {
250
262
  continue
@@ -252,7 +264,7 @@ export abstract class AbstractMoveCoder<StructType> {
252
264
  } else if (resource.type.split('<')[0] !== typeQname) {
253
265
  continue
254
266
  }
255
- const result = this.decodedInternal(resource)
267
+ const result = this.decodedStructWithTag(resource)
256
268
  if (result) {
257
269
  results.push(result as DecodedStructWithTag<StructType, T>)
258
270
  }
@@ -260,13 +272,19 @@ export abstract class AbstractMoveCoder<StructType> {
260
272
  return results
261
273
  }
262
274
 
263
- protected decodedInternal<T>(typeStruct: StructWithTag<StructType>): DecodedStructWithTag<StructType, T> | undefined {
275
+ protected decodedStruct<T>(typeStruct: StructType): DecodedStructWithTag<StructType, T> | undefined {
276
+ return this.decodedStructWithTag(this.toStructWithTag(typeStruct)) as any
277
+ }
278
+
279
+ private decodedStructWithTag<T>(
280
+ typeStruct: StructWithTag<StructType>
281
+ ): DecodedStructWithTag<StructType, T> | undefined {
264
282
  const typeDescriptor = parseMoveType(typeStruct.type)
265
283
  const typeArguments = typeDescriptor.typeArgs.map((t) => t.getSignature())
266
284
 
267
285
  let dataTyped = undefined
268
286
  try {
269
- dataTyped = this.decode(typeStruct.data, typeDescriptor)
287
+ dataTyped = this.decode(this.toStructWithTag(typeStruct.data), typeDescriptor)
270
288
  } catch (e) {
271
289
  console.error('Decoding error for ', JSON.stringify(typeStruct), e)
272
290
  return undefined
@@ -1,12 +1,19 @@
1
1
  import { TypedEventInstance, TypedFunctionPayload, TypedSuiMoveObject } from './models.js'
2
2
  import { AbstractMoveCoder, StructWithTag } from '../move/abstract-move-coder.js'
3
- import { MoveCallSuiTransaction, SuiEvent, SuiMoveNormalizedModule, SuiCallArg, SuiMoveObject } from '@mysten/sui.js'
3
+ import {
4
+ MoveCallSuiTransaction,
5
+ SuiEvent,
6
+ SuiMoveNormalizedModule,
7
+ SuiCallArg,
8
+ SuiMoveObject,
9
+ SuiRawData,
10
+ } from '@mysten/sui.js'
4
11
  import { toInternalModule } from './move-types.js'
5
12
  import { SPLITTER, TypeDescriptor } from '../move/index.js'
6
13
  import { getMeaningfulFunctionParams } from './utils.js'
7
14
  import { dynamic_field } from './builtin/0x2.js'
8
15
 
9
- export class MoveCoder extends AbstractMoveCoder<SuiEvent> {
16
+ export class MoveCoder extends AbstractMoveCoder<SuiEvent | SuiMoveObject> {
10
17
  load(module: SuiMoveNormalizedModule) {
11
18
  if (this.contains(module.address, module.name)) {
12
19
  return
@@ -26,22 +33,27 @@ export class MoveCoder extends AbstractMoveCoder<SuiEvent> {
26
33
  }
27
34
  }
28
35
 
36
+ toStructWithTag(val: SuiEvent | SuiMoveObject): StructWithTag<SuiEvent> {
37
+ if (SuiEvent.is(val)) {
38
+ return {
39
+ ...val,
40
+ data: val.parsedJson as any,
41
+ }
42
+ }
43
+ if (SuiRawData.is(val)) {
44
+ return {
45
+ ...val,
46
+ data: val.fields as any,
47
+ }
48
+ }
49
+ return val as any
50
+ }
51
+
29
52
  decodeEvent<T>(event: SuiEvent): TypedEventInstance<T> | undefined {
30
- const res = this.decodedInternal<T>({ ...event, data: event.parsedJson })
31
- return { ...event, data_decoded: res?.data_decoded as T, type_arguments: res?.type_arguments || [] }
53
+ return this.decodedStruct<T>(event) as any
32
54
  }
33
55
  filterAndDecodeEvents<T>(typeQname: string, resources: SuiEvent[]): TypedEventInstance<T>[] {
34
- const resp = this.filterAndDecodeInternal(
35
- typeQname,
36
- resources.map((event) => {
37
- return { ...event, data: event.parsedJson, type: event.type }
38
- })
39
- )
40
- return resp.map((res) => {
41
- delete res.data
42
- const event = res as SuiEvent
43
- return { ...event, data_decoded: res?.data_decoded as T, type_arguments: res?.type_arguments || [] }
44
- })
56
+ return this.filterAndDecodeStruct(typeQname, resources)
45
57
  }
46
58
 
47
59
  getObjectsFromDynamicFields<Name, Value>(objects: SuiMoveObject[]): dynamic_field.Field<Name, Value>[] {
@@ -49,15 +61,7 @@ export class MoveCoder extends AbstractMoveCoder<SuiEvent> {
49
61
  }
50
62
 
51
63
  filterAndDecodeObjects<T>(typeQname: string, objects: SuiMoveObject[]): TypedSuiMoveObject<T>[] {
52
- const structs = objects.map((obj) => {
53
- return { ...obj, data: obj.fields, type: obj.type }
54
- }) as StructWithTag<any>
55
- const resp = this.filterAndDecodeInternal(typeQname, structs)
56
- return resp.map((res) => {
57
- delete res.data
58
- const event = res as any as SuiMoveObject
59
- return { ...event, data_decoded: res?.data_decoded as T, type_arguments: res?.type_arguments || [] }
60
- })
64
+ return this.filterAndDecodeStruct(typeQname, objects)
61
65
  }
62
66
 
63
67
  getMeaningfulFunctionParams(params: TypeDescriptor[]): TypeDescriptor[] {