Skip to content

Fix init option storage mismatch and add tests - #1338

Open
Peolite1 wants to merge 1 commit into
CalloraOrg:mainfrom
Peolite1:fix-init-1104
Open

Peolite1 wants to merge 1 commit into
CalloraOrg:mainfrom
Peolite1:fix-init-1104

Conversation

@Peolite1

Copy link
Copy Markdown

Title

Fix initial balance and authorized caller storage serialization (Closes #1104)

Description

This PR fixes a bug in the Vault contract initialization where initial_balance and authorized_caller (which are passed as Option types) were being written to instance storage natively as Option types. This caused a downstream deserialization failure during subsequent getter calls since the contract functions (e.g., deposit, deduct, balance) expect those fields to be resolved primitive types (like i128 and Address).

Changes Made

  • Resolved initial_balance: We now gracefully unwrap initial_balance during init using .unwrap_or(0) before writing it to DataKey::Balance as a plain i128.
  • Negative Balance Guard: Implemented a new validation check that cleanly returns VaultError::InitialBalanceNegative if the resolved initial balance evaluates to a negative integer.
  • Audited AuthorizedCaller Storage: Fixed a similar discrepancy with DataKey::AuthorizedCaller. We now conditionally save the underlying Address if Some is provided, preventing ScVal::Void / Option mismatch failures down the line.
  • Added Regression Tests: Appended a new test case (init_none_initial_balance_allows_deposit_and_deduct) to contracts/vault/src/test_init_hardening.rs that explicitly initializes the vault with a None balance and verifies that both subsequent deposit() and deduct() calls correctly run and report valid math.

Security and Failure-Mode Handling

Writing ScVal::Void into instance storage could permanently brick newly deployed vaults since the first deposit or deduction would fail parsing the storage slot into i128. By strictly converting these arguments into their plain primitive types at initialization, we eliminate the runtime crash and restore resilient type safety across the vault's core logic.

Closes #1104

@drips-wave

drips-wave Bot commented Sep 30, 2026

Copy link
Copy Markdown

@Peolite1 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Store vault initial balance as plain i128 value

1 participant