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

ref: rename release_unit to release_duration #674

Merged
merged 2 commits into from
Nov 27, 2024
Merged
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
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/finance/andromeda-vesting/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "andromeda-vesting"
version = "3.0.4-b.1"
version = "3.0.4-b.2"
edition = "2021"
rust-version = "1.75.0"

Expand Down
30 changes: 16 additions & 14 deletions contracts/finance/andromeda-vesting/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ pub fn handle_execute(mut ctx: ExecuteContext, msg: ExecuteMsg) -> Result<Respon
match msg {
ExecuteMsg::CreateBatch {
lockup_duration,
release_unit,
release_duration,
release_amount,
} => execute_create_batch(ctx, lockup_duration, release_unit, release_amount),
} => execute_create_batch(ctx, lockup_duration, release_duration, release_amount),
ExecuteMsg::Claim {
number_of_claims,
batch_id,
Expand All @@ -105,7 +105,7 @@ pub fn handle_execute(mut ctx: ExecuteContext, msg: ExecuteMsg) -> Result<Respon
fn execute_create_batch(
ctx: ExecuteContext,
lockup_duration: Option<Milliseconds>,
release_unit: Milliseconds,
release_duration: Milliseconds,
release_amount: WithdrawalType,
) -> Result<Response, ContractError> {
let ExecuteContext {
Expand Down Expand Up @@ -136,7 +136,7 @@ fn execute_create_batch(
);

ensure!(
!release_unit.is_zero() && !release_amount.is_zero(),
!release_duration.is_zero() && !release_amount.is_zero(),
ContractError::InvalidZeroAmount {}
);
ensure!(
Expand Down Expand Up @@ -180,7 +180,7 @@ fn execute_create_batch(
amount: funds.amount,
amount_claimed: Uint128::zero(),
lockup_end,
release_unit,
release_duration,
release_amount,
last_claimed_release_time: lockup_end,
};
Expand All @@ -191,7 +191,7 @@ fn execute_create_batch(
.add_attribute("action", "create_batch")
.add_attribute("amount", funds.amount)
.add_attribute("lockup_end", lockup_end.to_string())
.add_attribute("release_unit", release_unit.to_string())
.add_attribute("release_duration", release_duration.to_string())
.add_attribute("release_amount", release_amount_string))
}

Expand Down Expand Up @@ -274,7 +274,8 @@ fn execute_claim_all(
let key = batches().key(batch_id);

let elapsed_time = up_to_time.minus_milliseconds(batch.last_claimed_release_time);
let num_available_claims = elapsed_time.milliseconds() / batch.release_unit.milliseconds();
let num_available_claims =
elapsed_time.milliseconds() / batch.release_duration.milliseconds();

let amount_to_send = claim_batch(
&deps.querier,
Expand Down Expand Up @@ -321,7 +322,7 @@ fn claim_batch(
.query_balance(querier, env.contract.address.to_owned())?;

let elapsed_time = current_time.minus_milliseconds(batch.last_claimed_release_time);
let num_available_claims = elapsed_time.milliseconds() / batch.release_unit.milliseconds();
let num_available_claims = elapsed_time.milliseconds() / batch.release_duration.milliseconds();

let number_of_claims = cmp::min(
number_of_claims.unwrap_or(num_available_claims),
Expand All @@ -342,19 +343,20 @@ fn claim_batch(
batch.amount_claimed = batch.amount_claimed.checked_add(amount_to_send)?;

// Safe math version
let claims_release_unit = number_of_claims.checked_mul(batch.release_unit.milliseconds());
if claims_release_unit.is_none() {
let claims_release_duration =
number_of_claims.checked_mul(batch.release_duration.milliseconds());
if claims_release_duration.is_none() {
return Err(ContractError::Overflow {});
}

let claims_release_unit = Milliseconds(claims_release_unit.unwrap());
let claims_release_duration = Milliseconds(claims_release_duration.unwrap());

batch.last_claimed_release_time = batch
.last_claimed_release_time
.plus_milliseconds(claims_release_unit);
.plus_milliseconds(claims_release_duration);

// The unsafe version
// batch.last_claimed_release_time += number_of_claims * batch.release_unit;
// batch.last_claimed_release_time += number_of_claims * batch.release_duration;
}

Ok(amount_to_send)
Expand Down Expand Up @@ -433,7 +435,7 @@ fn get_batch_response(
number_of_available_claims,
lockup_end: batch.lockup_end,
release_amount: batch.release_amount,
release_unit: batch.release_unit,
release_duration: batch.release_duration,
last_claimed_release_time: previous_last_claimed_release_time,
};

Expand Down
10 changes: 5 additions & 5 deletions contracts/finance/andromeda-vesting/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ pub struct Batch {
/// When the lockup ends.
pub lockup_end: Milliseconds,
/// How often releases occur.
pub release_unit: Milliseconds,
/// Specifies how much is to be released after each `release_unit`. If
pub release_duration: Milliseconds,
/// Specifies how much is to be released after each `release_duration`. If
/// it is a percentage, it would be the percentage of the original amount.
pub release_amount: WithdrawalType,
/// The time at which the last claim took place in seconds.
Expand Down Expand Up @@ -140,7 +140,7 @@ mod tests {
amount: Uint128::new(100),
amount_claimed: Uint128::zero(),
lockup_end: current_time.plus_seconds(10),
release_unit: Milliseconds::from_seconds(10),
release_duration: Milliseconds::from_seconds(10),
release_amount: WithdrawalType::Amount(Uint128::new(10)),
last_claimed_release_time: current_time.minus_seconds(1),
};
Expand All @@ -149,7 +149,7 @@ mod tests {
amount: Uint128::new(100),
amount_claimed: Uint128::zero(),
lockup_end: current_time.minus_seconds(1),
release_unit: Milliseconds::from_seconds(10),
release_duration: Milliseconds::from_seconds(10),
release_amount: WithdrawalType::Amount(Uint128::new(10)),
last_claimed_release_time: current_time.minus_seconds(1),
};
Expand All @@ -158,7 +158,7 @@ mod tests {
amount: Uint128::new(100),
amount_claimed: Uint128::new(100),
lockup_end: current_time.minus_seconds(1),
release_unit: Milliseconds::from_seconds(10),
release_duration: Milliseconds::from_seconds(10),
release_amount: WithdrawalType::Amount(Uint128::new(10)),
last_claimed_release_time: current_time.minus_seconds(1),
};
Expand Down
Loading
Loading