The AtomicSwap::initialize function previously used a generic assert! to prevent double initialization:
assert!(!env.storage().instance().has(&DataKey::Config), "already initialized");While functional for internal debugging, this approach does not provide a structured error code that client-side applications or other smart contracts can reliably catch and interpret. For production-grade contracts, it is better to use Soroban's panic_with_error! macro with a dedicated ContractError variant.
This change introduces a structured error handling mechanism for the initialization process:
- ContractError Extension: A new variant
AlreadyInitialized = 4was added to theContractErrorenum. - Logic Refactor: The
assert!was replaced with a conditional check that triggerspanic_with_error!(&env, ContractError::AlreadyInitialized)if the configuration already exists in the contract's storage. - Automated Verification: A unit test
test_initialize_twice_panicswas added to ensure that the contract correctly panics withError(Contract, #4)when initialization is attempted more than once.
- Affected File:
contracts/atomic_swap/src/lib.rs - Error Code:
4(Contract Error) - Tooling used: Soroban SDK
panic_with_error!macro.
issue #98