Skip to content

Commit f292241

Browse files
authored
Merge pull request #37 from alchemyplatform/dn/refactor-shared
refactor: move common files to the packages for smoother deployment
2 parents eb85abc + ffb7802 commit f292241

8 files changed

Lines changed: 97 additions & 41 deletions

File tree

common/script/chain.js

Lines changed: 73 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,45 @@
11
const fs = require("fs");
22
const path = require("path");
33
const chainConfigs = require("../chainOptions.json");
4+
const readline = require("readline");
45

56
// Parse command line arguments
67
const args = process.argv.slice(2);
78
const chainIndex = args.indexOf("-c");
89
if (chainIndex === -1 || !args[chainIndex + 1]) {
9-
console.error("Error: Please provide a chain shortname using -c flag");
10-
process.exit(1);
10+
const availableChains = chainConfigs
11+
.map((chain) => ` [${chain.shortName}] ${chain.displayName}`)
12+
.join("\n");
13+
const exampleShortName = chainConfigs[0]?.shortName || "<shortname>";
14+
const exampleUsage = `\nExample: yarn chain -c ${exampleShortName}`;
15+
console.log(
16+
"No chain shortname provided with -c flag.\nAvailable chains (use the value in [brackets]):\n" +
17+
exampleUsage +
18+
availableChains
19+
);
20+
21+
const rl = readline.createInterface({
22+
input: process.stdin,
23+
output: process.stdout,
24+
});
25+
26+
rl.question("\nPlease enter a chain shortname: ", function (answer) {
27+
const chainShortName = answer.trim();
28+
const chainConfig = chainConfigs.find(
29+
(chain) => chain.shortName === chainShortName
30+
);
31+
if (!chainConfig) {
32+
console.error(
33+
`Error: Chain with shortname "${chainShortName}" not found`
34+
);
35+
rl.close();
36+
process.exit(1);
37+
}
38+
writeChainConfig(chainConfig);
39+
rl.close();
40+
process.exit(0);
41+
});
42+
return;
1143
}
1244

1345
const chainShortName = args[chainIndex + 1];
@@ -22,22 +54,44 @@ if (!chainConfig) {
2254
process.exit(1);
2355
}
2456

25-
// Create the new configuration object
26-
const newConfig = {
27-
mainnetName: chainConfig.mainnetName,
28-
mainnetChainId: chainConfig.mainnetChainId,
29-
testnetChainId: chainConfig.testnetChainId,
30-
testnetChainName: chainConfig.testnetChainName,
31-
};
32-
33-
// Write to chainConfig.json
34-
const configPath = path.join(__dirname, "..", "chainConfig.json");
35-
try {
36-
fs.writeFileSync(configPath, JSON.stringify(newConfig, null, 2));
37-
console.log(
38-
`Successfully updated chain configuration to ${chainConfig.displayName}`
57+
writeChainConfig(chainConfig);
58+
59+
function writeChainConfig(chainConfig) {
60+
const newConfig = {
61+
mainnetName: chainConfig.mainnetName,
62+
mainnetChainId: chainConfig.mainnetChainId,
63+
testnetChainId: chainConfig.testnetChainId,
64+
testnetChainName: chainConfig.testnetChainName,
65+
};
66+
const nextjsConfigPath = path.join(
67+
__dirname,
68+
"..",
69+
"..",
70+
"packages",
71+
"nextjs",
72+
"config",
73+
"chainConfig.json"
3974
);
40-
} catch (error) {
41-
console.error("Error writing to chainConfig.json:", error.message);
42-
process.exit(1);
75+
const hardhatConfigPath = path.join(
76+
__dirname,
77+
"..",
78+
"..",
79+
"packages",
80+
"hardhat",
81+
"config",
82+
"chainConfig.json"
83+
);
84+
try {
85+
fs.writeFileSync(nextjsConfigPath, JSON.stringify(newConfig, null, 2));
86+
fs.writeFileSync(hardhatConfigPath, JSON.stringify(newConfig, null, 2));
87+
console.log(
88+
`Successfully updated chain configuration to ${chainConfig.displayName} in both nextjs and hardhat packages`
89+
);
90+
} catch (error) {
91+
console.error(
92+
"Error writing to chainConfig.json in one or both locations:",
93+
error.message
94+
);
95+
process.exit(1);
96+
}
4397
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"mainnetName": "shape-mainnet",
3+
"mainnetChainId": 360,
4+
"testnetChainId": 11011,
5+
"testnetChainName": "shape-sepolia"
6+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"ALCHEMY_GAS_POLICY_ID": "f0d2920d-b0dc-4e55-ab21-2fcb483bc293",
3+
"ALCHEMY_API_KEY": "Aau4vg0U-46T4ZI857caO7otLxX3RVSo"
4+
}

packages/nextjs/hooks/scaffold-alchemy/useTransactor.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ export const useTransactor = (): TransactionFunc => {
6969
const network = chain.id;
7070
// Get full transaction from public client
7171
const publicClient = getPublicClient(config._internal.wagmiConfig);
72+
if (!publicClient) {
73+
throw new Error("Public client not found");
74+
}
7275

7376
notificationId = notification.loading(<TxnNotification message="Sending transaction..." />);
7477
if (typeof tx === "function") {

packages/nextjs/next.config.js

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,17 @@ import { fileURLToPath } from "url";
88
const __filename = fileURLToPath(import.meta.url);
99
const __dirname = path.dirname(__filename);
1010

11-
// Load environment variables
12-
const rootEnvPath = path.resolve(__dirname, "../../.env");
13-
const localEnvPath = path.resolve(__dirname, ".env");
11+
// Determine if we are in development mode
12+
const isDev = process.env.NODE_ENV === "development";
1413

15-
// Load root .env first, then local .env (which will override root values)
16-
dotenv.config({ path: rootEnvPath });
17-
dotenv.config({ path: localEnvPath });
14+
if (isDev) {
15+
const rootEnvPath = path.resolve(__dirname, "../../.env");
16+
const localEnvPath = path.resolve(__dirname, ".env");
17+
dotenv.config({ path: rootEnvPath });
18+
dotenv.config({ path: localEnvPath });
19+
}
1820

19-
// Load common configuration files
20-
const chainConfigPath = path.resolve(__dirname, "../../common/chainConfig.json");
21-
const defaultKeysPath = path.resolve(__dirname, "../../common/defaultKeys.json");
22-
23-
const chainConfig = JSON.parse(fs.readFileSync(chainConfigPath, "utf8"));
24-
const defaultKeys = JSON.parse(fs.readFileSync(defaultKeysPath, "utf8"));
21+
const defaultKeys = JSON.parse(fs.readFileSync(path.resolve(__dirname, "config", "defaultKeys.json"), "utf8"));
2522

2623
/** @type {import('next').NextConfig} */
2724
const nextConfig = {
@@ -38,12 +35,6 @@ const nextConfig = {
3835
return config;
3936
},
4037
env: {
41-
// Chain config (public)
42-
NEXT_PUBLIC_MAINNET_NAME: chainConfig.mainnetName,
43-
NEXT_PUBLIC_MAINNET_CHAIN_ID: String(chainConfig.mainnetChainId),
44-
NEXT_PUBLIC_TESTNET_CHAIN_ID: String(chainConfig.testnetChainId),
45-
NEXT_PUBLIC_TESTNET_CHAIN_NAME: chainConfig.testnetChainName,
46-
4738
// Alchemy config (server-side only)
4839
ALCHEMY_GAS_POLICY_ID: process.env.ALCHEMY_GAS_POLICY_ID || defaultKeys.ALCHEMY_GAS_POLICY_ID,
4940
ALCHEMY_API_KEY: process.env.ALCHEMY_API_KEY || defaultKeys.ALCHEMY_API_KEY,

packages/nextjs/scaffold.config.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1+
import chainConfig from "./config/chainConfig.json";
12
import * as chains from "viem/chains";
23

34
const isProd = process.env.NODE_ENV === "production";
45
const simulateProd = process.env.NEXT_PUBLIC_SIMULATE_PROD === "true";
5-
const chainId =
6-
isProd || simulateProd
7-
? Number(process.env.NEXT_PUBLIC_MAINNET_CHAIN_ID)
8-
: Number(process.env.NEXT_PUBLIC_TESTNET_CHAIN_ID);
6+
const chainId = isProd || simulateProd ? chainConfig.mainnetChainId : chainConfig.testnetChainId;
97
const chain = Object.values(chains).find(chain => chain.id === chainId);
108
if (!chain) {
119
throw new Error(`Chain with ID ${chainId} not found`);

0 commit comments

Comments
 (0)