getPureSerializationType function

String? getPureSerializationType(
  1. SuiMoveNormalizedType normalizedType,
  2. SuiJsonValue argVal
)

Implementation

String? getPureSerializationType(
  SuiMoveNormalizedType normalizedType,
  SuiJsonValue? argVal,
) {
  if (normalizedType is String && allowedTypes.contains(normalizedType)) {
    if (['U8', 'U16', 'U32', 'U64', 'U128', 'U256'].contains(normalizedType)) {
      expectType('number', argVal);
    } else if (normalizedType == 'Bool') {
      expectType('boolean', argVal);
    } else if (normalizedType == 'Address') {
      expectType('string', argVal);
      if (argVal is String && !isValidSuiAddress(argVal)) {
        throw ArgumentError('Invalid Sui Address');
      }
    }
    return normalizedType.toLowerCase();
  } else if (normalizedType is String) {
    throw ArgumentError(
      "Unknown pure normalized type ${jsonEncode(normalizedType)}",
    );
  }

  if ((normalizedType as Map).containsKey("Vector")) {
    if ((argVal == null || argVal is String) &&
        normalizedType["Vector"] == 'U8') {
      return 'string';
    }

    if (argVal != null && argVal is! Iterable) {
      throw ArgumentError(
        "Expect $argVal to be a array, received ${argVal.runtimeType}",
      );
    }

    final innerType = getPureSerializationType(
      normalizedType["Vector"],
      argVal ? argVal[0] : null,
    );

    if (innerType == null) {
      return null;
    }

    return "vector<$innerType>";
  }

  if (normalizedType["Struct"] != null) {
    if (isSameStruct(normalizedType["Struct"], RESOLVED_ASCII_STR)) {
      return 'string';
    } else if (isSameStruct(normalizedType["Struct"], RESOLVED_UTF8_STR)) {
      return 'utf8string';
    } else if (isSameStruct(normalizedType["Struct"], RESOLVED_SUI_ID)) {
      return 'address';
    } else if (isSameStruct(normalizedType["Struct"], RESOLVED_STD_OPTION)) {
      final optionToVec = {
        "Vector": normalizedType["Struct"]["typeArguments"][0],
      };
      return getPureSerializationType(optionToVec, argVal);
    }
  }

  return null;
}