File size: 1,190 Bytes
d16c049
80d4bc1
 
 
 
 
 
 
 
 
d16c049
 
80d4bc1
 
d16c049
80d4bc1
d16c049
80d4bc1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { BotContext } from '../types/botTypes';
import { createLogger } from '../../utils/logger';
import { supabase } from '../../db/supabase';

const logger = createLogger('CryptoService');

export class CryptoService {
  private static instance: CryptoService;
  private readonly cryptoAddress: string;

  private constructor(ctx: BotContext) {
    this.cryptoAddress = ctx.botData?.crypto_wallet_address || '';
  }

  public static getInstance(ctx: BotContext): CryptoService {
    if (!CryptoService.instance) {
      CryptoService.instance = new CryptoService(ctx);
    }
    return CryptoService.instance;
  }

  async getPaymentAddress(userId: string, amount: number): Promise<string> {
    try {
      // Create payment record in database
      const { data: payment, error } = await supabase
        .from('payments')
        .insert({
          user_id: userId,
          amount: amount,
          status: 'PENDING',
          payment_method: 'CRYPTO'
        })
        .select()
        .single();

      if (error) throw error;

      return this.cryptoAddress;
    } catch (error) {
      logger.error('Error creating crypto payment:', error);
      throw error;
    }
  }
}