From c5d000a09497d967f77609802ca97b892364b6a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrzej=20Bro=C5=84ski?= Date: Tue, 23 Jul 2024 12:58:44 +0200 Subject: [PATCH 1/4] Add server endpoint for getting chain name. --- kairos-server/src/lib.rs | 1 + kairos-server/src/routes/get_chain_name.rs | 18 ++++++++++++++++++ kairos-server/src/routes/mod.rs | 2 ++ 3 files changed, 21 insertions(+) create mode 100644 kairos-server/src/routes/get_chain_name.rs diff --git a/kairos-server/src/lib.rs b/kairos-server/src/lib.rs index 773669d4..d9dbe370 100644 --- a/kairos-server/src/lib.rs +++ b/kairos-server/src/lib.rs @@ -42,6 +42,7 @@ pub fn app_router(state: ServerState) -> Router { .typed_post(routes::withdraw_handler) .typed_post(routes::transfer_handler) .typed_post(routes::deposit_mock_handler) + .typed_get(routes::get_chain_name_handler) .typed_post(routes::get_nonce_handler) .typed_get(routes::contract_hash_handler) .with_state(state) diff --git a/kairos-server/src/routes/get_chain_name.rs b/kairos-server/src/routes/get_chain_name.rs new file mode 100644 index 00000000..e8c9e07a --- /dev/null +++ b/kairos-server/src/routes/get_chain_name.rs @@ -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, +) -> Result, 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))) +} diff --git a/kairos-server/src/routes/mod.rs b/kairos-server/src/routes/mod.rs index 7da4b4dd..a25b2609 100644 --- a/kairos-server/src/routes/mod.rs +++ b/kairos-server/src/routes/mod.rs @@ -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; @@ -12,6 +13,7 @@ pub use contract_hash::contract_hash_handler; pub use deposit::deposit_handler; #[cfg(feature = "deposit-mock")] pub use deposit_mock::deposit_mock_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; From bf5e6c5a5bae3ebe95114fa1ec98c0da18081928 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrzej=20Bro=C5=84ski?= Date: Tue, 23 Jul 2024 13:08:54 +0200 Subject: [PATCH 2/4] Add chain name argument to CLI, with automatic fallback. --- kairos-cli/src/client.rs | 17 ++++++++++++++++- kairos-cli/src/commands/deposit.rs | 9 ++++++++- kairos-cli/src/common/args.rs | 6 ++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/kairos-cli/src/client.rs b/kairos-cli/src/client.rs index 3c0d5f3c..dc4c2486 100644 --- a/kairos-cli/src/client.rs +++ b/kairos-cli/src/client.rs @@ -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; @@ -50,6 +51,7 @@ impl From for KairosClientError { pub fn deposit( base_url: &Url, depositor_secret_key: &SecretKey, + chain_name: &str, contract_hash: &ContractHash, amount: impl Into, recipient: casper_client_types::PublicKey, @@ -71,7 +73,7 @@ pub fn deposit( }, ); let deploy = DeployBuilder::new( - env!("CASPER_CHAIN_NAME"), + chain_name, deposit_session, depositor_secret_key, ) @@ -131,3 +133,16 @@ pub fn contract_hash(base_url: &Url) -> Result .map_err(KairosClientError::from) } } + +pub fn get_chain_name(base_url: &Url) -> Result { + 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::().map_err(KairosClientError::from), + } +} diff --git a/kairos-cli/src/commands/deposit.rs b/kairos-cli/src/commands/deposit.rs index cd72f23b..ea0dc03e 100644 --- a/kairos-cli/src/commands/deposit.rs +++ b/kairos-cli/src/commands/deposit.rs @@ -1,5 +1,5 @@ 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}; @@ -19,6 +19,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 { @@ -37,10 +39,15 @@ pub fn run(args: Args, kairos_server_address: Url) -> Result { } 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()?, diff --git a/kairos-cli/src/common/args.rs b/kairos-cli/src/common/args.rs index 50cc5bf5..a39f4d51 100644 --- a/kairos-cli/src/common/args.rs +++ b/kairos-cli/src/common/args.rs @@ -49,3 +49,9 @@ impl TryFrom 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, +} From 7e7760bd5a7969c218011adb27e49e1611146eaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrzej=20Bro=C5=84ski?= Date: Tue, 23 Jul 2024 13:12:19 +0200 Subject: [PATCH 3/4] Code formatting. --- kairos-cli/src/client.rs | 16 ++++++---------- kairos-cli/src/commands/deposit.rs | 4 +++- kairos-cli/src/common/args.rs | 7 ++++++- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/kairos-cli/src/client.rs b/kairos-cli/src/client.rs index dc4c2486..71ab7035 100644 --- a/kairos-cli/src/client.rs +++ b/kairos-cli/src/client.rs @@ -72,16 +72,12 @@ pub fn deposit( "recipient" => recipient }, ); - 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 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()) diff --git a/kairos-cli/src/commands/deposit.rs b/kairos-cli/src/commands/deposit.rs index ea0dc03e..6f2b6f84 100644 --- a/kairos-cli/src/commands/deposit.rs +++ b/kairos-cli/src/commands/deposit.rs @@ -1,5 +1,7 @@ use crate::client; -use crate::common::args::{AmountArg, ChainNameArg, ContractHashArg, PrivateKeyPathArg, RecipientArg}; +use crate::common::args::{ + AmountArg, ChainNameArg, ContractHashArg, PrivateKeyPathArg, RecipientArg, +}; use crate::error::CliError; use casper_client_types::{crypto::SecretKey, ContractHash}; diff --git a/kairos-cli/src/common/args.rs b/kairos-cli/src/common/args.rs index a39f4d51..1ec0487f 100644 --- a/kairos-cli/src/common/args.rs +++ b/kairos-cli/src/common/args.rs @@ -52,6 +52,11 @@ impl TryFrom for casper_client_types::PublicKey { #[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")] + #[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, } From e9188c8285c8c2e745fd71d72054afab6e5d3c51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrzej=20Bro=C5=84ski?= Date: Thu, 25 Jul 2024 09:42:39 +0200 Subject: [PATCH 4/4] Code formatting. --- kairos-server/src/routes/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kairos-server/src/routes/mod.rs b/kairos-server/src/routes/mod.rs index f10182a9..eeccbd6b 100644 --- a/kairos-server/src/routes/mod.rs +++ b/kairos-server/src/routes/mod.rs @@ -13,9 +13,9 @@ pub use contract_hash::contract_hash_handler; pub use deposit::deposit_handler; #[cfg(feature = "deposit-mock")] pub use deposit_mock::deposit_mock_handler; -pub use get_chain_name::get_chain_name_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;