diff options
-rw-r--r-- | packages/asset-buyer/src/asset_buyer.ts | 3 | ||||
-rw-r--r-- | packages/asset-buyer/test/asset_buyer_test.ts | 30 | ||||
-rw-r--r-- | packages/instant/src/index.umd.ts | 30 |
3 files changed, 54 insertions, 9 deletions
diff --git a/packages/asset-buyer/src/asset_buyer.ts b/packages/asset-buyer/src/asset_buyer.ts index 77cf403d7..43b60f928 100644 --- a/packages/asset-buyer/src/asset_buyer.ts +++ b/packages/asset-buyer/src/asset_buyer.ts @@ -227,7 +227,8 @@ export class AssetBuyer { assert.isBoolean('shouldForceOrderRefresh', shouldForceOrderRefresh); const assetPairs = await this.orderProvider.getAvailableMakerAssetDatasAsync(assetData); - if (!assetPairs.includes(assetData)) { + const etherTokenAssetData = this._getEtherTokenAssetDataOrThrow(); + if (!assetPairs.includes(etherTokenAssetData)) { return { tokensAvailableInBaseUnits: 0, ethValueAvailableInWei: 0, diff --git a/packages/asset-buyer/test/asset_buyer_test.ts b/packages/asset-buyer/test/asset_buyer_test.ts index 5e05d5564..9ed51f5e5 100644 --- a/packages/asset-buyer/test/asset_buyer_test.ts +++ b/packages/asset-buyer/test/asset_buyer_test.ts @@ -24,6 +24,8 @@ const expect = chai.expect; const FAKE_SRA_URL = 'https://fakeurl.com'; const FAKE_ASSET_DATA = '0xf47261b00000000000000000000000001dc4c1cefef38a777b15aa20260a54e584b16c48'; const TOKEN_DECIMALS = 18; +const DAI_ASSET_DATA = '0xf47261b000000000000000000000000089d24a6b4ccb1b6faa2625fe562bdd9a23260359"'; +const WETH_ASSET_DATA = '0xf47261b0000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2'; const WETH_DECIMALS = constants.ETHER_TOKEN_DECIMALS; const baseUnitAmount = (unitAmount: number, decimals = TOKEN_DECIMALS): BigNumber => { @@ -75,14 +77,26 @@ describe('AssetBuyer', () => { }); }); - it('should return 0s when asset pair not supported', async () => { - mockAvailableAssetDatas(mockOrderProvider, FAKE_ASSET_DATA, []); + describe('asset pair not supported', () => { + it('should return 0s when no asset pair not supported', async () => { + mockAvailableAssetDatas(mockOrderProvider, FAKE_ASSET_DATA, []); - const assetBuyer = new AssetBuyer(mockWeb3Provider.object, mockOrderProvider.object); - const liquidityResult = await assetBuyer.getLiquidityForAssetDataAsync(FAKE_ASSET_DATA); - expect(liquidityResult).to.deep.equal({ - tokensAvailableInBaseUnits: 0, - ethValueAvailableInWei: 0, + const assetBuyer = new AssetBuyer(mockWeb3Provider.object, mockOrderProvider.object); + const liquidityResult = await assetBuyer.getLiquidityForAssetDataAsync(FAKE_ASSET_DATA); + expect(liquidityResult).to.deep.equal({ + tokensAvailableInBaseUnits: 0, + ethValueAvailableInWei: 0, + }); + }); + it('should return 0s when only other asset pair supported', async () => { + mockAvailableAssetDatas(mockOrderProvider, FAKE_ASSET_DATA, [DAI_ASSET_DATA]); + + const assetBuyer = new AssetBuyer(mockWeb3Provider.object, mockOrderProvider.object); + const liquidityResult = await assetBuyer.getLiquidityForAssetDataAsync(FAKE_ASSET_DATA); + expect(liquidityResult).to.deep.equal({ + tokensAvailableInBaseUnits: 0, + ethValueAvailableInWei: 0, + }); }); }); @@ -98,7 +112,7 @@ describe('AssetBuyer', () => { }); beforeEach(() => { - mockAvailableAssetDatas(mockOrderProvider, FAKE_ASSET_DATA, [FAKE_ASSET_DATA]); + mockAvailableAssetDatas(mockOrderProvider, FAKE_ASSET_DATA, [WETH_ASSET_DATA]); }); it('should return 0s when no orders available', async () => { diff --git a/packages/instant/src/index.umd.ts b/packages/instant/src/index.umd.ts index 5a1eb1608..5c0ab8eae 100644 --- a/packages/instant/src/index.umd.ts +++ b/packages/instant/src/index.umd.ts @@ -1,4 +1,6 @@ +import { AssetBuyer } from '@0x/asset-buyer'; import { assetDataUtils } from '@0x/order-utils'; +import { Provider } from 'ethereum-types'; import * as _ from 'lodash'; import * as React from 'react'; import * as ReactDOM from 'react-dom'; @@ -12,8 +14,10 @@ import { } from './constants'; import { assetMetaDataMap } from './data/asset_meta_data_map'; import { ZeroExInstantOverlay, ZeroExInstantOverlayProps } from './index'; +import { Network, OrderSource } from './types'; import { analytics } from './util/analytics'; import { assert } from './util/assert'; +import { providerFactory } from './util/provider_factory'; import { util } from './util/util'; const isInstantRendered = (): boolean => !!document.getElementById(INJECTED_DIV_ID); @@ -134,6 +138,32 @@ export const hasMetaDataForAssetData = (assetData: string): boolean => { return assetMetaDataMap[assetData] !== undefined; }; +export const getLiquidityForAssetDataAsync = async ( + assetData: string, + orderSource: OrderSource, + networkId: Network = Network.Mainnet, + provider?: Provider, +) => { + assert.isHexString('assetData', assetData); + assert.isValidOrderSource('orderSource', orderSource); + assert.isNumber('networkId', networkId); + + if (provider !== undefined) { + assert.isWeb3Provider('provider', provider); + } + + const bestProvider: Provider = provider || providerFactory.getFallbackNoSigningProvider(networkId); + + const assetBuyerOptions = { networkId }; + + const assetBuyer = + typeof orderSource === 'string' + ? AssetBuyer.getAssetBuyerForStandardRelayerAPIUrl(bestProvider, orderSource, assetBuyerOptions) + : AssetBuyer.getAssetBuyerForProvidedOrders(bestProvider, orderSource, assetBuyerOptions); + + return assetBuyer.getLiquidityForAssetDataAsync(assetData); +}; + // Write version info to the exported object for debugging export const GIT_SHA = GIT_SHA_FROM_CONSTANT; export const NPM_VERSION = NPM_PACKAGE_VERSION; |