Deploy a smart contract to Validium using the Quickstart Repository in under 5 minutes
This tutorial shows you how to deploy and interact with a smart contract on Validium in less than 5 minutes. It will help you get familiar with the Validium smart contract development and deployment process using different tools.
In this section you will learn how to:
Build a smart contract named Counter.
Deploy the smart contract to the Validium Testnet.
Move to just the cloned folder and install dependencies:
cd Deploy-on-Validium
npm install
create a .env file and add these environment variables:
Add your account wallet private key here using which you want to deploy and interact with your contract. Also, you will need the Infura API key with the Holesky network enabled (the Underlying Ethereum network).
Review the Smart Contract
Review the contract inside contracts folder:
The Solidity smart contract contains three functions:
increment increases the value of the count state by 1 and emits the CountUpdated event.
decrement decreases the value of the count state by 1 and emits the CountUpdated event.
getCount returns the current value of the count state.
Validium is EVM compatible. You can write smart contracts with Solidity or Vyper and use existing popular libraries like OpenZeppelin, just like on Ethereum.
Compile and Deploy the Smart Contract
Use this command to compile the smart contract:
It will compile and generate an artifacts folder named artifacts-zk .
Logs:
Go to deploy.ts file inside deploy folder:
Update the following:
contractArtifactName (check generated artifacts-zk folder, usually the same as the contract file name)
constructorArguments (if providing any arguments to the constructor)
Deploy to Validium Testnet:
This will deploy the compiled contract to the Validium Testnet.
Logs:
Piece of cake right? π
Check the Contract in Validium Block Explorer
Use the contract address from the deployment logs to see the deployed contract on the Validium Block Explorer or use the link from the logs directly.
Deployed contract on Validium Block Explorer
You can also move to the Events tab and see the Greet event emitted through the constructor as a result of deployment:
Events tab showing the Greet Event value in Text format
Interact with the Deployed Contract
Go to interact.ts file inside deploy folder:
Update the following:
CONTRACT_ADDRESS (copy the contract address from the deployment logs and paste it here)
CONTRACT_NAME (contract artifact name from afticats-zk folder)
Run the interact script:
The interact script above triggers three transactions (two increment and one decrement) and one fetch (getCount) to the deployed contract. It also listens to the CountUpdated event when emitted.
Logs:
Check the transactions in Validium Block Explorer as well:
Three transactions which was triggered from the interact.ts script
Takeaways
EVM-compatibility: Validium is EVM-compatible and you can write smart contracts in Solidity or Vyper as in Ethereum.
Custom compilers: smart contracts deployed to Validium must be compiled with the customs compilers: zksolc for Solidity and zkvyper for Vyper.
Next steps
Follow the same above steps to deploy your contract, changes can be made to interact.ts file and use your methods from the contract.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
contract Counter {
uint256 public count = 0;
// Events
event Greet(string);
event CountUpdated(uint256);
constructor() {
// initializes the contract with a welcome message
emit Greet("Welcome to Validium Network!");
}
function increment() public {
count += 1;
emit CountUpdated(count);
}
function decrement() public {
require(count > 0, " UNDERFLOW: CANNOT DECREASE ANYMORE!");
count -= 1;
emit CountUpdated(count);
}
function getCount() public view returns (uint256) {
return count;
}
}
npm run compile
$ npm run compile
> deploy-on-validium@1.0.0 compile
> hardhat compile
Compiling contracts for ZKsync Era with zksolc v1.5.7 and zkvm-solc v0.8.17-1.0.1
Compiling 1 Solidity file
Successfully compiled 1 Solidity file
deploy.ts
import * as hre from "hardhat";
import { Deployer } from "@matterlabs/hardhat-zksync";
import { getWallet, verifyContract, verifyEnoughBalance } from "./utils";
import { ethers } from "ethers";
const deployContract = async (
contractArtifactName: string,
constructorArguments?: any[]
) => {
console.log(`\nStarting deployment process of "${contractArtifactName}"...`);
const 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 || []
);
console.log(
`Estimated deployment cost: ${ethers.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 address = await contract.getAddress();
const constructorArgs = contract.interface.encodeDeploy(constructorArguments);
const fullContractSource = `${artifact.sourceName}:${artifact.contractName}`;
// Display contract deployment info
console.log(`\n"${artifact.contractName}" was successfully deployed π:`);
console.log(` - Contract address: ${address}`);
console.log(` - Contract source: ${fullContractSource}`);
console.log(` - Encoded constructor arguments: ${constructorArgs}\n`);
console.log(
` - See on Validium Block Explorer: https://testnet.explorer.validium.network/address/${address}\n`
);
if (hre.network.config.verifyURL) {
console.log(`Requesting contract verification...`);
await verifyContract({
address,
contract: fullContractSource,
constructorArguments: constructorArgs,
bytecode: artifact.bytecode,
});
}
return contract;
};
export default async function () {
const contractArtifactName = "Counter";
const constructorArguments: any[] | undefined = [];
await deployContract(contractArtifactName, constructorArguments);
}
npm run deploy
$ npm run deploy
> deploy-on-validium@1.0.0 deploy
> hardhat deploy-zksync --script deploy.ts
Starting deployment process of "Counter"...
Estimated deployment cost: 0.000019009462581388 ETH
"Counter" was successfully deployed π:
- Contract address: 0xB168e33f0d03666590be03AdeAc92Bd76b3229af
- Contract source: contracts/Counter.sol:Counter
- Encoded constructor arguments: 0x
- See on Validium Block Explorer: https://testnet.explorer.validium.network/address/0xB168e33f0d03666590be03AdeAc92Bd76b3229af
interact.ts
import * as hre from "hardhat";
import { ethers } from "ethers";
import { getWallet } from "./utils";
// Address of the contract to interact with
const CONTRACT_ADDRESS = "";
const CONTRACT_NAME = "Counter";
if (!CONTRACT_NAME) throw "βοΈ Provide name of the contract to interact with!";
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(CONTRACT_NAME);
// Initialize contract instance for interaction
const contract = new ethers.Contract(
CONTRACT_ADDRESS,
contractArtifact.abi,
getWallet() // Interact with the contract on behalf of this wallet
);
async function increment() {
// Increment the counter
console.log("Calling increment...");
const incrementTx = await contract.increment();
console.log("Tx hash:", incrementTx.hash);
await incrementTx.wait();
console.log("Incremented!");
}
async function decrement() {
// Decrement the counter
console.log("Calling decrement...");
const decrementTx = await contract.decrement();
console.log("Tx hash:", decrementTx.hash);
await decrementTx.wait();
console.log("Decremented!");
}
async function fetchStoredInteger() {
// Fetch the current stored integer
console.log("Calling getCount...");
const currentStoredInteger = await contract.getCount();
console.log(`Count: ${currentStoredInteger}`);
}
// listen to events-
contract.on("CountUpdated", (newValue) => {
console.log(`Event - CountUpdated: ${newValue}`);
});
await increment();
await increment();
await decrement();
await fetchStoredInteger();
}