Skip to content

Commit

Permalink
add orderId param to getTrades cow api method
Browse files Browse the repository at this point in the history
  • Loading branch information
ramirotw committed Jun 8, 2022
1 parent e3322fa commit 688038b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
16 changes: 16 additions & 0 deletions src/api/cow/cow.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,22 @@ test('Valid: Get last 5 trades for a given trader ', async () => {
expect(trades.length).toEqual(5)
})

test('Valid: Get last 5 trades for a given order id ', async () => {
const TRADES_RESPONSE = Array(5).fill(TRADE_RESPONSE)
fetchMock.mockResponseOnce(JSON.stringify(TRADES_RESPONSE), { status: HTTP_STATUS_OK })
const trades = await cowSdk.cowApi.getTrades({
orderId: TRADE_RESPONSE.orderUid,
limit: 5,
offset: 0,
})
expect(fetchMock).toHaveBeenCalledTimes(1)
expect(fetchMock).toHaveBeenCalledWith(
`https://api.cow.fi/rinkeby/api/v1/trades?orderUid=${TRADE_RESPONSE.orderUid}&limit=5`,
FETCH_RESPONSE_PARAMETERS
)
expect(trades.length).toEqual(5)
})

test('Invalid: Get last 5 trades for an unexisting trader ', async () => {
fetchMock.mockResponseOnce(
JSON.stringify({
Expand Down
9 changes: 6 additions & 3 deletions src/api/cow/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,13 @@ export class CowApi {
}

async getTrades(params: GetTradesParams): Promise<TradeMetaData[]> {
const { owner, limit, offset } = params
const qsParams = objectToQueryString({ owner, limit, offset })
const { owner, orderId, limit, offset } = params
if (owner && orderId) {
throw new CowError('Cannot specify both owner and orderId')
}
const qsParams = objectToQueryString({ owner, orderUid: orderId, limit, offset })
const chainId = await this.context.chainId
log.debug(logPrefix, '[util:operator] Get trades for', chainId, owner, { limit, offset })
log.debug(logPrefix, '[util:operator] Get trades for', chainId, { owner, orderId, limit, offset })
try {
const response = await this.get(`/trades${qsParams}`)

Expand Down
11 changes: 8 additions & 3 deletions src/api/cow/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { GetQuoteResponse, OrderKind } from '@gnosis.pm/gp-v2-contracts'
import { StrictUnion } from 'utilities'
import { SupportedChainId as ChainId } from '../../constants/chains'
import { OrderCancellation, SigningSchemeValue } from '../../utils/sign'

Expand Down Expand Up @@ -69,9 +70,13 @@ export type GetOrdersParams = {
owner: string
} & PaginationParams

export type GetTradesParams = {
owner: string
} & PaginationParams
export type GetTradesParams = StrictUnion<
| {
owner: string
}
| { orderId: string }
> &
PaginationParams

export type ProfileData = {
totalTrades: number
Expand Down
3 changes: 3 additions & 0 deletions src/types/utilities.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type UnionKeys<T> = T extends T ? keyof T : never
type StrictUnionHelper<T, TAll> = T extends any ? T & Partial<Record<Exclude<UnionKeys<TAll>, keyof T>, never>> : never
export type StrictUnion<T> = StrictUnionHelper<T, T>

0 comments on commit 688038b

Please sign in to comment.