diff --git a/src/base_client.rs b/src/base_client.rs index 40e198f..400b0dc 100644 --- a/src/base_client.rs +++ b/src/base_client.rs @@ -17,6 +17,12 @@ type TxServiceClient = cosmrs::proto::cosmos::tx::v1beta1::service_client::Se type TendermintClient = cosmrs::proto::cosmos::base::tendermint::v1beta1::service_client::ServiceClient; +/// Default chain ID. +pub const DEFAULT_CHAIN_ID: &str = "gevulot"; + +/// Default token denomination. +pub const DEFAULT_TOKEN_DENOM: &str = "ucredit"; + /// BaseClient is a struct that provides various functionalities to interact with the blockchain. #[derive(derivative::Derivative)] #[derivative(Debug)] @@ -31,7 +37,8 @@ pub struct BaseClient { pub tx_client: TxServiceClient, gas_price: f64, - denom: String, + pub denom: String, + pub chain_id: String, gas_multiplier: f64, // Data from signer @@ -91,7 +98,8 @@ impl BaseClient { gov_client: GovQueryClient::new(channel.clone()), tendermint_client: TendermintClient::new(channel.clone()), tx_client: TxServiceClient::new(channel), - denom: "ucredit".to_owned(), + denom: DEFAULT_TOKEN_DENOM.to_string(), + chain_id: DEFAULT_CHAIN_ID.to_string(), gas_price, gas_multiplier, address: None, @@ -164,7 +172,7 @@ impl BaseClient { pub async fn get_account_balance(&mut self, address: &str) -> Result { let request = cosmrs::proto::cosmos::bank::v1beta1::QueryBalanceRequest { address: address.to_string(), - denom: String::from("ucredit"), + denom: self.denom.clone(), }; let response = self.bank_client.balance(request).await?; @@ -251,7 +259,8 @@ impl BaseClient { ) -> Result { let msg = cosmrs::Any::from_msg(&msg)?; let gas = 100_000u64; - let chain_id: cosmrs::tendermint::chain::Id = "gevulot" + let chain_id: cosmrs::tendermint::chain::Id = self + .chain_id .parse() .map_err(|_| Error::Parse("fail".to_string()))?; let tx_body = cosmrs::tx::BodyBuilder::new().msg(msg).memo(memo).finish(); @@ -313,7 +322,8 @@ impl BaseClient { log::debug!("fee: {:?}", fee); let msg = cosmrs::Any::from_msg(&msg)?; - let chain_id: cosmrs::tendermint::chain::Id = "gevulot" + let chain_id: cosmrs::tendermint::chain::Id = self + .chain_id .parse() .map_err(|_| Error::Parse("fail".to_string()))?; let tx_body = cosmrs::tx::BodyBuilder::new().msg(msg).memo(memo).finish(); diff --git a/src/gevulot_client.rs b/src/gevulot_client.rs index 38a3717..539652c 100644 --- a/src/gevulot_client.rs +++ b/src/gevulot_client.rs @@ -29,6 +29,8 @@ pub struct GevulotClient { /// Builder for GevulotClient pub struct GevulotClientBuilder { endpoint: String, + chain_id: Option, + denom: Option, gas_price: f64, gas_multiplier: f64, mnemonic: Option, @@ -40,6 +42,8 @@ impl Default for GevulotClientBuilder { fn default() -> Self { Self { endpoint: "http://127.0.0.1:9090".to_string(), + chain_id: None, + denom: None, gas_price: 0.025, gas_multiplier: 1.2, mnemonic: None, @@ -60,6 +64,18 @@ impl GevulotClientBuilder { self } + /// Sets the chain ID for the GevulotClient + pub fn chain_id(mut self, chain_id: &str) -> Self { + self.chain_id = Some(chain_id.to_string()); + self + } + + /// Sets the token denomination for the GevulotClient + pub fn denom(mut self, denom: &str) -> Self { + self.denom = Some(denom.to_string()); + self + } + /// Sets the gas price for the GevulotClient pub fn gas_price(mut self, gas_price: f64) -> Self { self.gas_price = gas_price; @@ -91,6 +107,16 @@ impl GevulotClientBuilder { BaseClient::new(&self.endpoint, self.gas_price, self.gas_multiplier).await?, )); + // If chain ID is provided, set it in the BaseClient + if let Some(chain_id) = self.chain_id { + base_client.write().await.chain_id = chain_id; + } + + // If token denomination is provided, set it in the BaseClient + if let Some(denom) = self.denom { + base_client.write().await.denom = denom; + } + // If a mnemonic is provided, set it in the BaseClient if let Some(mnemonic) = self.mnemonic { base_client diff --git a/src/gov_client.rs b/src/gov_client.rs index a8ffeff..e2c440c 100644 --- a/src/gov_client.rs +++ b/src/gov_client.rs @@ -224,7 +224,7 @@ impl GovClient { }; let deposit = vec![Coin { - denom: "ucredit".to_string(), + denom: self.base_client.read().await.denom.to_string(), amount: deposit.to_string(), }]; let msg = MsgSubmitProposal {