Skip to content

Commit

Permalink
rusk-wallet: Add rkyv serialization for the return value of http cont…
Browse files Browse the repository at this point in the history
…ract query
  • Loading branch information
Daksh14 committed Dec 12, 2024
1 parent c87cf08 commit eba9217
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
Binary file removed rusk-wallet/my_first_contract.wasm
Binary file not shown.
11 changes: 7 additions & 4 deletions rusk-wallet/src/bin/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,8 +545,6 @@ impl Command {
.try_into()
.map_err(|_| Error::InvalidContractId)?;

println!("{:?}", fn_args);

let call =
ContractCall::new(contract_id, fn_name.clone(), &fn_args)
.map_err(|_| Error::Rkyv)?;
Expand Down Expand Up @@ -577,12 +575,17 @@ impl Command {
}?;

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)
.http_contract_call(contract_id, &fn_name, fn_args)
.await?;

let http_call = if let Some(x) = http_call {
x.to_string()
} else {
"<empty>".to_string()
};

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

Expand Down
20 changes: 14 additions & 6 deletions rusk-wallet/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,8 +588,6 @@ 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 @@ -630,16 +628,26 @@ impl<F: SecureWalletFile + Debug> Wallet<F> {
&self,
contract_id: String,
fn_name: &str,
fn_args: &str,
) -> Result<String, Error> {
fn_args: Vec<u8>,
) -> Result<Option<u32>, Error> {
let client = self.state()?.client();

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

Ok(String::from_utf8_lossy(&response).to_string())
if !response.is_empty() {
// wasm can only return u32 right now
// NOTE: This will fail if the contract returns a u64
// for a 64 bit contract
let value: u32 =
rkyv::from_bytes(&response).map_err(|_| Error::Rkyv)?;

Ok(Some(value))
} else {
Ok(None)
}
}
}

Expand Down

0 comments on commit eba9217

Please sign in to comment.