SIMPLE DONATION CONTRACT
ð° 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
ãããª
ããã¯ã¹ã¿ãã¯
説æ
ð° 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 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
âš Features
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
ð§ Contract Functions
donate()
Visibility: External, Payable
Description: Allows anyone to donate ETH to the contract
Requirements: Donation amount must be greater than 0
Events: Emits
Donatedevent
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
Withdrawnevent
getBalance()
Visibility: External, View
Description: Returns the current balance of the contract
Returns: Current contract balance in Wei
ð State Variables
owner: Address of the contract deployer/ownerdonations: Mapping of donor addresses to their total donated amountstotalDonations: Total amount of all donations received
ð Deployment
Prerequisites
Solidity compiler version
^0.8.0Ethereum wallet (MetaMask, etc.)
ETH for gas fees
Deploy Steps
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.0or higherDeploy 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>
ð¡ Usage Examples
Donating ETH
// Using Web3.js
await donationContract.methods.donate().send({
from: userAddress,
value: web3.utils.toWei('1', 'ether')
});Checking Balance
// Using Web3.js
const balance = await donationContract.methods.getBalance().call();
console.log('Contract balance:', web3.utils.fromWei(balance, 'ether'), 'ETH');Withdrawing Funds (Owner Only)
// Using Web3.js
await donationContract.methods.withdraw().send({
from: ownerAddress
});ð Security Features
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
ð Events
Donated(address indexed donor, uint256 amount)
Emitted when a donation is made.
Withdrawn(address indexed owner, uint256 amount)
Emitted when the owner withdraws funds.
â ïž Important Notes
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
𧪠Testing
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