Skip to content

Commit

Permalink
feat: export contract abi on build (#470)
Browse files Browse the repository at this point in the history
## 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
ikemHood authored Oct 3, 2024
1 parent 6b5766b commit 9123ecf
Show file tree
Hide file tree
Showing 17 changed files with 4,345 additions and 5 deletions.
57 changes: 57 additions & 0 deletions .scripts/exportABi.ts
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`);
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ Check out our demo app showcasing a mini marketplace: [ArkProject SDK Demo](http
3. **Build Contracts**

```bash
cd contracts && scarb build --workspace
pnpm run build:contracts
```

4. **Launch Katana**
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"deploy:starknet": "dotenv -- turbo run deploy:starknet",
"deploy:starknet:local": "dotenv -- turbo run deploy:starknet:local --log-prefix=none",
"deploy:starknet:tokens": "dotenv -- turbo run deploy:starknet:tokens --log-prefix=none",
"version:update": "bun .scripts/updateVersion.ts"
"version:update": "bun .scripts/updateVersion.ts",
"build:contracts": "cd contracts && scarb build && bun ../.scripts/exportABi.ts"
},
"dependencies": {
"@ark-project/eslint-config": "workspace:*",
Expand All @@ -40,6 +41,7 @@
"dotenv-cli": "^7.3.0",
"glob": "^10.3.10",
"prettier": "^3.1.0",
"starknet": "6.7.0",
"prettier-plugin-sort-imports": "^1.8.1",
"turbo": "^1.13.3"
},
Expand Down
Loading

0 comments on commit 9123ecf

Please sign in to comment.