Skip to content

feat: surface contract function return values in Simulate and Submit UI - #2

Merged
zachfedor merged 3 commits into
theahaco:mainfrom
oceans404:feat/surface-return-values
Aug 19, 2026
Merged

zachfedor merged 3 commits into
theahaco:mainfrom
oceans404:feat/surface-return-values

Conversation

@oceans404

@oceans404 oceans404 commented Apr 1, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Closes #1

Displays contract function return values prominently at the top of both Simulate and Submit responses, right below the success alert — instead of requiring users to dig through raw JSON/XDR.

Changes

InvokeContractForm.tsx (Simulate path)

  • Extracts returnValueJson values from simulation results, filtering out void
  • Displays non-void return values in a styled box between the success alert and raw JSON
  • Uses String(v) for primitives and JSON.stringify for objects to handle all types

TransactionSuccessCard.tsx (Submit path)

  • Decodes response.result.returnValue (xdr.ScVal) using the async WASM XDR decoder
  • Uses useState + useEffect with a cancelled flag for cleanup on unmount
  • Displays the decoded return value below the success alert
  • Uses !== null check (not truthiness) so falsy values like false and 0 display correctly

Before / After

Simulate — before:

Successful Simulation — The Simulation succeeded with 1 returned value(s).

Simulate — after:

Successful Simulation — The Simulation succeeded with 1 returned value(s).
Return Value:
{ "i128": "5" }

Submit — before:

Transaction succeeded with 1 operation(s)

Submit — after:

Transaction succeeded with 1 operation(s)
Return Value:
{ "i128": "5" }

Test plan

Tested with a "type zoo" contract covering all ScVal return types:

  • tsc --noEmit passes
  • tsup build succeeds
  • Simulate read functions — return value shown (tested: total_supply, ret_true, ret_u64, ret_i128, ret_symbol, ret_string, ret_bytes, ret_address, ret_vec, ret_map, ret_struct, ret_enum_gold, ret_enum_nothing, ret_vec_of_structs, ret_some)
  • Submit read functions — return value shown (tested: ret_struct with full Monster struct decoded via async XDR path)
  • Falsy return values display correctly — ret_false shows {"bool": false}, ret_zero shows {"u64": "0"}
  • Void-returning functions show no return value section — ret_void and ret_none both show "0 returned value(s)" with no Return Value box
  • Complex return types pretty-printed — structs render as maps with key/val pairs, vecs as arrays, enums as tagged objects

🤖 Generated with Claude Code

Display return values prominently at the top of both Simulate and Submit
responses, right below the success alert.

Simulate: Extract returnValueJson from simulation results, filter out
void, and display non-void values in a styled box above the raw JSON.

Submit: Decode response.result.returnValue (xdr.ScVal) asynchronously
using the WASM XDR decoder, and display the decoded JSON. Uses
useState + useEffect with a cancelled flag for cleanup on unmount.

Handles all ScVal return types including falsy values (false, 0) by
using !== null checks instead of truthiness.

Closes theahaco#1

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@oceans404

Copy link
Copy Markdown
Contributor Author

Test contract used for validation

We created a "type zoo" contract to test all return types. Here's the source if you want to reproduce:

contracts/type-zoo/src/lib.rs
#![no_std]

use soroban_sdk::{
    contract, contractimpl, contracttype, Address, Bytes, Env, Map, String, Symbol, Vec,
};

#[contracttype]
#[derive(Clone, Debug)]
pub struct Monster {
    pub name: Symbol,
    pub hp: u32,
    pub is_alive: bool,
}

#[contracttype]
#[derive(Clone, Debug)]
pub enum Loot {
    Gold(i128),
    Weapon(Symbol),
    Nothing,
}

#[contract]
pub struct TypeZoo;

#[contractimpl]
impl TypeZoo {
    pub fn ret_true(_e: &Env) -> bool { true }
    pub fn ret_false(_e: &Env) -> bool { false }
    pub fn ret_zero(_e: &Env) -> u64 { 0 }
    pub fn ret_u64(_e: &Env) -> u64 { 42 }
    pub fn ret_i128(_e: &Env) -> i128 { 999_999_999_999_999_999 }
    pub fn ret_negative(_e: &Env) -> i128 { -12345 }
    pub fn ret_symbol(e: &Env) -> Symbol { Symbol::new(e, "hello") }
    pub fn ret_string(e: &Env) -> String { String::from_str(e, "Hello from the type zoo!") }
    pub fn ret_bytes(e: &Env) -> Bytes { Bytes::from_slice(e, &[0xDE, 0xAD, 0xBE, 0xEF]) }
    pub fn ret_address(e: &Env) -> Address { e.current_contract_address() }
    pub fn ret_vec(e: &Env) -> Vec<u32> {
        let mut v = Vec::new(&e);
        v.push_back(10); v.push_back(20); v.push_back(30);
        v
    }
    pub fn ret_map(e: &Env) -> Map<Symbol, i128> {
        let mut m = Map::new(&e);
        m.set(Symbol::new(&e, "apples"), 5);
        m.set(Symbol::new(&e, "bananas"), 12);
        m.set(Symbol::new(&e, "cherries"), 99);
        m
    }
    pub fn ret_struct(e: &Env) -> Monster {
        Monster { name: Symbol::new(&e, "Dragon"), hp: 9999, is_alive: true }
    }
    pub fn ret_enum_gold(_e: &Env) -> Loot { Loot::Gold(500) }
    pub fn ret_enum_nothing(_e: &Env) -> Loot { Loot::Nothing }
    pub fn ret_vec_of_structs(e: &Env) -> Vec<Monster> {
        let mut v = Vec::new(&e);
        v.push_back(Monster { name: Symbol::new(&e, "Goblin"), hp: 50, is_alive: true });
        v.push_back(Monster { name: Symbol::new(&e, "Skeleton"), hp: 0, is_alive: false });
        v
    }
    pub fn ret_some(_e: &Env) -> Option<u64> { Some(7) }
    pub fn ret_none(_e: &Env) -> Option<u64> { None }
    pub fn ret_void(_e: &Env) { }
}
contracts/type-zoo/Cargo.toml
[package]
name = "type-zoo"
edition.workspace = true
license.workspace = true
repository.workspace = true
publish = false
version.workspace = true

[lib]
crate-type = ["cdylib"]
doctest = false

[dependencies]
soroban-sdk = { workspace = true }

[dev-dependencies]
soroban-sdk = { workspace = true, features = ["testutils"] }

Add to environments.toml:

type_zoo = { client = true }

All 19 functions tested via Simulate, falsy edge cases (ret_false, ret_zero) and complex types (ret_struct, ret_vec_of_structs) also tested via Submit. Void functions (ret_void, ret_none) correctly show no return value section.

zachfedor and others added 2 commits August 18, 2026 21:21
Extract the duplicated return value markup into a shared ReturnValueBox
component and harden the async ScVal decode in TransactionSuccessCard.

- Replace the `returnValue !== null` sentinel with an explicit DecodeState
  union, so "not yet decoded", "no return value", "void" and "decode failed"
  are distinguishable from a legitimately falsy decoded value.
- Reset decode state on response change and set an explicit state in the
  no-return-value and error branches, so a previous transaction's return
  value can no longer persist into a later response.
- Surface "Return value could not be decoded." on decode failure instead of
  only logging to the console, per issue theahaco#1.
- Fix the no-floating-promises lint error and import ordering.
- Add component tests covering falsy values, void filtering, absent return
  values, decode failure, and the stale-value regression.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@zachfedor
zachfedor merged commit a33ed2a into theahaco:main Aug 19, 2026
5 checks passed
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 1.4.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Surface contract function return values prominently in the UI

2 participants