Skip to content

Commit

Permalink
refactor(rewards): remove Store and Contract abstractions (axelar…
Browse files Browse the repository at this point in the history
…network#324)

* remove contract and store abstractions

* use `load_rewards_pool_or_new`
  • Loading branch information
eguajardo authored Mar 27, 2024
1 parent a68eb5b commit 5419c6a
Show file tree
Hide file tree
Showing 4 changed files with 690 additions and 727 deletions.
34 changes: 18 additions & 16 deletions contracts/rewards/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
use itertools::Itertools;

use axelar_wasm_std::nonempty;
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{
to_json_binary, BankMsg, Binary, Coin, Deps, DepsMut, Env, MessageInfo, Response,
};
use error_stack::ResultExt;
use itertools::Itertools;

use crate::{
contract::execute::Contract,
error::ContractError,
msg::{ExecuteMsg, InstantiateMsg, QueryMsg},
state::{Config, Epoch, ParamsSnapshot, PoolId, CONFIG, PARAMS},
state::{self, Config, Epoch, ParamsSnapshot, PoolId, CONFIG, PARAMS},
};

mod execute;
Expand Down Expand Up @@ -67,25 +65,30 @@ pub fn execute(
chain_name,
contract: info.sender.clone(),
};
Contract::new(deps)
.record_participation(event_id, worker_address, pool_id, env.block.height)
.map_err(axelar_wasm_std::ContractError::from)?;
execute::record_participation(
deps.storage,
event_id,
worker_address,
pool_id,
env.block.height,
)
.map_err(axelar_wasm_std::ContractError::from)?;

Ok(Response::new())
}
ExecuteMsg::AddRewards { pool_id } => {
deps.api.addr_validate(pool_id.contract.as_str())?;

let mut contract = Contract::new(deps);
let amount = info
.funds
.iter()
.find(|coin| coin.denom == contract.config.rewards_denom)
.find(|coin| coin.denom == state::load_config(deps.storage).rewards_denom)
.filter(|_| info.funds.len() == 1) // filter here to make sure expected denom is the only one attached to this message, and other funds aren't silently swallowed
.ok_or(ContractError::WrongDenom)?
.amount;

contract.add_rewards(
execute::add_rewards(
deps.storage,
pool_id,
nonempty::Uint128::try_from(amount).change_context(ContractError::ZeroRewards)?,
)?;
Expand All @@ -98,26 +101,25 @@ pub fn execute(
} => {
deps.api.addr_validate(pool_id.contract.as_str())?;

let mut contract = Contract::new(deps);
let rewards = contract
.distribute_rewards(pool_id, env.block.height, epoch_count)
.map_err(axelar_wasm_std::ContractError::from)?;
let rewards =
execute::distribute_rewards(deps.storage, pool_id, env.block.height, epoch_count)
.map_err(axelar_wasm_std::ContractError::from)?;

let msgs = rewards
.into_iter()
.sorted()
.map(|(addr, amount)| BankMsg::Send {
to_address: addr.into(),
amount: vec![Coin {
denom: contract.config.rewards_denom.clone(),
denom: state::load_config(deps.storage).rewards_denom.clone(),
amount,
}],
});

Ok(Response::new().add_messages(msgs))
}
ExecuteMsg::UpdateParams { params } => {
Contract::new(deps).update_params(params, env.block.height, info.sender)?;
execute::update_params(deps.storage, params, env.block.height, info.sender)?;

Ok(Response::new())
}
Expand Down
Loading

0 comments on commit 5419c6a

Please sign in to comment.