2023-08-10 22:29:37 +03:00
|
|
|
/* eslint-disable import/no-unused-modules */
|
|
|
|
import { ImageResponse } from '@vercel/og'
|
|
|
|
import React from 'react'
|
|
|
|
|
2023-09-22 20:09:05 +03:00
|
|
|
import { getColor } from '../../../../src/utils/getColor'
|
2023-08-10 22:29:37 +03:00
|
|
|
import { WATERMARK_URL } from '../../../constants'
|
|
|
|
import getFont from '../../../utils/getFont'
|
|
|
|
import getNetworkLogoUrl from '../../../utils/getNetworkLogoURL'
|
|
|
|
import { getRequest } from '../../../utils/getRequest'
|
|
|
|
import getToken from '../../../utils/getToken'
|
|
|
|
|
|
|
|
export const onRequest: PagesFunction = async ({ params, request }) => {
|
|
|
|
try {
|
|
|
|
const origin = new URL(request.url).origin
|
|
|
|
const { index } = params
|
|
|
|
const networkName = String(index[0])
|
|
|
|
const tokenAddress = String(index[1])
|
|
|
|
|
|
|
|
const cacheUrl = origin + '/tokens/' + networkName + '/' + tokenAddress
|
|
|
|
|
|
|
|
const data = await getRequest(
|
|
|
|
cacheUrl,
|
|
|
|
() => getToken(networkName, tokenAddress, cacheUrl),
|
2023-08-11 01:32:08 +03:00
|
|
|
(data): data is NonNullable<Awaited<ReturnType<typeof getToken>>> => Boolean(data.symbol && data.name)
|
2023-08-10 22:29:37 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
if (!data) {
|
2023-08-14 23:50:49 +03:00
|
|
|
return new Response('Token not found.', { status: 404 })
|
2023-08-10 22:29:37 +03:00
|
|
|
}
|
|
|
|
|
2023-08-17 23:15:17 +03:00
|
|
|
const [fontData, palette] = await Promise.all([getFont(origin), getColor(data.ogImage, true)])
|
2023-08-10 22:29:37 +03:00
|
|
|
|
2023-08-17 23:15:17 +03:00
|
|
|
const networkLogo = getNetworkLogoUrl(networkName.toUpperCase(), origin)
|
2023-08-10 22:29:37 +03:00
|
|
|
|
|
|
|
// Capitalize name such that each word starts with a capital letter
|
|
|
|
let words = data.name.split(' ')
|
|
|
|
words = words.map((word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
|
|
|
|
let name = words.join(' ')
|
|
|
|
name = name.trim()
|
|
|
|
|
|
|
|
return new ImageResponse(
|
|
|
|
(
|
|
|
|
<div
|
|
|
|
style={{
|
|
|
|
backgroundColor: 'black',
|
|
|
|
display: 'flex',
|
|
|
|
width: '1200px',
|
|
|
|
height: '630px',
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<div
|
|
|
|
style={{
|
|
|
|
display: 'flex',
|
2023-08-12 01:32:16 +03:00
|
|
|
backgroundColor: `rgba(${palette[0]}, ${palette[1]}, ${palette[2]})`,
|
2023-08-10 22:29:37 +03:00
|
|
|
alignItems: 'center',
|
|
|
|
height: '100%',
|
|
|
|
padding: '72px',
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<div
|
|
|
|
style={{
|
|
|
|
display: 'flex',
|
|
|
|
flexDirection: 'column',
|
|
|
|
justifyContent: 'space-between',
|
|
|
|
alignItems: 'flex-start',
|
|
|
|
width: '100%',
|
|
|
|
height: '100%',
|
|
|
|
color: 'white',
|
|
|
|
}}
|
|
|
|
>
|
2023-08-11 01:32:08 +03:00
|
|
|
{data.ogImage ? (
|
2023-08-10 22:29:37 +03:00
|
|
|
<img src={data.ogImage} width="144px" style={{ borderRadius: '100%' }}>
|
|
|
|
{networkLogo != '' && (
|
|
|
|
<img
|
|
|
|
src={networkLogo}
|
|
|
|
width="48px"
|
|
|
|
style={{
|
|
|
|
position: 'absolute',
|
|
|
|
right: '2px',
|
|
|
|
bottom: '0px',
|
|
|
|
borderRadius: '100%',
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</img>
|
|
|
|
) : (
|
|
|
|
<div
|
|
|
|
style={{
|
|
|
|
width: '144px',
|
|
|
|
height: '144px',
|
|
|
|
borderRadius: '100%',
|
|
|
|
backgroundColor: 'rgba(255, 255, 255, 0.12)',
|
|
|
|
display: 'flex',
|
|
|
|
justifyContent: 'center',
|
|
|
|
alignItems: 'center',
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<div
|
|
|
|
style={{
|
|
|
|
fontFamily: 'Inter',
|
|
|
|
fontSize: '48px',
|
|
|
|
lineHeight: '58px',
|
|
|
|
color: 'white',
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{data.name.slice(0, 3).toUpperCase()}
|
|
|
|
</div>
|
|
|
|
{networkLogo != '' && (
|
|
|
|
<img
|
|
|
|
src={networkLogo}
|
|
|
|
width="48px"
|
|
|
|
style={{
|
|
|
|
position: 'absolute',
|
|
|
|
right: '2px',
|
|
|
|
bottom: '0px',
|
|
|
|
borderRadius: '100%',
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
<div
|
|
|
|
style={{
|
|
|
|
fontFamily: 'Inter',
|
|
|
|
fontSize: '72px',
|
2023-08-18 00:20:18 +03:00
|
|
|
lineHeight: '72px',
|
2023-08-10 22:29:37 +03:00
|
|
|
marginLeft: '-5px',
|
|
|
|
marginTop: '24px',
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{name}
|
|
|
|
</div>
|
|
|
|
<div
|
|
|
|
style={{
|
|
|
|
display: 'flex',
|
|
|
|
flexDirection: 'row',
|
|
|
|
justifyContent: 'space-between',
|
|
|
|
alignItems: 'flex-end',
|
|
|
|
width: '100%',
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<div
|
|
|
|
style={{
|
|
|
|
fontFamily: 'Inter',
|
|
|
|
fontSize: '168px',
|
|
|
|
lineHeight: '133px',
|
|
|
|
marginLeft: '-13px',
|
2023-08-18 00:20:18 +03:00
|
|
|
overflow: 'hidden',
|
|
|
|
textOverflow: 'ellipsis',
|
|
|
|
whiteSpace: 'nowrap',
|
|
|
|
width: '100%',
|
2023-08-10 22:29:37 +03:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
{data.symbol}
|
|
|
|
</div>
|
|
|
|
<img src={WATERMARK_URL} alt="Uniswap" height="72px" width="324px" />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
),
|
|
|
|
{
|
|
|
|
width: 1200,
|
|
|
|
height: 630,
|
|
|
|
fonts: [
|
|
|
|
{
|
|
|
|
name: 'Inter',
|
|
|
|
data: fontData,
|
|
|
|
style: 'normal',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
) as Response
|
|
|
|
} catch (error: any) {
|
|
|
|
return new Response(error.message || error.toString(), { status: 500 })
|
|
|
|
}
|
|
|
|
}
|