Overview
Implement persistent storage for transaction records and idempotency keys using SQLite.
Implementation Details
Files:
internal/storage/interface.go - Storage interface
internal/storage/sqlite.go - SQLite implementation
internal/middleware/idempotency.go - Idempotency middleware
Storage Interface
type Storage interface {
// Transactions
CreateTransaction(ctx, tx *TransactionRecord) error
UpdateTransaction(ctx, id string, updates map[string]interface{}) error
GetTransaction(ctx, id string) (*TransactionRecord, error)
GetTransactionByHash(ctx, txHash string) (*TransactionRecord, error)
ListTransactions(ctx, filters map[string]interface{}, limit, offset int) ([]*TransactionRecord, error)
// Idempotency
GetIdempotencyKey(ctx, key string) (*IdempotencyRecord, error)
SetIdempotencyKey(ctx, record *IdempotencyRecord) error
CleanupExpiredIdempotencyKeys(ctx) error
// Lifecycle
Close() error
Migrate(ctx) error
}
Data Models
TransactionRecord:
- ID, Network, TxHash, From, To, Amount, Asset
- Status (pending, confirmed, failed)
- CreatedAt, ConfirmedAt, ErrorMessage
IdempotencyRecord:
- Key, Response, CreatedAt, ExpiresAt
SQLite Setup
- WAL mode for better concurrency
- Parameterized queries (prevent SQL injection)
- Indexes on tx_hash, status, expires_at
Idempotency Middleware
- Check
Idempotency-Key header on POST requests
- Return cached response if key exists
- Store response after successful processing
- Set
X-Idempotency-Replayed: true header on replay
Acceptance Criteria
Dependencies
Branch
feature/storage
Overview
Implement persistent storage for transaction records and idempotency keys using SQLite.
Implementation Details
Files:
internal/storage/interface.go- Storage interfaceinternal/storage/sqlite.go- SQLite implementationinternal/middleware/idempotency.go- Idempotency middlewareStorage Interface
Data Models
TransactionRecord:
IdempotencyRecord:
SQLite Setup
Idempotency Middleware
Idempotency-Keyheader on POST requestsX-Idempotency-Replayed: trueheader on replayAcceptance Criteria
Dependencies
Branch
feature/storage