Skip to content

Commit 764e316

Browse files
committed
fix: reset eslint, remove debug statements, remove array return type
1 parent f921352 commit 764e316

File tree

6 files changed

+37
-76
lines changed

6 files changed

+37
-76
lines changed

.eslintignore

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
node_modules/
22
dist/
3-
www/
4-
src/
3+
www/

__tests__/utils/calldataDecode.test.ts

+4-37
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,18 @@
11
import { DecodeConfig } from '../../src/types';
22

33
import {
4-
// Account,
54
BigNumberish,
65
CairoUint256,
7-
// CairoCustomEnum,
8-
// CairoOption,
9-
// CairoOptionVariant,
10-
// CairoResult,
11-
// CairoResultVariant,
12-
// CairoUint256,
13-
// CairoUint512,
146
CallData,
157
Calldata,
16-
// CompiledSierra,
17-
// Contract,
18-
// DeclareDeployUDCResponse,
198
RawArgsArray,
209
RawArgsObject,
21-
// byteArray,
2210
cairo,
23-
// ec,
24-
// hash,
25-
// num,
26-
// selector,
27-
// shortString,
28-
// stark,
29-
// types,
30-
// type Uint512,
3111
} from '../../src';
3212

33-
import {
34-
// compiledC1v2,
35-
// compiledHelloSierra,
36-
compiledComplexSierra,
37-
} from '../config/fixtures';
13+
import { compiledComplexSierra } from '../config/fixtures';
3814

39-
const {
40-
// uint256,
41-
tuple,
42-
// isCairo1Abi
43-
} = cairo;
15+
const { tuple } = cairo;
4416

4517
describe('Cairo 1', () => {
4618
test('should correctly compile and decompile complex data structures', async () => {
@@ -131,14 +103,9 @@ describe('Cairo 1', () => {
131103
const compiledDataFromObject: Calldata = cd.compile('constructor', myRawArgsObject);
132104
const compiledDataFromArray: Calldata = cd.compile('constructor', myRawArgsArray);
133105
const decompiledDataFromObject = cd.decompile('constructor', compiledDataFromObject, config);
134-
const decompiledDataFromArray = cd.decompile(
135-
'constructor',
136-
compiledDataFromArray,
137-
config,
138-
true
139-
);
106+
const decompiledDataFromArray = cd.decompile('constructor', compiledDataFromArray, config);
140107

141108
expect(decompiledDataFromObject).toEqual(myRawArgsObject);
142-
expect(decompiledDataFromArray).toEqual(myRawArgsArray);
109+
expect(decompiledDataFromArray).toEqual(myRawArgsObject);
143110
});
144111
});

src/types/lib/contract/abi.ts

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { Uint256 } from '..';
2-
31
/** ABI */
42
export type Abi = ReadonlyArray<FunctionAbi | EventAbi | StructAbi | any>;
53

src/utils/calldata/cairo.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,15 @@ export const isTypeUint = (type: string) => Object.values(Uint).includes(type as
9696
* @returns {(Uint | null)} - The corresponding Uint enum value or null if not found.
9797
*/
9898
export const getUintType = (type: string): string | undefined => {
99-
for (const value of Object.values(Uint)) {
99+
const uintValues = Object.values(Uint);
100+
const iterator = uintValues[Symbol.iterator]();
101+
let next = iterator.next();
102+
while (!next.done) {
103+
const { value } = next;
100104
if (value === type) {
101105
return value;
102106
}
107+
next = iterator.next();
103108
}
104109

105110
return undefined;

src/utils/calldata/calldataDecoder.ts

+25-20
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
CairoEnum,
99
ParsedStruct,
1010
Uint256,
11-
Uint,
1211
} from '../../types';
1312
import { CairoUint256 } from '../cairoDataTypes/uint256';
1413
import { CairoUint512 } from '../cairoDataTypes/uint512';
@@ -72,80 +71,84 @@ function decodeBaseTypes(
7271

7372
case isTypeUint(type):
7473
switch (true) {
75-
case CairoUint256.isAbiType(type):
76-
console.log('got 256 uint value');
74+
case CairoUint256.isAbiType(type): {
7775
const low = it.next().value;
7876
const high = it.next().value;
7977

8078
const ret = new CairoUint256(low, high);
81-
let configConstructor = config?.['core::integer::u256'];
79+
const configConstructor = config?.['core::integer::u256'];
8280
if (configConstructor) {
8381
return configConstructor(ret);
8482
}
83+
8584
return ret;
85+
}
8686

87-
case CairoUint512.isAbiType(type):
87+
case CairoUint512.isAbiType(type): {
8888
const limb0 = it.next().value;
8989
const limb1 = it.next().value;
9090
const limb2 = it.next().value;
9191
const limb3 = it.next().value;
9292

9393
return new CairoUint512(limb0, limb1, limb2, limb3).toBigInt();
94+
}
9495

95-
default:
96+
default: {
9697
temp = it.next().value;
9798
const configType = getUintType(type);
9899
if (configType) {
99100
const UintConstructor = config?.[configType];
100101
if (UintConstructor) {
101102
return UintConstructor(temp);
102-
} else {
103-
return BigInt(temp);
104103
}
104+
return BigInt(temp);
105105
}
106+
}
106107
}
107108

109+
return BigInt(temp);
110+
108111
case isTypeEthAddress(type):
109112
temp = it.next().value;
110113
return BigInt(temp);
111114

112-
case isTypeContractAddress(type):
115+
case isTypeContractAddress(type): {
113116
temp = it.next().value;
114117
temp = toHex(temp);
115118
const configConstructor = config?.[type];
116119
if (configConstructor) {
117120
return configConstructor(temp);
118-
} else {
119-
return BigInt(temp);
120121
}
122+
return BigInt(temp);
123+
}
121124

122125
case isTypeBytes31(type):
123126
temp = it.next().value;
124127
return decodeShortString(temp);
125128

126-
case isTypeSecp256k1Point(type):
129+
case isTypeSecp256k1Point(type): {
127130
const xLow = removeHexPrefix(it.next().value).padStart(32, '0');
128131
const xHigh = removeHexPrefix(it.next().value).padStart(32, '0');
129132
const yLow = removeHexPrefix(it.next().value).padStart(32, '0');
130133
const yHigh = removeHexPrefix(it.next().value).padStart(32, '0');
131134
const pubK = BigInt(addHexPrefix(xHigh + xLow + yHigh + yLow));
132135

133136
return pubK;
137+
}
134138

135-
case isTypeFelt(type):
139+
case isTypeFelt(type): {
136140
temp = String(it.next().value);
137-
console.log('Original temp = ', temp);
138141
const configFeltConstructor = config?.['core::felt252'];
139142
if (configFeltConstructor) {
140143
if (configFeltConstructor === String) return decodeShortString(temp);
141-
else return configFeltConstructor(temp);
144+
return configFeltConstructor(temp);
142145
}
143146

144147
// Default
145148
return BigInt(temp);
149+
}
146150

147151
default:
148-
console.log('went to default block for ');
149152
temp = it.next().value;
150153
return BigInt(temp);
151154
}
@@ -294,14 +297,12 @@ function decodeCalldataValue(
294297
parsedDataArr.push(val);
295298
}
296299
}
297-
console.log('Returning array: ', parsedDataArr);
298300
const configConstructor = config?.[element.name];
299301
if (configConstructor) {
300302
const concatenatedString = parsedDataArr.join('');
301303
return concatenatedString;
302-
} else {
303-
return parsedDataArr;
304304
}
305+
return parsedDataArr;
305306
}
306307

307308
// base type
@@ -326,9 +327,10 @@ export default function decodeCalldataField(
326327
const { name, type } = input;
327328

328329
switch (true) {
329-
case isLen(name):
330+
case isLen(name): {
330331
const temp = calldataIterator.next().value;
331332
return BigInt(temp);
333+
}
332334

333335
case (structs && type in structs) || isTypeTuple(type):
334336
return decodeCalldataValue(calldataIterator, input, structs, enums, config);
@@ -341,8 +343,11 @@ export default function decodeCalldataField(
341343
if (isCairo1Type(type)) {
342344
return decodeCalldataValue(calldataIterator, input, structs, enums, config);
343345
}
346+
break;
344347

345348
default:
346349
return decodeBaseTypes(type, calldataIterator, config);
347350
}
351+
352+
return null;
348353
}

src/utils/calldata/index.ts

+1-14
Original file line numberDiff line numberDiff line change
@@ -248,12 +248,7 @@ export class CallData {
248248
* @param calldata Array of strings representing the encoded calldata.
249249
* @returns A structured object representing the decoded calldata.
250250
*/
251-
public decompile(
252-
method: string,
253-
calldata: string[],
254-
config?: DecodeConfig,
255-
returnArray?: boolean
256-
): RawArgs {
251+
public decompile(method: string, calldata: string[], config?: DecodeConfig): RawArgs {
257252
const abiMethod = this.abi.find(
258253
(entry) => entry.name === method && entry.type === 'function'
259254
) as FunctionAbi;
@@ -273,14 +268,6 @@ export class CallData {
273268
return acc;
274269
}, {} as RawArgsObject);
275270

276-
if (returnArray === true) {
277-
const decodedArgsArray: RawArgsArray = [];
278-
abiMethod.inputs.forEach((input) => {
279-
const value = decodedArgs[input.name];
280-
decodedArgsArray.push(value);
281-
});
282-
}
283-
284271
return decodedArgs;
285272
}
286273

0 commit comments

Comments
 (0)