Skip to content

Commit 14fb955

Browse files
committed
chore(calldata): fix linting errors
1 parent 1327a06 commit 14fb955

File tree

2 files changed

+65
-37
lines changed

2 files changed

+65
-37
lines changed

src/utils/calldata/apiDataEncoder.ts

-1
This file was deleted.

src/utils/calldata/calldataDecoder.ts

+65-36
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,17 @@ import assert from '../assert';
4141
function decodeBaseType(type: string, calldata: string | string[]): BigNumberish | CairoUint256 {
4242
switch (true) {
4343
case CairoUint256.isAbiType(type):
44-
assert(Array.isArray(calldata) && calldata.length === 2, 'Expected calldata for CairoUint256 as an array of two strings.')
44+
assert(
45+
Array.isArray(calldata) && calldata.length === 2,
46+
'Expected calldata for CairoUint256 as an array of two strings.'
47+
);
4548
return CairoUint256.fromCalldata([calldata[0], calldata[1]]);
4649

4750
case isTypeBytes31(type):
4851
return decodeShortString(calldata as string);
4952

5053
case isTypeFelt(type):
51-
assert(typeof calldata === 'string', 'Expected string calldata for base type decoding.')
54+
assert(typeof calldata === 'string', 'Expected string calldata for base type decoding.');
5255
return BigInt(calldata);
5356

5457
default:
@@ -64,19 +67,24 @@ function decodeBaseType(type: string, calldata: string | string[]): BigNumberish
6467
* @param enums The ABI enums.
6568
* @returns An array of decoded tuple elements.
6669
*/
67-
function decodeTuple(calldata: string[], typeStr: string, structs: AbiStructs, enums: AbiEnums) {
70+
function decodeTuple(
71+
calldata: string[],
72+
typeStr: string,
73+
structs: AbiStructs,
74+
enums: AbiEnums
75+
): any[] {
6876
// Parse typeStr to understand the tuple structure, e.g., "('felt', 'struct', 'enum')"
6977
const types: string[] = extractTupleMemberTypes(typeStr).map((type: string | object) =>
7078
String(type)
7179
);
7280

7381
// Assuming we now have an array of types, ['felt', 'YourStructName', 'YourEnumName'], etc.
74-
const decodedElements = [];
82+
const decodedElements: any = [];
7583
let calldataIndex = 0;
7684

77-
for (const type of types) {
85+
types.forEach((type) => {
7886
switch (true) {
79-
case isTypeStruct(type, structs):
87+
case isTypeStruct(type, structs): {
8088
const structRes = decodeStruct(
8189
calldata.slice(calldataIndex, calldataIndex + structs[type].size),
8290
type,
@@ -86,7 +94,8 @@ function decodeTuple(calldata: string[], typeStr: string, structs: AbiStructs, e
8694
decodedElements.push(structRes);
8795
calldataIndex += structs[type].size; // Assuming size is defined for structs.
8896
break;
89-
case isTypeEnum(type, enums):
97+
}
98+
case isTypeEnum(type, enums): {
9099
// Determine the expected calldata consumption for the current enum. (e.g., 1 or 2 elements for CairoOption, 2 elements for CairoResult, etc.)
91100
const expectedCalldataLength = getExpectedCalldataLengthForEnum(
92101
calldata[calldataIndex],
@@ -98,18 +107,21 @@ function decodeTuple(calldata: string[], typeStr: string, structs: AbiStructs, e
98107
decodedElements.push(enumRes);
99108
calldataIndex += expectedCalldataLength; // Move past the consumed calldata.
100109
break;
101-
case isTypeArray(type):
110+
}
111+
case isTypeArray(type): {
102112
const arrayType = getArrayType(type);
103113
const arrayRes = decodeCalldataValue([calldata[calldataIndex]], arrayType, structs, enums);
104114
decodedElements.push(arrayRes);
105115
calldataIndex += 1;
106116
break;
107-
default:
117+
}
118+
default: {
108119
const result = decodeBaseType(type, calldata[calldataIndex]);
109120
decodedElements.push(result);
110121
calldataIndex += 1;
122+
}
111123
}
112-
}
124+
});
113125

114126
return decodedElements;
115127
}
@@ -119,19 +131,20 @@ function decodeTuple(calldata: string[], typeStr: string, structs: AbiStructs, e
119131
* @param calldata The calldata array.
120132
* @returns The decoded byte array.
121133
*/
134+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
122135
function decodeByteArray(calldata: string[]): ByteArray {
123136
// Extract the length of the data array from the first element.
124137
const dataLength = parseInt(calldata[0], 10);
125-
138+
126139
// Extract the data array elements based on the extracted length.
127140
const data = calldata.slice(1, 1 + dataLength).map((str) => parseInt(str, 10));
128-
141+
129142
// The pending_word is the second-to-last element in the original array.
130143
const pending_word = parseInt(calldata[1 + dataLength], 10);
131-
144+
132145
// The pending_word_len is the last element in the original array.
133146
const pending_word_len = parseInt(calldata[2 + dataLength], 10);
134-
147+
135148
// Construct and return the ByteArray object.
136149
return {
137150
data,
@@ -207,7 +220,7 @@ function decodeCalldataValue(
207220
// CairoResult decoding
208221
if (isTypeResult(type)) {
209222
const matches = type.match(/Result<(.+),\s*(.+)>/);
210-
assert(matches !== null && matches.length > 2, `Type "${type}" is not a valid Option type.`)
223+
assert(matches !== null && matches.length > 2, `Type "${type}" is not a valid Option type.`);
211224

212225
const okType = matches[1];
213226
const errType = matches[2];
@@ -242,7 +255,7 @@ function decodeArray(
242255
const elementType = getArrayType(arrayType);
243256
const elements = [];
244257

245-
for (let i = 0; i < calldata.length; i++) {
258+
for (let i = 0; i < calldata.length; i += 1) {
246259
elements.push(decodeCalldataValue([calldata[i]], elementType, structs, enums));
247260
}
248261

@@ -270,12 +283,12 @@ function decodeStruct(
270283
let index = 0;
271284
const result: ParsedStruct = {};
272285

273-
for (const field of structAbi.members) {
286+
structAbi.members.forEach((field) => {
274287
const fieldType = field.type;
275288
const fieldCalldata = calldataSegment.slice(index, index + 1);
276289
result[field.name] = decodeCalldataValue(fieldCalldata[0], fieldType, structs, enums);
277290
index += 1;
278-
}
291+
});
279292

280293
return result;
281294
}
@@ -293,22 +306,28 @@ function decodeEnum(calldataValues: string[], enumName: string, enums: AbiEnums)
293306
assert(enumDefinition !== null, `Enum with name ${enumName} not found.`);
294307

295308
const variantIndex = parseInt(calldataValues[0], 10);
296-
assert(variantIndex >=0 && variantIndex < enumDefinition.variants.length, `Variant index ${variantIndex} out of range for enum ${enumName}.`);
309+
assert(
310+
variantIndex >= 0 && variantIndex < enumDefinition.variants.length,
311+
`Variant index ${variantIndex} out of range for enum ${enumName}.`
312+
);
297313

298314
const variant = enumDefinition.variants[variantIndex];
299315

300316
// Determine the enum type and decode accordingly
301317
switch (enumName) {
302318
case 'CairoOption':
303319
switch (variant.name) {
304-
case 'None':
320+
case 'None': {
305321
return new CairoOption(CairoOptionVariant.None);
306-
default: // "Some"
322+
}
323+
default: {
324+
// "Some"
307325
// const someValue = calldataValues[1]; // Placeholder logic.
308326
const someValue = decodeCalldataValue(calldataValues.slice(1), variant.type, {}, enums);
309327
return new CairoOption(CairoOptionVariant.Some, someValue);
328+
}
310329
}
311-
case 'CairoResult':
330+
case 'CairoResult': {
312331
// const resultValue = calldataValues[1]; // Placeholder logic.
313332
const resultValue = decodeCalldataValue(calldataValues.slice(1), variant.type, {}, enums);
314333

@@ -318,9 +337,11 @@ function decodeEnum(calldataValues: string[], enumName: string, enums: AbiEnums)
318337
default: // "Err"
319338
return new CairoResult(CairoResultVariant.Err, resultValue);
320339
}
321-
322-
default: // Handling CairoCustomEnum or simple enum types without associated data.
340+
}
341+
default: {
342+
// Handling CairoCustomEnum or simple enum types without associated data.
323343
return new CairoCustomEnum({ activeVariant: variant.name, variant: variant.name });
344+
}
324345
}
325346
}
326347

@@ -341,12 +362,15 @@ function decodeCairoOption(
341362
const optionIndicator = parseInt(calldata[0], 10);
342363

343364
switch (optionIndicator) {
344-
case 0: // None
345-
return CairoOptionVariant.None ;
346-
default:
347-
// Assuming the value is directly after the indicator
348-
const valueCalldata = calldata.slice(1);
349-
return decodeCalldataValue(valueCalldata, innerType, structs, enums);
365+
case 0: {
366+
// None
367+
return CairoOptionVariant.None;
368+
}
369+
default: {
370+
// Assuming the value is directly after the indicator
371+
const valueCalldata = calldata.slice(1);
372+
return decodeCalldataValue(valueCalldata, innerType, structs, enums);
373+
}
350374
}
351375
}
352376

@@ -369,12 +393,16 @@ function decodeCairoResult(
369393
const resultIndicator = parseInt(calldata[0], 10);
370394

371395
switch (resultIndicator) {
372-
case 0: // Code 0 indicates "Ok"
396+
case 0: {
397+
// Code 0 indicates "Ok"
373398
const okValueCalldata = calldata.slice(1);
374-
return { ok: decodeCalldataValue(okValueCalldata, okType, structs, enums) };
375-
default: // Non-zero code indicates "Err"
399+
return { ok: decodeCalldataValue(okValueCalldata, okType, structs, enums) };
400+
}
401+
default: {
402+
// Non-zero code indicates "Err"
376403
const errValueCalldata = calldata.slice(1);
377404
return { err: decodeCalldataValue(errValueCalldata, errType, structs, enums) };
405+
}
378406
}
379407
}
380408

@@ -420,14 +448,15 @@ export function decodeCalldataField(
420448
structs: AbiStructs,
421449
enums: AbiEnums
422450
): any {
423-
const { name, type } = input;
451+
const { type } = input;
424452

425453
switch (true) {
426-
427454
// Handling Array types
428455
case isTypeArray(type): {
429456
const elementType = getArrayType(type);
430-
return calldata.map(elementCalldata => decodeCalldataValue([elementCalldata], elementType, structs, enums));
457+
return calldata.map((elementCalldata) =>
458+
decodeCalldataValue([elementCalldata], elementType, structs, enums)
459+
);
431460
}
432461

433462
// Handling StarkNet addresses

0 commit comments

Comments
 (0)