Skip to content

Commit

Permalink
refactor: improve method to extract tuple elements
Browse files Browse the repository at this point in the history
  • Loading branch information
CJ42 committed Nov 9, 2023
1 parent 380513a commit 9a8137a
Showing 1 changed file with 14 additions and 25 deletions.
39 changes: 14 additions & 25 deletions src/lib/decodeData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
* @author Callum Grindle <@CallumGrindle>
* @date 2023
*/

import { isHexStrict } from 'web3-utils';
import { COMPACT_BYTES_ARRAY_STRING } from '../constants/constants';

Expand All @@ -44,17 +43,8 @@ const isValidTupleDefinition = (tupleContent: string): boolean => {
return true;
};

const extractValueTypesFromTuple = (tupleContent: string): string[] => {
let valueTypeToDecode = tupleContent;

if (tupleContent.includes(COMPACT_BYTES_ARRAY_STRING)) {
valueTypeToDecode = tupleContent.replace(COMPACT_BYTES_ARRAY_STRING, '');
}

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

export const isValidTuple = (valueType: string, valueContent: string) => {
if (
Expand All @@ -64,14 +54,14 @@ export const isValidTuple = (valueType: string, valueContent: string) => {
return false;
}

// At this stage, we can assume the user is trying to use a tuple, let's throw errors instead of returning
// false
// At this stage, we can assume the user is trying to use a tuple,
// let's throw errors instead of returning false

const valueTypeParts = extractValueTypesFromTuple(valueType);
// Sanitize the string to keep only the tuple, if we are dealing with `CompactBytesArray`
const valueTypeToDecode = valueType.replace(COMPACT_BYTES_ARRAY_STRING, '');

const valueContentParts = valueContent
.substring(1, valueContent.length - 1)
.split(',');
const valueTypeParts = extractTupleElements(valueTypeToDecode);
const valueContentParts = extractTupleElements(valueContent);

const tuplesValidValueTypes = [
'bytes2',
Expand Down Expand Up @@ -148,20 +138,19 @@ export const decodeTupleKeyValue = (
): Array<string> => {
// We assume data has already been validated at this stage

const valueTypeParts = extractValueTypesFromTuple(valueType);
// Sanitize the string to keep only the tuple, if we are dealing with `CompactBytesArray`
const valueTypeToDecode = valueType.replace(COMPACT_BYTES_ARRAY_STRING, '');

const valueContentParts = valueContent
.substring(1, valueContent.length - 1)
.split(',');
const valueTypeParts = extractTupleElements(valueTypeToDecode);
const valueContentParts = extractTupleElements(valueContent);

const bytesLengths: number[] = [];

valueTypeParts.forEach((valueTypePart) => {
const regexMatch = valueTypePart.match(tupleValueTypesRegex);

// if we are dealing with `bytesN`
if (regexMatch) {
bytesLengths.push(parseInt(regexMatch[1], 10));
}
if (regexMatch) bytesLengths.push(parseInt(regexMatch[1], 10));

if (valueTypePart === 'address') bytesLengths.push(20);
});
Expand Down

0 comments on commit 9a8137a

Please sign in to comment.