Exchange: triangulation, dated rates, provider trait, bid/ask #45

Closed
opened 2026-08-17 13:37:36 +00:00 by rosa · 1 comment
Owner

Problem

Exchange stores one undated rate per pair and only converts through a directly stored pair. Real rate handling needs more:

  • Rates change over time; a table with no dates cannot answer "what was the rate on the invoice date".
  • If the table has EUR/USD and USD/JPY, converting EUR to JPY fails even though ExchangeRate::cross_with can already derive the cross rate.
  • Real quotes are two-sided (bid/ask); a single number silently averages or picks a side.
  • Rates come from external sources, but there is no trait seam for plugging one in.

Proposal

Four separable pieces, roughly in value order:

  1. Triangulation: convert falls back to a path through a configured base currency (or any shared currency) when the direct pair is missing, built on cross_with.
  2. Dated rates: rates carry an as-of date; lookups take a date and return the rate in effect. Possibly a separate HistoricalExchange so the simple table stays simple.
  3. RateProvider trait: a trait for fetching rates, with Exchange as the offline/cached implementation. The crate stays I/O-free; async and HTTP live in the caller or companion crates.
  4. Bid/ask: a two-sided rate type; convert picks the correct side for the direction. The Side enum already exists.

Each piece can land as its own PR; this issue tracks the direction.

## Problem `Exchange` stores one undated rate per pair and only converts through a directly stored pair. Real rate handling needs more: - Rates change over time; a table with no dates cannot answer "what was the rate on the invoice date". - If the table has EUR/USD and USD/JPY, converting EUR to JPY fails even though `ExchangeRate::cross_with` can already derive the cross rate. - Real quotes are two-sided (bid/ask); a single number silently averages or picks a side. - Rates come from external sources, but there is no trait seam for plugging one in. ## Proposal Four separable pieces, roughly in value order: 1. **Triangulation**: `convert` falls back to a path through a configured base currency (or any shared currency) when the direct pair is missing, built on `cross_with`. 2. **Dated rates**: rates carry an as-of date; lookups take a date and return the rate in effect. Possibly a separate `HistoricalExchange` so the simple table stays simple. 3. **`RateProvider` trait**: a trait for fetching rates, with `Exchange` as the offline/cached implementation. The crate stays I/O-free; async and HTTP live in the caller or companion crates. 4. **Bid/ask**: a two-sided rate type; `convert` picks the correct side for the direction. The `Side` enum already exists. Each piece can land as its own PR; this issue tracks the direction.
Author
Owner

Split into four issues, one per piece: #47 (triangulation), #48 (dated rates), #49 (rate provider trait), #50 (two-sided quotes). Each carries the open design questions for that piece.

Split into four issues, one per piece: #47 (triangulation), #48 (dated rates), #49 (rate provider trait), #50 (two-sided quotes). Each carries the open design questions for that piece.
rosa closed this issue 2026-08-18 00:40:06 +00:00
Sign in to join this conversation.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
rosa/lucre#45
No description provided.