dao_hooks/
stake.rs

1use cosmwasm_schema::cw_serde;
2use cosmwasm_std::{to_json_binary, Addr, StdResult, Storage, SubMsg, Uint128, WasmMsg};
3use cw_hooks::Hooks;
4
5/// An enum representing staking hooks.
6#[cw_serde]
7pub enum StakeChangedHookMsg {
8    Stake { addr: Addr, amount: Uint128 },
9    Unstake { addr: Addr, amount: Uint128 },
10}
11
12/// Prepares StakeChangedHookMsg::Stake hook SubMsgs,
13/// containing the address and the amount staked.
14pub fn stake_hook_msgs(
15    hooks: Hooks,
16    storage: &dyn Storage,
17    addr: Addr,
18    amount: Uint128,
19) -> StdResult<Vec<SubMsg>> {
20    let msg = to_json_binary(&StakeChangedExecuteMsg::StakeChangeHook(
21        StakeChangedHookMsg::Stake { addr, amount },
22    ))?;
23    hooks.prepare_hooks(storage, |a| {
24        let execute = WasmMsg::Execute {
25            contract_addr: a.to_string(),
26            msg: msg.clone(),
27            funds: vec![],
28        };
29        Ok(SubMsg::new(execute))
30    })
31}
32
33/// Prepares StakeChangedHookMsg::Unstake hook SubMsgs,
34/// containing the address and the amount unstaked.
35pub fn unstake_hook_msgs(
36    hooks: Hooks,
37    storage: &dyn Storage,
38    addr: Addr,
39    amount: Uint128,
40) -> StdResult<Vec<SubMsg>> {
41    let msg = to_json_binary(&StakeChangedExecuteMsg::StakeChangeHook(
42        StakeChangedHookMsg::Unstake { addr, amount },
43    ))?;
44    hooks.prepare_hooks(storage, |a| {
45        let execute = WasmMsg::Execute {
46            contract_addr: a.to_string(),
47            msg: msg.clone(),
48            funds: vec![],
49        };
50        Ok(SubMsg::new(execute))
51    })
52}
53
54#[cw_serde]
55pub enum StakeChangedExecuteMsg {
56    StakeChangeHook(StakeChangedHookMsg),
57}