multiversx_sc_scenario/facade/
contract_info.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
use std::ops::{Deref, DerefMut};

use multiversx_sc::{
    abi::TypeAbiFrom,
    types::{AnnotatedValue, ManagedBuffer, TxEnv, TxFrom, TxFromSpecified, TxTo, TxToSpecified},
};

use crate::multiversx_sc::{
    api::ManagedTypeApi,
    codec::{EncodeErrorHandler, TopEncode, TopEncodeOutput},
    contract_base::ProxyObjNew,
    types::{Address, ManagedAddress},
};

use crate::scenario::model::{AddressKey, AddressValue};

/// Bundles a representation of a contract with the contract proxy,
/// so that it can be easily called in the context of a blockchain mock.
pub struct ContractInfo<P: ProxyObjNew> {
    pub scenario_address_expr: AddressKey,
    proxy_inst: P::ProxyTo,
}

impl<P> ContractInfo<P>
where
    P: ProxyObjNew,
{
    pub fn new<A>(address_expr: A) -> Self
    where
        AddressKey: From<A>,
    {
        let mandos_address_expr = AddressKey::from(address_expr);
        let proxy_inst = P::new_proxy_obj().contract(mandos_address_expr.value.clone().into());
        ContractInfo {
            scenario_address_expr: mandos_address_expr,
            proxy_inst,
        }
    }

    pub fn to_address(&self) -> Address {
        self.scenario_address_expr.to_address()
    }

    /// For historical reasons the proxy consumes its address whenever it is called.
    ///
    /// When using it in tests, as part of `ContractInfo`,
    /// it is convenient to refresh it before each call.
    ///
    /// It is sort of a hack, designed to optimize proxy use in contracts,
    /// while making it easier to use in tests.
    fn refresh_proxy_address(&mut self) {
        self.proxy_inst =
            P::new_proxy_obj().contract(self.scenario_address_expr.value.clone().into());
    }
}

impl<P: ProxyObjNew> From<&ContractInfo<P>> for AddressKey {
    fn from(from: &ContractInfo<P>) -> Self {
        from.scenario_address_expr.clone()
    }
}

impl<P: ProxyObjNew> From<ContractInfo<P>> for AddressKey {
    fn from(from: ContractInfo<P>) -> Self {
        from.scenario_address_expr
    }
}

impl<P: ProxyObjNew> From<&ContractInfo<P>> for AddressValue {
    fn from(from: &ContractInfo<P>) -> Self {
        AddressValue::from(&from.scenario_address_expr)
    }
}

impl<P: ProxyObjNew> From<ContractInfo<P>> for AddressValue {
    fn from(from: ContractInfo<P>) -> Self {
        AddressValue::from(&from.scenario_address_expr)
    }
}

impl<P: ProxyObjNew> Deref for ContractInfo<P> {
    type Target = P::ProxyTo;
    fn deref(&self) -> &Self::Target {
        &self.proxy_inst
    }
}

impl<P: ProxyObjNew> DerefMut for ContractInfo<P> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.refresh_proxy_address();
        &mut self.proxy_inst
    }
}

impl<P: ProxyObjNew> TopEncode for ContractInfo<P> {
    fn top_encode_or_handle_err<O, H>(&self, output: O, h: H) -> Result<(), H::HandledErr>
    where
        O: TopEncodeOutput,
        H: EncodeErrorHandler,
    {
        self.scenario_address_expr
            .value
            .top_encode_or_handle_err(output, h)
    }
}

impl<P: ProxyObjNew> TypeAbiFrom<ContractInfo<P>> for Address {}
impl<P: ProxyObjNew> TypeAbiFrom<&ContractInfo<P>> for Address {}
impl<M: ManagedTypeApi, P: ProxyObjNew> TypeAbiFrom<ContractInfo<P>> for ManagedAddress<M> {}
impl<M: ManagedTypeApi, P: ProxyObjNew> TypeAbiFrom<&ContractInfo<P>> for ManagedAddress<M> {}

impl<Env, P> AnnotatedValue<Env, ManagedAddress<Env::Api>> for &ContractInfo<P>
where
    Env: TxEnv,
    P: ProxyObjNew,
{
    fn annotation(&self, _env: &Env) -> ManagedBuffer<Env::Api> {
        self.scenario_address_expr.original.as_str().into()
    }

    fn to_value(&self, _env: &Env) -> ManagedAddress<Env::Api> {
        (&self.scenario_address_expr.value).into()
    }
}

impl<P, Env> TxFrom<Env> for &ContractInfo<P>
where
    Env: TxEnv,
    P: ProxyObjNew,
{
    fn resolve_address(&self, _env: &Env) -> ManagedAddress<Env::Api> {
        (&self.scenario_address_expr.value).into()
    }
}
impl<P, Env> TxFromSpecified<Env> for &ContractInfo<P>
where
    Env: TxEnv,
    P: ProxyObjNew,
{
}
impl<P, Env> TxTo<Env> for &ContractInfo<P>
where
    Env: TxEnv,
    P: ProxyObjNew,
{
}
impl<P, Env> TxToSpecified<Env> for &ContractInfo<P>
where
    Env: TxEnv,
    P: ProxyObjNew,
{
}