New wren methods ticker_currency() and ticker_exchange()

This commit is contained in:
King_DuckZ 2022-05-14 07:54:13 +02:00
parent 6bd0dbb07e
commit 6cbe6ae47f
3 changed files with 57 additions and 8 deletions

View file

@ -24,11 +24,13 @@ R"wren(import "ticker_price" for TickerPrice
class Bitcoinity {
foreign static ticker(currency, exchange)
foreign static ticker_quick()
foreign static ticker_currency(currency)
foreign static ticker_exchange(exchange)
}
)wren"};
TickerPrice bitcoinity_ticker_price (std::string_view exchange, std::string_view currency, bool exact_match) {
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{
@ -62,8 +64,22 @@ TickerPrice bitcoinity_ticker_price (std::string_view exchange, std::string_view
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");
if (exact_match and (currency_out != currency or exchange_out != exchange))
continue; //wait 'til we get a better match
switch (match_type) {
case BitcoinityMatchType::Any:
break;
case BitcoinityMatchType::Exact:
if (currency_out != currency or exchange_out != exchange)
continue; //wait 'til we get a better match
break;
case BitcoinityMatchType::CurrencyOnly:
if (currency_out != currency)
continue;
break;
case BitcoinityMatchType::ExchangeOnly:
if (exchange_out != exchange)
continue;
break;
}
return {price_out, std::string(currency_out), std::string(exchange_out)};
} while (true);

View file

@ -20,10 +20,17 @@
#include "ticker_price.hpp"
namespace duck {
enum class BitcoinityMatchType {
Any,
Exact,
CurrencyOnly,
ExchangeOnly
};
TickerPrice bitcoinity_ticker_price (
std::string_view exchange,
std::string_view currency,
bool exact_match
BitcoinityMatchType match_type
);
extern const std::string_view g_bitcoinity_wren_module;

View file

@ -23,6 +23,9 @@
#include <algorithm>
namespace {
constexpr std::string_view g_dummy_currency {"EUR"};
constexpr std::string_view g_dummy_exchange {"kraken"};
constexpr char g_script[] =
R"script(
import "bitcoinity" for Bitcoinity
@ -37,6 +40,9 @@ class App {
var price_quick = Bitcoinity.ticker_quick()
System.print("Ticker (quick): %(price_quick.price) %(price_quick.currency) on %(price_quick.exchange)")
var price_currency = Bitcoinity.ticker_currency(_currency)
System.print("Ticker (%(_currency)): %(price_currency.price) %(price_currency.currency) on %(price_currency.exchange)")
var price = Bitcoinity.ticker(_exchange, _currency)
System.print("Ticker (%(_exchange), %(_currency)): %(price.price) %(price.currency) on %(price.exchange)")
}
@ -48,19 +54,37 @@ var the_app = App.new("kraken", "USD")
void quick_ticker_price_bitcoinity_wren (wren::VM& vm) {
using duck::TickerPrice;
using duck::bitcoinity_ticker_price;
using duck::BitcoinityMatchType;
TickerPrice* const tp = wren::make_foreign_object<TickerPrice>(vm, "ticker_price");
constexpr bool exact_match = false;
*tp = bitcoinity_ticker_price("kraken", "EUR", exact_match);
*tp = bitcoinity_ticker_price(g_dummy_exchange, g_dummy_currency, BitcoinityMatchType::Any);
}
void ticker_price_bitcoinity_wren (wren::VM& vm) {
using duck::TickerPrice;
using duck::bitcoinity_ticker_price;
using duck::BitcoinityMatchType;
TickerPrice* const tp = wren::make_foreign_object<TickerPrice>(vm, "ticker_price");
constexpr bool exact_match = true;
*tp = bitcoinity_ticker_price(wren::get<const char*>(vm, 1), wren::get<const char*>(vm, 2), exact_match);
*tp = bitcoinity_ticker_price(wren::get<const char*>(vm, 1), wren::get<const char*>(vm, 2), BitcoinityMatchType::Exact);
}
void currency_ticker_price_bitcoinity_wren (wren::VM& vm) {
using duck::TickerPrice;
using duck::bitcoinity_ticker_price;
using duck::BitcoinityMatchType;
TickerPrice* const tp = wren::make_foreign_object<TickerPrice>(vm, "ticker_price");
*tp = bitcoinity_ticker_price(g_dummy_exchange, wren::get<const char*>(vm, 1), BitcoinityMatchType::CurrencyOnly);
}
void exchange_ticker_price_bitcoinity_wren (wren::VM& vm) {
using duck::TickerPrice;
using duck::bitcoinity_ticker_price;
using duck::BitcoinityMatchType;
TickerPrice* const tp = wren::make_foreign_object<TickerPrice>(vm, "ticker_price");
*tp = bitcoinity_ticker_price(wren::get<const char*>(vm, 1), g_dummy_currency, BitcoinityMatchType::ExchangeOnly);
}
class MyWrenConfiguration : public wren::DefConfiguration {
@ -102,6 +126,8 @@ int main() {
vm.callback_manager()
.add_callback(true, "bitcoinity", "Bitcoinity", "ticker(_,_)", &ticker_price_bitcoinity_wren)
.add_callback(true, "bitcoinity", "Bitcoinity", "ticker_quick()", &quick_ticker_price_bitcoinity_wren)
.add_callback(true, "bitcoinity", "Bitcoinity", "ticker_currency(_)", &currency_ticker_price_bitcoinity_wren)
.add_callback(true, "bitcoinity", "Bitcoinity", "ticker_exchange(_)", &exchange_ticker_price_bitcoinity_wren)
.add_callback(false, "ticker_price", "TickerPrice", "price", wren::make_method_bindable<&TickerPrice::price>())
.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>())