Skip to content

Commit

Permalink
borrow input Deps, Env and MessageInfo whenever possible (#224)
Browse files Browse the repository at this point in the history
  • Loading branch information
dusan-maksimovic authored Feb 4, 2025
1 parent 4b5a2ff commit ed88739
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 95 deletions.
2 changes: 1 addition & 1 deletion artifacts/checksums.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
98e7c175d92fb0c9791886af183f65a21c88e85ead116c8eac50a68d1595776a hydro.wasm
f8a0ea2d42c8439742497b6f0f86a0b2b427a82f949fd3a639bf19706ba32319 hydro.wasm
83f12ed4aa3b4900588096c639a9abec78080a4c91720442b47cba1b8059e63a tribute.wasm
Binary file modified artifacts/hydro.wasm
Binary file not shown.
98 changes: 49 additions & 49 deletions contracts/hydro/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,8 @@ fn lock_tokens(

let funds = info.funds[0].clone();

let validator =
validate_denom(deps.as_ref(), current_round, &constants, funds.denom).map_err(|err| {
let validator = validate_denom(&deps.as_ref(), current_round, &constants, funds.denom)
.map_err(|err| {
ContractError::Std(StdError::generic_err(format!("validating denom: {}", err)))
})?;

Expand All @@ -306,7 +306,7 @@ fn lock_tokens(
)?;

// validate that the user does not have too many locks
if get_lock_count(deps.as_ref(), info.sender.clone()) >= MAX_LOCK_ENTRIES {
if get_lock_count(&deps.as_ref(), info.sender.clone()) >= MAX_LOCK_ENTRIES {
return Err(ContractError::Std(StdError::generic_err(format!(
"User has too many locks, only {} locks allowed",
MAX_LOCK_ENTRIES
Expand Down Expand Up @@ -453,7 +453,7 @@ fn refresh_lock_duration(
}

fn refresh_single_lock(
deps: &mut DepsMut<'_, NeutronQuery>,
deps: &mut DepsMut<NeutronQuery>,
info: &MessageInfo,
env: &Env,
constants: &Constants,
Expand All @@ -479,7 +479,7 @@ fn refresh_single_lock(
env.block.height,
)?;
let validator_result = validate_denom(
deps.as_ref(),
&deps.as_ref(),
current_round_id,
constants,
lock_entry.funds.denom.clone(),
Expand Down Expand Up @@ -666,7 +666,7 @@ fn unlock_tokens(
fn validate_previous_round_vote(
deps: &DepsMut<NeutronQuery>,
env: &Env,
sender: Addr,
sender: &Addr,
) -> Result<(), ContractError> {
let constants = load_current_constants(&deps.as_ref(), env)?;
let current_round_id = compute_current_round_id(env, &constants)?;
Expand Down Expand Up @@ -907,7 +907,7 @@ fn vote(
// save the new power into the proposal
let total_power =
get_total_power_for_proposal(deps.as_ref().storage, vote.prop_id)?;
proposal.power = total_power.to_uint_ceil(); // TODO: decide whether we need to round or represent as decimals
proposal.power = total_power.to_uint_ceil();

// Save the proposal
PROPOSAL_MAP.save(
Expand Down Expand Up @@ -979,7 +979,7 @@ fn vote(

// get the validator from the denom
let validator = match validate_denom(
deps.as_ref(),
&deps.as_ref(),
round_id,
&constants,
lock_entry.clone().funds.denom,
Expand All @@ -1002,7 +1002,7 @@ fn vote(
get_lock_time_weighted_shares(
&constants.round_lock_power_schedule,
round_end,
lock_entry.clone(),
&lock_entry,
lock_epoch_length,
),
Uint128::one(),
Expand Down Expand Up @@ -1077,7 +1077,7 @@ fn vote(
pub fn get_lock_time_weighted_shares(
round_lock_power_schedule: &RoundLockPowerSchedule,
round_end: Timestamp,
lock_entry: LockEntry,
lock_entry: &LockEntry,
lock_epoch_length: u64,
) -> Uint128 {
if round_end.nanos() > lock_entry.lock_end.nanos() {
Expand Down Expand Up @@ -1427,7 +1427,7 @@ fn create_icqs_for_validators(

// Validates that enough funds were sent to create ICQs for the given validator addresses.
fn validate_icq_deposit_funds_sent(
deps: DepsMut<'_, NeutronQuery>,
deps: DepsMut<NeutronQuery>,
info: &MessageInfo,
num_created_icqs: u64,
) -> Result<(), ContractError> {
Expand Down Expand Up @@ -1694,27 +1694,27 @@ pub fn query(deps: Deps<NeutronQuery>, env: Env, msg: QueryMsg) -> StdResult<Bin
start_from,
limit,
} => to_json_binary(&query_all_user_lockups(
deps, env, address, start_from, limit,
&deps, &env, address, start_from, limit,
)?),
QueryMsg::SpecificUserLockups { address, lock_ids } => {
to_json_binary(&query_specific_user_lockups(deps, env, address, lock_ids)?)
}
QueryMsg::SpecificUserLockups { address, lock_ids } => to_json_binary(
&query_specific_user_lockups(&deps, &env, address, lock_ids)?,
),
QueryMsg::AllUserLockupsWithTrancheInfos {
address,
start_from,
limit,
} => to_json_binary(&query_all_user_lockups_with_tranche_infos(
deps, env, address, start_from, limit,
&deps, &env, address, start_from, limit,
)?),
QueryMsg::SpecificUserLockupsWithTrancheInfos { address, lock_ids } => to_json_binary(
&query_specific_user_lockups_with_tranche_infos(deps, env, address, lock_ids)?,
&query_specific_user_lockups_with_tranche_infos(&deps, &env, address, lock_ids)?,
),
QueryMsg::ExpiredUserLockups {
address,
start_from,
limit,
} => to_json_binary(&query_expired_user_lockups(
deps, env, address, start_from, limit,
&deps, &env, address, start_from, limit,
)?),
QueryMsg::UserVotingPower { address } => {
to_json_binary(&query_user_voting_power(deps, env, address)?)
Expand Down Expand Up @@ -1824,9 +1824,9 @@ pub fn query_round_total_power(
deps: Deps<NeutronQuery>,
round_id: u64,
) -> StdResult<RoundTotalVotingPowerResponse> {
let total_round_power = get_total_power_for_round(deps, round_id)?;
let total_round_power = get_total_power_for_round(&deps, round_id)?;
Ok(RoundTotalVotingPowerResponse {
total_voting_power: total_round_power.to_uint_ceil(), // TODO: decide on rounding
total_voting_power: total_round_power.to_uint_ceil(),
})
}

Expand All @@ -1837,8 +1837,8 @@ pub fn query_constants(deps: Deps<NeutronQuery>, env: Env) -> StdResult<Constant
}

fn get_user_lockups_with_predicate(
deps: Deps<NeutronQuery>,
env: Env,
deps: &Deps<NeutronQuery>,
env: &Env,
address: String,
predicate: impl FnMut(&LockEntry) -> bool,
start_from: u32,
Expand All @@ -1848,8 +1848,8 @@ fn get_user_lockups_with_predicate(

let raw_lockups = query_user_lockups(deps, addr, predicate, start_from, limit);

let constants = load_current_constants(&deps, &env)?;
let current_round_id = compute_current_round_id(&env, &constants)?;
let constants = load_current_constants(deps, env)?;
let current_round_id = compute_current_round_id(env, &constants)?;
let round_end = compute_round_end(&constants, current_round_id)?;

// enrich the lockups by computing the voting power for each lockup
Expand All @@ -1864,8 +1864,8 @@ fn get_user_lockups_with_predicate(
}

pub fn query_all_user_lockups(
deps: Deps<NeutronQuery>,
env: Env,
deps: &Deps<NeutronQuery>,
env: &Env,
address: String,
start_from: u32,
limit: u32,
Expand All @@ -1875,8 +1875,8 @@ pub fn query_all_user_lockups(
}

pub fn query_specific_user_lockups(
deps: Deps<NeutronQuery>,
env: Env,
deps: &Deps<NeutronQuery>,
env: &Env,
address: String,
lock_ids: Vec<u64>,
) -> StdResult<SpecificUserLockupsResponse> {
Expand All @@ -1896,8 +1896,8 @@ pub fn query_specific_user_lockups(

// Helper function to handle the common logic for both query functions
fn enrich_lockups_with_tranche_infos(
deps: Deps<NeutronQuery>,
env: Env,
deps: &Deps<NeutronQuery>,
env: &Env,
address: String,
lockups: Vec<LockEntryWithPower>,
) -> StdResult<Vec<LockupWithPerTrancheInfo>> {
Expand All @@ -1908,8 +1908,8 @@ fn enrich_lockups_with_tranche_infos(
.map(|tranche| tranche.unwrap().1.id)
.collect::<Vec<u64>>();

let constants = load_current_constants(&deps, &env)?;
let current_round_id = compute_current_round_id(&env, &constants)?;
let constants = load_current_constants(deps, env)?;
let current_round_id = compute_current_round_id(env, &constants)?;

// enrich lockups with some info per tranche
let lockups_with_per_tranche_info: Vec<LockupWithPerTrancheInfo> = lockups
Expand Down Expand Up @@ -1984,26 +1984,26 @@ fn enrich_lockups_with_tranche_infos(
}

pub fn query_all_user_lockups_with_tranche_infos(
deps: Deps<NeutronQuery>,
env: Env,
deps: &Deps<NeutronQuery>,
env: &Env,
address: String,
start_from: u32,
limit: u32,
) -> StdResult<AllUserLockupsWithTrancheInfosResponse> {
let lockups = query_all_user_lockups(deps, env.clone(), address.clone(), start_from, limit)?;
let lockups = query_all_user_lockups(deps, env, address.clone(), start_from, limit)?;
let enriched_lockups = enrich_lockups_with_tranche_infos(deps, env, address, lockups.lockups)?;
Ok(AllUserLockupsWithTrancheInfosResponse {
lockups_with_per_tranche_infos: enriched_lockups,
})
}

pub fn query_specific_user_lockups_with_tranche_infos(
deps: Deps<NeutronQuery>,
env: Env,
deps: &Deps<NeutronQuery>,
env: &Env,
address: String,
lock_ids: Vec<u64>,
) -> StdResult<SpecificUserLockupsWithTrancheInfosResponse> {
let lockups = query_specific_user_lockups(deps, env.clone(), address.clone(), lock_ids)?;
let lockups = query_specific_user_lockups(deps, env, address.clone(), lock_ids)?;
let enriched_lockups = enrich_lockups_with_tranche_infos(deps, env, address, lockups.lockups)?;

Ok(SpecificUserLockupsWithTrancheInfosResponse {
Expand All @@ -2012,8 +2012,8 @@ pub fn query_specific_user_lockups_with_tranche_infos(
}

pub fn query_expired_user_lockups(
deps: Deps<NeutronQuery>,
env: Env,
deps: &Deps<NeutronQuery>,
env: &Env,
address: String,
start_from: u32,
limit: u32,
Expand Down Expand Up @@ -2133,7 +2133,7 @@ pub fn get_user_voting_power_for_past_round(
}
})
.map(|lockup| {
to_lockup_with_power(*deps, constants, round_id, round_end, lockup)
to_lockup_with_power(deps, constants, round_id, round_end, lockup)
.current_voting_power
.u128()
})
Expand All @@ -2156,7 +2156,7 @@ where
.range(deps.storage, None, None, Order::Ascending)
.filter_map(filter)
.map(|lockup| {
to_lockup_with_power(*deps, constants, round_id, round_end, lockup)
to_lockup_with_power(deps, constants, round_id, round_end, lockup)
.current_voting_power
.u128()
})
Expand Down Expand Up @@ -2306,7 +2306,7 @@ pub fn query_top_n_proposals(
}

// get total voting power for the round
let total_voting_power = get_total_power_for_round(deps, round_id)?.to_uint_ceil(); // TODO: decide on rounding
let total_voting_power = get_total_power_for_round(&deps, round_id)?.to_uint_ceil();

let top_proposals = top_props
.into_iter()
Expand Down Expand Up @@ -2338,7 +2338,7 @@ pub fn query_tranches(deps: Deps<NeutronQuery>) -> StdResult<TranchesResponse> {
}

fn query_user_lockups(
deps: Deps<NeutronQuery>,
deps: &Deps<NeutronQuery>,
user_address: Addr,
predicate: impl FnMut(&LockEntry) -> bool,
start_from: u32,
Expand Down Expand Up @@ -2489,14 +2489,14 @@ fn update_voting_power_on_proposals(
Some(lock_entry) => get_lock_time_weighted_shares(
&constants.round_lock_power_schedule,
round_end,
lock_entry.clone(),
lock_entry,
lock_epoch_length,
),
};
let new_scaled_shares = get_lock_time_weighted_shares(
&constants.round_lock_power_schedule,
round_end,
new_lock_entry.clone(),
&new_lock_entry,
lock_epoch_length,
);

Expand Down Expand Up @@ -2779,15 +2779,15 @@ where
}

// Returns the number of locks for a given user
fn get_lock_count(deps: Deps<NeutronQuery>, user_address: Addr) -> usize {
fn get_lock_count(deps: &Deps<NeutronQuery>, user_address: Addr) -> usize {
LOCKS_MAP
.prefix(user_address)
.range(deps.storage, None, None, Order::Ascending)
.count()
}

fn to_lockup_with_power(
deps: Deps<NeutronQuery>,
deps: &Deps<NeutronQuery>,
constants: &Constants,
round_id: u64,
round_end: Timestamp,
Expand Down Expand Up @@ -2819,7 +2819,7 @@ fn to_lockup_with_power(
let time_weighted_shares = get_lock_time_weighted_shares(
&constants.round_lock_power_schedule,
round_end,
lock_entry.clone(),
&lock_entry,
constants.lock_epoch_length,
);

Expand Down
8 changes: 4 additions & 4 deletions contracts/hydro/src/lsm_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ pub const COSMOS_VALIDATOR_ADDR_LENGTH: usize = 52; // e.g. cosmosvaloper15w6ra6
// of a validator that is also among the top max_validators validators
// for the given round, and returns the address of that validator.
pub fn validate_denom(
deps: Deps<NeutronQuery>,
deps: &Deps<NeutronQuery>,
round_id: u64,
constants: &Constants,
denom: String,
) -> StdResult<String> {
let validator = resolve_validator_from_denom(&deps, constants, denom)?;
let validator = resolve_validator_from_denom(deps, constants, denom)?;
let max_validators = constants.max_validator_shares_participating;

if is_active_round_validator(deps.storage, round_id, &validator) {
Expand Down Expand Up @@ -85,7 +85,7 @@ pub fn is_active_round_validator(storage: &dyn Storage, round_id: u64, validator
}

// Gets the current list of active validators for the given round
pub fn get_round_validators(deps: Deps<NeutronQuery>, round_id: u64) -> Vec<ValidatorInfo> {
pub fn get_round_validators(deps: &Deps<NeutronQuery>, round_id: u64) -> Vec<ValidatorInfo> {
VALIDATORS_INFO
.prefix(round_id)
.range(deps.storage, None, None, Order::Ascending)
Expand Down Expand Up @@ -292,7 +292,7 @@ pub fn update_total_power_due_to_power_ratio_change(
Ok(())
}

pub fn get_total_power_for_round(deps: Deps<NeutronQuery>, round_id: u64) -> StdResult<Decimal> {
pub fn get_total_power_for_round(deps: &Deps<NeutronQuery>, round_id: u64) -> StdResult<Decimal> {
Ok(
match TOTAL_VOTING_POWER_PER_ROUND.may_load(deps.storage, round_id)? {
None => Decimal::zero(),
Expand Down
2 changes: 1 addition & 1 deletion contracts/hydro/src/score_keeper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ mod tests {
let deps = mock_dependencies(no_op_grpc_query_mock());
let index_key = 5;

let total_power = get_total_power_for_round(deps.as_ref(), index_key).unwrap();
let total_power = get_total_power_for_round(&deps.as_ref(), index_key).unwrap();
assert_eq!(total_power, Decimal::zero());

let total_power = get_total_power_for_proposal(deps.as_ref().storage, index_key).unwrap();
Expand Down
6 changes: 3 additions & 3 deletions contracts/hydro/src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ fn lock_tokens_basic_test() {
let res = execute(deps.as_mut(), env.clone(), info2.clone(), msg);
assert!(res.is_ok());

let res = query_all_user_lockups(deps.as_ref(), env.clone(), info.sender.to_string(), 0, 2000);
let res = query_all_user_lockups(&deps.as_ref(), &env, info.sender.to_string(), 0, 2000);
assert!(res.is_ok());
let res = res.unwrap();
assert_eq!(2, res.lockups.len());
Expand Down Expand Up @@ -3190,8 +3190,8 @@ fn test_refresh_multiple_locks() {
// Verify the new lock durations
for (sender, expected_durations) in &case.expected_new_lock_durations {
let lockups = query_all_user_lockups(
deps.as_ref(),
env.clone(),
&deps.as_ref(),
&env,
get_address_as_str(&deps.api, sender),
0,
100,
Expand Down
Loading

0 comments on commit ed88739

Please sign in to comment.