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

Mdjakovic/update point #779

Merged
merged 8 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- ref: Rename Set Amount Splitter to Fixed Amount Splitter [(#754)](https://github.com/andromedaprotocol/andromeda-core/pull/754)
- Point ADO: remove Rates module from the contract[(#761)](https://github.com/andromedaprotocol/andromeda-core/pull/761)
- feat: SignedDecimal for Distance [(#774)](https://github.com/andromedaprotocol/andromeda-core/pull/774)
- feat: SignedDecimal for Point [(#779)](https://github.com/andromedaprotocol/andromeda-core/pull/779)

### Fixed

Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 3 additions & 5 deletions contracts/math/andromeda-graph/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,6 @@ pub fn execute_store_user_coordinate(
let user: AndrAddr = user_res.owner;
let user_addr = user.get_raw_address(&ctx.deps.as_ref())?;

user_point_coordinate.validate()?;

USER_COORDINATE.save(ctx.deps.storage, user_addr, &user_point_coordinate)?;
} else {
return Err(ContractError::InvalidADOType {
Expand Down Expand Up @@ -412,9 +410,9 @@ pub fn get_user_coordinate(deps: Deps, user: AndrAddr) -> Result<CoordinateInfo,
let user_coordinate = USER_COORDINATE.load(deps.storage, user_addr)?;

Ok(CoordinateInfo {
x: user_coordinate.x_coordinate,
y: user_coordinate.y_coordinate,
z: user_coordinate.z_coordinate,
x: user_coordinate.x_coordinate.to_string(),
y: user_coordinate.y_coordinate.to_string(),
z: user_coordinate.z_coordinate.map(|z| z.to_string()),
})
}

Expand Down
10 changes: 5 additions & 5 deletions contracts/math/andromeda-graph/src/testing/mock_querier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use cosmwasm_std::QuerierWrapper;
use cosmwasm_std::{
from_json,
testing::{mock_env, mock_info, MockApi, MockQuerier, MockStorage, MOCK_CONTRACT_ADDR},
Coin, ContractInfoResponse, OwnedDeps, Querier, QuerierResult, QueryRequest, SystemError,
SystemResult, WasmQuery,
Coin, ContractInfoResponse, OwnedDeps, Querier, QuerierResult, QueryRequest, SignedDecimal,
SystemError, SystemResult, WasmQuery,
};
use cosmwasm_std::{to_json_binary, Binary, ContractResult};

Expand Down Expand Up @@ -91,9 +91,9 @@ impl WasmMockQuerier {
match from_json(msg).unwrap() {
PointQueryMsg::GetPoint {} => {
let msg_response = PointCoordinate {
x_coordinate: "10".to_string(),
y_coordinate: "10".to_string(),
z_coordinate: Some("10".to_string()),
x_coordinate: SignedDecimal::percent(1000),
y_coordinate: SignedDecimal::percent(1000),
z_coordinate: Some(SignedDecimal::percent(1000)),
mdjakovic0920 marked this conversation as resolved.
Show resolved Hide resolved
};
SystemResult::Ok(ContractResult::Ok(to_json_binary(&msg_response).unwrap()))
}
Expand Down
2 changes: 1 addition & 1 deletion contracts/math/andromeda-point/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "andromeda-point"
version = "0.1.0"
version = "0.1.0-a.1"
authors = ["Mitar Djakovic <[email protected]>"]
edition = "2021"
rust-version = "1.75.0"
Expand Down
2 changes: 0 additions & 2 deletions contracts/math/andromeda-point/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ pub fn set_point(ctx: ExecuteContext, point: PointCoordinate) -> Result<Response
ContractError::Unauthorized {}
);

point.validate()?;

DATA.save(ctx.deps.storage, &point.clone())?;
DATA_OWNER.save(ctx.deps.storage, &sender)?;

Expand Down
111 changes: 44 additions & 67 deletions contracts/math/andromeda-point/src/testing/tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::contract::query;
use andromeda_math::point::{GetDataOwnerResponse, PointCoordinate, PointRestriction, QueryMsg};
use cosmwasm_std::{from_json, testing::mock_env};
use cosmwasm_std::{from_json, testing::mock_env, SignedDecimal};

use andromeda_std::{amp::AndrAddr, error::ContractError};

Expand All @@ -14,83 +14,40 @@ fn test_instantiation() {
#[test]
fn test_set_and_update_point() {
let (mut deps, info) = proper_initialization(PointRestriction::Private);
let point = PointCoordinate::from_f64(10_f64, 10_f64, Some(10_f64));
point.validate().unwrap();
let point = PointCoordinate {
x_coordinate: SignedDecimal::from_ratio(10, 1),
y_coordinate: SignedDecimal::from_ratio(10, 1),
z_coordinate: Some(SignedDecimal::from_ratio(10, 1)),
};

set_point(deps.as_mut(), &point, info.sender.as_ref()).unwrap();

let query_res: PointCoordinate = query_point(deps.as_ref()).unwrap();

assert_eq!(point, query_res);

let point = PointCoordinate::from_f64(5_f64, 5_f64, Some(5_f64));
point.validate().unwrap();
let point = PointCoordinate {
x_coordinate: SignedDecimal::from_ratio(5, 1),
y_coordinate: SignedDecimal::from_ratio(5, 1),
z_coordinate: Some(SignedDecimal::from_ratio(5, 1)),
};

set_point(deps.as_mut(), &point, info.sender.as_ref()).unwrap();

let query_res: PointCoordinate = query_point(deps.as_ref()).unwrap();

assert_eq!(point, query_res);
}

struct TestHandlePointCoordinate {
name: &'static str,
point_coordinate: PointCoordinate,
expected_error: Option<ContractError>,
}

#[test]
fn test_set_point_invalid() {
let test_cases = vec![
TestHandlePointCoordinate {
name: "Invalid x_coordinate",
point_coordinate: PointCoordinate {
x_coordinate: "10.abc".to_string(),
y_coordinate: "10".to_string(),
z_coordinate: Some("10".to_string()),
},
expected_error: Some(ContractError::ParsingError {
err: "x_coordinate: can not parse to f64".to_string(),
}),
},
TestHandlePointCoordinate {
name: "Invalid y_coordinate",
point_coordinate: PointCoordinate {
x_coordinate: "10".to_string(),
y_coordinate: "10.abc".to_string(),
z_coordinate: None,
},
expected_error: Some(ContractError::ParsingError {
err: "y_coordinate: can not parse to f64".to_string(),
}),
},
TestHandlePointCoordinate {
name: "Invalid z_coordinate",
point_coordinate: PointCoordinate {
x_coordinate: "10".to_string(),
y_coordinate: "10".to_string(),
z_coordinate: Some("10.abc".to_string()),
},
expected_error: Some(ContractError::ParsingError {
err: "z_coordinate: can not parse to f64".to_string(),
}),
},
];

for test in test_cases {
let res = test.point_coordinate.validate();

if let Some(err) = test.expected_error {
assert_eq!(res.unwrap_err(), err, "{}", test.name);
continue;
}

assert!(res.is_ok())
}
}

#[test]
fn test_delete_point() {
let (mut deps, info) = proper_initialization(PointRestriction::Private);
let point = PointCoordinate::from_f64(10_f64, 10_f64, Some(10_f64));
let point = PointCoordinate {
x_coordinate: SignedDecimal::from_ratio(10, 1),
y_coordinate: SignedDecimal::from_ratio(10, 1),
z_coordinate: Some(SignedDecimal::from_ratio(10, 1)),
};

set_point(deps.as_mut(), &point, info.sender.as_ref()).unwrap();
delete_point(deps.as_mut(), info.sender.as_ref()).unwrap();
query_point(deps.as_ref()).unwrap_err();
Expand All @@ -100,7 +57,11 @@ fn test_delete_point() {
fn test_restriction_private() {
let (mut deps, info) = proper_initialization(PointRestriction::Private);

let point = PointCoordinate::from_f64(10_f64, 10_f64, Some(10_f64));
let point = PointCoordinate {
x_coordinate: SignedDecimal::from_ratio(10, 1),
y_coordinate: SignedDecimal::from_ratio(10, 1),
z_coordinate: Some(SignedDecimal::from_ratio(10, 1)),
};
let external_user = "external".to_string();

// Set Point as owner
Expand All @@ -125,7 +86,11 @@ fn test_restriction_private() {
fn test_restriction_public() {
let (mut deps, info) = proper_initialization(PointRestriction::Public);

let point = PointCoordinate::from_f64(10_f64, 10_f64, Some(10_f64));
let point = PointCoordinate {
x_coordinate: SignedDecimal::from_ratio(10, 1),
y_coordinate: SignedDecimal::from_ratio(10, 1),
z_coordinate: Some(SignedDecimal::from_ratio(10, 1)),
};
let external_user = "external".to_string();

// Set Point as owner
Expand All @@ -152,8 +117,16 @@ fn test_restriction_public() {
fn test_restriction_restricted() {
let (mut deps, info) = proper_initialization(PointRestriction::Restricted);

let point = PointCoordinate::from_f64(10_f64, 10_f64, Some(10_f64));
let point2 = PointCoordinate::from_f64(5_f64, 5_f64, Some(5_f64));
let point = PointCoordinate {
x_coordinate: SignedDecimal::from_ratio(10, 1),
y_coordinate: SignedDecimal::from_ratio(10, 1),
z_coordinate: Some(SignedDecimal::from_ratio(10, 1)),
};
let point2 = PointCoordinate {
x_coordinate: SignedDecimal::from_ratio(5, 1),
y_coordinate: SignedDecimal::from_ratio(5, 1),
z_coordinate: Some(SignedDecimal::from_ratio(5, 1)),
};
let external_user = "external".to_string();
let external_user2 = "external2".to_string();

Expand Down Expand Up @@ -198,7 +171,11 @@ fn test_query_data_owner() {
let (mut deps, _) = proper_initialization(PointRestriction::Restricted);
let external_user = "external".to_string();
let external_user2 = "external2".to_string();
let point = PointCoordinate::from_f64(10_f64, 10_f64, Some(10_f64));
let point = PointCoordinate {
x_coordinate: SignedDecimal::from_ratio(10, 1),
y_coordinate: SignedDecimal::from_ratio(10, 1),
z_coordinate: Some(SignedDecimal::from_ratio(10, 1)),
};
set_point(deps.as_mut(), &point, &external_user.clone()).unwrap();

let res: GetDataOwnerResponse =
Expand Down
115 changes: 5 additions & 110 deletions packages/andromeda-math/src/point.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use andromeda_std::{amp::AndrAddr, andr_exec, andr_instantiate, andr_query, error::ContractError};
use andromeda_std::{amp::AndrAddr, andr_exec, andr_instantiate, andr_query};
use cosmwasm_schema::{cw_serde, QueryResponses};
use cosmwasm_std::SignedDecimal;

#[andr_instantiate]
#[cw_serde]
Expand All @@ -24,49 +25,9 @@ pub enum PointRestriction {

#[cw_serde]
pub struct PointCoordinate {
pub x_coordinate: String,
pub y_coordinate: String,
pub z_coordinate: Option<String>,
}

impl PointCoordinate {
pub fn from_f64(x_coordinate: f64, y_coordinate: f64, z_coordinate: Option<f64>) -> Self {
let z_coordinate: Option<String> = z_coordinate.map(|z| z.to_string());

Self {
x_coordinate: x_coordinate.to_string(),
y_coordinate: y_coordinate.to_string(),
z_coordinate,
}
}
pub fn validate(&self) -> Result<(), ContractError> {
let x_coordinate = self.x_coordinate.parse::<f64>();
if x_coordinate.is_err() {
return Err(ContractError::ParsingError {
err: "x_coordinate: can not parse to f64".to_string(),
});
}

let y_coordinate = self.y_coordinate.parse::<f64>();
if y_coordinate.is_err() {
return Err(ContractError::ParsingError {
err: "y_coordinate: can not parse to f64".to_string(),
});
}

match &self.z_coordinate {
None => (),
Some(z) => {
let z_coordinate = z.parse::<f64>();
if z_coordinate.is_err() {
return Err(ContractError::ParsingError {
err: "z_coordinate: can not parse to f64".to_string(),
});
}
}
}
Ok(())
}
pub x_coordinate: SignedDecimal,
pub y_coordinate: SignedDecimal,
pub z_coordinate: Option<SignedDecimal>,
}

#[andr_query]
Expand All @@ -83,69 +44,3 @@ pub enum QueryMsg {
pub struct GetDataOwnerResponse {
pub owner: AndrAddr,
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_validate_point_coordinate_valid() {
let point = PointCoordinate {
x_coordinate: "10".to_string(),
y_coordinate: "10".to_string(),
z_coordinate: Some("10".to_string()),
};
let res = point.validate();
assert!(res.is_ok());

let point = PointCoordinate {
x_coordinate: "10".to_string(),
y_coordinate: "10".to_string(),
z_coordinate: None,
};
let res = point.validate();
assert!(res.is_ok());
}

#[test]
fn test_validate_point_coordinate_invalid() {
let point = PointCoordinate {
x_coordinate: "10.abc".to_string(),
y_coordinate: "10".to_string(),
z_coordinate: Some("10".to_string()),
};
let res = point.validate().unwrap_err();
assert_eq!(
res,
ContractError::ParsingError {
err: "x_coordinate: can not parse to f64".to_string()
}
);

let point = PointCoordinate {
x_coordinate: "10".to_string(),
y_coordinate: "10.abc".to_string(),
z_coordinate: Some("10".to_string()),
};
let res = point.validate().unwrap_err();
assert_eq!(
res,
ContractError::ParsingError {
err: "y_coordinate: can not parse to f64".to_string()
}
);

let point = PointCoordinate {
x_coordinate: "10".to_string(),
y_coordinate: "10".to_string(),
z_coordinate: Some("10.xyz".to_string()),
};
let res = point.validate().unwrap_err();
assert_eq!(
res,
ContractError::ParsingError {
err: "z_coordinate: can not parse to f64".to_string()
}
);
}
}
Loading