Skip to content

[5] Add upgrade() function to all 5 contracts #5

Description

@EmeditWeb

Problem

None of the 5 contracts (creditline, liquidity-pool, reputation, vendor-registry, token-mock) currently expose an upgrade() entry point. Once deployed to testnet, any bug fix requires a full redeploy and address change — breaking every client that references the old address.

Context

Soroban supports in-place WASM upgrades preserving contract address and storage. Without this, every contract bugfix invalidates the API's contract IDs and the mobile app's hardcoded references. Upgradeability is the difference between a one-shot deploy and a maintainable protocol.

Before Starting

Read these context files first:

  • context/architecture-context.md
  • context/code-standards.md
  • context/progress-tracker.md
  • contracts/*/src/lib.rs

What To Build

  1. Define a shared snippet (in each contract's lib.rs):
    pub fn upgrade(env: Env, new_wasm_hash: BytesN<32>) -> Result<(), ContractError> {
        let admin: Address = env.storage().instance().get(&StorageKey::Admin).ok_or(ContractError::NotInitialized)?;
        admin.require_auth();
        env.deployer().update_current_contract_wasm(new_wasm_hash);
        Ok(())
    }
  2. Add to all 5 contracts: creditline, liquidity-pool, reputation, vendor-registry, token-mock.
  3. Emit a CONTRACTUPGRADED event with (old_version, new_version, ts) — add a get_version() -> u32 function returning current version, bumped by upgrade.
  4. Persist StorageKey::Version in each contract's instance storage, default 1.
  5. Write one unit test per contract: non-admin call rejected, admin call succeeds (mock the wasm hash).
  6. Document the upgrade flow in contracts/README.md.

Files To Touch

  • contracts/creditline-contract/src/lib.rs
  • contracts/liquidity-pool-contract/src/lib.rs
  • contracts/reputation-contract/src/lib.rs
  • contracts/vendor-registry-contract/src/lib.rs
  • contracts/token-mock-contract/src/lib.rs
  • Each contract's events.rs, storage.rs, errors.rs
  • contracts/README.md

Acceptance Criteria

  • All 5 contracts expose upgrade(env, new_wasm_hash)
  • All 5 contracts expose get_version() returning a u32
  • Admin auth check is the FIRST executable line of upgrade()
  • 5 new tests pass (one per contract)
  • CONTRACTUPGRADED event emitted on successful upgrade
  • Upgrade flow documented in README

Mandatory Checks Before PR

  • cargo build passes with zero errors
  • cargo test — all 93 existing tests still pass
  • require_auth() is FIRST line of every mutating function
  • extend_ttl() called after EVERY persistent storage write
  • New unit tests written for every new function
  • context/progress-tracker.md updated

Metadata

Metadata

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions