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

fix: parse XDR Result to JSON; encode RawVal return types #541

Merged
merged 3 commits into from
Mar 31, 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
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)
chadoh marked this conversation as resolved.
Show resolved Hide resolved
| (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