From 764e3165cfb035ef72498d9e536a0cf251fb2b79 Mon Sep 17 00:00:00 2001 From: Aryan Godara Date: Fri, 3 May 2024 21:07:15 +0530 Subject: [PATCH] fix: reset eslint, remove debug statements, remove array return type --- .eslintignore | 3 +- __tests__/utils/calldataDecode.test.ts | 41 +++-------------------- src/types/lib/contract/abi.ts | 2 -- src/utils/calldata/cairo.ts | 7 +++- src/utils/calldata/calldataDecoder.ts | 45 ++++++++++++++------------ src/utils/calldata/index.ts | 15 +-------- 6 files changed, 37 insertions(+), 76 deletions(-) diff --git a/.eslintignore b/.eslintignore index fabe01b1e..23e705206 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,4 +1,3 @@ node_modules/ dist/ -www/ -src/ \ No newline at end of file +www/ \ No newline at end of file diff --git a/__tests__/utils/calldataDecode.test.ts b/__tests__/utils/calldataDecode.test.ts index 1b02844dc..5dbdfbc90 100644 --- a/__tests__/utils/calldataDecode.test.ts +++ b/__tests__/utils/calldataDecode.test.ts @@ -1,46 +1,18 @@ import { DecodeConfig } from '../../src/types'; import { - // Account, BigNumberish, CairoUint256, - // CairoCustomEnum, - // CairoOption, - // CairoOptionVariant, - // CairoResult, - // CairoResultVariant, - // CairoUint256, - // CairoUint512, CallData, Calldata, - // CompiledSierra, - // Contract, - // DeclareDeployUDCResponse, RawArgsArray, RawArgsObject, - // byteArray, cairo, - // ec, - // hash, - // num, - // selector, - // shortString, - // stark, - // types, - // type Uint512, } from '../../src'; -import { - // compiledC1v2, - // compiledHelloSierra, - compiledComplexSierra, -} from '../config/fixtures'; +import { compiledComplexSierra } from '../config/fixtures'; -const { - // uint256, - tuple, - // isCairo1Abi -} = cairo; +const { tuple } = cairo; describe('Cairo 1', () => { test('should correctly compile and decompile complex data structures', async () => { @@ -131,14 +103,9 @@ describe('Cairo 1', () => { const compiledDataFromObject: Calldata = cd.compile('constructor', myRawArgsObject); const compiledDataFromArray: Calldata = cd.compile('constructor', myRawArgsArray); const decompiledDataFromObject = cd.decompile('constructor', compiledDataFromObject, config); - const decompiledDataFromArray = cd.decompile( - 'constructor', - compiledDataFromArray, - config, - true - ); + const decompiledDataFromArray = cd.decompile('constructor', compiledDataFromArray, config); expect(decompiledDataFromObject).toEqual(myRawArgsObject); - expect(decompiledDataFromArray).toEqual(myRawArgsArray); + expect(decompiledDataFromArray).toEqual(myRawArgsObject); }); }); diff --git a/src/types/lib/contract/abi.ts b/src/types/lib/contract/abi.ts index 72eedab45..d65ec4de9 100644 --- a/src/types/lib/contract/abi.ts +++ b/src/types/lib/contract/abi.ts @@ -1,5 +1,3 @@ -import { Uint256 } from '..'; - /** ABI */ export type Abi = ReadonlyArray; diff --git a/src/utils/calldata/cairo.ts b/src/utils/calldata/cairo.ts index 50922268d..3d38283a2 100644 --- a/src/utils/calldata/cairo.ts +++ b/src/utils/calldata/cairo.ts @@ -96,10 +96,15 @@ export const isTypeUint = (type: string) => Object.values(Uint).includes(type as * @returns {(Uint | null)} - The corresponding Uint enum value or null if not found. */ export const getUintType = (type: string): string | undefined => { - for (const value of Object.values(Uint)) { + const uintValues = Object.values(Uint); + const iterator = uintValues[Symbol.iterator](); + let next = iterator.next(); + while (!next.done) { + const { value } = next; if (value === type) { return value; } + next = iterator.next(); } return undefined; diff --git a/src/utils/calldata/calldataDecoder.ts b/src/utils/calldata/calldataDecoder.ts index 399f9c809..12fcc0676 100644 --- a/src/utils/calldata/calldataDecoder.ts +++ b/src/utils/calldata/calldataDecoder.ts @@ -8,7 +8,6 @@ import { CairoEnum, ParsedStruct, Uint256, - Uint, } from '../../types'; import { CairoUint256 } from '../cairoDataTypes/uint256'; import { CairoUint512 } from '../cairoDataTypes/uint512'; @@ -72,58 +71,62 @@ function decodeBaseTypes( case isTypeUint(type): switch (true) { - case CairoUint256.isAbiType(type): - console.log('got 256 uint value'); + case CairoUint256.isAbiType(type): { const low = it.next().value; const high = it.next().value; const ret = new CairoUint256(low, high); - let configConstructor = config?.['core::integer::u256']; + const configConstructor = config?.['core::integer::u256']; if (configConstructor) { return configConstructor(ret); } + return ret; + } - case CairoUint512.isAbiType(type): + case CairoUint512.isAbiType(type): { const limb0 = it.next().value; const limb1 = it.next().value; const limb2 = it.next().value; const limb3 = it.next().value; return new CairoUint512(limb0, limb1, limb2, limb3).toBigInt(); + } - default: + default: { temp = it.next().value; const configType = getUintType(type); if (configType) { const UintConstructor = config?.[configType]; if (UintConstructor) { return UintConstructor(temp); - } else { - return BigInt(temp); } + return BigInt(temp); } + } } + return BigInt(temp); + case isTypeEthAddress(type): temp = it.next().value; return BigInt(temp); - case isTypeContractAddress(type): + case isTypeContractAddress(type): { temp = it.next().value; temp = toHex(temp); const configConstructor = config?.[type]; if (configConstructor) { return configConstructor(temp); - } else { - return BigInt(temp); } + return BigInt(temp); + } case isTypeBytes31(type): temp = it.next().value; return decodeShortString(temp); - case isTypeSecp256k1Point(type): + case isTypeSecp256k1Point(type): { const xLow = removeHexPrefix(it.next().value).padStart(32, '0'); const xHigh = removeHexPrefix(it.next().value).padStart(32, '0'); const yLow = removeHexPrefix(it.next().value).padStart(32, '0'); @@ -131,21 +134,21 @@ function decodeBaseTypes( const pubK = BigInt(addHexPrefix(xHigh + xLow + yHigh + yLow)); return pubK; + } - case isTypeFelt(type): + case isTypeFelt(type): { temp = String(it.next().value); - console.log('Original temp = ', temp); const configFeltConstructor = config?.['core::felt252']; if (configFeltConstructor) { if (configFeltConstructor === String) return decodeShortString(temp); - else return configFeltConstructor(temp); + return configFeltConstructor(temp); } // Default return BigInt(temp); + } default: - console.log('went to default block for '); temp = it.next().value; return BigInt(temp); } @@ -294,14 +297,12 @@ function decodeCalldataValue( parsedDataArr.push(val); } } - console.log('Returning array: ', parsedDataArr); const configConstructor = config?.[element.name]; if (configConstructor) { const concatenatedString = parsedDataArr.join(''); return concatenatedString; - } else { - return parsedDataArr; } + return parsedDataArr; } // base type @@ -326,9 +327,10 @@ export default function decodeCalldataField( const { name, type } = input; switch (true) { - case isLen(name): + case isLen(name): { const temp = calldataIterator.next().value; return BigInt(temp); + } case (structs && type in structs) || isTypeTuple(type): return decodeCalldataValue(calldataIterator, input, structs, enums, config); @@ -341,8 +343,11 @@ export default function decodeCalldataField( if (isCairo1Type(type)) { return decodeCalldataValue(calldataIterator, input, structs, enums, config); } + break; default: return decodeBaseTypes(type, calldataIterator, config); } + + return null; } diff --git a/src/utils/calldata/index.ts b/src/utils/calldata/index.ts index 5f4f239ca..a5b97825b 100644 --- a/src/utils/calldata/index.ts +++ b/src/utils/calldata/index.ts @@ -248,12 +248,7 @@ export class CallData { * @param calldata Array of strings representing the encoded calldata. * @returns A structured object representing the decoded calldata. */ - public decompile( - method: string, - calldata: string[], - config?: DecodeConfig, - returnArray?: boolean - ): RawArgs { + public decompile(method: string, calldata: string[], config?: DecodeConfig): RawArgs { const abiMethod = this.abi.find( (entry) => entry.name === method && entry.type === 'function' ) as FunctionAbi; @@ -273,14 +268,6 @@ export class CallData { return acc; }, {} as RawArgsObject); - if (returnArray === true) { - const decodedArgsArray: RawArgsArray = []; - abiMethod.inputs.forEach((input) => { - const value = decodedArgs[input.name]; - decodedArgsArray.push(value); - }); - } - return decodedArgs; }