-
Notifications
You must be signed in to change notification settings - Fork 374
Add UniswapApi plugin #986
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yuvanvk
wants to merge
13
commits into
corsairdev:main
Choose a base branch
from
yuvanvk:feat/uniswapapi
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
111f70b
feat(uniswapapi): add UniswapApi plugin
yuvanvk d434e7f
fix(uniswapapi): parse errorCode/detail from API error body
yuvanvk 7dd771d
fix: webhook and oauth not supported
yuvanvk 985cfac
fix(uniswapapi): preserve retry metadata and validate outputs
yuvanvk 22b991d
fix(uniswapapi): enforce output schema validation
yuvanvk 46b0132
fix(uniswapapi): align endpoints with live Uniswap Trading API contracts
Mayank-saraswal 6b075ef
fix(uniswapapi): auth with x-api-key only
ambikeesshh e2cc89c
fix(uniswapapi): throw if the api key is missing
ambikeesshh 0f4625e
fix(uniswapapi): don't treat "429" in a message as rate-limited
ambikeesshh e23162d
fix(uniswapapi): log swap.create input
ambikeesshh 7a397b3
chore(uniswapapi): call it Uniswap
ambikeesshh 66b8f6b
fix(uniswapapi): use live swap status values
ambikeesshh 89782a4
test(uniswapapi): cover auth, missing key, and status enum
ambikeesshh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,84 @@ | ||
| import type { ApiRequestOptions, OpenAPIConfig } from 'corsair/http'; | ||
| import { ApiError, request } from 'corsair/http'; | ||
|
|
||
| export class UniswapApiAPIError extends Error { | ||
| constructor( | ||
| message: string, | ||
| public readonly code?: string, | ||
| ) { | ||
| super(message); | ||
| this.name = 'UniswapApiAPIError'; | ||
| } | ||
| } | ||
|
|
||
| const UNISWAPAPI_API_BASE = 'https://trade-api.gateway.uniswap.org'; | ||
|
|
||
| export async function makeUniswapApiRequest<T>( | ||
| endpoint: string, | ||
| apiKey: string, | ||
| options: { | ||
| method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH'; | ||
| body?: Record<string, unknown>; | ||
| query?: Record<string, string | number | boolean | undefined>; | ||
| } = {}, | ||
| ): Promise<T> { | ||
| const { method = 'GET', body, query } = options; | ||
|
|
||
| const config: OpenAPIConfig = { | ||
| BASE: UNISWAPAPI_API_BASE, | ||
| VERSION: '1.0.0', | ||
| WITH_CREDENTIALS: false, | ||
| CREDENTIALS: 'omit', | ||
| TOKEN: apiKey, | ||
| HEADERS: { | ||
| 'Content-Type': 'application/json', | ||
| 'x-api-key': apiKey, | ||
| 'x-permit2-disabled': 'false', | ||
| }, | ||
| }; | ||
|
|
||
| const requestOptions: ApiRequestOptions = { | ||
| method, | ||
| url: endpoint, | ||
| body: | ||
| method === 'POST' || method === 'PUT' || method === 'PATCH' | ||
| ? body | ||
| : undefined, | ||
| mediaType: 'application/json; charset=utf-8', | ||
| query: method === 'GET' ? query : undefined, | ||
| }; | ||
|
|
||
| try { | ||
| return await request<T>(config, requestOptions); | ||
| } catch (error) { | ||
| if (error instanceof ApiError) { | ||
| // UniswapApi error responses use { errorCode, detail } instead of the | ||
| // generic { code, message } shape — extract those fields explicitly, | ||
| // falling back to error.message / error.status if the body doesn't match. | ||
| const body = error.body; | ||
|
|
||
| const message = | ||
| typeof body === 'object' && | ||
| body !== null && | ||
| 'detail' in body && | ||
| typeof body.detail === 'string' | ||
| ? body.detail | ||
| : error.message; | ||
|
|
||
| const code = | ||
| typeof body === 'object' && | ||
| body !== null && | ||
| 'errorCode' in body && | ||
| typeof body.errorCode === 'string' | ||
| ? body.errorCode | ||
| : error.status?.toString(); | ||
| throw new UniswapApiAPIError(message, code); | ||
|
greptile-apps[bot] marked this conversation as resolved.
Outdated
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| if (error instanceof Error) { | ||
| throw new UniswapApiAPIError(error.message); | ||
| } | ||
|
|
||
| throw new UniswapApiAPIError('Unknown error'); | ||
| } | ||
| } | ||
This file contains hidden or 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,29 @@ | ||
| import { logEventFromContext } from 'corsair/core'; | ||
| import type { UniswapApiEndpoints } from '..'; | ||
| import { makeUniswapApiRequest } from '../client'; | ||
| import type { UniswapApiEndpointOutputs } from './types'; | ||
|
|
||
| export const check: UniswapApiEndpoints['approvalCheck'] = async ( | ||
| ctx, | ||
| input, | ||
| ) => { | ||
| const response = await makeUniswapApiRequest< | ||
| UniswapApiEndpointOutputs['approvalCheck'] | ||
| >('/v1/check_approval', ctx.key, { | ||
| method: 'POST', | ||
| body: { | ||
| token: input.token, | ||
| amount: input.amount, | ||
| walletAddress: input.walletAddress, | ||
| chainId: input.chainId, | ||
| }, | ||
| }); | ||
|
|
||
| await logEventFromContext( | ||
| ctx, | ||
| 'uniswapapi.approval.check', | ||
| { ...input }, | ||
| 'completed', | ||
| ); | ||
| return response; | ||
| }; |
This file contains hidden or 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,27 @@ | ||
| import { logEventFromContext } from 'corsair/core'; | ||
| import type { UniswapApiEndpoints } from '..'; | ||
| import { makeUniswapApiRequest } from '../client'; | ||
| import type { UniswapApiEndpointOutputs } from './types'; | ||
|
|
||
| export const check: UniswapApiEndpoints['delegationCheck'] = async ( | ||
| ctx, | ||
| input, | ||
| ) => { | ||
| const response = await makeUniswapApiRequest< | ||
| UniswapApiEndpointOutputs['delegationCheck'] | ||
| >('/v1/check_delegation', ctx.key, { | ||
| method: 'POST', | ||
| body: { | ||
| walletAddress: input.walletAddress, | ||
| chainIds: input.chainIds, | ||
| }, | ||
| }); | ||
|
|
||
| await logEventFromContext( | ||
| ctx, | ||
| 'uniswapapi.delegation.check', | ||
| { ...input }, | ||
| 'completed', | ||
| ); | ||
| return response; | ||
| }; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.