MCPcopy Create free account
hub / github.com/dfinity/orbit / check_balance_before_transfer

Function check_balance_before_transfer

libs/orbit-essentials/src/utils/cycles.rs:5–23  ·  view source on GitHub ↗
(transfer_amount: u128)

Source from the content-addressed store, hash-verified

3use ic_cdk::id;
4
5pub async fn check_balance_before_transfer(transfer_amount: u128) -> Result<(), String> {
6 let self_id = id();
7 let status = canister_status(CanisterIdRecord {
8 canister_id: self_id,
9 })
10 .await
11 .map_err(|(_, err)| err)?
12 .0;
13 // enough cycles to keep the canister unfrozen for its freezing threshold duration
14 let min_balance =
15 status.idle_cycles_burned_per_day * status.settings.freezing_threshold * 2_u64 / 86_400u64;
16 if canister_balance() < min_balance + transfer_amount {
17 let err = format!(
18 "Canister {self_id} has insufficient cycles balance to transfer {transfer_amount} cycles."
19 );
20 return Err(err);
21 }
22 Ok(())
23}

Callers 2

deploy_stationMethod · 0.85
create_canisterFunction · 0.85

Calls 3

idFunction · 0.85
canister_balanceFunction · 0.85
canister_statusFunction · 0.50

Tested by

no test coverage detected