XRC Mock Canister

Hello everyone,

I’m currently working on a project and would like to integrate the XRC (Exchange Rate Canister) to fetch the ICP-USD exchange rate. I noticed that the XRC demo is implemented in Motoko, but I’m looking for guidance or resources on how to achieve this in Rust.

also i noticed a mock canister inside xrc canister repo but i did not find for how to use it so could anyone share insights on setting up an XRC-like service specifically for Rust? Any example code or steps would be greatly appreciated.
@THLO
Thanks in advance!

1 Like

If you want to run your own instance of the XRC alongside your canister, you can pull it into your project using dfx deps.
Note that the XRC is meant to be run in a system subnet, so you need to configure your local replica accordingly.
For testing on mainnet, I’d recommend sending requests to the main XRC.

It doesn’t matter if your canister is written in Rust or Motoko. In both cases you’ll make the same request to the get_exchange_rate endpoint.

I hope this helps!

3 Likes


const XRC_CANISTER_ID: &str = “avqkn-guaaa-aaaaa-qaaea-cai”;

#[update]
async fn get_exchange_rate(symbol: String) → Result<ExchangeRate, String> {
let request = GetExchangeRateRequest {
base_asset: Asset {
symbol: symbol.clone(),
class: AssetClass::Cryptocurrency,
},
quote_asset: Asset {
symbol: “USDT”.to_string(),
class: AssetClass::Cryptocurrency,
},
timestamp: None,
};

let xrc_canister: Principal = Principal::from_text(XRC_CANISTER_ID).expect("Invalid principal");
let (result,): (GetExchangeRateResult,) = ic_cdk::api::call::call(xrc_canister, "get_exchange_rate", (request,))
    .await
    .map_err(|e| format!("Call failed: {:?}", e))?;

match result {
    GetExchangeRateResult::Ok(rate) => Ok(rate),
    GetExchangeRateResult::Err(err) => Err(format!("Error getting exchange rate: {:?}", err)),
}

} This what i did and i recieve the same error always not enough cycles , is what i implemented is a correct way ?

1 Like

Did you set the subnet type to “system” as I wrote above?

it always gives me error like this Running dfx start for version 0.20.1
Using project-specific network ‘local’ defined in /mnt/d/Mercatura/ICP/Mercx/dfx.json
thread ‘main’ panicked at rs/crypto/internal/crypto_service_provider/src/secret_key_store/proto_store.rs:120:14:
wrong crypto root permissions: “Crypto state directory /mnt/d/Mercatura/ICP/Mercx/.dfx/network/local/state/replicated_state/node-100/crypto has permissions 0o40777, allowing general access”
note: run with RUST_BACKTRACE=1 environment variable to display a backtrace
N.B i am a windows system

You don’t need to create your own XRC canister to get the ICP-USD rate. Just making sure you know that.

1 Like

The XRC charges a small fee for the service it provides. It costs 1B cycles per request. ic_cdk::api::call::call attaches no cycles to the call. You should use call_with_payment128 instead and attach 1B cycles to the call.

The replica is not built to run on Windows and doesn’t even run properly on WSL2. If you want to run it locally, use a dev container.

1 Like