dfinity / exchange-rate-canister

The exchange rate canister (XRC) makes use of the HTTP requests feature to provide exchange rates as a service to the IC.
Apache License 2.0
37 stars 10 forks source link

bugfix: Properly assign the forex timestamp when multiplying rates #183

Closed dfinity-ryancroote closed 1 year ago

dfinity-ryancroote commented 1 year ago

When multiplying QueriedExchangeRates, the forex timestamp was only assigned when the rates both had a forex timestamp that were equal. This would result in the following:

(
  variant {
    Ok = record {
      metadata = record {
        decimals = 9 : nat32;
        forex_timestamp = null;
        quote_asset_num_received_rates = 1 : nat64;
        base_asset_num_received_rates = 5 : nat64;
        base_asset_num_queried_sources = 6 : nat64;
        standard_deviation = 5_926_094 : nat64;
        quote_asset_num_queried_sources = 7 : nat64;
      };
      rate = 3_798_391_839 : nat64;
      timestamp = 1_678_321_020 : nat64;
      quote_asset = record {
        class = variant { FiatCurrency };
        symbol = "CXDR";
      };
      base_asset = record {
        class = variant { Cryptocurrency };
        symbol = "ICP";
      };
    }
  },
)

This PR updates the logic so that if one rate has a timestamp or both rates have the same timestamp, the forex timestamp is carried over to the result.

(
  variant {
    Ok = record {
      metadata = record {
        decimals = 9 : nat32;
        forex_timestamp = opt (1_678_320_000 : nat64);
        quote_asset_num_received_rates = 1 : nat64;
        base_asset_num_received_rates = 5 : nat64;
        base_asset_num_queried_sources = 6 : nat64;
        standard_deviation = 5_926_094 : nat64;
        quote_asset_num_queried_sources = 7 : nat64;
      };
      rate = 3_798_391_839 : nat64;
      timestamp = 1_678_321_020 : nat64;
      quote_asset = record {
        class = variant { FiatCurrency };
        symbol = "CXDR";
      };
      base_asset = record {
        class = variant { Cryptocurrency };
        symbol = "ICP";
      };
    }
  },
)