transaction.service.ts raw
1 import { SMESH_API_BASE_URL } from '@/constants'
2
3 class TransactionService {
4 static instance: TransactionService
5
6 constructor() {
7 if (!TransactionService.instance) {
8 TransactionService.instance = this
9 }
10 return TransactionService.instance
11 }
12
13 async createTransaction(
14 pubkey: string,
15 amount: number
16 ): Promise<{
17 transactionId: string
18 invoiceId: string
19 }> {
20 const url = new URL('/v1/transactions', SMESH_API_BASE_URL).toString()
21 const response = await fetch(url, {
22 method: 'POST',
23 headers: {
24 'Content-Type': 'application/json'
25 },
26 body: JSON.stringify({
27 pubkey,
28 amount,
29 purpose: 'translation'
30 })
31 })
32 const data = await response.json()
33 if (!response.ok) {
34 throw new Error(data.error ?? 'Failed to create transaction')
35 }
36 return data
37 }
38
39 async checkTransaction(transactionId: string): Promise<{
40 state: 'pending' | 'failed' | 'settled'
41 }> {
42 const url = new URL(`/v1/transactions/${transactionId}/check`, SMESH_API_BASE_URL).toString()
43 const response = await fetch(url, {
44 method: 'POST'
45 })
46 const data = await response.json()
47 if (!response.ok) {
48 throw new Error(data.error ?? 'Failed to complete transaction')
49 }
50 return data
51 }
52 }
53
54 const instance = new TransactionService()
55 export default instance
56