Skip to content

Commit

Permalink
feat: support encoding for unsigned integers from uint8 to uint256
Browse files Browse the repository at this point in the history
  • Loading branch information
CJ42 committed Nov 24, 2023
1 parent a9a2439 commit 108aede
Show file tree
Hide file tree
Showing 3 changed files with 380 additions and 172 deletions.
3 changes: 0 additions & 3 deletions src/lib/decodeData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ const isValidTupleDefinition = (tupleContent: string): boolean => {
return true;
};

const extractTupleElements = (tupleContent: string): string[] =>
tupleContent.substring(1, tupleContent.length - 1).split(',');

const extractTupleElements = (tupleContent: string): string[] =>
tupleContent.substring(1, tupleContent.length - 1).split(',');

Expand Down
59 changes: 35 additions & 24 deletions src/lib/encoder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,39 @@ describe('encoder', () => {
});
});

describe('`uint128` type', () => {
describe('`uintN` type', () => {
const validTestCases = [
{
valueType: 'uint256',
decodedValue: 1337,
encodedValue:
'0x0000000000000000000000000000000000000000000000000000000000000539',
},
{
valueType: 'uint8',
decodedValue: 10,
encodedValue: '0x0a',
},
{
valueType: 'uint16',
decodedValue: 10,
encodedValue: '0x000a',
},
{
valueType: 'uint24',
decodedValue: 10,
encodedValue: '0x00000a',
},
{
valueType: 'uint32',
decodedValue: 10,
encodedValue: '0x0000000a',
},
{
valueType: 'uint64',
decodedValue: 25,
encodedValue: '0x0000000000000019',
},
{
valueType: 'uint128',
decodedValue: 11,
Expand All @@ -350,30 +381,10 @@ describe('encoder', () => {
);
});
});
});

describe('`uint256` type', () => {
const validTestCases = [
{
valueType: 'uint256',
decodedValue: 1337,
encodedValue:
'0x0000000000000000000000000000000000000000000000000000000000000539',
},
];

validTestCases.forEach((testCase) => {
it(`encodes/decodes: ${testCase.decodedValue} as ${testCase.valueType}`, () => {
const encodedValue = encodeValueType(
testCase.valueType,
testCase.decodedValue,
);

assert.deepStrictEqual(encodedValue, testCase.encodedValue);
assert.deepStrictEqual(
decodeValueType(testCase.valueType, encodedValue),
testCase.decodedValue,
);
['uint1', 'uint129', 'uint257'].forEach((invalidUintType) => {
it(`should error with invalid valueType for type ${invalidUintType}`, async () => {
assert.throws(() => encodeValueType(invalidUintType, '12345'));
});
});
});
Expand Down
Loading

0 comments on commit 108aede

Please sign in to comment.