hackquest logo

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 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

📊 State Variables

  • owner: Address of the contract deployer/owner

  • donations: Mapping of donor addresses to their total donated amounts

  • totalDonations: Total amount of all donations received

🚀 Deployment

Prerequisites

  • Solidity compiler version ^0.8.0

  • Ethereum wallet (MetaMask, etc.)

  • ETH for gas fees

Deploy Steps

  1. 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)

  2. Using Hardhat:

    npx hardhat compile
    npx hardhat run scripts/deploy.js --network <network-name>
  3. 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:

  1. Donations from multiple addresses

  2. Owner withdrawal functionality

  3. Non-owner withdrawal attempts (should fail)

  4. Zero-value donation attempts (should fail)

  5. Balance checking after various operations

ハッカ゜ンの進行状況

Our team has made significant progress in developing our project during the hackathon. We began by brainstorming ideas that address real-world problems and finalized our concept — a blockchain-based donation system ensuring transparency and trust in charitable contributions. After defining the architecture, we wrote and deployed a smart contract in Solidity using Remix IDE. The contract records all donations, tracks donors, and allows only the verified owner to withdraw funds. We successfully connected Remix with GitHub for version control and documentation. The next steps include integrating a simple frontend for user interaction, testing transactions on a test network, and refining the UI for better usability. Through effective collaboration and continuous debugging, our team has built a functional prototype that demonstrates the power of decentralized technology in solving social issues transparently and efficiently.

テックスタック

Solidity
チヌムリヌダヌ
Bbishal pritam chowdhury
オヌプン゜ヌス
業界
DeFi