Repository navigation
feat: surface contract function return values in Simulate and Submit UI - #2
Merged
Merged
Conversation
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>
Contributor
Author
Test contract used for validationWe 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 type_zoo = { client = true }All 19 functions tested via Simulate, falsy edge cases ( |
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>
|
🎉 This PR is included in version 1.4.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
1 task done
1 task done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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)returnValueJsonvalues from simulation results, filtering outvoidString(v)for primitives andJSON.stringifyfor objects to handle all typesTransactionSuccessCard.tsx(Submit path)response.result.returnValue(xdr.ScVal) using the async WASM XDR decoderuseState+useEffectwith acancelledflag for cleanup on unmount!== nullcheck (not truthiness) so falsy values likefalseand0display correctlyBefore / After
Simulate — before:
Simulate — after:
Submit — before:
Submit — after:
Test plan
Tested with a "type zoo" contract covering all ScVal return types:
tsc --noEmitpassestsupbuild succeedstotal_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)ret_structwith full Monster struct decoded via async XDR path)ret_falseshows{"bool": false},ret_zeroshows{"u64": "0"}ret_voidandret_noneboth show "0 returned value(s)" with no Return Value box🤖 Generated with Claude Code