Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/better handling env variables #55

Merged
merged 17 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# The mnemonic phrase used to derive the wallet's private keys
# MNEMONIC=...

# Index of the account generated from the mnemonic to be used as the Voucher Manager
# IEXEC_VOUCHER_MANAGER_ACCOUNT_INDEX=...
Le-Caignec marked this conversation as resolved.
Show resolved Hide resolved

# Index of the account generated from the mnemonic to be used as the Voucher Minter
# IEXEC_VOUCHER_MINTER_ACCOUNT_INDEX=...

# The private key for accessing the production account
# PROD_PRIVATE_KEY=...
Le-Caignec marked this conversation as resolved.
Show resolved Hide resolved

# Flag to indicate whether to use a local blockchain fork (set to 'true' for local fork development)
# IS_LOCAL_FORK=...

# Override the default iExec PoCo (Proof-of-Contribution) contract address
# IEXEC_POCO_ADDRESS=...

# Indicates whether the deployment involves a factory contract
# FACTORY=...
zguesmi marked this conversation as resolved.
Show resolved Hide resolved
12 changes: 6 additions & 6 deletions deploy/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ContractFactory } from 'ethers';
import { deployments, ethers, upgrades } from 'hardhat';
import { HardhatRuntimeEnvironment } from 'hardhat/types';
import deploymentConfig from '../config/deployment';
import { isLocalFork } from '../hardhat.config';
import { env } from '../env';
Le-Caignec marked this conversation as resolved.
Show resolved Hide resolved
import * as voucherHubUtils from '../scripts/voucherHubUtils';
import * as voucherUtils from '../scripts/voucherUtils';
import {
Expand All @@ -19,7 +19,7 @@ import {
} from '../typechain-types';

export default async function (hre: HardhatRuntimeEnvironment) {
if (isLocalFork) {
if (env.IS_LOCAL_FORK) {
Le-Caignec marked this conversation as resolved.
Show resolved Hide resolved
/**
* This fixes following issue when deploying to a local Bellecour fork:
* `ProviderError: No known hardfork for execution on historical block [...] in chain with id 134.`
Expand Down Expand Up @@ -182,15 +182,15 @@ export async function getDeploymentConfig(chainId: number) {
// Read default config of the target chain.
const config = deploymentConfig[chainId];
// Override config if required.
if (process.env.IEXEC_POCO_ADDRESS) {
config.pocoAddress = process.env.IEXEC_POCO_ADDRESS;
if (env.IEXEC_POCO_ADDRESS) {
config.pocoAddress = env.IEXEC_POCO_ADDRESS;
}
// Check final config.
if (!ethers.isAddress(config.pocoAddress)) {
throw new Error('Valid PoCo address must be provided');
}
if (process.env.FACTORY) {
config.factory = process.env.FACTORY == 'true';
if (env.FACTORY) {
config.factory = env.FACTORY;
}
return config;
}
42 changes: 42 additions & 0 deletions env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import 'dotenv/config';
import { z } from 'zod';
Le-Caignec marked this conversation as resolved.
Show resolved Hide resolved

const envSchema = z.object({
IEXEC_VOUCHER_HUB_ADDRESS: z
Le-Caignec marked this conversation as resolved.
Show resolved Hide resolved
.string()
.regex(/^0x[a-fA-F0-9]{40}$/, 'Invalid Ethereum address')
.optional(),
IEXEC_VOUCHER_MANAGER_ACCOUNT_INDEX: z
.string()
.optional()
.refine(
(val) => val === undefined || /^\d+$/.test(val),
'Must be a numeric index if provided',
),
zguesmi marked this conversation as resolved.
Show resolved Hide resolved
IEXEC_VOUCHER_MINTER_ACCOUNT_INDEX: z
.string()
.optional()
.refine(
(val) => val === undefined || /^\d+$/.test(val),
'Must be a numeric index if provided',
),
IS_LOCAL_FORK: z.preprocess(
(val) => (typeof val === 'string' ? val.toLowerCase() === 'true' : val === true),
z.boolean().default(false),
),
zguesmi marked this conversation as resolved.
Show resolved Hide resolved
MNEMONIC: z.string().optional(),
PROD_PRIVATE_KEY: z
.string()
.regex(/^([a-fA-F0-9]{64})$/, 'Invalid private key format')
.optional(),
IEXEC_POCO_ADDRESS: z
.string()
.regex(/^0x[a-fA-F0-9]{40}$/, 'Invalid Ethereum address if provided')
Le-Caignec marked this conversation as resolved.
Show resolved Hide resolved
.optional(),
FACTORY: z.preprocess(
(val) => (typeof val === 'string' ? val.toLowerCase() === 'true' : val === true),
z.boolean().default(false),
),
});

export const env = envSchema.parse(process.env);
16 changes: 8 additions & 8 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import {
defaultLocalhostNetworkParams,
} from 'hardhat/internal/core/config/default-config';
import 'solidity-docgen';
import { env } from './env';
import { forceZeroGasPriceWithSolidityCoverage } from './scripts/utils/modify-solidity-coverage-lib-api-js';

const managerAccount = Number(process.env.IEXEC_VOUCHER_MANAGER_ACCOUNT_INDEX) || null;
const minterAccount = Number(process.env.IEXEC_VOUCHER_MINTER_ACCOUNT_INDEX) || null;
export const isLocalFork = process.env.LOCAL_FORK == 'true';
const managerAccount = Number(env.IEXEC_VOUCHER_MANAGER_ACCOUNT_INDEX) || null;
Le-Caignec marked this conversation as resolved.
Show resolved Hide resolved
const minterAccount = Number(env.IEXEC_VOUCHER_MINTER_ACCOUNT_INDEX) || null;
const bellecourBlockscoutUrl = 'https://blockscout.bellecour.iex.ec';

const config: HardhatUserConfig = {
Expand Down Expand Up @@ -50,9 +50,9 @@ const config: HardhatUserConfig = {
hardhat: {
hardfork: 'berlin', // No EIP-1559 before London fork
accounts: {
mnemonic: process.env.MNEMONIC || HARDHAT_NETWORK_MNEMONIC,
mnemonic: env.MNEMONIC || HARDHAT_NETWORK_MNEMONIC,
},
...(isLocalFork && {
...(env.IS_LOCAL_FORK && {
forking: {
url: 'https://bellecour.iex.ec',
},
Expand All @@ -65,7 +65,7 @@ const config: HardhatUserConfig = {
...defaultHardhatNetworkParams,
...defaultLocalhostNetworkParams,
accounts: 'remote', // will use accounts set in hardhat network config
...(isLocalFork && {
...(env.IS_LOCAL_FORK && {
chainId: 134,
}),
gasPrice: 0,
Expand All @@ -74,15 +74,15 @@ const config: HardhatUserConfig = {
chainId: 65535,
url: 'http://localhost:8545',
accounts: {
mnemonic: process.env.MNEMONIC || '',
mnemonic: env.MNEMONIC || '',
},
gasPrice: 0, // Get closer to Bellecour network
},
bellecour: {
chainId: 134,
url: 'https://bellecour.iex.ec',
accounts: [
process.env.PROD_PRIVATE_KEY ||
env.PROD_PRIVATE_KEY ||
'0x0000000000000000000000000000000000000000000000000000000000000000',
],
gasPrice: 0,
Expand Down
27 changes: 26 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@
"prettier-plugin-organize-imports": "^4.0.0",
"prettier-plugin-solidity": "^1.4.1",
"sol2uml": "^2.5.19",
"solidity-docgen": "^0.6.0-beta.36"
"solidity-docgen": "^0.6.0-beta.36",
"dotenv": "^16.4.7",
"zod": "^3.24.1"
},
"devDependencies_comment": {
"@sol2uml": "UML generation does not work properly after v2.5.19. Try before bump."
Expand Down
Loading