Skip to content

Commit

Permalink
feat: add a callback for funding execution
Browse files Browse the repository at this point in the history
This callback allows for fetching metrics after a funding round is completed
  • Loading branch information
jedna committed Sep 10, 2024
1 parent 716fbca commit ac6b842
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
10 changes: 10 additions & 0 deletions canfund-rs/src/manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,9 @@ impl FundManager {
}
}
}

// Execute funding callback after the canisters have been funded.
manager.borrow().funding_callback();
}

/// Fetches the cycles balance for the provided canisters and calculates the needed cycles to fund them.
Expand Down Expand Up @@ -419,6 +422,13 @@ impl FundManagerCore {
pub fn unregister(&mut self, canister_id: CanisterId) -> Option<CanisterRecord> {
self.canisters.remove(&canister_id)
}

/// Executes the funding callback if it is set in the options.
pub fn funding_callback(&self) {
if let Some(funding_callback) = self.options.funding_callback() {
funding_callback(self.canisters.values().cloned().collect());
}
}
}

/// Calculates the needed cycles to fund the canister based on the current, previous cycles balance and
Expand Down
20 changes: 19 additions & 1 deletion canfund-rs/src/manager/options.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use std::{fmt::Debug, sync::Arc};
use std::{fmt::Debug, rc::Rc, sync::Arc};

use candid::Principal;
use ic_ledger_types::AccountIdentifier;

use crate::operations::obtain::ObtainCycles;

use super::record::CanisterRecord;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EstimatedRuntime {
/// The estimated min runtime in seconds to trigger the funding operation.
Expand Down Expand Up @@ -181,6 +183,8 @@ pub struct CycleMintingOptions {
pub icp_account_id: AccountIdentifier,
}

pub type ObserverCallback = Rc<dyn Fn(Vec<CanisterRecord>) + Send + Sync>;

/// The options when initializing the fund manager.
#[derive(Clone)]
pub struct FundManagerOptions {
Expand All @@ -196,6 +200,8 @@ pub struct FundManagerOptions {
strategy: FundStrategy,
/// Obtain cycles options to handle the funding canister balance getting low.
obtain_cycles_options: Option<ObtainCyclesOptions>,
/// Funding callback is executed after a funding round is completed.
funding_callback: Option<ObserverCallback>,
}

impl Default for FundManagerOptions {
Expand All @@ -207,6 +213,7 @@ impl Default for FundManagerOptions {
strategy: FundStrategy::default(),
delayed_start: false,
obtain_cycles_options: None,
funding_callback: None,
}
}
}
Expand Down Expand Up @@ -249,6 +256,12 @@ impl FundManagerOptions {
self
}

/// Set a callback to be executed after a round of funding.
pub fn with_funding_callback(mut self, callback: ObserverCallback) -> Self {
self.funding_callback = Some(callback);
self
}

/// Get the interval in secs to track the canister balance.
pub fn interval_secs(&self) -> u64 {
self.interval_secs
Expand All @@ -273,6 +286,11 @@ impl FundManagerOptions {
pub fn delayed_start(&self) -> bool {
self.delayed_start
}

/// Get the funding callback to call when a funding round is completed.
pub fn funding_callback(&self) -> Option<ObserverCallback> {
self.funding_callback.clone()
}
}

#[cfg(test)]
Expand Down

0 comments on commit ac6b842

Please sign in to comment.