Skip to content

Commit

Permalink
feat(calldata): partial implementation of CairoUint256.fromCalldata
Browse files Browse the repository at this point in the history
  • Loading branch information
AryanGodara committed Mar 15, 2024
1 parent fd206a1 commit c238cfe
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/utils/cairoDataTypes/uint256.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Empty file.
78 changes: 78 additions & 0 deletions src/utils/calldata/calldataDecoder.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit c238cfe

Please sign in to comment.