decred.org/dcrdex@v1.0.5/server/db/driver/pg/internal/epochs.go (about) 1 package internal 2 3 const ( 4 // CreateEpochsTable creates a table specified via the %s printf specifier 5 // for epoch data. 6 CreateEpochsTable = `CREATE TABLE IF NOT EXISTS %s ( 7 epoch_idx INT8, 8 epoch_dur INT4, -- epoch duration in milliseconds 9 match_time INT8, -- time at which matching and book/unbooks began 10 csum BYTEA, -- commitment checksum 11 seed BYTEA, -- preimage-derived shuffle seed 12 revealed BYTEA[], -- order IDs with revealed preimages 13 missed BYTEA[], -- IDs of orders with no preimage 14 PRIMARY KEY(epoch_idx, epoch_dur) -- epoch idx:dur is unique and the primary key 15 );` 16 17 // InsertEpoch inserts the epoch's match proof data into the epoch table. 18 InsertEpoch = `INSERT INTO %s (epoch_idx, epoch_dur, match_time, csum, seed, revealed, missed) 19 VALUES ($1, $2, $3, $4, $5, $6, $7);` 20 21 SelectLastEpochRate = `SELECT end_rate 22 FROM %s 23 ORDER BY epoch_end DESC 24 LIMIT 1;` 25 26 // CreateEpochReportTable creates an epoch_reports table that holds 27 // epoch-end reports that can be used to construct market history data sets. 28 CreateEpochReportTable = `CREATE TABLE IF NOT EXISTS %s ( 29 epoch_end INT8 PRIMARY KEY, -- using timestamp instead of index to facilitate sorting and filtering with less math 30 epoch_dur INT4, -- epoch duration in milliseconds 31 match_volume INT8, -- total matched during epoch's match cycle 32 quote_volume INT8, -- total matched volume in terms of quote asset 33 book_buys INT8, -- booked buy volume 34 book_buys_5 INT8, -- booked buy volume within 5 pct of market 35 book_buys_25 INT8, -- booked buy volume within 25 pct of market 36 book_sells INT8, -- booked sell volume 37 book_sells_5 INT8, -- booked sell volume within 5 pct of market 38 book_sells_25 INT8, -- booked sell volume within 25 pct of market 39 high_rate INT8, -- the highest rate matched 40 low_rate INT8, -- the lowest rate matched 41 start_rate INT8, -- the rate of the first match in the epoch 42 end_rate INT8 -- the rate of the last match in the epoch 43 );` 44 45 // InsertEpochReport inserts a row into the epoch_reports table. 46 InsertEpochReport = `INSERT INTO %s (epoch_end, epoch_dur, match_volume, quote_volume, 47 book_buys, book_buys_5, book_buys_25, book_sells, book_sells_5, book_sells_25, 48 high_rate, low_rate, start_rate, end_rate) 49 VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14);` 50 51 InsertPartialEpochReport = `INSERT INTO %s (epoch_end, epoch_dur, match_volume, quote_volume, 52 book_buys, book_buys_5, book_buys_25, book_sells, book_sells_5, book_sells_25, -- zeros 53 high_rate, low_rate, start_rate, end_rate) 54 VALUES($1, $2, $3, $4, 0, 0, 0, 0, 0, 0, $5, $6, $7, $8) 55 ON CONFLICT (epoch_end) DO NOTHING;` 56 57 // SelectEpochCandles selects all rows from the epoch_reports table sorted 58 // by ascending time. 59 SelectEpochCandles = `SELECT epoch_end, epoch_dur, match_volume, quote_volume, 60 high_rate, low_rate, start_rate, end_rate 61 FROM %s 62 WHERE epoch_end >= $1 63 ORDER BY epoch_end;` 64 65 // CreateEpochReportTable creates an candles table that holds binned 66 // candle data. 67 CreateCandlesTable = `CREATE TABLE IF NOT EXISTS %s ( 68 end_stamp INT8 PRIMARY KEY, 69 match_volume INT8, 70 quote_volume INT8, 71 high_rate INT8, 72 low_rate INT8, 73 start_rate INT8, 74 end_rate INT8 75 );` 76 77 InsertCandle = `INSERT INTO %s ( 78 end_stamp, match_volume, quote_volume, high_rate, low_rate, start_rate, end_rate 79 ) 80 VALUES ($1, $2, $3, $4, $5, $6, $7) 81 ON CONFLICT (end_stamp) DO UPDATE 82 SET match_volume = $2, quote_volume = $3, high_rate = $4, low_rate = $5, start_rate = $6, end_rate = $7;` 83 84 SelectCandles = `SELECT end_stamp, match_volume, quote_volume, 85 high_rate, low_rate, start_rate, end_rate 86 FROM %s 87 ORDER BY end_stamp 88 LIMIT $1;` 89 90 SelectLastEndStamp = `SELECT (end_stamp) 91 FROM %s 92 ORDER BY end_stamp 93 DESC 94 LIMIT 1;` 95 )