Interpret a scaled quote from the ICP Exchange Rate Canister

The XRC returns an integer together with metadata describing its scale. Storing only the integer loses information needed to display or compare the quote.

Read the pair in order

For the illustrative ICP/USD response below, divide the integer by ten to the power of metadata.decimals. The result is dollars per ICP. Use a decimal type or retain the rational numerator and denominator when settling small amounts. Inverting the pair also inverts its units.

base = ICP, quote = USD
rate = 4725000000, metadata.decimals = 9
USD per ICP = 4.725

Retain observation quality

Keep the returned timestamp, base and quote asset classes and source-count metadata alongside the rate. A successful historical lookup can still be stale for a live conversion. Define a maximum observation age and a policy for missing or sparse source coverage before accepting a quote.

Sources