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

@0x/orderbook #2056

Merged
merged 16 commits into from
Sep 16, 2019
Merged

@0x/orderbook #2056

merged 16 commits into from
Sep 16, 2019

Conversation

dekz
Copy link
Member

@dekz dekz commented Aug 12, 2019

Description

@0x/orderbook

Package to help fetch orders from a remote source (Standard Relayer API, Mesh) and keep the local orderbook synced and up to date.

This means shouldForceRefresh (in AssetSwapper) is not required as orderbook is kept up to date with the settings provided by the developer. It allows the developer to switch from Polling to Websockets to Mesh with minimal changes.

It allows us to modify the architecture of AssetSwapper to transparently refresh quotes with the latest orders prior to execution to reduce transaction failure.

It can be used on Launch Kit to keep a local orderbook in the front end to reduce excess polling and improve performance.

Supported Order Providers:

  • SRA HTTP Polling
  • SRA Websocket
  • Mesh

To be considered at a later date

  • Coordinator: specialised Coordinator Orderbook if the Coordinator does not propagate soft cancels via SRA.
// Create an orderbook for makerAssetData, takerAssetData using the SRA Polling Order Provider
// This Provider polls the SRA endpoint automatically every 5 seconds on the supplied asset pairs
const orderbook = Orderbook.getOrderbookForPollingProvider({
    httpEndpoint: 'https://sra.0x.org/v2',
    pollingIntervalMs: 5000,
});
const orders = await orderbook.getOrdersAsync(makerAssetData, takerAssetData);
// Create an orderbook for makerAssetData, takerAssetData using the SRA Websocket Order Provider
// This provider subscribes via websocket to receive order updates on the supplied asset pairs
const orderbook = Orderbook.getOrderbookForWebsocketProvider({
    httpEndpoint: 'https://sra.0x.org/v2',
    websocketEndpoint: 'wss://ws.sra.0x.org',
});
const orders = await orderbook.getOrdersAsync(makerAssetData, takerAssetData);
// Create an orderbook for makerAssetData, takerAssetData using the Mesh Order Provider
// This provider subscribes via websocket to receive order updates on all orders stored in Mesh
const orderbook = Orderbook.getOrderbookForMeshProvider({
    websocketEndpoint: 'wss://MESH_ENDPOINT',
});
const orders = await orderbook.getOrdersAsync(makerAssetData, takerAssetData);

Checklist:

  • Update AssetSwapper to use @0x/orderbook

Fixes #2062 as this will not throw, only returning empty orders.

@buildsize

This comment has been minimized.

@dekz dekz force-pushed the feature/orderbook branch from e281cde to 7eb839e Compare August 14, 2019 02:29
@dekz dekz marked this pull request as ready for review August 14, 2019 05:07
@dekz dekz force-pushed the feature/orderbook branch from 4117e4f to 4403405 Compare August 26, 2019 02:50
@coveralls
Copy link

coveralls commented Aug 26, 2019

Coverage Status

Coverage remained the same at 77.532% when pulling 09ec6d6 on feature/orderbook into 50f86dd on development.

@dekz dekz force-pushed the feature/orderbook branch 2 times, most recently from de58499 to 6cd9c1f Compare August 26, 2019 06:38
@dekz dekz requested a review from BMillman19 August 27, 2019 02:06
packages/asset-swapper/webpack.config.js Show resolved Hide resolved
packages/orderbook/package.json Outdated Show resolved Hide resolved
super(orderStore);
this._httpClient = new HttpClient(httpEndpoint);
this._perPage = perPage;
this._networkId = networkId;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If networkId is optional, we should also add a default. Otherwise it could be undefined.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

networkId is allowed to be undefined in @0x/connect. Defaulting (to say 1 for Mainnet) could cause confusing behaviour if for example the SRA endpoint is Kovan only (kovan.sra.0x.org) and networkId 1 is provided as a default.

}
const addedRemoved = addedRemovedByKey[assetPairKey];
// If we have the metadata informing us that the order cannot be filled for any amount we don't add it
const remainingFillableTakerAssetAmount = (order.metaData as any).remainingFillableTakerAssetAmount;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When would we not have the metadata?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do not define what is inside of the metaData as per the SRA spec. Radar and Launch kit return remainingFillableTakerAssetAmount but it is not enforced by the spec.

private readonly _map: Map<string, APIOrder>;
constructor(orders: APIOrder[] = []) {
this._map = new Map();
(this as any)[Symbol.iterator] = this.values;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment about what this is doing?


public add(item: APIOrder): void {
const orderHash = utils.getOrderHash(item);
(item.metaData as any).orderHash = orderHash;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this any cast?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do not define what is inside of the metaData as per the SRA spec. Launch kit returns orderHash but it is not enforced by the spec and Radar does not return this.

Copy link
Contributor

@dave4506 dave4506 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed most of the changes request by @fabioberger; package looks good to me.

@dekz dekz merged commit ab6938f into development Sep 16, 2019
@dekz dekz deleted the feature/orderbook branch December 2, 2019 03:54
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

asset-swapper: Cannot quote WETH/USDC without disabling fee fetch
4 participants