Skip to content

Commit

Permalink
Merge pull request #162 from margined-protocol/fix/better-string-check
Browse files Browse the repository at this point in the history
changed string check
  • Loading branch information
matthewb99 authored Nov 1, 2022
2 parents e2a3849 + 1e8f0b7 commit 4c65663
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 64 deletions.
60 changes: 6 additions & 54 deletions contracts/margined_vamm/src/testing/swap_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,10 @@ fn test_bad_reserves() {
fn test_bad_asset_strings() {
let mut deps = mock_dependencies();

// test quote asset capitalisation
// test quote asset is alphabetic
let msg = InstantiateMsg {
decimals: 9u8,
quote_asset: "ETh".to_string(),
quote_asset: "ET4".to_string(),
base_asset: "USD".to_string(),
quote_asset_reserve: to_decimals(100),
base_asset_reserve: to_decimals(10_000),
Expand All @@ -201,13 +201,13 @@ fn test_bad_asset_strings() {

let res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap_err();

assert_eq!(res.to_string(), "Generic error: Assets not capitalised");
assert_eq!(res.to_string(), "Generic error: Not a valid string");

// test base asset capitalisation
// test base asset is alphabetic
let msg = InstantiateMsg {
decimals: 9u8,
quote_asset: "ETH".to_string(),
base_asset: "USd".to_string(),
base_asset: "US3".to_string(),
quote_asset_reserve: to_decimals(100),
base_asset_reserve: to_decimals(10_000),
funding_period: 3_600_u64,
Expand All @@ -222,55 +222,7 @@ fn test_bad_asset_strings() {

let res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap_err();

assert_eq!(res.to_string(), "Generic error: Assets not capitalised");

// test quote asset length
let msg = InstantiateMsg {
decimals: 6u8,
quote_asset: "ETHEREUM".to_string(),
base_asset: "USD".to_string(),
quote_asset_reserve: to_decimals(100),
base_asset_reserve: Uint128::from(10_000u128),
funding_period: 3_600_u64,
toll_ratio: Uint128::zero(),
spread_ratio: Uint128::zero(),
fluctuation_limit_ratio: Uint128::zero(),
margin_engine: Some("addr0000".to_string()),
insurance_fund: Some("insurance_fund".to_string()),
pricefeed: "oracle".to_string(),
};
let info = mock_info("addr0000", &[]);

let res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap_err();

assert_eq!(
res.to_string(),
"Generic error: Length of asset strings is incorrect"
);

// test base asset length
let msg = InstantiateMsg {
decimals: 6u8,
quote_asset: "ETH".to_string(),
base_asset: "UNITEDSTATESDOLLAR".to_string(),
quote_asset_reserve: to_decimals(100),
base_asset_reserve: Uint128::from(10_000u128),
funding_period: 3_600_u64,
toll_ratio: Uint128::zero(),
spread_ratio: Uint128::zero(),
fluctuation_limit_ratio: Uint128::zero(),
margin_engine: Some("addr0000".to_string()),
insurance_fund: Some("insurance_fund".to_string()),
pricefeed: "oracle".to_string(),
};
let info = mock_info("addr0000", &[]);

let res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap_err();

assert_eq!(
res.to_string(),
"Generic error: Length of asset strings is incorrect"
);
assert_eq!(res.to_string(), "Generic error: Not a valid string");
}

#[test]
Expand Down
14 changes: 4 additions & 10 deletions packages/margined_common/src/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,11 @@ pub fn validate_ratio(value: Uint128, decimals: Uint128) -> StdResult<Response>
Ok(Response::new())
}

/// Validates that the asset's string name is all caps, and either 3 or 4 characters
/// Validates that the asset's string name is all alphabetic
pub fn validate_assets(string: String) -> StdResult<Response> {
// check that the string is all caps
if string.to_uppercase() != string {
return Err(StdError::generic_err("Assets not capitalised"));
}

if string.len() != 3 && string.len() != 4 {
return Err(StdError::generic_err(
"Length of asset strings is incorrect",
));
// check that the string is all alphabetic
if !(string.chars().all(|x| x.is_alphabetic())) {
return Err(StdError::generic_err("Not a valid string"));
}

Ok(Response::new())
Expand Down

0 comments on commit 4c65663

Please sign in to comment.