180 lines
5.5 KiB
C++
180 lines
5.5 KiB
C++
/* Copyright 2022, Michele Santullo
|
|
* This file is part of duckticker.
|
|
*
|
|
* Wrenpp is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* Wrenpp is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with duckticker. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "websocket_reader.hpp"
|
|
#include <simdjson.h>
|
|
#include <wrenpp/vm_fun.hpp>
|
|
#include <wrenpp/def_configuration.hpp>
|
|
#include <wrenpp/callback_manager.hpp>
|
|
#include <utility>
|
|
|
|
namespace {
|
|
constexpr char g_script[] =
|
|
R"script(
|
|
foreign class TickerPrice {
|
|
construct new() { }
|
|
|
|
foreign price
|
|
foreign currency
|
|
foreign exchange
|
|
foreign is_valid
|
|
}
|
|
|
|
class Bitcoinity {
|
|
foreign static ticker(currency, exchange)
|
|
}
|
|
|
|
class App {
|
|
construct new(currency, exchange) {
|
|
_currency = currency
|
|
_exchange = exchange
|
|
}
|
|
|
|
start() {
|
|
var price = Bitcoinity.ticker(_currency, _exchange)
|
|
System.print("Ticker: %(price.price) %(price.currency) on %(price.exchange)")
|
|
}
|
|
}
|
|
|
|
var the_app = App.new("USD", "kraken")
|
|
)script";
|
|
|
|
|
|
class TickerPrice {
|
|
public:
|
|
TickerPrice() = default;
|
|
TickerPrice (double price, std::string&& currency, std::string&& exchange) :
|
|
m_price(price),
|
|
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_bitoinity (std::string_view currency, std::string_view exchange) {
|
|
static const TickerPrice error_price {0.0, "", ""};
|
|
|
|
duck::WebsocketReader websocket{"bitcoinity.org",
|
|
"80",
|
|
"/webs_bridge/websocket",
|
|
std::string{duck::WebsocketReader::BeastVersionString} + " 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 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_bitcoinity_wren (wren::VM& vm) {
|
|
TickerPrice* const tp = wren::make_foreign_object<TickerPrice>(vm, "main");
|
|
*tp = ticker_price_bitoinity(wren::get<const char*>(vm, 1), wren::get<const char*>(vm, 2));
|
|
}
|
|
|
|
class MyWrenConfiguration : public wren::DefConfiguration {
|
|
public:
|
|
wren::foreign_class_t foreign_class_fn(
|
|
wren::VM* vm,
|
|
std::string_view module,
|
|
std::string_view class_name
|
|
) {
|
|
if (module == "main" and class_name == "TickerPrice")
|
|
return wren::make_foreign_class<TickerPrice>();
|
|
else
|
|
return {nullptr, nullptr};
|
|
}
|
|
};
|
|
} //unnamed namespace
|
|
|
|
int main() {
|
|
MyWrenConfiguration config;
|
|
wren::VM vm(&config, nullptr);
|
|
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<void>(vm, {"main", "the_app"}, "start");
|
|
|
|
return 0;
|
|
}
|