aboutsummaryrefslogtreecommitdiffstats
path: root/packages/pipeline/src/data_sources/oasis/index.ts
blob: 3b30e9dfdd2082975c20c7330f6c7037a49b91ac (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { fetchAsync } from '@0x/utils';

const OASIS_BASE_URL = 'https://data.makerdao.com/v1';
const OASIS_MARKET_QUERY = `query {
  oasisMarkets(period: "1 week") {
    nodes {
      id
      base
      quote
      buyVol
      sellVol
      price
      high
      low
    }
  }
}`;
const OASIS_ORDERBOOK_QUERY = `query ($market: String!) {
  allOasisOrders(condition: { market: $market }) {
    totalCount
    nodes {
      market
      offerId
      price
      amount
      act
    }
  }
}`;
export const OASIS_SOURCE = 'oasis';

export interface OasisMarket {
    id: string; // market symbol e.g MKRDAI
    base: string; // base symbol   e.g MKR
    quote: string; // quote symbol  e.g DAI
    buyVol: number; // total buy volume (base)
    sellVol: number; // total sell volume (base)
    price: number; // volume weighted price (quote)
    high: number; // max sell price
    low: number; // min buy price
}

export interface OasisMarketResponse {
    data: {
        oasisMarkets: {
            nodes: OasisMarket[];
        };
    };
}

export interface OasisOrder {
    offerId: number; // Offer Id
    market: string; // Market symbol (base/quote)
    price: string; // Offer price (quote)
    amount: string; // Offer amount (base)
    act: string; // Action (ask|bid)
}

export interface OasisOrderbookResponse {
    data: {
        allOasisOrders: {
            totalCount: number;
            nodes: OasisOrder[];
        };
    };
}

// tslint:disable:prefer-function-over-method
// ^ Keep consistency with other sources and help logical organization
export class OasisSource {
    /**
     * Call Ddex API to find out which markets they are maintaining orderbooks for.
     */
    public async getActiveMarketsAsync(): Promise<OasisMarket[]> {
        const params = {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({ query: OASIS_MARKET_QUERY }),
        };
        const resp = await fetchAsync(OASIS_BASE_URL, params);
        const respJson: OasisMarketResponse = await resp.json();
        const markets = respJson.data.oasisMarkets.nodes;
        return markets;
    }

    /**
     * Retrieve orderbook from Oasis API for a given market.
     * @param marketId String identifying the market we want data for. Eg. 'REPAUG'.
     */
    public async getMarketOrderbookAsync(marketId: string): Promise<OasisOrder[]> {
        const input = {
            market: marketId,
        };
        const params = {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({ query: OASIS_ORDERBOOK_QUERY, variables: input }),
        };
        const resp = await fetchAsync(OASIS_BASE_URL, params);
        const respJson: OasisOrderbookResponse = await resp.json();
        return respJson.data.allOasisOrders.nodes;
    }
}