mirror of
https://github.com/darkrenaissance/darkfi.git
synced 2026-04-28 03:00:18 -04:00
drk: DAO import.
This commit is contained in:
@@ -247,8 +247,11 @@ enum DaoSubcmd {
|
||||
dao_name: String,
|
||||
},
|
||||
|
||||
/// List imported DAOs
|
||||
List,
|
||||
/// List imported DAOs (or info about a specific one)
|
||||
List {
|
||||
/// Named identifier for the DAO (optional)
|
||||
dao_name: Option<String>,
|
||||
},
|
||||
|
||||
/// Mint an imported DAO on-chain
|
||||
Mint {
|
||||
@@ -759,7 +762,7 @@ async fn main() -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
DaoSubcmd::List => todo!(),
|
||||
DaoSubcmd::List { dao_name } => todo!(),
|
||||
|
||||
DaoSubcmd::Mint { dao_name } => todo!(),
|
||||
|
||||
|
||||
@@ -16,7 +16,15 @@
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use anyhow::Result;
|
||||
use anyhow::{anyhow, Result};
|
||||
use darkfi::{rpc::jsonrpc::JsonRequest, wallet::walletdb::QueryType};
|
||||
use darkfi_dao_contract::dao_client::{
|
||||
DAO_DAOS_COL_APPROVAL_RATIO_BASE, DAO_DAOS_COL_APPROVAL_RATIO_QUOT, DAO_DAOS_COL_BULLA_BLIND,
|
||||
DAO_DAOS_COL_DAO_ID, DAO_DAOS_COL_GOV_TOKEN_ID, DAO_DAOS_COL_NAME, DAO_DAOS_COL_PROPOSER_LIMIT,
|
||||
DAO_DAOS_COL_QUORUM, DAO_DAOS_COL_SECRET, DAO_DAOS_TABLE,
|
||||
};
|
||||
use darkfi_serial::serialize;
|
||||
use serde_json::json;
|
||||
|
||||
use super::Drk;
|
||||
use crate::DaoParams;
|
||||
@@ -24,6 +32,53 @@ use crate::DaoParams;
|
||||
impl Drk {
|
||||
/// Import given DAO into the wallet
|
||||
pub async fn dao_import(&self, dao_name: String, dao_params: DaoParams) -> Result<()> {
|
||||
// First let's check if we've imported this DAO before. We use the name
|
||||
// as the identifier.
|
||||
let query = format!(
|
||||
"SELECT {} FROM {} WHERE {} = {}",
|
||||
DAO_DAOS_COL_DAO_ID, DAO_DAOS_TABLE, DAO_DAOS_COL_NAME, dao_name
|
||||
);
|
||||
let params = json!([query, QueryType::Integer as u8, DAO_DAOS_COL_DAO_ID]);
|
||||
let req = JsonRequest::new("wallet.query_row_single", params);
|
||||
|
||||
if (self.rpc_client.request(req).await).is_ok() {
|
||||
return Err(anyhow!("DAO \"{}\" already imported in wallet.", dao_name))
|
||||
}
|
||||
|
||||
eprintln!("Importing \"{}\" DAO into wallet", dao_name);
|
||||
|
||||
let query = format!(
|
||||
"INSERT INTO {} ({}, {}, {}, {}, {}, {}, {}, {}) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8);",
|
||||
DAO_DAOS_TABLE, DAO_DAOS_COL_NAME, DAO_DAOS_COL_PROPOSER_LIMIT,
|
||||
DAO_DAOS_COL_QUORUM, DAO_DAOS_COL_APPROVAL_RATIO_BASE, DAO_DAOS_COL_APPROVAL_RATIO_QUOT,
|
||||
DAO_DAOS_COL_GOV_TOKEN_ID, DAO_DAOS_COL_SECRET, DAO_DAOS_COL_BULLA_BLIND,
|
||||
);
|
||||
|
||||
let params = json!([
|
||||
query,
|
||||
QueryType::Blob as u8,
|
||||
serialize(&dao_name),
|
||||
QueryType::Integer as u8,
|
||||
dao_params.proposer_limit,
|
||||
QueryType::Integer as u8,
|
||||
dao_params.quorum,
|
||||
QueryType::Integer as u8,
|
||||
dao_params.approval_ratio_base,
|
||||
QueryType::Integer as u8,
|
||||
dao_params.approval_ratio_quot,
|
||||
QueryType::Blob as u8,
|
||||
serialize(&dao_params.gov_token_id),
|
||||
QueryType::Blob as u8,
|
||||
serialize(&dao_params.secret_key),
|
||||
QueryType::Blob as u8,
|
||||
serialize(&dao_params.bulla_blind),
|
||||
]);
|
||||
|
||||
eprintln!("Executing JSON-RPC request to add DAO to wallet");
|
||||
let req = JsonRequest::new("wallet.exec_sql", params);
|
||||
self.rpc_client.request(req).await?;
|
||||
eprintln!("DAO imported successfully");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user