Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
name = "stellar-router"
resolver = "2"
members = [
"contracts/router-common",
"contracts/router-core",
"contracts/router-registry",
"contracts/router-access",
Expand Down
1 change: 1 addition & 0 deletions contracts/router-access/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ crate-type = ["cdylib", "rlib"]

[dependencies]
soroban-sdk = { workspace = true }
router-common = { path = "../router-common" }

[dev-dependencies]
soroban-sdk = { version = "21.7.6", features = ["testutils"] }
7 changes: 7 additions & 0 deletions contracts/router-common/src/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "router-common"
version = "0.1.0"
edition = "2021"

[dependencies]
soroban-sdk = { version = "22", default-features = false } # Match your other contracts' version
37 changes: 37 additions & 0 deletions contracts/router-common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#![no_std]

use soroban_sdk::{Address, Env, Symbol};

/// Macro to require admin with custom error types.
///
/// This eliminates the repetitive `require_admin` / `require_super_admin` boilerplate
/// across all router contracts while allowing each contract to use its own error enum.
#[macro_export]
macro_rules! require_admin {
($env:expr, $caller:expr, $data_key:expr, $not_init_err:expr, $unauth_err:expr) => {{
let admin: soroban_sdk::Address = $env
.storage()
.instance()
.get($data_key)
.ok_or($not_init_err)?;

if &admin != $caller {
return Err($unauth_err);
}
Ok(())
}};
}

/// Convenience version when using DataKey::Admin and standard error variants
#[macro_export]
macro_rules! require_admin_simple {
($env:expr, $caller:expr, $data_key:expr, $error_type:ty) => {
$crate::require_admin!(
$env,
$caller,
$data_key,
<$error_type>::NotInitialized,
<$error_type>::Unauthorized
)
};
}
1 change: 1 addition & 0 deletions contracts/router-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ crate-type = ["cdylib", "rlib"]

[dependencies]
soroban-sdk = { workspace = true }
router-common = { path = "../router-common" }

[dev-dependencies]
soroban-sdk = { version = "21.7.6", features = ["testutils"] }
1 change: 1 addition & 0 deletions contracts/router-middleware/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ crate-type = ["cdylib", "rlib"]

[dependencies]
soroban-sdk = { workspace = true }
router-common = { path = "../router-common" }

[dev-dependencies]
soroban-sdk = { version = "21.7.6", features = ["testutils"] }
32 changes: 17 additions & 15 deletions contracts/router-middleware/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,24 +588,26 @@ impl RouterMiddleware {
/// # Errors
/// * [`MiddlewareError::Unauthorized`] — if `current` is not the admin.
/// * [`MiddlewareError::NotInitialized`] — if the contract has not been initialized.
pub fn transfer_admin(
env: Env,
current: Address,
new_admin: Address,
) -> Result<(), MiddlewareError> {
current.require_auth();
Self::require_admin(&env, &current)?;
pub fn transfer_admin(env: Env, current: Address, new_admin: Address) -> Result<(), MiddlewareError> {
current.require_auth();

env.storage().instance().set(&DataKey::Admin, &new_admin);
// One-liner using the shared macro
router_common::require_admin_simple!(
&env,
&current,
&DataKey::Admin,
MiddlewareError
)?;

// ← FIXED: Emit event to match other contracts (router-access, router-core, etc.)
env.events().publish(
(Symbol::new(&env, "admin_transferred"),),
(current.clone(), new_admin.clone()),
);
env.storage().instance().set(&DataKey::Admin, &new_admin);

Ok(())
}
env.events().publish(
(Symbol::new(&env, "admin_transferred"),),
(current, new_admin),
);

Ok(())
}

// ── Helpers ───────────────────────────────────────────────────────────────

Expand Down
1 change: 1 addition & 0 deletions contracts/router-multicall/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ crate-type = ["cdylib", "rlib"]

[dependencies]
soroban-sdk = { workspace = true }
router-common = { path = "../router-common" }

[dev-dependencies]
soroban-sdk = { version = "21.7.6", features = ["testutils"] }
1 change: 1 addition & 0 deletions contracts/router-registry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ crate-type = ["cdylib", "rlib"]

[dependencies]
soroban-sdk = { workspace = true }
router-common = { path = "../router-common" }

[dev-dependencies]
soroban-sdk = { version = "21.7.6", features = ["testutils"] }
1 change: 1 addition & 0 deletions contracts/router-timelock/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ crate-type = ["cdylib", "rlib"]

[dependencies]
soroban-sdk = { workspace = true }
router-common = { path = "../router-common" }

[dev-dependencies]
soroban-sdk = { version = "21.7.6", features = ["testutils"] }
26 changes: 20 additions & 6 deletions contracts/router-timelock/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -804,12 +804,26 @@ impl RouterTimelock {
/// # Errors
/// * [`TimelockError::Unauthorized`] — if `current` is not the admin.
/// * [`TimelockError::NotInitialized`] — if the contract has not been initialized.
pub fn transfer_admin(env: Env, current: Address, new_admin: Address) -> Result<(), TimelockError> {
current.require_auth();
Self::require_admin(&env, &current)?;
env.storage().instance().set(&DataKey::Admin, &new_admin);
Ok(())
}
pub fn transfer_admin(env: Env, current: Address, new_admin: Address) -> Result<(), MiddlewareError> {
current.require_auth();

// One-liner using the shared macro
router_common::require_admin_simple!(
&env,
&current,
&DataKey::Admin,
MiddlewareError
)?;

env.storage().instance().set(&DataKey::Admin, &new_admin);

env.events().publish(
(Symbol::new(&env, "admin_transferred"),),
(current, new_admin),
);

Ok(())
}

// ── Helpers ───────────────────────────────────────────────────────────────

Expand Down
Loading