multiversx_sc_scenario/facade/
contract_info.rs1use std::ops::{Deref, DerefMut};
2
3use multiversx_sc::{
4 abi::TypeAbiFrom,
5 types::{AnnotatedValue, ManagedBuffer, TxEnv, TxFrom, TxFromSpecified, TxTo, TxToSpecified},
6};
7
8use crate::multiversx_sc::{
9 api::ManagedTypeApi,
10 codec::{EncodeErrorHandler, TopEncode, TopEncodeOutput},
11 contract_base::ProxyObjNew,
12 types::{Address, ManagedAddress},
13};
14
15use crate::scenario::model::{AddressKey, AddressValue};
16
17pub struct ContractInfo<P: ProxyObjNew> {
20 pub scenario_address_expr: AddressKey,
21 proxy_inst: P::ProxyTo,
22}
23
24impl<P> ContractInfo<P>
25where
26 P: ProxyObjNew,
27{
28 pub fn new<A>(address_expr: A) -> Self
29 where
30 AddressKey: From<A>,
31 {
32 let mandos_address_expr = AddressKey::from(address_expr);
33 let proxy_inst = P::new_proxy_obj().contract(mandos_address_expr.value.clone().into());
34 ContractInfo {
35 scenario_address_expr: mandos_address_expr,
36 proxy_inst,
37 }
38 }
39
40 pub fn to_address(&self) -> Address {
41 self.scenario_address_expr.to_address()
42 }
43
44 fn refresh_proxy_address(&mut self) {
52 self.proxy_inst =
53 P::new_proxy_obj().contract(self.scenario_address_expr.value.clone().into());
54 }
55}
56
57impl<P: ProxyObjNew> From<&ContractInfo<P>> for AddressKey {
58 fn from(from: &ContractInfo<P>) -> Self {
59 from.scenario_address_expr.clone()
60 }
61}
62
63impl<P: ProxyObjNew> From<ContractInfo<P>> for AddressKey {
64 fn from(from: ContractInfo<P>) -> Self {
65 from.scenario_address_expr
66 }
67}
68
69impl<P: ProxyObjNew> From<&ContractInfo<P>> for AddressValue {
70 fn from(from: &ContractInfo<P>) -> Self {
71 AddressValue::from(&from.scenario_address_expr)
72 }
73}
74
75impl<P: ProxyObjNew> From<ContractInfo<P>> for AddressValue {
76 fn from(from: ContractInfo<P>) -> Self {
77 AddressValue::from(&from.scenario_address_expr)
78 }
79}
80
81impl<P: ProxyObjNew> Deref for ContractInfo<P> {
82 type Target = P::ProxyTo;
83 fn deref(&self) -> &Self::Target {
84 &self.proxy_inst
85 }
86}
87
88impl<P: ProxyObjNew> DerefMut for ContractInfo<P> {
89 fn deref_mut(&mut self) -> &mut Self::Target {
90 self.refresh_proxy_address();
91 &mut self.proxy_inst
92 }
93}
94
95impl<P: ProxyObjNew> TopEncode for ContractInfo<P> {
96 fn top_encode_or_handle_err<O, H>(&self, output: O, h: H) -> Result<(), H::HandledErr>
97 where
98 O: TopEncodeOutput,
99 H: EncodeErrorHandler,
100 {
101 self.scenario_address_expr
102 .value
103 .top_encode_or_handle_err(output, h)
104 }
105}
106
107impl<P: ProxyObjNew> TypeAbiFrom<ContractInfo<P>> for Address {}
108impl<P: ProxyObjNew> TypeAbiFrom<&ContractInfo<P>> for Address {}
109impl<M: ManagedTypeApi, P: ProxyObjNew> TypeAbiFrom<ContractInfo<P>> for ManagedAddress<M> {}
110impl<M: ManagedTypeApi, P: ProxyObjNew> TypeAbiFrom<&ContractInfo<P>> for ManagedAddress<M> {}
111
112impl<Env, P> AnnotatedValue<Env, ManagedAddress<Env::Api>> for &ContractInfo<P>
113where
114 Env: TxEnv,
115 P: ProxyObjNew,
116{
117 fn annotation(&self, _env: &Env) -> ManagedBuffer<Env::Api> {
118 self.scenario_address_expr.original.as_str().into()
119 }
120
121 fn to_value(&self, _env: &Env) -> ManagedAddress<Env::Api> {
122 (&self.scenario_address_expr.value).into()
123 }
124}
125
126impl<P, Env> TxFrom<Env> for &ContractInfo<P>
127where
128 Env: TxEnv,
129 P: ProxyObjNew,
130{
131 fn resolve_address(&self, _env: &Env) -> ManagedAddress<Env::Api> {
132 (&self.scenario_address_expr.value).into()
133 }
134}
135impl<P, Env> TxFromSpecified<Env> for &ContractInfo<P>
136where
137 Env: TxEnv,
138 P: ProxyObjNew,
139{
140}
141impl<P, Env> TxTo<Env> for &ContractInfo<P>
142where
143 Env: TxEnv,
144 P: ProxyObjNew,
145{
146}
147impl<P, Env> TxToSpecified<Env> for &ContractInfo<P>
148where
149 Env: TxEnv,
150 P: ProxyObjNew,
151{
152}