Skip to content

kmjones1979/scaffold-tron

Repository files navigation

πŸ— Scaffold-TRON

πŸ§ͺ A dual-blockchain development toolkit built with Scaffold-ETH 2 for both Ethereum and Tron networks. This toolkit makes it easier for developers to create and deploy smart contracts on both blockchains and build user interfaces that interact with those contracts.

βš™οΈ Built using NextJS, RainbowKit, Hardhat, Wagmi, Viem, TronBox, TronWeb, and Typescript.

✨ Enhanced Features

Ethereum Support (Original)

  • βœ… Contract Hot Reload: Your frontend auto-adapts to your smart contract as you edit it.
  • πŸͺ Custom hooks: Collection of React hooks wrapper around wagmi to simplify interactions with smart contracts with typescript autocompletion.
  • 🧱 Components: Collection of common web3 components to quickly build your frontend.
  • πŸ”₯ Burner Wallet & Local Faucet: Quickly test your application with a burner wallet and local faucet.
  • πŸ” Integration with Wallet Providers: Connect to different wallet providers and interact with the Ethereum network.

Tron Support (New)

  • πŸš€ TronBox Integration: Deploy smart contracts to Tron networks (Shasta, Nile, Mainnet)
  • πŸ”‘ Account Management: Automated Tron account generation with QR codes
  • πŸ’° Balance Checking: Real-time TRX balance monitoring across networks
  • πŸ§ͺ Testnet Support: Easy deployment and testing on Tron testnets
  • ⚑ TVM Compatibility: Leverage Tron Virtual Machine's EVM compatibility
  • πŸ”„ Unified Components: Address and balance components that work seamlessly with both Ethereum and Tron
  • πŸ“‹ Copy Functionality: One-click copy for all address types with proper formatting
  • πŸ”— Block Explorer Links: Direct links to appropriate block explorers for each network

Configuration Management

  • βš™οΈ Unified Network Config: Configure both Ethereum and Tron networks in a single scaffold.config.ts
  • 🌐 Network Switching: Easy switching between different Tron networks (Shasta, Nile, Mainnet)
  • 🎯 Target Network Selection: Set your preferred Tron network just like Ethereum networks

Debug Contracts tab

πŸ” Tron vs Ethereum: Key Differences for Developers

Understanding the fundamental differences between Tron and Ethereum is crucial for effective dual-blockchain development. While both networks support smart contracts and use similar development tools, they have distinct characteristics that affect how you build and deploy applications.

πŸ—οΈ Network Architecture

Aspect Ethereum Tron
Consensus Proof of Stake (PoS) Delegated Proof of Stake (DPoS)
Block Time ~12 seconds ~3 seconds
TPS ~15 transactions/second ~2,000 transactions/second
Validators Unlimited validators 27 Super Representatives
Finality Probabilistic Near-instant

πŸ’° Cost Structure

Ethereum:

  • Gas Model: Pay gas fees in ETH for all operations
  • Variable Costs: Fees fluctuate based on network congestion
  • Contract Deployment: Can cost $50-500+ depending on network conditions

Tron:

  • Energy/Bandwidth Model: Three resource types:
    • TRX: Native token for transactions
    • Energy: Consumed by smart contract execution
    • Bandwidth: Used for transaction data
  • Free Daily Allowance: Users get free bandwidth daily
  • Predictable Costs: More stable pricing, deployment typically costs 50-100 TRX (~$5-10)

πŸ“ Address Formats

// Ethereum addresses (20 bytes, hexadecimal)
0x742d35Cc6634C0532925a3b8D9C24A8c9A4c7c7b

// Tron addresses (21 bytes, Base58 encoded)
T9yD14Nj9j7xAB4dbGeiX9h8unkKHxuWwb

// Both can be used interchangeably in smart contracts
// Tron also supports hex format internally

βš™οΈ Smart Contract Differences

Deployment & Execution

Ethereum:

// Standard deployment
contract MyContract {
    constructor() {
        // Initialization code
    }
}

Tron:

// Same Solidity code, but different execution context
contract MyContract {
    constructor() {
        // Initialization - executed on TVM (Tron Virtual Machine)
    }

    // Tron-specific considerations:
    // - Lower gas costs for computation
    // - Different opcodes for some operations
    // - Energy instead of gas
}

Key Technical Differences

1. Transaction Types

  • Ethereum: Simple transactions and contract calls
  • Tron: 30+ transaction types (Transfer, TriggerSmartContract, CreateSmartContract, etc.)

2. Resource Management

// Ethereum: Gas estimation
uint256 gasRequired = estimateGas(functionCall);

// Tron: Energy consumption is more predictable
// Energy cost depends on instruction complexity, not network congestion

3. Built-in Functions

// Tron has additional built-in functions:
// - freeze/unfreeze for staking
// - vote for Super Representatives
// - energy and bandwidth queries

Development Considerations

Solidity Compatibility:

  • Ethereum: Latest Solidity versions (0.8.x)
  • Tron: TVM supports most Solidity features with some limitations:
    • Some newer opcodes may not be available
    • Different gas cost structure for operations
    • Events and logs work similarly but with different indexing

Testing Differences:

// Ethereum testing (Hardhat)
describe("Contract", function () {
    it("Should deploy and work", async function () {
        const contract = await MyContract.deploy();
        // Standard Web3.js/Ethers.js patterns
    });
});

// Tron testing (TronBox + TronWeb)
describe("Contract", function () {
    it("Should deploy and work", async function () {
        const contract = await tronWeb.contract().new({
            // TronWeb-specific deployment
        });
        // TronWeb has different API patterns
    });
});

πŸ”§ Development Tools

Ethereum Ecosystem:

  • Hardhat/Foundry: Development frameworks
  • Web3.js/Ethers.js: JavaScript libraries
  • MetaMask: Primary wallet integration
  • Etherscan: Block explorer

Tron Ecosystem:

  • TronBox: Development framework (similar to Truffle)
  • TronWeb: JavaScript library (similar to Web3.js)
  • TronLink: Primary wallet integration
  • TronScan: Block explorer

🌐 Network Selection

Ethereum Networks:

// scaffold.config.ts
targetNetworks: [
    chains.hardhat, // Local development
    chains.sepolia, // Testnet
    chains.mainnet, // Production
];

Tron Networks:

// scaffold.config.ts
targetTronNetwork: tronShasta,  // Testnet
// targetTronNetwork: tronNile,    // Alternative testnet
// targetTronNetwork: tronMainnet, // Production

πŸ“Š Performance Characteristics

Ethereum:

  • Higher security through decentralization
  • Slower transaction finality
  • More expensive operations
  • Larger developer ecosystem

Tron:

  • Faster transaction processing
  • Lower transaction costs
  • More centralized consensus
  • Growing DeFi ecosystem

πŸš€ Migration Considerations

When porting contracts between networks:

  1. Gas β†’ Energy: Review resource consumption patterns
  2. Address handling: Ensure proper address format conversion
  3. Event indexing: May require adjustments for different explorers
  4. Wallet integration: Different connection patterns (MetaMask vs TronLink)
  5. Testing: Network-specific test patterns and tools

πŸ’‘ Best Practices

For Dual-Chain Development:

  • Write network-agnostic smart contracts when possible
  • Use Scaffold-TRON's unified components for consistent UX
  • Test thoroughly on both networks' testnets
  • Consider cost implications when choosing primary network
  • Implement proper error handling for network-specific features

Performance Tips:

  • Ethereum: Optimize for gas efficiency, batch operations
  • Tron: Leverage faster block times, utilize free bandwidth
  • Both: Use events for indexing, implement proper access controls

This dual-blockchain approach gives you the best of both worlds: Ethereum's security and ecosystem with Tron's speed and low costs!

Requirements

Before you begin, you need to install the following tools:

Quickstart

πŸš€ Initial Setup

  1. Clone the repository:
git clone https://github.com/kmjones1979/scaffold-tron.git tron-dapp
  1. Install dependencies:
cd tron-dapp
yarn install

βš™οΈ Configure Your Networks

  1. Configure your target networks in packages/nextjs/scaffold.config.ts:
const scaffoldConfig = {
    // Ethereum networks (existing)
    targetNetworks: [chains.hardhat],

    // Tron network (new!)
    // Available options:
    // - tronShasta: Shasta testnet (for development)
    // - tronNile: Nile testnet (for development)
    // - tronMainnet: Tron mainnet (for production)
    targetTronNetwork: tronShasta,

    // ... other config
};

πŸ”· Ethereum Development (Original Workflow)

  1. Run a local Ethereum network:
yarn chain
  1. Deploy Ethereum contracts:
yarn deploy
  1. Start the NextJS frontend:
yarn start

Visit your app on: http://localhost:3000. You can interact with your smart contract using the Debug Contracts page.

πŸ”Ί Tron Development (Enhanced Workflow)

Step 1: Generate Tron Accounts

Generate Tron accounts for all networks (includes QR codes for easy wallet import):

yarn tron:setup

This creates accounts for:

  • Shasta Testnet (primary for testing)
  • Nile Testnet (alternative testnet)
  • Mainnet (production)
  • Development (local)

Step 2: Fund Your Testnet Accounts

Get free TRX from faucets for testing:

Shasta Testnet Faucets:

Nile Testnet Faucet:

Copy your Shasta testnet address from the setup output and request test TRX.

Step 3: Check Your Balances

yarn tron:balance

This shows real-time balances for all your Tron accounts.

Step 4: Compile Tron Contracts

yarn tron:compile

Step 5: Deploy to Tron Networks

Deploy to Shasta Testnet:

yarn tron:deploy:testnet

Deploy to Nile Testnet:

yarn tron:deploy:nile

Deploy to Mainnet (requires real TRX):

yarn tron:deploy:mainnet

Step 6: Run Tests

yarn tron:test

Tests run against the Shasta testnet to verify contract functionality.

🎨 Enhanced UI Components

UnifiedAddress Component

The UnifiedAddress component automatically detects and handles both Ethereum and Tron addresses:

import { UnifiedAddress } from "~~/components/scaffold-eth";

// Works with both Ethereum (0x...) and Tron (T...) addresses
<UnifiedAddress
    address="T9yD14Nj9j7xAB4dbGeiX9h8unkKHxuWwb"
    format="short"
    size="base"
/>;

Features:

  • βœ… Auto-detection: Automatically detects address type
  • πŸ“‹ Copy functionality: One-click copy for all addresses
  • πŸ”— Block explorer links: Direct links to appropriate explorers
  • 🎨 Consistent styling: Matches Ethereum address styling
  • πŸ“± Responsive design: Works across all screen sizes

Network Configuration

Configure your preferred Tron network in scaffold.config.ts:

// For development
targetTronNetwork: tronShasta,

// For production
targetTronNetwork: tronMainnet,

The entire application will automatically use your configured network!

πŸ“‹ Available Scripts

Ethereum Commands

  • yarn chain - Start local Ethereum network
  • yarn deploy - Deploy to local Ethereum network
  • yarn start - Start NextJS frontend
  • yarn hardhat:test - Run Ethereum contract tests

Tron Commands

  • yarn tron:setup - Generate Tron accounts with QR codes
  • yarn tron:account - Display account information
  • yarn tron:balance - Check TRX balances across networks
  • yarn tron:compile - Compile contracts for Tron
  • yarn tron:deploy:testnet - Deploy to Shasta testnet
  • yarn tron:deploy:nile - Deploy to Nile testnet
  • yarn tron:deploy:mainnet - Deploy to Tron mainnet
  • yarn tron:test - Run Tron contract tests
  • yarn tron:console - Open TronBox console

🌐 Network Information

Tron Networks

πŸ—οΈ Project Structure

tron-dapp/
β”œβ”€β”€ packages/
β”‚   β”œβ”€β”€ hardhat/                 # Ethereum & Tron smart contracts
β”‚   β”‚   β”œβ”€β”€ contracts/           # Solidity contracts (compatible with both chains)
β”‚   β”‚   β”œβ”€β”€ deploy/             # Ethereum deployment scripts
β”‚   β”‚   β”œβ”€β”€ tron-migrations/    # Tron deployment scripts
β”‚   β”‚   β”œβ”€β”€ test/               # Ethereum tests
β”‚   β”‚   β”œβ”€β”€ tron-test/          # Tron tests
β”‚   β”‚   β”œβ”€β”€ scripts/            # Utility scripts for Tron
β”‚   β”‚   β”œβ”€β”€ hardhat.config.ts   # Ethereum configuration
β”‚   β”‚   └── tronbox.js          # Tron configuration
β”‚   └── nextjs/                 # Frontend application
β”‚       β”œβ”€β”€ app/                # Next.js app router
β”‚       β”œβ”€β”€ components/         # React components
β”‚       β”‚   └── scaffold-eth/   # Enhanced components
β”‚       β”‚       β”œβ”€β”€ UnifiedAddress.tsx     # Unified address component
β”‚       β”‚       β”œβ”€β”€ UnifiedBalance.tsx     # Unified balance component
β”‚       β”‚       └── UnifiedConnectButton.tsx # Dual-blockchain connect button
β”‚       β”œβ”€β”€ hooks/              # Custom React hooks
β”‚       β”‚   └── scaffold-eth/   # Enhanced hooks
β”‚       β”‚       β”œβ”€β”€ useTronReadContract.ts   # Tron contract reading
β”‚       β”‚       β”œβ”€β”€ useTronWriteContract.ts  # Tron contract writing
β”‚       β”‚       └── useUnifiedWriteContract.ts # Unified contract interactions
β”‚       β”œβ”€β”€ services/           # Web3 services
β”‚       β”‚   └── web3/          # Web3 providers
β”‚       β”‚       β”œβ”€β”€ tronConfig.tsx          # Tron provider
β”‚       β”‚       └── unifiedWeb3Context.tsx  # Unified Web3 context
β”‚       └── scaffold.config.ts  # Unified network configuration

πŸ”§ Configuration Files

Main Configuration

  • Unified Config: packages/nextjs/scaffold.config.ts - Configure both Ethereum and Tron networks
  • Ethereum: packages/hardhat/hardhat.config.ts - Ethereum-specific configuration
  • Tron: packages/hardhat/tronbox.js - Tron-specific configuration
  • Environment: packages/hardhat/.env - Auto-generated by setup

Network Configuration Example

// packages/nextjs/scaffold.config.ts
import {
    tronShasta,
    tronNile,
    tronMainnet,
} from "~~/utils/scaffold-eth/networks";

const scaffoldConfig = {
    // Ethereum networks
    targetNetworks: [chains.hardhat],

    // Tron network selection
    targetTronNetwork: tronShasta, // Easy to change!

    // Other configuration...
};

πŸ’‘ Development Tips

For Ethereum Development:

  • Use yarn chain for local development
  • Deploy contracts with yarn deploy
  • Test with yarn hardhat:test

For Tron Development:

  • Always fund testnet accounts before deploying
  • Use yarn tron:balance to check funding status
  • Test on Shasta testnet before mainnet deployment
  • Contract deployment costs ~50-100 TRX on testnets

Dual-Blockchain Development:

  • Smart contracts are compatible with both Ethereum and Tron
  • Same Solidity code deploys to both networks
  • Use network-specific tools for deployment and testing
  • Configure target networks in scaffold.config.ts
  • Use UnifiedAddress component for consistent address display

UI/UX Best Practices:

  • Use UnifiedAddress instead of Address for better Tron support
  • Configure your preferred Tron network in scaffold config
  • Test address copying and block explorer links
  • Ensure proper network switching in your dApp

πŸ†• Latest Enhancements

December 2024 Updates:

  • 🎨 Rebranded to Scaffold-TRON with proper Scaffold-ETH attribution
  • βš™οΈ Unified Configuration: Configure Tron networks in scaffold.config.ts
  • πŸ”„ Enhanced Components: Improved UnifiedAddress with copy functionality and block explorer links
  • πŸ“± Better UX: Consistent address formatting and interaction patterns
  • πŸ—οΈ Component Renaming: Updated internal component names for consistency
  • 🌐 Network Utilities: Added getTargetTronNetwork() utility function

πŸ“š Documentation & Resources

🀝 Contributing

We welcome contributions to Scaffold-TRON!

Please see CONTRIBUTING.MD for more information and guidelines for contributing to Scaffold-ETH 2.

🎯 What's Next?

  1. Configure your networks in scaffold.config.ts
  2. Deploy your contracts to both Ethereum and Tron testnets
  3. Test extensively using the provided test suites
  4. Build your frontend using the enhanced Scaffold-TRON components
  5. Go multi-chain by deploying to production networks

Happy building! πŸš€


Built with ❀️ on Scaffold-ETH 2

About

No description, website, or topics provided.

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published