Skip to content

refactor(contracts): use typed contracttype storage keys in flash loan and yield farming - #1986

Open
Mukhtar5527 wants to merge 1 commit into
StellarDevHub:mainfrom
Mukhtar5527:feat/typed-storage-keys-1870
Open

Mukhtar5527 wants to merge 1 commit into
StellarDevHub:mainfrom
Mukhtar5527:feat/typed-storage-keys-1870

Conversation

@Mukhtar5527

Copy link
Copy Markdown

Summary

Replaces plain string-tuple storage keys with private #[contracttype] enums in two of the three contracts named in #1870:

  • contracts/flash_loan_provider/src/lib.rs: ("admin"), ("token"), ("fee_bps") become DataKey::Admin, DataKey::Token, DataKey::FeeBps.
  • contracts/yield_farming/src/lib.rs: ("admin"), ("staking_token"), ("reward_token"), ("reward_rate"), ("total_staked") become DataKey::Admin, DataKey::StakingToken, DataKey::RewardToken, DataKey::RewardRate, DataKey::TotalStaked.

No logic, signature, or event changes. Each enum is private to its module.

What breaks if this is wrong

  • Stored-data layout change. Instance entries written under the old string keys are not read by the new code. This is the main risk. Any deployed instance of these two contracts that is upgraded in place will look uninitialized to the new code (missing admin, token, fee, reward rate, and total staked).
  • Re-initialization on upgraded flash loan instances. flash_loan_provider::initialize guards on DataKey::Admin. On an upgraded instance that guard reads nothing, so initialize can run again and overwrite the admin, token, and fee. yield_farming::initialize has no guard at all, so this was already true there.
  • Yield farming balances. On an upgraded instance, total_staked reads as 0 and reward_rate as the default 1, which misprices rewards until state is migrated.
  • Migration concern. If either contract is deployed, do not upgrade it in place. Either write a migration that copies old keys to new keys (readable only while the old keys are still present) or redeploy with fresh state. I did not confirm deployment status: contracts/deployments.md contains no entries for these two contracts on a grep of flash, yield, and access.
  • Off-chain readers. Anything that reads the old string keys directly (indexers, scripts, explorers) stops seeing data for these contracts after the change.

Scope

Done (2 of the 3 named files):

  • contracts/flash_loan_provider/src/lib.rs
  • contracts/yield_farming/src/lib.rs

Not changed, and why:

  • contracts/access_control/src/lib.rs holds no storage keys of its own. Its keys live in contracts/access_control/src/roles.rs and contracts/access_control/src/timelock.rs. roles.rs already uses a StorageKey enum, but PAUSED_KEY is still symbol_short!("AC_PAUSED"). Fixing that is a one-line change in roles.rs, which is outside the three named files, so it is left for a follow-up. Note that StorageKey::Paused already exists but is unused. Switching to it changes the stored paused flag key, so existing paused state would read as not paused after upgrade.
  • No shared type file was changed. No dependencies, lockfiles, or build scripts were touched.

Remaining work

Issue #1870 asks for 100% of workspace contracts. That is not done by this PR. The workspace has 41 crates under contracts/. Files with storage calls that still use symbol constants, Symbol::new, or literal keys (found by a pattern scan, not a per-call audit):

  • contracts/access_control/src/roles.rs (PAUSED_KEY)
  • contracts/amm/src/circuit_breaker.rs
  • contracts/commit_reveal_rng/src/lib.rs
  • contracts/hello_world/src/freelance_platform.rs
  • contracts/hello_world/src/reputation_system.rs
  • contracts/lending_pool/src/flash_loan.rs
  • contracts/lending_pool/src/lib.rs
  • contracts/multisig_wallet_timelock/src/lib.rs
  • contracts/payment_gateway/src/lib.rs
  • contracts/payment_streaming/src/lib.rs
  • contracts/quadratic_funding/src/lib.rs
  • contracts/smart_vault/src/circuit_breaker.rs
  • contracts/smart_vault/src/lib.rs
  • contracts/smart_vault/src/reentrancy_guard.rs
  • contracts/src/passkey_wallet.rs

Other crates with storage calls (for example contracts/freelance-platform, contracts/dao_governance, contracts/amm, contracts/proxy) were not individually checked and are also open. Full inventory needs a per-call review in each crate.

Verification

Verified by reading only:

  • Every changed file was read in full at base bd9921de, and the diff was checked line by line. Every storage call in the two files now uses a DataKey variant. No string keys remain.
  • Each set, get, and has call type-checks by inspection: keys are passed by reference, and value types match the original code.
  • Public signatures and return values are unchanged. Call sites were checked within each crate (the two crates have no other source files).
  • Neither crate has a build.rs. Neither has a dependency on the other or on access_control. Manifests were not changed.

NOT run:

  • cargo build, cargo test, cargo clippy, and the wasm build were not run. This PR has not been compiled.
  • No on-chain or testnet behavior was checked.

Closes #1870

…n and yield farming

- flash_loan_provider: replace the ("admin"), ("token") and ("fee_bps") string keys with a private #[contracttype] DataKey enum (Admin, Token, FeeBps).
- yield_farming: replace the ("admin"), ("staking_token"), ("reward_token"), ("reward_rate") and ("total_staked") string keys with a private #[contracttype] DataKey enum.
- Reason: string-keyed instance storage can collide across modules and across upgrade iterations (SC-073). Enum variants XDR-encode to distinct keys, so collisions are ruled out by the type system.
- No logic change: initialize, flash_loan, stake and get_reward_per_token keep the same checks, defaults (fee_bps 10, reward_rate 1) and return values. Public function signatures are unchanged.
- Consequence: the storage key layout changes. Instance entries written by previously deployed versions under the old string keys are not read by this code. flash_loan_provider initialize guards on DataKey::Admin, so an upgraded deployed instance reports as uninitialized and can be initialized again, which would overwrite its admin. Upgrading a live instance in place requires a migration, or redeployment with fresh state.
- Not changed: access_control (its lib.rs holds no storage keys; the plain symbol key remains in access_control/src/roles.rs PAUSED_KEY and is listed as remaining work).
@vercel

vercel Bot commented Nov 25, 2026

Copy link
Copy Markdown

@Mukhtar5527 is attempting to deploy a commit to the Ayomide Adeniran's projects Team on Vercel.

A member of the Team first needs to authorize it.

@drips-wave

drips-wave Bot commented Nov 25, 2026

Copy link
Copy Markdown

@Mukhtar5527 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

This branch has not been deployed

No deployments
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.

[SC-073] Storage Key Prefix Namespacing & State Collision Prevention Standard

1 participant