Skip to content

Commit

Permalink
pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
W3stside committed Jul 13, 2022
1 parent 8f80c2a commit 2d7fa78
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 30 deletions.
35 changes: 13 additions & 22 deletions src/api/0x/0x.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import fetchMock, { enableFetchMocks } from 'jest-fetch-mock'
import { OrderKind } from '@cowprotocol/contracts'
import { PriceQuoteParams } from '../cow/types'
import { SupportedChainId } from '../../constants/chains'
import { ZeroXSdk } from '../../0xSdk'
import CowSdk from '../../CowSdk'
import ZeroXError from './error'

enableFetchMocks()

const chainId = 1 as SupportedChainId //Rinkeby
const chainId = SupportedChainId.MAINNET

const zeroXSdk = new ZeroXSdk(chainId, {}, { loglevel: 'debug' })
const cowSdk = new CowSdk(chainId, {}, { loglevel: 'debug' })

const HTTP_STATUS_OK = 200

Expand Down Expand Up @@ -54,14 +54,16 @@ afterEach(() => {
jest.restoreAllMocks()
})

const query = {
baseToken: '0x6810e776880c02933d47db1b9fc05908e5386b96',
quoteToken: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
amount: '1234567890',
kind: OrderKind.BUY,
} as PriceQuoteParams

test('Valid: Get Price Quote', async () => {
fetchMock.mockResponseOnce(JSON.stringify(PRICE_QUOTE_RESPONSE), { status: HTTP_STATUS_OK })
const price = await zeroXSdk.api.getQuote({
baseToken: '0x6810e776880c02933d47db1b9fc05908e5386b96',
quoteToken: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
amount: '1234567890',
kind: OrderKind.BUY,
} as PriceQuoteParams)
const price = await cowSdk.zeroXApi.getQuote(query)
expect(fetchMock).toHaveBeenCalledTimes(1)
expect(fetchMock).toHaveBeenCalledWith(
'https://api.0x.org/swap/v1/price?sellToken=0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2&buyToken=0x6810e776880c02933d47db1b9fc05908e5386b96&buyAmount=1234567890&excludedSources=&affiliateAddress=0x9008D19f58AAbD9eD0D60971565AA8510560ab41',
Expand All @@ -70,6 +72,7 @@ test('Valid: Get Price Quote', async () => {
expect(price?.value).toEqual(PRICE_QUOTE_RESPONSE.value)
expect(price?.buyTokenAddress).toEqual(PRICE_QUOTE_RESPONSE.buyTokenAddress)
})

test('Invalid: Get Price Quote', async () => {
fetchMock.mockResponseOnce(
JSON.stringify({
Expand All @@ -85,18 +88,6 @@ test('Invalid: Get Price Quote', async () => {
}),
{ status: 400 }
)
try {
await zeroXSdk.api.getQuote({
baseToken: '0x6810e776880c02933d47db1b9fc05908e5386b96',
quoteToken: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
amount: '0',
kind: OrderKind.BUY,
} as PriceQuoteParams)
} catch (e) {
const error = e as ZeroXError

expect(error.message).toEqual('Validation Failed')
expect(error.description).toEqual('INSUFFICIENT_ASSET_LIQUIDITY')
expect(fetchMock).toHaveBeenCalledTimes(1)
}
await expect(cowSdk.zeroXApi.getQuote(query)).rejects.toThrowError(ZeroXError)
})
19 changes: 11 additions & 8 deletions src/api/0x/error/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,18 @@ export default class ZeroXError extends CowError {
log.error(logPrefix, 'Unknown validation reason for bad price request', errorResponse)
return errorResponse.reason
} else {
throw 'Error response body properties "reason" and "validationErrors" missing.'
throw new Error('Error response body properties "reason" and "validationErrors" missing.')
}
} catch (error) {
log.error(logPrefix, 'Error handling a 4xx error. Likely a problem deserialising the JSON response')
return 'Price fetch failed. This may be due to a server or network connectivity issue. Please try again later.'
const isError = error instanceof Error
log.error(
logPrefix,
'Error handling a 4xx error. Likely a problem deserialising the JSON response.',
isError && error?.message
)
return isError
? error?.message
: 'Price fetch failed. This may be due to a server or network connectivity issue. Please try again later.'
}
}

Expand All @@ -57,11 +64,7 @@ export default class ZeroXError extends CowError {
return 'Server Error - Too many open connections'

default:
log.error(
logPrefix,
'[ZeroXError::getErrorFromStatusCode] Error fetching quote, status code:',
response.status || 'unknown'
)
log.error(logPrefix, 'Error fetching quote, status code:', response.status || 'unknown')
return 'Error fetching quote'
}
}
Expand Down

0 comments on commit 2d7fa78

Please sign in to comment.