Skip to content
Merged
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
55 changes: 20 additions & 35 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,12 +360,7 @@ impl StableRouteRouter {
/// that `compute_route_fee` and `quote_route` already raise, keeping
/// one error code for "this pair does not exist" across the contract.
fn require_pair_registered(env: &Env, source: &Symbol, destination: &Symbol) {
if !env
.storage()
.persistent()
.get::<_, bool>(&DataKey::Pair(source.clone(), destination.clone()))
.unwrap_or(false)
{
if !Self::read_pair_registered(env, source, destination) {
panic_with_error!(env, RouterError::PairNotRegistered);
}
}
Expand Down Expand Up @@ -437,6 +432,19 @@ impl StableRouteRouter {
.publish((symbol_short!("executed"),), new_admin);
}

/// Read whether `(source, destination)` is a registered pair from
/// persistent storage.
///
/// Returns `false` when the slot is absent — the documented default for an
/// unregistered pair. This is the single source of truth for the
/// [`DataKey::Pair`] sentinel value.
fn read_pair_registered(env: &Env, source: &Symbol, destination: &Symbol) -> bool {
env.storage()
.persistent()
.get(&DataKey::Pair(source.clone(), destination.clone()))
.unwrap_or(false)
}

/// Read the per-pair fee in basis points from persistent storage.
///
/// Returns `0` (free) when the slot is absent — the documented
Expand Down Expand Up @@ -801,11 +809,7 @@ impl StableRouteRouter {
/// Returns true iff the pair is registered AND has non-zero
/// reported liquidity. Useful as a quick is-routable check.
pub fn is_pair_active(env: Env, source: Symbol, destination: Symbol) -> bool {
let s = env.storage().persistent();
if !s
.get::<_, bool>(&DataKey::Pair(source.clone(), destination.clone()))
.unwrap_or(false)
{
if !Self::read_pair_registered(&env, &source, &destination) {
return false;
}
Self::read_pair_liquidity(&env, &source, &destination) > 0
Expand All @@ -816,9 +820,7 @@ impl StableRouteRouter {
pub fn get_pair_info(env: Env, source: Symbol, destination: Symbol) -> PairInfo {
let s = env.storage().persistent();
PairInfo {
registered: s
.get(&DataKey::Pair(source.clone(), destination.clone()))
.unwrap_or(false),
registered: Self::read_pair_registered(&env, &source, &destination),
fee_bps: Self::read_pair_fee_bps(&env, &source, &destination),
min_amount: Self::read_pair_min(&env, &source, &destination),
max_amount: Self::read_pair_max(&env, &source, &destination),
Expand All @@ -839,9 +841,7 @@ impl StableRouteRouter {
pub fn get_pair_info_ext(env: Env, source: Symbol, destination: Symbol) -> PairInfoExt {
let s = env.storage().persistent();
PairInfoExt {
registered: s
.get(&DataKey::Pair(source.clone(), destination.clone()))
.unwrap_or(false),
registered: Self::read_pair_registered(&env, &source, &destination),
fee_bps: Self::read_pair_fee_bps(&env, &source, &destination),
min_amount: Self::read_pair_min(&env, &source, &destination),
max_amount: Self::read_pair_max(&env, &source, &destination),
Expand Down Expand Up @@ -881,14 +881,7 @@ impl StableRouteRouter {
if amount <= 0 {
panic_with_error!(&env, RouterError::AmountMustBePositive);
}
if !env
.storage()
.persistent()
.get::<_, bool>(&DataKey::Pair(source.clone(), destination.clone()))
.unwrap_or(false)
{
panic_with_error!(&env, RouterError::PairNotRegistered);
}
Self::require_pair_registered(&env, &source, &destination);
if matches!(
env.storage()
.persistent()
Expand Down Expand Up @@ -1280,10 +1273,7 @@ impl StableRouteRouter {
///
/// Registration is independent of liquidity or routing activity.
pub fn is_pair_registered(env: Env, source: Symbol, destination: Symbol) -> bool {
env.storage()
.persistent()
.get(&DataKey::Pair(source, destination))
.unwrap_or(false)
Self::read_pair_registered(&env, &source, &destination)
}

/// Configure the routing fee for a single registered pair.
Expand Down Expand Up @@ -1388,12 +1378,7 @@ impl StableRouteRouter {
Self::route_abort(&env, RouterError::AmountMustBePositive);
}

if !env
.storage()
.persistent()
.get::<_, bool>(&DataKey::Pair(source.clone(), destination.clone()))
.unwrap_or(false)
{
if !Self::read_pair_registered(&env, &source, &destination) {
Self::route_abort(&env, RouterError::PairNotRegistered);
}
let min_amount = Self::read_pair_min(&env, &source, &destination);
Expand Down