Skip to content

Commit

Permalink
Improve readability
Browse files Browse the repository at this point in the history
  • Loading branch information
bpierre committed Apr 11, 2024
1 parent dfcd7a2 commit 415a356
Showing 1 changed file with 18 additions and 21 deletions.
39 changes: 18 additions & 21 deletions contracts/utils/deployment-artifacts-to-next-env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Options:

const ZAddress = z.string().regex(/^0x[0-9a-fA-F]{40}$/);

const DeploymentContext = z.object({
const ZDeploymentContext = z.object({
options: z.object({
chainId: z.number(),
deployer: z.string(), // can be an address or a private key
Expand All @@ -26,6 +26,8 @@ const DeploymentContext = z.object({
deployedContracts: z.record(ZAddress),
});

type DeploymentContext = z.infer<typeof ZDeploymentContext>;

const NULL_ADDRESS = `0x${"0".repeat(40)}`;

export async function main() {
Expand All @@ -50,27 +52,22 @@ function objectToEnvironmentVariables(object: Record<string, unknown>) {
.join("\n");
}

function deploymentContextToNextEnvVariables(deploymentContext: z.infer<typeof DeploymentContext>) {
const contracts = Object.fromEntries(
Object
.entries(deploymentContext.deployedContracts)
.reduce((entries, [contractName, address]) => {
const envVar = contractNameToNextEnvVariable(contractName);
if (envVar) {
entries.push([envVar, address]);
}
return entries;
}, [] as [string, string][])
.filter(Boolean),
);
function deploymentContextToNextEnvVariables({ deployedContracts, options }: DeploymentContext) {
const nextEnvVariables: Record<string, string> = {
NEXT_PUBLIC_CHAIN_ID: String(options.chainId),
};

contracts.NEXT_PUBLIC_CONTRACT_FUNCTION_CALLER = NULL_ADDRESS;
contracts.NEXT_PUBLIC_CONTRACT_HINT_HELPERS = NULL_ADDRESS;
for (const [contractName, address] of Object.entries(deployedContracts)) {
const envVarName = contractNameToNextEnvVariable(contractName);
if (envVarName) {
nextEnvVariables[envVarName] = address;
}
}

return {
NEXT_PUBLIC_CHAIN_ID: deploymentContext.options.chainId,
...contracts,
};
nextEnvVariables.NEXT_PUBLIC_CONTRACT_FUNCTION_CALLER = NULL_ADDRESS;
nextEnvVariables.NEXT_PUBLIC_CONTRACT_HINT_HELPERS = NULL_ADDRESS;

return nextEnvVariables;
}

function contractNameToNextEnvVariable(contractName: string) {
Expand Down Expand Up @@ -113,7 +110,7 @@ function parseDeploymentContext(content: string) {
process.exit(1);
}

const context = DeploymentContext.safeParse(json);
const context = ZDeploymentContext.safeParse(json);
if (!context.success) {
console.error("\nInvalid deployment context provided.\n");
console.error(
Expand Down

0 comments on commit 415a356

Please sign in to comment.