Skip to content

Commit fcc286c

Browse files
committed
Fix struct field name normalization for gas estimation auto-fill
Dedot's SCALE codec normalizes struct field names to camelCase via stringCamelCase (ref_time → refTime, proof_size → proofSize). Our codec and auto-fill code was using raw metadata names (snake_case), causing 'Cannot mix BigInt and other types' encoding errors in the Information Pane after gas estimation. Fix at the shared layer: - Add normalizeFieldName() to lib/codec.ts matching Dedot's convention - Use it in decomposeArgHex for struct and enum field value lookups - Simplify gas estimation auto-fill to use camelCase directly
1 parent ee246d9 commit fcc286c

2 files changed

Lines changed: 16 additions & 26 deletions

File tree

components/builder/extrinsic-builder.tsx

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -110,29 +110,11 @@ const ExtrinsicBuilder: React.FC<ExtrinsicBuilderProps> = ({
110110
useEffect(() => {
111111
if (!isReviveInstantiate || !gasEstimation.weightRequired || !tx) return;
112112

113-
// Resolve weight field names from metadata
114-
const weightField = tx.meta?.fields?.find((f) => f.name === "weight_limit");
115-
if (weightField) {
116-
try {
117-
const weightType = client.registry.findType(weightField.typeId);
118-
const { typeDef } = weightType;
119-
if (typeDef.type === "Struct" && typeDef.value.fields.length >= 2) {
120-
const [field0, field1] = typeDef.value.fields;
121-
const name0 = String(field0.name);
122-
const name1 = String(field1.name);
123-
builderForm.setValue("weight_limit", {
124-
[name0]: String(gasEstimation.weightRequired.refTime),
125-
[name1]: String(gasEstimation.weightRequired.proofSize),
126-
});
127-
}
128-
} catch {
129-
// Fallback: use camelCase names
130-
builderForm.setValue("weight_limit", {
131-
refTime: String(gasEstimation.weightRequired.refTime),
132-
proofSize: String(gasEstimation.weightRequired.proofSize),
133-
});
134-
}
135-
}
113+
// Auto-fill weight_limit with camelCase keys (Dedot codec convention)
114+
builderForm.setValue("weight_limit", {
115+
refTime: String(gasEstimation.weightRequired.refTime),
116+
proofSize: String(gasEstimation.weightRequired.proofSize),
117+
});
136118

137119
// Auto-fill storage deposit only for Charge
138120
if (gasEstimation.storageDeposit?.type === "Charge") {

lib/codec.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import { DedotClient } from "dedot";
2-
import { u8aToHex, hexToU8a, hexStripPrefix, hexAddPrefix, decodeAddress } from "dedot/utils";
2+
import { u8aToHex, hexToU8a, hexStripPrefix, hexAddPrefix, decodeAddress, stringCamelCase } from "dedot/utils";
3+
4+
/**
5+
* Normalize a metadata field name to camelCase, matching Dedot's internal convention.
6+
* Metadata uses snake_case (ref_time, proof_size), but Dedot codecs expect camelCase.
7+
*/
8+
export function normalizeFieldName(name: string): string {
9+
return stringCamelCase(name.replace("#", "_"));
10+
}
311

412
/**
513
* Result type for encoding operations
@@ -541,7 +549,7 @@ export function decomposeArgHex(
541549
if (fields.length > 0 && typeof value === "object" && value !== null && !Array.isArray(value)) {
542550
const obj = value as Record<string, unknown>;
543551
const children: HexChildItem[] = fields.map((field) => {
544-
const fieldName = field.name || "";
552+
const fieldName = normalizeFieldName(field.name || "");
545553
const fieldValue = obj[fieldName];
546554
const result = encodeArg(client, field.typeId, fieldValue);
547555
return {
@@ -577,7 +585,7 @@ export function decomposeArgHex(
577585
if (typeof enumObj.value === "object" && enumObj.value !== null) {
578586
const obj = enumObj.value as Record<string, unknown>;
579587
const children: HexChildItem[] = variant.fields.map((field) => {
580-
const fieldName = field.name || "";
588+
const fieldName = normalizeFieldName(field.name || "");
581589
const fieldValue = obj[fieldName];
582590
const result = encodeArg(client, field.typeId, fieldValue);
583591
return {

0 commit comments

Comments
 (0)