Skip to content
This repository has been archived by the owner on Jan 15, 2021. It is now read-only.

Commit

Permalink
Add best ask API (#1477)
Browse files Browse the repository at this point in the history
* Add best ask

* Change icon from wxDAI into DAI logo
  • Loading branch information
anxolin authored Oct 6, 2020
1 parent 50ee856 commit 734ee62
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/api/dexPriceEstimator/DexPriceEstimatorApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ORDER_BOOK_HOPS_DEFAULT, ORDER_BOOK_HOPS_MAX } from 'const'

export interface DexPriceEstimatorApi {
getPrice(params: GetPriceParams): Promise<BigNumber | null>
getBestAsk(params: GetBestAskParams): Promise<BigNumber | null>
getOrderBookUrl(params: OrderBookParams): string
getOrderBookData(params: OrderBookParams): Promise<OrderBookData>
getMinOrderAmounInOWLUrl(networkId: number): string
Expand All @@ -18,6 +19,8 @@ interface GetPriceParams {
inWei?: boolean
}

type GetBestAskParams = Omit<GetPriceParams, 'amountInUnits' | 'inWei'>

interface OrderBookParams {
networkId: number
baseTokenId: number
Expand Down Expand Up @@ -125,6 +128,32 @@ export class DexPriceEstimatorApiImpl implements DexPriceEstimatorApi {
}
}

public async getBestAsk(params: GetBestAskParams): Promise<BigNumber | null> {
const {
networkId,
baseToken: { id: baseTokenId },
quoteToken: { id: quoteTokenId },
} = params

// Query format: markets/7-1/estimated-best-ask-price?unit=baseunits&roundingBuffer=enabled
const queryString = `markets/${baseTokenId}-${quoteTokenId}/estimated-best-ask-price`

try {
const response = await this.query<number>(networkId, queryString)

if (response === null) {
return response
}

return new BigNumber(response)
} catch (e) {
console.error(e)
throw new Error(
`Failed to query best ask for baseToken id ${baseTokenId} quoteToken id ${quoteTokenId}: ${e.message}`,
)
}
}

public getOrderBookUrl(params: OrderBookParams): string {
const { networkId, baseTokenId, quoteTokenId, hops = ORDER_BOOK_HOPS_DEFAULT, batchId } = params
assert(hops >= 0, 'Hops should be positive')
Expand Down
1 change: 1 addition & 0 deletions src/api/dexPriceEstimator/DexPriceEstimatorApiProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export class DexPriceEstimatorApiProxy extends DexPriceEstimatorApiImpl {

this.cache.injectCache<DexPriceEstimatorApi>(this, [
{ method: 'getPrice', ttl: PRICES_CACHE_TIME },
{ method: 'getBestAsk', ttl: PRICES_CACHE_TIME },
{ method: 'getMinOrderAmounInOWL', ttl: PRICES_CACHE_TIME },
])
}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 734ee62

Please sign in to comment.