@d9-network/ink 1.2.6 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -993,6 +993,88 @@ function createTypedRpc(client) {
993
993
  } });
994
994
  }
995
995
 
996
+ //#endregion
997
+ //#region src/result-normalization.ts
998
+ function getTypeEntry(types, typeId) {
999
+ const entry = types.find((item) => item.id === typeId);
1000
+ if (!entry) throw new Error(`Type ${typeId} not found in metadata`);
1001
+ return entry;
1002
+ }
1003
+ function getResultTypeIds(typeEntry) {
1004
+ const path = typeEntry.type.path;
1005
+ if (!path || path[0] !== "Result") return null;
1006
+ const params = typeEntry.type.params;
1007
+ if (!params || params.length < 2) return null;
1008
+ const okTypeId = params[0]?.type;
1009
+ const errTypeId = params[1]?.type;
1010
+ if (okTypeId === void 0 || errTypeId === void 0) return null;
1011
+ return {
1012
+ okTypeId,
1013
+ errTypeId
1014
+ };
1015
+ }
1016
+ function hasOkErrVariants(typeEntry) {
1017
+ const variants = typeEntry.type.def.variant?.variants;
1018
+ if (!variants) return false;
1019
+ const names = new Set(variants.map((variant) => variant.name));
1020
+ return names.has("Ok") && names.has("Err");
1021
+ }
1022
+ function isRecord(value) {
1023
+ return typeof value === "object" && value !== null;
1024
+ }
1025
+ function isRuntimeResult(value) {
1026
+ if (!isRecord(value)) return false;
1027
+ if ("success" in value) return typeof value.success === "boolean" && "value" in value;
1028
+ if ("type" in value) return (value.type === "Ok" || value.type === "Err") && "value" in value;
1029
+ return false;
1030
+ }
1031
+ function isRuntimeResultSuccess(value) {
1032
+ if ("success" in value) return value.success;
1033
+ return value.type === "Ok";
1034
+ }
1035
+ function analyzeMessageReturn(metadata, messageLabel) {
1036
+ const types = metadata.types;
1037
+ const message = metadata.spec.messages.find((item) => item.label === messageLabel);
1038
+ if (!message) throw new Error(`Message "${messageLabel}" not found in metadata`);
1039
+ let currentTypeId = getResultTypeIds(getTypeEntry(types, message.returnType.type))?.okTypeId ?? message.returnType.type;
1040
+ let businessResultDepth = 0;
1041
+ while (true) {
1042
+ const currentType = getTypeEntry(types, currentTypeId);
1043
+ const resultTypeIds = getResultTypeIds(currentType);
1044
+ if (!resultTypeIds || !hasOkErrVariants(currentType)) break;
1045
+ businessResultDepth += 1;
1046
+ currentTypeId = resultTypeIds.okTypeId;
1047
+ }
1048
+ return {
1049
+ businessResultDepth,
1050
+ finalTypeId: currentTypeId
1051
+ };
1052
+ }
1053
+ function buildMessageReturnAnalysisMap(metadata) {
1054
+ const analysisMap = /* @__PURE__ */ new Map();
1055
+ const messages = metadata.spec.messages;
1056
+ for (const message of messages) analysisMap.set(message.label, analyzeMessageReturn(metadata, message.label));
1057
+ return analysisMap;
1058
+ }
1059
+ function normalizeDecodedResponse(value, businessResultDepth) {
1060
+ let current = value;
1061
+ for (let index = 0; index < businessResultDepth; index += 1) {
1062
+ if (!isRuntimeResult(current)) return {
1063
+ success: false,
1064
+ error: /* @__PURE__ */ new Error(`Expected Result payload at depth ${index + 1}, received ${typeof current}`)
1065
+ };
1066
+ if (!isRuntimeResultSuccess(current)) return {
1067
+ success: false,
1068
+ error: current.value
1069
+ };
1070
+ current = current.value;
1071
+ }
1072
+ return {
1073
+ success: true,
1074
+ value: current
1075
+ };
1076
+ }
1077
+
996
1078
  //#endregion
997
1079
  //#region src/contract.ts
998
1080
  /**
@@ -1104,6 +1186,7 @@ function createD9InkContract(descriptor, address, options) {
1104
1186
  const addressBytes = ss58ToBytes(address);
1105
1187
  const rpc = createTypedRpc(client);
1106
1188
  const messageCodecCache = /* @__PURE__ */ new Map();
1189
+ const messageReturnAnalysis = buildMessageReturnAnalysisMap(patchedMetadata);
1107
1190
  function getMessageCodec(label) {
1108
1191
  const cached = messageCodecCache.get(label);
1109
1192
  if (cached) return cached;
@@ -1176,9 +1259,14 @@ function createD9InkContract(descriptor, address, options) {
1176
1259
  };
1177
1260
  decodedResponse = fallbackResult.value;
1178
1261
  }
1262
+ const normalizedResponse = normalizeDecodedResponse(decodedResponse, messageReturnAnalysis.get(method)?.businessResultDepth ?? 0);
1263
+ if (!normalizedResponse.success) return {
1264
+ success: false,
1265
+ error: normalizedResponse.error instanceof Error ? new _d9_network_spec.DecodeError(method, normalizedResponse.error.message, normalizedResponse.error) : new _d9_network_spec.ContractExecutionError(method, normalizedResponse.error)
1266
+ };
1179
1267
  return {
1180
1268
  success: true,
1181
- value: decodedResponse,
1269
+ value: normalizedResponse.value,
1182
1270
  events: [],
1183
1271
  gasConsumed: callResult.gas.gasConsumed,
1184
1272
  gasRequired: callResult.gas.gasRequired,
@@ -1364,10 +1452,10 @@ function createD9InkContract(descriptor, address, options) {
1364
1452
  * });
1365
1453
  *
1366
1454
  * if (result.success) {
1367
- * console.log("Balance:", result.value.response);
1455
+ * console.log("Balance:", result.value);
1368
1456
  *
1369
1457
  * // Send transaction from the query result
1370
- * const txResult = await result.value.send().signAndSubmit(aliceSigner);
1458
+ * const txResult = await result.send().signAndSubmit(aliceSigner);
1371
1459
  * }
1372
1460
  *
1373
1461
  * // Or send directly