Skip to content

Commit

Permalink
rename functions
Browse files Browse the repository at this point in the history
  • Loading branch information
JanKuczma committed Sep 25, 2024
1 parent 779f4b4 commit 836d435
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 23 deletions.
43 changes: 22 additions & 21 deletions amm/contracts/router_v2/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub mod router_v2 {
/// Returns Pool for `pool_id` if it exists.
/// Adds the Pool to the cache.
#[inline]
fn get_pool(&mut self, pool_id: AccountId) -> Result<Pool, RouterV2Error> {
fn get_and_cache_pool(&mut self, pool_id: AccountId) -> Result<Pool, RouterV2Error> {
match self.cached_pools.get(pool_id) {
Some(pool) => Ok(pool),
None => {
Expand All @@ -62,11 +62,12 @@ pub mod router_v2 {
}

/// Returns StablePool for `pool_id`.
/// Adds the StablePool to the cache.
#[inline]
fn get_stable_pool(&mut self, pool_id: AccountId) -> Result<StablePool, RouterV2Error> {
match self.get_pool(pool_id) {
Ok(Pool::StablePool(pool)) => Ok(pool),
_ => Err(RouterV2Error::PoolNotFound),
fn get_and_cache_stable_pool(&mut self, pool_id: AccountId) -> Result<StablePool, RouterV2Error> {
match self.get_and_cache_pool(pool_id)? {
Pool::StablePool(pool) => Ok(pool),
Pool::Pair(_) => Err(RouterV2Error::InvalidPoolAddress),
}
}

Expand All @@ -76,7 +77,7 @@ pub mod router_v2 {
/// exist in the pair Factory.
/// Adds the Pair to the cache.
#[inline]
fn get_pair(
fn get_and_cache_pair(
&mut self,
pool_id: Option<AccountId>,
token_0: AccountId,
Expand All @@ -86,9 +87,9 @@ pub mod router_v2 {
Some(pool_id) => pool_id,
None => self.pair_factory_ref().create_pair(token_0, token_1)?,
};
match self.get_pool(pool_id)? {
match self.get_and_cache_pool(pool_id)? {
Pool::Pair(pair) => Ok(pair),
Pool::StablePool(_) => Err(RouterV2Error::PairNotFound),
Pool::StablePool(_) => Err(RouterV2Error::InvalidPoolAddress),
}
}

Expand All @@ -101,15 +102,15 @@ pub mod router_v2 {
) -> Result<(), RouterV2Error> {
let n_pools = path.len();
for i in 0..n_pools - 1 {
self.get_pool(path[i].pool_id)?.swap(
self.get_and_cache_pool(path[i].pool_id)?.swap(
path[i].token_in,
path[i + 1].token_in,
amounts[i + 1],
path[i + 1].pool_id,
)?;
}
// If last pool in the path, transfer tokens to the `to` recipient.
self.get_pool(path[n_pools - 1].pool_id)?.swap(
self.get_and_cache_pool(path[n_pools - 1].pool_id)?.swap(
path[n_pools - 1].token_in,
token_out,
amounts[n_pools],
Expand All @@ -130,13 +131,13 @@ pub mod router_v2 {
let n_pools = path.len();
let mut amounts = vec![0; n_pools + 1];
amounts[n_pools] = amount_out;
amounts[n_pools - 1] = self.get_pool(path[n_pools - 1].pool_id)?.get_amount_in(
amounts[n_pools - 1] = self.get_and_cache_pool(path[n_pools - 1].pool_id)?.get_amount_in(
path[n_pools - 1].token_in,
token_out,
amount_out,
)?;
for i in (0..n_pools - 1).rev() {
amounts[i] = self.get_pool(path[i].pool_id)?.get_amount_in(
amounts[i] = self.get_and_cache_pool(path[i].pool_id)?.get_amount_in(
path[i].token_in,
path[i + 1].token_in,
amounts[i + 1],
Expand All @@ -159,13 +160,13 @@ pub mod router_v2 {
let mut amounts = vec![0; n_pools + 1];
amounts[0] = amount_in;
for i in 0..n_pools - 1 {
amounts[i + 1] = self.get_pool(path[i].pool_id)?.get_amount_out(
amounts[i + 1] = self.get_and_cache_pool(path[i].pool_id)?.get_amount_out(
path[i].token_in,
path[i + 1].token_in,
amounts[i],
)?;
}
amounts[n_pools] = self.get_pool(path[n_pools - 1].pool_id)?.get_amount_out(
amounts[n_pools] = self.get_and_cache_pool(path[n_pools - 1].pool_id)?.get_amount_out(
path[n_pools - 1].token_in,
token_out,
amounts[n_pools - 1],
Expand Down Expand Up @@ -392,7 +393,7 @@ pub mod router_v2 {
to: AccountId,
deadline: u64,
) -> Result<(u128, u128, u128), RouterV2Error> {
let pair = self.get_pair(pair, token_0, token_1)?;
let pair = self.get_and_cache_pair(pair, token_0, token_1)?;
pair.add_liquidity(
token_0,
token_1,
Expand All @@ -417,7 +418,7 @@ pub mod router_v2 {
deadline: u64,
) -> Result<(u128, Balance, u128), RouterV2Error> {
let wnative = self.wnative;
let pair = self.get_pair(pair, token, wnative)?;
let pair = self.get_and_cache_pair(pair, token, wnative)?;
pair.add_liquidity_native(
token,
wnative,
Expand All @@ -441,7 +442,7 @@ pub mod router_v2 {
to: AccountId,
deadline: u64,
) -> Result<(u128, u128), RouterV2Error> {
let pair = self.get_pair(Some(pair), token_0, token_1)?;
let pair = self.get_and_cache_pair(Some(pair), token_0, token_1)?;
pair.remove_liquidity(
token_0,
token_1,
Expand All @@ -465,7 +466,7 @@ pub mod router_v2 {
deadline: u64,
) -> Result<(u128, Balance), RouterV2Error> {
let wnative = self.wnative;
let pair = self.get_pair(Some(pair), token, wnative)?;
let pair = self.get_and_cache_pair(Some(pair), token, wnative)?;
pair.remove_liquidity_native(
token,
wnative,
Expand All @@ -490,7 +491,7 @@ pub mod router_v2 {
native: bool,
) -> Result<(u128, u128), RouterV2Error> {
let wnative = if native { Some(self.wnative) } else { None };
self.get_stable_pool(pool)?.add_liquidity(
self.get_and_cache_stable_pool(pool)?.add_liquidity(
min_share_amount,
amounts,
to,
Expand All @@ -510,7 +511,7 @@ pub mod router_v2 {
native: bool,
) -> Result<(u128, u128), RouterV2Error> {
let wnative = if native { Some(self.wnative) } else { None };
self.get_stable_pool(pool)?.remove_liquidity(
self.get_and_cache_stable_pool(pool)?.remove_liquidity(
max_share_amount,
amounts,
to,
Expand All @@ -530,7 +531,7 @@ pub mod router_v2 {
native: bool,
) -> Result<Vec<u128>, RouterV2Error> {
let wnative = if native { Some(self.wnative) } else { None };
self.get_stable_pool(pool)?.remove_liquidity_by_share(
self.get_and_cache_stable_pool(pool)?.remove_liquidity_by_share(
share_amount,
min_amounts,
to,
Expand Down
2 changes: 0 additions & 2 deletions amm/traits/router_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,6 @@ pub enum RouterV2Error {
Expired,
InvalidPoolAddress,
InvalidToken,
PairNotFound,
PoolNotFound,
TransferError,

ExcessiveInputAmount,
Expand Down

0 comments on commit 836d435

Please sign in to comment.