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
use std::ops::{Deref, DerefMut};

use multiversx_sc::{
    api::ManagedTypeApi,
    codec::{CodecFrom, EncodeErrorHandler, TopEncode, TopEncodeOutput},
    contract_base::ProxyObjBase,
    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: ProxyObjBase> {
    pub scenario_address_expr: AddressKey,
    proxy_inst: P,
}

impl<P: ProxyObjBase> ContractInfo<P> {
    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()
    }
}

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

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

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

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

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

impl<P: ProxyObjBase> DerefMut for ContractInfo<P> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        let proxy_inst = core::mem::replace(&mut self.proxy_inst, P::new_proxy_obj());
        let proxy_inst = proxy_inst.contract(self.scenario_address_expr.value.clone().into());
        let _ = core::mem::replace(&mut self.proxy_inst, proxy_inst);
        &mut self.proxy_inst
    }
}

impl<P: ProxyObjBase> 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: ProxyObjBase> CodecFrom<ContractInfo<P>> for Address {}
impl<P: ProxyObjBase> CodecFrom<&ContractInfo<P>> for Address {}
impl<M: ManagedTypeApi, P: ProxyObjBase> CodecFrom<ContractInfo<P>> for ManagedAddress<M> {}
impl<M: ManagedTypeApi, P: ProxyObjBase> CodecFrom<&ContractInfo<P>> for ManagedAddress<M> {}