@d9-network/ink 1.2.5 → 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 +102 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +20 -7
- package/dist/index.d.mts +20 -7
- package/dist/index.mjs +103 -5
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
package/dist/index.cjs
CHANGED
|
@@ -496,6 +496,8 @@ function createAsciiEventTopic(contractName, eventLabel) {
|
|
|
496
496
|
var ContractEventParser = class {
|
|
497
497
|
eventDecoders;
|
|
498
498
|
eventSignatures;
|
|
499
|
+
/** Maps ink! variant index (first byte of event data) to event label */
|
|
500
|
+
eventVariantMap;
|
|
499
501
|
contractAddressBytes;
|
|
500
502
|
contractAddress;
|
|
501
503
|
metadata;
|
|
@@ -506,10 +508,13 @@ var ContractEventParser = class {
|
|
|
506
508
|
this.eventDecoders = buildAllEventDecoders(this.metadata);
|
|
507
509
|
this.contractAddressBytes = (0, _polkadot_labs_hdkd_helpers.ss58Decode)(contractAddress)[0];
|
|
508
510
|
this.eventSignatures = /* @__PURE__ */ new Map();
|
|
511
|
+
this.eventVariantMap = /* @__PURE__ */ new Map();
|
|
509
512
|
const events = this.metadata.spec.events;
|
|
510
|
-
for (
|
|
513
|
+
for (let i = 0; i < events.length; i++) {
|
|
514
|
+
const event = events[i];
|
|
511
515
|
const sig = getEventSignature(event.label);
|
|
512
516
|
this.eventSignatures.set(event.label, sig);
|
|
517
|
+
this.eventVariantMap.set(i, event.label);
|
|
513
518
|
}
|
|
514
519
|
}
|
|
515
520
|
/**
|
|
@@ -541,6 +546,11 @@ var ContractEventParser = class {
|
|
|
541
546
|
break;
|
|
542
547
|
}
|
|
543
548
|
}
|
|
549
|
+
if (!eventLabel && data.length > 0) {
|
|
550
|
+
const variantIndex = data[0];
|
|
551
|
+
const candidateLabel = this.eventVariantMap.get(variantIndex);
|
|
552
|
+
if (candidateLabel && this.eventDecoders.has(candidateLabel)) eventLabel = candidateLabel;
|
|
553
|
+
}
|
|
544
554
|
if (!eventLabel) {
|
|
545
555
|
console.warn("Unknown event signature:", signature);
|
|
546
556
|
return null;
|
|
@@ -983,6 +993,88 @@ function createTypedRpc(client) {
|
|
|
983
993
|
} });
|
|
984
994
|
}
|
|
985
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
|
+
|
|
986
1078
|
//#endregion
|
|
987
1079
|
//#region src/contract.ts
|
|
988
1080
|
/**
|
|
@@ -1094,6 +1186,7 @@ function createD9InkContract(descriptor, address, options) {
|
|
|
1094
1186
|
const addressBytes = ss58ToBytes(address);
|
|
1095
1187
|
const rpc = createTypedRpc(client);
|
|
1096
1188
|
const messageCodecCache = /* @__PURE__ */ new Map();
|
|
1189
|
+
const messageReturnAnalysis = buildMessageReturnAnalysisMap(patchedMetadata);
|
|
1097
1190
|
function getMessageCodec(label) {
|
|
1098
1191
|
const cached = messageCodecCache.get(label);
|
|
1099
1192
|
if (cached) return cached;
|
|
@@ -1166,9 +1259,14 @@ function createD9InkContract(descriptor, address, options) {
|
|
|
1166
1259
|
};
|
|
1167
1260
|
decodedResponse = fallbackResult.value;
|
|
1168
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
|
+
};
|
|
1169
1267
|
return {
|
|
1170
1268
|
success: true,
|
|
1171
|
-
value:
|
|
1269
|
+
value: normalizedResponse.value,
|
|
1172
1270
|
events: [],
|
|
1173
1271
|
gasConsumed: callResult.gas.gasConsumed,
|
|
1174
1272
|
gasRequired: callResult.gas.gasRequired,
|
|
@@ -1354,10 +1452,10 @@ function createD9InkContract(descriptor, address, options) {
|
|
|
1354
1452
|
* });
|
|
1355
1453
|
*
|
|
1356
1454
|
* if (result.success) {
|
|
1357
|
-
* console.log("Balance:", result.value
|
|
1455
|
+
* console.log("Balance:", result.value);
|
|
1358
1456
|
*
|
|
1359
1457
|
* // Send transaction from the query result
|
|
1360
|
-
* const txResult = await result.
|
|
1458
|
+
* const txResult = await result.send().signAndSubmit(aliceSigner);
|
|
1361
1459
|
* }
|
|
1362
1460
|
*
|
|
1363
1461
|
* // Or send directly
|