Skip to content

Commit

Permalink
reafactor: hardhat scripts, task and config
Browse files Browse the repository at this point in the history
  • Loading branch information
juliopavila committed Aug 20, 2024
1 parent 2662df1 commit 3c53365
Show file tree
Hide file tree
Showing 12 changed files with 275 additions and 152 deletions.
9 changes: 5 additions & 4 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import { HttpNetworkUserConfig } from 'hardhat/types'

dotenv.config()

import './tasks/deploy'
import './tasks/mastercopy-extract'
import './tasks/mastercopy-deploy'
import './tasks/mastercopy-verify'
import "./tasks/deploy-mastercopies";
import "./tasks/deploy-mastercopy";
import "./tasks/extract-mastercopy";
import "./tasks/verify-mastercopies";
import "./tasks/verify-mastercopy";

const { INFURA_KEY, MNEMONIC, ETHERSCAN_API_KEY } = process.env

Expand Down
76 changes: 51 additions & 25 deletions mastercopies.json

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
"test": "hardhat test",
"coverage": "hardhat coverage",
"deploy": "yarn hardhat deploy",
"extract-mastercopy": "yarn run build && yarn hardhat mastercopy:extract",
"deploy-mastercopy": "yarn hardhat mastercopy:deploy --network",
"verify-mastercopy": "yarn hardhat mastercopy:verify --network",
"extract:mastercopy": "yarn run build && yarn hardhat extract:mastercopy",
"deploy:mastercopies": "yarn hardhat deploy:mastercopies --network",
"deploy:mastercopy": "yarn hardhat deploy:mastercopy --network",
"verify:mastercopies": "yarn hardhat verify:mastercopies --network",
"verify:mastercopy": "yarn hardhat verify:mastercopy --network",
"prepack": "yarn run clean && yarn run build",
"prepare": "yarn run clean && yarn run build",
"lint": "yarn lint:sol && yarn lint:ts",
Expand Down
19 changes: 19 additions & 0 deletions tasks/create-EIP1193.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { EIP1193Provider } from "@gnosis-guild/zodiac-core";
import { Signer } from "ethers";
import { EthereumProvider } from "hardhat/types";

export function createEIP1193(
provider: EthereumProvider,
signer: Signer
): EIP1193Provider {
return {
request: async ({ method, params }) => {
if (method == "eth_sendTransaction") {
const { hash } = await signer.sendTransaction((params as any[])[0]);
return hash;
}

return provider.request({ method, params });
},
};
}
16 changes: 16 additions & 0 deletions tasks/deploy-mastercopies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { task } from 'hardhat/config'

import { deployAllMastercopies } from '@gnosis-guild/zodiac-core'
import { createEIP1193 } from './create-EIP1193'

task(
'deploy:mastercopies',
'For every version entry on the artifacts file, deploys a mastercopy into the current network'
).setAction(async (_, hre) => {
const [signer] = await hre.ethers.getSigners()
const provider = createEIP1193(hre.network.provider, signer)

await deployAllMastercopies({
provider,
})
})
59 changes: 59 additions & 0 deletions tasks/deploy-mastercopy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { task, types } from 'hardhat/config'

import {
EIP1193Provider,
deployMastercopy,
readMastercopy,
} from '@gnosis-guild/zodiac-core'
import { createEIP1193 } from './create-EIP1193'

task(
'deploy:mastercopy',
'For every version entry on the artifacts file, deploys a mastercopy into the current network'
)
.addOptionalParam(
'contractVersion',
'The specific version of the contract to deploy',
'latest', // Default value
types.string
)
.setAction(async ({ contractVersion }, hre) => {
const [signer] = await hre.ethers.getSigners()
const provider = createEIP1193(hre.network.provider, signer)

// Deploy the contracts based on the provided version
await deployLatestMastercopyFromDisk(provider, contractVersion)
})

async function deployLatestMastercopyFromDisk(
provider: EIP1193Provider,
version?: string
) {
const contract = 'Delay'
try {
// Read the artifact for the specific contract and version
const artifact = readMastercopy({
contractName: contract,
contractVersion: version === 'latest' ? undefined : version,
})

const { address, noop } = await deployMastercopy({
...artifact,
provider,
})

if (noop) {
console.log(
`🔄 ${artifact.contractName}@${version}: Already deployed at ${address}`
)
} else {
console.log(
`🚀 ${artifact.contractName}@${version}: Successfully deployed at ${address}`
)
}
} catch (error) {
console.error(
`⏭️ Skipping deployment of ${contract}@${version}: Version not found.`
)
}
}
62 changes: 0 additions & 62 deletions tasks/deploy.ts

This file was deleted.

21 changes: 8 additions & 13 deletions tasks/mastercopy-extract.ts → tasks/extract-mastercopy.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
import { task } from 'hardhat/config'

import { storeMastercopyArtifact } from '@gnosis-guild/zodiac-core'
import { writeMastercopyFromBuild } from '@gnosis-guild/zodiac-core'

import packageJson from '../package.json'

const AddressOne = '0x0000000000000000000000000000000000000001'

task(
'mastercopy:extract',
'extract:mastercopy',
'Extracts and persists current mastercopy build artifacts'
).setAction(async (_, hre) => {
const contractVersion = packageJson.version
const contractName = 'Delay'
const contractSource = 'contracts/Delay.sol'
const compilerInput = await hre.run('verify:etherscan-get-minimal-input', {
sourceName: contractSource,
})

storeMastercopyArtifact({
contractVersion,
contractName,
compilerInput,
writeMastercopyFromBuild({
contractVersion: packageJson.version,
contractName: 'Delay',
compilerInput: await hre.run('verify:etherscan-get-minimal-input', {
sourceName: 'contracts/Delay.sol',
}),
constructorArgs: {
types: ['address', 'address', 'address', 'uint256', 'uint256'],
values: [AddressOne, AddressOne, AddressOne, 0, 0],
Expand Down
31 changes: 0 additions & 31 deletions tasks/mastercopy-deploy.ts

This file was deleted.

14 changes: 0 additions & 14 deletions tasks/mastercopy-verify.ts

This file was deleted.

19 changes: 19 additions & 0 deletions tasks/verify-mastercopies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { task } from 'hardhat/config'
import { verifyAllMastercopies } from '@gnosis-guild/zodiac-core'

const { ETHERSCAN_API_KEY } = process.env

task(
'verify:mastercopies',
'Verifies all mastercopies from the artifacts file, in the block explorer corresponding to the current network'
).setAction(async (_, hre) => {
if (!ETHERSCAN_API_KEY) {
throw new Error('Missing ENV ETHERSCAN_API_KEY')
}

const chainId = String((await hre.ethers.provider.getNetwork()).chainId)
await verifyAllMastercopies({
apiUrlOrChainId: chainId,
apiKey: ETHERSCAN_API_KEY,
})
})
93 changes: 93 additions & 0 deletions tasks/verify-mastercopy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { task, types } from 'hardhat/config'
import {
readMastercopy,
verifyAllMastercopies,
} from '@gnosis-guild/zodiac-core'
import path from 'path'
import fs from 'fs'
import { cwd } from 'process'

const { ETHERSCAN_API_KEY } = process.env

/**
* Simulates the SDK's `defaultMastercopyArtifactsFile`, pointing to the mastercopies.json file.
*
* @returns {string} The absolute path to the mastercopy artifacts file.
*/
function getMastercopyArtifactsFile(): string {
return path.join(cwd(), 'temp-mastercopies.json')
}

task(
'verify:mastercopy',
'Verifies all mastercopies from the artifacts file in the block explorer corresponding to the current network'
)
.addOptionalParam(
'contractVersion',
'The specific version of the contract to deploy',
'latest', // Default value
types.string
)
.setAction(async ({ contractVersion }, hre) => {
if (!ETHERSCAN_API_KEY) {
throw new Error('Missing ENV ETHERSCAN_API_KEY')
}

const chainId = String((await hre.ethers.provider.getNetwork()).chainId)
await verifyLatestMastercopyFromDisk(chainId, contractVersion)
})

/**
* Verifies the latest mastercopy from disk, handling multiple contracts and versions.
*
* @param {string} chainId - The chain ID of the network.
* @param {string} [version] - The specific version of the contract to verify.
*/
async function verifyLatestMastercopyFromDisk(
chainId: string,
version?: string
) {
const verifyDir = path.dirname(getMastercopyArtifactsFile())

// Ensure the directory exists
if (!fs.existsSync(verifyDir)) {
fs.mkdirSync(verifyDir, { recursive: true })
}

// Define the mastercopyObject with the appropriate type
const mastercopyObject: { [key: string]: { [version: string]: any } } = {}
const contract = 'Delay'
try {
// Read the artifact for the specific contract and version
const latestArtifact = readMastercopy({
contractName: contract,
contractVersion: version === 'latest' ? undefined : version,
})

if (!latestArtifact) {
console.error(
`⏭️ Skipping verify of ${contract}@${version}: Artifact not found.`
)
}

// Add the contract to the expected structure
mastercopyObject[contract] = {
[latestArtifact.contractVersion]: latestArtifact,
}
} catch (error) {
console.error(
`⏭️ Skipping deployment of ${contract}@${version}: Version not found.`
)
}

const tempFilePath = getMastercopyArtifactsFile()
fs.writeFileSync(tempFilePath, JSON.stringify(mastercopyObject, null, 2))

await verifyAllMastercopies({
apiUrlOrChainId: chainId,
apiKey: ETHERSCAN_API_KEY as string,
mastercopyArtifactsFile: tempFilePath,
})

fs.unlinkSync(tempFilePath)
}

0 comments on commit 3c53365

Please sign in to comment.