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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
use crate::store::contract_state::{get_contract_state_v1, set_contract_state_v1, CONTRACT_TYPE};
use crate::types::error::ContractError;
use crate::util::validation_utils::check_funds_are_empty;
use cosmwasm_std::{DepsMut, Env, MessageInfo, Response};
use result_extensions::ResultExtensions;

/// Invoked via the contract's execute functionality.  This function will only accept the request if
/// the sender is the registered contract admin in the [contract state](crate::store::contract_state::ContractStateV1).
/// The function sets a new collection of attribute names required when an account deposits their
/// deposit denom into the contract via the [fund_trading](crate::execute::fund_trading::fund_trading)
/// execution route.
///
/// # Parameters
/// * `deps` A dependencies object provided by the cosmwasm framework.  Allows access to useful
/// resources like contract internal storage and a querier to retrieve blockchain objects.
/// * `env` An environment object provided by the cosmwasm framework.  Describes the contract's
/// details, as well as blockchain information at the time of the transaction.
/// * `info` A message information object provided by the cosmwasm framework.  Describes the sender
/// of the instantiation message, as well as the funds provided as an amount during the transaction.
/// * `attributes` The new attributes that will be set in the contract state's
/// [required_deposit_attributes](crate::store::contract_state::ContractStateV1#required_deposit_attributes)
/// property upon successful execution.
pub fn admin_update_deposit_required_attributes(
    deps: DepsMut,
    env: Env,
    info: MessageInfo,
    attributes: Vec<String>,
) -> Result<Response, ContractError> {
    check_funds_are_empty(&info)?;
    let mut contract_state = get_contract_state_v1(deps.storage)?;
    if info.sender != contract_state.admin {
        return ContractError::NotAuthorizedError {
            message: "only the contract admin may update attributes".to_string(),
        }
        .to_err();
    }
    let previous_attributes = contract_state.required_deposit_attributes.clone();
    contract_state.required_deposit_attributes = attributes;
    set_contract_state_v1(deps.storage, &contract_state)?;
    Response::new()
        .add_attribute("action", "admin_update_deposit_required_attributes")
        .add_attribute("contract_address", env.contract.address.as_str())
        .add_attribute("contract_type", CONTRACT_TYPE)
        .add_attribute("contract_name", &contract_state.contract_name)
        .add_attribute(
            "previous_attributes",
            format!("[{}]", previous_attributes.join(",").as_str()),
        )
        .add_attribute(
            "new_attributes",
            format!(
                "[{}]",
                contract_state
                    .required_deposit_attributes
                    .join(",")
                    .as_str()
            ),
        )
        .to_ok()
}

#[cfg(test)]
mod tests {
    use crate::execute::admin_update_deposit_required_attributes::admin_update_deposit_required_attributes;
    use crate::store::contract_state::CONTRACT_TYPE;
    use crate::test::attribute_extractor::AttributeExtractor;
    use crate::test::test_constants::{DEFAULT_ADMIN, DEFAULT_CONTRACT_NAME};
    use crate::test::test_instantiate::test_instantiate_with_msg;
    use crate::types::error::ContractError;
    use crate::types::msg::InstantiateMsg;
    use cosmwasm_std::testing::{message_info, mock_env, MOCK_CONTRACT_ADDR};
    use cosmwasm_std::{coins, Addr};
    use provwasm_mocks::mock_provenance_dependencies;

    #[test]
    fn provided_funds_should_cause_an_error() {
        let mut deps = mock_provenance_dependencies();
        let error = admin_update_deposit_required_attributes(
            deps.as_mut(),
            mock_env(),
            message_info(
                &Addr::unchecked(DEFAULT_ADMIN),
                &coins(400, "fourhundredcoins"),
            ),
            vec![],
        )
        .expect_err("an error should occur when funds are provided");
        assert!(
            matches!(&error, ContractError::InvalidFundsError { .. }),
            "unexpected error encountered: {error:?}",
        );
    }

    #[test]
    fn missing_contract_state_should_cause_an_error() {
        let mut deps = mock_provenance_dependencies();
        let error = admin_update_deposit_required_attributes(
            deps.as_mut(),
            mock_env(),
            message_info(&Addr::unchecked(DEFAULT_ADMIN), &[]),
            vec![],
        )
        .expect_err("an error should occur when the contract state is missing");
        assert!(
            matches!(&error, ContractError::StorageError { .. }),
            "unexpected error encountered: {error:?}",
        );
    }

    #[test]
    fn successful_input_should_derive_a_response_with_both_previous_and_new_values() {
        do_successful_attribute_test(
            "Both previous and new values populated",
            vec!["prevA".to_string(), "prevB".to_string()],
            vec!["new".to_string()],
            "[prevA,prevB]",
            "[new]",
        );
    }

    #[test]
    fn successful_input_should_derive_a_response_with_missing_previous_values() {
        do_successful_attribute_test(
            "Missing previous values",
            vec![],
            vec!["new-value".to_string()],
            "[]",
            "[new-value]",
        );
    }

    #[test]
    fn successful_input_should_derive_a_response_with_missing_new_values() {
        do_successful_attribute_test(
            "Missing new values",
            vec!["old-value".to_string()],
            vec![],
            "[old-value]",
            "[]",
        );
    }

    fn do_successful_attribute_test<S1: Into<String>, S2: Into<String>, S3: Into<String>>(
        test_name: S1,
        previous_attributes: Vec<String>,
        new_attributes: Vec<String>,
        expected_previous_attributes_attr_value: S2,
        expected_new_attributes_attr_value: S3,
    ) {
        let test_name = test_name.into();
        let mut deps = mock_provenance_dependencies();
        test_instantiate_with_msg(
            deps.as_mut(),
            InstantiateMsg {
                required_deposit_attributes: previous_attributes.to_vec(),
                ..InstantiateMsg::default()
            },
        );
        let response = admin_update_deposit_required_attributes(
            deps.as_mut(),
            mock_env(),
            message_info(&Addr::unchecked(DEFAULT_ADMIN), &[]),
            new_attributes,
        )
        .unwrap_or_else(|_| {
            panic!(
                "{}: proper input on an instantiated contract should derive a successful response",
                test_name
            )
        });
        assert!(
            response.messages.is_empty(),
            "{}: no messages should be emitted in the response",
            test_name,
        );
        assert_eq!(
            6,
            response.attributes.len(),
            "{}: six attributes should be emitted in the response",
            test_name,
        );
        response.assert_attribute_with_message_prefix(
            "action",
            "admin_update_deposit_required_attributes",
            &test_name,
        );
        response.assert_attribute_with_message_prefix(
            "contract_address",
            MOCK_CONTRACT_ADDR,
            &test_name,
        );
        response.assert_attribute_with_message_prefix("contract_type", CONTRACT_TYPE, &test_name);
        response.assert_attribute_with_message_prefix(
            "contract_name",
            DEFAULT_CONTRACT_NAME,
            &test_name,
        );
        response.assert_attribute_with_message_prefix(
            "previous_attributes",
            expected_previous_attributes_attr_value.into(),
            &test_name,
        );
        response.assert_attribute_with_message_prefix(
            "new_attributes",
            expected_new_attributes_attr_value.into(),
            &test_name,
        );
    }
}