Deploy Contracts on Morph
The Morph Holesky Testnet allows anyone to deploy a smart contract on Morph. This tutorial will guide you through deploying a contract on Morph Holesky using common Ethereum development tools.
This demo repo illustrates contract deployment with Hardhat and Foundry.
Deploy with Hardhat
Clone the repo
git clone https://github.com/morph-l2/morph-examples.git
Install Dependencies
If you haven't already, install nodejs and yarn.
cd contract-deployment-demos/hardhat-demo
yarn install
This will install everything you need include hardhat for you.
Compile
Compile your contract
yarn compile
Test
This will run the test script in test/Lock.ts
yarn test
Deploy
Create a .env
file following the example .env.example
in the root directory. Change PRIVATE_KEY
to your own account private key in the .env
.
And Change the network settings in the hardhat.config.ts file with the following information:
morphTestnet: {
url: process.env.MORPH_TESTNET_URL || "",
accounts:
process.env.PRIVATE_KEY !== undefined ? [process.env.PRIVATE_KEY] : [],
}
Then run the following command to deploy the contract on the Morph Holesky Testnet. This will run the deployment script that set the initialing parameters, you can edit the script in scripts/deploy.ts
yarn deploy:morphTestnet
Verify your contracts on Morph Explorer
To verify your contract through hardhat, you need to add the following Etherscan and Sourcify configs to your hardhat.config.js file:
module.exports = {
networks: {
morphTestnet: { ... }
},
etherscan: {
apiKey: {
morphTestnet: 'anything',
},
customChains: [
{
network: 'morphTestnet',
chainId: 2810,
urls: {
apiURL: 'https://explorer-api-holesky.morphl2.io/api? ',
browserURL: 'https://explorer-holesky.morphl2.io/',
},
},
],
},
};
Then run the hardhat verify command to finish the verification
npx hardhat verify --network morphTestnet DEPLOYED_CONTRACT_ADDRESS <ConstructorParameter>
For example
npx hardhat verify --network morphTestnet 0x8025985e35f1bAFfd661717f66fC5a434417448E '0.00001'
Once succeed, you can check your contract and the deployment transaction on Morph Holesky Explorer
Deploy contracts with Foundry
Clone the repo
git clone https://github.com/morph-l2/morph-examples.git
Install Foundry
curl -L https://foundry.paradigm.xyz | bash
foundryup
Then go the right folder of our example:
cd contract-deployment-demos/foundry-demo
Compile
forge build
Deploy
A Deployment script and use of environment variables has already been set up for you. You can view the script at script/Counter.s.sol
Rename your .env.example file to .env and fill in your private key. The RPC URL has already been filled in along with the verifier URL.
To use the variables in your .env file run the following command:
source .env
You can now deploy to Morph with the following command:
forge script script/Counter.s.sol --rpc-url $RPC_URL --broadcast --private-key $DEPLOYER_PRIVATE_KEY --legacy
Adjust as needed for your own script names.
Verify
Verification requires some flags passed to the normal verification script. You can verify using the command below:
forge verify-contract YourContractAddress Counter\
--chain 2810 \
--verifier-url $VERIFIER_URL \
--verifier blockscout --watch
Once succeeded, you can check your contract and the deployment transaction on Morph Holesky Explorer.
Questions and Feedback
Thank you for participating in and developing on the Morph Holesky Testnet! If you encounter any issues, join our Discord and find us at #dev-support channel.