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 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
//! # Implementation of the contract trait and contract wrapper
use crate::error::{anyhow, bail, AnyError, AnyResult};
use cosmwasm_std::{
from_json, Binary, CosmosMsg, CustomMsg, CustomQuery, Deps, DepsMut, Empty, Env, MessageInfo,
QuerierWrapper, Reply, Response, SubMsg,
};
use serde::de::DeserializeOwned;
use std::fmt::{Debug, Display};
use std::ops::Deref;
/// This trait serves as a primary interface for interacting with contracts.
#[rustfmt::skip]
pub trait Contract<C, Q = Empty>
where
C: CustomMsg,
Q: CustomQuery,
{
/// Evaluates contract's `execute` entry-point.
fn execute(&self, deps: DepsMut<Q>, env: Env, info: MessageInfo, msg: Vec<u8>) -> AnyResult<Response<C>>;
/// Evaluates contract's `instantiate` entry-point.
fn instantiate(&self, deps: DepsMut<Q>, env: Env, info: MessageInfo, msg: Vec<u8>) -> AnyResult<Response<C>>;
/// Evaluates contract's `query` entry-point.
fn query(&self, deps: Deps<Q>, env: Env, msg: Vec<u8>) -> AnyResult<Binary>;
/// Evaluates contract's `sudo` entry-point.
fn sudo(&self, deps: DepsMut<Q>, env: Env, msg: Vec<u8>) -> AnyResult<Response<C>>;
/// Evaluates contract's `reply` entry-point.
fn reply(&self, deps: DepsMut<Q>, env: Env, msg: Reply) -> AnyResult<Response<C>>;
/// Evaluates contract's `migrate` entry-point.
fn migrate(&self, deps: DepsMut<Q>, env: Env, msg: Vec<u8>) -> AnyResult<Response<C>>;
}
#[rustfmt::skip]
mod closures {
use super::*;
// function types
pub type ContractFn<T, C, E, Q> = fn(deps: DepsMut<Q>, env: Env, info: MessageInfo, msg: T) -> Result<Response<C>, E>;
pub type PermissionedFn<T, C, E, Q> = fn(deps: DepsMut<Q>, env: Env, msg: T) -> Result<Response<C>, E>;
pub type ReplyFn<C, E, Q> = fn(deps: DepsMut<Q>, env: Env, msg: Reply) -> Result<Response<C>, E>;
pub type QueryFn<T, E, Q> = fn(deps: Deps<Q>, env: Env, msg: T) -> Result<Binary, E>;
// closure types
pub type ContractClosure<T, C, E, Q> = Box<dyn Fn(DepsMut<Q>, Env, MessageInfo, T) -> Result<Response<C>, E>>;
pub type PermissionedClosure<T, C, E, Q> = Box<dyn Fn(DepsMut<Q>, Env, T) -> Result<Response<C>, E>>;
pub type ReplyClosure<C, E, Q> = Box<dyn Fn(DepsMut<Q>, Env, Reply) -> Result<Response<C>, E>>;
pub type QueryClosure<T, E, Q> = Box<dyn Fn(Deps<Q>, Env, T) -> Result<Binary, E>>;
}
use closures::*;
/// This structure wraps the [Contract] trait implementor
/// and provides generic access to the contract's entry-points.
///
/// List of generic types used in [ContractWrapper]:
/// - **T1** type of message passed to [execute] entry-point.
/// - **T2** type of message passed to [instantiate] entry-point.
/// - **T3** type of message passed to [query] entry-point.
/// - **T4** type of message passed to [sudo] entry-point.
/// - instead of **~~T5~~**, always the `Reply` type is used in [reply] entry-point.
/// - **T6** type of message passed to [migrate] entry-point.
/// - **E1** type of error returned from [execute] entry-point.
/// - **E2** type of error returned from [instantiate] entry-point.
/// - **E3** type of error returned from [query] entry-point.
/// - **E4** type of error returned from [sudo] entry-point.
/// - **E5** type of error returned from [reply] entry-point.
/// - **E6** type of error returned from [migrate] entry-point.
/// - **C** type of custom message returned from all entry-points except [query].
/// - **Q** type of custom query in `Querier` passed as 'Deps' or 'DepsMut' to all entry-points.
///
/// The following table summarizes the purpose of all generic types used in [ContractWrapper].
/// ```text
/// ┌─────────────┬────────────────┬─────────────────────┬─────────┬─────────┬───────┬───────┐
/// │ Contract │ Contract │ │ │ │ │ │
/// │ entry-point │ wrapper │ Closure type │ Message │ Message │ Error │ Query │
/// │ │ member │ │ IN │ OUT │ OUT │ │
/// ╞═════════════╪════════════════╪═════════════════════╪═════════╪═════════╪═══════╪═══════╡
/// │ (1) │ │ │ │ │ │ │
/// ╞═════════════╪════════════════╪═════════════════════╪═════════╪═════════╪═══════╪═══════╡
/// │ execute │ execute_fn │ ContractClosure │ T1 │ C │ E1 │ Q │
/// ├─────────────┼────────────────┼─────────────────────┼─────────┼─────────┼───────┼───────┤
/// │ instantiate │ instantiate_fn │ ContractClosure │ T2 │ C │ E2 │ Q │
/// ├─────────────┼────────────────┼─────────────────────┼─────────┼─────────┼───────┼───────┤
/// │ query │ query_fn │ QueryClosure │ T3 │ Binary │ E3 │ Q │
/// ├─────────────┼────────────────┼─────────────────────┼─────────┼─────────┼───────┼───────┤
/// │ sudo │ sudo_fn │ PermissionedClosure │ T4 │ C │ E4 │ Q │
/// ├─────────────┼────────────────┼─────────────────────┼─────────┼─────────┼───────┼───────┤
/// │ reply │ reply_fn │ ReplyClosure │ Reply │ C │ E5 │ Q │
/// ├─────────────┼────────────────┼─────────────────────┼─────────┼─────────┼───────┼───────┤
/// │ migrate │ migrate_fn │ PermissionedClosure │ T6 │ C │ E6 │ Q │
/// └─────────────┴────────────────┴─────────────────────┴─────────┴─────────┴───────┴───────┘
/// ```
/// The general schema depicting which generic type is used in entry points is shown below.
/// Entry point, when called, is provided minimum two arguments: custom query of type **Q**
/// (inside `Deps` or `DepsMut`) and input message of type **T1**, **T2**, **T3**, **T4**,
/// **Reply** or **T6**. As a result, entry point returns custom output message of type
/// Response<**C**> or **Binary** and an error of type **E1**, **E2**, **E3**, **E4**, **E5**
/// or **E6**.
///
/// ```text
/// entry_point(query, .., message_in) -> Result<message_out, error>
/// ┬ ┬ ┬ ┬
/// Q >──┘ │ │ └──> E1,E2,E3,E4,E5,E6
/// T1,T2,T3,T4,Reply,T6 >────┘ └─────────────> C,Binary
/// ```
/// Generic type **C** defines a custom message that is specific for the **whole blockchain**.
/// Similarly, the generic type **Q** defines a custom query that is also specific
/// to the **whole blockchain**. Other generic types are specific to the implemented contract.
/// So all smart contracts used in the same blockchain will have the same types for **C** and **Q**,
/// but each contract may use different type for other generic types.
/// It means that e.g. **T1** in smart contract `A` may differ from **T1** in smart contract `B`.
///
/// [execute]: Contract::execute
/// [instantiate]: Contract::instantiate
/// [query]: Contract::query
/// [sudo]: Contract::sudo
/// [reply]: Contract::reply
/// [migrate]: Contract::migrate
pub struct ContractWrapper<
T1,
T2,
T3,
E1,
E2,
E3,
C = Empty,
Q = Empty,
T4 = Empty,
E4 = AnyError,
E5 = AnyError,
T6 = Empty,
E6 = AnyError,
> where
T1: DeserializeOwned, // Type of message passed to `execute` entry-point.
T2: DeserializeOwned, // Type of message passed to `instantiate` entry-point.
T3: DeserializeOwned, // Type of message passed to `query` entry-point.
T4: DeserializeOwned, // Type of message passed to `sudo` entry-point.
T6: DeserializeOwned, // Type of message passed to `migrate` entry-point.
E1: Display + Debug + Send + Sync, // Type of error returned from `execute` entry-point.
E2: Display + Debug + Send + Sync, // Type of error returned from `instantiate` entry-point.
E3: Display + Debug + Send + Sync, // Type of error returned from `query` entry-point.
E4: Display + Debug + Send + Sync, // Type of error returned from `sudo` entry-point.
E5: Display + Debug + Send + Sync, // Type of error returned from `reply` entry-point.
E6: Display + Debug + Send + Sync, // Type of error returned from `migrate` entry-point.
C: CustomMsg, // Type of custom message returned from all entry-points except `query`.
Q: CustomQuery + DeserializeOwned, // Type of custom query in querier passed as deps/deps_mut to all entry-points.
{
execute_fn: ContractClosure<T1, C, E1, Q>,
instantiate_fn: ContractClosure<T2, C, E2, Q>,
query_fn: QueryClosure<T3, E3, Q>,
sudo_fn: Option<PermissionedClosure<T4, C, E4, Q>>,
reply_fn: Option<ReplyClosure<C, E5, Q>>,
migrate_fn: Option<PermissionedClosure<T6, C, E6, Q>>,
}
impl<T1, T2, T3, E1, E2, E3, C, Q> ContractWrapper<T1, T2, T3, E1, E2, E3, C, Q>
where
T1: DeserializeOwned + 'static, // Type of message passed to `execute` entry-point.
T2: DeserializeOwned + 'static, // Type of message passed to `instantiate` entry-point.
T3: DeserializeOwned + 'static, // Type of message passed to `query` entry-point.
E1: Display + Debug + Send + Sync + 'static, // Type of error returned from `execute` entry-point.
E2: Display + Debug + Send + Sync + 'static, // Type of error returned from `instantiate` entry-point.
E3: Display + Debug + Send + Sync + 'static, // Type of error returned from `query` entry-point.
C: CustomMsg + 'static, // Type of custom message returned from all entry-points except `query`.
Q: CustomQuery + DeserializeOwned + 'static, // Type of custom query in querier passed as deps/deps_mut to all entry-points.
{
/// Creates a new contract wrapper with default settings.
pub fn new(
execute_fn: ContractFn<T1, C, E1, Q>,
instantiate_fn: ContractFn<T2, C, E2, Q>,
query_fn: QueryFn<T3, E3, Q>,
) -> Self {
Self {
execute_fn: Box::new(execute_fn),
instantiate_fn: Box::new(instantiate_fn),
query_fn: Box::new(query_fn),
sudo_fn: None,
reply_fn: None,
migrate_fn: None,
}
}
/// This will take a contract that returns `Response<Empty>` and will _upgrade_ it
/// to `Response<C>` if needed, to be compatible with a chain-specific extension.
pub fn new_with_empty(
execute_fn: ContractFn<T1, Empty, E1, Empty>,
instantiate_fn: ContractFn<T2, Empty, E2, Empty>,
query_fn: QueryFn<T3, E3, Empty>,
) -> Self {
Self {
execute_fn: customize_contract_fn(execute_fn),
instantiate_fn: customize_contract_fn(instantiate_fn),
query_fn: customize_query_fn(query_fn),
sudo_fn: None,
reply_fn: None,
migrate_fn: None,
}
}
}
#[allow(clippy::type_complexity)]
impl<T1, T2, T3, E1, E2, E3, C, Q, T4, E4, E5, T6, E6>
ContractWrapper<T1, T2, T3, E1, E2, E3, C, Q, T4, E4, E5, T6, E6>
where
T1: DeserializeOwned, // Type of message passed to `execute` entry-point.
T2: DeserializeOwned, // Type of message passed to `instantiate` entry-point.
T3: DeserializeOwned, // Type of message passed to `query` entry-point.
T4: DeserializeOwned, // Type of message passed to `sudo` entry-point.
T6: DeserializeOwned, // Type of message passed to `migrate` entry-point.
E1: Display + Debug + Send + Sync, // Type of error returned from `execute` entry-point.
E2: Display + Debug + Send + Sync, // Type of error returned from `instantiate` entry-point.
E3: Display + Debug + Send + Sync, // Type of error returned from `query` entry-point.
E4: Display + Debug + Send + Sync, // Type of error returned from `sudo` entry-point.
E5: Display + Debug + Send + Sync, // Type of error returned from `reply` entry-point.
E6: Display + Debug + Send + Sync, // Type of error returned from `migrate` entry-point.
C: CustomMsg + 'static, // Type of custom message returned from all entry-points except `query`.
Q: CustomQuery + DeserializeOwned + 'static, // Type of custom query in querier passed as deps/deps_mut to all entry-points.
{
/// Populates [ContractWrapper] with contract's `sudo` entry-point and custom message type.
pub fn with_sudo<T4A, E4A>(
self,
sudo_fn: PermissionedFn<T4A, C, E4A, Q>,
) -> ContractWrapper<T1, T2, T3, E1, E2, E3, C, Q, T4A, E4A, E5, T6, E6>
where
T4A: DeserializeOwned + 'static,
E4A: Display + Debug + Send + Sync + 'static,
{
ContractWrapper {
execute_fn: self.execute_fn,
instantiate_fn: self.instantiate_fn,
query_fn: self.query_fn,
sudo_fn: Some(Box::new(sudo_fn)),
reply_fn: self.reply_fn,
migrate_fn: self.migrate_fn,
}
}
/// Populates [ContractWrapper] with contract's `sudo` entry-point and `Empty` as a custom message.
pub fn with_sudo_empty<T4A, E4A>(
self,
sudo_fn: PermissionedFn<T4A, Empty, E4A, Empty>,
) -> ContractWrapper<T1, T2, T3, E1, E2, E3, C, Q, T4A, E4A, E5, T6, E6>
where
T4A: DeserializeOwned + 'static,
E4A: Display + Debug + Send + Sync + 'static,
{
ContractWrapper {
execute_fn: self.execute_fn,
instantiate_fn: self.instantiate_fn,
query_fn: self.query_fn,
sudo_fn: Some(customize_permissioned_fn(sudo_fn)),
reply_fn: self.reply_fn,
migrate_fn: self.migrate_fn,
}
}
/// Populates [ContractWrapper] with contract's `reply` entry-point and custom message type.
pub fn with_reply<E5A>(
self,
reply_fn: ReplyFn<C, E5A, Q>,
) -> ContractWrapper<T1, T2, T3, E1, E2, E3, C, Q, T4, E4, E5A, T6, E6>
where
E5A: Display + Debug + Send + Sync + 'static,
{
ContractWrapper {
execute_fn: self.execute_fn,
instantiate_fn: self.instantiate_fn,
query_fn: self.query_fn,
sudo_fn: self.sudo_fn,
reply_fn: Some(Box::new(reply_fn)),
migrate_fn: self.migrate_fn,
}
}
/// Populates [ContractWrapper] with contract's `reply` entry-point and `Empty` as a custom message.
pub fn with_reply_empty<E5A>(
self,
reply_fn: ReplyFn<Empty, E5A, Empty>,
) -> ContractWrapper<T1, T2, T3, E1, E2, E3, C, Q, T4, E4, E5A, T6, E6>
where
E5A: Display + Debug + Send + Sync + 'static,
{
ContractWrapper {
execute_fn: self.execute_fn,
instantiate_fn: self.instantiate_fn,
query_fn: self.query_fn,
sudo_fn: self.sudo_fn,
reply_fn: Some(customize_permissioned_fn(reply_fn)),
migrate_fn: self.migrate_fn,
}
}
/// Populates [ContractWrapper] with contract's `migrate` entry-point and custom message type.
pub fn with_migrate<T6A, E6A>(
self,
migrate_fn: PermissionedFn<T6A, C, E6A, Q>,
) -> ContractWrapper<T1, T2, T3, E1, E2, E3, C, Q, T4, E4, E5, T6A, E6A>
where
T6A: DeserializeOwned + 'static,
E6A: Display + Debug + Send + Sync + 'static,
{
ContractWrapper {
execute_fn: self.execute_fn,
instantiate_fn: self.instantiate_fn,
query_fn: self.query_fn,
sudo_fn: self.sudo_fn,
reply_fn: self.reply_fn,
migrate_fn: Some(Box::new(migrate_fn)),
}
}
/// Populates [ContractWrapper] with contract's `migrate` entry-point and `Empty` as a custom message.
pub fn with_migrate_empty<T6A, E6A>(
self,
migrate_fn: PermissionedFn<T6A, Empty, E6A, Empty>,
) -> ContractWrapper<T1, T2, T3, E1, E2, E3, C, Q, T4, E4, E5, T6A, E6A>
where
T6A: DeserializeOwned + 'static,
E6A: Display + Debug + Send + Sync + 'static,
{
ContractWrapper {
execute_fn: self.execute_fn,
instantiate_fn: self.instantiate_fn,
query_fn: self.query_fn,
sudo_fn: self.sudo_fn,
reply_fn: self.reply_fn,
migrate_fn: Some(customize_permissioned_fn(migrate_fn)),
}
}
}
fn customize_contract_fn<T, C, E, Q>(
raw_fn: ContractFn<T, Empty, E, Empty>,
) -> ContractClosure<T, C, E, Q>
where
T: DeserializeOwned + 'static,
E: Display + Debug + Send + Sync + 'static,
C: CustomMsg,
Q: CustomQuery + DeserializeOwned,
{
Box::new(
move |mut deps: DepsMut<Q>,
env: Env,
info: MessageInfo,
msg: T|
-> Result<Response<C>, E> {
let deps = decustomize_deps_mut(&mut deps);
raw_fn(deps, env, info, msg).map(customize_response::<C>)
},
)
}
fn customize_query_fn<T, E, Q>(raw_fn: QueryFn<T, E, Empty>) -> QueryClosure<T, E, Q>
where
T: DeserializeOwned + 'static,
E: Display + Debug + Send + Sync + 'static,
Q: CustomQuery + DeserializeOwned,
{
Box::new(
move |deps: Deps<Q>, env: Env, msg: T| -> Result<Binary, E> {
let deps = decustomize_deps(&deps);
raw_fn(deps, env, msg)
},
)
}
fn customize_permissioned_fn<T, C, E, Q>(
raw_fn: PermissionedFn<T, Empty, E, Empty>,
) -> PermissionedClosure<T, C, E, Q>
where
T: DeserializeOwned + 'static,
E: Display + Debug + Send + Sync + 'static,
C: CustomMsg,
Q: CustomQuery + DeserializeOwned,
{
Box::new(
move |mut deps: DepsMut<Q>, env: Env, msg: T| -> Result<Response<C>, E> {
let deps = decustomize_deps_mut(&mut deps);
raw_fn(deps, env, msg).map(customize_response::<C>)
},
)
}
fn decustomize_deps_mut<'a, Q>(deps: &'a mut DepsMut<Q>) -> DepsMut<'a, Empty>
where
Q: CustomQuery + DeserializeOwned,
{
DepsMut {
storage: deps.storage,
api: deps.api,
querier: QuerierWrapper::new(deps.querier.deref()),
}
}
fn decustomize_deps<'a, Q>(deps: &'a Deps<'a, Q>) -> Deps<'a, Empty>
where
Q: CustomQuery + DeserializeOwned,
{
Deps {
storage: deps.storage,
api: deps.api,
querier: QuerierWrapper::new(deps.querier.deref()),
}
}
fn customize_response<C>(resp: Response<Empty>) -> Response<C>
where
C: CustomMsg,
{
let mut customized_resp = Response::<C>::new()
.add_submessages(resp.messages.into_iter().map(customize_msg::<C>))
.add_events(resp.events)
.add_attributes(resp.attributes);
customized_resp.data = resp.data;
customized_resp
}
fn customize_msg<C>(msg: SubMsg<Empty>) -> SubMsg<C>
where
C: CustomMsg,
{
SubMsg {
id: msg.id,
payload: Binary::default(),
msg: match msg.msg {
CosmosMsg::Wasm(wasm) => CosmosMsg::Wasm(wasm),
CosmosMsg::Bank(bank) => CosmosMsg::Bank(bank),
CosmosMsg::Staking(staking) => CosmosMsg::Staking(staking),
CosmosMsg::Distribution(distribution) => CosmosMsg::Distribution(distribution),
CosmosMsg::Custom(_) => unreachable!(),
CosmosMsg::Ibc(ibc) => CosmosMsg::Ibc(ibc),
CosmosMsg::Any(any) => CosmosMsg::Any(any),
_ => panic!("unknown message variant {:?}", msg),
},
gas_limit: msg.gas_limit,
reply_on: msg.reply_on,
}
}
impl<T1, T2, T3, E1, E2, E3, C, T4, E4, E5, T6, E6, Q> Contract<C, Q>
for ContractWrapper<T1, T2, T3, E1, E2, E3, C, Q, T4, E4, E5, T6, E6>
where
T1: DeserializeOwned, // Type of message passed to `execute` entry-point.
T2: DeserializeOwned, // Type of message passed to `instantiate` entry-point.
T3: DeserializeOwned, // Type of message passed to `query` entry-point.
T4: DeserializeOwned, // Type of message passed to `sudo` entry-point.
T6: DeserializeOwned, // Type of message passed to `migrate` entry-point.
E1: Display + Debug + Send + Sync + 'static, // Type of error returned from `execute` entry-point.
E2: Display + Debug + Send + Sync + 'static, // Type of error returned from `instantiate` entry-point.
E3: Display + Debug + Send + Sync + 'static, // Type of error returned from `query` entry-point.
E4: Display + Debug + Send + Sync + 'static, // Type of error returned from `sudo` entry-point.
E5: Display + Debug + Send + Sync + 'static, // Type of error returned from `reply` entry-point.
E6: Display + Debug + Send + Sync + 'static, // Type of error returned from `migrate` entry-point.
C: CustomMsg, // Type of custom message returned from all entry-points except `query`.
Q: CustomQuery + DeserializeOwned, // Type of custom query in querier passed as deps/deps_mut to all entry-points.
{
/// Calls [execute] on wrapped [Contract] trait implementor.
///
/// [execute]: Contract::execute
fn execute(
&self,
deps: DepsMut<Q>,
env: Env,
info: MessageInfo,
msg: Vec<u8>,
) -> AnyResult<Response<C>> {
let msg: T1 = from_json(msg)?;
(self.execute_fn)(deps, env, info, msg).map_err(|err: E1| anyhow!(err))
}
/// Calls [instantiate] on wrapped [Contract] trait implementor.
///
/// [instantiate]: Contract::instantiate
fn instantiate(
&self,
deps: DepsMut<Q>,
env: Env,
info: MessageInfo,
msg: Vec<u8>,
) -> AnyResult<Response<C>> {
let msg: T2 = from_json(msg)?;
(self.instantiate_fn)(deps, env, info, msg).map_err(|err: E2| anyhow!(err))
}
/// Calls [query] on wrapped [Contract] trait implementor.
///
/// [query]: Contract::query
fn query(&self, deps: Deps<Q>, env: Env, msg: Vec<u8>) -> AnyResult<Binary> {
let msg: T3 = from_json(msg)?;
(self.query_fn)(deps, env, msg).map_err(|err: E3| anyhow!(err))
}
/// Calls [sudo] on wrapped [Contract] trait implementor.
/// Returns an error when the contract does not implement [sudo].
///
/// [sudo]: Contract::sudo
fn sudo(&self, deps: DepsMut<Q>, env: Env, msg: Vec<u8>) -> AnyResult<Response<C>> {
let msg: T4 = from_json(msg)?;
match &self.sudo_fn {
Some(sudo) => sudo(deps, env, msg).map_err(|err: E4| anyhow!(err)),
None => bail!("sudo is not implemented for contract"),
}
}
/// Calls [reply] on wrapped [Contract] trait implementor.
/// Returns an error when the contract does not implement [reply].
///
/// [reply]: Contract::reply
fn reply(&self, deps: DepsMut<Q>, env: Env, reply_data: Reply) -> AnyResult<Response<C>> {
let msg: Reply = reply_data;
match &self.reply_fn {
Some(reply) => reply(deps, env, msg).map_err(|err: E5| anyhow!(err)),
None => bail!("reply is not implemented for contract"),
}
}
/// Calls [migrate] on wrapped [Contract] trait implementor.
/// Returns an error when the contract does not implement [migrate].
///
/// [migrate]: Contract::migrate
fn migrate(&self, deps: DepsMut<Q>, env: Env, msg: Vec<u8>) -> AnyResult<Response<C>> {
let msg: T6 = from_json(msg)?;
match &self.migrate_fn {
Some(migrate) => migrate(deps, env, msg).map_err(|err: E6| anyhow!(err)),
None => bail!("migrate is not implemented for contract"),
}
}
}