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

rusk-wallet: Implement http contract query #3169

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
20 changes: 17 additions & 3 deletions rusk-wallet/src/bin/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,8 +545,9 @@ impl Command {
.try_into()
.map_err(|_| Error::InvalidContractId)?;

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

let tx = match address {
Address::Shielded(_) => {
Expand All @@ -573,7 +574,13 @@ impl Command {
}
}?;

Ok(RunResult::Tx(tx.hash()))
let contract_id = hex::encode(contract_id);

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

Ok(RunResult::ContractCall(tx.hash(), http_call))
}

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

writeln!(f, "> Http contract query: {:?}", bytes)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will give really ugly output, use hex::encode(bytes)

}
ExportedKeys(pk, kp) => {
let pk = pk.display();
let kp = kp.display();
Expand Down
34 changes: 19 additions & 15 deletions rusk-wallet/src/bin/interactive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,22 +92,26 @@ pub(crate) async fn run_loop(
prompt::show_cursor()?;
// output results
println!("\r{}", res);
if let RunResult::Tx(hash) = res {
let tx_id = hex::encode(hash.to_bytes());

// Wait for transaction confirmation
// from network
let gql = GraphQL::new(
settings.state.to_string(),
io::status::interactive,
)?;
gql.wait_for(&tx_id).await?;

if let Some(explorer) = &settings.explorer {
let url = format!("{explorer}{tx_id}");
println!("> URL: {url}");
prompt::launch_explorer(url)?;
match res {
RunResult::Tx(hash)
| RunResult::ContractCall(hash, _) => {
let tx_id = hex::encode(hash.to_bytes());

// Wait for transaction confirmation
// from network
let gql = GraphQL::new(
settings.state.to_string(),
io::status::interactive,
)?;
gql.wait_for(&tx_id).await?;

if let Some(explorer) = &settings.explorer {
let url = format!("{explorer}{tx_id}");
println!("> URL: {url}");
prompt::launch_explorer(url)?;
}
}
_ => (),
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions rusk-wallet/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,17 @@ async fn exec() -> anyhow::Result<()> {
RunResult::ContractId(id) => {
println!("Contract ID: {:?}", id);
}
RunResult::ContractCall(scalar, result) => {
let tx_id = hex::encode(scalar.to_bytes());

// Wait for transaction confirmation from network
let gql = GraphQL::new(settings.state, status::headless)?;
gql.wait_for(&tx_id).await?;

println!("{tx_id}");

println!("HTTP call result: {:?}", result);
}
RunResult::Settings() => {}
RunResult::Create() | RunResult::Restore() => {}
}
Expand Down
2 changes: 1 addition & 1 deletion rusk-wallet/src/clients/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub(crate) async fn sync_db(
let mut stream = client
.call_raw(
CONTRACTS_TARGET,
TRANSFER_CONTRACT,
Some(TRANSFER_CONTRACT.to_string()),
"leaves_from_pos",
&req,
true,
Expand Down
2 changes: 1 addition & 1 deletion rusk-wallet/src/gql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ impl GraphQL {
/// Call the graphql endpoint of a node
pub async fn query(&self, query: &str) -> Result<Vec<u8>, Error> {
self.client
.call("graphql", None, "query", query.as_bytes())
.call("graphql", None::<String>, "query", query.as_bytes())
.await
}
}
Expand Down
9 changes: 6 additions & 3 deletions rusk-wallet/src/rues.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,11 @@ impl RuesHttpClient {
{
let data = rkyv::to_bytes(value).map_err(|_| Error::Rkyv)?.to_vec();

let contract: Option<&str> = contract.into();
let contract: Option<String> = contract.map(|e| e.to_owned());

let response = self
.call_raw(CONTRACTS_TARGET, contract.into(), method, &data, false)
.call_raw(CONTRACTS_TARGET, contract, method, &data, false)
.await?;

Ok(response.bytes().await?.to_vec())
Expand All @@ -79,7 +82,7 @@ impl RuesHttpClient {
request: &[u8],
) -> Result<Vec<u8>, Error>
where
E: Into<Option<&'static str>>,
E: Into<Option<String>>,
{
let response =
self.call_raw(target, entity, topic, request, false).await?;
Expand All @@ -97,7 +100,7 @@ impl RuesHttpClient {
feed: bool,
) -> Result<Response, Error>
where
E: Into<Option<&'static str>>,
E: Into<Option<String>>,
{
let uri = &self.uri;
let entity = entity.into().map(|e| format!(":{e}")).unwrap_or_default();
Expand Down
17 changes: 17 additions & 0 deletions rusk-wallet/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,23 @@ impl<F: SecureWalletFile + Debug> Wallet<F> {

Ok(gas_prices)
}

/// do a contract call over http
pub async fn http_contract_call(
&self,
contract_id: String,
fn_name: &str,
fn_args: Vec<u8>,
) -> Result<Vec<u8>, Error> {
let client = self.state()?.client();

// query the rusk vm
let response = client
.call("contracts", Some(contract_id), fn_name, &fn_args)
.await?;

Ok(response)
}
}

/// This structs represent a Note decoded enriched with useful chain information
Expand Down
Loading