Skip to content

Commit

Permalink
templatise foundry.toml file
Browse files Browse the repository at this point in the history
  • Loading branch information
technophile-04 committed Jan 2, 2025
1 parent 70d2d2a commit 2947be3
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 62 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { withDefaults } from "../../../../utils.js";

const content = ({ profileDefaults, rpcEndpoints, etherscan, fmt, extraConfig }) => `[profile.default]
src = 'contracts'
out = 'out'
libs = ['lib']
fs_permissions = [{ access = "read-write", path = "./"}]
${profileDefaults.filter(Boolean).join("\n")}
[rpc_endpoints]
default_network = "http://127.0.0.1:8545"
localhost = "http://127.0.0.1:8545"
mainnet = "https://eth-mainnet.alchemyapi.io/v2/\${ALCHEMY_API_KEY}"
sepolia = "https://eth-sepolia.g.alchemy.com/v2/\${ALCHEMY_API_KEY}"
arbitrum = "https://arb-mainnet.g.alchemy.com/v2/\${ALCHEMY_API_KEY}"
arbitrumSepolia = "https://arb-sepolia.g.alchemy.com/v2/\${ALCHEMY_API_KEY}"
optimism = "https://opt-mainnet.g.alchemy.com/v2/\${ALCHEMY_API_KEY}"
optimismSepolia = "https://opt-sepolia.g.alchemy.com/v2/\${ALCHEMY_API_KEY}"
polygon = "https://polygon-mainnet.g.alchemy.com/v2/\${ALCHEMY_API_KEY}"
polygonMumbai = "https://polygon-mumbai.g.alchemy.com/v2/\${ALCHEMY_API_KEY}"
polygonZkEvm = "https://zkevm-rpc.com"
polygonZkEvmTestnet = "https://rpc.public.zkevm-test.net"
gnosis = "https://rpc.gnosischain.com"
chiado = "https://rpc.chiadochain.net"
base = "https://mainnet.base.org"
baseSepolia = "https://sepolia.base.org"
scrollSepolia = "https://sepolia-rpc.scroll.io"
scroll = "https://rpc.scroll.io"
pgn = "https://rpc.publicgoods.network"
pgnTestnet = "https://sepolia.publicgoods.network"
${rpcEndpoints.filter(Boolean).join("\n")}
[etherscan]
polygonMumbai = { key = "\${ETHERSCAN_API_KEY}" }
sepolia = { key = "\${ETHERSCAN_API_KEY}" }
${etherscan.filter(Boolean).join("\n")}
[fmt]
line_length = 120
tab_width = 4
quote_style = "double"
bracket_spacing = true
int_types = "long"
${fmt.filter(Boolean).join("\n")}
${extraConfig.filter(Boolean).join("\n")}
# See more config options https://github.com/foundry-rs/foundry/tree/master/config`;

export default withDefaults(content, {
profileDefaults: "",
rpcEndpoints: "",
etherscan: "",
fmt: "",
extraConfig: "",
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { readdirSync, statSync, readFileSync, existsSync, mkdirSync, writeFileSync } from "fs";
import {
readdirSync,
statSync,
readFileSync,
existsSync,
mkdirSync,
writeFileSync,
} from "fs";
import { join, dirname } from "path";
import { fileURLToPath } from "url";
import { format } from "prettier";
Expand Down Expand Up @@ -44,7 +51,12 @@ function getDeploymentHistory(broadcastPath) {

// Sort files to process them in chronological order
const runFiles = files
.filter(file => file.startsWith("run-") && file.endsWith(".json") && !file.includes("run-latest"))
.filter(
(file) =>
file.startsWith("run-") &&
file.endsWith(".json") &&
!file.includes("run-latest")
)
.sort((a, b) => {
// Extract run numbers and compare them
const runA = parseInt(a.match(/run-(\d+)/)?.[1] || "0");
Expand Down Expand Up @@ -72,11 +84,17 @@ function getDeploymentHistory(broadcastPath) {
}

function getArtifactOfContract(contractName) {
const current_path_to_artifacts = join(__dirname, "..", `out/${contractName}.sol`);
const current_path_to_artifacts = join(
__dirname,
"..",
`out/${contractName}.sol`
);

if (!existsSync(current_path_to_artifacts)) return null;

const artifactJson = JSON.parse(readFileSync(`${current_path_to_artifacts}/${contractName}.json`));
const artifactJson = JSON.parse(
readFileSync(`${current_path_to_artifacts}/${contractName}.json`)
);

return artifactJson;
}
Expand All @@ -87,7 +105,9 @@ function getInheritedFromContracts(artifact) {
for (const astNode of artifact.ast.nodes) {
if (astNode.nodeType == "ContractDefinition") {
if (astNode.baseContracts.length > 0) {
inheritedFromContracts = astNode.baseContracts.map(({ baseName }) => baseName.name);
inheritedFromContracts = astNode.baseContracts.map(
({ baseName }) => baseName.name
);
}
}
}
Expand Down Expand Up @@ -119,20 +139,25 @@ function processAllDeployments(broadcastPath) {
const scriptFolders = getDirectories(broadcastPath);
const allDeployments = new Map();

scriptFolders.forEach(scriptFolder => {
scriptFolders.forEach((scriptFolder) => {
const scriptPath = join(broadcastPath, scriptFolder);
const chainFolders = getDirectories(scriptPath);

chainFolders.forEach(chainId => {
chainFolders.forEach((chainId) => {
const chainPath = join(scriptPath, chainId);
const deploymentHistory = getDeploymentHistory(chainPath);

deploymentHistory.forEach(deployment => {
const timestamp = parseInt(deployment.deploymentFile.match(/run-(\d+)/)?.[1] || "0");
deploymentHistory.forEach((deployment) => {
const timestamp = parseInt(
deployment.deploymentFile.match(/run-(\d+)/)?.[1] || "0"
);
const key = `${chainId}-${deployment.contractName}`;

// Only update if this deployment is newer
if (!allDeployments.has(key) || timestamp > allDeployments.get(key).timestamp) {
if (
!allDeployments.has(key) ||
timestamp > allDeployments.get(key).timestamp
) {
allDeployments.set(key, {
...deployment,
timestamp,
Expand All @@ -146,7 +171,7 @@ function processAllDeployments(broadcastPath) {

const allContracts = {};

allDeployments.forEach(deployment => {
allDeployments.forEach((deployment) => {
const { chainId, contractName } = deployment;
const artifact = getArtifactOfContract(contractName);

Expand Down Expand Up @@ -176,15 +201,19 @@ function main() {
const deployments = {};

// Load existing deployments from deployments directory
Deploymentchains.forEach(chain => {
Deploymentchains.forEach((chain) => {
if (!chain.endsWith(".json")) return;
chain = chain.slice(0, -5);
var deploymentObject = JSON.parse(readFileSync(`${current_path_to_deployments}/${chain}.json`));
var deploymentObject = JSON.parse(
readFileSync(`${current_path_to_deployments}/${chain}.json`)
);
deployments[chain] = deploymentObject;
});

// Process all deployments from all script folders
const allGeneratedContracts = processAllDeployments(current_path_to_broadcast);
const allGeneratedContracts = processAllDeployments(
current_path_to_broadcast
);

// Update contract keys based on deployments if they exist
Object.entries(allGeneratedContracts).forEach(([chainId, contracts]) => {
Expand All @@ -206,12 +235,19 @@ function main() {
}

// Generate the deployedContracts content
const fileContent = Object.entries(allGeneratedContracts).reduce((content, [chainId, chainConfig]) => {
return `${content}${parseInt(chainId).toFixed(0)}:${JSON.stringify(chainConfig, null, 2)},`;
}, "");
const fileContent = Object.entries(allGeneratedContracts).reduce(
(content, [chainId, chainConfig]) => {
return `${content}${parseInt(chainId).toFixed(0)}:${JSON.stringify(
chainConfig,
null,
2
)},`;
},
""
);

// Write the files
const fileTemplate = importPath => `
const fileTemplate = (importPath) => `
${generatedContractComment}
import { GenericContractsDeclaration } from "${importPath}";
Expand All @@ -224,10 +260,12 @@ function main() {
`${NEXTJS_TARGET_DIR}deployedContracts.ts`,
format(fileTemplate("~~/utils/scaffold-eth/contract"), {
parser: "typescript",
}),
})
);

console.log(`📝 Updated TypeScript contract definition file on ${NEXTJS_TARGET_DIR}deployedContracts.ts`);
console.log(
`📝 Updated TypeScript contract definition file on ${NEXTJS_TARGET_DIR}deployedContracts.ts`
);
}

try {
Expand Down

0 comments on commit 2947be3

Please sign in to comment.