Skip to content
latraelPublic

About

AidLedger is a Solana-based platform for registering NGOs, publishing verifiable on-chain impact data, and enabling transparent, tamper-proof auditing. This is the on-chain work.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

ย 

History

9 Commits

Folders and files

Repository files navigation

AidLedger - Smart Contract Backend

Solana Anchor Rust TypeScript

AidLedger is a decentralized platform built on Solana for registering NGOs and publishing verifiable, tamper-proof impact data on-chain. This repository contains the smart contract backend and blockchain infrastructure that powers transparent, auditable aid distribution tracking.

๐Ÿ”— Live Deployment

๐Ÿ“ Solana Devnet Program ID: 4wcEn4cPenW3GM1eYfNoAHsmnN1SPNLnLqSCtBruaobD

๐Ÿ” View on Solana Explorer | ๐ŸŒ Network: Devnet

Note: This is the backend/blockchain repository. The frontend Repository is at: aidledger-frontend.

๐Ÿ“‘ Table of Contents

๐ŸŒŸ Overview

AidLedger addresses the critical need for transparency and accountability in humanitarian aid distribution. By leveraging Solana's high-performance blockchain, we enable:

  • Immutable Record Keeping: All NGO registrations and aid distribution batches are permanently recorded on-chain
  • Real-time Verification: Stakeholders can instantly verify aid distribution data without intermediaries
  • Cost-Effective Operations: Solana's low transaction fees make micro-transactions economically viable
  • Global Accessibility: Decentralized infrastructure ensures 24/7 availability regardless of location
  • Cryptographic Integrity: Merkle trees and on-chain verification prevent data tampering

๐Ÿš€ Why Solana?

Performance & Scalability

  • Sub-second finality: Transactions confirm in ~400ms, enabling real-time aid tracking
  • High throughput: 65,000+ TPS capacity handles global scale humanitarian operations
  • Low costs: ~$0.00025 per transaction makes frequent updates economically viable

Developer Experience

  • Anchor Framework: Type-safe smart contract development with automatic IDL generation
  • Rich Tooling: Comprehensive CLI tools, local development networks, and debugging capabilities
  • Account Model: Flexible data storage patterns optimized for complex application state

Ecosystem Benefits

  • Composability: Easy integration with DeFi protocols for automated fund management
  • Interoperability: Cross-chain bridges enable multi-blockchain humanitarian ecosystems
  • Energy Efficiency: Proof-of-History consensus is environmentally sustainable

๐Ÿ—๏ธ Technical Architecture

Smart Contract Design

// Core program instructions
pub fn register_ngo(ctx: Context<RegisterNgo>, metadata_uri: String) -> Result<()>
pub fn submit_batch(
    ctx: Context<SubmitBatch>,
    batch_index: u64,
    merkle_root: [u8; 32],
    data_uri: String,
    region: String,
    program_tag: String,
    start_time: i64,
    end_time: i64,
) -> Result<()>

Account Structure

NGO Account (~310 bytes)

pub struct Ngo {
    pub admin: Pubkey,           // 32 bytes - NGO administrator wallet
    pub metadata_uri: String,    // Variable - IPFS link to detailed NGO information
    pub is_active: bool,         // 1 byte - Active status flag
    pub bump: u8,               // 1 byte - PDA bump seed
    pub created_at: i64,        // 8 bytes - Unix timestamp
}

Batch Account (~494 bytes)

pub struct Batch {
    pub ngo: Pubkey,            // 32 bytes - Associated NGO account
    pub batch_index: u64,       // 8 bytes - Sequential batch identifier
    pub merkle_root: [u8; 32],  // 32 bytes - Root hash of beneficiary data
    pub data_uri: String,       // Variable - IPFS link to full batch data
    pub region: String,         // Variable - Geographic region
    pub program_tag: String,    // Variable - Aid program identifier
    pub start_time: i64,        // 8 bytes - Distribution period start
    pub end_time: i64,          // 8 bytes - Distribution period end
    pub is_flagged: bool,       // 1 byte - Audit flag
    pub bump: u8,              // 1 byte - PDA bump seed
}

Program Derived Addresses (PDAs)

// NGO Account PDA: Deterministic address per wallet
seeds = [b"ngo", admin.key().as_ref()]

// Batch Account PDA: Deterministic address per NGO + batch index
seeds = [b"batch", ngo.key().as_ref(), &batch_index.to_le_bytes()]

๐Ÿ› ๏ธ Technology Stack

Core Blockchain

  • Solana: Layer-1 blockchain with sub-second finality
  • Anchor Framework v0.30.1: Solana's Sealevel runtime framework
  • Rust: Systems programming language for on-chain programs

Development Tools

  • Anchor CLI: Build, test, and deploy smart contracts
  • Solana CLI: Blockchain interaction and wallet management
  • TypeScript: Type-safe off-chain interaction scripts

Infrastructure

  • IPFS: Decentralized storage for metadata and batch data
  • Pinata: IPFS pinning service for data persistence
  • Solana Devnet: Testing and development environment

๐Ÿ“‚ Project Structure

aidledger/
โ”œโ”€โ”€ programs/
โ”‚   โ””โ”€โ”€ aidledger/
โ”‚       โ”œโ”€โ”€ src/
โ”‚       โ”‚   โ”œโ”€โ”€ lib.rs              # Main program entry point
โ”‚       โ”‚   โ”œโ”€โ”€ instructions.rs     # Program instructions implementation
โ”‚       โ”‚   โ””โ”€โ”€ state.rs           # Account structure definitions
โ”‚       โ””โ”€โ”€ Cargo.toml             # Rust dependencies
โ”œโ”€โ”€ offchain/
โ”‚   โ”œโ”€โ”€ registerNgo.ts            # NGO registration script
โ”‚   โ”œโ”€โ”€ submitBatch.ts            # Batch submission script
โ”‚   โ””โ”€โ”€ package.json              # Node.js dependencies
โ”œโ”€โ”€ tests/
โ”‚   โ””โ”€โ”€ aidledger.ts              # Comprehensive test suite
โ”œโ”€โ”€ migrations/
โ”‚   โ””โ”€โ”€ deploy.ts                 # Deployment script
โ”œโ”€โ”€ Anchor.toml                   # Anchor configuration
โ”œโ”€โ”€ Cargo.toml                    # Workspace configuration
โ””โ”€โ”€ package.json                  # Project metadata

๐Ÿš€ Quick Start

Prerequisites

# Install Rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

# Install Solana CLI
sh -c "$(curl -sSfL https://release.solana.com/v1.17.0/install)"

# Install Anchor CLI
npm install -g @coral-xyz/[email protected]

# Install Node.js dependencies
yarn install

Development Setup

# Configure Solana for devnet
solana config set --url devnet

# Generate a new wallet (or use existing)
solana-keygen new --outfile ~/.config/solana/id.json

# Request SOL airdrop for testing
solana airdrop 2

# Build the smart contract
anchor build

# Run tests
anchor test

# Deploy to devnet
anchor deploy --provider.cluster devnet

Local Development

# Start local validator
solana-test-validator

# Deploy to local network
anchor deploy --provider.cluster localnet

# Run interactive tests
anchor test --skip-local-validator

๐Ÿงช Testing

Comprehensive Test Suite

Our test suite covers all critical functionality:

# Run all tests
anchor test

# Run specific test categories
anchor test --grep "NGO Registration"
anchor test --grep "Batch Submission"
anchor test --grep "Error Handling"

Test Coverage

  • โœ… NGO registration with metadata validation
  • โœ… Duplicate NGO prevention
  • โœ… Batch submission with proper authority checks
  • โœ… Sequential batch indexing
  • โœ… Account size validation
  • โœ… Unauthorized access prevention
  • โœ… Edge case error handling

๐Ÿ“‹ Usage Examples

NGO Registration

// Register a new NGO on-chain
const metadataUri = "ipfs://QmNGOMetadataHash";
const tx = await program.methods
  .registerNgo(metadataUri)
  .accounts({
    ngo: ngoPda,
    admin: adminKeypair.publicKey,
    systemProgram: SystemProgram.programId,
  })
  .signers([adminKeypair])
  .rpc();

Batch Submission

// Submit aid distribution batch
const tx = await program.methods
  .submitBatch(
    new anchor.BN(batchIndex),
    merkleRoot,
    "ipfs://QmBatchDataHash",
    "Kenya",
    "CashTransfers-2024",
    new anchor.BN(startTime),
    new anchor.BN(endTime)
  )
  .accounts({
    ngo: ngoPda,
    batch: batchPda,
    admin: adminKeypair.publicKey,
    systemProgram: SystemProgram.programId,
  })
  .signers([adminKeypair])
  .rpc();

๐Ÿ”ง CLI Tools

NGO Registration Script

cd offchain
ANCHOR_WALLET=~/.config/solana/id.json \
ANCHOR_PROVIDER_URL=https://api.devnet.solana.com \
npx ts-node registerNgo.ts "ipfs://QmYourMetadataHash"

Batch Submission Script

cd offchain
ANCHOR_WALLET=~/.config/solana/id.json \
ANCHOR_PROVIDER_URL=https://api.devnet.solana.com \
npx ts-node submitBatch.ts \
  "NGO_PDA_ADDRESS" \
  "0" \
  "ipfs://QmBatchDataHash" \
  "Kenya" \
  "CashTransfers-2024" \
  "1640995200" \
  "1643587200"

๐ŸŒ Deployment

Current Deployments

  • Program ID: 4wcEn4cPenW3GM1eYfNoAHsmnN1SPNLnLqSCtBruaobD
  • Network: Solana Devnet
  • RPC Endpoint: https://api.devnet.solana.com

Production Deployment

# Configure for mainnet
solana config set --url mainnet-beta

# Deploy with sufficient SOL for rent exemption
anchor deploy --provider.cluster mainnet-beta

# Update program ID in Anchor.toml
anchor keys sync

๐Ÿค Integration with Frontend

This smart contract backend integrates seamlessly with the aidledger-frontend web application:

API Integration Points

  • NGO Management: Registration, listing, and metadata retrieval
  • Batch Operations: Submission, querying, and verification
  • Wallet Integration: Multi-wallet support and transaction signing
  • Real-time Updates: WebSocket connections for live data synchronization

IDL Generation

# Generate TypeScript client
anchor build
# IDL available at: target/idl/aidledger.json

๐Ÿ“Š Performance Metrics

Transaction Costs (Devnet)

  • NGO Registration: 0.003 SOL ($0.10)
  • Batch Submission: 0.004 SOL ($0.13)
  • Account Queries: Free (RPC calls)

Throughput Capabilities

  • Theoretical: 65,000+ TPS (Solana network limit)
  • Practical: Limited by application logic and account contention
  • Batch Processing: Parallel submissions across different NGOs

๐Ÿ”’ Security Considerations

Access Control

  • Only NGO administrators can submit batches for their organization
  • Program Derived Addresses prevent unauthorized account access
  • Signer validation on all state-changing operations

Data Integrity

  • Merkle roots provide cryptographic proof of batch data integrity
  • IPFS content addressing ensures immutable metadata storage
  • On-chain timestamps prevent backdating of aid distribution

Audit Trail

  • All operations emit events for external monitoring
  • Account state changes are permanently recorded
  • Batch flagging mechanism for dispute resolution

๐Ÿ›ฃ๏ธ Roadmap

Phase 1: Core Infrastructure โœ…

  • NGO registration system
  • Batch submission mechanism
  • Basic access controls
  • Test coverage

Phase 2: Enhanced Features ๐Ÿšง

  • Multi-signature NGO administration
  • Batch verification challenges
  • Automated compliance checks
  • Cross-program composability

Phase 3: Ecosystem Integration ๐Ÿ”ฎ

  • DeFi integration for automated funding
  • Cross-chain bridge support
  • Mobile SDK development
  • Enterprise API gateway

๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿค Contributing

We welcome contributions! Please see our Contributing Guidelines for details on:

  • Code style and standards
  • Testing requirements
  • Pull request process
  • Security vulnerability reporting

๐Ÿ“ž Support & Community


Built with โค๏ธ for humanitarian transparency on Solana

About

AidLedger is a Solana-based platform for registering NGOs, publishing verifiable on-chain impact data, and enabling transparent, tamper-proof auditing. This is the on-chain work.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages