Skip to content

Latest commit

 

History

History
323 lines (243 loc) · 11.1 KB

File metadata and controls

323 lines (243 loc) · 11.1 KB

Alchemy SDK MeToken Creation Implementation

This document outlines the complete implementation of MeToken creation using Alchemy SDK and Supabase integration, following the requirements specified in your query.

🏗️ Architecture Overview

The implementation follows a three-tier architecture:

  1. Frontend Layer: React components using Account Kit for smart account interactions
  2. Backend Layer: Supabase Edge Functions and API routes for orchestration
  3. Blockchain Layer: Alchemy SDK for reliable blockchain interactions
┌─────────────────┐    ┌──────────────────┐    ┌─────────────────┐
│   Frontend      │    │    Supabase      │    │   Alchemy SDK   │
│   (React)       │◄──►│   (Backend)      │◄──►│  (Blockchain)   │
│                 │    │                  │    │                 │
│ • Account Kit   │    │ • Edge Functions │    │ • Diamond       │
│ • Smart Wallets │    │ • API Routes     │    │   Contract      │
│ • UI Components │    │ • Database       │    │ • DAI Token     │
└─────────────────┘    └──────────────────┘    └─────────────────┘

📁 File Structure

New Files Created

lib/sdk/alchemy/
├── metoken-service.ts          # Core Alchemy SDK integration
└── (existing files...)

supabase/functions/
└── create-metoken/
    └── index.ts                # Edge Function for orchestration

components/UserProfile/
├── AlchemyMeTokenCreator.tsx   # Enhanced frontend component
└── (existing files...)

app/api/metokens/
├── alchemy/
│   └── route.ts                # API route for Alchemy integration
└── (existing files...)

lib/sdk/supabase/
└── enhanced-schema.sql         # Enhanced database schema

🔧 Implementation Details

1. Alchemy SDK Integration (lib/sdk/alchemy/metoken-service.ts)

Key Features:

  • Reliable Blockchain Access: Uses Alchemy's Supernode for consistent data
  • Gas Optimization: Leverages Alchemy's gas estimation and optimization
  • Transaction Management: Handles approval and creation in sequence
  • Error Handling: Comprehensive error handling with detailed messages

Core Functions:

// Create a new MeToken
await alchemyMeTokenService.createMeToken({
  name: "My Creative Token",
  symbol: "MCT",
  hubId: 1,
  assetsDeposited: "100.00",
  creatorAddress: "0x..."
});

// Get MeToken information
const info = await alchemyMeTokenService.getMeTokenInfo(meTokenAddress);

// Check subscription status
const isSubscribed = await alchemyMeTokenService.isMeTokenSubscribed(meTokenAddress);

2. Supabase Edge Function (supabase/functions/create-metoken/index.ts)

Purpose: Orchestrates the MeToken creation process and manages data storage.

Workflow:

  1. Validation: Checks user authentication and input parameters
  2. Duplicate Prevention: Ensures one MeToken per creator address
  3. Transaction Tracking: Monitors blockchain transactions
  4. Data Storage: Stores MeToken data in Supabase database
  5. Analytics: Records creation transactions for analytics

3. Enhanced Frontend Component (components/UserProfile/AlchemyMeTokenCreator.tsx)

Features:

  • Smart Account Integration: Uses Account Kit for seamless UX
  • Real-time Balance Checking: Monitors DAI balance and allowance
  • Transaction Status: Shows detailed progress during creation
  • Error Handling: User-friendly error messages and recovery options
  • Gas Optimization: Automatic gas estimation and optimization

User Experience:

  1. User enters MeToken details (name, symbol, hub ID, DAI amount)
  2. Component checks DAI balance and allowance
  3. If needed, approves DAI for Diamond contract
  4. Deploys MeToken via METOKEN_FACTORY.create contract call
  5. Tracks create transaction and calls DIAMOND.mint to mint tokens (with Supabase storage of both create and mint tx data)
  6. Shows success confirmation with transaction links

4. Enhanced Database Schema (lib/sdk/supabase/enhanced-schema.sql)

New Tables:

  • metoken_analytics: Trading and performance metrics
  • alchemy_integrations: Alchemy SDK integration tracking
  • gas_optimizations: Gas usage optimization data

Enhanced Features:

  • Full-text Search: Search MeTokens by name, symbol, description
  • Analytics Views: Pre-computed views for common queries
  • Row Level Security: Secure access control
  • Automatic Timestamps: Updated_at triggers for all tables

🚀 Usage Instructions

1. Environment Setup

Add these environment variables to your .env.local:

# Alchemy Configuration
NEXT_PUBLIC_ALCHEMY_API_KEY=your_alchemy_api_key
ALCHEMY_SWAP_PRIVATE_KEY=your_private_key_for_server_operations

# Supabase Configuration
NEXT_PUBLIC_SUPABASE_URL=your_supabase_url
NEXT_PUBLIC_SUPABASE_ANON_KEY=your_supabase_anon_key
SUPABASE_SERVICE_ROLE_KEY=your_supabase_service_key

2. Database Setup

Run the enhanced schema in your Supabase SQL editor:

-- Copy and paste the contents of lib/sdk/supabase/enhanced-schema.sql
-- This will create all necessary tables, indexes, and policies

3. Deploy Supabase Edge Function

# Install Supabase CLI if not already installed
npm install -g supabase

# Deploy the Edge Function
supabase functions deploy create-metoken

4. Frontend Integration

Use the new Alchemy MeToken Creator component:

import { AlchemyMeTokenCreator } from '@/components/UserProfile/AlchemyMeTokenCreator';

function MyPage() {
  const handleMeTokenCreated = (meTokenAddress: string, transactionHash: string) => {
    console.log('MeToken created:', { meTokenAddress, transactionHash });
    // Handle success (redirect, show notification, etc.)
  };

  return (
    <AlchemyMeTokenCreator onMeTokenCreated={handleMeTokenCreated} />
  );
}

🔄 MeToken Creation Flow

Step 1: Frontend Validation

  1. User connects smart wallet using Account Kit
  2. Component validates MeToken parameters
  3. Checks DAI balance and allowance

Step 2: Blockchain Interaction

  1. DAI Approval: If needed, approves DAI for Diamond contract
  2. MeToken Deployment: Calls METOKEN_FACTORY.create to deploy the MeToken contract
  3. Token Minting: Calls DIAMOND.mint to mint initial tokens
  4. Transaction Confirmation: Waits for blockchain confirmation

Step 3: Data Storage

  1. Transaction Tracking: Records transaction in Supabase
  2. MeToken Storage: Stores MeToken data with blockchain info
  3. Analytics: Updates analytics and metrics

Step 4: User Feedback

  1. Success Notification: Shows transaction hash and MeToken address
  2. External Links: Provides links to BaseScan for verification
  3. Form Reset: Clears form for next creation

💰 Cost Analysis

Alchemy Infrastructure

  • Free Plan: 300M Compute Units/month (sufficient for development)
  • Growth Plan: $49/month (for production scaling)
  • Scale Plan: $199/month (for high-volume applications)

Supabase Infrastructure

  • Free Plan: 500MB database, 500K Edge Function invocations (sufficient for MVP)
  • Pro Plan: $25/month + usage (for production)

MeToken Protocol Fees

  • Mint Fee: Up to 5% (controlled by governance)
  • Gas Costs: Optimized using Alchemy's gas estimation
  • DAI Collateral: Required for MeToken creation

🔒 Security Considerations

Smart Contract Security

  • Diamond Standard: Uses battle-tested Diamond proxy pattern
  • Access Control: Proper ownership and permission management
  • Fee Limits: Maximum 5% fee rate enforced by governance

Application Security

  • Row Level Security: Supabase RLS policies protect user data
  • Authentication: JWT-based authentication for API access
  • Input Validation: Comprehensive validation on all inputs
  • Error Handling: Secure error messages without sensitive data

Key Management

  • Account Kit: Secure smart account management
  • No Private Keys: Client-side operations use smart accounts
  • Environment Variables: Secure storage of API keys

📊 Monitoring and Analytics

Built-in Analytics

  • Trading Metrics: Volume, trades, unique traders
  • Liquidity Tracking: Pooled and locked balances
  • User Activity: Holder counts and growth
  • Gas Optimization: Gas usage and savings tracking

Alchemy Monitoring

  • Transaction Status: Real-time transaction monitoring
  • Gas Optimization: Automatic gas price optimization
  • Error Tracking: Comprehensive error logging and alerts

🚀 Deployment Checklist

Pre-deployment

  • Environment variables configured
  • Database schema deployed
  • Supabase Edge Functions deployed
  • Alchemy API keys configured
  • Smart contract addresses verified

Post-deployment

  • Test MeToken creation flow
  • Verify database storage
  • Check transaction tracking
  • Monitor gas optimization
  • Test error handling

🔧 Troubleshooting

Common Issues

1. "Insufficient DAI balance"

  • Solution: Use the DAI funding options component
  • Check: DAI balance and allowance

2. "Transaction failed"

  • Solution: Check gas prices and network congestion
  • Retry: With higher gas limit if needed

3. "MeToken already exists"

  • Solution: Each address can only create one MeToken
  • Check: Existing MeToken in database

4. "Alchemy API errors"

  • Solution: Verify API key and rate limits
  • Check: Alchemy dashboard for usage

Debug Mode

Enable debug logging by setting:

NEXT_PUBLIC_DEBUG_METOKEN=true

This will provide detailed console logs for troubleshooting.

📈 Future Enhancements

Planned Features

  1. Batch Operations: Create multiple MeTokens in one transaction
  2. Advanced Analytics: Real-time price feeds and market data
  3. Social Features: MeToken discovery and social trading
  4. Mobile Support: React Native integration
  5. Multi-chain: Support for additional networks

Performance Optimizations

  1. Caching: Redis integration for faster queries
  2. CDN: Static asset optimization
  3. Database: Query optimization and indexing
  4. Blockchain: Batch transaction processing

📚 Additional Resources

🤝 Support

For technical support or questions:

  1. Check the troubleshooting section above
  2. Review the console logs for error details
  3. Verify environment configuration
  4. Test with smaller amounts first
  5. Contact support with specific error messages

This implementation provides a robust, scalable, and user-friendly solution for MeToken creation using Alchemy SDK and Supabase, following all the requirements and best practices outlined in your query.