Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customize chain ID and token denomination in GevulotClient #53

Merged
merged 1 commit into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions src/base_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ type TxServiceClient<T> = cosmrs::proto::cosmos::tx::v1beta1::service_client::Se
type TendermintClient<T> =
cosmrs::proto::cosmos::base::tendermint::v1beta1::service_client::ServiceClient<T>;

/// 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)]
Expand All @@ -31,7 +37,8 @@ pub struct BaseClient {
pub tx_client: TxServiceClient<Channel>,

gas_price: f64,
denom: String,
pub denom: String,
pub chain_id: String,
gas_multiplier: f64,

// Data from signer
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -164,7 +172,7 @@ impl BaseClient {
pub async fn get_account_balance(&mut self, address: &str) -> Result<Coin> {
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?;

Expand Down Expand Up @@ -251,7 +259,8 @@ impl BaseClient {
) -> Result<SimulateResponse> {
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();
Expand Down Expand Up @@ -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();
Expand Down
26 changes: 26 additions & 0 deletions src/gevulot_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ pub struct GevulotClient {
/// Builder for GevulotClient
pub struct GevulotClientBuilder {
endpoint: String,
chain_id: Option<String>,
denom: Option<String>,
gas_price: f64,
gas_multiplier: f64,
mnemonic: Option<String>,
Expand All @@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/gov_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading