Skip to content

shardeum/shardeum-dapp-boilerplate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

42 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Shardeum dApp Boilerplate

A complete, production-ready boilerplate for building decentralized applications (dApps) on Shardeum EVM Testnet. This project includes an enhanced ERC20 token contract with OpenZeppelin security features and a React frontend with wallet integration.

Shardeum dApp

⚠️ Security Disclaimer

This is a reference boilerplate only. Security audits, comprehensive testing, and safe deployment practices are the responsibility of the user/developer. Always:

  • Conduct thorough security audits before deploying to production
  • Test extensively on testnets before mainnet deployment
  • Never expose private keys or sensitive information
  • Use professional audit services for production applications
  • Run security analysis tools (see Security Testing section below)

✨ Features

  • βœ… Enhanced Smart Contract: ERC20 token with OpenZeppelin security features
    • ERC20Burnable for token burning
    • ERC20Permit for gasless approvals (EIP-2612)
    • Ownable for access control
    • Mint function for additional token creation
  • βœ… React + TypeScript: Modern frontend with Vite and strict type safety
  • βœ… Wallet Integration: MetaMask connection with proper event handling
  • βœ… Token Interface: View balances, transfer tokens with validation
  • βœ… Error Handling: Comprehensive error states and user feedback
  • βœ… Transaction Explorer: Direct links to Shardeum explorer
  • βœ… Pre-configured: Ready for Shardeum EVM Testnet (Chain ID: 8119)
  • βœ… Security Tools: SAST integration with Slither and Aderyn

πŸš€ Quick Start

Option 1: Clone and Run

git clone https://github.com/shardeum/shardeum-dapp-boilerplate.git
cd shardeum-dapp-boilerplate
npm install

Option 2: Start from Scratch

Follow the complete tutorial in our documentation.

πŸ“‹ Prerequisites

Before you begin, ensure you have:

βš™οΈ Configuration

1. Environment Variables

Create a .env file in the root directory:

cp .env.example .env

Add your wallet's private key to .env:

PRIVATE_KEY=your_private_key_here

⚠️ Important: Never commit your .env file to version control!

2. Network Configuration

The project is pre-configured for Shardeum EVM Testnet:

Configuration is in src/config/networks.ts.

πŸ› οΈ Project Structure

shardeum-dapp-boilerplate/
β”œβ”€β”€ contracts/              # Smart contracts
β”‚   └── MyToken.sol        # ERC20 token contract
β”œβ”€β”€ scripts/               # Deployment scripts
β”‚   └── deploy.ts          # Deploy MyToken
β”œβ”€β”€ src/                   # Frontend source
β”‚   β”œβ”€β”€ components/        # React components
β”‚   β”‚   └── TokenInterface.tsx
β”‚   β”œβ”€β”€ context/          # React context
β”‚   β”‚   └── WalletContext.tsx
β”‚   β”œβ”€β”€ config/           # Configuration
β”‚   β”‚   β”œβ”€β”€ networks.ts
β”‚   β”‚   └── erc20Abi.ts
β”‚   β”œβ”€β”€ App.tsx           # Main app component
β”‚   β”œβ”€β”€ App.css           # Styles
β”‚   β”œβ”€β”€ main.tsx          # Entry point
β”‚   └── index.css         # Global styles
β”œβ”€β”€ hardhat.config.ts     # Hardhat configuration
β”œβ”€β”€ vite.config.ts        # Vite configuration
β”œβ”€β”€ tsconfig.json         # TypeScript config
└── package.json          # Dependencies

πŸ“ Smart Contract

MyToken.sol

An enhanced ERC20 token implementation with OpenZeppelin security features:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Permit.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

/**
 * @title MyToken
 * @dev Implementation of a basic ERC20 token with additional safety features
 * Uses OpenZeppelin's secure and tested contracts
 */
contract MyToken is ERC20, ERC20Burnable, ERC20Permit, Ownable {
    constructor()
        ERC20("My Token", "MTK")
        ERC20Permit("My Token")
        Ownable(msg.sender)
    {
        _mint(msg.sender, 1_000_000 * 10 ** decimals());
    }

    function mint(address to, uint256 amount) public onlyOwner {
        _mint(to, amount);
    }
}

Features:

  • ERC20: Standard token functionality (transfer, approve, balanceOf)
  • ERC20Burnable: Users can burn their own tokens
  • ERC20Permit: Gasless approvals using EIP-2612 signatures
  • Ownable: Access control for owner-only functions
  • Mint Function: Owner can mint additional tokens
  • Initial supply of 1,000,000 tokens minted to deployer

🚒 Deployment

1. Deploy Smart Contract

Use the convenient npm script:

npm run deploy:testnet

Or use Hardhat directly:

npx hardhat run scripts/deploy.ts --network shardeumTestnet

✨ The deploy script automatically updates src/config/networks.ts with your contract address!

2. Verify Contract (Optional but Recommended)

Verify your contract on the Shardeum explorer:

npx hardhat verify --network shardeumTestnet <DEPLOYED_ADDRESS>

3. Run the Frontend

npm run dev

Open http://localhost:5173 in your browser!

🎯 How to Use

1. Connect Wallet

  • Click "Connect Wallet" button
  • Approve the connection in MetaMask

2. Switch to Shardeum Testnet

  • If prompted, approve adding and switching to Shardeum Testnet
  • Network will be added automatically

3. Get Test Tokens

Visit the Shardeum Faucet to get test SHM tokens.

4. Transfer Tokens

  • View your token balance
  • Enter recipient address and amount
  • Click "Transfer" and confirm in MetaMask
  • View transaction on explorer

πŸ—οΈ Building for Production

Build the Frontend

npm run build

The production build will be in the dist/ folder.

Deploy to Hosting

Vercel:

npm install -g vercel
vercel --prod

Netlify:

npm install -g netlify-cli
netlify deploy --prod --dir=dist

IPFS (Decentralized):

npm install -g ipfs-deploy
ipfs-deploy dist

πŸ”§ Development Commands

# Install dependencies
npm install

# Run dev server (frontend)
npm run dev

# Build for production
npm run build

# Preview production build
npm run preview

# Deploy smart contract
npm run deploy:testnet
# Or: npx hardhat run scripts/deploy.ts --network shardeumTestnet

# Compile contracts
npx hardhat compile

# Run Hardhat console
npx hardhat console --network shardeumTestnet

πŸ”’ Security Testing

Understanding your smart contract's security posture is critical before deployment. This boilerplate recommends using industry-standard SAST (Static Application Security Testing) tools:

Slither - Static Analysis Tool

Slither is a Solidity static analysis framework that detects vulnerabilities and suggests code improvements.

Installation:

pip3 install slither-analyzer

Run Slither:

slither .

Slither will analyze your contracts and provide:

  • Vulnerability detection (reentrancy, integer overflow, etc.)
  • Code quality suggestions
  • Gas optimization recommendations
  • Best practice violations

Example Output:

MyToken.mint(address,uint256) (contracts/MyToken.sol#27-29) should emit an event for:
  - _mint(to,amount) (contracts/MyToken.sol#28)

Aderyn - Rust-based Solidity Auditor

Aderyn is a fast, Rust-based static analyzer designed for Solidity smart contracts.

Installation:

cargo install aderyn

Or use with cargo-binstall:

cargo binstall aderyn

Run Aderyn:

aderyn .

Aderyn provides:

  • Fast analysis with detailed reports
  • Security vulnerability detection
  • Code quality metrics
  • markdown report generation

Generate detailed report:

aderyn . --output report.md

Best Practices

  1. Run both tools - Slither and Aderyn complement each other and may catch different issues
  2. Fix critical issues - Always address high and medium severity findings
  3. Review warnings - Even low-severity warnings can indicate code quality issues
  4. Run before deployment - Make security analysis part of your deployment checklist
  5. Professional audit - For production applications, consider professional security audits

Quick Security Checklist

Before deploying to production:

  • Run slither . and address all findings
  • Run aderyn . and review the report
  • Test all functions on testnet
  • Verify access control (onlyOwner functions)
  • Check for reentrancy vulnerabilities
  • Validate input parameters
  • Review token economics (mint, burn, supply)
  • Ensure private keys are secure
  • Consider professional security audit

πŸ“š What You'll Learn

This boilerplate teaches you:

  • βœ… Smart contract deployment with Hardhat
  • βœ… ERC20 token implementation
  • βœ… React with TypeScript
  • βœ… MetaMask wallet integration
  • βœ… Reading blockchain data with ethers.js
  • βœ… Sending transactions
  • βœ… Network switching
  • βœ… Error handling
  • βœ… Transaction tracking

πŸ›‘οΈ Best Practices

Security

  1. Never expose private keys - Use environment variables
  2. Validate user input - Check addresses and amounts before sending
  3. Handle errors gracefully - Display user-friendly error messages
  4. Test thoroughly - Always test on testnet before mainnet

Performance

  1. Cache contract instances - Don't recreate on every render
  2. Debounce user input - For real-time updates
  3. Use event listeners - Instead of polling
  4. Optimize bundle size - Use code splitting

User Experience

  1. Show loading states - Let users know transactions are processing
  2. Display transaction links - Link to block explorer
  3. Handle disconnection - Gracefully handle wallet disconnecting
  4. Mobile responsive - Test on mobile devices

πŸ› Troubleshooting

MetaMask Not Detected

if (!window.ethereum) {
  alert('Please install MetaMask!');
}

Wrong Network

The app automatically prompts users to switch to Shardeum Testnet.

Transaction Failing

  • Check you have enough SHM for gas
  • Verify recipient address is valid
  • Ensure you have enough token balance

Contract Not Deployed

Make sure you've deployed the contract and updated TOKEN_ADDRESS in src/config/networks.ts.

πŸ“– Additional Resources

🀝 Contributing

We welcome contributions! Whether you're fixing bugs, adding features, or improving documentation, your help makes a difference.

How to Contribute

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ™ Acknowledgments

πŸ’¬ Need Help?


Happy Building on Shardeum! πŸš€

About

A ready-to-go configurable boilerplate for blockchain projects.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Contributors