Skip to content

Commit

Permalink
Merge pull request #74 from invariant-labs/create-collection-tests
Browse files Browse the repository at this point in the history
Create collection tests
  • Loading branch information
zielvna authored Nov 28, 2023
2 parents 6b9d763 + 426b949 commit b76fd5e
Show file tree
Hide file tree
Showing 7 changed files with 470 additions and 151 deletions.
63 changes: 54 additions & 9 deletions src/contracts/collections/fee_tiers.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use crate::{math::types::percentage::Percentage, InvariantError};
use ink::storage::Mapping;

use crate::math::types::percentage::Percentage;

#[derive(scale::Decode, scale::Encode, Debug, Copy, Clone, PartialEq)]
#[cfg_attr(
feature = "std",
derive(scale_info::TypeInfo, ink::storage::traits::StorageLayout)
)]
// key(fee: Percentage, tick_spacing: u16)
pub struct FeeTierKey(pub Percentage, pub u16);

#[ink::storage_item]
Expand All @@ -17,13 +15,60 @@ pub struct FeeTiers {
}

impl FeeTiers {
pub fn get_fee_tier(&self, key: FeeTierKey) -> Option<()> {
self.fee_tiers.get(&key)
pub fn add(&mut self, fee_tier_key: FeeTierKey) -> Result<(), InvariantError> {
self.fee_tiers
.get(&fee_tier_key)
.map_or(Ok(()), |_| Err(InvariantError::FeeTierAlreadyExist))?;

self.fee_tiers.insert(&fee_tier_key, &());
Ok(())
}

pub fn remove(&mut self, fee_tier_key: FeeTierKey) -> Result<(), InvariantError> {
self.fee_tiers
.get(fee_tier_key)
.ok_or(InvariantError::FeeTierNotFound)?;

self.fee_tiers.remove(&fee_tier_key);
Ok(())
}

pub fn get(&self, fee_tier_key: FeeTierKey) -> Option<()> {
self.fee_tiers.get(fee_tier_key)
}
pub fn add_fee_tier(&mut self, key: FeeTierKey) {
self.fee_tiers.insert(&key, &());
}

#[cfg(test)]
mod tests {
use super::*;
use crate::math::percentage::Percentage;
use decimal::*;

#[ink::test]
fn test_add() {
let fee_tiers = &mut FeeTiers::default();
let fee_tier_key = FeeTierKey(Percentage::new(0), 1);
let new_fee_tier_key = FeeTierKey(Percentage::new(0), 2);

fee_tiers.add(fee_tier_key).unwrap();
assert_eq!(fee_tiers.get(fee_tier_key), Some(()));
assert_eq!(fee_tiers.get(new_fee_tier_key), None);

let result = fee_tiers.add(fee_tier_key);
assert_eq!(result, Err(InvariantError::FeeTierAlreadyExist));
}
pub fn remove_fee_tier(&mut self, key: FeeTierKey) {
self.fee_tiers.remove(&key);

#[ink::test]
fn test_remove() {
let fee_tiers = &mut FeeTiers::default();
let fee_tier_key = FeeTierKey(Percentage::new(0), 1);

fee_tiers.add(fee_tier_key).unwrap();

fee_tiers.remove(fee_tier_key).unwrap();
assert_eq!(fee_tiers.get(fee_tier_key), None);

let result = fee_tiers.remove(fee_tier_key);
assert_eq!(result, Err(InvariantError::FeeTierNotFound));
}
}
117 changes: 99 additions & 18 deletions src/contracts/collections/pools.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::contracts::Pool;
use crate::contracts::PoolKey;
use crate::InvariantError;
use crate::{
contracts::{Pool, PoolKey},
InvariantError,
};
use ink::storage::Mapping;

#[ink::storage_item]
Expand All @@ -11,14 +12,28 @@ pub struct Pools {

impl Pools {
pub fn add(&mut self, pool_key: PoolKey, pool: &Pool) -> Result<(), InvariantError> {
if self.pools.get(&pool_key).is_some() {
return Err(InvariantError::PoolAlreadyExist);
}
self.pools
.get(&pool_key)
.map_or(Ok(()), |_| Err(InvariantError::PoolAlreadyExist))?;

self.pools.insert(pool_key, pool);
Ok(())
}

pub fn update(&mut self, pool_key: PoolKey, pool: &Pool) -> Result<(), InvariantError> {
self.get(pool_key)?;

self.pools.insert(pool_key, pool);
Ok(())
}

pub fn remove(&mut self, pool_key: PoolKey) -> Result<(), InvariantError> {

Check warning on line 30 in src/contracts/collections/pools.rs

View workflow job for this annotation

GitHub Actions / Run clippy and unit tests

method `remove` is never used

Check warning on line 30 in src/contracts/collections/pools.rs

View workflow job for this annotation

GitHub Actions / Run clippy and unit tests

method `remove` is never used
self.get(pool_key)?;

self.pools.remove(&pool_key);
Ok(())
}

pub fn get(&self, pool_key: PoolKey) -> Result<Pool, InvariantError> {
let pool = self
.pools
Expand All @@ -27,22 +42,88 @@ impl Pools {

Ok(pool)
}
}

pub fn update(&mut self, pool_key: PoolKey, pool: &Pool) -> Result<(), InvariantError> {
self.pools
.get(pool_key)
.ok_or(InvariantError::PoolNotFound)?;
#[cfg(test)]
mod tests {
use super::*;
use crate::{contracts::FeeTier, math::percentage::Percentage};
use decimal::*;
use ink::primitives::AccountId;

self.pools.insert(pool_key, pool);
Ok(())
#[ink::test]
fn test_add() {
let pools = &mut Pools::default();
let token_x = AccountId::from([0x01; 32]);
let token_y = AccountId::from([0x02; 32]);
let fee_tier = FeeTier {
fee: Percentage::new(0),
tick_spacing: 1,
};
let new_fee_tier = FeeTier {
fee: Percentage::new(0),
tick_spacing: 2,
};
let pool_key = PoolKey::new(token_x, token_y, fee_tier).unwrap();
let new_pool_key = PoolKey::new(token_x, token_y, new_fee_tier).unwrap();
let pool = Pool::default();

pools.add(pool_key, &pool).unwrap();
assert_eq!(pools.get(pool_key), Ok(pool.clone()));
assert_eq!(pools.get(new_pool_key), Err(InvariantError::PoolNotFound));

let result = pools.add(pool_key, &pool);
assert_eq!(result, Err(InvariantError::PoolAlreadyExist));
}

pub fn remove(&mut self, pool_key: PoolKey) -> Result<(), InvariantError> {
self.pools
.get(pool_key)
.ok_or(InvariantError::PoolNotFound)?;
#[ink::test]
fn test_update() {
let pools = &mut Pools::default();
let token_x = AccountId::from([0x01; 32]);
let token_y = AccountId::from([0x02; 32]);
let fee_tier = FeeTier {
fee: Percentage::new(0),
tick_spacing: 1,
};
let new_fee_tier = FeeTier {
fee: Percentage::new(0),
tick_spacing: 2,
};
let pool_key = PoolKey::new(token_x, token_y, fee_tier).unwrap();
let new_pool_key = PoolKey::new(token_x, token_y, new_fee_tier).unwrap();
let pool = Pool::default();
let new_pool = Pool {
current_tick_index: 1,
..Pool::default()
};

self.pools.remove(&pool_key);
Ok(())
pools.add(pool_key, &pool).unwrap();

pools.update(pool_key, &new_pool).unwrap();
assert_eq!(pools.get(pool_key), Ok(new_pool.clone()));

let result = pools.update(new_pool_key, &new_pool);
assert_eq!(result, Err(InvariantError::PoolNotFound));
}

#[ink::test]
fn test_remove() {
let pools = &mut Pools::default();
let token_x = AccountId::from([0x01; 32]);
let token_y = AccountId::from([0x02; 32]);
let fee_tier = FeeTier {
fee: Percentage::new(0),
tick_spacing: 1,
};
let pool_key = PoolKey::new(token_x, token_y, fee_tier).unwrap();
let pool = Pool::default();

pools.add(pool_key, &pool).unwrap();

pools.remove(pool_key).unwrap();
assert_eq!(pools.get(pool_key), Err(InvariantError::PoolNotFound));

let result = pools.remove(pool_key);
assert_eq!(result, Err(InvariantError::PoolNotFound));
}
}
74 changes: 41 additions & 33 deletions src/contracts/collections/positions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ pub struct Positions {
}

impl Positions {
pub fn add(&mut self, account_id: AccountId, position: Position) {
pub fn add(&mut self, account_id: AccountId, position: &Position) {
let positions_length = self.get_length(account_id);

self.positions
.insert((account_id, positions_length), &position);
.insert((account_id, positions_length), position);

self.positions_length
.insert(account_id, &(positions_length + 1));
Expand Down Expand Up @@ -42,12 +42,7 @@ impl Positions {
index: u32,
) -> Result<Position, InvariantError> {
let positions_length = self.get_length(account_id);

if index >= positions_length {
return Err(InvariantError::PositionNotFound);
}

let position = self.positions.get((account_id, index));
let position = self.get(account_id, index)?;

if index < positions_length - 1 {
let last_position = self
Expand All @@ -62,7 +57,7 @@ impl Positions {
self.positions_length
.insert(account_id, &(positions_length - 1));

Ok(position.unwrap())
Ok(position)
}

pub fn transfer(
Expand All @@ -72,22 +67,26 @@ impl Positions {
receiver_account_id: AccountId,
) -> Result<(), InvariantError> {
let position = self.remove(account_id, index)?;
self.add(receiver_account_id, position);
self.add(receiver_account_id, &position);

Ok(())
}

pub fn get(&self, account_id: AccountId, index: u32) -> Result<Position, InvariantError> {
let position = self
.positions
.get((account_id, index))
.ok_or(InvariantError::PositionNotFound)?;

Ok(position)
}

pub fn get_all(&self, account_id: AccountId) -> Vec<Position> {
(0..self.get_length(account_id))
.map(|index| self.positions.get((account_id, index)).unwrap())
.collect()
}

pub fn get(&mut self, account_id: AccountId, index: u32) -> Option<Position> {
let position = self.positions.get((account_id, index));
position
}

pub fn get_length(&self, account_id: AccountId) -> u32 {
let positions_length = self.positions_length.get(account_id).unwrap_or(0);
positions_length
Expand All @@ -109,11 +108,14 @@ mod tests {
..Position::default()
};

positions.add(account_id, position);
positions.add(account_id, new_position);
assert_eq!(positions.get(account_id, 0), Some(position));
assert_eq!(positions.get(account_id, 1), Some(new_position));
assert_eq!(positions.get(account_id, 2), None);
positions.add(account_id, &position);
positions.add(account_id, &new_position);
assert_eq!(positions.get(account_id, 0), Ok(position));
assert_eq!(positions.get(account_id, 1), Ok(new_position));
assert_eq!(
positions.get(account_id, 2),
Err(InvariantError::PositionNotFound)
);
assert_eq!(positions.get_length(account_id), 2);
}

Expand All @@ -128,10 +130,10 @@ mod tests {
..Position::default()
};

positions.add(account_id, position);
positions.add(account_id, &position);

positions.update(account_id, 0, &new_position).unwrap();
assert_eq!(positions.get(account_id, 0), Some(new_position));
assert_eq!(positions.get(account_id, 0), Ok(new_position));
assert_eq!(positions.get_length(account_id), 1);

let result = positions.update(account_id, 1, &new_position);
Expand All @@ -149,17 +151,20 @@ mod tests {
..Position::default()
};

positions.add(account_id, position);
positions.add(account_id, new_position);
positions.add(account_id, &position);
positions.add(account_id, &new_position);

let result = positions.remove(account_id, 0);
assert_eq!(result, Ok(position));
assert_eq!(positions.get(account_id, 0), Some(new_position));
assert_eq!(positions.get(account_id, 0), Ok(new_position));
assert_eq!(positions.get_length(account_id), 1);

let result = positions.remove(account_id, 0);
assert_eq!(result, Ok(new_position));
assert_eq!(positions.get(account_id, 0), None);
assert_eq!(
positions.get(account_id, 0),
Err(InvariantError::PositionNotFound)
);
assert_eq!(positions.get_length(account_id), 0);

let result = positions.remove(account_id, 0);
Expand All @@ -173,14 +178,17 @@ mod tests {
let receiver_account_id = AccountId::from([0x02; 32]);
let position = Position::default();

positions.add(account_id, position);
positions.add(account_id, &position);

positions
.transfer(account_id, 0, receiver_account_id)
.unwrap();
assert_eq!(positions.get(account_id, 0), None);
assert_eq!(
positions.get(account_id, 0),
Err(InvariantError::PositionNotFound)
);
assert_eq!(positions.get_length(account_id), 0);
assert_eq!(positions.get(receiver_account_id, 0), Some(position));
assert_eq!(positions.get(receiver_account_id, 0), Ok(position));
assert_eq!(positions.get_length(receiver_account_id), 1);

let result = positions.transfer(account_id, 0, receiver_account_id);
Expand All @@ -203,8 +211,8 @@ mod tests {
assert_eq!(result.len(), 0);
assert_eq!(positions.get_length(account_id), 0);

positions.add(account_id, position);
positions.add(account_id, new_position);
positions.add(account_id, &position);
positions.add(account_id, &new_position);

let result = positions.get_all(account_id);
assert_eq!(result, vec![position, new_position]);
Expand All @@ -226,8 +234,8 @@ mod tests {
let result = positions.get_length(account_id);
assert_eq!(result, 0);

positions.add(account_id, position);
positions.add(account_id, new_position);
positions.add(account_id, &position);
positions.add(account_id, &new_position);

let result = positions.get_length(account_id);
assert_eq!(result, 2);
Expand Down
Loading

0 comments on commit b76fd5e

Please sign in to comment.