Skip to content
This repository has been archived by the owner on Jan 8, 2025. It is now read-only.

Commit

Permalink
(feat) add the .env to hardhat with configurable network
Browse files Browse the repository at this point in the history
  • Loading branch information
oceans404 committed Apr 16, 2024
1 parent 121f5ce commit 56b4868
Show file tree
Hide file tree
Showing 7 changed files with 337 additions and 24 deletions.
6 changes: 4 additions & 2 deletions packages/hardhat/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ ALCHEMY_API_KEY=
DEPLOYER_PRIVATE_KEY=
ETHERSCAN_API_KEY=

NEXT_PUBLIC_NILLION_BLOCKCHAIN_RPC_ENDPOINT=
NEXT_PUBLIC_NILLION_WALLET_PRIVATE_KEY=
# Auto populated by yarn nillion-devnet
NILLION_CONFIG_RPC_URL=
NILLION_CONFIG_DEPLOYER_PRIVATE_KEY=
NILLION_CONFIG_CHAIN_ID=
1 change: 1 addition & 0 deletions packages/hardhat/deployments/configNetwork/.chainId
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
31337
256 changes: 256 additions & 0 deletions packages/hardhat/deployments/configNetwork/YourContract.json

Large diffs are not rendered by default.

Large diffs are not rendered by default.

19 changes: 15 additions & 4 deletions packages/hardhat/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ const deployerPrivateKey =
// If not set, it uses ours Etherscan default API key.
const etherscanApiKey = process.env.ETHERSCAN_API_KEY || "DNXJA8RX2Q3VZ4URQIWP7Z68CJXQZSC6AW";

// Add any other testnet from config
const configNetwork =
process.env.NILLION_CONFIG_RPC_URL &&
process.env.NILLION_CONFIG_DEPLOYER_PRIVATE_KEY &&
process.env.NILLION_CONFIG_CHAIN_ID
? {
configNetwork: {
url: process.env.NILLION_CONFIG_RPC_URL,
accounts: [process.env.NILLION_CONFIG_DEPLOYER_PRIVATE_KEY],
chainId: parseInt(process.env.NILLION_CONFIG_CHAIN_ID),
},
}
: {};

const config: HardhatUserConfig = {
solidity: {
version: "0.8.17",
Expand Down Expand Up @@ -126,10 +140,7 @@ const config: HardhatUserConfig = {
url: "https://sepolia.publicgoods.network",
accounts: [deployerPrivateKey],
},
configTestnet: {
url: process.env.NEXT_PUBLIC_NILLION_BLOCKCHAIN_RPC_ENDPOINT,
accounts: [process.env.NEXT_PUBLIC_NILLION_WALLET_PRIVATE_KEY || deployerPrivateKey],
},
...configNetwork,
},
// configuration for harhdat-verify plugin
etherscan: {
Expand Down
2 changes: 1 addition & 1 deletion packages/nextjs/scaffold.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export type ScaffoldConfig = {
walletAutoConnect: boolean;
};

const fallbackChain = chains.sepolia;
const fallbackChain = chains.hardhat;

const rpcUrls = process.env.NEXT_PUBLIC_NILLION_BLOCKCHAIN_RPC_ENDPOINT
? {
Expand Down
39 changes: 22 additions & 17 deletions packages/nillion/bootstrap-local-env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ OUTFILE=$(mktemp);
PIDFILE=$(mktemp);

"$NILLION_DEVNET" --seed scaffold-nillion >"$OUTFILE" & echo $! >"$PIDFILE";
ENV_TO_UPDATE=".env ../nextjs/.env"
ENV_TO_UPDATE_WITH_HARDHAT=".env ../nextjs/.env ../hardhat/.env"
NEXTJS_ENV=".env ../nextjs/.env"
HARDHAT_ENV=".env ../hardhat/.env"
echo "--------------------"
echo "Updating your ${ENV_TO_UPDATE_WITH_HARDHAT} files with nillion-devnet environment info... This may take a minute."
echo "Updating your .env files with nillion-devnet environment info... This may take a minute."
echo "--------------------"
time_limit=160
while true; do
Expand Down Expand Up @@ -100,30 +100,35 @@ for ((i=1; i<=$num_node_keys; i++)); do
nodekey_file=$(mktemp)
"$NILLION_CLI" "$NILLION_CLI_COMMAND_NODE_KEYGEN" "$nodekey_file"
NODEKEY_FILES+=("$nodekey_file")
update_env "NEXT_PUBLIC_NILLION_NODEKEY_PATH_PARTY_$i" "$nodekey_file" $ENV_TO_UPDATE
update_env "NEXT_PUBLIC_NILLION_NODEKEY_TEXT_PARTY_$i" "$(log_file_contents $nodekey_file)" $ENV_TO_UPDATE
update_env "NEXT_PUBLIC_NILLION_NODEKEY_PATH_PARTY_$i" "$nodekey_file" $NEXTJS_ENV
update_env "NEXT_PUBLIC_NILLION_NODEKEY_TEXT_PARTY_$i" "$(log_file_contents $nodekey_file)" $NEXTJS_ENV
done

# Generate user keys and add to .env - ex: NEXT_PUBLIC_NILLION_USERKEY_PATH_PARTY_1
for ((i=1; i<=$num_user_keys; i++)); do
userkey_file=$(mktemp)
"$NILLION_CLI" "$NILLION_CLI_COMMAND_USER_KEYGEN" "$userkey_file"
USERKEY_FILES+=("$userkey_file")
update_env "NEXT_PUBLIC_NILLION_USERKEY_PATH_PARTY_$i" "$userkey_file" $ENV_TO_UPDATE
update_env "NEXT_PUBLIC_NILLION_USERKEY_TEXT_PARTY_$i" "$(log_file_contents $userkey_file)" $ENV_TO_UPDATE
update_env "NEXT_PUBLIC_NILLION_USERKEY_PATH_PARTY_$i" "$userkey_file" $NEXTJS_ENV
update_env "NEXT_PUBLIC_NILLION_USERKEY_TEXT_PARTY_$i" "$(log_file_contents $userkey_file)" $NEXTJS_ENV
done

echo "🔑 Node key and user keys have been generated and added to .env"

# Add environment variables to .env
update_env "NEXT_PUBLIC_NILLION_WEBSOCKETS" "$WEBSOCKET" $ENV_TO_UPDATE
update_env "NEXT_PUBLIC_NILLION_CLUSTER_ID" "$CLUSTER_ID" $ENV_TO_UPDATE
update_env "NEXT_PUBLIC_NILLION_BLOCKCHAIN_RPC_ENDPOINT" "$PAYMENTS_RPC" $ENV_TO_UPDATE_WITH_HARDHAT
update_env "NEXT_PUBLIC_NILLION_BLINDING_FACTORS_MANAGER_SC_ADDRESS" "$PAYMENTS_BF_ADDR" $ENV_TO_UPDATE
update_env "NEXT_PUBLIC_NILLION_PAYMENTS_SC_ADDRESS" "$PAYMENTS_SC_ADDR" $ENV_TO_UPDATE
update_env "NEXT_PUBLIC_NILLION_CHAIN_ID" "$PAYMENTS_CHAIN" $ENV_TO_UPDATE
update_env "NEXT_PUBLIC_NILLION_WALLET_PRIVATE_KEY" "$WALLET_PRIVATE_KEY" $ENV_TO_UPDATE_WITH_HARDHAT
update_env "NEXT_PUBLIC_NILLION_BOOTNODE_MULTIADDRESS" "$BOOT_MULTIADDR" $ENV_TO_UPDATE
# Add environment variables to NextJs .env
update_env "NEXT_PUBLIC_NILLION_WEBSOCKETS" "$WEBSOCKET" $NEXTJS_ENV
update_env "NEXT_PUBLIC_NILLION_CLUSTER_ID" "$CLUSTER_ID" $NEXTJS_ENV
update_env "NEXT_PUBLIC_NILLION_BLOCKCHAIN_RPC_ENDPOINT" "$PAYMENTS_RPC" $NEXTJS_ENV
update_env "NEXT_PUBLIC_NILLION_BLINDING_FACTORS_MANAGER_SC_ADDRESS" "$PAYMENTS_BF_ADDR" $NEXTJS_ENV
update_env "NEXT_PUBLIC_NILLION_PAYMENTS_SC_ADDRESS" "$PAYMENTS_SC_ADDR" $NEXTJS_ENV
update_env "NEXT_PUBLIC_NILLION_CHAIN_ID" "$PAYMENTS_CHAIN" $NEXTJS_ENV
update_env "NEXT_PUBLIC_NILLION_WALLET_PRIVATE_KEY" "$WALLET_PRIVATE_KEY" $NEXTJS_ENV
update_env "NEXT_PUBLIC_NILLION_BOOTNODE_MULTIADDRESS" "$BOOT_MULTIADDR" $NEXTJS_ENV

# Add environment variables to Hardhat .env
update_env "NILLION_CONFIG_RPC_URL" "$PAYMENTS_RPC" $HARDHAT_ENV
update_env "NILLION_CONFIG_DEPLOYER_PRIVATE_KEY" "$WALLET_PRIVATE_KEY" $HARDHAT_ENV
update_env "NILLION_CONFIG_CHAIN_ID" "$PAYMENTS_CHAIN" $HARDHAT_ENV

echo "Running at process pid: $(pgrep -f $NILLION_DEVNET)"

Expand All @@ -142,5 +147,5 @@ echo "-------------------------------------------------------"
echo "-----------🦆 CONNECTED TO NILLION-DEVNET 🦆-----------"
echo "-------------------------------------------------------"

echo "ℹ️ Your $ENV_TO_UPDATE file configurations were updated with nillion-devnet connection variables: websocket, cluster id, keys, blockchain info"
echo "ℹ️ Your NextJS and Hardhat .env file configurations were updated with nillion-devnet connection variables: websocket, cluster id, keys, blockchain info"
echo "💻 The Nillion devnet is still running behind the scenes; to spin down the Nillion devnet at any time, run 'yarn nillion-devnet-stop'"

0 comments on commit 56b4868

Please sign in to comment.