Skip to content

Commit

Permalink
feat: add bytesList to generated array helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
bowd committed Aug 21, 2024
1 parent 59c5471 commit 9a3e100
Show file tree
Hide file tree
Showing 2 changed files with 365 additions and 73 deletions.
26 changes: 23 additions & 3 deletions bin/gen-array-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,22 @@ const TARGETS = {
},
bytes32s: {
type: 'bytes32'
},
bytesList: {
type: 'bytes',
skipContains: true
}
} as const;
// @dev MAX_SIZE controls the maximum size of the array that can be generated
// the script will create overloaded functions with 1 to MAX_SIZE arguments.
const MAX_SIZE = 8;
const MEMORY_IN_ARGS = {
"bytes": true,
"uint256": false,
"address": false,
"bytes4": false,
"bytes32": false
}

const HEADER = `
// AUTOGENERATED FILE (bin/gen-array-helpers.ts) DON'T MODIFY DIRECTLY
Expand All @@ -39,10 +50,17 @@ const templateFnAssignments = (size: number): string => {
return content;
}

const maybeMemory = (type: string): string => {
if (MEMORY_IN_ARGS[type]) {
return ' memory';
}
return ''
}

const templateFnArgs = (type: string, size: number): string => {
let content = '';
for (let j = 0; j < size; j++) {
content += ` ${type} e${j}${j < size - 1 ? ',\n' : ''}`
content += ` ${type}${maybeMemory(type)} e${j}${j < size - 1 ? ',\n' : ''}`
}
return content;
}
Expand All @@ -58,7 +76,7 @@ ${templateFnAssignments(size)}
`

const templateContains = (type: string) => `
function contains(${type}[] memory self, ${type} value) pure returns(bool) {
function contains(${type}[] memory self, ${type}${maybeMemory(type)} value) pure returns(bool) {
for (uint256 i = 0; i < self.length; i++) {
if (self[i] == value) {
return true;
Expand All @@ -74,7 +92,9 @@ for (const [fn, config] of Object.entries(TARGETS)) {
for (let i = 1; i <= MAX_SIZE; i++) {
fileContent += templateFunction(fn, type, i);
}
fileContent += templateContains(type);
if (!config.skipContains) {
fileContent += templateContains(type);
}
}

fs.writeFileSync("src/Array.sol", fileContent);
Loading

0 comments on commit 9a3e100

Please sign in to comment.