diff --git a/src/app_sparkd/sparkd/md/rakurai/RakuraiDexDataHandler.cpp b/src/app_sparkd/sparkd/md/rakurai/RakuraiDexDataHandler.cpp index 605c6a5e6..cac229c6b 100644 --- a/src/app_sparkd/sparkd/md/rakurai/RakuraiDexDataHandler.cpp +++ b/src/app_sparkd/sparkd/md/rakurai/RakuraiDexDataHandler.cpp @@ -326,10 +326,25 @@ std::string build_security_desc( short_pool_id); } -domain::BookLevel to_book_level(const ConnectorBookLevel& source) { +double quantity_scale_from_decimals(std::uint8_t decimals) { + double scale = 1.0; + for (std::uint8_t i = 0; i < decimals; ++i) { + scale *= 10.0; + } + return scale; +} + +domain::BookLevel to_book_level( + const ConnectorBookLevel& source, + double quantity_scale) { domain::BookLevel level; level.price = domain::Price::from_double(source.price); - level.total_quantity = domain::Quantity(static_cast(source.quantity)); + const auto normalized_quantity = + quantity_scale > 0.0 + ? static_cast(source.quantity) / quantity_scale + : static_cast(source.quantity); + level.total_quantity = + domain::Quantity(static_cast(normalized_quantity)); level.direct_quantity = level.total_quantity; level.n_orders = 0; return level; @@ -500,6 +515,8 @@ class ListenerAdapter final : public ::rakurai::dex_connector::dex_listener { } pool_to_security_id_[pool_id] = it->second->id(); + pool_quantity_scales_[pool_id] = + quantity_scale_from_decimals(security->token_a.decimals); emitted.push_back(it->second.get()); } @@ -549,16 +566,21 @@ class ListenerAdapter final : public ::rakurai::dex_connector::dex_listener { pool_key); return; } + const auto quantity_scale_it = pool_quantity_scales_.find(pool_key); + const auto quantity_scale = + quantity_scale_it != pool_quantity_scales_.end() + ? quantity_scale_it->second + : 1.0; auto book = domain::Book::empty(it->second); const auto bid_depth = std::min(depth_limit_, bids.size()); const auto ask_depth = std::min(depth_limit_, asks.size()); for (std::size_t i = 0; i < bid_depth; ++i) { - book.bids()[i] = to_book_level(bids[i]); + book.bids()[i] = to_book_level(bids[i], quantity_scale); } for (std::size_t i = 0; i < ask_depth; ++i) { - book.asks()[i] = to_book_level(asks[i]); + book.asks()[i] = to_book_level(asks[i], quantity_scale); } listener_->on_book(book); @@ -601,6 +623,7 @@ class ListenerAdapter final : public ::rakurai::dex_connector::dex_listener { persistence::SqliteStore pool_alias_store_{"rakurai_pool_alias"}; std::unordered_map> securities_; std::unordered_map pool_to_security_id_; + std::unordered_map pool_quantity_scales_; }; } // namespace