From 8f2a819ebe92de311250c90e70af9e0b53f8ee4a Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Fri, 13 May 2022 23:11:01 +0200 Subject: [PATCH] Query the websocket to get the correct value for the ticker --- .gitmodules | 3 + meson.build | 2 + sample_responses.json | 1798 +++++++++++++++++++++++++++++++++++++++++ src/main.cpp | 184 ++++- src/meson.build | 8 + subprojects/beast | 1 + subprojects/wrenpp | 2 +- 7 files changed, 1954 insertions(+), 44 deletions(-) create mode 100644 sample_responses.json create mode 160000 subprojects/beast diff --git a/.gitmodules b/.gitmodules index 52eebb4..b52e217 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "subprojects/wrenpp"] path = subprojects/wrenpp url = https://alarmpi.no-ip.org/gitan/King_DuckZ/wrenpp.git +[submodule "subprojects/beast"] + path = subprojects/beast + url = https://github.com/boostorg/beast.git diff --git a/meson.build b/meson.build index a51a618..76ad5ba 100644 --- a/meson.build +++ b/meson.build @@ -8,6 +8,8 @@ project('duckticker', 'cpp', full_config_dir = get_option('prefix') / get_option('sysconfdir') compiler_opts = ['-DWRENPP_WITH_NAME_GUESSING'] +beast_include = include_directories('subprojects/beast/include') + subdir('src') #if meson.source_root() != full_config_dir diff --git a/sample_responses.json b/sample_responses.json new file mode 100644 index 0000000..a5a27f0 --- /dev/null +++ b/sample_responses.json @@ -0,0 +1,1798 @@ +{ + "event": "phx_reply", + "payload": { + "response": {}, + "status": "ok" + }, + "ref": "3", + "topic": "webs:markets_bitfinex_USD" +} + +{ + "event": "new_msg", + "payload": { + "data": { + "currency": "USD", + "exchange_name": "bitfinex", + "trade": { + "amount": 0.0486, + "date": 1652471682, + "exchange_name": "bitfinex", + "price": 30072 + } + } + }, + "ref": null, + "topic": "webs:markets_bitfinex_USD" +} + +{ + "event": "new_msg", + "payload": { + "data": { + "currency": "USD", + "depth": { + "price": 30071, + "type": 1, + "volume": -1.4123 + }, + "exchange_name": "bitfinex" + } + }, + "ref": null, + "topic": "webs:markets_bitfinex_USD" +} + +{ + "event": "new_msg", + "payload": { + "data": { + "currency": "USD", + "depth_shot": [ + [ + [ + 27300, + 695.04972892 + ], + [ + 27400, + 695.04953373 + ], + [ + 27500, + 693.46354632 + ], + [ + 27600, + 690.57702434 + ], + [ + 27700, + 675.2377192499999 + ], + [ + 27800, + 673.0658800399999 + ], + [ + 27900, + 671.3799514499999 + ], + [ + 28000, + 668.13026536 + ], + [ + 28100, + 652.6496043799999 + ], + [ + 28200, + 645.6617948499999 + ], + [ + 28300, + 645.1253953299998 + ], + [ + 28400, + 644.1510087499998 + ], + [ + 28500, + 633.3997066599999 + ], + [ + 28560, + 627.9892887599999 + ], + [ + 28570, + 627.9660559299999 + ], + [ + 28590, + 624.9378051599999 + ], + [ + 28600, + 624.9341799299999 + ], + [ + 28600, + 624.9241799299999 + ], + [ + 28610, + 622.8756946399999 + ], + [ + 28620, + 622.8736606199999 + ], + [ + 28630, + 622.8160538399999 + ], + [ + 28640, + 622.8144586599999 + ], + [ + 28650, + 622.6790741299999 + ], + [ + 28660, + 622.6592741299999 + ], + [ + 28670, + 622.5546924999999 + ], + [ + 28680, + 622.5534812399999 + ], + [ + 28690, + 622.5168503499999 + ], + [ + 28700, + 622.4937507199999 + ], + [ + 28710, + 621.4777658099998 + ], + [ + 28720, + 621.4773168099998 + ], + [ + 28730, + 621.4704862099999 + ], + [ + 28740, + 621.4549448199999 + ], + [ + 28750, + 621.4449448199999 + ], + [ + 28760, + 621.2892159899999 + ], + [ + 28770, + 621.2877652099999 + ], + [ + 28780, + 621.2876745599999 + ], + [ + 28790, + 621.2363131199999 + ], + [ + 28800, + 621.2273263699999 + ], + [ + 28810, + 621.1694901299999 + ], + [ + 28820, + 621.1589775599999 + ], + [ + 28830, + 621.10254817 + ], + [ + 28840, + 620.9656886299999 + ], + [ + 28850, + 580.7417394299999 + ], + [ + 28860, + 576.73453407 + ], + [ + 28870, + 576.60787997 + ], + [ + 28880, + 576.57521239 + ], + [ + 28890, + 545.25801657 + ], + [ + 28900, + 545.23518584 + ], + [ + 28910, + 544.9141889599999 + ], + [ + 28920, + 543.8945793299999 + ], + [ + 28930, + 543.89357133 + ], + [ + 28940, + 543.87414925 + ], + [ + 28950, + 518.51615135 + ], + [ + 28960, + 518.0673412799999 + ], + [ + 28970, + 514.5884730299999 + ], + [ + 28980, + 514.4792401499999 + ], + [ + 28990, + 514.44480583 + ], + [ + 29000, + 514.38165695 + ], + [ + 29010, + 493.69949583999994 + ], + [ + 29020, + 493.4886495399999 + ], + [ + 29030, + 493.03920968999995 + ], + [ + 29040, + 493.03526256999993 + ], + [ + 29050, + 492.59692320999994 + ], + [ + 29060, + 489.85506948999995 + ], + [ + 29070, + 489.83562193999995 + ], + [ + 29080, + 489.82005014999993 + ], + [ + 29090, + 489.2393236699999 + ], + [ + 29100, + 489.2294177799999 + ], + [ + 29110, + 488.80416588999987 + ], + [ + 29120, + 488.8006708899999 + ], + [ + 29130, + 488.7857048599999 + ], + [ + 29140, + 488.4680032899999 + ], + [ + 29150, + 487.0627863499999 + ], + [ + 29160, + 487.0465999299999 + ], + [ + 29170, + 487.0445999299999 + ], + [ + 29180, + 484.18537422999987 + ], + [ + 29190, + 470.5810011099999 + ], + [ + 29200, + 470.33094110999986 + ], + [ + 29210, + 463.42815623999985 + ], + [ + 29220, + 463.39545539999983 + ], + [ + 29230, + 451.95870630999985 + ], + [ + 29240, + 451.95720513999987 + ], + [ + 29250, + 451.35720513999985 + ], + [ + 29260, + 451.33518441999985 + ], + [ + 29270, + 451.3328111999986 + ], + [ + 29280, + 451.33266989999987 + ], + [ + 29290, + 451.19126533999986 + ], + [ + 29300, + 441.19471984999984 + ], + [ + 29310, + 441.14571884999987 + ], + [ + 29330, + 440.7911281999999 + ], + [ + 29340, + 438.34335385999987 + ], + [ + 29350, + 438.33940909999984 + ], + [ + 29360, + 437.82745828999987 + ], + [ + 29370, + 437.8184667899999 + ], + [ + 29380, + 437.6728326199999 + ], + [ + 29390, + 437.1511220499999 + ], + [ + 29400, + 437.06810375999993 + ], + [ + 29410, + 430.25551547999993 + ], + [ + 29420, + 429.5740094899999 + ], + [ + 29430, + 427.4175066599999 + ], + [ + 29450, + 427.4173662199999 + ], + [ + 29467, + 424.5846583899999 + ], + [ + 29474, + 421.6730583899999 + ], + [ + 29482, + 420.97315838999987 + ], + [ + 29485, + 420.97170760999984 + ], + [ + 29495, + 418.6313681899998 + ], + [ + 29500, + 415.13216818999985 + ], + [ + 29516, + 413.8763921899998 + ], + [ + 29522, + 410.7123759899998 + ], + [ + 29540, + 410.13647598999984 + ], + [ + 29544, + 410.08065978999986 + ], + [ + 29549, + 410.07607407999984 + ], + [ + 29553, + 410.0746233099998 + ], + [ + 29560, + 404.1840233099998 + ], + [ + 29565, + 403.0343233099998 + ], + [ + 29566, + 400.7278233099998 + ], + [ + 29588, + 400.7128233099998 + ], + [ + 29592, + 398.1913138699998 + ], + [ + 29593, + 397.24607934999983 + ], + [ + 29596, + 391.5701179299998 + ], + [ + 29600, + 391.5700017499998 + ], + [ + 29605, + 391.5630017499998 + ], + [ + 29616, + 391.08870174999976 + ], + [ + 29617, + 391.08725094999977 + ], + [ + 29622, + 388.16435094999974 + ], + [ + 29631, + 388.05561457999977 + ], + [ + 29640, + 388.0456145799998 + ], + [ 29645, + 388.0204385799998 + ], + [ + 29647, + 388.0185867399998 + ], + [ + 29651, + 387.54568673999984 + ], + [ + 29668, + 387.54485168999986 + ], + [ + 29674, + 380.29911728999986 + ], + [ + 29675, + 380.27394128999987 + ], + [ + 29680, + 380.2487652899999 + ], + [ + 29681, + 380.2477652899999 + ], + [ + 29683, + 380.2330498699999 + ], + [ + 29684, + 380.2315990799999 + ], + [ + 29686, + 379.8433990799999 + ], + [ + 29698, + 379.8399829499999 + ], + [ + 29699, + 379.8215390599999 + ], + [ + 29717, + 379.1841390599999 + ], + [ + 29721, + 378.9241390599999 + ], + [ + 29722, + 375.4612390599999 + ], + [ + 29723, + 339.8912390599999 + ], + [ + 29725, + 339.11553905999995 + ], + [ + 29727, + 337.12673165999996 + ], + [ + 29730, + 335.24135994999995 + ], + [ + 29737, + 332.36425994999996 + ], + [ + 29741, + 332.36372790999997 + ], + [ + 29749, + 332.35195190999997 + ], + [ + 29750, + 332.31224327999996 + ], + [ + 29752, + 332.31069249999996 + ], + [ + 29753, + 332.30511249999995 + ], + [ + 29754, + 329.95091249999996 + ], + [ + 29760, + 324.10951249999994 + ], + [ + 29761, + 324.1093736099999 + ], + [ + 29765, + 324.0975976099999 + ], + [ + 29769, + 324.0858216099999 + ], + [ + 29771, + 323.7862216099999 + ], + [ + 29772, + 323.7861608899999 + ], + [ + 29780, + 320.1234396999999 + ], + [ + 29781, + 318.5601336999999 + ], + [ + 29785, + 315.1305336999999 + ], + [ + 29793, + 314.4939336999999 + ], + [ + 29798, + 312.1569336999999 + ], + [ + 29800, + 311.9569336999999 + ], + [ + 29803, + 286.4569336999999 + ], + [ + 29804, + 280.5615336999999 + ], + [ + 29811, + 279.01093369999995 + ], + [ + 29816, + 277.75439496999996 + ], + [ + 29817, + 273.29622718999997 + ], + [ + 29819, + 244.39477637999997 + ], + [ + 29826, + 244.07647637999997 + ], + [ + 29829, + 228.86557637999996 + ], + [ + 29831, + 228.86534274999997 + ], + [ + 29839, + 205.85824274999996 + ], + [ + 29849, + 204.71526489999997 + ], + [ + 29852, + 198.93656489999998 + ], + [ + 29854, + 198.84739427999997 + ], + [ + 29857, + 194.12519211999998 + ], + [ + 29861, + 187.01839212 + ], + [ + 29862, + 186.49649212 + ], + [ + 29863, + 186.49577711999999 + ], + [ + 29867, + 178.03337711999998 + ], + [ + 29872, + 176.99935706 + ], + [ + 29878, + 175.72735706 + ], + [ + 29879, + 164.05375706 + ], + [ + 29884, + 102.05375706000001 + ], + [ + 29885, + 102.05230626000001 + ], + [ + 29886, + 82.05230626000001 + ], + [ + 29887, + 76.58048704000001 + ], + [ + 29888, + 76.58009681000001 + ], + [ + 29889, + 76.57670376000002 + ], + [ + 29892, + 76.49550345000002 + ], + [ + 29894, + 75.66194945000001 + ], + [ + 29895, + 74.61734945 + ], + [ + 29897, + 73.64795565 + ], + [ + 29898, + 73.62424782 + ], + [ + 29900, + 73.62407576999999 + ], + [ + 29902, + 73.29057476999999 + ], + [ + 29903, + 72.95712277 + ], + [ + 29907, + 72.12358077 + ], + [ + 29909, + 72.12029021000001 + ], + [ + 29910, + 72.11802494000001 + ], + [ + 29911, + 71.68982494000001 + ], + [ + 29917, + 69.77063715000001 + ], + [ + 29919, + 69.58193715000002 + ], + [ + 29920, + 69.23519615000002 + ], + [ + 29921, + 69.16244933000002 + ], + [ + 29922, + 68.94824933000001 + ], + [ + 29924, + 68.61444933000001 + ], + [ + 29925, + 67.70968178000001 + ], + [ + 29928, + 67.70917841 + ], + [ + 29931, + 67.53217841 + ], + [ + 29932, + 67.44867829 + ], + [ + 29933, + 67.08570229 + ], + [ + 29934, + 66.74531329 + ], + [ + 29936, + 66.57119887 + ], + [ + 29937, + 65.19179887 + ], + [ + 29939, + 65.01609887 + ], + [ + 29940, + 64.68260787 + ], + [ + 29941, + 63.8488024 + ], + [ + 29944, + 63.66823323 + ], + [ + 29945, + 62.83470823 + ], + [ + 29946, + 61.97810823 + ], + [ + 29947, + 61.13796693 + ], + [ + 29949, + 60.80435893 + ], + [ + 29950, + 60.80409073 + ], + [ + 29951, + 60.786810769999995 + ], + [ + 29952, + 57.60892379999999 + ], + [ + 29954, + 57.604453019999994 + ], + [ + 29956, + 57.50120414999999 + ], + [ + 29957, + 57.34470165999999 + ], + [ + 29958, + 56.67448536999999 + ], + [ + 29959, + 55.805667369999995 + ], + [ + 29960, + 55.70566736999999 + ], + [ + 29961, + 53.75845232999999 + ], + [ + 29962, + 53.42365291999999 + ], + [ + 29963, + 53.42265291999999 + ], + [ + 29964, + 51.88590291999999 + ], + [ + 29966, + 51.88092291999999 + ], + [ + 29968, + 51.70893558999999 + ], + [ + 29969, + 51.37534958999999 + ], + [ + 29970, + 48.047500789999994 + ], + [ + 29971, + 45.684700789999994 + ], + [ + 29973, + 45.350006789999995 + ], + [ + 29974, + 44.567543969999996 + ], + [ + 29975, + 44.56300397 + ], + [ + 29976, + 44.56200397 + ], + [ + 29977, + 43.58725782 + ], + [ + 29978, + 43.58625782 + ], + [ + 61.788244680000005 + ], + [ + 30117, + 65.12283333 + ], + [ + 30119, + 68.45068213 + ], + [ + 30120, + 73.15235941 + ], + [ + 30121, + 76.48675563 + ], + [ + 30122, + 78.28665563 + ], + [ + 30123, + 78.55340862999999 + ], + [ + 30124, + 79.13080862999999 + ], + [ + 30126, + 81.68939576 + ], + [ + 30127, + 85.01956215999999 + ], + [ + 30129, + 90.02625930999999 + ], + [ + 30130, + 94.79697405999998 + ], + [ + 30131, + 94.99807405999998 + ], + [ + 30132, + 98.33266270999998 + ], + [ + 30134, + 99.26866270999999 + ], + [ + 30136, + 102.60415892999998 + ], + [ + 30138, + 103.13903192999999 + ], + [ + 30142, + 103.14013192999998 + ], + [ + 30143, + 103.84323192999999 + ], + [ + 30144, + 104.16914003 + ], + [ + 30145, + 104.17058478999999 + ], + [ + 30149, + 104.17542442999999 + ], + [ + 30150, + 105.34712443 + ], + [ + 30156, + 106.59712443 + ], + [ + 30162, + 106.77262443 + ], + [ + 30163, + 107.12362442999999 + ], + [ + 30164, + 107.98042443 + ], + [ + 30165, + 109.23042443 + ], + [ + 30171, + 114.80905271 + ], + [ + 30176, + 114.86691224 + ], + [ + 30181, + 121.92511224 + ], + [ + 30182, + 149.73734924000001 + ], + [ + 30187, + 150.16514924 + ], + [ + 30191, + 150.54561014 + ], + [ + 30192, + 150.74561014 + ], + [ + 30195, + 163.39591013999998 + ], + [ + 30196, + 163.61011014 + ], + [ + 30197, + 163.62188614 + ], + [ + 30199, + 168.302486139999 350.6854566399998 + ], + [ + 30429, + 350.6945475499998 + ], + [ + 30431, + 350.6947851499998 + ], + [ + 30434, + 350.7038760599998 + ], + [ + 30444, + 353.0293578799998 + ], + [ + 30450, + 353.04075887999977 + ], + [ + 30456, + 353.4661378799998 + ], + [ + 30458, + 353.4913138799998 + ], + [ + 30465, + 353.51648987999977 + ], + [ + 30466, + 353.53096987999976 + ], + [ + 30469, + 354.00366987999973 + ], + [ + 30470, + 354.11216553999975 + ], + [ + 30485, + 354.1123021499998 + ], + [ + 30487, + 354.1137469099998 + ], + [ + 30492, + 358.0015610799998 + ], + [ + 30493, + 360.5973352699998 + ], + [ + 30497, + 360.6973352699998 + ], + [ + 30500, + 362.05843526999985 + ], + [ + 30510, + 364.90717476999987 + ], + [ + 30513, + 364.90757091999984 + ], + [ + 30517, + 364.90774197999986 + ], + [ + 30522, + 365.48878401999985 + ], + [ + 30530, + 365.4888484599999 + ], + [ + 30543, + 365.48905151999986 + ], + [ + 30544, + 368.73429605999985 + ], + [ + 30545, + 368.7348809599998 + ], + [ + 30550, + 369.2199809599998 + ], + [ + 30553, + 369.2201961499998 + ], + [ + 30556, + 369.22164090999985 + ], + [ + 30562, + 372.02894090999985 + ], + [ + 30566, + 372.04394090999983 + ], + [ + 30569, + 372.0572881499998 + ], + [ + 30576, + 373.2089881499998 + ], + [ + 30578, + 373.20911514999983 + ], + [ + 30599, + 373.4091151499998 + ], + [ + 30600, + 373.4191151499998 + ], + [ + 30612, + 373.47114194999983 + ], + [ + 30614, + 373.47158445999986 + ], + [ + 30619, + 373.48570445999985 + ], + [ + 30624, + 373.48730545999985 + ], + [ + 30625, + 373.4887502199999 + ], + [ + 30636, + 373.5169883899999 + ], + [ + 30650, + 373.52798338999986 + ], + [ + 30652, + 376.45578338999985 + ], + [ + 30654, + 376.45591924999985 + ], + [ + 30656, + 383.53846454999984 + ], + [ + 30670, + 383.57150114999985 + ], + [ + 30680, + 383.57208722999985 + ], + [ + 30690, + 384.27448722999986 + ], + [ + 30700, + 385.7301149899999 + ], + [ + 30710, + 385.7303769699999 + ], + [ + 30730, + 385.7347892699999 + ], + [ + 30740, + 385.7352097499999 + ], + [ + 30750, + 395.0213912599999 + ], + [ + 30760, + 395.84582622999994 + ], + [ + 30770, + 405.60831483999993 + ], + [ + 30780, + 405.61432647999993 + ], + [ + 30790, + 405.63059580999993 + ], + [ + 30800, + 407.73629680999994 + ], + [ + 30810, + 407.73667917999995 + ], + [ + 30820, + 407.73681428 + ], + [ + 30830, + 407.73825904 + ], + [ + 30840, + 413.64765904 + ], + [ + 30850, + 413.65873534 + ], + [ + 30860, + 413.71746567 + ], + [ + 30870, + 413.72746567 + ], + [ + 30880, + 413.72826673000003 + ], + [ + 30890, + 414.02826673000004 + ], + [ + 30900, + 426.99529529000006 + ], + [ + 30910, + 426.9992952900001 + ], + [ + 30920, + 427.0001753300001 + ], + [ + 30930, + 427.0480712200001 + ], + [ + 30940, + 427.0824540200001 + ], + [ + 30950, + 430.0117540200001 + ], + [ + 30960, + 434.0875352900001 + ], + [ + 30970, + 434.1447810500001 + ], + [ + 30980, + 435.7415115800001 + ], + [ + 30990, + 435.8966652900001 + ], + [ + 31000, + 439.9449896500001 + ], + [ + 31010, + 443.3425896500001 + ], + [ + 31020, + 443.3429118500001 + ], + [ + 31030, + 443.3529372800001 + ], + [ + 31040, + 443.3553820400001 + ], + [ + 31050, + 468.8194147400001 + ], + [ + 31070, + 468.83545401000015 + ], + [ + 31080, + 468.85573301000016 + ], + [ + 31090, + 468.8607943700002 + ], + [ + 31100, + 469.3309950600002 + ], + [ + 31110, + 469.3326898200002 + ], + [ + 31120, + 469.3330432200002 + ], + [ + 31130, + 495.2633449200002 + ], + [ + 31140, + 495.2788385000002 + ], + [ + 31150, + 532.4772299500002 + ], + [ + 31160, + 537.7417263100001 + ], + [ + 31170, + 576.6269835500001 + ], + [ + 31180, + 576.6310800300001 + ], + [ + 31190, + 576.6427400300001 + ], + [ + 31200, + 576.8363090900001 + ], + [ + 31210, + 576.8579737400001 + ], + [ + 31220, + 583.8590737400001 + ], + [ + 31240, + 583.8896293000001 + ], + [ + 31250, + 585.9048825500001 + ], + [ + 31260, + 585.9303531600001 + ], + [ + 31270, + 585.9503531600001 + ], + [ + 31280, + 585.9610458300001 + ], + [ + 31290, + 586.9435081600001 + ], + [ + 31300, + 587.2450081600001 + ], + [ + 31310, + 587.2510081600001 + ], + [ + 31320, + 590.8110734700001 + ], + [ + 31330, + 592.8692844700001 + ], + [ + 31340, + 592.8915208400001 + ], + [ + 31350, + 592.9879243600001 + ], + [ + 31360, + 592.9897143800001 + ], + [ + 31370, + 598.9860643800001 + ], + [ + 31380, + 598.9870853800002 + ], + [ + 31390, + 601.3210414900002 + ], + [ + 31400, + 602.4904210400002 + ], + [ + 31410, + 602.5046170400002 + ], + [ + 31420, + 602.5091670400002 + ], + [ + 31430, + 602.5132270400002 + ], + [ + 31440, + 602.5786603100001 + ], + [ + 31450, + 602.5886603100001 + ], + [ + 31460, + 605.4812603100002 + ], + [ + 31470, + 605.5076695700002 + ], + [ + 31480, + 605.5087165700002 + ], + [ + 31490, + 605.6127165700002 + ], + [ + 31500, + 606.1404049200002 + ], + [ + 31510, + 606.1555295900001 + ], + [ + 31530, + 608.5817708400001 + ], + [ + 31540, + 608.5897156000001 + ], + [ + 31550, + 608.66203684 + ], + [ + 31560, + 608.67364938 + ], + [ + 31600, + 619.49581686 + ], + [ + 31700, + 620.73313501 + ], + [ + 31800, + 631.30647557 + ], + [ + 31900, + 634.07002054 + ], + [ + 32000, + 638.83675005 + ], + [ + 32100, + 643.81200322 + ], + [ + 32200, + 656.5014491 + ], + [ + 32300, + 658.6303733 + ], + [ + 32400, + 659.71048786 + ], + [ + 32500, + 691.5618261799999 + ], + [ + 32600, + 695.1201035999999 + ], + [ + 32700, + 695.4542757299998 + ], + [ + 32800, + 707.0705397999998 + ], + [ + 32900, + 708.7282736299998 + ] + ] + ], + "exchange_name": "bitfinex" + } + }, + "ref": null, + "topic": "webs:markets_bitfinex_USD" +} diff --git a/src/main.cpp b/src/main.cpp index 5538b62..47d63a6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,10 +15,12 @@ * along with duckticker. If not, see . */ -#include "nap/quick_rest.hpp" +#include +#include #include #include #include +#include #include namespace { @@ -29,88 +31,178 @@ foreign class TickerPrice { foreign price foreign currency + foreign exchange + foreign is_valid } class Bitcoinity { - foreign static ticker(currency) + foreign static ticker(currency, exchange) } class App { - construct new(currency) { + construct new(currency, exchange) { _currency = currency + _exchange = exchange } start() { - var price = Bitcoinity.ticker(_currency) - System.print("Ticker: %(price.price) %(price.currency)") + var price = Bitcoinity.ticker(_currency, _exchange) + System.print("Ticker: %(price.price) %(price.currency) on %(price.exchange)") } } -var the_app = App.new("USD") +var the_app = App.new("USD", "kraken") )script"; +class WebsocketReader { + using tcp = boost::asio::ip::tcp; +public: + WebsocketReader (std::string host, + const std::string& port, + const std::string& path, + const std::string& user_agent, + const std::string& message + ); + + ~WebsocketReader(); + + std::string read(); + +private: + boost::asio::io_context m_ioc; + tcp::resolver m_resolver; + boost::beast::websocket::stream m_ws; +}; + +WebsocketReader::WebsocketReader (std::string host, + const std::string& port, + const std::string& path, + const std::string& user_agent, + const std::string& message +) : + m_resolver{m_ioc}, + m_ws{m_ioc} +{ + //see https://www.boost.org/doc/libs/develop/libs/beast/example/websocket/client/sync/websocket_client_sync.cpp + const auto results = m_resolver.resolve(host, port); + auto ep = boost::asio::connect(m_ws.next_layer(), results); + host += ':' + std::to_string(ep.port()); + m_ws.set_option(boost::beast::websocket::stream_base::decorator( + [user_agent](boost::beast::websocket::request_type& req) + { + req.set(boost::beast::http::field::user_agent, user_agent); + } + )); + m_ws.handshake(host, path); + m_ws.write(boost::asio::buffer(message)); +} + +std::string WebsocketReader::read() { + boost::beast::flat_buffer buffer; + m_ws.read(buffer); + return boost::beast::buffers_to_string(buffer.data()); +} + +WebsocketReader::~WebsocketReader() { + m_ws.close(boost::beast::websocket::close_code::normal); +} + class TickerPrice { public: TickerPrice() = default; - TickerPrice (double price, std::string&& currency) : + TickerPrice (double price, std::string&& currency, std::string&& exchange) : m_price(price), - m_currency(std::move(currency)) + m_currency(std::move(currency)), + m_exchange(std::move(exchange)) {} void set_price (double price) { m_price = price; } void set_currency (std::string&& currency) { m_currency = std::move(currency); } + void set_exchange (std::string&& exchange) { m_exchange = std::move(exchange); } double price() const noexcept { return m_price; } const char* currency() const noexcept { return m_currency.c_str(); } + const char* exchange() const noexcept { return m_exchange.c_str(); } + bool is_valid() const noexcept { return not m_exchange.empty(); } private: double m_price{}; std::string m_currency; + std::string m_exchange; }; -TickerPrice ticker_price (std::string_view currency) { - nap::QuickRest qrest{simdjson::SIMDJSON_PADDING}; - qrest.set_user_agent("duckticker"); - qrest.add_post("currency", currency); +TickerPrice ticker_price_bitoinity (std::string_view currency, std::string_view exchange) { + static const TickerPrice error_price {0.0, "", ""}; - auto response = qrest.fetch("https://bitcoinity.org/simple_switch_currency"); - //std::cout << response.body << '\n'; + WebsocketReader websocket{"bitcoinity.org", + "80", + "/webs_bridge/websocket", + std::string(BOOST_BEAST_VERSION_STRING) + " websocket-client-coro", + R"({"topic":"webs:markets_)" + std::string{exchange} + "_" + + std::string{currency} + R"(","event":"phx_join","payload":{},"ref":"3"})" + }; simdjson::ondemand::parser parser; - simdjson::padded_string_view body{response.body, response.body.size() + simdjson::SIMDJSON_PADDING}; - simdjson::ondemand::document doc = parser.iterate(body); - const double price_out = doc["price"]; - const std::string_view currency_out = doc["currency"]; - return {price_out, std::string{currency_out}}; + { + simdjson::padded_string body = websocket.read(); + std::cout << body << '\n'; + if (body.size() == 0) + return error_price; + simdjson::ondemand::document doc = parser.iterate(body); + + if (doc["event"].get_string().value() != "phx_reply" or doc["payload"]["status"].get_string().value() != "ok") + return error_price; + } + + { + double price_out; + std::string currency_out; + std::string exchange_out; + do { + simdjson::padded_string body = websocket.read(); + std::cout << body << '\n'; + simdjson::ondemand::document doc = parser.iterate(body); + auto payload_result = doc["payload"]; + if (payload_result.error()) + return error_price; + simdjson::ondemand::object payload = payload_result; + auto data_result = payload["data"]; + if (data_result.error()) + return error_price; + simdjson::ondemand::object data = data_result; + + simdjson::error_code price_error; + if (not data["depth"].error()) { + price_error = data["depth"]["price"].get(price_out); + } + else if (not data["trade"].error()) { + price_error = data["trade"]["price"].get(price_out); + } + else if (not data["depth_shot"].error()) { + continue; + } + + std::string_view currency_out_view, exchange_out_view; + const auto currency_error = data["currency"].get(currency_out_view); + const auto exchange_error = data["exchange_name"].get(exchange_out_view); + + if (price_error or currency_error or exchange_error) + return error_price; + + currency_out = currency_out_view; + exchange_out = exchange_out_view; + } while (false); + + return {price_out, std::move(currency_out), std::move(exchange_out)}; + } } -void ticker_price_wren (wren::VM& vm) { +void ticker_price_bitcoinity_wren (wren::VM& vm) { TickerPrice* const tp = wren::make_foreign_object(vm, "main"); - *tp = ticker_price(wren::get(vm, 1)); + *tp = ticker_price_bitoinity(wren::get(vm, 1), wren::get(vm, 2)); } class MyWrenConfiguration : public wren::DefConfiguration { public: - wren::foreign_method_t foreign_method_fn( - wren::VM* vm, - std::string_view module, - std::string_view class_name, - bool is_static, - std::string_view signature - ) { - if ("main" == module) { - if (is_static and "Bitcoinity" == class_name and "ticker(_)" == signature) { - return &ticker_price_wren; - } - else if ("TickerPrice" == class_name) { - if (not is_static and "price" == signature) - return wren::make_method_bindable<&TickerPrice::price>(); - else if (not is_static and "currency" == signature) - return wren::make_method_bindable<&TickerPrice::currency>(); - } - } - return nullptr; - } - wren::foreign_class_t foreign_class_fn( wren::VM* vm, std::string_view module, @@ -127,8 +219,14 @@ public: int main() { MyWrenConfiguration config; wren::VM vm(&config, nullptr); - vm.interpret("main", g_script); + vm.callback_manager() + .add_callback(true, "main", "Bitcoinity", "ticker(_,_)", &ticker_price_bitcoinity_wren) + .add_callback(false, "main", "TickerPrice", "price", wren::make_method_bindable<&TickerPrice::price>()) + .add_callback(false, "main", "TickerPrice", "currency", wren::make_method_bindable<&TickerPrice::currency>()) + .add_callback(false, "main", "TickerPrice", "is_valid", wren::make_method_bindable<&TickerPrice::is_valid>()) + .add_callback(false, "main", "TickerPrice", "exchange", wren::make_method_bindable<&TickerPrice::exchange>()); + vm.interpret("main", g_script); wren::call(vm, {"main", "the_app"}, "start"); return 0; diff --git a/src/meson.build b/src/meson.build index d4415a5..8a7e7ed 100644 --- a/src/meson.build +++ b/src/meson.build @@ -10,6 +10,10 @@ wrenpp_dep = dependency('wrenpp', version: '>=0.1.1', default_options: ['wren_with_rand=true'], ) +boost_dep = dependency('boost', version: '>=1.78.0', + modules: ['coroutine', 'filesystem', 'system', 'thread'], +) + simdjson_dep = dependency('simdjson', version: '>=0.5.0', fallback: ['simdjson', 'simdjson_dep'], ) @@ -24,6 +28,10 @@ executable(meson.project_name(), curlcpp_dep, wrenpp_dep, simdjson_dep, + boost_dep, ], cpp_args: compiler_opts, + include_directories: [ + beast_include, + ], ) diff --git a/subprojects/beast b/subprojects/beast new file mode 160000 index 0000000..9d23bec --- /dev/null +++ b/subprojects/beast @@ -0,0 +1 @@ +Subproject commit 9d23bec2bc523223a377582302bd28d04cb514ed diff --git a/subprojects/wrenpp b/subprojects/wrenpp index edd5f27..d985ebc 160000 --- a/subprojects/wrenpp +++ b/subprojects/wrenpp @@ -1 +1 @@ -Subproject commit edd5f27ab2013e7d768fead766274b1a9eab6075 +Subproject commit d985ebc417f80c59fe26200a9e1e677f54ccb9e9