-
Notifications
You must be signed in to change notification settings - Fork 29
feat(trading): use suggested slippage from BFF #544
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
de9ccba
feat(trading): use suggested slippage from BFF
shoom3301 45bfe7f
chore: fix test
shoom3301 5d7b77b
Merge branch 'main' of https://github.com/cowprotocol/cow-sdk into fe…
shoom3301 acdc74c
fix: do not use slippage suggest API for fast quotes
shoom3301 83ff5da
chore: fix tests
shoom3301 f0b2c3c
chore: fix flaky test
shoom3301 8fd6725
ci: update inter-package dependencies
shoom3301 c0cdeb3
ci: update inter-package dependencies
shoom3301 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,214 @@ | ||
| import { CoWBFFClient } from './cowBffClient' | ||
|
|
||
| // Mock fetch globally | ||
| const mockFetch = jest.fn() | ||
| global.fetch = mockFetch | ||
|
|
||
| describe('CoWBFFClient', () => { | ||
| beforeEach(() => { | ||
| mockFetch.mockClear() | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| jest.restoreAllMocks() | ||
| }) | ||
|
|
||
| describe('constructor', () => { | ||
| it('should use prod base URL when env is prod', () => { | ||
| const client = new CoWBFFClient('prod') | ||
| expect((client as any).baseUrl).toBe('https://bff.cow.fi') | ||
| }) | ||
|
|
||
| it('should use barn base URL when env is staging', () => { | ||
| const client = new CoWBFFClient('staging') | ||
| expect((client as any).baseUrl).toBe('https://bff.barn.cow.fi') | ||
| }) | ||
|
|
||
| it('should default to prod when no env provided', () => { | ||
| const client = new CoWBFFClient() | ||
| expect((client as any).baseUrl).toBe('https://bff.cow.fi') | ||
| }) | ||
| }) | ||
|
|
||
| describe('getSlippageTolerance', () => { | ||
| const sellToken = '0x6b175474e89094c44da98b954eedeac495271d0f' | ||
| const buyToken = '0x2260fac5e5542a773aa44fbcfedf7c193bc2c599' | ||
| const chainId = 1 | ||
|
|
||
| it('should return slippage tolerance on successful API response', async () => { | ||
| const client = new CoWBFFClient('prod') | ||
| const mockResponse = { slippageBps: 150 } | ||
| mockFetch.mockResolvedValue({ | ||
| ok: true, | ||
| json: () => Promise.resolve(mockResponse), | ||
| }) | ||
|
|
||
| const result = await client.getSlippageTolerance({ sellToken, buyToken, chainId }) | ||
|
|
||
| expect(mockFetch).toHaveBeenCalledWith( | ||
| `https://bff.cow.fi/${chainId}/markets/${sellToken}-${buyToken}/slippageTolerance`, | ||
| { | ||
| method: 'GET', | ||
| headers: { | ||
| 'Accept': 'application/json', | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| signal: expect.any(AbortSignal), | ||
| } | ||
| ) | ||
| expect(result).toEqual({ slippageBps: 150 }) | ||
| }) | ||
|
|
||
| it('should use staging URL when env is staging', async () => { | ||
| const client = new CoWBFFClient('staging') | ||
| const mockResponse = { slippageBps: 200 } | ||
| mockFetch.mockResolvedValue({ | ||
| ok: true, | ||
| json: () => Promise.resolve(mockResponse), | ||
| }) | ||
|
|
||
| await client.getSlippageTolerance({ sellToken, buyToken, chainId }) | ||
|
|
||
| expect(mockFetch).toHaveBeenCalledWith( | ||
| `https://bff.barn.cow.fi/${chainId}/markets/${sellToken}-${buyToken}/slippageTolerance`, | ||
| expect.any(Object) | ||
| ) | ||
| }) | ||
|
|
||
| it('should return null on HTTP error response', async () => { | ||
| const client = new CoWBFFClient() | ||
| mockFetch.mockResolvedValue({ | ||
| ok: false, | ||
| status: 404, | ||
| statusText: 'Not Found', | ||
| }) | ||
|
|
||
| const result = await client.getSlippageTolerance({ sellToken, buyToken, chainId }) | ||
|
|
||
| expect(result).toBeNull() | ||
| }) | ||
|
|
||
| it('should return null on invalid response structure', async () => { | ||
| const client = new CoWBFFClient() | ||
| const mockResponse = { invalidField: 'value' } | ||
| mockFetch.mockResolvedValue({ | ||
| ok: true, | ||
| json: () => Promise.resolve(mockResponse), | ||
| }) | ||
|
|
||
| const result = await client.getSlippageTolerance({ sellToken, buyToken, chainId }) | ||
|
|
||
| expect(result).toBeNull() | ||
| }) | ||
|
|
||
| it('should return null when slippageBps is not a number', async () => { | ||
| const client = new CoWBFFClient() | ||
| const mockResponse = { slippageBps: 'invalid' } | ||
| mockFetch.mockResolvedValue({ | ||
| ok: true, | ||
| json: () => Promise.resolve(mockResponse), | ||
| }) | ||
|
|
||
| const result = await client.getSlippageTolerance({ sellToken, buyToken, chainId }) | ||
|
|
||
| expect(result).toBeNull() | ||
| }) | ||
|
|
||
| it('should return null on network error', async () => { | ||
| const client = new CoWBFFClient() | ||
| mockFetch.mockRejectedValue(new Error('Network error')) | ||
|
|
||
| const result = await client.getSlippageTolerance({ sellToken, buyToken, chainId }) | ||
|
|
||
| expect(result).toBeNull() | ||
| }) | ||
|
|
||
| it('should return null when response is null', async () => { | ||
| const client = new CoWBFFClient() | ||
| mockFetch.mockResolvedValue({ | ||
| ok: true, | ||
| json: () => Promise.resolve(null), | ||
| }) | ||
|
|
||
| const result = await client.getSlippageTolerance({ sellToken, buyToken, chainId }) | ||
|
|
||
| expect(result).toBeNull() | ||
| }) | ||
|
|
||
| it('should construct correct URL for different chain IDs', async () => { | ||
| const client = new CoWBFFClient() | ||
| const mockResponse = { slippageBps: 100 } | ||
| mockFetch.mockResolvedValue({ | ||
| ok: true, | ||
| json: () => Promise.resolve(mockResponse), | ||
| }) | ||
|
|
||
| await client.getSlippageTolerance({ sellToken, buyToken, chainId: 137 }) | ||
|
|
||
| expect(mockFetch).toHaveBeenCalledWith( | ||
| `https://bff.cow.fi/137/markets/${sellToken}-${buyToken}/slippageTolerance`, | ||
| expect.any(Object) | ||
| ) | ||
| }) | ||
|
|
||
| it('should apply timeout to fetch request', async () => { | ||
| const client = new CoWBFFClient() | ||
|
|
||
| // Mock fetch to simulate timeout by throwing AbortError | ||
| mockFetch.mockRejectedValue(new Error('The operation was aborted')) | ||
|
|
||
| const customTimeoutMs = 100 | ||
| const result = await client.getSlippageTolerance({ | ||
| sellToken, | ||
| buyToken, | ||
| chainId, | ||
| timeoutMs: customTimeoutMs, | ||
| }) | ||
|
|
||
| expect(result).toBeNull() | ||
| }) | ||
|
|
||
| it('should use default timeout of 2000ms when not specified', async () => { | ||
| const client = new CoWBFFClient() | ||
| const mockResponse = { slippageBps: 150 } | ||
| mockFetch.mockResolvedValue({ | ||
| ok: true, | ||
| json: () => Promise.resolve(mockResponse), | ||
| }) | ||
|
|
||
| await client.getSlippageTolerance({ sellToken, buyToken, chainId }) | ||
|
|
||
| // Verify the call was made with signal (indicates timeout setup) | ||
| expect(mockFetch).toHaveBeenCalledWith( | ||
| expect.any(String), | ||
| expect.objectContaining({ | ||
| signal: expect.any(AbortSignal), | ||
| }) | ||
| ) | ||
| }) | ||
|
|
||
| it('should use custom timeout when specified', async () => { | ||
| const client = new CoWBFFClient() | ||
| const mockResponse = { slippageBps: 150 } | ||
| mockFetch.mockResolvedValue({ | ||
| ok: true, | ||
| json: () => Promise.resolve(mockResponse), | ||
| }) | ||
|
|
||
| await client.getSlippageTolerance({ | ||
| sellToken, | ||
| buyToken, | ||
| chainId, | ||
| timeoutMs: 5000, | ||
| }) | ||
|
|
||
| // Verify the call was made with signal (indicates timeout setup) | ||
| expect(mockFetch).toHaveBeenCalledWith( | ||
| expect.any(String), | ||
| expect.objectContaining({ | ||
| signal: expect.any(AbortSignal), | ||
| }) | ||
| ) | ||
| }) | ||
| }) | ||
| }) |
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,78 @@ | ||
| import { log } from '@cowprotocol/sdk-common' | ||
| import { SupportedChainId, CowEnv } from '@cowprotocol/sdk-config' | ||
| import { BFF_ENDPOINTS } from './consts' | ||
|
|
||
| const DEFAULT_TIMEOUT = 2000 // 2 sec | ||
|
|
||
| export interface SlippageToleranceResponse { | ||
| slippageBps: number | ||
| } | ||
|
|
||
| export interface SlippageToleranceRequest { | ||
| chainId: SupportedChainId | ||
| sellToken: string | ||
| buyToken: string | ||
| timeoutMs?: number | ||
| } | ||
|
|
||
| export class CoWBFFClient { | ||
| private readonly baseUrl: string | ||
|
|
||
| constructor(env: CowEnv = 'prod') { | ||
| this.baseUrl = BFF_ENDPOINTS[env] | ||
| } | ||
|
|
||
| /** | ||
| * Fetches slippage tolerance from the API for a given token pair | ||
| * @param sellToken - Address of the token to sell (e.g., '0x6b175474e89094c44da98b954eedeac495271d0f') | ||
| * @param buyToken - Address of the token to buy (e.g., '0x2260fac5e5542a773aa44fbcfedf7c193bc2c599') | ||
| * @param chainId - Chain ID for the API request | ||
| * @param timeoutMs - Request timeout in milliseconds (default: 2000ms) | ||
| * @returns Promise<SlippageToleranceResponse | null> - Returns null if API call fails | ||
| */ | ||
| async getSlippageTolerance({ | ||
| sellToken, | ||
| buyToken, | ||
| chainId, | ||
| timeoutMs = DEFAULT_TIMEOUT, | ||
| }: SlippageToleranceRequest): Promise<SlippageToleranceResponse | null> { | ||
| try { | ||
| const url = `${this.baseUrl}/${chainId}/markets/${sellToken}-${buyToken}/slippageTolerance` | ||
|
|
||
| log(`Fetching slippage tolerance from API: ${url} (timeout: ${timeoutMs}ms)`) | ||
|
|
||
| const controller = new AbortController() | ||
| const timeoutId = setTimeout(() => controller.abort(), timeoutMs) | ||
|
|
||
| const response = await fetch(url, { | ||
| method: 'GET', | ||
| headers: { | ||
| Accept: 'application/json', | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| signal: controller.signal, | ||
| }) | ||
|
|
||
| clearTimeout(timeoutId) | ||
|
|
||
| if (!response.ok) { | ||
| log(`Slippage tolerance API error: ${response.status} ${response.statusText}`) | ||
| return null | ||
| } | ||
|
|
||
| const data = await response.json() | ||
|
|
||
| // Validate response structure | ||
| if (typeof data !== 'object' || data === null || typeof data.slippageBps !== 'number') { | ||
| log('Invalid slippage tolerance API response structure') | ||
| return null | ||
| } | ||
|
|
||
| log(`Retrieved slippage tolerance from API: ${data.slippageBps} BPS`) | ||
| return { slippageBps: data.slippageBps } | ||
| } catch (error) { | ||
| log(`Failed to fetch slippage tolerance from API: ${error instanceof Error ? error.message : 'Unknown error'}`) | ||
| return null | ||
| } | ||
| } | ||
| } | ||
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.