|
| 1 | +import { |
| 2 | + AbiEntry, |
| 3 | + AbiEnums, |
| 4 | + AbiStructs, |
| 5 | + BigNumberish, |
| 6 | + ByteArray, |
| 7 | + CairoEnum, |
| 8 | + ParsedStruct, |
| 9 | + Tupled, |
| 10 | +} from '../../types'; |
| 11 | +import { CairoUint256 } from '../cairoDataTypes/uint256'; |
| 12 | +import { encodeShortString, isText, splitLongString } from '../shortString'; |
| 13 | +import { byteArrayFromString } from './byteArray'; |
| 14 | +import { |
| 15 | + felt, |
| 16 | + getArrayType, |
| 17 | + isTypeArray, |
| 18 | + isTypeBytes31, |
| 19 | + isTypeEnum, |
| 20 | + isTypeOption, |
| 21 | + isTypeResult, |
| 22 | + isTypeStruct, |
| 23 | + isTypeTuple, |
| 24 | +} from './cairo'; |
| 25 | +import { |
| 26 | + CairoCustomEnum, |
| 27 | + CairoOption, |
| 28 | + CairoOptionVariant, |
| 29 | + CairoResult, |
| 30 | + CairoResultVariant, |
| 31 | +} from './enum'; |
| 32 | +import extractTupleMemberTypes from './tuple'; |
| 33 | + |
| 34 | +import { decodeShortString } from '../shortString'; |
| 35 | + |
| 36 | +/** |
| 37 | + * Parse base types |
| 38 | + * @param type Type from ABI |
| 39 | + * @param calldata Calldata provided |
| 40 | + * @returns Parsed value |
| 41 | + */ |
| 42 | +// function decodeBaseType(type: string, calldata: string | string[]): BigNumberish { |
| 43 | +// switch (true) { |
| 44 | +// case CairoUint256.isAbiType(type): |
| 45 | +// // Assuming there exists a method to construct CairoUint256 from calldata |
| 46 | +// return CairoUint256.fromCalldata(calldata).toBigInt(); |
| 47 | +// case isTypeBytes31(type): |
| 48 | +// // Assuming a method to decode a short string back from calldata |
| 49 | +// return decodeShortString(calldata as string); |
| 50 | +// default: |
| 51 | +// // Direct conversion for 'felt' types, assuming calldata is a string representing a number |
| 52 | +// return BigInt(calldata.toString()); |
| 53 | +// } |
| 54 | +// } |
| 55 | + |
| 56 | +function decodeBaseType(type: string, calldata: string | string[]): BigNumberish | CairoUint256 { |
| 57 | + switch (true) { |
| 58 | + case CairoUint256.isAbiType(type): |
| 59 | + // Assuming calldata for CairoUint256 comes as an array of two strings. |
| 60 | + if (!Array.isArray(calldata) || calldata.length !== 2) { |
| 61 | + throw new Error("Expected calldata for CairoUint256 as an array of two strings."); |
| 62 | + } |
| 63 | + // Use the fromCalldata method to construct and return a CairoUint256 instance. |
| 64 | + return CairoUint256.fromCalldata([calldata[0], calldata[1]]); |
| 65 | + |
| 66 | + case isTypeBytes31(type): |
| 67 | + // Handle decoding for bytes31, which might involve reversing the encoding logic. |
| 68 | + // Placeholder for actual decoding logic. |
| 69 | + return decodeShortString(calldata as string); |
| 70 | + |
| 71 | + default: |
| 72 | + // Direct conversion for 'felt' types. Ensure calldata is a string. |
| 73 | + if (typeof calldata !== "string") { |
| 74 | + throw new Error("Expected string calldata for base type decoding."); |
| 75 | + } |
| 76 | + return BigInt(calldata); |
| 77 | + } |
| 78 | +} |
0 commit comments