diff --git a/src/utils/cairoDataTypes/uint256.ts b/src/utils/cairoDataTypes/uint256.ts index 1c51c4c63..20277a4b9 100644 --- a/src/utils/cairoDataTypes/uint256.ts +++ b/src/utils/cairoDataTypes/uint256.ts @@ -133,4 +133,22 @@ export class CairoUint256 { toApiRequest() { return [CairoFelt(this.low), CairoFelt(this.high)]; } + + /** + * Construct CairoUint256 from calldata + * @param calldata Array of two strings representing the low and high parts of a uint256. + */ + static fromCalldata(calldata: [string, string]): CairoUint256 { + if (calldata.length !== 2) { + throw new Error("Calldata must contain exactly two elements for low and high parts of uint256."); + } + + // Validate each part to ensure they are within the acceptable range. + const [low, high] = calldata; + const validatedLow = CairoUint256.validateProps(low, UINT_256_LOW_MIN.toString()); + const validatedHigh = CairoUint256.validateProps(high, UINT_256_HIGH_MIN.toString()); + + // Construct a new instance based on the validated low and high parts. + return new CairoUint256(validatedLow.low, validatedHigh.high); + } } diff --git a/src/utils/calldata/apiDataEncoder.ts b/src/utils/calldata/apiDataEncoder.ts new file mode 100644 index 000000000..e69de29bb diff --git a/src/utils/calldata/calldataDecoder.ts b/src/utils/calldata/calldataDecoder.ts new file mode 100644 index 000000000..ba3f19327 --- /dev/null +++ b/src/utils/calldata/calldataDecoder.ts @@ -0,0 +1,78 @@ +import { + AbiEntry, + AbiEnums, + AbiStructs, + BigNumberish, + ByteArray, + CairoEnum, + ParsedStruct, + Tupled, +} from '../../types'; +import { CairoUint256 } from '../cairoDataTypes/uint256'; +import { encodeShortString, isText, splitLongString } from '../shortString'; +import { byteArrayFromString } from './byteArray'; +import { + felt, + getArrayType, + isTypeArray, + isTypeBytes31, + isTypeEnum, + isTypeOption, + isTypeResult, + isTypeStruct, + isTypeTuple, +} from './cairo'; +import { + CairoCustomEnum, + CairoOption, + CairoOptionVariant, + CairoResult, + CairoResultVariant, +} from './enum'; +import extractTupleMemberTypes from './tuple'; + +import { decodeShortString } from '../shortString'; + +/** + * Parse base types + * @param type Type from ABI + * @param calldata Calldata provided + * @returns Parsed value + */ +// function decodeBaseType(type: string, calldata: string | string[]): BigNumberish { +// switch (true) { +// case CairoUint256.isAbiType(type): +// // Assuming there exists a method to construct CairoUint256 from calldata +// return CairoUint256.fromCalldata(calldata).toBigInt(); +// case isTypeBytes31(type): +// // Assuming a method to decode a short string back from calldata +// return decodeShortString(calldata as string); +// default: +// // Direct conversion for 'felt' types, assuming calldata is a string representing a number +// return BigInt(calldata.toString()); +// } +// } + +function decodeBaseType(type: string, calldata: string | string[]): BigNumberish | CairoUint256 { + switch (true) { + case CairoUint256.isAbiType(type): + // Assuming calldata for CairoUint256 comes as an array of two strings. + if (!Array.isArray(calldata) || calldata.length !== 2) { + throw new Error("Expected calldata for CairoUint256 as an array of two strings."); + } + // Use the fromCalldata method to construct and return a CairoUint256 instance. + return CairoUint256.fromCalldata([calldata[0], calldata[1]]); + + case isTypeBytes31(type): + // Handle decoding for bytes31, which might involve reversing the encoding logic. + // Placeholder for actual decoding logic. + return decodeShortString(calldata as string); + + default: + // Direct conversion for 'felt' types. Ensure calldata is a string. + if (typeof calldata !== "string") { + throw new Error("Expected string calldata for base type decoding."); + } + return BigInt(calldata); + } +}