@@ -12,6 +12,7 @@ import { SwapQuoteRequestOpts, SwapQuoterOpts } from '@0x/asset-swapper/lib/src/
1212import { ContractAddresses , getContractAddressesForChainOrThrow } from '@0x/contract-addresses' ;
1313import { ERC20TokenContract , WETH9Contract } from '@0x/contract-wrappers' ;
1414import { assetDataUtils , SupportedProvider } from '@0x/order-utils' ;
15+ import { MarketOperation } from '@0x/types' ;
1516import { BigNumber , decodeThrownErrorAsRevertError , RevertError } from '@0x/utils' ;
1617import { TxData , Web3Wrapper } from '@0x/web3-wrapper' ;
1718import * as _ from 'lodash' ;
@@ -38,6 +39,8 @@ import { InsufficientFundsError } from '../errors';
3839import { logger } from '../logger' ;
3940import { TokenMetadatasForChains } from '../token_metadatas_for_networks' ;
4041import {
42+ BucketedPriceDepth ,
43+ CalaculateMarketDepthParams ,
4144 CalculateSwapQuoteParams ,
4245 GetSwapQuoteResponse ,
4346 GetTokenPricesResponse ,
@@ -46,6 +49,7 @@ import {
4649 SwapVersion ,
4750 TokenMetadata ,
4851} from '../types' ;
52+ import { marketDepthUtils } from '../utils/market_depth_utils' ;
4953import { createResultCache , ResultCache } from '../utils/result_cache' ;
5054import { serviceUtils } from '../utils/service_utils' ;
5155import { getTokenMetadataIfExists } from '../utils/token_metadata_utils' ;
@@ -269,6 +273,58 @@ export class SwapService {
269273 return prices ;
270274 }
271275
276+ public async calculateMarketDepthAsync (
277+ params : CalaculateMarketDepthParams ,
278+ ) : Promise < {
279+ asks : { depth : BucketedPriceDepth [ ] } ;
280+ bids : { depth : BucketedPriceDepth [ ] } ;
281+ } > {
282+ const { buyToken, sellToken, sellAmount, numSamples, sampleDistributionBase, excludedSources } = params ;
283+ const marketDepth = await this . _swapQuoter . getBidAskLiquidityForMakerTakerAssetPairAsync (
284+ buyToken . tokenAddress ,
285+ sellToken . tokenAddress ,
286+ sellAmount ,
287+ {
288+ numSamples,
289+ excludedSources : [ ...( excludedSources || [ ] ) , ERC20BridgeSource . MultiBridge ] ,
290+ sampleDistributionBase,
291+ } ,
292+ ) ;
293+
294+ const maxEndSlippagePercentage = 20 ;
295+ const scalePriceByDecimals = ( priceDepth : BucketedPriceDepth [ ] ) =>
296+ priceDepth . map ( b => ( {
297+ ...b ,
298+ price : b . price . times ( new BigNumber ( 10 ) . pow ( sellToken . decimals - buyToken . decimals ) ) ,
299+ } ) ) ;
300+ const askDepth = scalePriceByDecimals (
301+ marketDepthUtils . calculateDepthForSide (
302+ marketDepth . asks ,
303+ MarketOperation . Sell ,
304+ numSamples * 2 ,
305+ sampleDistributionBase ,
306+ maxEndSlippagePercentage ,
307+ ) ,
308+ ) ;
309+ const bidDepth = scalePriceByDecimals (
310+ marketDepthUtils . calculateDepthForSide (
311+ marketDepth . bids ,
312+ MarketOperation . Buy ,
313+ numSamples * 2 ,
314+ sampleDistributionBase ,
315+ maxEndSlippagePercentage ,
316+ ) ,
317+ ) ;
318+ return {
319+ // We're buying buyToken and SELLING sellToken (DAI) (50k)
320+ // Price goes from HIGH to LOW
321+ asks : { depth : askDepth } ,
322+ // We're BUYING sellToken (DAI) (50k) and selling buyToken
323+ // Price goes from LOW to HIGH
324+ bids : { depth : bidDepth } ,
325+ } ;
326+ }
327+
272328 private async _getSwapQuoteForWethAsync (
273329 params : CalculateSwapQuoteParams ,
274330 isUnwrap : boolean ,
0 commit comments