Skip to content

[28] Replace inline require_admin boilerplate with a shared helper #28

Description

@merlik787-droi

Problem Statement

Every contract repeats the same five-line pattern in admin-only functions:

admin.require_auth();
let stored_admin: Address = env.storage().instance().get(&DataKey::Admin).expect("...");
assert!(admin == stored_admin, "Unauthorized");
// ... business logic

This is verbose, prone to copy/paste error (the auth check is sometimes assert!, sometimes panic!("Unauthorized"), with random string variations), and lacks a structured ContractError::Unauthorized enum value.

Why It Matters

  • Inconsistent error strings across contracts (some say "Unauthorized", others "Unauthorized: Caller is not the protocol admin", others "Contract not initialized", others "Not initialized") make off-chain error handling fragile.
  • Boilerplate crowding obscures business logic.

Expected Outcome

  • A shared helper contracts_common::auth::require_admin(env, expected: &Address).
  • A ContractError enum with explicit variants.
  • Migration of all six contracts.

Acceptance Criteria

  • grep -r "Unauthorized" contracts/*/src/lib.rs returns only references inside the (now centralized) error enum.
  • All tests still pass.
  • New tests for the helper edge cases.

Implementation Notes

Files / Modules Affected

All six contracts; new contracts/common/src/auth.rs and errors.rs.

Dependencies

Issue #7.

Difficulty

Medium.

Estimated Effort

1–2 days.

Suggested Labels

quality, refactor, P2, tech-debt


Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions