• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

cowprotocol / cow-sdk / 4165011940

pending completion
4165011940

Pull #96

github

GitHub
Merge 0a2c0d626 into cc2748e68
Pull Request #96: SDK refactoring for dogfooding

38 of 56 branches covered (67.86%)

Branch coverage included in aggregate %.

89 of 89 new or added lines in 7 files covered. (100.0%)

112 of 129 relevant lines covered (86.82%)

3.38 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

72.73
/src/api/order-book/api.ts
1
import {
2
  Address,
3
  BaseHttpRequest,
4
  CancelablePromise,
5
  DefaultService,
6
  OpenAPIConfig,
7
  OrderBookClient,
8
  OrderCancellation,
9
  OrderCreation,
10
  OrderPostError,
11
  OrderQuoteRequest,
12
  OrderQuoteResponse,
13
  Trade,
14
  TransactionHash,
15
  UID,
16
} from './generated'
17
import { CowError } from '../../common/cow-error'
18
import { SupportedChainId } from '../../common/chains'
19
import { EnvConfig, PROD_CONFIG, STAGING_CONFIG } from '../../common/configs'
20
import { transformOrder } from './transformOrder'
21
import { EnrichedOrder } from './types'
22
import { ApiRequestOptions } from './generated/core/ApiRequestOptions'
23
import { request as __request } from './generated/core/request'
24

25
class FetchHttpRequest extends BaseHttpRequest {
26
  constructor(config: OpenAPIConfig) {
27
    super(config)
1✔
28
  }
29

30
  /**
31
   * Request method
32
   * @param options The request options from the service
33
   * @returns CancelablePromise<T>
34
   * @throws ApiError
35
   */
36
  public override request<T>(options: ApiRequestOptions): CancelablePromise<T> {
37
    return __request(this.config, {
19✔
38
      ...options,
39
      headers: {
40
        ...options.headers,
41
        'Content-Type': 'application/json',
42
      },
43
    })
44
  }
45
}
46

47
export class OrderBookApi {
48
  private envConfig: EnvConfig
49
  private service: DefaultService
50

51
  constructor(chainId: SupportedChainId, env: 'prod' | 'staging' = 'prod') {
1✔
52
    this.envConfig = (env === 'prod' ? PROD_CONFIG : STAGING_CONFIG)[chainId]
1!
53

54
    this.service = new OrderBookClient({ BASE: this.envConfig.apiUrl }, FetchHttpRequest).default
1✔
55
  }
56

57
  getTrades({ owner, orderId }: { owner?: Address; orderId?: UID }): CancelablePromise<Array<Trade>> {
58
    if (owner && orderId) {
5✔
59
      return new CancelablePromise((resolve, reject) => {
1✔
60
        reject(new CowError('Cannot specify both owner and orderId'))
1✔
61
      })
62
    }
63

64
    return this.service.getApiV1Trades(owner, orderId)
4✔
65
  }
66

67
  getOrders({
68
    owner,
69
    offset = 0,
×
70
    limit = 1000,
×
71
  }: {
72
    owner: Address
73
    offset?: number
74
    limit?: number
75
  }): Promise<Array<EnrichedOrder>> {
76
    return this.service.getApiV1AccountOrders(owner, offset, limit).then((orders) => {
4✔
77
      return orders.map(transformOrder)
×
78
    })
79
  }
80

81
  getTxOrders(txHash: TransactionHash): Promise<Array<EnrichedOrder>> {
82
    return this.service.getApiV1TransactionsOrders(txHash).then((orders) => {
3✔
83
      return orders.map(transformOrder)
×
84
    })
85
  }
86

87
  getOrder(uid: UID): Promise<EnrichedOrder> {
88
    return this.service.getApiV1Orders(uid).then((order) => {
4✔
89
      return transformOrder(order)
×
90
    })
91
  }
92

93
  getQuote(requestBody: OrderQuoteRequest): CancelablePromise<OrderQuoteResponse> {
94
    return this.service.postApiV1Quote(requestBody)
×
95
  }
96

97
  sendSignedOrderCancellation(uid: UID, requestBody: OrderCancellation): CancelablePromise<void> {
98
    return this.service.deleteApiV1Orders1(uid, requestBody)
2✔
99
  }
100

101
  sendOrder(requestBody: OrderCreation): Promise<UID> {
102
    return this.service.postApiV1Orders(requestBody).catch((error) => {
2✔
103
      const body: OrderPostError = error.body
2✔
104

105
      if (body?.errorType) {
2!
106
        throw new Error(body.errorType)
×
107
      }
108

109
      throw error
2✔
110
    })
111
  }
112

113
  getOrderLink(uid: UID): string {
114
    return this.envConfig.apiUrl + `/api/v1/orders/${uid}`
1✔
115
  }
116
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc