Looking at this example of invoking smart contract instance from outside:
If the return value from the smart contract is a JSON structure, what is the preferred way to deserialize it outside the smart contract to the same structure? Is there any standard way?:
In the smart contract testing we can do something like this with the parse_return value:
let update = chain
.contract_update(SIGNER, ALICE, ALICE_ADDR, Energy::from(1000), UpdateContractPayload {
amount: Amount::zero(),
address: init.contract_address,
receive_name: OwnedReceiveName::new_unchecked(“dummyt.view”.to_string()),
message: OwnedParameter::empty(),
}).expect(“Invoke view”);
let view_state: dummy::ViewState = update.parse_return_value().expect(“Failed to decode ViewState”);
Good suggestion. We should add a similar helper function to the ReturnValue type:
Unitl that is available, here is the conversion helper function from the testing_library which might be useful if you implement the conversion on your side.
/// Try to parse the return value into a type that implements [`Deserial`].
///
/// Ensures that all bytes of the return value are read.
pub fn parse_return_value<T: Deserial>(&self) -> ParseResult<T> {
use contracts_common::{Cursor, Get, ParseError};
let mut cursor = Cursor::new(&self.return_value);
let res = cursor.get()?;
// Check that all bytes have been read, as leftover bytes usually indicate
// errors.
if cursor.offset != self.return_value.len() {
return Err(ParseError::default());
}
Ok(res)
}