From 4cf78a902a290f81164b2ae4d488ef396e29db74 Mon Sep 17 00:00:00 2001 From: Kevin Yang <5478483+k-yang@users.noreply.github.com> Date: Mon, 11 Mar 2024 13:28:39 -0500 Subject: [PATCH 1/3] refactor: remove recipient --- .../core-token-vesting-v2/src/contract.rs | 39 ++++++------------- contracts/core-token-vesting-v2/src/msg.rs | 2 - .../core-token-vesting-v2/src/testing.rs | 4 -- 3 files changed, 12 insertions(+), 33 deletions(-) diff --git a/contracts/core-token-vesting-v2/src/contract.rs b/contracts/core-token-vesting-v2/src/contract.rs index d51fc95..155f301 100644 --- a/contracts/core-token-vesting-v2/src/contract.rs +++ b/contracts/core-token-vesting-v2/src/contract.rs @@ -77,13 +77,8 @@ pub fn execute( ExecuteMsg::DeregisterVestingAccounts { addresses } => { deregister_vesting_accounts(deps, env, info, addresses) } - ExecuteMsg::Claim { - denoms: _denoms, - recipient, - } => claim(deps, env, info, recipient), - ExecuteMsg::Withdraw { amount, recipient } => { - withdraw(deps, env, info, amount, recipient) - } + ExecuteMsg::Claim { denoms: _denoms } => claim(deps, env, info), + ExecuteMsg::Withdraw { amount } => withdraw(deps, env, info, amount), } } @@ -95,15 +90,15 @@ pub fn withdraw( _env: Env, info: MessageInfo, amount: Uint128, - recipient: String, ) -> Result { let whitelist = WHITELIST.load(deps.storage)?; let mut unallocated_amount = UNALLOCATED_AMOUNT.load(deps.storage)?; let denom = DENOM.load(deps.storage)?; - if !whitelist.is_admin(info.sender) { + if !whitelist.is_admin(&info.sender) { return Err(StdError::generic_err("Unauthorized").into()); } + let recipient = info.sender.as_str(); let amount_max = min(amount, unallocated_amount); if amount_max.is_zero() { @@ -113,13 +108,10 @@ pub fn withdraw( unallocated_amount -= amount_max; UNALLOCATED_AMOUNT.save(deps.storage, &unallocated_amount)?; - // validate recipient address - deps.api.addr_validate(&recipient)?; - Ok(Response::new() - .add_messages(vec![build_send_msg(&denom, amount_max, &recipient)]) + .add_messages(vec![build_send_msg(&denom, amount_max, recipient)]) .add_attribute("action", "withdraw") - .add_attribute("recipient", &recipient) + .add_attribute("recipient", recipient) .add_attribute("amount", amount_max.to_string()) .add_attribute("unallocated_amount", unallocated_amount.to_string())) } @@ -320,12 +312,7 @@ fn deregister_vesting_account( // transfer already vested amount to the user let claimable_amount = vested_amount.checked_sub(claimed_amount)?; - send_if_amount_is_not_zero( - messages, - claimable_amount, - &denom, - address, - )?; + send_if_amount_is_not_zero(messages, claimable_amount, &denom, address)?; // transfer left vesting amount to the admin let left_vesting_amount = @@ -366,16 +353,14 @@ fn claim( deps: DepsMut, env: Env, info: MessageInfo, - recipient: Option, ) -> Result { - let sender = &info.sender; - let recipient = &recipient.unwrap_or_else(|| sender.to_string()); + let recipient = info.sender.as_str(); let denom = DENOM.load(deps.storage)?; let mut attrs: Vec = vec![]; // vesting_account existence check - let account = VESTING_ACCOUNTS.may_load(deps.storage, sender.as_str())?; + let account = VESTING_ACCOUNTS.may_load(deps.storage, recipient)?; if account.is_none() { return Err(StdError::generic_err(format!( "vesting entry is not found for denom {}", @@ -395,9 +380,9 @@ fn claim( account.claimed_amount = vested_amount; if account.claimed_amount == account.vesting_amount { - VESTING_ACCOUNTS.remove(deps.storage, sender.as_str()); + VESTING_ACCOUNTS.remove(deps.storage, recipient); } else { - VESTING_ACCOUNTS.save(deps.storage, sender.as_str(), &account)?; + VESTING_ACCOUNTS.save(deps.storage, recipient, &account)?; } attrs.extend( @@ -412,7 +397,7 @@ fn claim( Ok(Response::new() .add_messages(vec![build_send_msg(&denom, claimable_amount, recipient)]) - .add_attributes(vec![("action", "claim"), ("address", sender.as_str())]) + .add_attributes(vec![("action", "claim"), ("address", recipient)]) .add_attributes(attrs)) } diff --git a/contracts/core-token-vesting-v2/src/msg.rs b/contracts/core-token-vesting-v2/src/msg.rs index 25f2255..6671cd7 100644 --- a/contracts/core-token-vesting-v2/src/msg.rs +++ b/contracts/core-token-vesting-v2/src/msg.rs @@ -35,13 +35,11 @@ pub enum ExecuteMsg { /// Claim is an operation that allows one to claim vested tokens. Claim { denoms: Vec, - recipient: Option, }, // Withdraw allows the admin to withdraw the funds from the contract Withdraw { amount: Uint128, - recipient: String, }, } diff --git a/contracts/core-token-vesting-v2/src/testing.rs b/contracts/core-token-vesting-v2/src/testing.rs index def135f..24eb330 100644 --- a/contracts/core-token-vesting-v2/src/testing.rs +++ b/contracts/core-token-vesting-v2/src/testing.rs @@ -498,7 +498,6 @@ fn test_withdraw() -> TestResult { // unauthorized sender let msg = ExecuteMsg::Withdraw { - recipient: "addr0000".to_string(), amount: Uint128::new(1000), }; require_error( @@ -511,7 +510,6 @@ fn test_withdraw() -> TestResult { // withdraw more than unallocated let msg = ExecuteMsg::Withdraw { - recipient: "addr0000".to_string(), amount: Uint128::new(1001), }; let res = @@ -541,7 +539,6 @@ fn test_withdraw() -> TestResult { // withdraw but there's no more unallocated let msg = ExecuteMsg::Withdraw { - recipient: "addr0000".to_string(), amount: Uint128::new(1), }; require_error( @@ -797,7 +794,6 @@ fn claim_native() -> TestResult { // valid claim let info = mock_info("addr0001", &[]); let msg = ExecuteMsg::Claim { - recipient: None, denoms: vec![], }; From a9f5793312272f1e964a90bcf34c562fc37ff48a Mon Sep 17 00:00:00 2001 From: Kevin Yang <5478483+k-yang@users.noreply.github.com> Date: Mon, 11 Mar 2024 13:30:55 -0500 Subject: [PATCH 2/3] chore: bump package version --- Cargo.lock | 2 +- contracts/core-token-vesting-v2/Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6ff94ce..39e2a96 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -371,7 +371,7 @@ checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f" [[package]] name = "core-token-vesting-v2" -version = "2.0.0" +version = "2.1.0" dependencies = [ "anyhow", "cosmwasm-schema", diff --git a/contracts/core-token-vesting-v2/Cargo.toml b/contracts/core-token-vesting-v2/Cargo.toml index 02b11cb..3cd727c 100644 --- a/contracts/core-token-vesting-v2/Cargo.toml +++ b/contracts/core-token-vesting-v2/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "core-token-vesting-v2" -version = "2.0.0" +version = "2.1.0" edition = "2021" description = "Token vesting contract v2" From cde2198b0f38eb8b5a7f1d09ac951d22e71c3720 Mon Sep 17 00:00:00 2001 From: Kevin Yang <5478483+k-yang@users.noreply.github.com> Date: Mon, 11 Mar 2024 13:55:38 -0500 Subject: [PATCH 3/3] refactor: remove denom field in Claim msg --- contracts/core-token-vesting-v2/src/contract.rs | 4 ++-- contracts/core-token-vesting-v2/src/msg.rs | 6 ++---- contracts/core-token-vesting-v2/src/testing.rs | 4 +--- 3 files changed, 5 insertions(+), 9 deletions(-) diff --git a/contracts/core-token-vesting-v2/src/contract.rs b/contracts/core-token-vesting-v2/src/contract.rs index 155f301..32ba435 100644 --- a/contracts/core-token-vesting-v2/src/contract.rs +++ b/contracts/core-token-vesting-v2/src/contract.rs @@ -2,7 +2,7 @@ use cosmwasm_std::entry_point; use cosmwasm_std::{ to_json_binary, Attribute, BankMsg, Binary, Coin, CosmosMsg, Deps, DepsMut, - Env, MessageInfo, Response, StdError, StdResult, Storage, SubMsg, Timestamp, + Env, MessageInfo, Response, StdError, StdResult, Storage, Timestamp, Uint128, }; use std::cmp::min; @@ -77,7 +77,7 @@ pub fn execute( ExecuteMsg::DeregisterVestingAccounts { addresses } => { deregister_vesting_accounts(deps, env, info, addresses) } - ExecuteMsg::Claim { denoms: _denoms } => claim(deps, env, info), + ExecuteMsg::Claim { } => claim(deps, env, info), ExecuteMsg::Withdraw { amount } => withdraw(deps, env, info, amount), } } diff --git a/contracts/core-token-vesting-v2/src/msg.rs b/contracts/core-token-vesting-v2/src/msg.rs index 6671cd7..6595111 100644 --- a/contracts/core-token-vesting-v2/src/msg.rs +++ b/contracts/core-token-vesting-v2/src/msg.rs @@ -33,9 +33,7 @@ pub enum ExecuteMsg { }, /// Claim is an operation that allows one to claim vested tokens. - Claim { - denoms: Vec, - }, + Claim {}, // Withdraw allows the admin to withdraw the funds from the contract Withdraw { @@ -91,7 +89,7 @@ pub enum QueryMsg { }, VestingAccounts { address: Vec, - } + }, } #[cw_serde] diff --git a/contracts/core-token-vesting-v2/src/testing.rs b/contracts/core-token-vesting-v2/src/testing.rs index 24eb330..e73585d 100644 --- a/contracts/core-token-vesting-v2/src/testing.rs +++ b/contracts/core-token-vesting-v2/src/testing.rs @@ -793,9 +793,7 @@ fn claim_native() -> TestResult { // valid claim let info = mock_info("addr0001", &[]); - let msg = ExecuteMsg::Claim { - denoms: vec![], - }; + let msg = ExecuteMsg::Claim {}; let res = execute(deps.as_mut(), env.clone(), info.clone(), msg.clone())?; assert_eq!(