|
1 | 1 | 'use server' |
2 | 2 | import { unstable_cache } from 'next/cache' |
3 | | -import { fetchWithSentry } from '@/utils' |
4 | 3 | import { getExchangeRate } from './exchange-rate' |
5 | 4 | import { AccountType } from '@/interfaces' |
| 5 | +import { mantecaApi } from '@/services/manteca' |
| 6 | + |
| 7 | +const MANTECA_CURRENCIES = ['ARS', 'BRL', 'COP', 'CRC', 'PUSD', 'GTQ', 'PHP', 'BOB'] |
6 | 8 |
|
7 | 9 | export const getCurrencyPrice = unstable_cache( |
8 | | - async (currencyCode: string): Promise<number> => { |
9 | | - let price: number |
| 10 | + async (currencyCode: string): Promise<{ buy: number; sell: number }> => { |
| 11 | + let buy: number |
| 12 | + let sell: number |
10 | 13 | currencyCode = currencyCode.toUpperCase() |
11 | | - switch (currencyCode) { |
12 | | - case 'USD': |
13 | | - price = 1 |
14 | | - break |
15 | | - case 'EUR': |
16 | | - case 'MXN': |
17 | | - { |
18 | | - let accountType: AccountType |
19 | | - if (currencyCode === 'EUR') { |
20 | | - accountType = AccountType.IBAN |
21 | | - } else if (currencyCode === 'MXN') { |
22 | | - accountType = AccountType.CLABE |
23 | | - } else { |
24 | | - throw new Error('Invalid currency code') |
25 | | - } |
26 | | - const { data, error } = await getExchangeRate(accountType) |
27 | | - if (error) { |
28 | | - throw new Error('Failed to fetch exchange rate from bridge') |
29 | | - } |
30 | | - if (!data) { |
31 | | - throw new Error('No data returned from exchange rate API') |
32 | | - } |
33 | | - price = parseFloat(data.buy_rate) |
34 | | - } |
35 | | - break |
36 | | - case 'ARS': |
37 | | - { |
38 | | - const response = await fetchWithSentry('https://dolarapi.com/v1/dolares/cripto') |
39 | | - const data = await response.json() |
40 | | - |
41 | | - if (!data.compra || !data.venta) { |
42 | | - throw new Error('Invalid response from dolarapi') |
43 | | - } |
44 | | - |
45 | | - // Average between buy and sell price |
46 | | - price = (data.compra + data.venta) / 2 |
47 | | - } |
48 | | - break |
49 | | - default: |
50 | | - throw new Error('Unsupported currency') |
| 14 | + if (currencyCode === 'USD') { |
| 15 | + buy = 1 |
| 16 | + sell = 1 |
| 17 | + } else if (['EUR', 'MXN'].includes(currencyCode)) { |
| 18 | + let accountType: AccountType |
| 19 | + if (currencyCode === 'EUR') { |
| 20 | + accountType = AccountType.IBAN |
| 21 | + } else if (currencyCode === 'MXN') { |
| 22 | + accountType = AccountType.CLABE |
| 23 | + } else { |
| 24 | + throw new Error('Invalid currency code') |
| 25 | + } |
| 26 | + const { data, error } = await getExchangeRate(accountType) |
| 27 | + if (error) { |
| 28 | + throw new Error('Failed to fetch exchange rate from bridge') |
| 29 | + } |
| 30 | + if (!data) { |
| 31 | + throw new Error('No data returned from exchange rate API') |
| 32 | + } |
| 33 | + buy = parseFloat(data.buy_rate) |
| 34 | + sell = parseFloat(data.sell_rate) |
| 35 | + } else if (MANTECA_CURRENCIES.includes(currencyCode)) { |
| 36 | + const response = await mantecaApi.getPrices({ asset: 'USDC', against: currencyCode }) |
| 37 | + buy = Number(response.effectiveBuy) |
| 38 | + sell = Number(response.effectiveSell) |
| 39 | + } else { |
| 40 | + throw new Error('Invalid currency code') |
51 | 41 | } |
52 | | - return price |
| 42 | + return { buy, sell } |
53 | 43 | }, |
54 | 44 | ['getCurrencyPrice'], |
55 | 45 | { |
|
0 commit comments