#include"Bitcoin/Tx.hpp" #include"Boss/Mod/BoltzSwapper/Env.hpp" #include"Boss/Mod/Rpc.hpp" #include"Boss/Msg/Init.hpp" #include"Boss/log.hpp" #include"Ev/Io.hpp" #include"Ev/yield.hpp" #include"Jsmn/Object.hpp" #include"Json/Out.hpp" #include"S/Bus.hpp" namespace Boss { namespace Mod { namespace BoltzSwapper { void Env::start() { bus.subscribe([this](Msg::Init const& init) { rpc = &init.rpc; return Ev::lift(); }); } Ev::Io Env::get_feerate() { return Ev::yield().then([this]() { /* Should not happen. */ if (!rpc) return Ev::lift(2000); return rpc->command("feerates" , Json::Out() .start_object() .field("style", "perkw") .end_object() ).then([](Jsmn::Object res) { auto rate = res["perkw"]["opening"]; return Ev::lift(double(rate)); }); }); } Ev::Io Env::broadcast_tx(Bitcoin::Tx n_tx) { auto tx = std::make_shared(std::move(n_tx)); return Ev::yield().then([this, tx]() { /* Should not happen. */ if (!rpc) return Ev::lift(false); return rpc->command( "sendrawtransaction" , Json::Out() .start_object() .field( "tx" , std::string(*tx) ) .field( "allowhighfees" , true ) .end_object() ).then([](Jsmn::Object res) { return Ev::lift(true); }).catching([](RpcError const& _) { return Ev::lift(false); }); }); } Ev::Io Env::logd(std::string msg) { return Boss::log( bus, Debug , "%s", msg.c_str() ); } Ev::Io Env::loge(std::string msg) { return Boss::log( bus, Warn , "%s", msg.c_str() ); } }}}