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

refactor(airdrop-token-vesting): remove unnecessary fields #129

Merged
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
78 changes: 41 additions & 37 deletions contracts/airdrop-token-vesting/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ fn reward_users(
env: Env,
info: MessageInfo,
rewards: Vec<RewardUserRequest>,
mut vesting_schedule: VestingSchedule,
vesting_schedule: VestingSchedule,
) -> Result<Response, ContractError> {
let mut res = vec![];

Expand All @@ -160,33 +160,19 @@ fn reward_users(
StdError::generic_err("Insufficient funds for all rewards").into()
);
}
vesting_schedule.validate_time(env.block.time)?;
vesting_schedule.validate(env.block.time)?;

let mut attrs: Vec<Attribute> = vec![];
for req in rewards {
// validate amounts and cliff details if there's one
req.validate(vesting_schedule.clone())?;

// update the vesting schedule to match with the request
match &mut vesting_schedule {
VestingSchedule::LinearVesting { vesting_amount, .. } => {
*vesting_amount = req.vesting_amount;
}
VestingSchedule::LinearVestingWithCliff {
vesting_amount,
cliff_amount,
..
} => {
*vesting_amount = req.vesting_amount;
*cliff_amount = req.cliff_amount.unwrap();
}
}
req.validate()?;

let result = register_vesting_account(
deps.storage,
env.block.time,
req.user_address.clone(),
req.vesting_amount,
req.cliff_amount,
vesting_schedule.clone(),
);

Expand Down Expand Up @@ -221,7 +207,8 @@ fn register_vesting_account(
storage: &mut dyn Storage,
block_time: Timestamp,
address: String,
deposit_amount: Uint128,
vesting_amount: Uint128,
cliff_amount: Uint128,
vesting_schedule: VestingSchedule,
) -> Result<Response, ContractError> {
// vesting_account existence check
Expand All @@ -235,7 +222,8 @@ fn register_vesting_account(
address.as_str(),
&VestingAccount {
address: address.to_string(),
vesting_amount: deposit_amount,
vesting_amount,
cliff_amount,
vesting_schedule,
claimed_amount: Uint128::zero(),
},
Expand All @@ -244,7 +232,7 @@ fn register_vesting_account(
Ok(Response::new().add_attributes(vec![
("action", "register_vesting_account"),
("address", address.as_str()),
("vesting_amount", &deposit_amount.to_string()),
("vesting_amount", &vesting_amount.to_string()),
]))
}

Expand Down Expand Up @@ -281,9 +269,7 @@ fn deregister_vesting_account(
// remove vesting account
VESTING_ACCOUNTS.remove(deps.storage, address.as_str());

let vested_amount = account
.vesting_schedule
.vested_amount(env.block.time.seconds())?;
let vested_amount = account.vested_amount(env.block.time.seconds())?;
let claimed_amount = account.claimed_amount;

// transfer already vested amount to vested_token_recipient and if
Expand Down Expand Up @@ -362,9 +348,7 @@ fn claim(
}

let mut account = account.unwrap();
let vested_amount = account
.vesting_schedule
.vested_amount(env.block.time.seconds())?;
let vested_amount = account.vested_amount(env.block.time.seconds())?;
let claimed_amount = account.claimed_amount;

let claimable_amount = vested_amount.checked_sub(claimed_amount)?;
Expand Down Expand Up @@ -439,9 +423,8 @@ fn vesting_account(
match account {
None => Err(StdError::not_found("Vesting account not found")),
Some(account) => {
let vested_amount = account
.vesting_schedule
.vested_amount(env.block.time.seconds())?;
let vested_amount =
account.vested_amount(env.block.time.seconds())?;

let vesting = VestingData {
vesting_account: account.clone(),
Expand Down Expand Up @@ -530,21 +513,42 @@ pub mod tests {
rewards: vec![RewardUserRequest {
user_address: "addr0001".to_string(),
vesting_amount: Uint128::new(5000u128),
cliff_amount: None,
cliff_amount: Uint128::zero(),
}],
vesting_schedule: VestingSchedule::LinearVesting {
vesting_schedule: VestingSchedule::LinearVestingWithCliff {
start_time: Uint64::new(100),
end_time: Uint64::new(110),
vesting_amount: Uint128::new(1000000u128),
cliff_time: Uint64::new(105),
},
};

execute(
let res = execute(
deps.as_mut(),
env.clone(), // Use the custom environment with the adjusted block time
testing::mock_info("admin-sender", &[coin(1000000, "token")]),
register_msg,
)?;
assert_eq!(
res.attributes,
vec![
Attribute {
key: "action".to_string(),
value: "register_vesting_account".to_string()
},
Attribute {
key: "address".to_string(),
value: "addr0001".to_string()
},
Attribute {
key: "vesting_amount".to_string(),
value: "5000".to_string()
},
Attribute {
key: "method".to_string(),
value: "reward_users".to_string()
}
]
);

// Try to deregister with unauthorized sender
let msg = ExecuteMsg::DeregisterVestingAccount {
Expand Down Expand Up @@ -577,12 +581,12 @@ pub mod tests {
rewards: vec![RewardUserRequest {
user_address: "addr0001".to_string(),
vesting_amount: Uint128::new(5000u128),
cliff_amount: None,
cliff_amount: Uint128::zero(),
}],
vesting_schedule: VestingSchedule::LinearVesting {
vesting_schedule: VestingSchedule::LinearVestingWithCliff {
start_time: Uint64::new(100),
end_time: Uint64::new(110),
vesting_amount: Uint128::new(1000000u128),
cliff_time: Uint64::new(105),
},
};

Expand Down
3 changes: 0 additions & 3 deletions contracts/airdrop-token-vesting/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ pub enum ContractError {

#[derive(thiserror::Error, Debug, PartialEq)]
pub enum CliffError {
#[error("cliff_amount is zero but should be greater than 0")]
ZeroAmount,

#[error("cliff_time ({cliff_time}) should be greater than block_time ({block_time})")]
InvalidTime { cliff_time: u64, block_time: u64 },

Expand Down
Loading
Loading