Welcome to your first smart contract deployment on the [[ZKP/ZKP Testnet/Overview|ZKP Testnet]]! This simple example will help you understand how to create, deploy, and interact with smart contracts on our [[ZKP/ZKP Base Layer/ZKP Blockchain/High Level Overview|blockchain]]
## What We're Building
We'll create a "Hello World" smart contract that:
- Stores a greeting message
- Allows anyone to read the message
- Lets the owner update the message
- Keeps track of how many times it's been updated
This is perfect for learning the basics without any complexity!
## The Smart Contract Code
Here's our simple smart contract written in Solidity:
```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract HelloWorld {
// State variables
string public greeting;
address public owner;
uint256 public updateCount;
// Event to log when greeting is updated
event GreetingUpdated(string newGreeting, uint256 updateNumber);
// Constructor - runs when contract is deployed
constructor() {
greeting = "Hello, ZKP World!";
owner = msg.sender;
updateCount = 0;
}
// Function to update the greeting (only owner can do this)
function updateGreeting(string memory _newGreeting) public {
require(msg.sender == owner, "Only owner can update greeting");
greeting = _newGreeting;
updateCount++;
emit GreetingUpdated(_newGreeting, updateCount);
}
// Function to read the greeting (anyone can call this)
function getGreeting() public view returns (string memory) {
return greeting;
}
// Function to get contract info
function getContractInfo() public view returns (string memory, address, uint256) {
return (greeting, owner, updateCount);
}
}
```
## Step-by-Step Deployment
### Prerequisites
- MetaMask wallet connected to [[ZKP/ZKP Testnet/Getting Started/Network|ZKP testnet]]
- Some [[ZKP/ZKP Testnet/Getting Started/Claim Test Coins|ZKP coins from our faucet]]
- Basic understanding of how to use Remix IDE
### Step 1: Open Remix IDE
1. Go to [https://remix.ethereum.org](https://remix.ethereum.org/)
2. This is a free, web-based development environment
3. No downloads or installations needed!
### Step 2: Create Your Contract File
1. In the file explorer (left panel), click the "+" button
2. Name your file `HelloWorld.sol`
3. Copy and paste the smart contract code above
4. The file will automatically save
### Step 3: Compile Your Contract
1. Click on the "Solidity Compiler" tab (second icon on the left)
2. Make sure the compiler version is set to `0.8.0` or higher
3. Click "Compile HelloWorld.sol"
4. You should see a green checkmark when compilation succeeds
### Step 4: Connect to ZKP Testnet
1. Make sure MetaMask is connected to ZKP testnet
2. Click on the "Deploy & Run Transactions" tab (third icon on the left)
3. In the "Environment" dropdown, select "Injected Web3"
4. MetaMask should connect automatically
5. Confirm you see "ZKP Testnet" in the network indicator
### Step 5: Deploy Your Contract
1. Make sure "HelloWorld" is selected in the contract dropdown
2. Click the orange "Deploy" button
3. MetaMask will popup asking you to confirm the transaction
4. Confirm the transaction (it will cost a small amount of ZKP for gas)
5. Wait for the transaction to be confirmed (usually takes a few seconds)
### Step 6: Interact with Your Contract
Once deployed, you'll see your contract in the "Deployed Contracts" section:
**Reading Data (Free Operations):**
- Click `greeting` to see the current greeting
- Click `owner` to see who owns the contract
- Click `updateCount` to see how many times it's been updated
- Click `getGreeting` to read the greeting via function call
- Click `getContractInfo` to get all info at once
**Writing Data (Costs Gas):**
- Enter a new message in the `updateGreeting` field
- Click the button to update the greeting
- Confirm the transaction in MetaMask
- Check that `updateCount` increased by 1
## Understanding What Happened
### Contract Deployment
When you deployed the contract:
1. The constructor ran and set the initial greeting
2. Your wallet address became the owner
3. The contract was given a unique address on the blockchain
4. The code is now permanently stored and publicly verifiable
### Reading vs Writing
- **Reading data** (view functions) is free and instant
- **Writing data** (state changes) costs gas and requires confirmation
- Only the owner can update the greeting (access control)
- Anyone can read the greeting (public accessibility)
### Events and Logs
When you update the greeting, the contract emits an event that:
- Gets permanently recorded on the blockchain
- Can be searched and filtered
- Provides a history of all updates
## Verifying Your Contract
### On the Block Explorer
1. Go to Explorer (Available soon)
2. Search for your contract address
3. View the transaction history
4. See the contract code and interactions
### Contract Address
Your contract now has a permanent address like: `0x742d35Cc6634C0532925a3b8D421B3e8A51F7E6d`
Anyone can:
- Interact with your contract using this address
- View its code and transaction history
- Call its public functions
## Common Issues and Solutions
### "Transaction Failed"
- **Problem:** Not enough ZKP for gas fees
- **Solution:** Get more coins from the faucet
### "Only Owner Can Update"
- **Problem:** Trying to update from wrong wallet
- **Solution:** Use the same wallet that deployed the contract
### "Contract Not Found"
- **Problem:** Wrong network or address
- **Solution:** Double-check you're on ZKP testnet
### "Compilation Error"
- **Problem:** Syntax error in the code
- **Solution:** Copy the exact code from this example
## Next Steps
### Experiment with Your Contract
- Try updating the greeting with different messages
- Check how the `updateCount` increases
- Share your contract address with friends so they can read your greeting
### Learn More
- Modify the code to add new features
- Try creating a contract that stores multiple messages
- Experiment with different data types and functions
### Advanced Features
- Add more complex logic
- Implement multiple owner functionality
- Create contracts that interact with each other
- Explore ZKP's privacy features
## Code Explanation
### State Variables
```solidity
string public greeting; // Stores the greeting message
address public owner; // Stores the contract owner's address
uint256 public updateCount; // Counts how many times greeting was updated
```
### Constructor
```solidity
constructor() {
greeting = "Hello, ZKP World!"; // Set initial greeting
owner = msg.sender; // Set deployer as owner
updateCount = 0; // Initialize counter
}
```
### Access Control
```solidity
require(msg.sender == owner, "Only owner can update greeting");
```
This line ensures only the person who deployed the contract can update it.
### Events
```solidity
event GreetingUpdated(string newGreeting, uint256 updateNumber);
emit GreetingUpdated(_newGreeting, updateCount);
```
Events create a permanent log of important actions.
## Why This Matters
This simple example demonstrates:
- **Decentralization:** No central authority controls your contract
- **Transparency:** All code and transactions are publicly visible
- **Permanence:** Once deployed, your contract lives forever on the blockchain
- **Accessibility:** Anyone can interact with your contract from anywhere
- **Low Cost:** Deployment and interaction costs are minimal
## Congratulations! 🎉
You've successfully deployed your first smart contract on the ZKP blockchain! You now understand:
- How to write basic Solidity code
- How to compile and deploy contracts
- How to interact with deployed contracts
- The difference between reading and writing blockchain data
This is just the beginning. Smart contracts can power everything from simple games to complex financial systems. The ZKP testnet is your playground to experiment and learn without any risk.
Ready to build something more complex? Join our community and start exploring the endless possibilities of decentralized applications!
See also: [[JSON-RPC API]]