Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed clippy warnings #96

Merged
merged 7 commits into from
Dec 4, 2023
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
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: clippy
# args: --all-targets -- --no-deps -D warnings (TODO: fix clippy warnings first)
args: --all-targets -- --no-deps -D warnings

- name: Check if substrate-contracts-node exists
id: check-substrate-contracts-node
Expand Down
1 change: 1 addition & 0 deletions src/contracts/collections/pool_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ impl PoolKeys {
Ok(())
}

#[allow(dead_code)]
pub fn remove(&mut self, pool_key: PoolKey) -> Result<(), InvariantError> {
let index = self
.pool_keys
Expand Down
1 change: 1 addition & 0 deletions src/contracts/collections/pools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ impl Pools {
Ok(())
}

#[allow(dead_code)]
pub fn remove(&mut self, pool_key: PoolKey) -> Result<(), InvariantError> {
self.get(pool_key)?;

Expand Down
2 changes: 1 addition & 1 deletion src/contracts/entrypoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
contract::{CalculateSwapResult, Hop, QuoteResult},
contracts::{FeeTier, Pool, PoolKey, Position, Tick},
math::{
liquidity::Liquidity, percentage::Percentage, sqrt_price::sqrt_price::SqrtPrice,
liquidity::Liquidity, percentage::Percentage, sqrt_price::SqrtPrice,
token_amount::TokenAmount,
},
InvariantError,
Expand Down
7 changes: 6 additions & 1 deletion src/contracts/logic/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use decimal::*;
use traceable_result::*;

use crate::math::liquidity::Liquidity;
use crate::math::sqrt_price::sqrt_price::{calculate_sqrt_price, SqrtPrice};
use crate::math::sqrt_price::{calculate_sqrt_price, SqrtPrice};
use crate::math::token_amount::TokenAmount;
use crate::math::MAX_TICK;

Expand All @@ -19,6 +19,7 @@ pub struct SingleTokenLiquidity {
pub amount: TokenAmount,
}

#[allow(dead_code)]
pub fn get_liquidity(
x: TokenAmount,
y: TokenAmount,
Expand Down Expand Up @@ -92,6 +93,7 @@ pub fn get_liquidity(
})
}

#[allow(dead_code)]
pub fn get_liquidity_by_x(
x: TokenAmount,
lower_tick: i32,
Expand Down Expand Up @@ -164,6 +166,7 @@ pub fn get_liquidity_by_x_sqrt_price(
})
}

#[allow(dead_code)]
pub fn get_liquidity_by_y(
y: TokenAmount,
lower_tick: i32,
Expand All @@ -187,6 +190,7 @@ pub fn get_liquidity_by_y(
))
}

#[allow(dead_code)]
pub fn get_liquidity_by_y_sqrt_price(
y: TokenAmount,
lower_sqrt_price: SqrtPrice,
Expand Down Expand Up @@ -235,6 +239,7 @@ pub fn get_liquidity_by_y_sqrt_price(
})
}

#[allow(dead_code)]
pub fn calculate_x(
nominator: SqrtPrice,
denominator: SqrtPrice,
Expand Down
2 changes: 1 addition & 1 deletion src/contracts/storage/oracle.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::math::types::sqrt_price::sqrt_price::SqrtPrice;
use crate::math::types::sqrt_price::SqrtPrice;
use ink::prelude::{vec, vec::Vec};

#[derive(Default, Debug, PartialEq, Clone, scale::Decode, scale::Encode)]
Expand Down
9 changes: 5 additions & 4 deletions src/contracts/storage/pool.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
use super::{FeeTier, Oracle, Tick}; // Tickmap
use super::{FeeTier, Oracle, Tick};
use crate::{
contracts::PoolKey,
math::{
math::*,
sqrt_price::sqrt_price::calculate_sqrt_price,
clamm::*,
log::get_tick_at_sqrt_price,
types::{
fee_growth::FeeGrowth,
liquidity::Liquidity,
percentage::Percentage,
sqrt_price::{log::get_tick_at_sqrt_price, sqrt_price::SqrtPrice},
sqrt_price::{calculate_sqrt_price, SqrtPrice},
token_amount::TokenAmount,
},
},
};

use decimal::*;
use ink::primitives::AccountId;
use traceable_result::*;
Expand Down
6 changes: 3 additions & 3 deletions src/contracts/storage/position.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use super::{Pool, PoolKey, Tick}; // Tickmap
use super::{Pool, PoolKey, Tick};
use crate::{
math::{
math::*,
clamm::*,
types::{
fee_growth::{calculate_fee_growth_inside, FeeGrowth},
liquidity::Liquidity,
sqrt_price::sqrt_price::SqrtPrice,
sqrt_price::SqrtPrice,
token_amount::TokenAmount,
},
},
Expand Down
13 changes: 5 additions & 8 deletions src/contracts/storage/tick.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
use super::Pool;
use crate::math::types::{
fee_growth::FeeGrowth, liquidity::Liquidity, sqrt_price::sqrt_price::calculate_sqrt_price,
sqrt_price::sqrt_price::SqrtPrice,
fee_growth::FeeGrowth, liquidity::Liquidity, sqrt_price::calculate_sqrt_price,
sqrt_price::SqrtPrice,
};

use traceable_result::*;

use decimal::*;

use super::Pool;
use traceable_result::*;

#[derive(Debug, Copy, Clone, scale::Decode, scale::Encode, PartialEq)]
#[cfg_attr(
Expand Down Expand Up @@ -160,7 +157,7 @@ impl Tick {
mod tests {
use decimal::{Decimal, Factories};

use crate::math::math::calculate_max_liquidity_per_tick;
use crate::math::clamm::calculate_max_liquidity_per_tick;

use super::*;

Expand Down
3 changes: 2 additions & 1 deletion src/contracts/storage/tickmap.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::contracts::PoolKey;
use crate::math::{
types::sqrt_price::sqrt_price::{calculate_sqrt_price, SqrtPrice},
types::sqrt_price::{calculate_sqrt_price, SqrtPrice},
MAX_TICK,
};
use ink::storage::Mapping;

pub const TICK_SEARCH_RANGE: i32 = 256;
pub const CHUNK_SIZE: i32 = 64;

Expand Down
63 changes: 25 additions & 38 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
extern crate alloc;
mod contracts;
pub mod math;

#[derive(Debug, PartialEq, Eq, scale::Encode, scale::Decode)]
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
pub enum InvariantError {
Expand All @@ -30,26 +29,27 @@
TokensAreTheSame,
AmountUnderMinimumAmountOut,
InvalidFee,
NotEmptyTickDeinitialization,
}
#[ink::contract]
pub mod contract {
use crate::InvariantError;
// use math::fee_growth::FeeGrowth;

use crate::contracts::state::State;
use crate::contracts::Invariant;
use crate::contracts::Pool;
use crate::contracts::PoolKeys;
use crate::contracts::Tick;
use crate::contracts::Tickmap;
use crate::contracts::{FeeTier, FeeTiers, PoolKey, Pools, Position, Positions, Ticks}; //
use crate::contracts::{FeeTier, FeeTiers, PoolKey, Pools, Position, Positions, Ticks};
use crate::math::calculate_min_amount_out;
use crate::math::check_tick;
use crate::math::log::get_tick_at_sqrt_price;
use crate::math::percentage::Percentage;
use crate::math::sqrt_price::log::get_tick_at_sqrt_price;
use crate::math::sqrt_price::sqrt_price::SqrtPrice;
use crate::math::sqrt_price::SqrtPrice;
use crate::math::token_amount::TokenAmount;
use crate::math::types::liquidity::Liquidity;
// use crate
use crate::math::types::liquidity::Liquidity; //

use crate::math::{compute_swap_step, MAX_SQRT_PRICE, MIN_SQRT_PRICE};
use decimal::*;
use ink::contract_ref;
Expand Down Expand Up @@ -189,7 +189,7 @@
sqrt_price_limit: SqrtPrice,
) -> Result<CalculateSwapResult, InvariantError> {
let current_timestamp = self.env().block_timestamp();
let caller = self.env().caller();

if amount.is_zero() {
return Err(InvariantError::AmountIsZero);
}
Expand Down Expand Up @@ -258,7 +258,7 @@

if let Some((tick_index, is_initialized)) = limiting_tick {
if is_initialized {
let mut tick = self.ticks.get(pool_key, tick_index).unwrap();
let mut tick = self.ticks.get(pool_key, tick_index)?;

let (amount_to_add, has_crossed) = pool.cross_tick(
result,
Expand Down Expand Up @@ -300,9 +300,14 @@
})
}

fn remove_tick(&mut self, key: PoolKey, index: i32) -> Result<(), InvariantError> {
self.ticks.remove(key, index)?;
fn remove_tick(&mut self, key: PoolKey, tick: Tick) -> Result<(), InvariantError> {
if !tick.liquidity_gross.is_zero() {
return Err(InvariantError::NotEmptyTickDeinitialization);
}

self.tickmap
.flip(false, tick.index, key.fee_tier.tick_spacing, key);
self.ticks.remove(key, tick.index)?;
Ok(())
}

Expand Down Expand Up @@ -557,7 +562,7 @@
crossed_tick_indexes.push(tick.index);
}

if crossed_tick_indexes.len() > 0 {
if !crossed_tick_indexes.is_empty() {
self.emit_cross_tick_event(caller, pool_key, crossed_tick_indexes);
}

Expand Down Expand Up @@ -796,41 +801,23 @@
position.pool_key.fee_tier.tick_spacing,
);

self.pools.update(position.pool_key, pool).unwrap();
self.pools.update(position.pool_key, pool)?;

if deinitialize_lower_tick {
self.tickmap.flip(
false,
lower_tick.index,
position.pool_key.fee_tier.tick_spacing,
position.pool_key,
);
self.ticks
.remove(position.pool_key, position.lower_tick_index)
.unwrap();
self.remove_tick(position.pool_key, lower_tick)?;
} else {
self.ticks
.update(position.pool_key, position.lower_tick_index, &lower_tick)
.unwrap();
.update(position.pool_key, position.lower_tick_index, &lower_tick)?;
}

if deinitialize_upper_tick {
self.tickmap.flip(
false,
upper_tick.index,
position.pool_key.fee_tier.tick_spacing,
position.pool_key,
);
self.ticks
.remove(position.pool_key, position.upper_tick_index)
.unwrap();
self.remove_tick(position.pool_key, upper_tick)?;
} else {
self.ticks
.update(position.pool_key, position.upper_tick_index, &upper_tick)
.unwrap();
.update(position.pool_key, position.upper_tick_index, &upper_tick)?;
}

self.positions.remove(caller, index).unwrap();
self.positions.remove(caller, index)?;

let mut token_x: contract_ref!(PSP22) = position.pool_key.token_x.into();
token_x
Expand Down Expand Up @@ -1070,11 +1057,11 @@
use crate::contracts::{get_liquidity, get_liquidity_by_x, get_liquidity_by_y};
use crate::math::fee_growth::FeeGrowth;
use crate::math::get_delta_y;
use crate::math::sqrt_price::log::get_tick_at_sqrt_price;
use crate::math::sqrt_price::sqrt_price::{calculate_sqrt_price, get_max_tick};
use crate::math::log::get_tick_at_sqrt_price;
use crate::math::sqrt_price::{calculate_sqrt_price, get_max_tick};
use crate::math::MAX_TICK;
use ink::prelude::vec;
use ink::prelude::vec::Vec;

Check warning on line 1064 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Uint and e2e tests + linit

unused import: `ink::prelude::vec::Vec`
use ink_e2e::build_message;

use test_helpers::{
Expand Down Expand Up @@ -1252,7 +1239,7 @@

let init_tick = -23028;

let pool = create_pool!(

Check warning on line 1242 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Uint and e2e tests + linit

unused variable: `pool`
client,
ContractRef,
dex,
Expand Down Expand Up @@ -1281,7 +1268,7 @@

add_fee_tier!(client, ContractRef, dex, fee_tier, alice);

let pool = create_pool!(

Check warning on line 1271 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Uint and e2e tests + linit

unused variable: `pool`
client,
ContractRef,
dex,
Expand Down Expand Up @@ -1476,7 +1463,7 @@

add_fee_tier!(client, ContractRef, dex, fee_tier, alice);

let pool = create_pool!(

Check warning on line 1466 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Uint and e2e tests + linit

unused variable: `pool`
client,
ContractRef,
dex,
Expand Down Expand Up @@ -1638,7 +1625,7 @@

add_fee_tier!(client, ContractRef, dex, fee_tier, alice);

let pool = create_pool!(

Check warning on line 1628 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Uint and e2e tests + linit

unused variable: `pool`
client,
ContractRef,
dex,
Expand Down Expand Up @@ -1756,7 +1743,7 @@
let transferred_index = 1;
let owner_list_before = get_all_positions!(client, ContractRef, dex, alice);
let recipient_list_before = get_all_positions!(client, ContractRef, dex, bob);
let removed_position =

Check warning on line 1746 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Uint and e2e tests + linit

unused variable: `removed_position`
get_position!(client, ContractRef, dex, transferred_index, alice).unwrap();
let last_position_before = owner_list_before[owner_list_before.len() - 1];

Expand All @@ -1769,7 +1756,7 @@
alice
);

let recipient_position =

Check warning on line 1759 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Uint and e2e tests + linit

unused variable: `recipient_position`
get_position!(client, ContractRef, dex, transferred_index, bob).unwrap();
let owner_list_after = get_all_positions!(client, ContractRef, dex, alice);
let recipient_list_after = get_all_positions!(client, ContractRef, dex, bob);
Expand All @@ -1786,7 +1773,7 @@
{
let owner_list_before = get_all_positions!(client, ContractRef, dex, alice);
let transferred_index = (owner_list_before.len() - 1) as u32;
let recipient_list_before = get_all_positions!(client, ContractRef, dex, bob);

Check warning on line 1776 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Uint and e2e tests + linit

unused variable: `recipient_list_before`
let removed_position =
get_position!(client, ContractRef, dex, transferred_index, alice).unwrap();

Expand All @@ -1799,7 +1786,7 @@
alice
);

let owner_list_after = get_all_positions!(client, ContractRef, dex, alice);

Check warning on line 1789 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Uint and e2e tests + linit

unused variable: `owner_list_after`
let recipient_list_after = get_all_positions!(client, ContractRef, dex, bob);
let recipient_position_index = (recipient_list_after.len() - 1) as u32;
let recipient_position =
Expand All @@ -1815,7 +1802,7 @@
let recipient_list_before = get_all_positions!(client, ContractRef, dex, bob);
let removed_position =
get_position!(client, ContractRef, dex, transferred_index, alice).unwrap();
let last_position_before = owner_list_before[owner_list_before.len() - 1];

Check warning on line 1805 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Uint and e2e tests + linit

unused variable: `last_position_before`

transfer_position!(
client,
Expand Down
8 changes: 4 additions & 4 deletions src/math/math.rs → src/math/clamm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use decimal::*;
use traceable_result::*;

use crate::math::consts::*;
use crate::math::types::{liquidity::*, percentage::*, sqrt_price::sqrt_price::*, token_amount::*};
use crate::math::types::{liquidity::*, percentage::*, sqrt_price::*, token_amount::*};

#[derive(PartialEq, Debug, Copy, Clone)]
pub struct SwapResult {
Expand Down Expand Up @@ -1289,7 +1289,7 @@ mod tests {
.get();
assert_eq!(
cause,
"conversion to contract::math::types::sqrt_price::sqrt_price::SqrtPrice type failed"
"conversion to contract::math::types::sqrt_price::SqrtPrice type failed"
);
assert_eq!(stack.len(), 2);
}
Expand Down Expand Up @@ -1343,7 +1343,7 @@ mod tests {
.get();
assert_eq!(
cause,
"conversion to contract::math::types::sqrt_price::sqrt_price::SqrtPrice type failed"
"conversion to contract::math::types::sqrt_price::SqrtPrice type failed"
);
assert_eq!(stack.len(), 2);
}
Expand All @@ -1358,7 +1358,7 @@ mod tests {
.get();
assert_eq!(
cause,
"conversion to contract::math::types::sqrt_price::sqrt_price::SqrtPrice type failed"
"conversion to contract::math::types::sqrt_price::SqrtPrice type failed"
);
assert_eq!(stack.len(), 2);
}
Expand Down
2 changes: 1 addition & 1 deletion src/math/types/sqrt_price/log.rs → src/math/log.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use traceable_result::*;

use crate::math::consts::*;
use crate::math::types::sqrt_price::sqrt_price::SqrtPrice;
use crate::math::types::sqrt_price::SqrtPrice;

const LOG2_SCALE: u8 = 32;
const LOG2_DOUBLE_SCALE: u8 = 64;
Expand Down
7 changes: 5 additions & 2 deletions src/math/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
pub mod clamm;
pub mod consts;
pub mod math;
pub mod log;
pub mod types;

pub use clamm::*;
pub use consts::*;
pub use math::*;
pub use log::*;
pub use types::*;
4 changes: 1 addition & 3 deletions src/math/types/fee_growth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,7 @@ pub fn calculate_fee_growth_inside(
mod tests {
use super::*;
use crate::math::consts::MAX_TICK;
// use crate::math::calculate_sqrt_price;
use crate::math::types::sqrt_price::sqrt_price::SqrtPrice;
// use decimal::{BetweenDecimals, Decimal, Factories};
use crate::math::types::sqrt_price::SqrtPrice;

#[test]
fn test_unchecked_add() {
Expand Down
File renamed without changes.
2 changes: 0 additions & 2 deletions src/math/types/sqrt_price/mod.rs

This file was deleted.

2 changes: 1 addition & 1 deletion src/math/types/token_amount.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use decimal::*;
use traceable_result::*;

use super::sqrt_price::sqrt_price::SqrtPrice;
use super::sqrt_price::SqrtPrice;

#[decimal(0)]
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, scale::Decode, scale::Encode)]
Expand Down
Loading
Loading