Skip to content

Commit

Permalink
fix(staking): added min liquidity amount (astroport-fi#45)
Browse files Browse the repository at this point in the history
* added min liquidity amount
  • Loading branch information
P-Yevhenii authored Apr 13, 2023
1 parent e83a620 commit cc302a3
Show file tree
Hide file tree
Showing 7 changed files with 231 additions and 72 deletions.
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.

6 changes: 3 additions & 3 deletions contracts/pair_astro_xastro/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ fn test_pair_swap() {
&[],
)
.unwrap();
assert_user_balance(&mut router, &contracts.xastro_instance, &user1, 10_000u64);
assert_user_balance(&mut router, &contracts.xastro_instance, &user1, 9_000u64);

router
.execute_contract(
Expand Down Expand Up @@ -537,7 +537,7 @@ fn test_pair_swap() {
contracts.xastro_instance.clone(),
&Cw20ExecuteMsg::Send {
contract: contracts.pair_instance.clone().to_string(),
amount: Uint128::from(10_000u64),
amount: Uint128::from(9_000u64),
msg: to_binary(&Cw20HookMsg::Swap {
ask_asset_info: None,
belief_price: None,
Expand All @@ -549,7 +549,7 @@ fn test_pair_swap() {
&[],
)
.unwrap();
assert_user_balance(&mut router, &contracts.astro_instance, &user1, 10_000u64);
assert_user_balance(&mut router, &contracts.astro_instance, &user1, 9_000u64);

router
.execute_contract(
Expand Down
6 changes: 3 additions & 3 deletions contracts/tokenomics/generator/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ fn test_boost_checkpoints_with_delegation() {
// Create short lock user1
helper_controller
.escrow_helper
.mint_xastro(&mut app, USER1, 100);
.mint_xastro(&mut app, USER1, 200);

helper_controller
.escrow_helper
Expand Down Expand Up @@ -491,7 +491,7 @@ fn test_boost_checkpoints() {
// Create short lock user1
helper_controller
.escrow_helper
.mint_xastro(&mut app, USER1, 100);
.mint_xastro(&mut app, USER1, 200);

helper_controller
.escrow_helper
Expand Down Expand Up @@ -3656,7 +3656,7 @@ fn test_proxy_generator_incorrect_virtual_amount() {
);
helper_controller
.escrow_helper
.mint_xastro(&mut app, USER1, 100);
.mint_xastro(&mut app, USER1, 200);
helper_controller
.escrow_helper
.create_lock(&mut app, USER1, WEEK * 3, 100f32)
Expand Down
2 changes: 1 addition & 1 deletion contracts/tokenomics/staking/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "astroport-staking"
version = "1.0.2"
version = "1.1.0"
authors = ["Astroport"]
edition = "2021"

Expand Down
74 changes: 57 additions & 17 deletions contracts/tokenomics/staking/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use cosmwasm_std::{
entry_point, from_binary, to_binary, Addr, Binary, CosmosMsg, Deps, DepsMut, Env, MessageInfo,
Reply, ReplyOn, Response, StdError, StdResult, SubMsg, Uint128, WasmMsg,
attr, entry_point, from_binary, to_binary, wasm_execute, Addr, Binary, CosmosMsg, Deps,
DepsMut, Env, MessageInfo, Reply, ReplyOn, Response, StdError, StdResult, SubMsg, Uint128,
WasmMsg,
};

use crate::error::ContractError;
Expand Down Expand Up @@ -28,6 +29,9 @@ const TOKEN_SYMBOL: &str = "xASTRO";
/// A `reply` call code ID used for sub-messages.
const INSTANTIATE_TOKEN_REPLY_ID: u64 = 1;

/// Minimum initial xastro share
pub(crate) const MINIMUM_STAKE_AMOUNT: Uint128 = Uint128::new(1_000);

/// Creates a new contract with the specified parameters in the [`InstantiateMsg`].
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn instantiate(
Expand Down Expand Up @@ -131,41 +135,72 @@ fn receive_cw20(
let config: Config = CONFIG.load(deps.storage)?;

let recipient = cw20_msg.sender;
let amount = cw20_msg.amount;
let mut amount = cw20_msg.amount;

let mut total_deposit = query_token_balance(
&deps.querier,
&config.astro_token_addr,
env.contract.address,
env.contract.address.clone(),
)?;
let total_shares = query_supply(&deps.querier, &config.xastro_token_addr)?;

match from_binary(&cw20_msg.msg)? {
Cw20HookMsg::Enter {} => {
let mut messages = vec![];
if info.sender != config.astro_token_addr {
return Err(ContractError::Unauthorized {});
}

// In a CW20 `send`, the total balance of the recipient is already increased.
// To properly calculate the total amount of ASTRO deposited in staking, we should subtract the user deposit from the pool
total_deposit -= amount;
let mint_amount: Uint128 = if total_shares.is_zero() || total_deposit.is_zero() {
amount = amount
.checked_sub(MINIMUM_STAKE_AMOUNT)
.map_err(|_| ContractError::MinimumStakeAmountError {})?;

// amount cannot become zero after minimum stake subtraction
if amount.is_zero() {
return Err(ContractError::MinimumStakeAmountError {});
}

messages.push(wasm_execute(
config.xastro_token_addr.clone(),
&Cw20ExecuteMsg::Mint {
recipient: env.contract.address.to_string(),
amount: MINIMUM_STAKE_AMOUNT,
},
vec![],
)?);

amount
} else {
amount
amount = amount
.checked_mul(total_shares)?
.checked_div(total_deposit)?
.checked_div(total_deposit)?;

if amount.is_zero() {
return Err(ContractError::StakeAmountTooSmall {});
}

amount
};

let res = Response::new().add_message(CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: config.xastro_token_addr.to_string(),
msg: to_binary(&Cw20ExecuteMsg::Mint {
recipient,
messages.push(wasm_execute(
config.xastro_token_addr,
&Cw20ExecuteMsg::Mint {
recipient: recipient.clone(),
amount: mint_amount,
})?,
funds: vec![],
}));
},
vec![],
)?);

Ok(res)
Ok(Response::new().add_messages(messages).add_attributes(vec![
attr("action", "enter"),
attr("recipient", recipient),
attr("astro_amount", cw20_msg.amount),
attr("xastro_amount", mint_amount),
]))
}
Cw20HookMsg::Leave {} => {
if info.sender != config.xastro_token_addr {
Expand All @@ -186,13 +221,18 @@ fn receive_cw20(
.add_message(CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: config.astro_token_addr.to_string(),
msg: to_binary(&Cw20ExecuteMsg::Transfer {
recipient,
recipient: recipient.clone(),
amount: what,
})?,
funds: vec![],
}));

Ok(res)
Ok(res.add_attributes(vec![
attr("action", "leave"),
attr("recipient", recipient),
attr("xastro_amount", cw20_msg.amount),
attr("astro_amount", what),
]))
}
}
}
Expand Down Expand Up @@ -238,7 +278,7 @@ pub fn migrate(deps: DepsMut, _env: Env, _msg: MigrateMsg) -> Result<Response, C

match contract_version.contract.as_ref() {
"astroport-staking" => match contract_version.version.as_ref() {
"1.0.0" | "1.0.1" => {}
"1.0.0" | "1.0.1" | "1.0.2" => {}
_ => return Err(ContractError::MigrationError {}),
},
_ => return Err(ContractError::MigrationError {}),
Expand Down
7 changes: 7 additions & 0 deletions contracts/tokenomics/staking/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::contract::MINIMUM_STAKE_AMOUNT;
use cosmwasm_std::{DivideByZeroError, OverflowError, StdError};
use thiserror::Error;

Expand All @@ -12,6 +13,12 @@ pub enum ContractError {

#[error("An error occurred during migration")]
MigrationError {},

#[error("Initial stake amount must be more than {}", MINIMUM_STAKE_AMOUNT)]
MinimumStakeAmountError {},

#[error("Insufficient amount of Stake")]
StakeAmountTooSmall {},
}

impl From<OverflowError> for ContractError {
Expand Down
Loading

0 comments on commit cc302a3

Please sign in to comment.