aboutsummaryrefslogtreecommitdiffstats
path: root/packages/asset-buyer/src/order_fetchers/provided_order_fetcher.ts
blob: 397f296d7b608b6b9272ba981c971f508f2d38d7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { schemas } from '@0xproject/json-schemas';
import { SignedOrder } from '@0xproject/types';
import * as _ from 'lodash';

import { OrderProvider, OrderProviderRequest, OrderProviderResponse } from '../types';
import { assert } from '../utils/assert';

export class ProvidedOrderFetcher implements OrderProvider {
    public readonly providedOrders: SignedOrder[];
    /**
     * Instantiates a new ProvidedOrderFetcher instance
     * @param   providedOrders  An array of objects that conform to SignedOrder to fetch from.
     * @return  An instance of ProvidedOrderFetcher
     */
    constructor(providedOrders: SignedOrder[]) {
        assert.doesConformToSchema('providedOrders', providedOrders, schemas.signedOrdersSchema);
        this.providedOrders = providedOrders;
    }
    /**
     * Given an object that conforms to OrderFetcherRequest, return the corresponding OrderFetcherResponse that satisfies the request.
     * @param   orderFetchRequest   An instance of OrderFetcherRequest. See type for more information.
     * @return  An instance of OrderFetcherResponse. See type for more information.
     */
    public async getOrdersAsync(orderFetchRequest: OrderProviderRequest): Promise<OrderProviderResponse> {
        assert.isValidOrderFetcherRequest('orderFetchRequest', orderFetchRequest);
        const { makerAssetData, takerAssetData } = orderFetchRequest;
        const orders = _.filter(this.providedOrders, order => {
            return order.makerAssetData === makerAssetData && order.takerAssetData === takerAssetData;
        });
        return { orders };
    }
}