Deploy Your First Smart Contract on Nexis
This comprehensive tutorial walks you through deploying a smart contract on Nexis Appchain from start to finish. You’ll learn how to set up your development environment, write a contract, deploy it to the testnet, and verify it on the block explorer.Prerequisites
Before starting this tutorial, ensure you have:Node.js v16+
Download from nodejs.org
MetaMask Wallet
Install from metamask.io
Testnet NZT
Get tokens from the faucet
Code Editor
VS Code, Sublime, or your preferred editor
Part 1: Network Setup
Step 1: Add Nexis Network to MetaMask
First, configure your wallet to connect to Nexis Appchain Testnet.- Manual Configuration
- Programmatic Addition
Open MetaMask and navigate to Settings → Networks → Add Network. Enter these parameters:
Click “Save” to add the network.
Step 2: Verify Network Connection
Test your RPC connection:Success! If you see a hex result, your RPC connection is working correctly.
Part 2: Project Setup with Hardhat
Step 1: Initialize a New Project
Create a new directory and initialize your project:Step 2: Initialize Hardhat
Run the Hardhat initialization wizard:- What do you want to do? → Create a JavaScript project
- Hardhat project root → Press Enter (use current directory)
- Add a .gitignore? → Yes
- Install dependencies? → Yes
Hardhat will create a basic project structure with sample contracts, tests, and scripts.
Step 3: Configure Hardhat for Nexis
Edithardhat.config.js to add Nexis network configuration:
hardhat.config.js
Step 4: Create Environment Variables
Create a.env file in your project root:
.env
Step 5: Get Your Private Key
To export your private key from MetaMask:- Open MetaMask
- Click the three dots menu
- Select Account details
- Click Show private key
- Enter your MetaMask password
- Copy the private key and paste it in your
.envfile
Part 3: Write Your Smart Contract
Step 1: Create the Contract File
Create a new filecontracts/AITaskRequester.sol:
contracts/AITaskRequester.sol
This contract demonstrates best practices:
- Uses OpenZeppelin’s secure contracts
- Implements proper access control
- Includes reentrancy protection
- Has comprehensive error handling
- Emits events for tracking
- Includes detailed NatSpec documentation
Step 2: Compile the Contract
Compile your contract to check for errors:- Solidity version matches your config
- All imports are correctly installed
- No syntax errors in the code
Part 4: Deploy to Nexis Testnet
Step 1: Create Deployment Script
Createscripts/deploy.js:
scripts/deploy.js
Step 2: Deploy the Contract
Run the deployment script:Deployment typically takes 5-15 seconds on Nexis due to 2-second block times.
Congratulations! Your contract is now deployed on Nexis Appchain.
Step 3: Verify the Contract
Verify your contract on the block explorer:DEPLOYED_ADDRESS with your actual contract address.
Expected output:
Part 5: Deploy with Foundry (Alternative)
If you prefer Foundry over Hardhat, follow these steps:Step 1: Install Foundry
Step 2: Create Foundry Project
Step 3: Configure Foundry
Editfoundry.toml:
foundry.toml
Step 4: Create Contract
Move your contract tosrc/AITaskRequester.sol (use the same code as above).
Step 5: Deploy with Forge
Part 6: Testing Your Deployment
Step 1: Interact via Hardhat Console
Test your deployed contract:Step 2: View on Block Explorer
Visit the explorer to see your contract:- Contract code (if verified)
- Recent transactions
- Events emitted
- Current balance
Troubleshooting Common Issues
Error: Insufficient funds
Error: Insufficient funds
Problem: Your account doesn’t have enough NZT for gas fees.Solution:
- Visit the faucet
- Request testnet NZT
- Wait for transaction to confirm
- Check balance:
await ethers.provider.getBalance(YOUR_ADDRESS)
Error: Nonce too high
Error: Nonce too high
Problem: Your local nonce is out of sync with the network.Solution:
Error: Contract verification failed
Error: Contract verification failed
Problem: Verification on explorer is failing.Solution:
- Wait 1-2 minutes after deployment
- Make sure constructor arguments are correct
- Check Solidity version matches (0.8.20)
- Try manual verification on explorer UI
Error: Invalid JSON RPC response
Error: Invalid JSON RPC response
Problem: Cannot connect to Nexis RPC.Solution:
Gas estimation failed
Gas estimation failed
Problem: Transaction gas estimation is failing.Solution:
Best Practices Checklist
1
Security
- Never commit private keys to git
- Use environment variables for secrets
- Add
.envto.gitignore - Test on testnet before mainnet
- Get contracts audited for production
2
Gas Optimization
- Enable Solidity optimizer
- Use
immutablefor constants set in constructor - Batch operations when possible
- Emit events instead of storing strings
- Use
calldatainstead ofmemoryfor external function parameters
3
Testing
- Write comprehensive unit tests
- Test edge cases and error conditions
- Use Hardhat’s local network for rapid testing
- Test with actual testnet before production
- Monitor gas usage in tests
4
Documentation
- Add NatSpec comments to all functions
- Document constructor parameters
- Explain complex logic
- Keep README up to date
- Document deployment process
Next Steps
Register an AI Agent
Learn how to register and stake an agent to claim tasks
Create Tasks
Post AI inference tasks for agents to complete
Integrate AI Services
Connect your AI models to Nexis for verifiable inference
Smart Contract Reference
Explore all available contract methods