aboutsummaryrefslogtreecommitdiffstats
path: root/packages/pipeline/src/data_sources/ddex/index.ts
blob: 2bbd8c29b9b5b2c3f75a66b69d82e6e768532a64 (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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { fetchAsync, logUtils } from '@0x/utils';

const DDEX_BASE_URL = 'https://api.ddex.io/v2';
const ACTIVE_MARKETS_URL = `${DDEX_BASE_URL}/markets`;
const NO_AGGREGATION_LEVEL = 3; // See https://docs.ddex.io/#get-orderbook
const ORDERBOOK_ENDPOINT = `/orderbook?level=${NO_AGGREGATION_LEVEL}`;
export const DDEX_SOURCE = 'ddex';

export interface DdexActiveMarketsResponse {
    status: number;
    desc: string;
    data: {
        markets: DdexMarket[];
    };
}

export interface DdexMarket {
    id: string;
    quoteToken: string;
    quoteTokenDecimals: number;
    quoteTokenAddress: string;
    baseToken: string;
    baseTokenDecimals: number;
    baseTokenAddress: string;
    minOrderSize: string;
    maxOrderSize: string;
    pricePrecision: number;
    priceDecimals: number;
    amountDecimals: number;
}

export interface DdexOrderbookResponse {
    status: number;
    desc: string;
    data: {
        orderBook: DdexOrderbook;
    };
}

export interface DdexOrderbook {
    marketId: string;
    bids: DdexOrder[];
    asks: DdexOrder[];
}

export interface DdexOrder {
    price: string;
    amount: string;
    orderId: string;
}

// tslint:disable:prefer-function-over-method
// ^ Keep consistency with other sources and help logical organization
export class DdexSource {
    /**
     * Call Ddex API to find out which markets they are maintaining orderbooks for.
     */
    public async getActiveMarketsAsync(): Promise<DdexMarket[]> {
        logUtils.log('Getting all active DDEX markets');
        const resp = await fetchAsync(ACTIVE_MARKETS_URL);
        const respJson: DdexActiveMarketsResponse = await resp.json();
        const markets = respJson.data.markets;
        logUtils.log(`Got ${markets.length} markets.`);
        return markets;
    }

    /**
     * Retrieve orderbook from Ddex API for a given market.
     * @param marketId String identifying the market we want data for. Eg. 'REP/AUG'
     */
    public async getMarketOrderbookAsync(marketId: string): Promise<DdexOrderbook> {
        logUtils.log(`${marketId}: Retrieving orderbook.`);
        const marketOrderbookUrl = `${ACTIVE_MARKETS_URL}/${marketId}${ORDERBOOK_ENDPOINT}`;
        const resp = await fetchAsync(marketOrderbookUrl);
        const respJson: DdexOrderbookResponse = await resp.json();
        return respJson.data.orderBook;
    }
}