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

Results handling #75

Closed
wants to merge 4 commits into from
Closed
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
24 changes: 20 additions & 4 deletions src/contracts/collections/fee_tiers.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::InvariantError;
use ink::storage::Mapping;

use crate::math::types::percentage::Percentage;
Expand All @@ -17,13 +18,28 @@ pub struct FeeTiers {
}

impl FeeTiers {
pub fn get_fee_tier(&self, key: FeeTierKey) -> Option<()> {
self.fee_tiers.get(&key)
pub fn get_fee_tier(&self, key: FeeTierKey) -> Result<(), InvariantError> {
let fee_tier = self
.fee_tiers
.get(&key)
.ok_or(InvariantError::FeeTierNotFound)?;
Ok(fee_tier)
}
pub fn add_fee_tier(&mut self, key: FeeTierKey) {
pub fn add_fee_tier(&mut self, key: FeeTierKey) -> Result<(), InvariantError> {
if self.fee_tiers.get(&key).is_some() {
return Err(InvariantError::FeeTierAlreadyAdded);
}
// self.fee_tiers
// .get(&key)
// .ok_or(InvariantError::FeeTierAlreadyAdded)?;
self.fee_tiers.insert(&key, &());
Ok(())
}
pub fn remove_fee_tier(&mut self, key: FeeTierKey) {
pub fn remove_fee_tier(&mut self, key: FeeTierKey) -> Result<(), InvariantError> {
self.fee_tiers
.get(&key)
.ok_or(InvariantError::FeeTierNotFound)?;
self.fee_tiers.remove(&key);
Ok(())
}
}
59 changes: 33 additions & 26 deletions src/contracts/collections/positions.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{contracts::Position, InvariantError};
use crate::contracts::Position;
use crate::InvariantError;
use ink::{prelude::vec::Vec, primitives::AccountId, storage::Mapping};

#[ink::storage_item]
Expand All @@ -9,14 +10,16 @@ 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) -> Result<(), InvariantError> {
let positions_length = self.get_length(account_id);
let key = (account_id, positions_length);

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

self.positions_length
.insert(account_id, &(positions_length + 1));

Ok(())
}

pub fn update(
Expand Down Expand Up @@ -72,7 +75,7 @@ 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(())
}
Expand All @@ -83,9 +86,13 @@ impl Positions {
.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(&mut self, account_id: AccountId, index: u32) -> Result<Position, InvariantError> {
let position = self
.positions
.get((account_id, index))
.ok_or(InvariantError::PositionAlreadyExist)?;

Ok(position)
}

pub fn get_length(&self, account_id: AccountId) -> u32 {
Expand All @@ -109,11 +116,11 @@ 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).unwrap();
positions.add(account_id, new_position).unwrap();
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), None);
assert_eq!(positions.get_length(account_id), 2);
}

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

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

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 +156,17 @@ mod tests {
..Position::default()
};

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

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), None);
assert_eq!(positions.get_length(account_id), 0);

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

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

positions
.transfer(account_id, 0, receiver_account_id)
.unwrap();
assert_eq!(positions.get(account_id, 0), None);
// assert_eq!(positions.get(account_id, 0), None);
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 +210,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).unwrap();
positions.add(account_id, new_position).unwrap();

let result = positions.get_all(account_id);
assert_eq!(result, vec![position, new_position]);
Expand All @@ -226,8 +233,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).unwrap();
positions.add(account_id, new_position).unwrap();

let result = positions.get_length(account_id);
assert_eq!(result, 2);
Expand Down
21 changes: 15 additions & 6 deletions src/contracts/collections/ticks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ pub struct Ticks {
}

impl Ticks {
pub fn get_tick(&self, key: PoolKey, index: i32) -> Option<Tick> {
self.ticks.get(&(key, index))
pub fn get_tick(&self, key: PoolKey, index: i32) -> Result<Tick, InvariantError> {
let tick = self
.ticks
.get(&(key, index))
.ok_or(InvariantError::TickNotFound)?;
Ok(tick)
}
// pub fn update_tick(&mut self, key: PoolKey, index: i32, tick: Tick) {}
pub fn remove_tick(&mut self, key: PoolKey, index: i32) -> Result<(), InvariantError> {
Expand All @@ -23,8 +27,13 @@ impl Ticks {
Ok(())
}

pub fn add_tick(&mut self, key: PoolKey, index: i32, tick: Tick) {
pub fn add_tick(&mut self, key: PoolKey, index: i32, tick: Tick) -> Result<(), InvariantError> {
if self.ticks.get(&(key, index)).is_some() {
return Err(InvariantError::TickAlreadyExist);
}

self.ticks.insert(&(key, index), &tick);
Ok(())
}

pub fn update_tick(
Expand All @@ -33,9 +42,9 @@ impl Ticks {
index: i32,
tick: &Tick,
) -> Result<(), InvariantError> {
self.ticks
.get(&(key, index))
.ok_or(InvariantError::TickNotFound)?;
// self.ticks
// .get(&(key, index))
// .ok_or(InvariantError::TickNotFound)?;

self.ticks.insert((&key, index), tick);

Expand Down
2 changes: 0 additions & 2 deletions src/contracts/storage/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ use crate::{
InvariantError,
};
use decimal::*;
use ink::prelude::vec;
use ink::primitives::AccountId;
use traceable_result::*;
#[derive(PartialEq, Default, Debug, Copy, Clone, scale::Decode, scale::Encode)]
#[cfg_attr(
Expand Down
Loading