Skip to content

Commit

Permalink
Merge branch 'main' into feature/built-in-customizable-deposit
Browse files Browse the repository at this point in the history
  • Loading branch information
koxu1996 authored Jul 25, 2024
2 parents 0c95fd3 + a06b32b commit 66e9ca6
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 11 deletions.
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 @@ -49,6 +50,7 @@ pub fn deposit(
base_url: &Url,
deposit_session_wasm_bytes: &[u8],
depositor_secret_key: &SecretKey,
chain_name: &str,
contract_hash: &ContractHash,
amount: impl Into<U512>,
recipient: casper_client_types::PublicKey,
Expand All @@ -61,16 +63,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 @@ -122,3 +120,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
Expand Up @@ -2,7 +2,9 @@ use std::fs;
use std::path::PathBuf;

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 @@ -27,6 +29,8 @@ pub struct Args {
recipient: RecipientArg,
#[clap(flatten)]
session_path: SessionPathArg,
#[clap(flatten)]
chain_name: ChainNameArg,
}

pub fn run(args: Args, kairos_server_address: Url) -> Result<String, CliError> {
Expand All @@ -45,6 +49,10 @@ 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,
};

let deposit_session_wasm: Vec<u8> = match args.session_path.field {
Some(deposit_session_wasm_path) => {
Expand All @@ -62,6 +70,7 @@ pub fn run(args: Args, kairos_server_address: Url) -> Result<String, CliError> {
&kairos_server_address,
&deposit_session_wasm,
&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

0 comments on commit 66e9ca6

Please sign in to comment.