Exchange rate canister

From Internet Computer Wiki
Revision as of 10:53, 13 January 2023 by Thomas.locher (talk | contribs)
Jump to: navigation, search

Overview

The exchange rate canister (XRC) is a canister running on the uzr34 system subnet that provides exchange rates to requesting canisters. A request comprises a base asset, a quote asset, and an optional (UNIX epoch) timestamp. The base and quote asset can be any combination of cryptocurrency and fiat currency assets, for example, BTC/ICP, ICP/USD, or USD/EUR. The timestamp parameter makes it possible to request historic rates. If no timestamp is provided in the request, the rate for the current time is returned.

The XRC constitutes an on-chain oracle for exchange rates, which is particularly useful for DeFi applications but can further add value to any application that requires exchange rate information.

The cycle minting canister of the NNS will make use of the XRC to obtain up-to-date ICP/XDR rates, which it requires for the conversion of ICP to cycles.

Usage

The canister ID of the XRC is uf6dk-hyaaa-aaaaq-qaaaq-cai. It A request of the form

type GetExchangeRateRequest = record {
   base_asset: Asset;
   quote_asset: Asset;
   // An optional timestamp to get the rate for a specific time period.
   timestamp: opt nat64;
}; 

can be sent to the XRC, which replies with the following result:

type GetExchangeRateResult = variant {
   // Successfully retrieved the exchange rate from the cache or API calls.
   Ok: ExchangeRate;
   // Failed to retrieve the exchange rate due to invalid API calls, invalid timestamp, etc.
   Err: ExchangeRateError;
};

The full candid file can be found here.