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

Dehardcode chain name in CLI #156

Merged
merged 7 commits into from
Jul 25, 2024
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
31 changes: 21 additions & 10 deletions kairos-cli/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use casper_client::types::{DeployBuilder, DeployHash, ExecutableDeployItem, Time
use casper_client_types::{crypto::SecretKey, runtime_args, ContractHash, RuntimeArgs, U512};
use kairos_server::routes::contract_hash::ContractHashPath;
use kairos_server::routes::deposit::DepositPath;
use kairos_server::routes::get_chain_name::GetChainNamePath;
use kairos_server::routes::get_nonce::GetNoncePath;
use kairos_server::PublicKey;
use reqwest::Url;
Expand Down Expand Up @@ -50,6 +51,7 @@ impl From<reqwest::Error> for KairosClientError {
pub fn deposit(
base_url: &Url,
depositor_secret_key: &SecretKey,
chain_name: &str,
contract_hash: &ContractHash,
amount: impl Into<U512>,
recipient: casper_client_types::PublicKey,
Expand All @@ -70,16 +72,12 @@ pub fn deposit(
"recipient" => recipient
},
);
let deploy = DeployBuilder::new(
env!("CASPER_CHAIN_NAME"),
deposit_session,
depositor_secret_key,
)
.with_standard_payment(MAX_GAS_FEE_PAYMENT_AMOUNT) // max amount allowed to be used on gas fees
.with_timestamp(Timestamp::now())
.with_ttl(TimeDiff::from_millis(60_000)) // 1 min
.build()
.map_err(|err| KairosClientError::CasperClientError(err.to_string()))?;
let deploy = DeployBuilder::new(chain_name, deposit_session, depositor_secret_key)
.with_standard_payment(MAX_GAS_FEE_PAYMENT_AMOUNT) // max amount allowed to be used on gas fees
.with_timestamp(Timestamp::now())
.with_ttl(TimeDiff::from_millis(60_000)) // 1 min
.build()
.map_err(|err| KairosClientError::CasperClientError(err.to_string()))?;

let response = reqwest::blocking::Client::new()
.post(base_url.join(DepositPath::PATH).unwrap())
Expand Down Expand Up @@ -131,3 +129,16 @@ pub fn contract_hash(base_url: &Url) -> Result<ContractHash, KairosClientError>
.map_err(KairosClientError::from)
}
}

pub fn get_chain_name(base_url: &Url) -> Result<String, KairosClientError> {
let response = reqwest::blocking::Client::new()
.get(base_url.join(GetChainNamePath::PATH).unwrap())
.send()
.map_err(KairosClientError::from)?
.error_for_status();

match response {
Err(err) => Err(KairosClientError::from(err)),
Ok(response) => response.json::<String>().map_err(KairosClientError::from),
}
}
11 changes: 10 additions & 1 deletion kairos-cli/src/commands/deposit.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::client;
use crate::common::args::{AmountArg, ContractHashArg, PrivateKeyPathArg, RecipientArg};
use crate::common::args::{
AmountArg, ChainNameArg, ContractHashArg, PrivateKeyPathArg, RecipientArg,
};
use crate::error::CliError;

use casper_client_types::{crypto::SecretKey, ContractHash};
Expand All @@ -19,6 +21,8 @@ pub struct Args {
contract_hash: ContractHashArg,
#[clap(flatten)]
recipient: RecipientArg,
#[clap(flatten)]
chain_name: ChainNameArg,
}

pub fn run(args: Args, kairos_server_address: Url) -> Result<String, CliError> {
Expand All @@ -37,10 +41,15 @@ pub fn run(args: Args, kairos_server_address: Url) -> Result<String, CliError> {
}
None => client::contract_hash(&kairos_server_address)?,
};
let chain_name = match args.chain_name.field {
None => client::get_chain_name(&kairos_server_address)?,
Some(name) => name,
};

client::deposit(
&kairos_server_address,
&depositor_secret_key,
&chain_name,
&contract_hash,
amount,
args.recipient.try_into()?,
Expand Down
11 changes: 11 additions & 0 deletions kairos-cli/src/common/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,14 @@ impl TryFrom<RecipientArg> for casper_client_types::PublicKey {
Ok(pk)
}
}

#[derive(Args, Debug)]
pub struct ChainNameArg {
#[arg(
id = "chain-name",
long,
value_name = "NAME",
help = "Name of the chain, to avoid the deploy from being accidentally included in a different chain"
)]
pub field: Option<String>,
}
1 change: 1 addition & 0 deletions kairos-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub fn app_router(state: ServerState) -> Router {
.typed_post(routes::deposit_handler)
.typed_post(routes::withdraw_handler)
.typed_post(routes::transfer_handler)
.typed_get(routes::get_chain_name_handler)
.typed_post(routes::get_nonce_handler)
.typed_get(routes::contract_hash_handler);
#[cfg(feature = "deposit-mock")]
Expand Down
18 changes: 18 additions & 0 deletions kairos-server/src/routes/get_chain_name.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use axum::{extract::State, Json};
use axum_extra::routing::TypedPath;
use tracing::*;

use crate::{state::ServerState, AppErr};

#[derive(TypedPath, Debug, Clone, Copy)]
#[typed_path("/api/v1/chain_name")]
pub struct GetChainNamePath;

#[instrument(level = "trace", skip(_state), ret)]
pub async fn get_chain_name_handler(
_: GetChainNamePath,
_state: State<ServerState>,
) -> Result<Json<String>, AppErr> {
let chain_name = env!("CASPER_CHAIN_NAME"); // NOTE: This should be obtained from RPC, rather than from hardcoded value.
Ok(Json(String::from(chain_name)))
}
2 changes: 2 additions & 0 deletions kairos-server/src/routes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub mod contract_hash;
pub mod deposit;
#[cfg(feature = "deposit-mock")]
pub mod deposit_mock;
pub mod get_chain_name;
pub mod get_nonce;
pub mod transfer;
pub mod withdraw;
Expand All @@ -14,6 +15,7 @@ pub use deposit::deposit_handler;
pub use deposit_mock::deposit_mock_handler;
#[cfg(feature = "database")]
pub use fetch::query_transactions_handler;
pub use get_chain_name::get_chain_name_handler;
pub use get_nonce::get_nonce_handler;
pub use transfer::transfer_handler;
pub use withdraw::withdraw_handler;
Expand Down
Loading