Skip to main content

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
Important: Never share your private keys or commit them to version control. Always use environment variables for sensitive data.

Part 1: Network Setup

Step 1: Add Nexis Network to MetaMask

First, configure your wallet to connect to Nexis Appchain Testnet.
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:
Select these options:
  • 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

Edit hardhat.config.js to add Nexis network configuration:
hardhat.config.js

Step 4: Create Environment Variables

Create a .env file in your project root:
.env
Security Best Practice: Add .env to your .gitignore file to prevent committing secrets:

Step 5: Get Your Private Key

To export your private key from MetaMask:
  1. Open MetaMask
  2. Click the three dots menu
  3. Select Account details
  4. Click Show private key
  5. Enter your MetaMask password
  6. Copy the private key and paste it in your .env file

Part 3: Write Your Smart Contract

Step 1: Create the Contract File

Create a new file contracts/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:
Expected output:
If you see compilation errors, check:
  • 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

Create scripts/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.
Expected output:
Congratulations! Your contract is now deployed on Nexis Appchain.

Step 3: Verify the Contract

Verify your contract on the block explorer:
Replace 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

Edit foundry.toml:
foundry.toml

Step 4: Create Contract

Move your contract to src/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:
In the console:

Step 2: View on Block Explorer

Visit the explorer to see your contract:
You should see:
  • Contract code (if verified)
  • Recent transactions
  • Events emitted
  • Current balance

Troubleshooting Common Issues

Problem: Your account doesn’t have enough NZT for gas fees.Solution:
  1. Visit the faucet
  2. Request testnet NZT
  3. Wait for transaction to confirm
  4. Check balance: await ethers.provider.getBalance(YOUR_ADDRESS)
Problem: Your local nonce is out of sync with the network.Solution:
Problem: Verification on explorer is failing.Solution:
  1. Wait 1-2 minutes after deployment
  2. Make sure constructor arguments are correct
  3. Check Solidity version matches (0.8.20)
  4. Try manual verification on explorer UI
Problem: Cannot connect to Nexis RPC.Solution:
Problem: Transaction gas estimation is failing.Solution:

Best Practices Checklist

1

Security

  • Never commit private keys to git
  • Use environment variables for secrets
  • Add .env to .gitignore
  • Test on testnet before mainnet
  • Get contracts audited for production
2

Gas Optimization

  • Enable Solidity optimizer
  • Use immutable for constants set in constructor
  • Batch operations when possible
  • Emit events instead of storing strings
  • Use calldata instead of memory for 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

Additional Resources


Pro Tip: Save your deployment addresses in a deployments.json file to track contracts across networks. This makes it easier to interact with them later.