-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete_asset_definition.rs
219 lines (209 loc) · 9.09 KB
/
delete_asset_definition.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
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
use crate::core::error::ContractError;
use crate::core::msg::ExecuteMsg;
use crate::core::state::delete_asset_definition_by_asset_type_v3;
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::DeleteAssetDefinition](crate::core::msg::ExecuteMsg::DeleteAssetDefinition)
/// for ease of use in the underlying [delete_asset_definition](self::delete_asset_definition) function.
///
/// # Parameters
///
/// * `asset_type` The asset type to delete.
pub struct DeleteAssetDefinitionV1 {
pub asset_type: String,
}
impl DeleteAssetDefinitionV1 {
/// Constructs a new instance of this struct.
///
/// # Parameters
///
/// * `asset_type` The asset type to delete.
pub fn new(asset_type: &str) -> Self {
Self {
asset_type: asset_type.to_string(),
}
}
/// Attempts to create an instance of this struct from a provided execute msg. If the provided
/// value is not of the [DeleteAssetDefinition](crate::core::msg::ExecuteMsg::DeleteAssetDefinition)
/// 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<DeleteAssetDefinitionV1> {
match msg {
ExecuteMsg::DeleteAssetDefinition { asset_type } => {
DeleteAssetDefinitionV1::new(&asset_type).to_ok()
}
_ => ContractError::InvalidMessageType {
expected_message_type: "ExecuteMsg::DeleteAssetDefinition".to_string(),
}
.to_err(),
}
}
}
/// Route implementation for [ExecuteMsg::DeleteAssetDefinition](crate::core::msg::ExecuteMsg::DeleteAssetDefinition).
/// This function allows for the admin address to completely remove an asset definition. This is
/// dangerous, because existing assets in the onboarding process for an asset definition will start
/// emitting errors when being verified or retried. This should only ever be used on a definition
/// that is guaranteed to be not in use and/or was erroneously added.
///
/// # 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 delete asset definition v1 struct, provided by conversion from an
/// [ExecuteMsg](crate::core::msg::ExecuteMsg).
pub fn delete_asset_definition(
deps: DepsMut,
info: MessageInfo,
msg: DeleteAssetDefinitionV1,
) -> EntryPointResponse {
check_admin_only(&deps.as_ref(), &info)?;
check_funds_are_empty(&info)?;
let deleted_asset_type =
delete_asset_definition_by_asset_type_v3(deps.storage, &msg.asset_type)?;
Response::new()
.add_attributes(
EventAttributes::new(EventType::DeleteAssetDefinition)
.set_asset_type(deleted_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::execute::delete_asset_definition::{
delete_asset_definition, DeleteAssetDefinitionV1,
};
use crate::testutil::test_constants::{DEFAULT_ADMIN_ADDRESS, DEFAULT_ASSET_TYPE};
use crate::testutil::test_utilities::{
empty_mock_info, mock_info_with_funds, single_attribute_for_key, test_instantiate_success,
InstArgs,
};
use crate::util::constants::{ASSET_EVENT_TYPE_KEY, ASSET_TYPE_KEY};
use crate::util::event_attributes::EventType;
use cosmwasm_std::coin;
use cosmwasm_std::testing::mock_env;
use provwasm_mocks::mock_provenance_dependencies;
#[test]
fn test_delete_asset_definition_success_for_asset_type() {
let mut deps = mock_provenance_dependencies();
test_instantiate_success(deps.as_mut(), &InstArgs::default());
load_asset_definition_by_type_v3(deps.as_ref().storage, DEFAULT_ASSET_TYPE).expect(
"sanity check: expected the default asset type to be inserted after instantiation",
);
let response = delete_asset_definition(
deps.as_mut(),
empty_mock_info(DEFAULT_ADMIN_ADDRESS),
DeleteAssetDefinitionV1::new(DEFAULT_ASSET_TYPE),
)
.expect("expected deletion by asset type to succeed");
assert!(
response.messages.is_empty(),
"the route should not emit messages",
);
assert_eq!(
2,
response.attributes.len(),
"expected the correct number of attributes to be emitted",
);
assert_eq!(
EventType::DeleteAssetDefinition.event_name(),
single_attribute_for_key(&response, ASSET_EVENT_TYPE_KEY),
"expected the event type attribute to be set correctly",
);
assert_eq!(
DEFAULT_ASSET_TYPE,
single_attribute_for_key(&response, ASSET_TYPE_KEY),
"expected the asset type attribute to be set correctly",
);
let err = load_asset_definition_by_type_v3(deps.as_ref().storage, DEFAULT_ASSET_TYPE)
.expect_err("expected an error to occur when loading the default asset definition");
assert!(
matches!(err, ContractError::RecordNotFound { .. }),
"expected the record not found error to occur when attempting to access the asset definition after deletion, but got: {:?}",
err,
);
}
#[test]
fn test_delete_asset_definition_success_from_execute_route() {
let mut deps = mock_provenance_dependencies();
test_instantiate_success(deps.as_mut(), &InstArgs::default());
execute(
deps.as_mut(),
mock_env(),
empty_mock_info(DEFAULT_ADMIN_ADDRESS),
ExecuteMsg::DeleteAssetDefinition {
asset_type: DEFAULT_ASSET_TYPE.to_string(),
},
)
.expect("expected the deletion to be successful");
let err = load_asset_definition_by_type_v3(deps.as_ref().storage, DEFAULT_ASSET_TYPE)
.expect_err("expected an error to occur when loading the default asset definition");
assert!(
matches!(err, ContractError::RecordNotFound { .. }),
"expected the record not found error to occur when attempting to access the asset definition after deletion, but got: {:?}",
err,
);
}
#[test]
fn test_delete_asset_definition_failure_for_invalid_sender() {
let mut deps = mock_provenance_dependencies();
test_instantiate_success(deps.as_mut(), &InstArgs::default());
let err = delete_asset_definition(
deps.as_mut(),
empty_mock_info("bad-actor"),
DeleteAssetDefinitionV1::new(DEFAULT_ASSET_TYPE),
)
.expect_err(
"expected an error to occur when a non-admin user attempts to access the route",
);
assert!(
matches!(err, ContractError::Unauthorized { .. }),
"expected an unauthorized error to be emitted, but got: {:?}",
err,
);
}
#[test]
fn test_delete_asset_definition_failure_for_provided_funds() {
let mut deps = mock_provenance_dependencies();
test_instantiate_success(deps.as_mut(), &InstArgs::default());
let err = delete_asset_definition(
deps.as_mut(),
mock_info_with_funds(DEFAULT_ADMIN_ADDRESS, &[coin(100, "coindollars")]),
DeleteAssetDefinitionV1::new(DEFAULT_ASSET_TYPE),
)
.expect_err("expected an error to occur when funds are provided by the admin");
assert!(
matches!(err, ContractError::InvalidFunds(..)),
"expected an invalid funds error to be emitted, but got: {:?}",
err,
);
}
#[test]
fn test_delete_asset_definition_failure_for_missing_definition() {
let mut deps = mock_provenance_dependencies();
test_instantiate_success(deps.as_mut(), &InstArgs::default());
let err = delete_asset_definition(
deps.as_mut(),
empty_mock_info(DEFAULT_ADMIN_ADDRESS),
DeleteAssetDefinitionV1::new("not real asset type"),
)
.expect_err("expected an error to occur when an invalid asset type is provided");
assert!(
matches!(err, ContractError::RecordNotFound { .. }),
"expected a record not found error to be emitted when deleting a missing asset type, but got: {:?}",
err,
);
}
}