Skip to content

Commit

Permalink
fix: reset eslint, remove debug statements, remove array return type
Browse files Browse the repository at this point in the history
  • Loading branch information
AryanGodara committed May 3, 2024
1 parent f921352 commit 764e316
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 76 deletions.
3 changes: 1 addition & 2 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
node_modules/
dist/
www/
src/
www/
41 changes: 4 additions & 37 deletions __tests__/utils/calldataDecode.test.ts
Original file line number Diff line number Diff line change
@@ -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 () => {
Expand Down Expand Up @@ -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);
});
});
2 changes: 0 additions & 2 deletions src/types/lib/contract/abi.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { Uint256 } from '..';

/** ABI */
export type Abi = ReadonlyArray<FunctionAbi | EventAbi | StructAbi | any>;

Expand Down
7 changes: 6 additions & 1 deletion src/utils/calldata/cairo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
45 changes: 25 additions & 20 deletions src/utils/calldata/calldataDecoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
CairoEnum,
ParsedStruct,
Uint256,
Uint,
} from '../../types';
import { CairoUint256 } from '../cairoDataTypes/uint256';
import { CairoUint512 } from '../cairoDataTypes/uint512';
Expand Down Expand Up @@ -72,80 +71,84 @@ 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');
const yHigh = removeHexPrefix(it.next().value).padStart(32, '0');
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);
}
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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;
}
15 changes: 1 addition & 14 deletions src/utils/calldata/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down

0 comments on commit 764e316

Please sign in to comment.