💰 Smart Donation Contract A simple, secure Ethereum smart contract that allows anyone to donate ETH while only the contract owner can withdraw the funds. 📋 Overview This Solidity smart contract pro
A simple, secure Ethereum smart contract that allows anyone to donate ETH while only the contract owner can withdraw the funds.
This Solidity smart contract provides a transparent donation platform where:
Anyone can donate ETH to the contract
All donations are tracked and logged
Only the contract owner can withdraw funds
Full transparency with public balance checking
Public Donations: Anyone can donate any amount of ETH
Donation Tracking: Track individual donations per address
Owner-Only Withdrawals: Secure withdrawal mechanism for the contract owner
Event Logging: All donations and withdrawals are logged on the blockchain
Transparent Balance: Anyone can check the current contract balance
donate()Visibility: External, Payable
Description: Allows anyone to donate ETH to the contract
Requirements: Donation amount must be greater than 0
Events: Emits Donated event
withdraw()Visibility: External
Description: Allows the owner to withdraw all funds from the contract
Requirements:
Only the owner can call this function
Contract must have a balance greater than 0
Events: Emits Withdrawn event
getBalance()Visibility: External, View
Description: Returns the current balance of the contract
Returns: Current contract balance in Wei
owner: Address of the contract deployer/owner
donations: Mapping of donor addresses to their total donated amounts
totalDonations: Total amount of all donations received
Solidity compiler version ^0.8.0
Ethereum wallet (MetaMask, etc.)
ETH for gas fees
Using Remix IDE (Recommended for beginners):
Go to Remix Ethereum IDE
Create a new file and paste the contract code
Compile with Solidity 0.8.0 or higher
Deploy to your preferred network (Testnet recommended for testing)
Using Hardhat:
npx hardhat compile
npx hardhat run scripts/deploy.js --network <network-name>Using Truffle:
truffle compile
truffle migrate --network <network-name>// Using Web3.js
await donationContract.methods.donate().send({
from: userAddress,
value: web3.utils.toWei('1', 'ether')
});// Using Web3.js
const balance = await donationContract.methods.getBalance().call();
console.log('Contract balance:', web3.utils.fromWei(balance, 'ether'), 'ETH');// Using Web3.js
await donationContract.methods.withdraw().send({
from: ownerAddress
});Owner-Only Withdrawal: Only the deployer can withdraw funds
Secure Transfer Method: Uses call() for ETH transfers (best practice)
Input Validation: Ensures donations are greater than 0
Event Logging: All transactions are logged for transparency
Donated(address indexed donor, uint256 amount)Emitted when a donation is made.
Withdrawn(address indexed owner, uint256 amount)Emitted when the owner withdraws funds.
The contract deployer becomes the owner automatically
There is no function to change ownership (intentional for simplicity)
Always test on a testnet (Goerli, Sepolia) before mainnet deployment
Ensure you have the private key for the owner address securely stored
Consider testing:
Donations from multiple addresses
Owner withdrawal functionality
Non-owner withdrawal attempts (should fail)
Zero-value donation attempts (should fail)
Balance checking after various operations