Eth + Avax Proof Smart Contract Management
Managing blockchain assets securely and efficiently is a cornerstone of modern decentralized application (DApp) development. This guide explores a practical implementation of a Solidity smart contract designed to manage Ether (ETH) and Avalanche (AVAX) token operations with robust error handling, ownership control, and front-end integration. Built around the Assesment contract, this project demonstrates core principles in blockchain development including secure fund management, wallet connectivity, and user-friendly interface design using React.
Whether you're building on Ethereum or Avalanche, understanding how to structure secure, auditable smart contracts and connect them to intuitive front ends is essential. This article breaks down the architecture, functionality, and deployment workflow of the project while highlighting best practices in smart contract security, DApp integration, and user experience design.
Core Features of the Assessment Smart Contract
The Assesment contract serves as a foundational example of secure Ether management. Written in Solidity, it enables deposit, withdrawal, and transfer functions—all protected by an onlyOwner modifier to prevent unauthorized access.
Key Functionalities
- Deposit Ether: The owner can increase the contract’s balance.
- Withdraw Funds: Controlled withdrawals ensure funds aren’t drained by unauthorized parties.
- Transfer Assets: Send Ether to any specified address with proper validation.
- Balance Inquiry: Publicly accessible function to check current contract balance.
- Event Logging: Transparent tracking via
Deposit,Withdraw, andTransferevents.
These operations are critical for any asset management system on Ethereum-compatible blockchains like Avalanche (AVAX), where cross-chain interoperability and gas efficiency matter.
Smart Contract Functions Explained
deposit(uint256 _amount)
This function allows the contract owner to send ETH into the contract. The _amount parameter specifies how much Ether to deposit. Upon execution, the internal balance variable increases, and a Deposit event logs the transaction for transparency.
function deposit(uint256 _amount) public onlyOwner {
balance += _amount;
emit Deposit(_amount);
}👉 Learn how secure deposit systems work in real-world DeFi platforms.
withdraw(uint256 _withdrawAmount)
Only the owner can call this function to retrieve funds. It checks whether sufficient balance exists before proceeding, preventing overdraws.
function withdraw(uint256 _withdrawAmount) public onlyOwner {
require(balance >= _withdrawAmount, "Insufficient balance");
balance -= _withdrawAmount;
payable(owner).transfer(_withdrawAmount);
emit Withdraw(_withdrawAmount);
}transfer(address _to, uint256 _amount)
Enables fund transfers to external addresses. Includes safety checks to avoid accidental loss of funds.
function transfer(address _to, uint256 _amount) public onlyOwner {
require(balance >= _amount, "Insufficient balance");
balance -= _amount;
payable(_to).transfer(_amount);
emit Transfer(_to, _amount);
}getBalance() – Read-Only Access
A simple view function that returns the current contract balance. Anyone can query this without cost.
function getBalance() public view returns (uint256) {
return address(this).balance;
}Security Through Modifiers and Events
onlyOwner Modifier
Critical for access control, this modifier ensures only the deploying account can execute sensitive functions.
modifier onlyOwner {
require(msg.sender == owner, "Not the owner");
_;
}This pattern is widely used across DeFi protocols and governance contracts to protect administrative privileges.
Event Emission for Transparency
Events provide off-chain monitoring capabilities:
event Deposit(uint256 amount);event Withdraw(uint256 amount);event Transfer(address indexed _to, uint256 amount);
Indexing the _to address in the Transfer event allows efficient filtering in blockchain explorers or analytics tools.
Front-End Integration with React & MetaMask
The DApp frontend uses React and Ethers.js to enable seamless interaction between users and the smart contract. It abstracts complex blockchain logic into an intuitive interface.
Wallet Connection Setup
Two key functions handle wallet connectivity:
getWallet(): Detects if MetaMask is installed and retrieves the active account.connectAccount(): Triggers MetaMask permission request and initializes contract interaction.
This setup ensures users maintain full control over their private keys while securely connecting to the DApp.
Smart Contract Interaction Layer
Functions like getATMContract(), deposit(), withdraw(), and transferFunds() bridge UI actions with blockchain transactions. For example:
const deposit = async (amount) => {
const contract = getATMContract();
const tx = await contract.deposit({ value: ethers.utils.parseEther(amount) });
await tx.wait();
getBalance();
};Each action prompts MetaMask for user confirmation—ensuring no unauthorized transactions occur.
👉 See how top DApps integrate wallet connectivity smoothly.
User Interface & Experience Design
The DApp features dynamic rendering through the initUser() function, which displays components based on connection status:
- Shows connected wallet address
- Displays real-time balance updates
- Provides input fields for deposits, withdrawals, and transfers
- Includes an "Exit" button to disconnect cleanly
Styling uses CSS gradients and animations to enhance visual appeal without sacrificing performance—ideal for lightweight DApps.
Deployment Workflow Using Hardhat & Gitpod
To run this project locally or in a cloud environment like Gitpod:
Install dependencies:
npm iStart a local Ethereum node:
npx hardhat nodeDeploy the contract to localhost:
npx hardhat run --network localhost scripts/deploy.jsLaunch the front end:
npm run dev
Access the app at http://localhost:3000.
For external access:
- Expose Hardhat’s RPC port (e.g.,
8545) Configure MetaMask with:
- RPC URL: Public Gitpod link ending in
.ws-us104.gitpod.io - Chain ID:
31337 - Currency Symbol:
ETH
- RPC URL: Public Gitpod link ending in
Add account #0 from the Hardhat node for testing.
Best Practices Highlighted in This Project
- ✅ Ownership Control: Prevents unauthorized access via
onlyOwner. - ✅ Input Validation: Uses
require()statements to validate conditions. - ✅ Event Logging: Enhances auditability and transparency.
- ✅ Frontend Security: Never handles private keys; relies on MetaMask.
- ✅ Clear README & Documentation: Essential for open-source collaboration.
Frequently Asked Questions (FAQ)
Q: Can this contract be used on Avalanche (AVAX)?
A: Yes. With minor configuration changes—such as updating the network settings in Hardhat and MetaMask—you can deploy this contract on the Avalanche C-chain, enabling AVAX transfers.
Q: Is it safe to use transfer() for sending Ether?
A: While .transfer() reverts automatically on failure and limits gas (making it safer against reentrancy), consider using .call() with proper checks in more complex scenarios.
Q: How do I make the contract upgradeable?
A: This version is not upgradeable. To support upgrades, implement OpenZeppelin’s Proxy pattern (UUPS or Transparent Proxy).
Q: Why use Hardhat for development?
A: Hardhat offers built-in testing, debugging, scriptable deployments, and a local network simulator—making it ideal for full-cycle smart contract development.
Q: Can non-owners interact with the contract?
A: Only getBalance() is publicly accessible. All other state-changing functions require ownership due to the onlyOwner modifier.
Q: What tools can verify this contract on Etherscan or Snowtrace?
A: Use Hardhat plugins like hardhat-etherscan or hardhat-snowtrace to verify source code after deployment.
Final Thoughts
This project exemplifies how a minimal yet powerful smart contract can be securely developed, tested, and integrated into a user-facing DApp. From Solidity fundamentals to React-based interfaces and wallet connectivity, it covers key aspects of blockchain development, making it ideal for learners and professionals alike.
Whether your focus is on Ethereum, Avalanche, or multi-chain strategies, mastering these patterns builds a strong foundation for creating scalable, secure decentralized applications.
👉 Explore advanced tools used by professional blockchain developers today.