Deploy Smart Contracts
Learn how to deploy and interact with smart contracts on ZippyChain.
Table of Contents
Prerequisites
Before you begin deploying smart contracts on ZippyChain, make sure you have:
- Basic knowledge of Solidity and smart contract development
- Node.js and npm installed on your computer
- A code editor (like Visual Studio Code)
- MetaMask wallet configured for ZippyChain Add ZippyChain to MetaMask
- Some test ZIPPY tokens for deployment (for testnet)
Environment Setup
Let's set up your development environment:
- Install Hardhat, a popular Ethereum development environment:
npm install --save-dev hardhat
- Install additional dependencies:
npm install --save-dev @nomiclabs/hardhat-ethers ethers @nomiclabs/hardhat-waffle ethereum-waffle chai
Create a Project
Create a new Hardhat project:
- Create a new directory for your project and navigate to it:
mkdir my-zippy-project
cd my-zippy-project
- Initialize a Hardhat project:
npx hardhat
Select "Create a JavaScript project" and follow the prompts.
- Configure Hardhat for ZippyChain by editing the
hardhat.config.js
file:
require("@nomiclabs/hardhat-waffle");
// Replace this private key with your wallet private key
const PRIVATE_KEY = "your-private-key";
module.exports = {
solidity: "0.8.4",
networks: {
zippyTestnet: {
url: "https://testnet-rpc.zippychain.com",
accounts: [`0x${PRIVATE_KEY}`],
chainId: 7778
},
zippyMainnet: {
url: "https://rpc.zippychain.com",
accounts: [`0x${PRIVATE_KEY}`],
chainId: 7777
}
}
};
Note: Never commit your private key to version control. Use environment variables or a secure secret management solution in production.
Write a Smart Contract
Let's create a simple smart contract. Create a file named SimpleStorage.sol
in the contracts
directory:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract SimpleStorage {
uint256 private value;
event ValueChanged(uint256 newValue);
function setValue(uint256 _value) public {
value = _value;
emit ValueChanged(_value);
}
function getValue() public view returns (uint256) {
return value;
}
}
This is a simple contract that allows storing and retrieving a value.
Compile the Contract
Compile your smart contract using Hardhat:
npx hardhat compile
If the compilation is successful, you'll see the compiled artifacts in the artifacts
directory.
Deploy the Contract
Create a deployment script. Create a file named deploy.js
in the scripts
directory:
const hre = require("hardhat");
async function main() {
// Get the contract factory
const SimpleStorage = await hre.ethers.getContractFactory("SimpleStorage");
// Deploy the contract
const simpleStorage = await SimpleStorage.deploy();
// Wait for the contract to be deployed
await simpleStorage.deployed();
console.log("SimpleStorage deployed to:", simpleStorage.address);
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
Deploy the contract to ZippyChain Testnet:
npx hardhat run scripts/deploy.js --network zippyTestnet
If successful, you'll see the contract address in the console output. Save this address as you'll need it to interact with your contract.
Verify the Contract
Verifying your contract on the ZippyChain Explorer allows others to view and interact with your contract's source code:
- Install the Hardhat Etherscan plugin (ZippyChain Explorer is compatible with Etherscan):
npm install --save-dev @nomiclabs/hardhat-etherscan
- Update your
hardhat.config.js
file:
require("@nomiclabs/hardhat-waffle");
require("@nomiclabs/hardhat-etherscan");
const PRIVATE_KEY = "your-private-key";
const ZIPPY_EXPLORER_API_KEY = "your-api-key"; // Get this from ZippyChain Explorer
module.exports = {
solidity: "0.8.4",
networks: {
zippyTestnet: {
url: "https://testnet-rpc.zippychain.com",
accounts: [`0x${PRIVATE_KEY}`],
chainId: 700
},
zippyMainnet: {
url: "https://rpc.zippychain.com",
accounts: [`0x${PRIVATE_KEY}`],
chainId: 700
}
},
etherscan: {
apiKey: ZIPPY_EXPLORER_API_KEY
}
};
- Verify your contract:
npx hardhat verify --network zippyTestnet YOUR_CONTRACT_ADDRESS
Replace YOUR_CONTRACT_ADDRESS
with the address of your deployed contract.
Interact with the Contract
You can interact with your deployed contract using Hardhat scripts or through the ZippyChain Explorer.
Using Hardhat Scripts
Create a file named interact.js
in the scripts
directory:
const hre = require("hardhat");
async function main() {
// The address of your deployed contract
const contractAddress = "YOUR_CONTRACT_ADDRESS";
// Get the contract instance
const SimpleStorage = await hre.ethers.getContractFactory("SimpleStorage");
const simpleStorage = await SimpleStorage.attach(contractAddress);
// Set a value
const tx = await simpleStorage.setValue(42);
await tx.wait();
console.log("Value set to 42");
// Get the value
const value = await simpleStorage.getValue();
console.log("Current value:", value.toString());
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
Run the script:
npx hardhat run scripts/interact.js --network zippyTestnet
Using ZippyChain Explorer
You can also interact with your verified contract directly through the ZippyChain Explorer:
- Visit the ZippyChain Explorer
- Search for your contract address
- Go to the "Contract" tab
- Connect your wallet
- Use the "Write Contract" section to call functions that modify state
- Use the "Read Contract" section to call view functions
Best Practices
Follow these best practices when developing smart contracts for ZippyChain:
- Always test your contracts thoroughly on the testnet before deploying to mainnet
- Use the latest version of Solidity to benefit from security improvements and new features
- Consider using OpenZeppelin contracts for standard functionality to avoid security issues
- Optimize your contracts to reduce gas costs
- Always verify your contracts on the ZippyChain Explorer for transparency
- Use proper error handling and event logging for better debugging
- Consider having your contracts audited for critical applications