-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemstore.go
More file actions
108 lines (97 loc) · 2.49 KB
/
memstore.go
File metadata and controls
108 lines (97 loc) · 2.49 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package paywall
import (
"sync"
"github.com/opd-ai/paywall/wallet"
)
// MemoryStore implements Store interface for in-memory payment tracking.
// This is a minimal implementation for demonstration purposes.
//
// Warning: Data is not persisted and will be lost on server restart
type MemoryStore struct {
payments map[string]*Payment
mu sync.RWMutex
}
// NewMemoryStore creates a new in-memory payment store instance.
//
// Returns:
// - *MemoryStore: Empty payment store
//
// Related: Store interface
func NewMemoryStore() *MemoryStore {
return &MemoryStore{
payments: make(map[string]*Payment),
}
}
// CreatePayment stores a new payment record.
//
// Parameters:
// - p: Payment record to store
//
// Returns:
// - error: Always returns nil in this implementation
func (m *MemoryStore) CreatePayment(p *Payment) error {
m.mu.Lock()
defer m.mu.Unlock()
m.payments[p.ID] = p
return nil
}
// GetPayment retrieves a payment record by ID.
//
// Parameters:
// - id: Payment identifier
//
// Returns:
// - *Payment: Payment record if found, nil if not found
// - error: Always nil in this implementation
func (m *MemoryStore) GetPayment(id string) (*Payment, error) {
m.mu.RLock()
defer m.mu.RUnlock()
return m.payments[id], nil
}
// UpdatePayment updates an existing payment record.
//
// Parameters:
// - p: Payment record with updated fields
//
// Returns:
// - error: Always nil in this implementation
func (m *MemoryStore) UpdatePayment(p *Payment) error {
m.mu.Lock()
defer m.mu.Unlock()
m.payments[p.ID] = p
return nil
}
// ListPendingPayments returns all pending payment records.
//
// Returns:
// - []*Payment: Slice of payments with 1 or fewer confirmations
// - error: Always nil in this implementation
func (m *MemoryStore) ListPendingPayments() ([]*Payment, error) {
m.mu.RLock()
defer m.mu.RUnlock()
var payments []*Payment
for _, p := range m.payments {
if p.Confirmations <= 1 {
payments = append(payments, p)
}
}
return payments, nil
}
// GetPaymentByAddress retrieves a payment record by Bitcoin address.
//
// Parameters:
// - addr: Bitcoin address associated with the payment
//
// Returns:
// - *Payment: Payment record if found, nil if not found
// - error: Always nil in this implementation
func (m *MemoryStore) GetPaymentByAddress(addr string) (*Payment, error) {
m.mu.RLock()
defer m.mu.RUnlock()
for _, p := range m.payments {
if p.Addresses[wallet.Bitcoin] == addr || p.Addresses[wallet.Monero] == addr {
return p, nil
}
}
return nil, nil
}