|
| 1 | +import { fetchAnalytics } from "data/analytics/fetch-analytics"; |
| 2 | + |
| 3 | +export interface WalletStats { |
| 4 | + date: string; |
| 5 | + uniqueWalletsConnected: number; |
| 6 | + totalConnections: number; |
| 7 | + walletType: string; |
| 8 | +} |
| 9 | + |
| 10 | +export interface WalletUserStats { |
| 11 | + date: string; |
| 12 | + newUsers: number; |
| 13 | + returningUsers: number; |
| 14 | + totalUsers: number; |
| 15 | +} |
| 16 | + |
| 17 | +export interface InAppWalletStats { |
| 18 | + date: string; |
| 19 | + authenticationMethod: string; |
| 20 | + uniqueWalletsConnected: number; |
| 21 | +} |
| 22 | + |
| 23 | +export interface EcosystemWalletStats extends InAppWalletStats {} |
| 24 | + |
| 25 | +export interface UserOpStats { |
| 26 | + date: string; |
| 27 | + successful: number; |
| 28 | + failed: number; |
| 29 | + sponsoredUsd: number; |
| 30 | + chainId?: string; |
| 31 | +} |
| 32 | + |
| 33 | +interface AnalyticsQueryParams { |
| 34 | + clientId?: string; |
| 35 | + accountId?: string; |
| 36 | + from?: Date; |
| 37 | + to?: Date; |
| 38 | + period?: "day" | "week" | "month" | "year" | "all"; |
| 39 | +} |
| 40 | + |
| 41 | +function buildSearchParams(params: AnalyticsQueryParams): URLSearchParams { |
| 42 | + const searchParams = new URLSearchParams(); |
| 43 | + if (params.clientId) { |
| 44 | + searchParams.append("clientId", params.clientId); |
| 45 | + } |
| 46 | + if (params.accountId) { |
| 47 | + searchParams.append("accountId", params.accountId); |
| 48 | + } |
| 49 | + if (params.from) { |
| 50 | + searchParams.append("from", params.from.toISOString()); |
| 51 | + } |
| 52 | + if (params.to) { |
| 53 | + searchParams.append("to", params.to.toISOString()); |
| 54 | + } |
| 55 | + if (params.period) { |
| 56 | + searchParams.append("period", params.period); |
| 57 | + } |
| 58 | + return searchParams; |
| 59 | +} |
| 60 | + |
| 61 | +export async function getWalletConnections( |
| 62 | + params: AnalyticsQueryParams, |
| 63 | +): Promise<WalletStats[]> { |
| 64 | + const searchParams = buildSearchParams(params); |
| 65 | + const res = await fetchAnalytics(`v1/wallets?${searchParams.toString()}`, { |
| 66 | + method: "GET", |
| 67 | + headers: { "Content-Type": "application/json" }, |
| 68 | + }); |
| 69 | + |
| 70 | + if (res?.status !== 200) { |
| 71 | + console.error("Failed to fetch wallet connections"); |
| 72 | + return []; |
| 73 | + } |
| 74 | + |
| 75 | + const json = await res.json(); |
| 76 | + return json.data as WalletStats[]; |
| 77 | +} |
| 78 | + |
| 79 | +export async function getInAppWalletUsage( |
| 80 | + params: AnalyticsQueryParams, |
| 81 | +): Promise<InAppWalletStats[]> { |
| 82 | + const searchParams = buildSearchParams(params); |
| 83 | + const res = await fetchAnalytics( |
| 84 | + `v1/wallets/in-app?${searchParams.toString()}`, |
| 85 | + { |
| 86 | + method: "GET", |
| 87 | + headers: { "Content-Type": "application/json" }, |
| 88 | + }, |
| 89 | + ); |
| 90 | + |
| 91 | + if (res?.status !== 200) { |
| 92 | + console.error("Failed to fetch in-app wallet usage"); |
| 93 | + return []; |
| 94 | + } |
| 95 | + |
| 96 | + const json = await res.json(); |
| 97 | + return json.data as InAppWalletStats[]; |
| 98 | +} |
| 99 | + |
| 100 | +export async function getUserOpUsage( |
| 101 | + params: AnalyticsQueryParams, |
| 102 | +): Promise<UserOpStats[]> { |
| 103 | + const searchParams = buildSearchParams(params); |
| 104 | + const res = await fetchAnalytics(`v1/user-ops?${searchParams.toString()}`, { |
| 105 | + method: "GET", |
| 106 | + headers: { "Content-Type": "application/json" }, |
| 107 | + }); |
| 108 | + |
| 109 | + if (res?.status !== 200) { |
| 110 | + console.error("Failed to fetch user ops usage"); |
| 111 | + return []; |
| 112 | + } |
| 113 | + |
| 114 | + const json = await res.json(); |
| 115 | + return json.data as UserOpStats[]; |
| 116 | +} |
| 117 | + |
| 118 | +export async function getWalletUsers( |
| 119 | + params: AnalyticsQueryParams, |
| 120 | +): Promise<WalletUserStats[]> { |
| 121 | + const searchParams = buildSearchParams(params); |
| 122 | + const res = await fetchAnalytics( |
| 123 | + `v1/wallets/users?${searchParams.toString()}`, |
| 124 | + { |
| 125 | + method: "GET", |
| 126 | + headers: { "Content-Type": "application/json" }, |
| 127 | + }, |
| 128 | + ); |
| 129 | + |
| 130 | + if (res?.status !== 200) { |
| 131 | + console.error("Failed to fetch wallet user stats"); |
| 132 | + return []; |
| 133 | + } |
| 134 | + |
| 135 | + const json = await res.json(); |
| 136 | + return json.data as WalletUserStats[]; |
| 137 | +} |
0 commit comments