Skip to content

Commit

Permalink
Remove duplicate code (#5637)
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles authored Apr 5, 2024
1 parent c5d66e8 commit 83bbf17
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 25 deletions.
6 changes: 6 additions & 0 deletions backend/canisters/cycles_dispenser/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [unreleased]

### Changed

- Remove duplicate code ([#5637](https://github.com/open-chat-labs/open-chat/pull/5637))

## [[2.0.1126](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.1126-cycles_dispenser)] - 2024-04-05

### Added

- Automatically add SNS controlled canisters to the whitelist ([#5625](https://github.com/open-chat-labs/open-chat/pull/5625))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::updates::c2c_request_cycles::top_up_canister;
use crate::{mutate_state, read_state};
use sns_root_canister::get_sns_canisters_summary::CanisterSummary;
use std::time::Duration;
use types::{CanisterId, Cycles, Empty};
use utils::canister::deposit_cycles;
use utils::canister_timers::run_now_then_interval;

const INTERVAL: Duration = Duration::from_secs(24 * 60 * 60); // 1 day
Expand Down Expand Up @@ -51,7 +51,7 @@ async fn run_async(canister_id: CanisterId) {
let top_up_amount = read_state(|state| state.data.max_top_up_amount);

for canister_id in to_top_up {
let _ = top_up_canister(canister_id, top_up_amount).await;
let _ = deposit_cycles(canister_id, top_up_amount).await;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use crate::{mutate_state, State};
use canister_tracing_macros::trace;
use cycles_dispenser_canister::c2c_request_cycles::{Response::*, *};
use ic_cdk::api::call::CallResult;
use ic_cdk_macros::update;
use std::cmp::min;
use tracing::{error, info};
use types::{CanisterId, Cycles, Milliseconds, TimestampMillis};
use utils::canister::deposit_cycles;

Expand All @@ -16,7 +14,7 @@ async fn c2c_request_cycles(args: Args) -> Response {
Err(response) => return response,
};

let result = top_up_canister(canister_id, amount).await;
let result = deposit_cycles(canister_id, amount).await;

mutate_state(|state| commit(&canister_id, result.is_ok().then_some(amount), state));

Expand Down Expand Up @@ -63,26 +61,6 @@ fn commit(canister_id: &CanisterId, top_up_amount: Option<Cycles>, state: &mut S
}
}

pub(crate) async fn top_up_canister(canister_id: CanisterId, amount: Cycles) -> CallResult<()> {
let response = deposit_cycles(canister_id, amount).await;

if let Err((code, msg)) = response {
error!(
canister_id = canister_id.to_string().as_str(),
error_code = code as u8,
error_message = msg.as_str(),
"Error calling 'deposit_cycles'"
);
Err((code, msg))
} else {
info!(
canister_id = canister_id.to_string().as_str(),
amount, "Topped up canister with cycles"
);
Ok(())
}
}

fn calc_required_wait_period(
latest_top_up: Option<TimestampMillis>,
min_interval: Milliseconds,
Expand Down

0 comments on commit 83bbf17

Please sign in to comment.