Skip to content

Commit

Permalink
Merge pull request #13 from cybercongress/v3-update-coefficients
Browse files Browse the repository at this point in the history
Add update coefficients
  • Loading branch information
cyborgshead authored Mar 4, 2024
2 parents 9416ad8 + c30d6ba commit d382260
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 9 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.

2 changes: 1 addition & 1 deletion contracts/cw-cyber-gift/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cw-cyber-gift"
version = "2.0.0"
version = "3.0.0"
authors = ["CyberHead"]
edition = "2018"

Expand Down
11 changes: 5 additions & 6 deletions contracts/cw-cyber-gift/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use cw2::{get_contract_version, set_contract_version};
use crate::error::ContractError;
use crate::msg::{ExecuteMsg, InstantiateMsg, QueryMsg};
use crate::state::{Config, CONFIG, RELEASES_STATS, State, STATE};
use crate::execute::{execute_claim, execute_execute, execute_register_merkle_root, execute_release, execute_update_owner, execute_update_target, execute_update_treasury};
use crate::execute::{execute_claim, execute_execute, execute_register_merkle_root, execute_release, execute_update_coefficients, execute_update_owner, execute_update_target, execute_update_treasury};
use crate::query::{query_all_release_stage_states, query_claim, query_config, query_is_claimed, query_merkle_root, query_release_stage_state, query_release_state, query_state};
use cyber_std::CyberMsgWrapper;
use semver::Version;
Expand All @@ -15,7 +15,7 @@ type Response = cosmwasm_std::Response<CyberMsgWrapper>;

// Version info, for migration info
const CONTRACT_NAME: &str = "cyber-gift";
const CONTRACT_VERSION: &str = "2.0.0";
const CONTRACT_VERSION: &str = "3.0.0";

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn instantiate(
Expand Down Expand Up @@ -73,6 +73,9 @@ pub fn execute(
ExecuteMsg::UpdateTarget { new_target } => {
execute_update_target(deps, env, info, new_target)
}
ExecuteMsg::UpdateCoefficients { new_coefficient_up, new_coefficient_down } => {
execute_update_coefficients(deps, env, info, new_coefficient_up, new_coefficient_down)
}
ExecuteMsg::RegisterMerkleRoot { merkle_root } => {
execute_register_merkle_root(deps, env, info, merkle_root)
}
Expand Down Expand Up @@ -126,9 +129,5 @@ pub fn migrate(
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;
}

for i in 0..100 {
RELEASES_STATS.save(deps.storage, i as u8, &(0 as u32))?;
}

Ok(Response::new())
}
29 changes: 29 additions & 0 deletions contracts/cw-cyber-gift/src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,35 @@ pub fn execute_update_target(
]))
}

pub fn execute_update_coefficients(
deps: DepsMut,
_env: Env,
info: MessageInfo,
new_coefficient_up: Uint128,
new_coefficient_down: Uint128,
) -> Result<Response, ContractError> {
let cfg = CONFIG.load(deps.storage)?;
let owner = cfg.clone().owner.ok_or(ContractError::Unauthorized {})?;
if info.sender != owner {
return Err(ContractError::Unauthorized {});
}

CONFIG.update(deps.storage, |mut cfg| -> StdResult<_> {
cfg.coefficient_up = new_coefficient_up;
cfg.coefficient_down = new_coefficient_down;
Ok(cfg)
})?;
let config = CONFIG.load(deps.storage)?;
let mut state = STATE.load(deps.storage)?;
update_coefficient(deps.storage, Uint128::zero(), &config, &mut state)?;

Ok(Response::new().add_attributes(vec![
attr("action", "update_coefficients"),
attr("new_coefficient_up", new_coefficient_up.to_string()),
attr("new_coefficient_down", new_coefficient_down.to_string()),
]))
}

pub fn execute_register_merkle_root(
deps: DepsMut,
_env: Env,
Expand Down
4 changes: 4 additions & 0 deletions contracts/cw-cyber-gift/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ pub enum ExecuteMsg {
UpdateTarget {
new_target: Uint64,
},
UpdateCoefficients {
new_coefficient_up: Uint128,
new_coefficient_down: Uint128,
},
RegisterMerkleRoot {
/// MerkleRoot is hex-encoded merkle root.
merkle_root: String,
Expand Down
28 changes: 27 additions & 1 deletion contracts/cw-cyber-gift/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod tests {
use std::borrow::BorrowMut;
use cosmwasm_std::testing::{mock_dependencies, mock_env, mock_info};
use cosmwasm_std::{attr, from_binary, Binary, Coin, Uint128, Uint64, Empty, Addr, coins};
use crate::msg::{AllReleaseStageStateResponse, ConfigResponse, ExecuteMsg, InstantiateMsg, MerkleRootResponse, QueryMsg};
use crate::msg::{AllReleaseStageStateResponse, ConfigResponse, ExecuteMsg, InstantiateMsg, MerkleRootResponse, QueryMsg, StateResponse};
use crate::ContractError;
use crate::contract::{execute, instantiate, query};
use cw_multi_test::{next_block, Contract, ContractWrapper, Executor};
Expand All @@ -12,6 +12,7 @@ mod tests {
use cyber_std_test::CyberApp;
use csv;
use serde::Deserialize;
use crate::state::Config;

const NATIVE_TOKEN: &str = "boot";
const OWNER: &str = "owner";
Expand Down Expand Up @@ -328,9 +329,34 @@ mod tests {
claim_and_release(app.borrow_mut(), &gift_addr, &treasury_addr, &data, 0, 5, 0, 5);
claim_and_release(app.borrow_mut(), &gift_addr, &treasury_addr, &data, 5, 10, 0, 10);
claim_and_release(app.borrow_mut(), &gift_addr, &treasury_addr, &data, 10, 15, 0, 15);
let _ = app.execute_contract(
Addr::unchecked(OWNER),
gift_addr.clone(),
&ExecuteMsg::UpdateCoefficients {
new_coefficient_up: Uint128::new(10),
new_coefficient_down: Uint128::new(5),
}, &[],
);
claim_and_release(app.borrow_mut(), &gift_addr, &treasury_addr, &data, 15, 19, 0, 19);
claim_and_release(app.borrow_mut(), &gift_addr, &treasury_addr, &data, 19, 20, 0, 20);

let res: ConfigResponse = app.wrap().query_wasm_smart(&gift_addr, &QueryMsg::Config {}).unwrap();
println!("Config - {:?}", res);
let res: StateResponse = app.wrap().query_wasm_smart(&gift_addr, &QueryMsg::State {}).unwrap();
println!("State - {:?}", res);
let _ = app.execute_contract(
Addr::unchecked(OWNER),
gift_addr.clone(),
&ExecuteMsg::UpdateCoefficients {
new_coefficient_up: Uint128::new(5),
new_coefficient_down: Uint128::new(2),
}, &[],
);
let res: ConfigResponse = app.wrap().query_wasm_smart(&gift_addr, &QueryMsg::Config {}).unwrap();
println!("Config - {:?}", res);
let res: StateResponse = app.wrap().query_wasm_smart(&gift_addr, &QueryMsg::State {}).unwrap();
println!("State - {:?}", res);

for i in 0..20 {
println!("PASSPORT #{:?} BAL- {:?}", i, app.wrap().query_balance(&Addr::unchecked(data[i].bostrom_address.clone()), "boot").unwrap());
}
Expand Down

0 comments on commit d382260

Please sign in to comment.