Tutorials and Examples: Building and Deploying a Simple dApp
Last updated
Last updated
This tutorial will guide you through creating a simple decentralized application (dApp) on JuChain: a "Greeting" smart contract where users can set and read custom greetings. We'll write the contract in Solidity, deploy it to the JuChain Testnet using Truffle, and build a frontend interface to interact with it via MetaMask. The tutorial showcases JuChain’s development workflow, fast transaction confirmations, and low-cost benefits.
Tools:
Node.js (v16 or higher, download: )
Truffle Suite (install globally: npm install -g truffle
)
MetaMask browser extension (install: )
JuChain Testnet:
RPC Endpoint: https://testnet-rpc.juchain.org
Network ID: 202599
Test JU Tokens: Obtain via the JuChain Testnet faucet (assumed: https://faucet-testnet.juchain.org
)
Knowledge: Basic understanding of JavaScript, Solidity, and blockchain concepts.
Create and Initialize the Project:
This creates folders like contracts/
, migrations/
, and truffle-config.js
.
Install Dependencies:
@openzeppelin/contracts
: Secure contract templates.
web3
: Frontend-blockchain interaction.
dotenv
: Environment variable management.
Create a .env
File:
In the project root, create .env
:
Security Note: Never commit
.env
to GitHub.
In the contracts/
folder, create Greeting.sol
:
Explanation:
greeting
: Public variable storing the greeting.
setGreeting
: Sets a new greeting and emits an event.
getGreeting
: Retrieves the current greeting.
Event: Logs each greeting change for frontend monitoring.
Edit truffle-config.js
to include the JuChain Testnet:
Checkpoint:
Ensure MNEMONIC
and RPC_URL
in .env
are correctly set.
Test the network connection:
A block number output confirms a successful connection.
Create a Deployment Script:
In migrations/
, create 2_deploy_greeting.js
:
Deploy to JuChain Testnet:
Example output:
Note the contract address (e.g., 0x1234...5678
) for later use.
Verify Deployment: In the Truffle console:
Expected output: "Hello, JuChain!"
.
Initialize the Frontend Folder:
Write index.html
:
Add the following to public/index.html
:
Obtain the ABI:
Copy the "abi"
field from build/contracts/Greeting.json
and replace the abi
variable in index.html
.
Update contractAddress
with the address from Step 4.
Start a Local Server:
Access http://localhost:8080
by default.
Connect MetaMask:
Add the JuChain Testnet to MetaMask:
Network Name: JuChain Testnet
RPC URL: https://testnet-rpc.juchain.org
Chain ID: 202599
Currency Symbol: JU
Ensure your account has test JU tokens (via the faucet).
Test the dApp:
Open http://localhost:8080
in your browser.
Verify the initial greeting displays as "Hello, JuChain!"
.
Enter a new greeting (e.g., "Welcome to JuChain!"
) and click “Set Greeting”.
Confirm the status updates to “Greeting updated successfully!” and the new greeting appears.
Check MetaMask: Transactions should confirm in 3-6 seconds with minimal fees (~0.001 JU).
You’ve successfully built and deployed a simple “Greeting” dApp:
Smart Contract: Deployed on the JuChain Testnet, supporting greeting updates and retrieval.
Frontend Interface: Interacts with the contract via MetaMask, displaying results intuitively.
JuChain Advantages:
Fast Confirmation: Transactions complete in seconds, enhancing user experience.
Low Cost: Setting a greeting incurs minimal fees, ideal for frequent interactions.
EVM Compatibility: Leverages familiar Ethereum tools (Truffle, Web3.js) for seamless development.
Network Status: Ensure the JuChain Testnet RPC is operational; contact JuChain support if issues arise.
Gas Fees: Testnet costs are low, but estimate mainnet expenses before production deployment.