Skip to content

Commit

Permalink
rusk-wallet: Seperate http query and tx contract call
Browse files Browse the repository at this point in the history
Signed-off-by: Daksh <[email protected]>
  • Loading branch information
Daksh14 committed Dec 22, 2024
1 parent 4d125ed commit a30dec6
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 42 deletions.
87 changes: 49 additions & 38 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;
use crate::io::prompt::{self, request_contract_call_method};
use crate::settings::Settings;
use crate::{WalletFile, WalletPath};

Expand Down Expand Up @@ -569,42 +569,51 @@ impl Command {
.try_into()
.map_err(|_| Error::InvalidContractId)?;

let call =
ContractCall::new(contract_id, fn_name.clone(), &fn_args)
.map_err(|_| Error::Rkyv)?;

let tx = match address {
Address::Shielded(_) => {
wallet.sync().await?;
wallet
.phoenix_execute(
addr_idx,
Dusk::from(0),
gas,
call.into(),
)
.await
}
Address::Public(_) => {
wallet
.moonlight_execute(
addr_idx,
Dusk::from(0),
Dusk::from(0),
gas,
call.into(),
)
.await
}
}?;
match request_contract_call_method()? {
prompt::ContractCall::Query => {
let contract_id = hex::encode(contract_id);

let contract_id = hex::encode(contract_id);
let http_call = wallet
.http_contract_call(contract_id, &fn_name, fn_args)
.await?;

let http_call = wallet
.http_contract_call(contract_id, &fn_name, fn_args)
.await?;
Ok(RunResult::ContractCallQuery(http_call))
}
prompt::ContractCall::Transaction => {
let call = ContractCall::new(
contract_id,
fn_name.clone(),
&fn_args,
)
.map_err(|_| Error::Rkyv)?;

Ok(RunResult::ContractCall(tx.hash(), http_call))
let tx = match address {
Address::Shielded(_) => {
wallet.sync().await?;
wallet
.phoenix_execute(
addr_idx,
Dusk::from(0),
gas,
call.into(),
)
.await
}
Address::Public(_) => {
wallet
.moonlight_execute(
addr_idx,
Dusk::from(0),
Dusk::from(0),
gas,
call.into(),
)
.await
}
}?;
Ok(RunResult::ContractCallTx(tx.hash()))
}
}
}

Self::ContractDeploy {
Expand Down Expand Up @@ -695,7 +704,8 @@ pub enum RunResult<'a> {
Profile((u8, &'a Profile)),
Profiles(&'a Vec<Profile>),
ContractId([u8; CONTRACT_ID_BYTES]),
ContractCall(BlsScalar, Vec<u8>),
ContractCallTx(BlsScalar),
ContractCallQuery(Vec<u8>),
ExportedKeys(PathBuf, PathBuf),
Create(),
Restore(),
Expand Down Expand Up @@ -773,10 +783,11 @@ impl fmt::Display for RunResult<'_> {
ContractId(bytes) => {
write!(f, "> Contract ID: {}", hex::encode(bytes))
}
ContractCall(scalar, bytes) => {
ContractCallTx(scalar) => {
let hash = hex::encode(scalar.to_bytes());
writeln!(f, "> Contract call transaction hash: {hash}",)?;

writeln!(f, "> Contract call transaction hash: {hash}",)
}
ContractCallQuery(bytes) => {
writeln!(f, "> Http contract query: {:?}", bytes)
}
ExportedKeys(pk, kp) => {
Expand Down
2 changes: 1 addition & 1 deletion rusk-wallet/src/bin/interactive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ pub(crate) async fn run_loop(
println!("\r{}", res);
match res {
RunResult::Tx(hash)
| RunResult::ContractCall(hash, _) => {
| RunResult::ContractCallTx(hash) => {
let tx_id = hex::encode(hash.to_bytes());

// Wait for transaction confirmation
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 @@ -371,7 +371,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
7 changes: 5 additions & 2 deletions rusk-wallet/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ async fn exec() -> anyhow::Result<()> {
RunResult::ContractId(id) => {
println!("Contract ID: {:?}", id);
}
RunResult::ContractCall(scalar, result) => {
RunResult::ContractCallTx(scalar) => {
let tx_id = hex::encode(scalar.to_bytes());

// Wait for transaction confirmation from network
Expand All @@ -410,7 +410,10 @@ async fn exec() -> anyhow::Result<()> {

println!("{tx_id}");

println!("HTTP call result: {:?}", result);

}
RunResult::ContractCallQuery(bytes) => {
println!("HTTP call result: {:?}", bytes);
}
RunResult::Settings() => {}
RunResult::Create() | RunResult::Restore() => {}
Expand Down

0 comments on commit a30dec6

Please sign in to comment.