Skip to content

Commit 4053c56

Browse files
committed
feat: allow to define the production endpoint from thegraph
1 parent c414a9a commit 4053c56

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
CURATE_GRAPH_URL=https://api.studio.thegraph.com/query/61738/legacy-curate-gnosis/version/latest
2+
GRAPH_API_KEY=
23
KLEROS_TOKENS_REGISTRY_ADDRESS=0xee1502e29795ef6c2d60f8d7120596abe3bad990
34

45
ENS_CONTRACT=0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e

src/utils/get-tokens.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
1+
import { TokenInfo } from '@uniswap/token-lists'
12
import { ethers } from 'ethers'
23
import fetch from 'node-fetch'
3-
import { TokenInfo } from '@uniswap/token-lists'
44

55
export interface Prop {
66
label: string
77
value: string
88
}
99

1010
export interface Item {
11-
metadata : {
11+
metadata: {
1212
props: Prop[]
1313
} | null
1414
id: string
1515
}
1616

1717
const fetchTokensBatch = async (id: string): Promise<Item[]> => {
18+
if (!process.env.CURATE_GRAPH_URL) {
19+
throw new Error('CURATE_GRAPH_URL environment variable is not set')
20+
}
1821
const subgraphQuery = {
1922
query: `
2023
{
@@ -34,16 +37,16 @@ const fetchTokensBatch = async (id: string): Promise<Item[]> => {
3437
}
3538
`,
3639
}
37-
const response = await fetch(
38-
'https://api.studio.thegraph.com/query/61738/legacy-curate-gnosis/version/latest',
39-
{
40-
method: 'POST',
41-
body: JSON.stringify(subgraphQuery),
42-
headers: {
43-
'Content-Type': 'application/json',
44-
},
40+
const response = await fetch(process.env.CURATE_GRAPH_URL, {
41+
method: 'POST',
42+
body: JSON.stringify(subgraphQuery),
43+
headers: {
44+
'Content-Type': 'application/json',
45+
...(process.env.GRAPH_API_KEY
46+
? { Authorization: `Bearer ${process.env.GRAPH_API_KEY}` }
47+
: {}),
4548
},
46-
)
49+
})
4750

4851
const { data } = await response.json()
4952
const tags: Item[] = data.litems
@@ -86,19 +89,22 @@ export default async function getTokens(): Promise<TokenInfo[]> {
8689

8790
const tokens: Map<string, TokenInfo> = new Map()
8891
for (const token of tokensFromSubgraph) {
89-
const caipAddress = token?.metadata?.props.find((p) => p.label === 'Address')
92+
const caipAddress = token?.metadata?.props.find(
93+
(p) => p.label === 'Address',
94+
)?.value as string
95+
const name = token?.metadata?.props.find((p) => p.label === 'Name')
9096
?.value as string
91-
const name = token?.metadata?.props.find((p) => p.label === 'Name')?.value as string
9297
const symbol = token?.metadata?.props.find((p) => p.label === 'Symbol')
9398
?.value as string
94-
const logo = token?.metadata?.props.find((p) => p.label === 'Logo')?.value as string
99+
const logo = token?.metadata?.props.find((p) => p.label === 'Logo')
100+
?.value as string
95101
const decimals = token?.metadata?.props.find((p) => p.label === 'Decimals')
96102
?.value as string
97103

98104
if (!caipAddress || !name || !symbol || !decimals) {
99105
continue
100106
}
101-
107+
102108
const [namespace] = caipAddress.split(':')
103109
if (namespace !== 'eip155') {
104110
nonEvmTokens.push(caipAddress)

0 commit comments

Comments
 (0)