-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlib.rs
68 lines (57 loc) · 2.01 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use std::{cell::RefCell, sync::Arc};
use candid::{self, CandidType, Deserialize, Principal};
use canfund::{
manager::{
options::{CyclesThreshold, FundManagerOptions, FundStrategy},
RegisterOpts,
},
operations::fetch::FetchCyclesBalanceFromCanisterStatus,
FundManager,
};
use ic_cdk::post_upgrade;
use ic_cdk_macros::init;
thread_local! {
/// Monitor the cycles of canisters and top up if necessary.
pub static FUND_MANAGER: RefCell<FundManager> = RefCell::new(FundManager::new());
}
#[derive(CandidType, Deserialize)]
pub struct FundingConfig {
pub funded_canister_ids: Vec<Principal>,
}
#[init]
fn initialize(config: FundingConfig) {
start_canister_cycles_monitoring(config);
}
#[post_upgrade]
fn post_upgrade(config: FundingConfig) {
start_canister_cycles_monitoring(config);
}
pub fn start_canister_cycles_monitoring(config: FundingConfig) {
if config.funded_canister_ids.is_empty() {
return;
}
FUND_MANAGER.with(|fund_manager| {
let mut fund_manager = fund_manager.borrow_mut();
let fund_manager_options = FundManagerOptions::new()
.with_interval_secs(12 * 60 * 60) // twice a day
.with_strategy(FundStrategy::BelowThreshold(
CyclesThreshold::new()
.with_min_cycles(400_000_000_000)
.with_fund_cycles(250_000_000_000),
));
fund_manager.with_options(fund_manager_options);
for canister_id in config.funded_canister_ids {
fund_manager.register(
canister_id,
RegisterOpts::new()
.with_cycles_fetcher(Arc::new(FetchCyclesBalanceFromCanisterStatus::new()))
.with_strategy(FundStrategy::BelowThreshold(
CyclesThreshold::new()
.with_min_cycles(400_000_000_000)
.with_fund_cycles(500_000_000_000),
)),
);
}
fund_manager.start();
});
}