This repository has been archived by the owner on Dec 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8391150
commit 175b6b1
Showing
13 changed files
with
284 additions
and
206 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,51 @@ | ||
# zkSync Hardhat project | ||
# zkSync Hardhat project template | ||
|
||
This project was scaffolded with [zksync-cli](https://github.com/matter-labs/zksync-cli). | ||
|
||
## Project structure | ||
## Project Layout | ||
|
||
- `/contracts`: smart contracts. | ||
- `/deploy`: deployment and contract interaction scripts. | ||
- `/test`: test files | ||
- `hardhat.config.ts`: configuration file. | ||
- `/contracts`: Contains solidity smart contracts. | ||
- `/deploy`: Scripts for contract deployment and interaction. | ||
- `/test`: Test files. | ||
- `hardhat.config.ts`: Configuration settings. | ||
|
||
## Commands | ||
## How to Use | ||
|
||
- `yarn hardhat compile` will compile the contracts. | ||
- `yarn run deploy` will execute the deployment script `/deploy/deploy-greeter.ts`. Requires [environment variable setup](#environment-variables). | ||
- `yarn run greet` will execute the script `/deploy/use-greeter.ts` which interacts with the Greeter contract deployed. | ||
- `yarn test`: run tests. **Check test requirements below.** | ||
- `yarn hardhat compile`: Compiles contracts. | ||
- `yarn deploy`: Deploys using script `/deploy/deploy.ts`. | ||
- `yarn interact`: Interacts with the deployed contract using `/deploy/interact.ts`. | ||
- `yarn test`: Tests the contracts. | ||
|
||
Both `yarn run deploy` and `yarn run greet` are configured in the `package.json` file and run `yarn hardhat deploy-zksync`. | ||
Note: Both `yarn deploy` and `yarn interact` are set in the `package.json`. You can also run your files directly, for example: `yarn hardhat deploy-zksync --script deploy.ts` | ||
|
||
### Environment variables | ||
### Environment Settings | ||
|
||
In order to prevent users to leak private keys, this project includes the `dotenv` package which is used to load environment variables. It's used to load the wallet private key, required to run the deploy script. | ||
To keep private keys safe, this project pulls in environment variables from `.env` files. Primarily, it fetches the wallet's private key. | ||
|
||
To use it, rename `.env.example` to `.env` and enter your private key. | ||
Rename `.env.example` to `.env` and fill in your private key: | ||
|
||
``` | ||
WALLET_PRIVATE_KEY=123cde574ccff.... | ||
WALLET_PRIVATE_KEY=your_private_key_here... | ||
``` | ||
|
||
### Local testing | ||
### Network Support | ||
|
||
In order to run test, you need to start the zkSync local environment. Please check [this section of the docs](https://v2-docs.zksync.io/api/hardhat/testing.html#prerequisites) which contains all the details. | ||
`hardhat.config.ts` comes with a list of networks to deploy and test contracts. Add more by adjusting the `networks` section in the `hardhat.config.ts`. To make a network the default, set the `defaultNetwork` to its name. You can also override the default using the `--network` option, like: `hardhat test --network dockerizedNode`. | ||
|
||
If you do not start the zkSync local environment, the tests will fail with error `Error: could not detect network (event="noNetwork", code=NETWORK_ERROR, version=providers/5.7.2)` | ||
### Local Tests | ||
|
||
## Official Links | ||
Running `yarn test` by default runs the [zkSync In-memory Node](https://era.zksync.io/docs/tools/testing/era-test-node.html) provided by the [@matterlabs/hardhat-zksync-node](https://era.zksync.io/docs/tools/hardhat/hardhat-zksync-node.html) tool. | ||
|
||
- [Website](https://zksync.io/) | ||
- [Documentation](https://v2-docs.zksync.io/dev/) | ||
Important: zkSync In-memory Node currently supports only the L2 node. If contracts also need L1, use another testing environment like Dockerized Node. Refer to [test documentation](https://era.zksync.io/docs/tools/testing/) for details. | ||
|
||
## Useful Links | ||
|
||
- [Docs](https://era.zksync.io/docs/dev/) | ||
- [Official Site](https://zksync.io/) | ||
- [GitHub](https://github.com/matter-labs) | ||
- [Twitter](https://twitter.com/zksync) | ||
- [Discord](https://discord.gg/nMaPGrDDwk) | ||
- [Discord](https://join.zksync.dev/) | ||
|
||
## License | ||
|
||
This project is under the [MIT](./LICENSE) license. |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { deployContract } from "./utils"; | ||
|
||
// An example of a basic deploy script | ||
// It will deploy a Greeter contract to selected network | ||
// as well as verify it on Block Explorer if possible for the network | ||
export default async function () { | ||
const contractArtifactName = "Greeter"; | ||
const constructorArguments = ["Hi there!"]; | ||
await deployContract(contractArtifactName, constructorArguments); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import * as hre from "hardhat"; | ||
import { getWallet } from "./utils"; | ||
import { ethers } from "ethers"; | ||
|
||
// Address of the contract to interact with | ||
const CONTRACT_ADDRESS = ""; | ||
if (!CONTRACT_ADDRESS) throw "⛔️ Provide address of the contract to interact with!"; | ||
|
||
// An example of a script to interact with the contract | ||
export default async function () { | ||
console.log(`Running script to interact with contract ${CONTRACT_ADDRESS}`); | ||
|
||
// Load compiled contract info | ||
const contractArtifact = await hre.artifacts.readArtifact("Greeter"); | ||
|
||
// Initialize contract instance for interaction | ||
const contract = new ethers.Contract( | ||
CONTRACT_ADDRESS, | ||
contractArtifact.abi, | ||
getWallet() // Interact with the contract on behalf of this wallet | ||
); | ||
|
||
// Run contract read function | ||
const response = await contract.greet(); | ||
console.log(`Current message is: ${response}`); | ||
|
||
// Run contract write function | ||
const transaction = await contract.setGreeting("Hello people!"); | ||
console.log(`Transaction hash of setting new message: ${transaction.hash}`); | ||
|
||
// Wait until transaction is processed | ||
await transaction.wait(); | ||
|
||
// Read message after transaction | ||
console.log(`The message now is: ${await contract.greet()}`); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import { Provider, Wallet } from "zksync-web3"; | ||
import * as hre from "hardhat"; | ||
import { Deployer } from "@matterlabs/hardhat-zksync-deploy"; | ||
import dotenv from "dotenv"; | ||
import { formatEther } from "ethers/lib/utils"; | ||
import { BigNumberish } from "ethers"; | ||
|
||
import "@matterlabs/hardhat-zksync-node/dist/type-extensions"; | ||
import "@matterlabs/hardhat-zksync-verify/dist/src/type-extensions"; | ||
|
||
// Load env file | ||
dotenv.config(); | ||
|
||
export const getProvider = () => { | ||
const rpcUrl = hre.network.config.url; | ||
if (!rpcUrl) throw `⛔️ RPC URL wasn't found in "${hre.network.name}"! Please add a "url" field to the network config in hardhat.config.ts`; | ||
|
||
// Initialize zkSync Provider | ||
const provider = new Provider(rpcUrl); | ||
|
||
return provider; | ||
} | ||
|
||
export const getWallet = (privateKey?: string) => { | ||
if (!privateKey) { | ||
// Get wallet private key from .env file | ||
if (!process.env.WALLET_PRIVATE_KEY) throw "⛔️ Wallet private key wasn't found in .env file!"; | ||
} | ||
|
||
const provider = getProvider(); | ||
|
||
// Initialize zkSync Wallet | ||
const wallet = new Wallet(privateKey ?? process.env.WALLET_PRIVATE_KEY!, provider); | ||
|
||
return wallet; | ||
} | ||
|
||
export const verifyEnoughBalance = async (wallet: Wallet, amount: BigNumberish) => { | ||
// Check if the wallet has enough balance | ||
const balance = await wallet.getBalance(); | ||
if (balance.lt(amount)) throw `⛔️ Wallet balance is too low! Required ${formatEther(amount)} ETH, but current ${wallet.address} balance is ${formatEther(balance)} ETH`; | ||
} | ||
|
||
/** | ||
* @param {string} data.contract The contract's path and name. E.g., "contracts/Greeter.sol:Greeter" | ||
*/ | ||
export const verifyContract = async (data: { | ||
address: string, | ||
contract: string, | ||
constructorArguments: string, | ||
bytecode: string | ||
}) => { | ||
const verificationRequestId: number = await hre.run("verify:verify", { | ||
...data, | ||
noCompile: true, | ||
}); | ||
return verificationRequestId; | ||
} | ||
|
||
type DeployContractOptions = { | ||
/** | ||
* If true, the deployment process will not print any logs | ||
*/ | ||
silent?: boolean | ||
/** | ||
* If true, the contract will not be verified on Block Explorer | ||
*/ | ||
noVerify?: boolean | ||
/** | ||
* If specified, the contract will be deployed using this wallet | ||
*/ | ||
wallet?: Wallet | ||
} | ||
export const deployContract = async (contractArtifactName: string, constructorArguments?: any[], options?: DeployContractOptions) => { | ||
const log = (message: string) => { | ||
if (!options?.silent) console.log(message); | ||
} | ||
|
||
log(`\nStarting deployment process of "${contractArtifactName}"...`); | ||
|
||
const wallet = options?.wallet ?? getWallet(); | ||
const deployer = new Deployer(hre, wallet); | ||
const artifact = await deployer.loadArtifact(contractArtifactName).catch((error) => { | ||
if (error?.message?.includes(`Artifact for contract "${contractArtifactName}" not found.`)) { | ||
console.error(error.message); | ||
throw `⛔️ Please make sure you have compiled your contracts or specified the correct contract name!`; | ||
} else { | ||
throw error; | ||
} | ||
}); | ||
|
||
// Estimate contract deployment fee | ||
const deploymentFee = await deployer.estimateDeployFee(artifact, constructorArguments || []); | ||
log(`Estimated deployment cost: ${formatEther(deploymentFee)} ETH`); | ||
|
||
// Check if the wallet has enough balance | ||
await verifyEnoughBalance(wallet, deploymentFee); | ||
|
||
// Deploy the contract to zkSync | ||
const contract = await deployer.deploy(artifact, constructorArguments); | ||
|
||
const constructorArgs = contract.interface.encodeDeploy(constructorArguments); | ||
const fullContractSource = `${artifact.sourceName}:${artifact.contractName}`; | ||
|
||
// Display contract deployment info | ||
log(`\n"${artifact.contractName}" was successfully deployed:`); | ||
log(` - Contract address: ${contract.address}`); | ||
log(` - Contract source: ${fullContractSource}`); | ||
log(` - Encoded constructor arguments: ${constructorArgs}\n`); | ||
|
||
if (!options?.noVerify && hre.network.config.verifyURL) { | ||
log(`Requesting contract verification...`); | ||
await verifyContract({ | ||
address: contract.address, | ||
contract: fullContractSource, | ||
constructorArguments: constructorArgs, | ||
bytecode: artifact.bytecode, | ||
}); | ||
} | ||
|
||
return contract; | ||
} |
Oops, something went wrong.