-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Michalstruck dev 1144 add analytics page (#971)
* DEV-1144 split app dashboard to ssr * DEV-1144 split app dashboard to ssr * DEV-1144 further split components for server side rendering, global ranking logic * Makefile DX improvements * DEV-1144 global rank on verified only * DEV-1144 working top stats * Fix chart object merging * DEV-1144 graphs reworked * DEV-1144 responsive statcards * DEV-1144 general logic, calculations, chart and stat styling, simplified data retrieval * DEV-1144 fix types
- Loading branch information
1 parent
b397c05
commit 1aa8826
Showing
23 changed files
with
954 additions
and
473 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
...Portal/Teams/TeamId/Apps/AppId/Transactions/page/server/getAccumulativeTransactionData.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
"use server"; | ||
|
||
import { logger } from "@/lib/logger"; | ||
import { PaymentMetadata } from "@/lib/types"; | ||
import { createSignedFetcher } from "aws-sigv4-fetch"; | ||
|
||
export type GetAccumulativeTransactionDataReturnType = Awaited< | ||
ReturnType<typeof getAccumulativeTransactionData> | ||
>; | ||
|
||
export const getAccumulativeTransactionData = async ( | ||
appId: string, | ||
): Promise<{ | ||
accumulativeTransactions: PaymentMetadata[]; | ||
accumulatedAmountUSD: number; | ||
}> => { | ||
try { | ||
if (!process.env.NEXT_SERVER_INTERNAL_PAYMENTS_ENDPOINT) { | ||
throw new Error("Internal payments endpoint must be set."); | ||
} | ||
|
||
const signedFetch = createSignedFetcher({ | ||
service: "execute-api", | ||
region: process.env.TRANSACTION_BACKEND_REGION, | ||
}); | ||
|
||
let url = `${process.env.NEXT_SERVER_INTERNAL_PAYMENTS_ENDPOINT}/miniapp?miniapp-id=${appId}`; | ||
|
||
const response = await signedFetch(url, { | ||
method: "GET", | ||
headers: { | ||
"User-Agent": "DevPortal/1.0", | ||
"Content-Type": "application/json", | ||
}, | ||
}); | ||
|
||
const data = await response.json(); | ||
if (!response.ok) { | ||
throw new Error( | ||
`Failed to fetch transaction data. Status: ${response.status}. Error: ${data}`, | ||
); | ||
} | ||
|
||
const sortedTransactions = (data?.result?.transactions || []).sort( | ||
(a: PaymentMetadata, b: PaymentMetadata) => | ||
new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime(), | ||
) as PaymentMetadata[]; | ||
|
||
let accumulatedTokenAmount = 0; | ||
const accumulativeTransactions = sortedTransactions.map((transaction) => { | ||
// TODO - floating point issues here? test on real data | ||
accumulatedTokenAmount += Number(transaction.inputTokenAmount); | ||
|
||
return { | ||
...transaction, | ||
inputTokenAmount: String(accumulatedTokenAmount), | ||
}; | ||
}); | ||
|
||
const tokenPriceResponse = (await ( | ||
await fetch( | ||
`${process.env.NEXT_PUBLIC_PRICES_ENDPOINT}?cryptoCurrencies=WLD&fiatCurrencies=USD`, | ||
) | ||
).json()) as { | ||
result: { | ||
prices: { WLD: { USD: { amount: string; decimals: number } } }; | ||
}; | ||
}; | ||
|
||
const accumulatedAmountUSD = | ||
(accumulatedTokenAmount * | ||
Number(tokenPriceResponse.result.prices.WLD.USD.amount)) / | ||
10 ** tokenPriceResponse.result.prices.WLD.USD.decimals; | ||
|
||
return { | ||
accumulativeTransactions, | ||
accumulatedAmountUSD, | ||
}; | ||
} catch (error) { | ||
logger.warn("Error fetching transaction data", { error }); | ||
return { | ||
accumulativeTransactions: [], | ||
accumulatedAmountUSD: 0, | ||
}; | ||
} | ||
}; |
Oops, something went wrong.