Skip to content

Commit c238cfe

Browse files
committed
feat(calldata): partial implementation of CairoUint256.fromCalldata
1 parent fd206a1 commit c238cfe

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

src/utils/cairoDataTypes/uint256.ts

+18
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,22 @@ export class CairoUint256 {
133133
toApiRequest() {
134134
return [CairoFelt(this.low), CairoFelt(this.high)];
135135
}
136+
137+
/**
138+
* Construct CairoUint256 from calldata
139+
* @param calldata Array of two strings representing the low and high parts of a uint256.
140+
*/
141+
static fromCalldata(calldata: [string, string]): CairoUint256 {
142+
if (calldata.length !== 2) {
143+
throw new Error("Calldata must contain exactly two elements for low and high parts of uint256.");
144+
}
145+
146+
// Validate each part to ensure they are within the acceptable range.
147+
const [low, high] = calldata;
148+
const validatedLow = CairoUint256.validateProps(low, UINT_256_LOW_MIN.toString());
149+
const validatedHigh = CairoUint256.validateProps(high, UINT_256_HIGH_MIN.toString());
150+
151+
// Construct a new instance based on the validated low and high parts.
152+
return new CairoUint256(validatedLow.low, validatedHigh.high);
153+
}
136154
}

src/utils/calldata/apiDataEncoder.ts

Whitespace-only changes.

src/utils/calldata/calldataDecoder.ts

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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

Comments
 (0)