Skip to content

Commit d8a8696

Browse files
committed
fix: Automatically refresh local ledger when pulling new data
1 parent 3614550 commit d8a8696

File tree

6 files changed

+36
-7
lines changed

6 files changed

+36
-7
lines changed

cli/src/argparse.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ pub struct AccountArgs {
7676
#[arg(long, requires = "identity")]
7777
pub balance: bool,
7878

79+
/// List all accounts in the local ledger
80+
#[arg(long, visible_aliases = ["list-accounts"])]
81+
pub list_all: bool,
82+
7983
/// Transfer funds to another account
8084
#[arg(long, requires = "identity")]
8185
pub transfer_to: Option<String>,
@@ -163,6 +167,10 @@ pub struct LedgerLocalArgs {
163167
/// List ledger entries
164168
#[arg(long)]
165169
pub list_entries: bool,
170+
171+
/// List all accounts in the local ledger
172+
#[arg(long)]
173+
pub list_accounts: bool,
166174
}
167175

168176
#[derive(Subcommand, PartialEq)]

cli/src/commands/account.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use crate::argparse::AccountArgs;
2+
use crate::identity::{list_identities, ListIdentityType};
23
use crate::ledger::handle_funds_transfer;
4+
use crate::LedgerMap;
35
use candid::Principal as IcPrincipal;
46
use dcc_common::{
57
account_balance_get_as_string, DccIdentity, IcrcCompatibleAccount, TokenAmountE9s,
@@ -12,7 +14,12 @@ pub async fn handle_account_command(
1214
network_url: &str,
1315
ledger_canister_id: IcPrincipal,
1416
identity: Option<String>,
17+
ledger_local: &LedgerMap,
1518
) -> Result<(), Box<dyn std::error::Error>> {
19+
if account_args.list_all {
20+
return list_identities(ledger_local, ListIdentityType::All, true);
21+
}
22+
1623
let identity = identity.expect("Identity must be specified for this command, use --identity");
1724
let dcc_id = DccIdentity::load_from_dir(&PathBuf::from(&identity))?;
1825

cli/src/commands/ledger.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::argparse::{LedgerLocalArgs, LedgerRemoteCommands};
2+
use crate::identity::{list_identities, ListIdentityType};
23
use base64::engine::general_purpose::STANDARD as BASE64;
34
use base64::Engine;
45
use candid::{Decode, Encode};
@@ -15,7 +16,9 @@ pub async fn handle_ledger_local_command(
1516
local_args: LedgerLocalArgs,
1617
ledger_local: LedgerMap,
1718
) -> Result<(), Box<dyn std::error::Error>> {
18-
if local_args.list_entries {
19+
if local_args.list_accounts {
20+
return list_identities(&ledger_local, ListIdentityType::All, true);
21+
} else if local_args.list_entries {
1922
println!("Entries:");
2023
for entry in ledger_local.iter(None) {
2124
match entry.label() {
@@ -44,7 +47,7 @@ pub async fn handle_ledger_remote_command(
4447
network_url: &str,
4548
ledger_canister_id: candid::Principal,
4649
identity: Option<String>,
47-
ledger_local: LedgerMap,
50+
mut ledger_local: LedgerMap,
4851
) -> Result<(), Box<dyn std::error::Error>> {
4952
let local_ledger_path = ledger_local
5053
.get_file_path()
@@ -54,7 +57,7 @@ pub async fn handle_ledger_remote_command(
5457
LedgerRemoteCommands::DataFetch => {
5558
let canister =
5659
LedgerCanister::new_without_identity(network_url, ledger_canister_id).await?;
57-
return crate::ledger::ledger_data_fetch(&canister, &ledger_local).await;
60+
return crate::ledger::ledger_data_fetch(&canister, &mut ledger_local).await;
5861
}
5962
LedgerRemoteCommands::DataPushAuthorize | LedgerRemoteCommands::DataPush => {
6063
let identity =

cli/src/commands/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,14 @@ pub async fn handle_command(
4545
match command {
4646
Commands::Keygen(args) => handle_keygen_command(args, identity).await,
4747
Commands::Account(args) => {
48-
handle_account_command(args, network_url, ledger_canister_id, identity).await
48+
handle_account_command(
49+
args,
50+
network_url,
51+
ledger_canister_id,
52+
identity,
53+
&ledger_local,
54+
)
55+
.await
4956
}
5057
Commands::Np(args) => {
5158
handle_np_command(

cli/src/commands/np.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub async fn handle_np_command(
1414
network_url: &str,
1515
ledger_canister_id: candid::Principal,
1616
identity: Option<String>,
17-
ledger_local: LedgerMap,
17+
mut ledger_local: LedgerMap,
1818
) -> Result<(), Box<dyn std::error::Error>> {
1919
match np_cmd {
2020
NpCommands::List(list_args) => {
@@ -73,7 +73,7 @@ pub async fn handle_np_command(
7373
let canister =
7474
LedgerCanister::new_without_identity(network_url, ledger_canister_id)
7575
.await?;
76-
ledger_data_fetch(&canister, &ledger_local).await?;
76+
ledger_data_fetch(&canister, &mut ledger_local).await?;
7777

7878
dcc_common::refresh_caches_from_ledger(&ledger_local)
7979
.expect("Loading balances from ledger failed");

cli/src/ledger/data_operations.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const PUSH_BLOCK_SIZE: u64 = 1024 * 1024;
1616

1717
pub async fn ledger_data_fetch(
1818
ledger_canister: &LedgerCanister,
19-
ledger_local: &LedgerMap,
19+
ledger_local: &mut LedgerMap,
2020
) -> Result<(), Box<dyn std::error::Error>> {
2121
let ledger_file_path = ledger_local
2222
.get_file_path()
@@ -100,6 +100,10 @@ pub async fn ledger_data_fetch(
100100
// Set the modified time to the current time, to mark that the data is up-to-date
101101
filetime::set_file_mtime(ledger_file_path, std::time::SystemTime::now().into())?;
102102

103+
if !data.is_empty() {
104+
ledger_local.refresh_ledger()?;
105+
}
106+
103107
Ok(())
104108
}
105109

0 commit comments

Comments
 (0)