From ff84ce5fe0233caaaa0323036dc817807002eb0f Mon Sep 17 00:00:00 2001 From: x Date: Mon, 29 May 2023 09:07:50 +0200 Subject: [PATCH] drk: add DAO aliases --- bin/drk/src/main.rs | 55 +++++++++++++++++++++---------------- bin/drk/src/wallet_dao.rs | 48 ++++++++++++++++++++++++++++++++ bin/drk/src/wallet_money.rs | 4 --- 3 files changed, 79 insertions(+), 28 deletions(-) diff --git a/bin/drk/src/main.rs b/bin/drk/src/main.rs index bde2f1957..37a2a2ec3 100644 --- a/bin/drk/src/main.rs +++ b/bin/drk/src/main.rs @@ -285,20 +285,20 @@ enum DaoSubcmd { /// Show the balance of a DAO Balance { - /// Numeric identifier for the DAO - dao_id: u64, + /// Name or numeric identifier for the DAO + dao_alias: String, }, /// Mint an imported DAO on-chain Mint { - /// Numeric identifier for the DAO - dao_id: u64, + /// Name or numeric identifier for the DAO + dao_alias: String, }, /// Create a proposal for a DAO Propose { - /// Numeric identifier for the DAO - dao_id: u64, + /// Name or numeric identifier for the DAO + dao_alias: String, /// Pubkey to send tokens to with proposal success recipient: String, @@ -307,19 +307,19 @@ enum DaoSubcmd { amount: String, /// Token ID to send from DAO with proposal success - token_id: String, + token: String, }, /// List DAO proposals Proposals { - /// Numeric identifier for the DAO - dao_id: u64, + /// Name or numeric identifier for the DAO + dao_alias: String, }, /// View a DAO proposal data Proposal { - /// Numeric identifier for the DAO - dao_id: u64, + /// Name or numeric identifier for the DAO + dao_alias: String, /// Numeric identifier for the proposal proposal_id: u64, @@ -327,8 +327,8 @@ enum DaoSubcmd { /// Vote on a given proposal Vote { - /// Numeric identifier for the DAO - dao_id: u64, + /// Name or numeric identifier for the DAO + dao_alias: String, /// Numeric identifier for the proposal proposal_id: u64, @@ -342,8 +342,8 @@ enum DaoSubcmd { /// Execute a DAO proposal Exec { - /// Numeric identifier for the DAO - dao_id: u64, + /// Name or numeric identifier for the DAO + dao_alias: String, /// Numeric identifier for the proposal proposal_id: u64, @@ -739,7 +739,7 @@ async fn main() -> Result<()> { let _ = f64::from_str(&amount).with_context(|| "Invalid amount")?; let rcpt = PublicKey::from_str(&recipient).with_context(|| "Invalid recipient")?; let drk = Drk::new(args.endpoint).await?; - let token_id = drk.get_token(token).await.with_context(|| "Invalid Token ID")?; + let token_id = drk.get_token(token).await.with_context(|| "Invalid token alias")?; let tx = drk .transfer(&amount, token_id, rcpt, dao, dao_bulla) @@ -958,8 +958,9 @@ async fn main() -> Result<()> { Ok(()) } - DaoSubcmd::Balance { dao_id } => { + DaoSubcmd::Balance { dao_alias } => { let drk = Drk::new(args.endpoint).await?; + let dao_id = drk.get_dao_id(&dao_alias).await?; let balmap = drk.dao_balance(dao_id).await.with_context(|| "Failed to fetch DAO balance")?; @@ -992,20 +993,22 @@ async fn main() -> Result<()> { Ok(()) } - DaoSubcmd::Mint { dao_id } => { + DaoSubcmd::Mint { dao_alias } => { let drk = Drk::new(args.endpoint).await?; + let dao_id = drk.get_dao_id(&dao_alias).await?; let tx = drk.dao_mint(dao_id).await.with_context(|| "Failed to mint DAO")?; println!("{}", bs58::encode(&serialize(&tx)).into_string()); Ok(()) } - DaoSubcmd::Propose { dao_id, recipient, amount, token_id } => { + DaoSubcmd::Propose { dao_alias, recipient, amount, token } => { let _ = f64::from_str(&amount).with_context(|| "Invalid amount")?; let amount = decode_base10(&amount, 8, true)?; let rcpt = PublicKey::from_str(&recipient).with_context(|| "Invalid recipient")?; let drk = Drk::new(args.endpoint).await?; - let token_id = drk.get_token(token_id).await.with_context(|| "Invalid Token ID")?; + let dao_id = drk.get_dao_id(&dao_alias).await?; + let token_id = drk.get_token(token).await.with_context(|| "Invalid token alias")?; let tx = drk .dao_propose(dao_id, rcpt, amount, token_id) @@ -1016,8 +1019,9 @@ async fn main() -> Result<()> { Ok(()) } - DaoSubcmd::Proposals { dao_id } => { + DaoSubcmd::Proposals { dao_alias } => { let drk = Drk::new(args.endpoint).await?; + let dao_id = drk.get_dao_id(&dao_alias).await?; let proposals = drk.get_dao_proposals(dao_id).await?; @@ -1028,8 +1032,9 @@ async fn main() -> Result<()> { Ok(()) } - DaoSubcmd::Proposal { dao_id, proposal_id } => { + DaoSubcmd::Proposal { dao_alias, proposal_id } => { let drk = Drk::new(args.endpoint).await?; + let dao_id = drk.get_dao_id(&dao_alias).await?; let proposals = drk.get_dao_proposals(dao_id).await?; let Some(proposal) = proposals.iter().find(|x| x.id == proposal_id) else { @@ -1042,8 +1047,9 @@ async fn main() -> Result<()> { Ok(()) } - DaoSubcmd::Vote { dao_id, proposal_id, vote, vote_weight } => { + DaoSubcmd::Vote { dao_alias, proposal_id, vote, vote_weight } => { let drk = Drk::new(args.endpoint).await?; + let dao_id = drk.get_dao_id(&dao_alias).await?; let _ = f64::from_str(&vote_weight).with_context(|| "Invalid vote weight")?; let weight = decode_base10(&vote_weight, 8, true)?; @@ -1066,8 +1072,9 @@ async fn main() -> Result<()> { Ok(()) } - DaoSubcmd::Exec { dao_id, proposal_id } => { + DaoSubcmd::Exec { dao_alias, proposal_id } => { let drk = Drk::new(args.endpoint).await?; + let dao_id = drk.get_dao_id(&dao_alias).await?; let dao = drk.get_dao_by_id(dao_id).await?; let proposal = drk.get_dao_proposal_by_id(proposal_id).await?; assert!(proposal.dao_bulla == dao.bulla()); diff --git a/bin/drk/src/wallet_dao.rs b/bin/drk/src/wallet_dao.rs index 4544f2dc5..7198380fc 100644 --- a/bin/drk/src/wallet_dao.rs +++ b/bin/drk/src/wallet_dao.rs @@ -423,6 +423,54 @@ impl Drk { Ok(()) } + pub async fn get_dao_id_by_alias(&self, alias_filter: &str) -> Result { + let query = format!( + "SELECT {}, {} FROM {}", + DAO_DAOS_COL_DAO_ID, DAO_DAOS_COL_NAME, DAO_DAOS_TABLE, + ); + let params = json!([ + query, + QueryType::Integer as u8, + DAO_DAOS_COL_DAO_ID, + QueryType::Blob as u8, + DAO_DAOS_COL_NAME, + ]); + + let req = JsonRequest::new("wallet.query_row_multi", params); + let rep = self.rpc_client.request(req).await?; + + // The returned thing should be an array of found rows. + let Some(rows) = rep.as_array() else { + return Err(anyhow!("[get_dao_id_by_alias] Unexpected response from darkfid: {}", rep)) + }; + + for row in rows { + let Some(row) = row.as_array() else { + return Err( + anyhow!("[get_dao_id_by_alias] Unexpected response from darkfid: {}", rep)) + }; + + let alias_bytes: Vec = serde_json::from_value(row[1].clone())?; + let alias: String = deserialize(&alias_bytes)?; + if alias != alias_filter { + continue + } + + let dao_id: u64 = serde_json::from_value(row[0].clone())?; + return Ok(dao_id) + } + + Err(anyhow!("[get_dao_id_by_alias] DAO not found")) + } + + /// Convenience function. Interprets the alias either as the DAO alias or its ID + pub async fn get_dao_id(&self, alias: &str) -> Result { + if let Ok(id) = self.get_dao_id_by_alias(alias).await { + return Ok(id) + } + Ok(alias.parse()?) + } + /// Import given DAO params into the wallet with a given name. pub async fn import_dao(&self, dao_name: String, dao_params: DaoParams) -> Result<()> { // First let's check if we've imported this DAO with the given name before. diff --git a/bin/drk/src/wallet_money.rs b/bin/drk/src/wallet_money.rs index 4b3b6eff1..90d6247d8 100644 --- a/bin/drk/src/wallet_money.rs +++ b/bin/drk/src/wallet_money.rs @@ -236,8 +236,6 @@ impl Drk { /// Optionally also fetch spent ones. /// The boolean in the returned tuple notes if the coin was marked as spent. pub async fn get_coins(&self, fetch_spent: bool) -> Result> { - eprintln!("Fetching OwnCoins from the wallet"); - let query = if fetch_spent { format!("SELECT * FROM {}", MONEY_COINS_TABLE) } else { @@ -703,8 +701,6 @@ impl Drk { alias_filter: Option, token_id_filter: Option, ) -> Result> { - eprintln!("Fetching Aliases from the wallet"); - let query = format!("SELECT * FROM {}", MONEY_ALIASES_TABLE); let params = json!([ query,