Hydrix Chain — dApp Integration
This section guides developers on connecting decentralized applications (dApps) to Hydrix Chain, interacting with smart contracts, and handling user wallets.
Connecting Wallets
Hydrix is EVM-compatible, so most Ethereum wallets work out-of-the-box:
- MetaMask
- WalletConnect
- Coinbase Wallet
- Any EVM-compatible wallet
Example: Connecting MetaMask
async function connectWallet() {
if (window.ethereum) {
try {
const accounts = await window.ethereum.request({ method: 'eth_requestAccounts' });
console.log("Connected account:", accounts[0]);
return accounts[0];
} catch (err) {
console.error("User rejected wallet connection:", err);
}
} else {
console.error("No Ethereum wallet found");
}
}
Setting Up a Provider
To interact with Hydrix RPC endpoints:
import { ethers } from "ethers";
const validators = [
"https://chainfree.info:5010/validator/1",
"https://chainfree.info:5010/validator/2",
"https://chainfree.info:5010/validator/3",
"https://chainfree.info:5010/validator/4"
];
let provider = new ethers.JsonRpcProvider(validators[0]); // Start with first validator
// Optional: switch validator if one is slow
async function switchValidator(index) {
provider = new ethers.JsonRpcProvider(validators[index]);
console.log(`Switched provider to validator ${index + 1}`);
}
Interacting with Smart Contracts
Once connected, dApps can read and write to contracts:
const contractAddress = "DEPLOYED_CONTRACT_ADDRESS";
const abi = [
"function message() view returns (string)",
"function setMessage(string calldata)"
];
const signer = provider.getSigner(); // Connected wallet
const contract = new ethers.Contract(contractAddress, abi, signer);
// Read example
const currentMessage = await contract.message();
console.log("Current message:", currentMessage);
// Write example
const tx = await contract.setMessage("Updated from dApp");
await tx.wait();
console.log("Message updated successfully");
Handling Transactions
- Always estimate gas before sending:
contract.estimateGas.setMessage("text") - Monitor transaction status with
tx.wait() - Handle user errors gracefully (rejected transactions, insufficient funds)
Using Lightweight Nodes
- For dApps with high read operations, use lightweight mobile or desktop nodes to reduce load on validators
- Query blockchain state directly through nodes instead of hitting validator RPC constantly
- Lightweight nodes can relay reads to the main network efficiently
Event Listening
React or Node.js dApps can listen to contract events:
contract.on("MessageUpdated", (newMessage, event) => {
console.log("Message updated:", newMessage);
});
- Events are emitted from smart contracts for real-time dApp updates
- Combine with lightweight nodes for efficient state monitoring
Best Practices
- Always allow users to switch validators if endpoints are slow
- Use standard libraries like Ethers.js and OpenZeppelin for secure interaction
- Test all dApp functionality on Hydrix testnet before mainnet deployment
- Keep front-end wallet interactions non-blocking to ensure smooth UX