#include"Boltz/Service.hpp" #include"Boltz/SwapInfo.hpp" #include"Boss/Mod/BoltzSwapper/ServiceModule.hpp" #include"Boss/Msg/AcceptSwapQuotation.hpp" #include"Boss/Msg/Block.hpp" #include"Boss/Msg/ProvideSwapQuotation.hpp" #include"Boss/Msg/SolicitSwapQuotation.hpp" #include"Boss/Msg/SwapCreation.hpp" #include"Boss/log.hpp" #include"Ev/Io.hpp" #include"S/Bus.hpp" #include"Util/make_unique.hpp" namespace Boss { namespace Mod { namespace BoltzSwapper { class ServiceModule::Impl { private: S::Bus& bus; std::unique_ptr service; std::unique_ptr blockheight; void start() { bus.subscribe([this](Msg::Block const& b) { if (!blockheight) blockheight = Util::make_unique(); *blockheight = b.height; return service->on_block(*blockheight); }); bus.subscribe([this](Msg::SolicitSwapQuotation const& q) { if (!blockheight) return Ev::lift(); auto solicitor = q.solicitor; auto amt = q.offchain_amount; return Ev::lift().then([this, amt]() { return service->get_quotation(amt); }).then([ this , solicitor ](std::unique_ptr fee) { if (!fee) return Ev::lift(); return bus.raise(Msg::ProvideSwapQuotation{ *fee, solicitor, this }); }); }); bus.subscribe([this](Msg::AcceptSwapQuotation const& q) { /* If not talking to us, ignore. */ if (q.provider != this) return Ev::lift(); if (!blockheight) { /* Should not happen. Just fail it. */ return bus.raise(Msg::SwapCreation{ false, "", Sha256::Hash(), 0, q.solicitor, this }); } auto amt = q.offchain_amount; auto addr = q.onchain_address; auto solicitor = q.solicitor; return Ev::lift().then([this, amt, addr]() { return service->swap(amt, addr, *blockheight); }).then([this, solicitor ](Boltz::SwapInfo res) { auto& invoice = res.invoice; auto timeout = res.timeout; auto& hash = res.hash; auto success = (invoice != ""); return bus.raise(Msg::SwapCreation{ success, std::move(invoice), std::move(hash), timeout, solicitor, this }); }); }); } public: Impl() =delete; explicit Impl( S::Bus& bus_ , std::unique_ptr service_ ) : bus(bus_) , service(std::move(service_)) { start(); } }; ServiceModule::ServiceModule(ServiceModule&&) =default; ServiceModule& ServiceModule::operator=(ServiceModule&&) =default; ServiceModule::~ServiceModule() =default; ServiceModule::ServiceModule ( S::Bus& bus , std::unique_ptr service ) : pimpl(Util::make_unique( bus , std::move(service) )) { } }}}