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(token-vesting): Use match to get concrete type #66

Closed
wants to merge 2 commits into from
Closed
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 artifacts/checksums.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
c549f596f4faba0ef06eafb8fa56984ad0d6bd861e67e9a02ca612ada64904a5 lockup.wasm
c65ba2019c202f0e637201f6aa06ae9716145c9d0d2d7de0077ebfff59b2f130 pricefeed.wasm
a9328c1a53f52e279ea38f93d62102d92ace8234b7061e606df279a9cc9bc20c shifter.wasm
d76da8d1886f4f88655db753a4d91d29bf7cf0eed337e803ad28430e58b4642b token_vesting.wasm
fe63adebd7fac5948001a6bd515207e67199fdb28123804c32b5a89371b20592 token_vesting.wasm
2 changes: 1 addition & 1 deletion artifacts/checksums_intermediate.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ c90a93d49a68fc067f9e29e12fdbf49dcce92e96915c88b8f4d7c2a258ac85bb target/wasm32-
48fab3b1295b9e274e7b31f5efe8c7fd91a16983ffcb1f403afdb6fd32958871 target/wasm32-unknown-unknown/release/lockup.wasm
d3e5b3b5c1e836d83d1106b839dc30ed6278f0f0a27c9181c94b8ae8b6554e5d target/wasm32-unknown-unknown/release/pricefeed.wasm
3b67e260906a1b510080c12028c1f2aee3ea87e30802e760307252f193c43c0b target/wasm32-unknown-unknown/release/shifter.wasm
a7c2a01a66685220cd51c2e9594b68d3ca06995edb2605284206637837ed30ee target/wasm32-unknown-unknown/release/token_vesting.wasm
b2f4375a765103caa6be336dafb89385bdc8db867c8064a2b36840df0820af63 target/wasm32-unknown-unknown/release/token_vesting.wasm
Binary file modified artifacts/token_vesting.wasm
Binary file not shown.
23 changes: 14 additions & 9 deletions contracts/token-vesting/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,19 @@
)
}
ExecuteMsg::DeregisterVestingAccount {
address,
denom,
vested_token_recipient,
left_vesting_token_recipient,
} => deregister_vesting_account(
deps,
env,
info,
address,
denom,
vested_token_recipient,
left_vesting_token_recipient,
),

Check warning on line 75 in contracts/token-vesting/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

contracts/token-vesting/src/contract.rs#L63-L75

Added lines #L63 - L75 were not covered by tests
ExecuteMsg::Claim { denoms, recipient } => {
claim(deps, env, info, denoms, recipient)
}
Expand All @@ -92,7 +92,7 @@

// vesting_account existence check
if VESTING_ACCOUNTS.has(storage, (address.as_str(), &denom_key)) {
return Err(StdError::generic_err("already exists"));

Check warning on line 95 in contracts/token-vesting/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

contracts/token-vesting/src/contract.rs#L95

Added line #L95 was not covered by tests
}

// validate vesting schedule
Expand Down Expand Up @@ -123,83 +123,88 @@
]))
}

fn deregister_vesting_account(
deps: DepsMut,
env: Env,
info: MessageInfo,
address: String,
denom: Denom,
vested_token_recipient: Option<String>,
left_vesting_token_recipient: Option<String>,
) -> StdResult<Response> {
let denom_key = denom_to_key(denom.clone());
let sender = info.sender;

let mut messages: Vec<CosmosMsg> = vec![];

// vesting_account existence check
let account = VESTING_ACCOUNTS
.may_load(deps.storage, (address.as_str(), &denom_key))?;
if account.is_none() {
return Err(StdError::generic_err(format!(
"vesting entry is not found for denom {:?}",
to_string(&denom).unwrap(),
)));
}
let account: VestingAccount;
match VESTING_ACCOUNTS
.may_load(deps.storage, (address.as_str(), &denom_key))?

Check warning on line 143 in contracts/token-vesting/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

contracts/token-vesting/src/contract.rs#L126-L143

Added lines #L126 - L143 were not covered by tests
{
Some(acc) => {
account = acc;
}

Check warning on line 147 in contracts/token-vesting/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

contracts/token-vesting/src/contract.rs#L145-L147

Added lines #L145 - L147 were not covered by tests
None => {
return Err(StdError::generic_err(format!(
"vesting entry is not found for denom {:?}",
to_string(&denom).unwrap(),
)));

Check warning on line 152 in contracts/token-vesting/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

contracts/token-vesting/src/contract.rs#L149-L152

Added lines #L149 - L152 were not covered by tests
}
};

let account = account.unwrap();
if account.master_address.is_none()
|| account.master_address.unwrap() != sender

Check warning on line 157 in contracts/token-vesting/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

contracts/token-vesting/src/contract.rs#L156-L157

Added lines #L156 - L157 were not covered by tests
{
return Err(StdError::generic_err("unauthorized"));
}

// remove vesting account
VESTING_ACCOUNTS.remove(deps.storage, (address.as_str(), &denom_key));

Check warning on line 163 in contracts/token-vesting/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

contracts/token-vesting/src/contract.rs#L159-L163

Added lines #L159 - L163 were not covered by tests

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

Check warning on line 168 in contracts/token-vesting/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

contracts/token-vesting/src/contract.rs#L165-L168

Added lines #L165 - L168 were not covered by tests

// transfer already vested but not claimed amount to
// a account address or the given `vested_token_recipient` address
let claimable_amount = vested_amount.checked_sub(claimed_amount)?;
if !claimable_amount.is_zero() {
let recipient =
vested_token_recipient.unwrap_or_else(|| address.to_string());
let msg_send: CosmosMsg = build_send_msg(
account.vesting_denom.clone(),
claimable_amount,
recipient,
)?;
messages.push(msg_send);
}

Check warning on line 182 in contracts/token-vesting/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

contracts/token-vesting/src/contract.rs#L172-L182

Added lines #L172 - L182 were not covered by tests

// transfer left vesting amount to owner or
// the given `left_vesting_token_recipient` address
let left_vesting_amount =
account.vesting_amount.checked_sub(vested_amount)?;
if !left_vesting_amount.is_zero() {
let recipient =
left_vesting_token_recipient.unwrap_or_else(|| sender.to_string());
let msg_send: CosmosMsg = build_send_msg(
account.vesting_denom.clone(),
left_vesting_amount,
recipient,
)?;
messages.push(msg_send);
}

Check warning on line 197 in contracts/token-vesting/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

contracts/token-vesting/src/contract.rs#L186-L197

Added lines #L186 - L197 were not covered by tests

Ok(Response::new().add_messages(messages).add_attributes(vec![
("action", "deregister_vesting_account"),
("address", address.as_str()),
("vesting_denom", &to_string(&account.vesting_denom).unwrap()),
("vesting_amount", &account.vesting_amount.to_string()),
("vested_amount", &vested_amount.to_string()),
("left_vesting_amount", &left_vesting_amount.to_string()),
]))
}

Check warning on line 207 in contracts/token-vesting/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

contracts/token-vesting/src/contract.rs#L199-L207

Added lines #L199 - L207 were not covered by tests

fn claim(
deps: DepsMut,
Expand Down Expand Up @@ -234,7 +239,7 @@

let claimable_amount = vested_amount.checked_sub(claimed_amount)?;
if claimable_amount.is_zero() {
continue;

Check warning on line 242 in contracts/token-vesting/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

contracts/token-vesting/src/contract.rs#L242

Added line #L242 was not covered by tests
}

account.claimed_amount = vested_amount;
Expand Down Expand Up @@ -320,7 +325,7 @@
amount,
vesting_schedule,
),
Err(_) => Err(StdError::generic_err("invalid cw20 hook message")),

Check warning on line 328 in contracts/token-vesting/src/contract.rs

View check run for this annotation

Codecov / codecov/patch

contracts/token-vesting/src/contract.rs#L328

Added line #L328 was not covered by tests
}
}

Expand Down
Loading