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

create pool keys collection and get pools message #83

Merged
merged 5 commits into from
Nov 29, 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: 2 additions & 0 deletions src/contracts/collections/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
pub mod fee_tiers;
pub mod pool_keys;
pub mod pools;
pub mod positions;
pub mod ticks;

pub use fee_tiers::*;
pub use pool_keys::*;
pub use pools::*;
pub use positions::*;
pub use ticks::*;
105 changes: 105 additions & 0 deletions src/contracts/collections/pool_keys.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
use crate::{contracts::PoolKey, InvariantError};
use alloc::vec::Vec;

#[ink::storage_item]
#[derive(Debug, Default)]
pub struct PoolKeys {
pool_keys: Vec<PoolKey>,
}

impl PoolKeys {
pub fn add(&mut self, pool_key: PoolKey) -> Result<(), InvariantError> {
if self.contains(pool_key) {
return Err(InvariantError::PoolKeyAlreadyExist);
}

self.pool_keys.push(pool_key);
Ok(())
}

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

Check warning on line 20 in src/contracts/collections/pool_keys.rs

View workflow job for this annotation

GitHub Actions / Run clippy and unit tests

method `remove` is never used

Check warning on line 20 in src/contracts/collections/pool_keys.rs

View workflow job for this annotation

GitHub Actions / Run clippy and unit tests

method `remove` is never used
let index = self
.pool_keys
.iter()
.position(|vec_pool_key| *vec_pool_key == pool_key)
.ok_or(InvariantError::PoolKeyNotFound)?;

self.pool_keys.remove(index);

Ok(())
}

pub fn contains(&self, fee_tier_key: PoolKey) -> bool {
zielvna marked this conversation as resolved.
Show resolved Hide resolved
self.pool_keys.contains(&fee_tier_key)
}

pub fn get_all(&self) -> Vec<PoolKey> {
self.pool_keys.clone()
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::{contracts::FeeTier, math::percentage::Percentage};
use decimal::*;
use ink::primitives::AccountId;

#[ink::test]
fn test_add() {
let pool_keys = &mut PoolKeys::default();
let pool_key = PoolKey::default();
let token_x = AccountId::from([1; 32]);
let token_y = AccountId::from([2; 32]);
let fee_tier = FeeTier {
zielvna marked this conversation as resolved.
Show resolved Hide resolved
fee: Percentage::new(0),
tick_spacing: 1,
};
let new_pool_key = PoolKey::new(token_x, token_y, fee_tier).unwrap();

pool_keys.add(pool_key).unwrap();
assert_eq!(pool_keys.contains(pool_key), true);
assert_eq!(pool_keys.contains(new_pool_key), false);

let result = pool_keys.add(pool_key);
assert_eq!(result, Err(InvariantError::PoolKeyAlreadyExist));
}

#[ink::test]
fn test_remove() {
let pool_keys = &mut PoolKeys::default();
let pool_key = PoolKey::default();

pool_keys.add(pool_key).unwrap();

pool_keys.remove(pool_key).unwrap();
assert_eq!(pool_keys.contains(pool_key), false);

let result = pool_keys.remove(pool_key);
assert_eq!(result, Err(InvariantError::PoolKeyNotFound));
}

#[ink::test]
fn test_get_all() {
let pool_keys = &mut PoolKeys::default();
let pool_key = PoolKey::default();
let token_x = AccountId::from([1; 32]);
let token_y = AccountId::from([2; 32]);
let fee_tier = FeeTier {
fee: Percentage::new(0),
tick_spacing: 1,
};
let new_pool_key = PoolKey::new(token_x, token_y, fee_tier).unwrap();

let result = pool_keys.get_all();
assert_eq!(result, vec![]);
assert_eq!(result.len(), 0);

pool_keys.add(pool_key).unwrap();
pool_keys.add(new_pool_key).unwrap();

let result = pool_keys.get_all();
assert_eq!(result, vec![pool_key, new_pool_key]);
assert_eq!(result.len(), 2);
}
}
3 changes: 3 additions & 0 deletions src/contracts/entrypoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ pub trait Invariant {
fee_tier: FeeTier,
) -> Result<Pool, InvariantError>;

#[ink(message)]
fn get_pools(&self) -> Vec<PoolKey>;

#[ink(message)]
fn get_tick(&self, key: PoolKey, index: i32) -> Result<Tick, InvariantError>;

Expand Down
15 changes: 12 additions & 3 deletions src/contracts/storage/fee_tier.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::math::types::percentage::Percentage;
use crate::{math::types::percentage::Percentage, InvariantError};
use decimal::*;
#[derive(scale::Decode, scale::Encode, Debug, Copy, Clone, PartialEq)]
#[cfg_attr(
feature = "std",
Expand All @@ -10,7 +11,15 @@ pub struct FeeTier {
}

impl FeeTier {
pub fn new(fee: Percentage, tick_spacing: u16) -> FeeTier {
FeeTier { fee, tick_spacing }
pub fn new(fee: Percentage, tick_spacing: u16) -> Result<Self, InvariantError> {
if tick_spacing == 0 || tick_spacing > 100 {
return Err(InvariantError::InvalidTickSpacing);
}

if fee > Percentage::from_integer(1) {
return Err(InvariantError::InvalidFee);
}

Ok(Self { fee, tick_spacing })
}
}
Loading
Loading