Skip to content

Commit

Permalink
feat: allow setting of a decimal
Browse files Browse the repository at this point in the history
  • Loading branch information
0xphilipp committed Jul 19, 2023
1 parent f7fdd10 commit 1aaa12f
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 18 deletions.
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.

2 changes: 1 addition & 1 deletion contracts/update-scaling-factor/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "eris-update-scaling-factor"
version = "1.0.0"
version = "1.1.0"
authors = ["devs <devs@erisprotocol.com>"]
edition = "2021"

Expand Down
98 changes: 82 additions & 16 deletions contracts/update-scaling-factor/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,22 @@ pub fn instantiate(
_env: Env,
_info: MessageInfo,
msg: InstantiateMsg,
) -> StdResult<Response> {
) -> ContractResult {
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;

cw_ownable::initialize_owner(deps.storage, deps.api, Some(&msg.owner))?;

if msg.decimals > 17 {
return Err(ContractError::DecimalsMustBeLessThan18);
}

CONFIG.save(
deps.storage,
&Config {
pool_id: msg.pool_id,
hub: deps.api.addr_validate(&msg.hub)?,
scale_first: msg.scale_first,
decimals: Some(msg.decimals),
},
)?;
Ok(Response::default())
Expand All @@ -60,9 +66,17 @@ fn update_scaling_factor(deps: DepsMut, env: Env, _info: MessageInfo) -> Contrac
let exchange_rate = state.exchange_rate;

let scaling_factors = if config.scale_first {
get_factors(exchange_rate.numerator(), exchange_rate.denominator())?
get_factors(
exchange_rate.numerator(),
exchange_rate.denominator(),
config.decimals,
)?
} else {
get_factors(exchange_rate.denominator(), exchange_rate.numerator())?
get_factors(
exchange_rate.denominator(),
exchange_rate.numerator(),
config.decimals,
)?
};

let factors = scaling_factors
Expand All @@ -83,10 +97,18 @@ fn update_scaling_factor(deps: DepsMut, env: Env, _info: MessageInfo) -> Contrac
.add_message(msg))
}

fn get_factors(numerator: Uint128, denominator: Uint128) -> CustomResult<Vec<u64>> {
fn get_factors(
numerator: Uint128,
denominator: Uint128,
decimals: Option<u32>,
) -> CustomResult<Vec<u64>> {
let decimals = decimals.unwrap_or(4);
let removing = 18 - decimals;
let divisor = 10u128.pow(removing);

// 10^9 -> 9 numbers after the comma
let first = numerator.u128().div(1000000000);
let second = denominator.u128().div(1000000000);
let first: u128 = numerator.u128().div(divisor);
let second: u128 = denominator.u128().div(divisor);

Ok(vec![u64::try_from(first)?, u64::try_from(second)?])
}
Expand All @@ -97,10 +119,11 @@ fn update_config(deps: DepsMut, _env: Env, info: MessageInfo, msg: ExecuteMsg) -
pool_id: pool,
hub,
scale_first,
decimals,
} => {
cw_ownable::assert_owner(deps.storage, &info.sender)?;

CONFIG.update(deps.storage, |mut config| -> StdResult<Config> {
CONFIG.update(deps.storage, |mut config| -> CustomResult<Config> {
if let Some(pool) = pool {
config.pool_id = pool
};
Expand All @@ -112,6 +135,13 @@ fn update_config(deps: DepsMut, _env: Env, info: MessageInfo, msg: ExecuteMsg) -
config.hub = deps.api.addr_validate(hub.as_str())?;
};

if let Some(decimals) = decimals {
if decimals > 17 {
return Err(ContractError::DecimalsMustBeLessThan18);
}
config.decimals = Some(decimals);
};

Ok(config)
})?;

Expand All @@ -133,7 +163,7 @@ fn get_config(store: &dyn Storage) -> StdResult<Config> {
CONFIG.load(store)
}

#[entry_point]
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn migrate(deps: DepsMut, _env: Env, _msg: MigrateMsg) -> ContractResult {
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;

Expand All @@ -151,29 +181,65 @@ mod tests {
#[test]
fn test_scaling_factors() {
let exchange_rate = Decimal::from_str("1.19234").unwrap();
let factors = get_factors(exchange_rate.numerator(), exchange_rate.denominator())
.expect("should be set");
let factors = get_factors(
exchange_rate.numerator(),
exchange_rate.denominator(),
Some(9),
)
.expect("should be set");
assert_eq!(factors, vec![1192340000, 1000000000]);

// cutoff after 9th position
let exchange_rate = Decimal::from_str("1.192342342456").unwrap();
let factors = get_factors(exchange_rate.numerator(), exchange_rate.denominator())
.expect("should be set");
let factors = get_factors(
exchange_rate.numerator(),
exchange_rate.denominator(),
Some(9),
)
.expect("should be set");
assert_eq!(factors, vec![1192342342, 1000000000]);

// cutoff after 9th position
let exchange_rate = Decimal::from_str("1.192342342456").unwrap();
let factors = get_factors(
exchange_rate.numerator(),
exchange_rate.denominator(),
Some(2),
)
.expect("should be set");
assert_eq!(factors, vec![119, 100]);

// cutoff after 9th position
let exchange_rate = Decimal::from_str("1.192342342456").unwrap();
let factors = get_factors(
exchange_rate.numerator(),
exchange_rate.denominator(),
Some(1),
)
.expect("should be set");
assert_eq!(factors, vec![11, 10]);

// Decimal::MAX too big
let exchange_rate = Decimal::MAX;
let factors = get_factors(exchange_rate.numerator(), exchange_rate.denominator())
.expect_err("should be error");
let factors = get_factors(
exchange_rate.numerator(),
exchange_rate.denominator(),
Some(9),
)
.expect_err("should be error");
assert_eq!(
factors.to_string(),
"out of range integral type conversion attempted"
);

// max exchange_rate that is supported
let exchange_rate = Decimal::from_str("18446744073.709551615").unwrap();
let factors = get_factors(exchange_rate.numerator(), exchange_rate.denominator())
.expect("should be set");
let factors = get_factors(
exchange_rate.numerator(),
exchange_rate.denominator(),
Some(9),
)
.expect("should be set");
assert_eq!(factors, vec![u64::MAX, 1000000000]);
}
}
3 changes: 3 additions & 0 deletions contracts/update-scaling-factor/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@ pub enum ContractError {

#[error("not supported")]
NotSupported,

#[error("decimals must be less than 18")]
DecimalsMustBeLessThan18,
}
3 changes: 3 additions & 0 deletions contracts/update-scaling-factor/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub struct InstantiateMsg {
pub scale_first: bool,
pub hub: String,
pub owner: String,
pub decimals: u32,
}

#[cw_serde]
Expand All @@ -16,6 +17,7 @@ pub enum ExecuteMsg {
pool_id: Option<u64>,
hub: Option<String>,
scale_first: Option<bool>,
decimals: Option<u32>,
},
UpdateOwnership(cw_ownable::Action),
}
Expand All @@ -38,4 +40,5 @@ pub struct Config {
pub pool_id: u64,
pub hub: Addr,
pub scale_first: bool,
pub decimals: Option<u32>,
}

0 comments on commit 1aaa12f

Please sign in to comment.