Skip to content

Commit

Permalink
rusk-wallet: Seperate http query and transaction contract call
Browse files Browse the repository at this point in the history
  • Loading branch information
Daksh14 committed Dec 30, 2024
1 parent e9d41d0 commit 8086729
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 24 deletions.
19 changes: 14 additions & 5 deletions rusk-wallet/src/bin/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use rusk_wallet::{
};
use wallet_core::BalanceInfo;

use crate::io::prompt::{self, request_contract_call_method};
use crate::io::prompt::{self};
use crate::settings::Settings;
use crate::{WalletFile, WalletPath};

Expand Down Expand Up @@ -229,6 +229,12 @@ pub(crate) enum Command {
#[arg(short = 'f', long)]
fn_args: Vec<u8>,

/// If query is true then HTTP contract call is done to obtain return
/// value of function if query is false then a transaction is
/// sent for mutation of the state of the contract
#[arg(short, long)]
query: bool,

/// Max amount of gas for this transaction
#[arg(short = 'l', long, default_value_t = DEFAULT_LIMIT_CALL)]
gas_limit: u64,
Expand Down Expand Up @@ -559,6 +565,7 @@ impl Command {
fn_args,
gas_limit,
gas_price,
query,
} => {
let gas = Gas::new(gas_limit).with_price(gas_price);

Expand All @@ -569,8 +576,8 @@ impl Command {
.try_into()
.map_err(|_| Error::InvalidContractId)?;

match request_contract_call_method()? {
prompt::ContractCall::Query => {
match query {
true => {
let contract_id = hex::encode(contract_id);

let http_call = wallet
Expand All @@ -579,7 +586,7 @@ impl Command {

Ok(RunResult::ContractCallQuery(http_call))
}
prompt::ContractCall::Transaction => {
false => {
let call = ContractCall::new(
contract_id,
fn_name.clone(),
Expand Down Expand Up @@ -611,6 +618,7 @@ impl Command {
.await
}
}?;

Ok(RunResult::ContractCallTx(tx.hash()))
}
}
Expand Down Expand Up @@ -785,10 +793,11 @@ impl fmt::Display for RunResult<'_> {
}
ContractCallTx(scalar) => {
let hash = hex::encode(scalar.to_bytes());

writeln!(f, "> Contract call transaction hash: {hash}",)
}
ContractCallQuery(bytes) => {
writeln!(f, "> Http contract query: {:?}", bytes)
writeln!(f, "> Http contract query: {:?}", hex::encode(bytes))
}
ExportedKeys(pk, kp) => {
let pk = pk.display();
Expand Down
26 changes: 26 additions & 0 deletions rusk-wallet/src/bin/interactive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,32 @@ fn confirm(cmd: &Command, wallet: &Wallet<WalletFile>) -> anyhow::Result<bool> {
}
prompt::ask_confirm()
}
Command::ContractCall {
address,
contract_id,
fn_name,
fn_args,
query,
gas_limit,
gas_price,
} => {
println!(" > Function name {}", fn_name);
println!(" > Function arguments {}", hex::encode(fn_args));
println!(" > Contract ID {}", hex::encode(contract_id));
println!(" > Is HTTP query? {}", query);

if !query {
let max_fee = gas_limit * gas_price;

println!(" > Max fee = {} DUSK", Dusk::from(max_fee));

if let Some(Address::Public(_)) = address {
println!(" > ALERT: THIS IS A PUBLIC TRANSACTION");
}
}

prompt::ask_confirm()
}
_ => Ok(true),
}
}
Expand Down
48 changes: 30 additions & 18 deletions rusk-wallet/src/bin/interactive/command_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use rusk_wallet::{
Address, Error, Wallet, MAX_CONTRACT_INIT_ARG_SIZE, MAX_FUNCTION_NAME_SIZE,
};

use super::prompt::request_contract_call_method;
use super::ProfileOp;
use crate::settings::Settings;
use crate::{prompt, Command, WalletFile};
Expand Down Expand Up @@ -296,27 +297,37 @@ pub(crate) async fn online(
}))
}
MenuItem::ContractCall => {
let (addr, balance) = pick_transaction_model(
wallet,
profile_idx,
phoenix_spendable,
moonlight_balance,
)?;

if check_min_gas_balance(
balance,
DEFAULT_LIMIT_CALL,
"a contract call",
)
.is_err()
{
return Ok(ProfileOp::Stay);
}

let mempool_gas_prices = wallet.get_mempool_gas_prices().await?;
let mut address = None;

let query = match request_contract_call_method()? {
prompt::ContractCall::Query => true,
prompt::ContractCall::Transaction => {
let (addr, balance) = pick_transaction_model(
wallet,
profile_idx,
phoenix_spendable,
moonlight_balance,
)?;

address = Some(addr);

if check_min_gas_balance(
balance,
DEFAULT_LIMIT_CALL,
"a contract call",
)
.is_err()
{
return Ok(ProfileOp::Stay);
}

false
}
};

ProfileOp::Run(Box::new(Command::ContractCall {
address: Some(addr),
address,
contract_id: prompt::request_bytes("contract id")?,
fn_name: prompt::request_str(
"function name to call",
Expand All @@ -330,6 +341,7 @@ pub(crate) async fn online(
DEFAULT_PRICE,
mempool_gas_prices,
)?,
query,
}))
}
MenuItem::History => {
Expand Down
3 changes: 2 additions & 1 deletion rusk-wallet/src/bin/io/prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,8 @@ pub(crate) fn request_address(
pub(crate) fn request_contract_code() -> anyhow::Result<PathBuf> {
let validator = |path_str: &str| {
let path = PathBuf::from(path_str);
if path.extension().map_or(false, |ext| ext == "wasm") {
if path.extension().map_or(false, |ext| ext == "wasm") && path.exists()
{
Ok(Validation::Valid)
} else {
Ok(Validation::Invalid("Not a valid directory".into()))
Expand Down

0 comments on commit 8086729

Please sign in to comment.