Skip to content

Issue #2 — Contract Initialization & Admin Queries #599

@yusuftomilola

Description

@yusuftomilola

With the scaffold in place, the contract needs a one-time initialize entry point to store the admin address and payment token. We also need the two public query functions (admin, payment_token) so that front-ends and tests can read back the configured values.

Task

Building on the code from Issue #1, add the following public functions to the #[contractimpl] block:

  1. pub fn initialize(env: Env, admin: Address, payment_token: Address) -> Result<(), Error>

    • Return Error::AlreadyInitialized if DataKey::Admin already exists in instance storage.
    • Call admin.require_auth().
    • Write admin to DataKey::Admin and payment_token to DataKey::PaymentToken in instance storage.
    • Emit an event: topic = (symbol_short!("init"),), data = (admin, payment_token).
  2. pub fn admin(env: Env) -> Result<Address, Error>

    • Delegate to Self::get_admin(&env).
  3. pub fn payment_token(env: Env) -> Result<Address, Error>

    • Delegate to Self::get_payment_token(&env).

Files to modify

  • contracts/workspace_booking/src/lib.rs

Acceptance criteria

  • cargo check -p workspace_booking passes.
  • cargo clippy -p workspace_booking -- -D warnings passes.
  • Calling initialize twice panics with Error(Contract, #3) (AlreadyInitialized).
  • admin() returns the address passed to initialize.
  • payment_token() returns the token address passed to initialize.
  • The init event is emitted on successful initialization.

Technical notes

  • Guard against double-initialization with env.storage().instance().has(&DataKey::Admin).
  • symbol_short! macros are limited to 9 characters.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions