-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_asset_definition.rs
More file actions
304 lines (289 loc) · 12.7 KB
/
update_asset_definition.rs
File metadata and controls
304 lines (289 loc) · 12.7 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
use crate::core::error::ContractError;
use crate::core::msg::ExecuteMsg;
use crate::core::state::replace_asset_definition_v3;
use crate::core::types::asset_definition::AssetDefinitionV3;
use crate::util::aliases::{AssetResult, EntryPointResponse};
use crate::util::contract_helpers::{check_admin_only, check_funds_are_empty};
use crate::util::event_attributes::{EventAttributes, EventType};
use cosmwasm_std::{DepsMut, MessageInfo, Response};
use result_extensions::ResultExtensions;
/// A transformation of [ExecuteMsg::UpdateAssetDefinition](crate::core::msg::ExecuteMsg::UpdateAssetDefinition)
/// for ease of use in the underlying [update_asset_definition](self::update_asset_definition) function.
///
/// # Parameters
///
/// * `asset_definition` The asset definition instance to update. Must have an [asset_type](crate::core::types::asset_definition::AssetDefinitionV3::asset_type)
/// property that matches an existing asset definition in contract storage.
#[derive(Clone, PartialEq, Eq)]
pub struct UpdateAssetDefinitionV1 {
pub asset_definition: AssetDefinitionV3,
}
impl UpdateAssetDefinitionV1 {
/// Constructs a new instance of this struct.
///
/// # Parameters
///
/// * `asset_definition` The asset definition instance to update. Must have an [asset_type](crate::core::types::asset_definition::AssetDefinitionV3::asset_type)
/// property that matches an existing asset definition in contract storage.
pub fn new(asset_definition: AssetDefinitionV3) -> Self {
UpdateAssetDefinitionV1 { asset_definition }
}
/// Attempts to create an instance of this struct from a provided execute msg. If the provided
/// value is not of the [UpdateAssetDefinition](crate::core::msg::ExecuteMsg::UpdateAssetDefinition)
/// variant, then an [InvalidMessageType](crate::core::error::ContractError::InvalidMessageType)
/// error will be returned.
///
/// # Parameters
///
/// * `msg` An execute msg provided by the contract's [execute](crate::contract::execute) function.
pub fn from_execute_msg(msg: ExecuteMsg) -> AssetResult<UpdateAssetDefinitionV1> {
match msg {
ExecuteMsg::UpdateAssetDefinition { asset_definition } => Self {
asset_definition: asset_definition.into_asset_definition(),
}
.to_ok(),
_ => ContractError::InvalidMessageType {
expected_message_type: "ExecuteMsg::UpdateAssetDefinition".to_string(),
}
.to_err(),
}
}
}
/// The function used by [execute](crate::contract::execute) when an [ExecuteMsg::UpdateAssetDefinition](crate::core::msg::ExecuteMsg::UpdateAssetDefinition)
/// message is provided. Attempts to replace an existing [AssetDefinitionV3](crate::core::types::asset_definition::AssetDefinitionV3)
/// value based on a matching [asset_type](crate::core::types::asset_definition::AssetDefinitionV3::asset_type)
/// property. If no matching type is present, the request will be rejected.
///
/// # 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.
/// * `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.
/// * `msg` An instance of the update asset definition v1 struct, provided by conversion from an
/// [ExecuteMsg](crate::core::msg::ExecuteMsg).
pub fn update_asset_definition(
deps: DepsMut,
info: MessageInfo,
msg: UpdateAssetDefinitionV1,
) -> EntryPointResponse {
check_admin_only(&deps.as_ref(), &info)?;
check_funds_are_empty(&info)?;
// Overwrite the existing asset definition with the new one
replace_asset_definition_v3(deps.storage, &msg.asset_definition)?;
Response::new()
.add_attributes(
EventAttributes::new(EventType::UpdateAssetDefinition)
.set_asset_type(&msg.asset_definition.asset_type),
)
.to_ok()
}
#[cfg(test)]
mod tests {
use crate::contract::execute;
use crate::core::error::ContractError;
use crate::core::msg::ExecuteMsg;
use crate::core::state::load_asset_definition_by_type_v3;
use crate::core::types::asset_definition::{AssetDefinitionInputV3, AssetDefinitionV3};
use crate::core::types::fee_destination::FeeDestinationV2;
use crate::core::types::verifier_detail::VerifierDetailV2;
use crate::execute::update_asset_definition::{
update_asset_definition, UpdateAssetDefinitionV1,
};
use crate::testutil::test_constants::{
DEFAULT_ADMIN_ADDRESS, DEFAULT_ASSET_TYPE, DEFAULT_ASSET_TYPE_DISPLAY_NAME,
DEFAULT_SENDER_ADDRESS,
};
use crate::testutil::test_utilities::{
empty_mock_info, get_default_entity_detail, single_attribute_for_key,
test_instantiate_success, InstArgs,
};
use crate::util::constants::{ASSET_EVENT_TYPE_KEY, ASSET_TYPE_KEY, NHASH};
use crate::util::event_attributes::EventType;
use crate::util::traits::OptionExtensions;
use crate::validation::validate_init_msg::validate_asset_definition_input;
use cosmwasm_std::testing::{message_info, mock_env};
use cosmwasm_std::{coin, Addr, Deps, Uint128};
use provwasm_mocks::mock_provenance_dependencies;
#[test]
fn test_valid_update_asset_definition_via_execute() {
let mut deps = mock_provenance_dependencies();
test_instantiate_success(deps.as_mut(), &InstArgs::default());
let asset_definition = get_update_asset_definition();
let response = execute(
deps.as_mut(),
mock_env(),
empty_mock_info(DEFAULT_ADMIN_ADDRESS),
ExecuteMsg::UpdateAssetDefinition {
asset_definition: asset_definition.clone(),
},
)
.expect("expected the update asset checks to work correctly");
assert!(
response.messages.is_empty(),
"updating an asset definition should not require messages",
);
assert_eq!(
2,
response.attributes.len(),
"updating an asset definition should produce the correct number of attributes",
);
assert_eq!(
EventType::UpdateAssetDefinition.event_name().as_str(),
single_attribute_for_key(&response, ASSET_EVENT_TYPE_KEY),
"the correct event type should be emitted",
);
assert_eq!(
DEFAULT_ASSET_TYPE,
single_attribute_for_key(&response, ASSET_TYPE_KEY),
"the asset type attribute should be added correctly",
);
test_asset_definition_was_updated_for_input(&asset_definition, &deps.as_ref());
}
#[test]
fn test_valid_update_asset_definition_via_internal() {
let mut deps = mock_provenance_dependencies();
test_instantiate_success(deps.as_mut(), &InstArgs::default());
let msg = get_valid_update_asset_definition();
update_asset_definition(
deps.as_mut(),
message_info(&Addr::unchecked(DEFAULT_ADMIN_ADDRESS), &[]),
msg.clone(),
)
.expect("expected the update asset definition function to return properly");
test_asset_definition_was_updated(&msg.asset_definition, &deps.as_ref());
}
#[test]
fn test_invalid_update_asset_definition_for_invalid_msg() {
let mut deps = mock_provenance_dependencies();
test_instantiate_success(deps.as_mut(), &InstArgs::default());
let msg = ExecuteMsg::UpdateAssetDefinition {
asset_definition: AssetDefinitionInputV3::new(
DEFAULT_ASSET_TYPE,
DEFAULT_ASSET_TYPE_DISPLAY_NAME,
vec![],
None,
None,
),
};
let error = execute(
deps.as_mut(),
mock_env(),
message_info(&Addr::unchecked(DEFAULT_ADMIN_ADDRESS), &[]),
msg,
)
.unwrap_err();
assert!(
matches!(error, ContractError::InvalidMessageFields { .. }),
"expected an invalid asset definition to cause an InvalidMessageFields error, but got {:?}",
error,
);
}
#[test]
fn test_invalid_update_asset_definition_for_invalid_sender() {
let mut deps = mock_provenance_dependencies();
test_instantiate_success(deps.as_mut(), &InstArgs::default());
let error = update_asset_definition(
deps.as_mut(),
// Send from the "sender address" which is the address of the account that does onboarding in tests
message_info(&Addr::unchecked(DEFAULT_SENDER_ADDRESS), &[]),
get_valid_update_asset_definition(),
)
.unwrap_err();
assert!(
matches!(error, ContractError::Unauthorized { .. }),
"expected the unauthorized response to be returned when a different address than the admin is the sender, but got error: {:?}",
error,
);
}
#[test]
fn test_invalid_update_asset_definition_for_provided_funds() {
let mut deps = mock_provenance_dependencies();
test_instantiate_success(deps.as_mut(), &InstArgs::default());
let error = update_asset_definition(
deps.as_mut(),
message_info(
&Addr::unchecked(DEFAULT_ADMIN_ADDRESS),
&[coin(420, "usdf")],
),
get_valid_update_asset_definition(),
)
.unwrap_err();
assert!(
matches!(error, ContractError::InvalidFunds(_)),
"expected the invalid funds response to be returned when funds are provided to the function, but got: {:?}",
error,
);
}
#[test]
fn test_invalid_update_asset_definition_for_missing_loan_type() {
let mut deps = mock_provenance_dependencies();
test_instantiate_success(deps.as_mut(), &InstArgs::default());
let missing_asset_definition = AssetDefinitionV3::new(
"nonexistent-type",
"WHOAMI".to_some(),
vec![VerifierDetailV2::new(
"verifier",
Uint128::new(100),
NHASH,
vec![FeeDestinationV2::new("fee-guy", 25)],
get_default_entity_detail().to_some(),
None,
None,
)],
);
let error = update_asset_definition(
deps.as_mut(),
message_info(&Addr::unchecked(DEFAULT_ADMIN_ADDRESS), &[]),
UpdateAssetDefinitionV1::new(missing_asset_definition),
)
.unwrap_err();
assert!(
matches!(error, ContractError::RecordNotFound { .. }),
"expected the not found response to be returned when an update is attempted for a definition that does not exist, but got: {:?}",
error,
);
}
fn test_asset_definition_was_updated_for_input(input: &AssetDefinitionInputV3, deps: &Deps) {
test_asset_definition_was_updated(&input.as_asset_definition(), deps)
}
fn test_asset_definition_was_updated(asset_definition: &AssetDefinitionV3, deps: &Deps) {
let state_def =
load_asset_definition_by_type_v3(deps.storage, &asset_definition.asset_type)
.expect("expected the updated asset definition to be stored in the state");
assert_eq!(
asset_definition, &state_def,
"the value in state should directly equate to the added value",
);
}
// This builds off of the existing default asset definition in test_utilities and adds/tweaks
// details. This uses randomly-generated bech32 provenance testnet addresses to be different than
// the default values
fn get_update_asset_definition() -> AssetDefinitionInputV3 {
let def = AssetDefinitionInputV3::new(
DEFAULT_ASSET_TYPE,
DEFAULT_ASSET_TYPE_DISPLAY_NAME,
vec![VerifierDetailV2::new(
"tp1y67rma23nplzy8rpvfqsztvktvp85hnmnjvzxs",
Uint128::new(1500000),
NHASH,
vec![
FeeDestinationV2::new("tp1knh6n2kafm78mfv0c6d6y3x3en3pcdph23r2e7", 450000),
FeeDestinationV2::new("tp1uqx5fcrx0nkcak52tt794p03d5tju62qfnwc52", 300000),
],
get_default_entity_detail().to_some(),
None,
None,
)],
None,
None,
);
validate_asset_definition_input(&def).expect("expected the asset definition to be valid");
def
}
fn get_valid_update_asset_definition() -> UpdateAssetDefinitionV1 {
UpdateAssetDefinitionV1 {
asset_definition: get_update_asset_definition().into_asset_definition(),
}
}
}