miracle-api 0.6.0

Miracle is a pay2e protocol for sovereign individuals living in Mirascape Horizon.
Documentation
//! Example: Building Claim Instructions with Database Hex String Data
//!
//! This example demonstrates how to use the Miracle API utils module
//! to convert database hex string output into properly formatted
//! EpochClaimData for building claim instructions.

use miracle_api::{
    dmt::DailyParticipantData,
    prelude::*,
    utils::{claim, hex},
};
use solana_program::pubkey::Pubkey;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    println!("🔍 Miracle API Utils Example: Building Claim Instructions");
    println!("========================================================");

    // ===== SCENARIO 1: Converting Database Hex Strings =====
    println!("\n📊 Scenario 1: Converting Database Hex Strings");
    println!("------------------------------------------------");

    // Simulate database query output (hex strings from SQL function)
    let db_customer_reward_pool = "0x1234567890abcdef";
    let db_merchant_reward_pool = "0xfedcba0987654321";
    let db_total_customer_activity = "0x0000000000000064"; // 100 in decimal
    let db_total_merchant_activity = "0x00000000000000c8"; // 200 in decimal

    println!("Database hex strings:");
    println!("  customer_reward_pool: {}", db_customer_reward_pool);
    println!("  merchant_reward_pool: {}", db_merchant_reward_pool);
    println!("  total_customer_activity: {}", db_total_customer_activity);
    println!("  total_merchant_activity: {}", db_total_merchant_activity);

    // Convert hex strings to EpochClaimData using utils
    let epoch_data = claim::epoch_claim_data_from_hex(
        db_customer_reward_pool,
        db_merchant_reward_pool,
        db_total_customer_activity,
        db_total_merchant_activity,
    )?;

    println!("\n✅ Converted to EpochClaimData:");
    println!(
        "  customer_reward_pool bytes: {:?}",
        epoch_data.customer_reward_pool
    );
    println!(
        "  merchant_reward_pool bytes: {:?}",
        epoch_data.merchant_reward_pool
    );
    println!(
        "  total_customer_activity bytes: {:?}",
        epoch_data.total_customer_activity
    );
    println!(
        "  total_merchant_activity bytes: {:?}",
        epoch_data.total_merchant_activity
    );

    // ===== SCENARIO 2: Converting Native u64 Values =====
    println!("\n📊 Scenario 2: Converting Native u64 Values");
    println!("---------------------------------------------");

    let native_customer_reward_pool = 1234567890u64;
    let native_merchant_reward_pool = 9876543210u64;
    let native_total_customer_activity = 100u64;
    let native_total_merchant_activity = 200u64;

    println!("Native u64 values:");
    println!("  customer_reward_pool: {}", native_customer_reward_pool);
    println!("  merchant_reward_pool: {}", native_merchant_reward_pool);
    println!(
        "  total_customer_activity: {}",
        native_total_customer_activity
    );
    println!(
        "  total_merchant_activity: {}",
        native_total_merchant_activity
    );

    // Convert u64 values to EpochClaimData using utils
    let epoch_data_u64 = claim::epoch_claim_data_from_u64(
        native_customer_reward_pool,
        native_merchant_reward_pool,
        native_total_customer_activity,
        native_total_merchant_activity,
    );

    println!("\n✅ Converted to EpochClaimData:");
    println!(
        "  customer_reward_pool bytes: {:?}",
        epoch_data_u64.customer_reward_pool
    );
    println!(
        "  merchant_reward_pool bytes: {:?}",
        epoch_data_u64.merchant_reward_pool
    );
    println!(
        "  total_customer_activity bytes: {:?}",
        epoch_data_u64.total_customer_activity
    );
    println!(
        "  total_merchant_activity bytes: {:?}",
        epoch_data_u64.total_merchant_activity
    );

    // ===== SCENARIO 3: Individual Hex Conversions =====
    println!("\n📊 Scenario 3: Individual Hex Conversions");
    println!("-------------------------------------------");

    // Convert individual hex strings to byte arrays
    let reward_pool_bytes = hex::hex_string_to_u64_bytes("0x1234567890abcdef")?;
    let hash_bytes = hex::hex_string_to_hash_bytes(
        "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
    )?;

    println!("Individual conversions:");
    println!("  reward_pool (8 bytes): {:?}", reward_pool_bytes);
    println!("  hash (32 bytes): {:?}", hash_bytes);

    // Convert bytes back to hex strings
    let reward_pool_hex = hex::bytes_to_hex_string(&reward_pool_bytes);
    println!("  reward_pool back to hex: {}", reward_pool_hex);

    // ===== SCENARIO 4: Building Claim Instruction =====
    println!("\n📊 Scenario 4: Building Claim Instruction");
    println!("------------------------------------------");

    // Create sample participant data
    let participant_data = DailyParticipantData::new(
        [1u8; 32],  // participant_id
        1723680000, // first_payment_timestamp
        1723766400, // last_payment_timestamp
        [1u8; 8],   // first_payment_tx_sig_short
        [2u8; 8],   // last_payment_tx_sig_short
        5,          // payment_count
        0,          // participant_type (customer)
    );

    // Create payment proof (empty for this example)
    let payment_proof = vec![[3u8; 32]];
    let payment_indices = vec![true];

    // Create seal proof (empty for this example)
    let seal_proof = vec![[4u8; 32]];
    let seal_indices = vec![false];

    // Payment root (32 bytes)
    let payment_root = [5u8; 32];

    // Build claim instruction using the converted epoch_data
    let claim_instruction = claim(
        Pubkey::new_from_array([6u8; 32]), // signer (placeholder)
        Pubkey::new_from_array([7u8; 32]), // beneficiary (placeholder)
        0,                                 // epoch
        participant_data,
        payment_proof,
        payment_indices,
        seal_proof,
        seal_indices,
        payment_root,
        epoch_data, // Using the converted epoch_data
        0,          // participant_type
        None,       // social_data
    );

    println!("✅ Claim instruction built successfully!");
    println!(
        "  Instruction data length: {} bytes",
        claim_instruction.data.len()
    );
    println!("  Number of accounts: {}", claim_instruction.accounts.len());

    // ===== SUMMARY =====
    println!("\n🎯 Summary");
    println!("===========");
    println!("✅ Successfully converted database hex strings to EpochClaimData");
    println!("✅ Successfully converted native u64 values to EpochClaimData");
    println!("✅ Successfully built claim instruction with converted data");
    println!("✅ All data type conversions working correctly");

    println!("\n🚀 The utils module successfully bridges the gap between:");
    println!("   - Database hex string output (from SQL functions)");
    println!("   - On-chain byte array requirements (for EpochClaimData)");
    println!("   - Claim instruction construction");

    Ok(())
}