Skip to content

Commit

Permalink
chore: expose raw_bytes API (#133)
Browse files Browse the repository at this point in the history
* Expose raw_bytes API

* Renamed try_into_bytes into raw_bytes

* CHANGELOG.md
  • Loading branch information
ChaoticTempest authored May 12, 2022
1 parent 5271dec commit ee29751
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
### Changed
- Updated default sandbox version to `97c0410de519ecaca369aaee26f0ca5eb9e7de06` commit of nearcore to include 1.26 protocol changes https://github.com/near/workspaces-rs/pull/134

- Exposed `CallExecutionDetails::raw_bytes` API: https://github.com/near/workspaces-rs/pull/133

## [0.2.1] - 2022-04-12

### Added
Expand Down
9 changes: 6 additions & 3 deletions workspaces/src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,22 @@ impl CallExecutionDetails {
/// the internal state does not meet up with [`serde::de::DeserializeOwned`]'s
/// requirements.
pub fn json<T: serde::de::DeserializeOwned>(&self) -> anyhow::Result<T> {
let buf = self.try_into_bytes()?;
let buf = self.raw_bytes()?;
serde_json::from_slice(&buf).map_err(Into::into)
}

/// Deserialize an instance of type `T` from bytes sourced from the execution
/// 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) -> anyhow::Result<T> {
let buf = self.try_into_bytes()?;
let buf = self.raw_bytes()?;
borsh::BorshDeserialize::try_from_slice(&buf).map_err(Into::into)
}

fn try_into_bytes(&self) -> anyhow::Result<Vec<u8>> {
/// Grab the underlying raw bytes returned from calling into a contract's function.
/// If we want to deserialize these bytes into a rust datatype, use [`CallExecutionDetails::json`]
/// or [`CallExecutionDetails::borsh`] instead.
pub fn raw_bytes(&self) -> anyhow::Result<Vec<u8>> {
let result: &str = match self.status {
FinalExecutionStatus::SuccessValue(ref val) => val,
FinalExecutionStatus::Failure(ref err) => anyhow::bail!(err.clone()),
Expand Down

0 comments on commit ee29751

Please sign in to comment.