cosmwasm-std 2.2.2

Standard library for Wasm based smart contracts on Cosmos blockchains
Documentation
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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
//! exports exposes the public wasm API
//!
//! interface_version_8, allocate and deallocate turn into Wasm exports
//! as soon as cosmwasm_std is `use`d in the contract, even privately.
//!
//! `do_execute`, `do_instantiate`, `do_migrate`, `do_query`, `do_reply`
//! and `do_sudo` should be wrapped with a extern "C" entry point including
//! the contract-specific function pointer. This is done via the `#[entry_point]`
//! macro attribute from cosmwasm-derive.
use alloc::vec::Vec;
use core::marker::PhantomData;

use serde::de::DeserializeOwned;

use crate::deps::OwnedDeps;
use crate::ibc::{IbcBasicResponse, IbcDestinationCallbackMsg, IbcSourceCallbackMsg};
#[cfg(feature = "stargate")]
use crate::ibc::{
    IbcChannelCloseMsg, IbcChannelConnectMsg, IbcPacketAckMsg, IbcPacketReceiveMsg,
    IbcPacketTimeoutMsg, IbcReceiveResponse,
};
use crate::ibc::{IbcChannelOpenMsg, IbcChannelOpenResponse};
use crate::imports::{ExternalApi, ExternalQuerier, ExternalStorage};
use crate::memory::{Owned, Region};
use crate::panic::install_panic_handler;
use crate::query::CustomQuery;
use crate::results::{ContractResult, QueryResponse, Reply, Response};
use crate::serde::{from_json, to_json_vec};
use crate::types::Env;
use crate::{CustomMsg, Deps, DepsMut, MessageInfo, MigrateInfo};

// These functions are used as markers for the chain to know which features this contract requires.
// If the chain does not support all the required features, it will reject storing the contract.
// See `docs/CAPABILITIES.md` for more details.
#[cfg(feature = "iterator")]
#[no_mangle]
extern "C" fn requires_iterator() {}

#[cfg(feature = "staking")]
#[no_mangle]
extern "C" fn requires_staking() {}

#[cfg(feature = "stargate")]
#[no_mangle]
extern "C" fn requires_stargate() {}

#[cfg(feature = "cosmwasm_1_1")]
#[no_mangle]
extern "C" fn requires_cosmwasm_1_1() {}

#[cfg(feature = "cosmwasm_1_2")]
#[no_mangle]
extern "C" fn requires_cosmwasm_1_2() {}

#[cfg(feature = "cosmwasm_1_3")]
#[no_mangle]
extern "C" fn requires_cosmwasm_1_3() {}

#[cfg(feature = "cosmwasm_1_4")]
#[no_mangle]
extern "C" fn requires_cosmwasm_1_4() {}

#[cfg(feature = "cosmwasm_2_0")]
#[no_mangle]
extern "C" fn requires_cosmwasm_2_0() {}

#[cfg(feature = "cosmwasm_2_1")]
#[no_mangle]
extern "C" fn requires_cosmwasm_2_1() {}

#[cfg(feature = "cosmwasm_2_2")]
#[no_mangle]
extern "C" fn requires_cosmwasm_2_2() {}

/// interface_version_* exports mark which Wasm VM interface level this contract is compiled for.
/// They can be checked by cosmwasm_vm.
/// Update this whenever the Wasm VM interface breaks.
#[no_mangle]
extern "C" fn interface_version_8() {}

/// allocate reserves the given number of bytes in wasm memory and returns a pointer
/// to a Region defining this data. This space is managed by the calling process
/// and should be accompanied by a corresponding deallocate
#[no_mangle]
extern "C" fn allocate(size: usize) -> u32 {
    Region::with_capacity(size).to_heap_ptr() as u32
}

/// deallocate expects a pointer to a Region created with allocate.
/// It will free both the Region and the memory referenced by the Region.
#[no_mangle]
extern "C" fn deallocate(pointer: u32) {
    // auto-drop Region on function end
    let _ = unsafe { Region::from_heap_ptr(pointer as *mut Region<Owned>) };
}

// TODO: replace with https://doc.rust-lang.org/std/ops/trait.Try.html once stabilized
macro_rules! r#try_into_contract_result {
    ($expr:expr) => {
        match $expr {
            Ok(val) => val,
            Err(err) => {
                return ContractResult::Err(err.to_string());
            }
        }
    };
    ($expr:expr,) => {
        $crate::try_into_contract_result!($expr)
    };
}

/// This should be wrapped in an external "C" export, containing a contract-specific function as an argument.
///
/// - `Q`: custom query type (see QueryRequest)
/// - `M`: message type for request
/// - `C`: custom response message type (see CosmosMsg)
/// - `E`: error type for responses
pub fn do_instantiate<Q, M, C, E>(
    instantiate_fn: &dyn Fn(DepsMut<Q>, Env, MessageInfo, M) -> Result<Response<C>, E>,
    env_ptr: u32,
    info_ptr: u32,
    msg_ptr: u32,
) -> u32
where
    Q: CustomQuery,
    M: DeserializeOwned,
    C: CustomMsg,
    E: ToString,
{
    install_panic_handler();
    let res = _do_instantiate(
        instantiate_fn,
        env_ptr as *mut Region<Owned>,
        info_ptr as *mut Region<Owned>,
        msg_ptr as *mut Region<Owned>,
    );
    let v = to_json_vec(&res).unwrap();
    Region::from_vec(v).to_heap_ptr() as u32
}

/// do_execute should be wrapped in an external "C" export, containing a contract-specific function as arg
///
/// - `Q`: custom query type (see QueryRequest)
/// - `M`: message type for request
/// - `C`: custom response message type (see CosmosMsg)
/// - `E`: error type for responses
pub fn do_execute<Q, M, C, E>(
    execute_fn: &dyn Fn(DepsMut<Q>, Env, MessageInfo, M) -> Result<Response<C>, E>,
    env_ptr: u32,
    info_ptr: u32,
    msg_ptr: u32,
) -> u32
where
    Q: CustomQuery,
    M: DeserializeOwned,
    C: CustomMsg,
    E: ToString,
{
    install_panic_handler();
    let res = _do_execute(
        execute_fn,
        env_ptr as *mut Region<Owned>,
        info_ptr as *mut Region<Owned>,
        msg_ptr as *mut Region<Owned>,
    );
    let v = to_json_vec(&res).unwrap();
    Region::from_vec(v).to_heap_ptr() as u32
}

/// do_migrate should be wrapped in an external "C" export, containing a contract-specific function as arg
///
/// - `Q`: custom query type (see QueryRequest)
/// - `M`: message type for request
/// - `C`: custom response message type (see CosmosMsg)
/// - `E`: error type for responses
pub fn do_migrate<Q, M, C, E>(
    migrate_fn: &dyn Fn(DepsMut<Q>, Env, M) -> Result<Response<C>, E>,
    env_ptr: u32,
    msg_ptr: u32,
) -> u32
where
    Q: CustomQuery,
    M: DeserializeOwned,
    C: CustomMsg,
    E: ToString,
{
    install_panic_handler();
    let res = _do_migrate(
        migrate_fn,
        env_ptr as *mut Region<Owned>,
        msg_ptr as *mut Region<Owned>,
    );
    let v = to_json_vec(&res).unwrap();
    Region::from_vec(v).to_heap_ptr() as u32
}

/// do_migrate_with_info should be wrapped in an external "C" export,
/// containing a contract-specific function as arg
///
/// - `Q`: custom query type (see QueryRequest)
/// - `M`: message type for request
/// - `C`: custom response message type (see CosmosMsg)
/// - `E`: error type for responses
#[cfg(feature = "cosmwasm_2_2")]
pub fn do_migrate_with_info<Q, M, C, E>(
    migrate_with_info_fn: &dyn Fn(DepsMut<Q>, Env, M, MigrateInfo) -> Result<Response<C>, E>,
    env_ptr: u32,
    msg_ptr: u32,
    migrate_info_ptr: u32,
) -> u32
where
    Q: CustomQuery,
    M: DeserializeOwned,
    C: CustomMsg,
    E: ToString,
{
    install_panic_handler();
    let res = _do_migrate_with_info(
        migrate_with_info_fn,
        env_ptr as *mut Region<Owned>,
        msg_ptr as *mut Region<Owned>,
        migrate_info_ptr as *mut Region<Owned>,
    );
    let v = to_json_vec(&res).unwrap();
    Region::from_vec(v).to_heap_ptr() as u32
}

/// do_sudo should be wrapped in an external "C" export, containing a contract-specific function as arg
///
/// - `Q`: custom query type (see QueryRequest)
/// - `M`: message type for request
/// - `C`: custom response message type (see CosmosMsg)
/// - `E`: error type for responses
pub fn do_sudo<Q, M, C, E>(
    sudo_fn: &dyn Fn(DepsMut<Q>, Env, M) -> Result<Response<C>, E>,
    env_ptr: u32,
    msg_ptr: u32,
) -> u32
where
    Q: CustomQuery,
    M: DeserializeOwned,
    C: CustomMsg,
    E: ToString,
{
    install_panic_handler();
    let res = _do_sudo(
        sudo_fn,
        env_ptr as *mut Region<Owned>,
        msg_ptr as *mut Region<Owned>,
    );
    let v = to_json_vec(&res).unwrap();
    Region::from_vec(v).to_heap_ptr() as u32
}

/// do_reply should be wrapped in an external "C" export, containing a contract-specific function as arg
/// message body is always `SubcallResult`
///
/// - `Q`: custom query type (see QueryRequest)
/// - `C`: custom response message type (see CosmosMsg)
/// - `E`: error type for responses
pub fn do_reply<Q, C, E>(
    reply_fn: &dyn Fn(DepsMut<Q>, Env, Reply) -> Result<Response<C>, E>,
    env_ptr: u32,
    msg_ptr: u32,
) -> u32
where
    Q: CustomQuery,
    C: CustomMsg,
    E: ToString,
{
    install_panic_handler();
    let res = _do_reply(
        reply_fn,
        env_ptr as *mut Region<Owned>,
        msg_ptr as *mut Region<Owned>,
    );
    let v = to_json_vec(&res).unwrap();
    Region::from_vec(v).to_heap_ptr() as u32
}

/// do_query should be wrapped in an external "C" export, containing a contract-specific function as arg
///
/// - `Q`: custom query type (see QueryRequest)
/// - `M`: message type for request
/// - `E`: error type for responses
pub fn do_query<Q, M, E>(
    query_fn: &dyn Fn(Deps<Q>, Env, M) -> Result<QueryResponse, E>,
    env_ptr: u32,
    msg_ptr: u32,
) -> u32
where
    Q: CustomQuery,
    M: DeserializeOwned,
    E: ToString,
{
    install_panic_handler();
    let res = _do_query(
        query_fn,
        env_ptr as *mut Region<Owned>,
        msg_ptr as *mut Region<Owned>,
    );
    let v = to_json_vec(&res).unwrap();
    Region::from_vec(v).to_heap_ptr() as u32
}

/// do_ibc_channel_open is designed for use with #[entry_point] to make a "C" extern
///
/// contract_fn does the protocol version negotiation during channel handshake phase
///
/// - `Q`: custom query type (see QueryRequest)
/// - `E`: error type for responses
#[cfg(feature = "stargate")]
pub fn do_ibc_channel_open<Q, E>(
    contract_fn: &dyn Fn(DepsMut<Q>, Env, IbcChannelOpenMsg) -> Result<IbcChannelOpenResponse, E>,
    env_ptr: u32,
    msg_ptr: u32,
) -> u32
where
    Q: CustomQuery,
    E: ToString,
{
    install_panic_handler();
    let res = _do_ibc_channel_open(
        contract_fn,
        env_ptr as *mut Region<Owned>,
        msg_ptr as *mut Region<Owned>,
    );
    let v = to_json_vec(&res).unwrap();
    Region::from_vec(v).to_heap_ptr() as u32
}

/// do_ibc_channel_connect is designed for use with #[entry_point] to make a "C" extern
///
/// contract_fn is a callback when a IBC channel is established (after both sides agree in open)
///
/// - `Q`: custom query type (see QueryRequest)
/// - `C`: custom response message type (see CosmosMsg)
/// - `E`: error type for responses
#[cfg(feature = "stargate")]
pub fn do_ibc_channel_connect<Q, C, E>(
    contract_fn: &dyn Fn(DepsMut<Q>, Env, IbcChannelConnectMsg) -> Result<IbcBasicResponse<C>, E>,
    env_ptr: u32,
    msg_ptr: u32,
) -> u32
where
    Q: CustomQuery,
    C: CustomMsg,
    E: ToString,
{
    install_panic_handler();
    let res = _do_ibc_channel_connect(
        contract_fn,
        env_ptr as *mut Region<Owned>,
        msg_ptr as *mut Region<Owned>,
    );
    let v = to_json_vec(&res).unwrap();
    Region::from_vec(v).to_heap_ptr() as u32
}

/// do_ibc_channel_close is designed for use with #[entry_point] to make a "C" extern
///
/// contract_fn is a callback when a IBC channel belonging to this contract is closed
///
/// - `Q`: custom query type (see QueryRequest)
/// - `C`: custom response message type (see CosmosMsg)
/// - `E`: error type for responses
#[cfg(feature = "stargate")]
pub fn do_ibc_channel_close<Q, C, E>(
    contract_fn: &dyn Fn(DepsMut<Q>, Env, IbcChannelCloseMsg) -> Result<IbcBasicResponse<C>, E>,
    env_ptr: u32,
    msg_ptr: u32,
) -> u32
where
    Q: CustomQuery,
    C: CustomMsg,
    E: ToString,
{
    install_panic_handler();
    let res = _do_ibc_channel_close(
        contract_fn,
        env_ptr as *mut Region<Owned>,
        msg_ptr as *mut Region<Owned>,
    );
    let v = to_json_vec(&res).unwrap();
    Region::from_vec(v).to_heap_ptr() as u32
}

/// do_ibc_packet_receive is designed for use with #[entry_point] to make a "C" extern
///
/// contract_fn is called when this chain receives an IBC Packet on a channel belonging
/// to this contract
///
/// - `Q`: custom query type (see QueryRequest)
/// - `C`: custom response message type (see CosmosMsg)
/// - `E`: error type for responses
#[cfg(feature = "stargate")]
pub fn do_ibc_packet_receive<Q, C, E>(
    contract_fn: &dyn Fn(DepsMut<Q>, Env, IbcPacketReceiveMsg) -> Result<IbcReceiveResponse<C>, E>,
    env_ptr: u32,
    msg_ptr: u32,
) -> u32
where
    Q: CustomQuery,
    C: CustomMsg,
    E: ToString,
{
    install_panic_handler();
    let res = _do_ibc_packet_receive(
        contract_fn,
        env_ptr as *mut Region<Owned>,
        msg_ptr as *mut Region<Owned>,
    );
    let v = to_json_vec(&res).unwrap();
    Region::from_vec(v).to_heap_ptr() as u32
}

/// do_ibc_packet_ack is designed for use with #[entry_point] to make a "C" extern
///
/// contract_fn is called when this chain receives an IBC Acknowledgement for a packet
/// that this contract previously sent
///
/// - `Q`: custom query type (see QueryRequest)
/// - `C`: custom response message type (see CosmosMsg)
/// - `E`: error type for responses
#[cfg(feature = "stargate")]
pub fn do_ibc_packet_ack<Q, C, E>(
    contract_fn: &dyn Fn(DepsMut<Q>, Env, IbcPacketAckMsg) -> Result<IbcBasicResponse<C>, E>,
    env_ptr: u32,
    msg_ptr: u32,
) -> u32
where
    Q: CustomQuery,
    C: CustomMsg,
    E: ToString,
{
    install_panic_handler();
    let res = _do_ibc_packet_ack(
        contract_fn,
        env_ptr as *mut Region<Owned>,
        msg_ptr as *mut Region<Owned>,
    );
    let v = to_json_vec(&res).unwrap();
    Region::from_vec(v).to_heap_ptr() as u32
}

/// do_ibc_packet_timeout is designed for use with #[entry_point] to make a "C" extern
///
/// contract_fn is called when a packet that this contract previously sent has provably
/// timedout and will never be relayed to the calling chain. This generally behaves
/// like ick_ack_fn upon an acknowledgement containing an error.
///
/// - `Q`: custom query type (see QueryRequest)
/// - `C`: custom response message type (see CosmosMsg)
/// - `E`: error type for responses
#[cfg(feature = "stargate")]
pub fn do_ibc_packet_timeout<Q, C, E>(
    contract_fn: &dyn Fn(DepsMut<Q>, Env, IbcPacketTimeoutMsg) -> Result<IbcBasicResponse<C>, E>,
    env_ptr: u32,
    msg_ptr: u32,
) -> u32
where
    Q: CustomQuery,
    C: CustomMsg,
    E: ToString,
{
    install_panic_handler();
    let res = _do_ibc_packet_timeout(
        contract_fn,
        env_ptr as *mut Region<Owned>,
        msg_ptr as *mut Region<Owned>,
    );
    let v = to_json_vec(&res).unwrap();
    Region::from_vec(v).to_heap_ptr() as u32
}

pub fn do_ibc_source_callback<Q, C, E>(
    contract_fn: &dyn Fn(DepsMut<Q>, Env, IbcSourceCallbackMsg) -> Result<IbcBasicResponse<C>, E>,
    env_ptr: u32,
    msg_ptr: u32,
) -> u32
where
    Q: CustomQuery,
    C: CustomMsg,
    E: ToString,
{
    install_panic_handler();
    let res = _do_ibc_source_callback(
        contract_fn,
        env_ptr as *mut Region<Owned>,
        msg_ptr as *mut Region<Owned>,
    );
    let v = to_json_vec(&res).unwrap();
    Region::from_vec(v).to_heap_ptr() as u32
}

pub fn do_ibc_destination_callback<Q, C, E>(
    contract_fn: &dyn Fn(
        DepsMut<Q>,
        Env,
        IbcDestinationCallbackMsg,
    ) -> Result<IbcBasicResponse<C>, E>,
    env_ptr: u32,
    msg_ptr: u32,
) -> u32
where
    Q: CustomQuery,
    C: CustomMsg,
    E: ToString,
{
    install_panic_handler();
    let res = _do_ibc_destination_callback(
        contract_fn,
        env_ptr as *mut Region<Owned>,
        msg_ptr as *mut Region<Owned>,
    );
    let v = to_json_vec(&res).unwrap();
    Region::from_vec(v).to_heap_ptr() as u32
}

fn _do_instantiate<Q, M, C, E>(
    instantiate_fn: &dyn Fn(DepsMut<Q>, Env, MessageInfo, M) -> Result<Response<C>, E>,
    env_ptr: *mut Region<Owned>,
    info_ptr: *mut Region<Owned>,
    msg_ptr: *mut Region<Owned>,
) -> ContractResult<Response<C>>
where
    Q: CustomQuery,
    M: DeserializeOwned,
    C: CustomMsg,
    E: ToString,
{
    let env: Vec<u8> = unsafe { Region::from_heap_ptr(env_ptr).into_vec() };
    let info: Vec<u8> = unsafe { Region::from_heap_ptr(info_ptr).into_vec() };
    let msg: Vec<u8> = unsafe { Region::from_heap_ptr(msg_ptr).into_vec() };

    let env: Env = try_into_contract_result!(from_json(env));
    let info: MessageInfo = try_into_contract_result!(from_json(info));
    let msg: M = try_into_contract_result!(from_json(msg));

    let mut deps = make_dependencies();
    instantiate_fn(deps.as_mut(), env, info, msg).into()
}

fn _do_execute<Q, M, C, E>(
    execute_fn: &dyn Fn(DepsMut<Q>, Env, MessageInfo, M) -> Result<Response<C>, E>,
    env_ptr: *mut Region<Owned>,
    info_ptr: *mut Region<Owned>,
    msg_ptr: *mut Region<Owned>,
) -> ContractResult<Response<C>>
where
    Q: CustomQuery,
    M: DeserializeOwned,
    C: CustomMsg,
    E: ToString,
{
    let env: Vec<u8> = unsafe { Region::from_heap_ptr(env_ptr).into_vec() };
    let info: Vec<u8> = unsafe { Region::from_heap_ptr(info_ptr).into_vec() };
    let msg: Vec<u8> = unsafe { Region::from_heap_ptr(msg_ptr).into_vec() };

    let env: Env = try_into_contract_result!(from_json(env));
    let info: MessageInfo = try_into_contract_result!(from_json(info));
    let msg: M = try_into_contract_result!(from_json(msg));

    let mut deps = make_dependencies();
    execute_fn(deps.as_mut(), env, info, msg).into()
}

fn _do_migrate<Q, M, C, E>(
    migrate_fn: &dyn Fn(DepsMut<Q>, Env, M) -> Result<Response<C>, E>,
    env_ptr: *mut Region<Owned>,
    msg_ptr: *mut Region<Owned>,
) -> ContractResult<Response<C>>
where
    Q: CustomQuery,
    M: DeserializeOwned,
    C: CustomMsg,
    E: ToString,
{
    let env: Vec<u8> = unsafe { Region::from_heap_ptr(env_ptr).into_vec() };
    let msg: Vec<u8> = unsafe { Region::from_heap_ptr(msg_ptr).into_vec() };

    let env: Env = try_into_contract_result!(from_json(env));
    let msg: M = try_into_contract_result!(from_json(msg));

    let mut deps = make_dependencies();
    migrate_fn(deps.as_mut(), env, msg).into()
}

fn _do_migrate_with_info<Q, M, C, E>(
    migrate_with_info_fn: &dyn Fn(DepsMut<Q>, Env, M, MigrateInfo) -> Result<Response<C>, E>,
    env_ptr: *mut Region<Owned>,
    msg_ptr: *mut Region<Owned>,
    migrate_info_ptr: *mut Region<Owned>,
) -> ContractResult<Response<C>>
where
    Q: CustomQuery,
    M: DeserializeOwned,
    C: CustomMsg,
    E: ToString,
{
    let env: Vec<u8> = unsafe { Region::from_heap_ptr(env_ptr).into_vec() };
    let msg: Vec<u8> = unsafe { Region::from_heap_ptr(msg_ptr).into_vec() };
    let migrate_info = unsafe { Region::from_heap_ptr(migrate_info_ptr).into_vec() };

    let env: Env = try_into_contract_result!(from_json(env));
    let msg: M = try_into_contract_result!(from_json(msg));
    let migrate_info: MigrateInfo = try_into_contract_result!(from_json(migrate_info));

    let mut deps = make_dependencies();
    migrate_with_info_fn(deps.as_mut(), env, msg, migrate_info).into()
}

fn _do_sudo<Q, M, C, E>(
    sudo_fn: &dyn Fn(DepsMut<Q>, Env, M) -> Result<Response<C>, E>,
    env_ptr: *mut Region<Owned>,
    msg_ptr: *mut Region<Owned>,
) -> ContractResult<Response<C>>
where
    Q: CustomQuery,
    M: DeserializeOwned,
    C: CustomMsg,
    E: ToString,
{
    let env: Vec<u8> = unsafe { Region::from_heap_ptr(env_ptr).into_vec() };
    let msg: Vec<u8> = unsafe { Region::from_heap_ptr(msg_ptr).into_vec() };

    let env: Env = try_into_contract_result!(from_json(env));
    let msg: M = try_into_contract_result!(from_json(msg));

    let mut deps = make_dependencies();
    sudo_fn(deps.as_mut(), env, msg).into()
}

fn _do_reply<Q, C, E>(
    reply_fn: &dyn Fn(DepsMut<Q>, Env, Reply) -> Result<Response<C>, E>,
    env_ptr: *mut Region<Owned>,
    msg_ptr: *mut Region<Owned>,
) -> ContractResult<Response<C>>
where
    Q: CustomQuery,
    C: CustomMsg,
    E: ToString,
{
    let env: Vec<u8> = unsafe { Region::from_heap_ptr(env_ptr).into_vec() };
    let msg: Vec<u8> = unsafe { Region::from_heap_ptr(msg_ptr).into_vec() };

    let env: Env = try_into_contract_result!(from_json(env));
    let msg: Reply = try_into_contract_result!(from_json(msg));

    let mut deps = make_dependencies();
    reply_fn(deps.as_mut(), env, msg).into()
}

fn _do_query<Q, M, E>(
    query_fn: &dyn Fn(Deps<Q>, Env, M) -> Result<QueryResponse, E>,
    env_ptr: *mut Region<Owned>,
    msg_ptr: *mut Region<Owned>,
) -> ContractResult<QueryResponse>
where
    Q: CustomQuery,
    M: DeserializeOwned,
    E: ToString,
{
    let env: Vec<u8> = unsafe { Region::from_heap_ptr(env_ptr).into_vec() };
    let msg: Vec<u8> = unsafe { Region::from_heap_ptr(msg_ptr).into_vec() };

    let env: Env = try_into_contract_result!(from_json(env));
    let msg: M = try_into_contract_result!(from_json(msg));

    let deps = make_dependencies();
    query_fn(deps.as_ref(), env, msg).into()
}

fn _do_ibc_channel_open<Q, E>(
    contract_fn: &dyn Fn(DepsMut<Q>, Env, IbcChannelOpenMsg) -> Result<IbcChannelOpenResponse, E>,
    env_ptr: *mut Region<Owned>,
    msg_ptr: *mut Region<Owned>,
) -> ContractResult<IbcChannelOpenResponse>
where
    Q: CustomQuery,
    E: ToString,
{
    let env: Vec<u8> = unsafe { Region::from_heap_ptr(env_ptr).into_vec() };
    let msg: Vec<u8> = unsafe { Region::from_heap_ptr(msg_ptr).into_vec() };

    let env: Env = try_into_contract_result!(from_json(env));
    let msg: IbcChannelOpenMsg = try_into_contract_result!(from_json(msg));

    let mut deps = make_dependencies();
    contract_fn(deps.as_mut(), env, msg).into()
}

#[cfg(feature = "stargate")]
fn _do_ibc_channel_connect<Q, C, E>(
    contract_fn: &dyn Fn(DepsMut<Q>, Env, IbcChannelConnectMsg) -> Result<IbcBasicResponse<C>, E>,
    env_ptr: *mut Region<Owned>,
    msg_ptr: *mut Region<Owned>,
) -> ContractResult<IbcBasicResponse<C>>
where
    Q: CustomQuery,
    C: CustomMsg,
    E: ToString,
{
    let env: Vec<u8> = unsafe { Region::from_heap_ptr(env_ptr).into_vec() };
    let msg: Vec<u8> = unsafe { Region::from_heap_ptr(msg_ptr).into_vec() };

    let env: Env = try_into_contract_result!(from_json(env));
    let msg: IbcChannelConnectMsg = try_into_contract_result!(from_json(msg));

    let mut deps = make_dependencies();
    contract_fn(deps.as_mut(), env, msg).into()
}

#[cfg(feature = "stargate")]
fn _do_ibc_channel_close<Q, C, E>(
    contract_fn: &dyn Fn(DepsMut<Q>, Env, IbcChannelCloseMsg) -> Result<IbcBasicResponse<C>, E>,
    env_ptr: *mut Region<Owned>,
    msg_ptr: *mut Region<Owned>,
) -> ContractResult<IbcBasicResponse<C>>
where
    Q: CustomQuery,
    C: CustomMsg,
    E: ToString,
{
    let env: Vec<u8> = unsafe { Region::from_heap_ptr(env_ptr).into_vec() };
    let msg: Vec<u8> = unsafe { Region::from_heap_ptr(msg_ptr).into_vec() };

    let env: Env = try_into_contract_result!(from_json(env));
    let msg: IbcChannelCloseMsg = try_into_contract_result!(from_json(msg));

    let mut deps = make_dependencies();
    contract_fn(deps.as_mut(), env, msg).into()
}

#[cfg(feature = "stargate")]
fn _do_ibc_packet_receive<Q, C, E>(
    contract_fn: &dyn Fn(DepsMut<Q>, Env, IbcPacketReceiveMsg) -> Result<IbcReceiveResponse<C>, E>,
    env_ptr: *mut Region<Owned>,
    msg_ptr: *mut Region<Owned>,
) -> ContractResult<IbcReceiveResponse<C>>
where
    Q: CustomQuery,
    C: CustomMsg,
    E: ToString,
{
    let env: Vec<u8> = unsafe { Region::from_heap_ptr(env_ptr).into_vec() };
    let msg: Vec<u8> = unsafe { Region::from_heap_ptr(msg_ptr).into_vec() };

    let env: Env = try_into_contract_result!(from_json(env));
    let msg: IbcPacketReceiveMsg = try_into_contract_result!(from_json(msg));

    let mut deps = make_dependencies();
    contract_fn(deps.as_mut(), env, msg).into()
}

#[cfg(feature = "stargate")]
fn _do_ibc_packet_ack<Q, C, E>(
    contract_fn: &dyn Fn(DepsMut<Q>, Env, IbcPacketAckMsg) -> Result<IbcBasicResponse<C>, E>,
    env_ptr: *mut Region<Owned>,
    msg_ptr: *mut Region<Owned>,
) -> ContractResult<IbcBasicResponse<C>>
where
    Q: CustomQuery,
    C: CustomMsg,
    E: ToString,
{
    let env: Vec<u8> = unsafe { Region::from_heap_ptr(env_ptr).into_vec() };
    let msg: Vec<u8> = unsafe { Region::from_heap_ptr(msg_ptr).into_vec() };

    let env: Env = try_into_contract_result!(from_json(env));
    let msg: IbcPacketAckMsg = try_into_contract_result!(from_json(msg));

    let mut deps = make_dependencies();
    contract_fn(deps.as_mut(), env, msg).into()
}

#[cfg(feature = "stargate")]
fn _do_ibc_packet_timeout<Q, C, E>(
    contract_fn: &dyn Fn(DepsMut<Q>, Env, IbcPacketTimeoutMsg) -> Result<IbcBasicResponse<C>, E>,
    env_ptr: *mut Region<Owned>,
    msg_ptr: *mut Region<Owned>,
) -> ContractResult<IbcBasicResponse<C>>
where
    Q: CustomQuery,
    C: CustomMsg,
    E: ToString,
{
    let env: Vec<u8> = unsafe { Region::from_heap_ptr(env_ptr).into_vec() };
    let msg: Vec<u8> = unsafe { Region::from_heap_ptr(msg_ptr).into_vec() };

    let env: Env = try_into_contract_result!(from_json(env));
    let msg: IbcPacketTimeoutMsg = try_into_contract_result!(from_json(msg));

    let mut deps = make_dependencies();
    contract_fn(deps.as_mut(), env, msg).into()
}

fn _do_ibc_source_callback<Q, C, E>(
    contract_fn: &dyn Fn(DepsMut<Q>, Env, IbcSourceCallbackMsg) -> Result<IbcBasicResponse<C>, E>,
    env_ptr: *mut Region<Owned>,
    msg_ptr: *mut Region<Owned>,
) -> ContractResult<IbcBasicResponse<C>>
where
    Q: CustomQuery,
    C: CustomMsg,
    E: ToString,
{
    let env: Vec<u8> = unsafe { Region::from_heap_ptr(env_ptr).into_vec() };
    let msg: Vec<u8> = unsafe { Region::from_heap_ptr(msg_ptr).into_vec() };

    let env: Env = try_into_contract_result!(from_json(env));
    let msg: IbcSourceCallbackMsg = try_into_contract_result!(from_json(msg));

    let mut deps = make_dependencies();
    contract_fn(deps.as_mut(), env, msg).into()
}

fn _do_ibc_destination_callback<Q, C, E>(
    contract_fn: &dyn Fn(
        DepsMut<Q>,
        Env,
        IbcDestinationCallbackMsg,
    ) -> Result<IbcBasicResponse<C>, E>,
    env_ptr: *mut Region<Owned>,
    msg_ptr: *mut Region<Owned>,
) -> ContractResult<IbcBasicResponse<C>>
where
    Q: CustomQuery,
    C: CustomMsg,
    E: ToString,
{
    let env: Vec<u8> = unsafe { Region::from_heap_ptr(env_ptr).into_vec() };
    let msg: Vec<u8> = unsafe { Region::from_heap_ptr(msg_ptr).into_vec() };

    let env: Env = try_into_contract_result!(from_json(env));
    let msg: IbcDestinationCallbackMsg = try_into_contract_result!(from_json(msg));

    let mut deps = make_dependencies();
    contract_fn(deps.as_mut(), env, msg).into()
}

/// Makes all bridges to external dependencies (i.e. Wasm imports) that are injected by the VM
pub(crate) fn make_dependencies<Q>() -> OwnedDeps<ExternalStorage, ExternalApi, ExternalQuerier, Q>
where
    Q: CustomQuery,
{
    OwnedDeps {
        storage: ExternalStorage::new(),
        api: ExternalApi::new(),
        querier: ExternalQuerier::new(),
        custom_query_type: PhantomData,
    }
}