forked from safe-global/safe-client-gateway
-
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.
Merge branch 'safe-global:main' into main
- Loading branch information
Showing
84 changed files
with
4,345 additions
and
1,338 deletions.
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
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 |
---|---|---|
|
@@ -48,3 +48,6 @@ lerna-debug.log* | |
|
||
# Database mounted volume | ||
data | ||
|
||
# ABIs | ||
/abis |
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 |
---|---|---|
@@ -1 +1,4 @@ | ||
/.yarn | ||
|
||
# ABIs | ||
/abis |
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,83 @@ | ||
/* eslint-disable @typescript-eslint/no-var-requires */ | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
|
||
/** | ||
* This generates const TypeScript ABIs for each asset in | ||
* `@safe-global/safe-deployments` package for `viem` to infer. | ||
* | ||
* Although it is possible to get a singleton programmatically and | ||
* import the JSON directly, neither is strictly typed. | ||
* | ||
* Once it is possible to import JSON "as const", the deployments | ||
* package should be updated to return the singletons as such. | ||
* | ||
* @see https://github.com/microsoft/TypeScript/issues/32063 | ||
*/ | ||
|
||
// Path to directory containing JSON assets | ||
const assetsDir = path.join( | ||
process.cwd(), | ||
'node_modules', | ||
'@safe-global', | ||
'safe-deployments', | ||
'dist', | ||
'assets', | ||
); | ||
|
||
// Path to directory where ABIs will be written | ||
const outputDir = path.join(process.cwd(), 'abis', 'safe'); | ||
|
||
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type | ||
function main() { | ||
// Remove any existing ABIs | ||
try { | ||
fs.rmSync(outputDir, { recursive: true }); | ||
} catch { | ||
// Swallow error if directory does not exist (first run) | ||
} | ||
|
||
// For each version... | ||
for (const version of fs.readdirSync(assetsDir)) { | ||
const versionOutputDir = path.join(outputDir, version); | ||
|
||
fs.mkdirSync(versionOutputDir, { recursive: true }); | ||
|
||
const versionDir = path.join(assetsDir, version); | ||
|
||
// ...parse the ABI for each asset | ||
for (const assetFile of fs.readdirSync(versionDir)) { | ||
// Read the asset JSON | ||
const assetPath = path.join(assetsDir, version, assetFile); | ||
const assetJson = fs.readFileSync(assetPath, 'utf8'); | ||
|
||
// Parse the asset JSON | ||
const { contractName, abi } = JSON.parse(assetJson); | ||
|
||
// Write the ABI to a file | ||
const fileName = `${contractName}.abi.ts`; | ||
const filePath = path.join(versionOutputDir, fileName); | ||
|
||
// It is generally better to use the Stream API for larger files | ||
// but as we are storing the JSON in memory, this is likely of | ||
// minimal benefit. As this script runs on build, we need not | ||
// worry too much about performance though. | ||
const stream = fs.createWriteStream(filePath); | ||
|
||
// Write formatted ABI to file | ||
stream.write( | ||
'// This file is auto-generated by scripts/generate-abis.js\nexport default ', | ||
); | ||
stream.write(JSON.stringify(abi, null, 2)); | ||
|
||
// Most important step: assert ABI as readonly | ||
stream.write(' as const;'); | ||
|
||
stream.end(); | ||
} | ||
} | ||
|
||
console.log('ABIs generated successfully!'); | ||
} | ||
|
||
main(); |
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
Oops, something went wrong.