Skip to content
This repository has been archived by the owner on Oct 11, 2024. It is now read-only.

Commit

Permalink
Implement getOrders based on getOrdersForPage
Browse files Browse the repository at this point in the history
  • Loading branch information
albrow committed Jan 18, 2020
1 parent 7c9eb01 commit 1404e84
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
41 changes: 41 additions & 0 deletions browser/ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,46 @@ export class Mesh {
return wrapperStatsToStats(wrapperStats);
}

/**
* Get all 0x signed orders currently stored in the Mesh node
* @param perPage number of signedOrders to fetch per paginated request
* @returns the snapshotID, snapshotTimestamp and all orders, their hashes and fillableTakerAssetAmounts
*/
public async getOrdersAsync(perPage: number = 200): Promise<GetOrdersResponse> {
await waitForLoadAsync();
if (this._wrapper === undefined) {
// If this is called after startAsync, this._wrapper is always
// defined. This check is here just in case and satisfies the
// compiler.
return Promise.reject(new Error('Mesh is still loading. Try again soon.'));
}

let snapshotID = ''; // New snapshot

// TODO(albrow): De-dupe this code with the method by the same name
// in the TypeScript RPC client.
let page = 0;
let getOrdersResponse = await this.getOrdersForPageAsync(page, perPage, snapshotID);
snapshotID = getOrdersResponse.snapshotID;
let ordersInfos = getOrdersResponse.ordersInfos;

let allOrderInfos: OrderInfo[] = [];

do {
allOrderInfos = [...allOrderInfos, ...ordersInfos];
page++;
getOrdersResponse = await this.getOrdersForPageAsync(page, perPage, snapshotID);
ordersInfos = getOrdersResponse.ordersInfos;
} while (ordersInfos.length > 0);

getOrdersResponse = {
snapshotID,
snapshotTimestamp: getOrdersResponse.snapshotTimestamp,
ordersInfos: allOrderInfos,
};
return getOrdersResponse;
}

/**
* Get page of 0x signed orders stored on the Mesh node at the specified snapshot
* @param page Page index at which to retrieve orders
Expand All @@ -801,6 +841,7 @@ export class Mesh {
// compiler.
return Promise.reject(new Error('Mesh is still loading. Try again soon.'));
}

const wrapperOrderResponse = await this._wrapper.getOrdersForPageAsync(page, perPage, snapshotID);
return wrapperGetOrdersResponseToGetOrdersResponse(wrapperOrderResponse);
}
Expand Down
6 changes: 5 additions & 1 deletion integration-tests/browser/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,16 @@ provider.start();
(async () => {
for (let event of events) {
// Check the happy path for getOrdersForPageAsync. There should
// be two orders.
// be two orders. (just make sure it doesn't throw/reject).
const firstOrdersResponse = await mesh.getOrdersForPageAsync(0, 1, '');
console.log(JSON.stringify(firstOrdersResponse));
const secondOrdersResponse = await mesh.getOrdersForPageAsync(1, 1, firstOrdersResponse.snapshotID);
console.log(JSON.stringify(secondOrdersResponse));

// Check the happy path for getOrders (just make sure it
// doesn't throw/reject).
await mesh.getOrders();

// Log the event. The Go code will be watching the logs for
// this.
console.log(JSON.stringify(event));
Expand Down

0 comments on commit 1404e84

Please sign in to comment.