Skip to content

Commit

Permalink
rusk-wallet: Implement http contract query
Browse files Browse the repository at this point in the history
Signed-off-by: Daksh <[email protected]>
  • Loading branch information
Daksh14 committed Dec 11, 2024
1 parent c65f155 commit c87cf08
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 23 deletions.
Binary file added rusk-wallet/my_first_contract.wasm
Binary file not shown.
23 changes: 20 additions & 3 deletions rusk-wallet/src/bin/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,8 +545,11 @@ impl Command {
.try_into()
.map_err(|_| Error::InvalidContractId)?;

let call = ContractCall::new(contract_id, fn_name, &fn_args)
.map_err(|_| Error::Rkyv)?;
println!("{:?}", fn_args);

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 +576,14 @@ impl Command {
}
}?;

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

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

writeln!(f, "> Http contract query: {string}",)
}
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
19 changes: 19 additions & 0 deletions rusk-wallet/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,8 @@ impl<F: SecureWalletFile + Debug> Wallet<F> {
let db_pos = state.cache().last_pos()?.unwrap_or(0);
let network_last_pos = state.fetch_num_notes().await? - 1;

println!("{} {}", db_pos, network_last_pos);

Ok(network_last_pos == db_pos)
}

Expand Down Expand Up @@ -622,6 +624,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: &str,
) -> Result<String, Error> {
let client = self.state()?.client();

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

Ok(String::from_utf8_lossy(&response).to_string())
}
}

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

0 comments on commit c87cf08

Please sign in to comment.