From f3483594ac1a3d313b830676c2289d5615640fa2 Mon Sep 17 00:00:00 2001 From: swrup Date: Sun, 29 Mar 2026 19:38:22 +0200 Subject: [PATCH] have api.ml not depends on config.ml --- src/api.ml | 59 +++++++++++++++++++++++++----------------------- src/config.ml | 60 ++++++++++++++++--------------------------------- src/mte_info.ml | 23 ++++++++++--------- 3 files changed, 63 insertions(+), 79 deletions(-) diff --git a/src/api.ml b/src/api.ml index 40b1f386..ab10f1e8 100644 --- a/src/api.ml +++ b/src/api.ml @@ -135,12 +135,41 @@ module ErrorDetail = struct end module CurrencySpecification = struct + module Alt_unit_names = struct + open Syntax + module Int_map = Map.Make (Int) + module String_map = Map.Make (String) + + type t = string Int_map.t + + let jsont = + let of_string_map str_map = + str_map + |> String_map.to_list + |> list_map (fun (k, v) -> + match int_of_string_opt k with + | None -> Error "not an integer" + | Some k -> Ok (k, v)) + |> function + | Error e -> Jsont.Error.msg Jsont.Meta.none e + | Ok l -> Int_map.of_list l + in + let to_string_map int_map = + int_map + |> Int_map.to_list + |> List.map (fun (k, v) -> (string_of_int k, v)) + |> String_map.of_list + in + let string_map_jsont = Jsont.Object.as_string_map Jsont.string in + Jsont.map ~dec:of_string_map ~enc:to_string_map string_map_jsont + end + type t = { name: string; num_fractional_input_digits: int; num_fractional_normal_digits: int; num_fractional_trailing_zero_digits: int; - alt_unit_names: Config.Currency.Alt_unit_names.t; + alt_unit_names: Alt_unit_names.t; common_amounts: Amount.t list; } @@ -172,8 +201,7 @@ module CurrencySpecification = struct ~enc:num_fractional_normal_digits |> mem "num_fractional_trailing_zero_digits" Jsont.int ~enc:num_fractional_trailing_zero_digits - |> mem "alt_unit_names" Config.Currency.Alt_unit_names.jsont - ~enc:alt_unit_names + |> mem "alt_unit_names" Alt_unit_names.jsont ~enc:alt_unit_names |> mem "common_amounts" (Jsont.list Amount.jsont) ~enc:common_amounts |> finish end @@ -227,31 +255,6 @@ module ExchangeVersionResponse = struct |> finish end -let currency_specification = - let open Config.Currency in - CurrencySpecification. - { - name= v.name; - num_fractional_input_digits= v.fractional_input_digits; - num_fractional_normal_digits= v.fractional_normal_digits; - num_fractional_trailing_zero_digits= v.fractional_trailing_zero_digits; - alt_unit_names= Config.Currency.v.alt_unit_names; - common_amounts= []; - } - -let config = - ExchangeVersionResponse. - { - version= Libtool_version.mte_protocol_version; - name= "taler-exchange"; - currency= Config.currency; - currency_specification; - implementation= None; - shopping_url= None; - open_banking_gateway= None; - aml_spa_dialect= None; - } - module RsaDenominationKey = struct type t = { age_mask: int; diff --git a/src/config.ml b/src/config.ml index d80b284f..6a5a48f3 100644 --- a/src/config.ml +++ b/src/config.ml @@ -86,35 +86,6 @@ module Exchangedb_postgres = struct end module Currency = struct - module Alt_unit_names = struct - open Syntax - module Int_map = Map.Make (Int) - module String_map = Map.Make (String) - - type t = string Int_map.t - - let jsont = - let of_string_map str_map = - str_map - |> String_map.to_list - |> list_map (fun (k, v) -> - match int_of_string_opt k with - | None -> Error "not an integer" - | Some k -> Ok (k, v)) - |> function - | Error e -> Jsont.Error.msg Jsont.Meta.none e - | Ok l -> Int_map.of_list l - in - let to_string_map int_map = - int_map - |> Int_map.to_list - |> List.map (fun (k, v) -> (string_of_int k, v)) - |> String_map.of_list - in - let string_map_jsont = Jsont.Object.as_string_map Jsont.string in - Jsont.map ~dec:of_string_map ~enc:to_string_map string_map_jsont - end - type t = { enabled: [ `YES | `NO ]; code: string; @@ -122,7 +93,7 @@ module Currency = struct fractional_input_digits: int; fractional_normal_digits: int; fractional_trailing_zero_digits: int; - alt_unit_names: Alt_unit_names.t; + alt_unit_names: Api.CurrencySpecification.Alt_unit_names.t; } let currency_sections = @@ -142,24 +113,31 @@ module Currency = struct get "fractional_trailing_zero_digits" |> int; alt_unit_names= get "alt_unit_names" - |> Jsont_bytesrw.decode_string Alt_unit_names.jsont + |> Jsont_bytesrw.decode_string + Api.CurrencySpecification.Alt_unit_names.jsont |> unwrap_or_failure; } - let all_currencies = List.map parse_currency currency_sections - - let v = - match - List.find_opt (fun v -> v.code = Exchange.currency) all_currencies - with + let specification = + currency_sections + |> List.map parse_currency + |> List.find_opt (fun v -> v.code = Exchange.currency) + |> function | None -> failure "section `[currency-%s]` not found, currency `%s` is not defined" Exchange.currency Exchange.currency - | Some v -> ( - match v.enabled = `YES with - | false -> failure "currency `%s` is not enabled" Exchange.currency - | true -> v) + | Some v when v.enabled <> `YES -> + failure "currency `%s` is not enabled" Exchange.currency + | Some v -> + { + Api.CurrencySpecification.name= v.name; + num_fractional_input_digits= v.fractional_input_digits; + num_fractional_normal_digits= v.fractional_normal_digits; + num_fractional_trailing_zero_digits= v.fractional_trailing_zero_digits; + alt_unit_names= v.alt_unit_names; + common_amounts= []; + } end module Coin = struct diff --git a/src/mte_info.ml b/src/mte_info.ml index c696ba04..57a814bc 100644 --- a/src/mte_info.ml +++ b/src/mte_info.ml @@ -18,16 +18,19 @@ let seed req _server _env = let config req _server _env = Logs.info (fun m -> m "GET /config"); - let jsont = Api.ExchangeVersionResponse.jsont in - Respond.ok req jsont config + Respond.ok req ExchangeVersionResponse.jsont + ExchangeVersionResponse. + { + version= Libtool_version.mte_protocol_version; + name= "taler-exchange"; + currency= Config.currency; + currency_specification= Config.Currency.specification; + implementation= None; + shopping_url= None; + open_banking_gateway= None; + aml_spa_dialect= None; + } -(* not implemented: - - kyc - - wads - - account limits - - zero limited operations - - recoup - - extensions *) let mk_keys ~db_conn (module Keys : Keys.S) ~last_issue_date = let version = Libtool_version.mte_protocol_version in let base_url = Config.base_url in @@ -35,7 +38,7 @@ let mk_keys ~db_conn (module Keys : Keys.S) ~last_issue_date = let shopping_url = Config.shopping_url in let open_banking_gateway = Config.open_banking_gateway_url in let bank_compliance_language = Config.bank_compliance_language in - let currency_specification = Api.currency_specification in + let currency_specification = Config.Currency.specification in let tiny_amount = Config.tiny_amount in let stefan_abs = Config.stefan_abs in let stefan_log = Config.stefan_log in