-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: fetch teleport transactions from subgraphs (#1677)
- Loading branch information
1 parent
4734c15
commit 05e2b2e
Showing
14 changed files
with
939 additions
and
17 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
112 changes: 112 additions & 0 deletions
112
packages/arb-token-bridge-ui/src/pages/api/teleports/erc20.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,112 @@ | ||
import { NextApiRequest, NextApiResponse } from 'next' | ||
import { gql } from '@apollo/client' | ||
|
||
import { | ||
getSourceFromSubgraphClient, | ||
getTeleporterSubgraphClient | ||
} from '../../../api-utils/ServerSubgraphUtils' | ||
import { FetchErc20TeleportsFromSubgraphResult } from '../../../util/teleports/fetchErc20TeleportsFromSubgraph' | ||
|
||
// Extending the standard NextJs request with Deposit-params | ||
type NextApiRequestWithErc20TeleportParams = NextApiRequest & { | ||
query: { | ||
sender?: string | ||
l1ChainId: string | ||
page?: string | ||
pageSize?: string | ||
} | ||
} | ||
|
||
type Erc20TeleportResponse = { | ||
meta?: { source: string | null } | ||
data: FetchErc20TeleportsFromSubgraphResult[] | ||
message?: string // in case of any error | ||
} | ||
|
||
export default async function handler( | ||
req: NextApiRequestWithErc20TeleportParams, | ||
res: NextApiResponse<Erc20TeleportResponse> | ||
) { | ||
try { | ||
const { sender, l1ChainId, page = '0', pageSize = '10' } = req.query | ||
|
||
// validate method | ||
if (req.method !== 'GET') { | ||
res | ||
.status(400) | ||
.send({ message: `invalid_method: ${req.method}`, data: [] }) | ||
return | ||
} | ||
|
||
// validate the request parameters | ||
const errorMessage = [] | ||
if (!l1ChainId) errorMessage.push('<l1ChainId> is required') | ||
if (!sender) errorMessage.push('<sender> is required') | ||
|
||
if (errorMessage.length) { | ||
res.status(400).json({ | ||
message: `incomplete request: ${errorMessage.join(', ')}`, | ||
data: [] | ||
}) | ||
return | ||
} | ||
|
||
// if invalid pageSize, send empty data instead of error | ||
if (isNaN(Number(pageSize)) || Number(pageSize) === 0) { | ||
res.status(200).json({ | ||
data: [] | ||
}) | ||
return | ||
} | ||
|
||
let subgraphClient | ||
try { | ||
subgraphClient = getTeleporterSubgraphClient(Number(l1ChainId)) | ||
} catch (error: any) { | ||
// catch attempt to query unsupported networks and throw a 400 | ||
res.status(400).json({ | ||
message: error?.message ?? 'Something went wrong', | ||
data: [] | ||
}) | ||
return | ||
} | ||
|
||
const subgraphResult = await subgraphClient.query({ | ||
query: gql(`{ | ||
teleporteds( | ||
where: { | ||
sender: "${sender}" | ||
} | ||
first: ${Number(pageSize)} | ||
skip: ${Number(page) * Number(pageSize)} | ||
orderBy: timestamp | ||
orderDirection: desc | ||
) { | ||
id | ||
sender | ||
l1Token | ||
l3FeeTokenL1Addr | ||
l1l2Router | ||
l2l3RouterOrInbox | ||
to | ||
amount | ||
transactionHash | ||
timestamp | ||
} | ||
}`) | ||
}) | ||
|
||
const transactions: FetchErc20TeleportsFromSubgraphResult[] = | ||
subgraphResult.data.teleporteds | ||
|
||
res.status(200).json({ | ||
meta: { source: getSourceFromSubgraphClient(subgraphClient) }, | ||
data: transactions | ||
}) | ||
} catch (error: any) { | ||
res.status(500).json({ | ||
message: error?.message ?? 'Something went wrong', | ||
data: [] | ||
}) | ||
} | ||
} |
Oops, something went wrong.