diff --git a/script/main.wren b/script/main.wren index 081a03e..3226b40 100644 --- a/script/main.wren +++ b/script/main.wren @@ -8,13 +8,13 @@ class App { start() { var price_quick = Bitcoinity.ticker_quick() - System.print("Ticker (quick): %(price_quick.price) %(price_quick.currency) on %(price_quick.exchange)") + System.print("Ticker (quick): %(price_quick.price) %(price_quick.currency) on %(price_quick.exchange) timestamp %(price_quick.timestamp)") var price_currency = Bitcoinity.ticker_currency(_currency) - System.print("Ticker (%(_currency)): %(price_currency.price) %(price_currency.currency) on %(price_currency.exchange)") + System.print("Ticker (%(_currency)): %(price_currency.price) %(price_currency.currency) on %(price_currency.exchange) timestamp %(price_currency.timestamp)") var price = Bitcoinity.ticker(_exchange, _currency) - System.print("Ticker (%(_exchange), %(_currency)): %(price.price) %(price.currency) on %(price.exchange)") + System.print("Ticker (%(_exchange), %(_currency)): %(price.price) %(price.currency) on %(price.exchange) timestamp %(price.timestamp)") } } diff --git a/src/bitcoinity_payload.hpp b/src/bitcoinity_payload.hpp index bb44ae2..eb934e8 100644 --- a/src/bitcoinity_payload.hpp +++ b/src/bitcoinity_payload.hpp @@ -19,6 +19,7 @@ #include #include +#include namespace duck { namespace detail { @@ -76,6 +77,11 @@ public: return {}; } + template + std::optional get_uint64 (Keys&&... keys) const { + return this->get(std::forward(keys)...); + } + template bool has_key (Keys&&... keys) const { return !!this->get(std::forward(keys)...); diff --git a/src/bitcoinity_ticker_price.cpp b/src/bitcoinity_ticker_price.cpp index 6aea42d..4cb1f56 100644 --- a/src/bitcoinity_ticker_price.cpp +++ b/src/bitcoinity_ticker_price.cpp @@ -21,6 +21,7 @@ #include #include +#include namespace duck { @@ -75,8 +76,6 @@ std::string make_user_agent_string() { } //unnamed namespace TickerPrice bitcoinity_ticker_price (std::string_view exchange, std::string_view currency, BitcoinityMatchType match_type) { - static const TickerPrice error_price {0.0, "", ""}; - duck::BitcoinityReader bitcoinity{ make_user_agent_string(), exchange, @@ -90,6 +89,7 @@ TickerPrice bitcoinity_ticker_price (std::string_view exchange, std::string_view // {"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"} double price_out; + std::uint64_t timestamp_out = 0; std::optional price; auto msg = bitcoinity.read(); @@ -106,6 +106,10 @@ TickerPrice bitcoinity_ticker_price (std::string_view exchange, std::string_view continue; //keep trying, sometimes I get a message like: //{"event":"new_msg","payload":{"data":{"connected_count":1355}},"ref":null,"topic":"webs:markets"} + auto timestamp = msg.payload.get_uint64("data", "trade", "date"); + if (timestamp) + timestamp_out = *timestamp; + std::string_view currency_out = msg.payload.get_string_or_empty("data", "currency"); std::string_view exchange_out = msg.payload.get_string_or_empty("data", "exchange_name"); switch (match_type) { @@ -125,7 +129,12 @@ TickerPrice bitcoinity_ticker_price (std::string_view exchange, std::string_view break; } - return {price_out, std::string(currency_out), std::string(exchange_out)}; + return TickerPrice{ + price_out, + timestamp_out, + std::string(currency_out), + std::string(exchange_out) + }; } while (true); } diff --git a/src/ticker_price.cpp b/src/ticker_price.cpp index 5967629..1aaac4e 100644 --- a/src/ticker_price.cpp +++ b/src/ticker_price.cpp @@ -27,14 +27,21 @@ R"wren(foreign class TickerPrice { construct new() { } foreign price + foreign timestamp foreign currency foreign exchange foreign is_valid } )wren"}; -TickerPrice::TickerPrice (double price, std::string&& currency, std::string&& exchange) : +TickerPrice::TickerPrice ( + double price, + std::uint64_t timestamp, + std::string&& currency, + std::string&& exchange +) : m_price(price), + m_timestamp(timestamp), m_currency(std::move(currency)), m_exchange(std::move(exchange)) {} @@ -43,6 +50,10 @@ void TickerPrice::set_price (double price) { m_price = price; } +void TickerPrice::set_timestamp(std::uint64_t timestamp) { + m_timestamp = timestamp; +} + void TickerPrice::set_currency (std::string&& currency) { m_currency = std::move(currency); } @@ -55,6 +66,10 @@ double TickerPrice::price() const noexcept { return m_price; } +std::uint64_t TickerPrice::timestamp() const noexcept { + return m_timestamp; +} + const char* TickerPrice::currency() const noexcept { return m_currency.c_str(); } @@ -70,6 +85,7 @@ bool TickerPrice::is_valid() const noexcept { void register_ticker_price_to_wren(wren::VM& vm) { vm.callback_manager() .add_callback(false, "ticker_price", "TickerPrice", "price", wren::make_method_bindable<&TickerPrice::price>()) + .add_callback(false, "ticker_price", "TickerPrice", "timestamp", wren::make_method_bindable<&TickerPrice::timestamp>()) .add_callback(false, "ticker_price", "TickerPrice", "currency", wren::make_method_bindable<&TickerPrice::currency>()) .add_callback(false, "ticker_price", "TickerPrice", "is_valid", wren::make_method_bindable<&TickerPrice::is_valid>()) .add_callback(false, "ticker_price", "TickerPrice", "exchange", wren::make_method_bindable<&TickerPrice::exchange>()); diff --git a/src/ticker_price.hpp b/src/ticker_price.hpp index 78265da..a7b9774 100644 --- a/src/ticker_price.hpp +++ b/src/ticker_price.hpp @@ -18,6 +18,7 @@ #pragma once #include +#include namespace wren { class VM; @@ -27,18 +28,21 @@ namespace duck { class TickerPrice { public: TickerPrice() = default; - TickerPrice (double price, std::string&& currency, std::string&& exchange); + TickerPrice (double price, std::uint64_t timestamp, std::string&& currency, std::string&& exchange); void set_price (double price); + void set_timestamp (std::uint64_t timestamp); void set_currency (std::string&& currency); void set_exchange (std::string&& exchange); double price() const noexcept; + std::uint64_t timestamp() const noexcept; const char* currency() const noexcept; const char* exchange() const noexcept; bool is_valid() const noexcept; private: double m_price{}; + std::uint64_t m_timestamp{}; std::string m_currency; std::string m_exchange; };