-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: export contract abi on build (#470)
## Description generate and export all contract ABIs. ## What type of PR is this? (check all applicable) - [✅] 🍕 Feature (`feat:`) ## Related Tickets & Documents Fixes #466 ## Added tests? - [✅] 👍 Tested Manually ## Added to documentation? - [✅] 📜 README.md - [ ] 📓 Documentation - [ ] 🙅 no documentation needed
- Loading branch information
Showing
17 changed files
with
4,345 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import fs from "fs"; | ||
import path from "path"; | ||
import prettier from "prettier"; | ||
import type { CompiledSierra } from "starknet"; | ||
|
||
const TARGET_DIR = path.join(__dirname, "../packages/core/src/abis"); | ||
const deploymentsDir = path.join(__dirname, "../contracts/target/dev"); | ||
|
||
const generatedContractComment = `/** | ||
* This file is autogenerated by | ||
* You should not edit it manually or your changes might be overwritten. | ||
*/`; | ||
|
||
//get all contracts with .contract_class.json | ||
const contracts = fs.readdirSync(deploymentsDir).filter((file) => | ||
file.endsWith(".contract_class.json") | ||
); | ||
console.log('\x1b[32m Generating ABI export... \x1b[0m') | ||
|
||
const exportedContracts: string[] = []; | ||
|
||
//export all contract abi | ||
contracts.forEach(async (contract) => { | ||
const contractPath = path.join(deploymentsDir, contract); | ||
const contractContent = fs.readFileSync(contractPath, "utf8"); | ||
const contractData: CompiledSierra = JSON.parse(contractContent); | ||
const abi = JSON.stringify(contractData.abi.filter((item) => item.type !== "l1_handler")); | ||
|
||
if (!fs.existsSync(TARGET_DIR)) { | ||
fs.mkdirSync(TARGET_DIR); | ||
} | ||
|
||
const contractName = contract.replace(".contract_class.json", ""); | ||
exportedContracts.push(contractName); | ||
|
||
const targetPath = path.join(TARGET_DIR, `${contractName}.ts`); | ||
const fileContent = `${generatedContractComment}\n\nexport default ${abi} as const;`; | ||
|
||
//save abi | ||
fs.writeFileSync(targetPath, await prettier.format(fileContent, { parser: "typescript" })); | ||
}); | ||
|
||
// Create index.ts file | ||
const indexPath = path.join(TARGET_DIR, "index.ts"); | ||
const indexContent = `${generatedContractComment} | ||
${exportedContracts.map(contract => `import ${contract} from './${contract}.js';`).join('\n')} | ||
export { | ||
${exportedContracts.join(',\n ')} | ||
}; | ||
`; | ||
|
||
fs.writeFileSync(indexPath, await prettier.format(indexContent, { parser: "typescript" })); | ||
|
||
console.log(`\x1b[32m exported contract abi's to ${TARGET_DIR} \x1b[0m`); | ||
console.log(`\x1b[32m created index.ts file in ${TARGET_DIR} \x1b[0m`); |
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.