aboutsummaryrefslogtreecommitdiffstats
path: root/packages/pipeline/src/scripts/create_tables.ts
blob: fd0d2b78b51154dfb357f244cf629b2ea11fccb5 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import * as commandLineArgs from 'command-line-args';

import { postgresClient } from '../postgres';
import { formatters } from '../utils';
const tableQueries: any = {
    events_full: `CREATE TABLE IF NOT EXISTS events_full (
        timestamp TIMESTAMP WITH TIME ZONE,
        event_type VARCHAR,
        error_id VARCHAR,
        order_hash CHAR(66),
        maker CHAR(42),
        maker_amount NUMERIC(78),
        maker_fee NUMERIC(78),
        maker_token CHAR(42),
        taker CHAR(42),
        taker_amount NUMERIC(78),
        taker_fee NUMERIC(78),
        taker_token CHAR(42),
        txn_hash CHAR(66),
        gas_used NUMERIC(78),
        gas_price NUMERIC(78),
        fee_recipient CHAR(42),
        method_id CHAR(10),
        salt VARCHAR,
        block_number BIGINT,
        log_index BIGINT,
        taker_symbol VARCHAR,
        taker_name VARCHAR,
        taker_decimals BIGINT,
        taker_usd_price NUMERIC(78),
        taker_txn_usd_value NUMERIC(78),
        maker_symbol VARCHAR,
        maker_name VARCHAR,
        maker_decimals BIGINT,
        maker_usd_price NUMERIC(78),
        maker_txn_usd_value NUMERIC(78),
        PRIMARY KEY (txn_hash, order_hash, log_index)
    )`,
    events: `CREATE TABLE IF NOT EXISTS events (
        timestamp TIMESTAMP WITH TIME ZONE,
        event_type VARCHAR,
        error_id VARCHAR,
        order_hash CHAR(66),
        maker CHAR(42),
        maker_amount NUMERIC(78),
        maker_fee NUMERIC(78),
        maker_token CHAR(42),
        taker CHAR(42),
        taker_amount NUMERIC(78),
        taker_fee NUMERIC(78),
        taker_token CHAR(42),
        txn_hash CHAR(66),
        gas_used NUMERIC(78),
        gas_price NUMERIC(78),
        fee_recipient CHAR(42),
        method_id CHAR(10),
        salt VARCHAR,
        block_number BIGINT,
        log_index BIGINT,
        PRIMARY KEY (txn_hash, order_hash, log_index)
    )`,
    events_staging: `CREATE TABLE IF NOT EXISTS events_staging (
        timestamp TIMESTAMP WITH TIME ZONE,
        event_type VARCHAR,
        error_id VARCHAR,
        order_hash CHAR(66),
        maker CHAR(42),
        maker_amount NUMERIC(78),
        maker_fee NUMERIC(78),
        maker_token CHAR(42),
        taker CHAR(42),
        taker_amount NUMERIC(78),
        taker_fee NUMERIC(78),
        taker_token CHAR(42),
        txn_hash CHAR(66),
        fee_recipient CHAR(42),
        block_number BIGINT,
        log_index BIGINT,
        PRIMARY KEY (txn_hash, order_hash, log_index)
    )`,
    events_raw: `CREATE TABLE IF NOT EXISTS events_raw (
        event_type VARCHAR,
        error_id VARCHAR,
        order_hash CHAR(66),
        maker CHAR(42),
        maker_amount NUMERIC(78),
        maker_fee NUMERIC(78),
        maker_token CHAR(42),
        taker CHAR(42),
        taker_amount NUMERIC(78),
        taker_fee NUMERIC(78),
        taker_token CHAR(42),
        txn_hash CHAR(66),
        fee_recipient CHAR(42),
        block_number BIGINT,
        log_index BIGINT,
        PRIMARY KEY (txn_hash, order_hash, log_index)
    )`,
    blocks: `CREATE TABLE IF NOT EXISTS blocks (
        timestamp TIMESTAMP WITH TIME ZONE,
        block_hash CHAR(66) UNIQUE,
        block_number BIGINT,
        PRIMARY KEY (block_hash)
    )`,
    transactions: `CREATE TABLE IF NOT EXISTS transactions (
        txn_hash CHAR(66) UNIQUE,
        block_hash CHAR(66),
        block_number BIGINT,
        gas_used NUMERIC(78),
        gas_price NUMERIC(78),
        method_id CHAR(10),
        salt VARCHAR,
        PRIMARY KEY (txn_hash)
    )`,
    tokens: `CREATE TABLE IF NOT EXISTS tokens (
        address CHAR(42) UNIQUE,
        name VARCHAR,
        symbol VARCHAR,
        decimals INT,
        PRIMARY KEY (address)
    )`,
    prices: `CREATE TABLE IF NOT EXISTS prices (
        address CHAR(42) UNIQUE,
        timestamp TIMESTAMP WITH TIME ZONE,
        price NUMERIC(78, 18),
        PRIMARY KEY (address, timestamp)
    )`,
    relayers: `CREATE TABLE IF NOT EXISTS relayers (
        name VARCHAR UNIQUE,
        url VARCHAR DEFAULT '',
        sra_http_endpoint VARCHAR DEFAULT '',
        sra_ws_endpoint VARCHAR DEFAULT '',
        fee_recipient_addresses CHAR(42)[] DEFAULT '{}',
        taker_addresses CHAR(42)[] DEFAULT '{}',
        PRIMARY KEY(name)`,
    historical_prices: `CREATE TABLE IF NOT EXISTS historical_prices (
        token VARCHAR,
        base VARCHAR,
        timestamp TIMESTAMP WITH TIME ZONE,
        close NUMERIC(78, 18),
        high NUMERIC(78, 18),
        low NUMERIC(78, 18),
        open NUMERIC(78, 18),
        volume_from NUMERIC(78, 18),
        volume_to NUMERIC(78, 18),
        PRIMARY KEY (token, base, timestamp)
    )`,
    orders: `CREATE TABLE IF NOT EXISTS orders (
        relayer_id VARCHAR,
        exchange_contract_address CHAR(42),
        maker CHAR(42),
        maker_amount NUMERIC(78),
        maker_fee NUMERIC(78),
        maker_token CHAR(42),
        taker CHAR(42),
        taker_amount NUMERIC(78),
        taker_fee NUMERIC(78),
        taker_token CHAR(42),
        fee_recipient CHAR(42),
        expiration_unix_timestamp_sec NUMERIC(78),
        salt VARCHAR,
        order_hash CHAR(66),
        PRIMARY KEY (relayer_id, order_hash)
    )`,
};
function _safeQuery(query: string): any {
    return new Promise((resolve, reject) => {
        postgresClient
            .query(query)
            .then((data: any) => {
                resolve(data);
            })
            .catch((err: any) => {
                reject(err);
            });
    });
}
export const tableScripts = {
    createTable(query: string): any {
        return _safeQuery(query);
    },
    createAllTables(): any {
        for (const tableName of tableQueries) {
            _safeQuery(tableQueries[tableName]);
        }
    },
};
export const insertDataScripts = {
    insertSingleRow(table: string, object: any): any {
        return new Promise((resolve, reject) => {
            const columns = Object.keys(object);
            const safeArray: any = [];
            for (const key of columns) {
                if (key in object) {
                    if (key === 'timestamp') {
                        safeArray.push('to_timestamp(' + object[key] + ')');
                    } else if (typeof object[key] === 'string' || object[key] instanceof String) {
                        safeArray.push(formatters.escapeSQLParam(object[key]));
                    } else {
                        safeArray.push(object[key]);
                    }
                } else {
                    safeArray.push('default');
                }
            }
            const queryString = `INSERT INTO ${table} (${columns}) VALUES (${safeArray}) ON CONFLICT DO NOTHING`;
            console.log(queryString);
            postgresClient
                .query(queryString)
                .then((data: any) => {
                    resolve(data);
                })
                .catch((err: any) => {
                    reject(err);
                });
        });
    },
    insertMultipleRows(table: string, rows: any[], columns: any[]): any {
        return new Promise((resolve, reject) => {
            if (rows.length > 0) {
                const rowsSplit = rows.map((value, index) => {
                    const safeArray: any = [];
                    for (const key of columns) {
                        if (key in value) {
                            if (key === 'timestamp') {
                                safeArray.push('to_timestamp(' + value[key] + ')');
                            } else if (typeof value[key] === 'string' || value[key] instanceof String) {
                                safeArray.push(formatters.escapeSQLParam(value[key]));
                            } else if (value[key] instanceof Array) {
                                const escapedArray = value[key].map((subValue: string, subIndex: number) => {
                                    return formatters.escapeSQLParam(subValue);
                                });
                                safeArray.push('ARRAY[' + escapedArray.toString() + ']');
                            } else {
                                safeArray.push(value[key]);
                            }
                        } else {
                            safeArray.push('default');
                        }
                    }
                    return '(' + safeArray + ')';
                });
                const queryString = `INSERT INTO ${table} (${columns}) VALUES ${rowsSplit} ON CONFLICT DO NOTHING`;
                postgresClient
                    .query(queryString)
                    .then((data: any) => {
                        resolve(data);
                    })
                    .catch((err: any) => {
                        // console.log(err);
                        reject(err);
                    });
            } else {
                resolve({});
            }
        });
    },
};