-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Permissions: Track method signatures for permissions (#23)
Co-authored-by: Facu Spagnuolo <[email protected]>
- Loading branch information
1 parent
e62615f
commit 66e3b64
Showing
7 changed files
with
64 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ build | |
types | ||
node_modules | ||
subgraph.yaml | ||
/src/permissions/dictionary.ts | ||
*.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { ethers } from 'ethers' | ||
import * as fs from 'fs' | ||
|
||
const FUNCTIONS = {} | ||
const DEPENDENCIES_DIR = './node_modules/@mimic-fi' | ||
const DICTIONARY_OUTPUT = './src/permissions/dictionary.ts' | ||
|
||
fs.readdirSync(DEPENDENCIES_DIR).forEach((dependency) => { | ||
const interfacesDir = `${DEPENDENCIES_DIR}/${dependency}/artifacts/contracts/interfaces` | ||
if (!fs.existsSync(interfacesDir)) return | ||
fs.readdirSync(interfacesDir).forEach((file) => processInterfaces(`${interfacesDir}/${file}`)) | ||
}) | ||
|
||
writeOutput() | ||
|
||
function processInterfaces(path: string): void { | ||
if (fs.statSync(path).isDirectory()) | ||
return fs.readdirSync(path).forEach((file) => processInterfaces(`${path}/${file}`)) | ||
|
||
const data = JSON.parse(fs.readFileSync(path).toString()) | ||
if (!data.abi) return | ||
|
||
const iface = new ethers.utils.Interface(data.abi) | ||
data.abi | ||
.filter((input) => input.type === 'function') | ||
.filter((input) => input.stateMutability === 'nonpayable') | ||
.forEach((input) => (FUNCTIONS[iface.getSighash(input.name)] = functionDefinition(input.name, input.inputs))) | ||
} | ||
|
||
function functionDefinition(name: string, inputs: { type: string; name: string }[]): string { | ||
const args = inputs.map((input) => `${input.type} ${input.name}`).join(', ') | ||
return `${name}(${args})` | ||
} | ||
|
||
function writeOutput(): void { | ||
if (fs.existsSync(DICTIONARY_OUTPUT)) fs.unlinkSync(DICTIONARY_OUTPUT) | ||
const content = `// This file was autogenerated at deployment time | ||
const dictionary = new Map<string, string>() | ||
${Object.entries(FUNCTIONS) | ||
.map(([selector, name]) => `dictionary.set('${selector}', '${name}')`) | ||
.join('\n')} | ||
export default dictionary | ||
` | ||
fs.writeFileSync(DICTIONARY_OUTPUT, `${content}`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import dictionary from './dictionary' | ||
|
||
export function getFunctionNameForSelector(selector: string): string { | ||
const functionName = dictionary.has(selector) ? dictionary.get(selector) : 'Unknown' | ||
return functionName | ||
} |