Skip to content

Commit

Permalink
add fee_collected to config state
Browse files Browse the repository at this point in the history
  • Loading branch information
codeninja819 committed Dec 6, 2023
1 parent a693e2c commit 4abc0c9
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 5 deletions.
2 changes: 1 addition & 1 deletion artifacts/checksums.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
16183aeac62b929d59d80c7028f19cd0b6702cfbe27a3aa928bc0b6e2b102aa5 staking.wasm
69b28c2756b01d10dcb495e77a1068f4498f18ec5388e55662717f3ea5ca9c10 staking.wasm
2 changes: 1 addition & 1 deletion artifacts/checksums_intermediate.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
f3d5ad956e02daa0ed5a8df7dac6ac9134921918bdca5192755499618932fa25 ./target/wasm32-unknown-unknown/release/staking.wasm
c8d4e09fa9ce2604f927692128f5c95b70a272c75c86380fcbe6e23955a0eb55 ./target/wasm32-unknown-unknown/release/staking.wasm
Binary file modified artifacts/staking.wasm
Binary file not shown.
1 change: 1 addition & 0 deletions src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub fn instantiate(
let config_state = Config {
owner: info.clone().sender.to_string(),
unstake_fee: msg.unstake_fee,
fee_collected: 0,
};
CONFIG.save(deps.storage, &config_state)?;
set_contract_version(deps.storage, "Injective CW721 Staking", "0.0.1")?;
Expand Down
3 changes: 3 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ pub enum ContractError {
#[error("Not enough reward pool")]
NotEnoughRewardPool {},

#[error("Not enough fee collected")]
NotEnoughFeeCollected {},

#[error("Not unstaked")]
NotUnstaked {},

Expand Down
19 changes: 16 additions & 3 deletions src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ pub fn withdraw_fee(
) -> Result<Response, ContractError> {
let store = deps.branch().storage;
check_contract_owner_only(info.clone(), store)?;
let mut config_state = CONFIG.load(store).unwrap();
let fee_amount = u128::from_str(&fee.clone().amount.to_string()).unwrap();
if fee_amount > config_state.fee_collected.clone() {
return Err(ContractError::NotEnoughFeeCollected {});
}
config_state.fee_collected = config_state.fee_collected.clone() - fee_amount;
CONFIG.save(deps.storage, &config_state)?;

let transfer_msg = BankMsg::Send {
to_address: info.sender.to_string(),
amount: vec![fee.clone()],
Expand Down Expand Up @@ -183,7 +191,7 @@ pub fn unstake(
) -> Result<Response, ContractError> {
let owner = info.sender.to_string();
let store = deps.branch().storage;
let config_state = CONFIG.load(store)?;
let mut config_state = CONFIG.load(store)?;
let mut stakings_state = STAKINGS.may_load(store, owner.clone())?.unwrap();
let staking_info = stakings_state[index as usize].clone();
let mut staking = &mut stakings_state[index as usize];
Expand All @@ -192,9 +200,14 @@ pub fn unstake(
return Err(ContractError::AlreadyUnstaked {});
}
if env.block.time.seconds() - staking_info.start_timestamp.clone().seconds() < collection.cycle
&& info.funds != vec![config_state.unstake_fee]
{
return Err(ContractError::NotEnoughUnstakeFee {});
if info.funds == vec![config_state.unstake_fee.clone()] {
config_state.fee_collected = config_state.fee_collected
+ u128::from_str(&config_state.unstake_fee.clone().amount.to_string()).unwrap();
CONFIG.save(store, &config_state)?;
} else {
return Err(ContractError::NotEnoughUnstakeFee {});
}
}
staking.end_timestamp = env.block.time;
let transfer_msg: CosmosMsg = CosmosMsg::Wasm(WasmMsg::Execute {
Expand Down
1 change: 1 addition & 0 deletions src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pub enum QueryMsg {
pub struct ConfigResponse {
pub owner: String,
pub unstake_fee: Coin,
pub fee_collected: u128,
}

#[cw_serde]
Expand Down
1 change: 1 addition & 0 deletions src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub fn get_config(deps: Deps) -> Result<QueryResponse, ContractError> {
Ok(to_json_binary(&ConfigResponse {
owner: config_state.clone().owner,
unstake_fee: config_state.clone().unstake_fee,
fee_collected: config_state.clone().fee_collected,
})
.unwrap())
}
Expand Down
1 change: 1 addition & 0 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::str::FromStr;
pub struct Config {
pub owner: String,
pub unstake_fee: Coin,
pub fee_collected: u128,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
Expand Down

0 comments on commit 4abc0c9

Please sign in to comment.