Skip to content

Commit

Permalink
initial commit after branch change
Browse files Browse the repository at this point in the history
  • Loading branch information
AryanGodara committed Apr 25, 2024
1 parent 23ae319 commit 4b6c6e7
Show file tree
Hide file tree
Showing 3 changed files with 573 additions and 0 deletions.
62 changes: 62 additions & 0 deletions __tests__/utils/calldataDecode.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { parseCalldataField } from '../../src/utils/calldata/requestParser';
import { decodeCalldataField } from '../../src/utils/calldata/calldataDecoder';
import assert from '../../src/utils/assert';
import { CairoUint256 } from '../../src/utils/cairoDataTypes/uint256';
import { AbiEnums, AbiStructs } from '../../src/types';

describe('Encode-Decode CalldataField Flow', () => {
it('correctly encodes and decodes various types', () => {
// Setup
const structs: AbiStructs = {
SimpleStruct: {
type: 'struct',
name: 'SimpleStruct',
size: 2,
members: [
{ name: 'id', type: 'felt', offset: 0 },
{ name: 'value', type: 'core::integer::u256', offset: 0 },
],
},
};
const enums: AbiEnums = {}; // Assuming no enums for this test
const simpleStructValue = { id: felt(123), value: new CairoUint256('0x1') };

Check failure on line 22 in __tests__/utils/calldataDecode.test.ts

View workflow job for this annotation

GitHub Actions / Run test on rpc-devnet / Run tests

Cannot find name 'felt'.

// Create a simple iterator for each value
function* createIterator(value: any): Iterator<any> {
yield value;
}

// Encode
const encodedId = parseCalldataField(
createIterator(simpleStructValue.id),
{ name: 'id', type: 'felt' },
structs,
enums
);
const encodedValue = parseCalldataField(
createIterator(simpleStructValue.value.toApiRequest()),
{ name: 'value', type: 'core::integer::u256' },
structs,
enums
);

// Decode
const decodedId = decodeCalldataField(
typeof encodedId === 'string' ? [encodedId] : encodedId,
{ name: 'id', type: 'felt' },
structs,
enums
);
const decodedValue = decodeCalldataField(
typeof encodedValue === 'string' ? [encodedValue] : encodedValue,
{ name: 'value', type: 'core::integer::u256' },
structs,
enums
);

// Assertions
assert(decodedId.toEqual(simpleStructValue.id));
assert(decodedValue.toBigInt().toEqual(simpleStructValue.value.toBigInt()));
// assert(2 - 1 === 1, 'abcd');
});
});
20 changes: 20 additions & 0 deletions src/utils/cairoDataTypes/uint256.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,24 @@ 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);
}
}
Loading

0 comments on commit 4b6c6e7

Please sign in to comment.