-
Notifications
You must be signed in to change notification settings - Fork 354
/
hook.rs
65 lines (56 loc) · 1.84 KB
/
hook.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use cosmwasm_schema::cw_serde;
use cosmwasm_std::{to_json_binary, Binary, CosmosMsg, StdResult, WasmMsg};
/// MemberDiff shows the old and new states for a given cw4 member
/// They cannot both be None.
/// old = None, new = Some -> Insert
/// old = Some, new = Some -> Update
/// old = Some, new = None -> Delete
#[cw_serde]
pub struct MemberDiff {
pub key: String,
pub old: Option<u64>,
pub new: Option<u64>,
}
impl MemberDiff {
pub fn new<T: Into<String>>(addr: T, old_weight: Option<u64>, new_weight: Option<u64>) -> Self {
MemberDiff {
key: addr.into(),
old: old_weight,
new: new_weight,
}
}
}
/// MemberChangedHookMsg should be de/serialized under `MemberChangedHook()` variant in a ExecuteMsg.
/// This contains a list of all diffs on the given transaction.
#[cw_serde]
pub struct MemberChangedHookMsg {
pub diffs: Vec<MemberDiff>,
}
impl MemberChangedHookMsg {
pub fn one(diff: MemberDiff) -> Self {
MemberChangedHookMsg { diffs: vec![diff] }
}
pub fn new(diffs: Vec<MemberDiff>) -> Self {
MemberChangedHookMsg { diffs }
}
/// serializes the message
pub fn into_json_binary(self) -> StdResult<Binary> {
let msg = MemberChangedExecuteMsg::MemberChangedHook(self);
to_json_binary(&msg)
}
/// creates a cosmos_msg sending this struct to the named contract
pub fn into_cosmos_msg<T: Into<String>>(self, contract_addr: T) -> StdResult<CosmosMsg> {
let msg = self.into_json_binary()?;
let execute = WasmMsg::Execute {
contract_addr: contract_addr.into(),
msg,
funds: vec![],
};
Ok(execute.into())
}
}
// This is just a helper to properly serialize the above message
#[cw_serde]
enum MemberChangedExecuteMsg {
MemberChangedHook(MemberChangedHookMsg),
}