Skip to content

Commit dbb99e3

Browse files
authored
Add TokenWidget (#849)
* Token Widget
1 parent 9e10075 commit dbb99e3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+8077
-179
lines changed

.changeset/sixty-rules-hide.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@relayprotocol/relay-kit-ui': major
3+
---
4+
5+
Add TokenWidget

demo/components/providers/RelayKitProviderWrapper.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {
2-
createClient,
32
LogLevel,
43
MAINNET_RELAY_WS,
54
RelayChain
@@ -41,7 +40,6 @@ export const RelayKitProviderWrapper: FC<{
4140
source: 'relay-demo',
4241
logLevel: LogLevel.Verbose,
4342
duneConfig: {
44-
apiKey: process.env.NEXT_PUBLIC_DUNE_TOKEN,
4543
apiBaseUrl: process.env.NEXT_PUBLIC_DUNE_API_URL
4644
},
4745
chains: dynamicChains,

demo/pages/api/dune/[...path].ts

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import type { NextApiRequest, NextApiResponse } from 'next'
2+
3+
const API_MAPPINGS = {
4+
'api/echo/v1/balances/evm': 'v1/evm/balances',
5+
'api/echo/beta2/balances/svm': 'beta/svm/balances'
6+
}
7+
8+
export default async function handler(
9+
req: NextApiRequest,
10+
res: NextApiResponse
11+
): Promise<void> {
12+
const DUNE_API_KEY = process.env.DUNE_API_KEY
13+
const allowedDomains = process.env.ALLOWED_API_DOMAINS
14+
? process.env.ALLOWED_API_DOMAINS.split(',')
15+
: []
16+
const cache = 60
17+
let origin = req.headers.origin || req.headers.referer || ''
18+
19+
try {
20+
origin = new URL(origin).origin
21+
} catch (e) {}
22+
23+
if (allowedDomains.includes(origin)) {
24+
res.setHeader('Access-Control-Allow-Origin', origin)
25+
res.setHeader('Vary', 'Origin')
26+
res.setHeader('Access-Control-Allow-Methods', 'GET,OPTIONS')
27+
res.setHeader(
28+
'Access-Control-Allow-Headers',
29+
'Content-Type,Authorization,x-sim-api-key'
30+
)
31+
}
32+
33+
if (req.method === 'OPTIONS') {
34+
res.status(200).end()
35+
return
36+
}
37+
38+
const path = req.url?.replace('/api/dune', '') || ''
39+
const newPath = Object.entries(API_MAPPINGS).reduce(
40+
(acc, [old, new_]) => acc.replace(old, new_),
41+
path
42+
)
43+
44+
let modifiedPath = newPath
45+
const chainIdsMatch = modifiedPath.match(/chain_ids=([^&]*)/)
46+
if (chainIdsMatch && chainIdsMatch[1].includes('mainnet')) {
47+
if (!chainIdsMatch[1].includes('747474')) {
48+
const newChainIds = chainIdsMatch[1] + ',747474'
49+
modifiedPath = modifiedPath.replace(
50+
/chain_ids=([^&]*)/,
51+
`chain_ids=${newChainIds}`
52+
)
53+
}
54+
}
55+
56+
const url = `https://api.sim.dune.com${modifiedPath}`
57+
58+
if (!DUNE_API_KEY) {
59+
res.status(500).json({
60+
error: 'Server configuration error'
61+
})
62+
return
63+
}
64+
65+
if (allowedDomains.length > 0 && !allowedDomains.includes(origin)) {
66+
res.status(403).json({ message: 'Forbidden: Origin not allowed' })
67+
return
68+
}
69+
70+
const duneResponse = await fetch(url, {
71+
headers: {
72+
'X-Sim-Api-Key': DUNE_API_KEY
73+
} as HeadersInit
74+
})
75+
76+
const response = await duneResponse.json()
77+
78+
if (!duneResponse.ok) {
79+
res.status(duneResponse.status).json(response)
80+
return
81+
}
82+
83+
// Set response cache
84+
res.setHeader('Cache-Control', `public, s-maxage=${cache}`)
85+
res.setHeader('CDN-Cache-Control', `public, s-maxage=${cache}`)
86+
res.setHeader('Vercel-CDN-Cache-Control', `public, s-maxage=${cache}`)
87+
88+
res.json(response)
89+
}

demo/pages/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const Index: NextPage = () => {
2121
</h2>
2222
<nav style={{ display: 'flex', gap: 15 }}>
2323
<Link href="/ui/swap">SwapWidget</Link>
24+
<Link href="/ui/token">TokenWidget</Link>
2425
<Link href="/ui/chain">ChainWidget</Link>
2526
<Link href="/ui/onramp">OnrampWidget</Link>
2627
<Link href="/ui/depositAddresses">Deposit Addresses</Link>

0 commit comments

Comments
 (0)