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

Added high level ViewResult type #5

Merged
merged 1 commit into from
Dec 1, 2023
Merged
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
4 changes: 2 additions & 2 deletions src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,8 @@ impl<S> Transaction<'_, S> {
if let Ok(actions) = &mut self.actions {
actions.push(
AddKeyAction {
public_key: pk.into(),
access_key: ak.into(),
public_key: pk,
access_key: ak,
}
.into(),
);
Expand Down
42 changes: 40 additions & 2 deletions src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ pub struct GasPrice;

impl ProcessQuery for ViewFunction {
type Method = methods::query::RpcQueryRequest;
type Output = CallResult;
type Output = ViewResult;

fn into_request(self, block_reference: BlockReference) -> Result<Self::Method> {
Ok(Self::Method {
Expand All @@ -172,7 +172,7 @@ impl ProcessQuery for ViewFunction {

fn from_response(resp: RpcQueryResponse) -> Result<Self::Output> {
match resp.kind {
QueryResponseKind::CallResult(result) => Ok(result),
QueryResponseKind::CallResult(result) => Ok(result.into()),
_ => Err(Error::RpcReturnedInvalidData(
"while querying account".into(),
)),
Expand Down Expand Up @@ -455,6 +455,44 @@ impl<'a> std::future::IntoFuture for QueryChunk<'a> {
}
}

/// The result from a call into a View function. This contains the contents or
/// the results from the view function call itself. The consumer of this object
/// can choose how to deserialize its contents.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct ViewResult {
/// Our result from our call into a view function.
pub result: Vec<u8>,
/// Logs generated from the view function.
pub logs: Vec<String>,
}

impl ViewResult {
/// Deserialize an instance of type `T` from bytes of JSON text sourced from the
/// execution result of this call. This conversion can fail if the structure of
/// the internal state does not meet up with [`serde::de::DeserializeOwned`]'s
/// requirements.
pub fn json<T: serde::de::DeserializeOwned>(&self) -> Result<T> {
Ok(serde_json::from_slice(&self.result)?)
}

/// Deserialize an instance of type `T` from bytes sourced from this view call's
/// result. This conversion can fail if the structure of the internal state does
/// not meet up with [`borsh::BorshDeserialize`]'s requirements.
pub fn borsh<T: borsh::BorshDeserialize>(&self) -> Result<T> {
Ok(borsh::BorshDeserialize::try_from_slice(&self.result)?)
}
}

impl From<CallResult> for ViewResult {
fn from(result: CallResult) -> Self {
Self {
result: result.result,
logs: result.logs,
}
}
}

impl Client {
/// Call into a contract's view function. Returns a [`Query`] which allows us
/// to specify further details like the arguments of the view call, or at what
Expand Down