Skip to content

Commit

Permalink
fix: parse XDR Result to JSON; encode RawVal return types (#541)
Browse files Browse the repository at this point in the history
* fix: parse XDR Result to JSON

The test here copies the logic of [the errors
example](https://github.com/stellar/soroban-examples/blob/main/errors/src/lib.rs),
showing that valid `Ok` results were not being correctly parsed to JSON.

Co-authored-by: Willem Wyndham <willem@ahalabs.dev>

* fix(strval): encode RawVal return types

---------

Co-authored-by: Willem Wyndham <willem@ahalabs.dev>
Co-authored-by: Paul Bellamy <paul@stellar.org>
  • Loading branch information
3 people authored Mar 31, 2023
1 parent 9f15784 commit 4ffd829
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![no_std]
use soroban_sdk::{
contractimpl, contracttype, vec, Address, Bytes, BytesN, Env, Map, Set, String, Symbol, Vec,
I256, U256,
contracterror, contractimpl, contracttype, vec, Address, Bytes, BytesN, Env, Map, RawVal, Set,
String, Symbol, Vec, I256, U256,
};

pub struct Contract;
Expand Down Expand Up @@ -43,12 +43,35 @@ pub enum ComplexEnum {
Void,
}

#[contracterror]
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
#[repr(u32)]
pub enum Error {
OhNo = 1,
}

#[contractimpl]
impl Contract {
pub fn hello(_env: Env, hello: Symbol) -> Symbol {
hello
}

pub fn void(_env: Env) {
// do nothing
}

pub fn raw_val(_env: Env) -> RawVal {
RawVal::default()
}

pub fn u32_fail_on_even(_env: Env, u32_: u32) -> Result<u32, Error> {
if u32_ % 2 == 1 {
Ok(u32_)
} else {
Err(Error::OhNo)
}
}

pub fn u32_(_env: Env, u32_: u32) -> u32 {
u32_
}
Expand Down
38 changes: 38 additions & 0 deletions cmd/crates/soroban-test/tests/it/custom_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,44 @@ fn number_arg() {
invoke_with_roundtrip("u32_", 42);
}

#[test]
fn number_arg_return_ok() {
invoke(&TestEnv::default(), "u32_fail_on_even")
.arg("--u32_")
.arg("1")
.assert()
.success()
.stdout("1\n");
}

#[test]
fn number_arg_return_err() {
invoke(&TestEnv::default(), "u32_fail_on_even")
.arg("--u32_")
.arg("2")
.assert()
.success()
.stderr(predicates::str::contains("Status(ContractError(1))"));
}

#[test]
fn void() {
invoke(&TestEnv::default(), "void")
.assert()
.success()
.stdout("\n")
.stderr("");
}

#[test]
fn raw_val() {
invoke(&TestEnv::default(), "raw_val")
.assert()
.success()
.stdout("null\n")
.stderr("");
}

#[test]
fn i32() {
invoke_with_roundtrip("i32_", 42);
Expand Down
8 changes: 7 additions & 1 deletion cmd/soroban-cli/src/strval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,8 @@ impl Spec {
/// May panic
pub fn xdr_to_json(&self, val: &ScVal, output: &ScType) -> Result<Value, Error> {
Ok(match (val, output) {
(ScVal::Map(None) | ScVal::Vec(None), ScType::Option(_)) => Value::Null,
(ScVal::Void, ScType::Val)
| (ScVal::Map(None) | ScVal::Vec(None), ScType::Option(_)) => Value::Null,
(ScVal::Bool(_), ScType::Bool)
| (ScVal::Void, ScType::Void)
| (ScVal::String(_), ScType::String)
Expand Down Expand Up @@ -650,6 +651,11 @@ impl Spec {

(ScVal::Address(v), ScType::Address) => sc_address_to_json(v),

(ok_val, ScType::Result(result_type)) => {
let ScSpecTypeResult { ok_type, .. } = result_type.as_ref();
self.xdr_to_json(ok_val, ok_type)?
}

(x, y) => return Err(Error::InvalidPair(x.clone(), y.clone())),
})
}
Expand Down

0 comments on commit 4ffd829

Please sign in to comment.