diff --git a/framework/scenario/tests/contract_without_macros.rs b/framework/scenario/tests/contract_without_macros.rs index 91ca8b2eba..1121e88fdd 100644 --- a/framework/scenario/tests/contract_without_macros.rs +++ b/framework/scenario/tests/contract_without_macros.rs @@ -7,7 +7,6 @@ // and maintenance. #![allow(unused)] -#![allow(deprecated)] // TODO: unified syntax use multiversx_sc::{ contract_base::ProxyObjNew, @@ -103,11 +102,116 @@ mod module_1 { } pub trait ProxyTrait: multiversx_sc::contract_base::ProxyObjBase + Sized { + #[allow(clippy::too_many_arguments)] + #[allow(clippy::type_complexity)] fn version( &mut self, - ) -> multiversx_sc::types::ContractCallNoPayment> { - let ___address___ = self.extract_address(); - multiversx_sc::types::ContractCallNoPayment::new(___address___, "version") + ) -> multiversx_sc::types::Tx< + multiversx_sc::types::TxScEnv, + (), + Self::To, + (), + (), + multiversx_sc::types::FunctionCall, + multiversx_sc::types::OriginalResultMarker>, + > { + multiversx_sc::types::TxBaseWithEnv::new_tx_from_sc() + .to(self.extract_proxy_to()) + .original_result() + .raw_call("version") + } + } +} + +mod sampler_adder_proxy { + #![allow(dead_code)] + #![allow(clippy::all)] + use multiversx_sc::proxy_imports::*; + pub struct AdderProxy; + impl TxProxyTrait for AdderProxy + where + Env: TxEnv, + From: TxFrom, + To: TxTo, + Gas: TxGas, + { + type TxProxyMethods = AdderProxyMethods; + fn proxy_methods(self, tx: Tx) -> Self::TxProxyMethods { + AdderProxyMethods { wrapped_tx: tx } + } + } + pub struct AdderProxyMethods + where + Env: TxEnv, + From: TxFrom, + To: TxTo, + Gas: TxGas, + { + wrapped_tx: Tx, + } + #[rustfmt::skip] + impl AdderProxyMethods + where + Env: TxEnv, + Env::Api: VMApi, + From: TxFrom, + Gas: TxGas, + { + pub fn init>>( + self, + initial_value: Arg0, + ) -> TxTypedDeploy { + self.wrapped_tx + .payment(NotPayable) + .raw_deploy() + .argument(&initial_value) + .original_result() + } + } + #[rustfmt::skip] + impl AdderProxyMethods + where + Env: TxEnv, + Env::Api: VMApi, + From: TxFrom, + To: TxTo, + Gas: TxGas, + { + pub fn upgrade>>( + self, + initial_value: Arg0, + ) -> TxTypedUpgrade { + self.wrapped_tx + .payment(NotPayable) + .raw_upgrade() + .argument(&initial_value) + .original_result() + } + } + #[rustfmt::skip] + impl AdderProxyMethods + where + Env: TxEnv, + Env::Api: VMApi, + From: TxFrom, + To: TxTo, + Gas: TxGas, + { + pub fn sum( + self, + ) -> TxTypedCall> { + self.wrapped_tx.payment(NotPayable).raw_call("getSum").original_result() + } + /// Add desired amount to the storage variable. + pub fn add>>( + self, + value: Arg0, + ) -> TxTypedCall { + self.wrapped_tx + .payment(NotPayable) + .raw_call("add") + .argument(&value) + .original_result() } } } @@ -121,21 +225,25 @@ mod sample_adder { pub trait Adder: super::module_1::VersionModule + multiversx_sc::contract_base::ContractBase + Sized { - fn init(&self, initial_value: &BigInt) { - self.set_sum(initial_value); - } - fn add(&self, value: BigInt) { - let mut sum = self.get_sum(); - sum.add_assign(value); - self.set_sum(&sum); - } - fn get_sum(&self) -> BigInt; - fn set_sum(&self, sum: &BigInt); - fn add_version(&self) { - self.add(self.version()) - } - fn callback(&self); - fn callbacks(&self) -> self::CallbackProxyObj; + #[allow(clippy::too_many_arguments)] + #[allow(clippy::type_complexity)] + fn init(&self, initial_value: multiversx_sc::types::BigUint) { + self.sum().set(initial_value); + } + #[allow(clippy::too_many_arguments)] + #[allow(clippy::type_complexity)] + fn upgrade(&self, initial_value: multiversx_sc::types::BigUint) { + self.init(initial_value); + } + /// Add desired amount to the storage variable. + #[allow(clippy::too_many_arguments)] + #[allow(clippy::type_complexity)] + fn add(&self, value: multiversx_sc::types::BigUint) { + self.sum().update(|sum| *sum += value); + } + #[allow(clippy::too_many_arguments)] + #[allow(clippy::type_complexity)] + fn sum(&self) -> SingleValueMapper>; } ///////////////////////////////////////////////////////////////////////////////////////////////// @@ -149,17 +257,14 @@ mod sample_adder { where C: AutoImpl + super::module_1::AutoImpl, { - fn get_sum(&self) -> BigInt { - let mut ___key___ = multiversx_sc::storage::StorageKey::::new(&b"sum"[..]); - multiversx_sc::storage_get(multiversx_sc::types::ManagedRef::new(&___key___)) - } - fn set_sum(&self, sum: &BigInt) { + #[allow(clippy::too_many_arguments)] + #[allow(clippy::type_complexity)] + fn sum(&self) -> SingleValueMapper> { let mut ___key___ = multiversx_sc::storage::StorageKey::::new(&b"sum"[..]); - multiversx_sc::storage_set(multiversx_sc::types::ManagedRef::new(&___key___), &sum); - } - fn callback(&self) {} - fn callbacks(&self) -> self::CallbackProxyObj { - as multiversx_sc::contract_base::CallbackProxyObjBase>::new_cb_proxy_obj() + , + > as multiversx_sc::storage::mappers::StorageMapper>::new(___key___) } } @@ -172,11 +277,11 @@ mod sample_adder { Adder + multiversx_sc::contract_base::ContractBase + super::module_1::EndpointWrappers { #[inline] - fn call_get_sum(&self) { + fn call_sum(&self) { ::init_static(); multiversx_sc::io::call_value_init::not_payable::(); let () = multiversx_sc::io::load_endpoint_args::(()); - let result = self.get_sum(); + let result = self.sum(); multiversx_sc::io::finish_multi::(&result); } #[inline] @@ -185,9 +290,19 @@ mod sample_adder { multiversx_sc::io::call_value_init::not_payable::(); let (initial_value, ()) = multiversx_sc::io::load_endpoint_args::< Self::Api, - (multiversx_sc::types::BigInt, ()), + (multiversx_sc::types::BigUint, ()), >(("initial_value", ())); - self.init(&initial_value); + self.init(initial_value); + } + #[inline] + fn call_upgrade(&self) { + ::init_static(); + multiversx_sc::io::call_value_init::not_payable::(); + let (initial_value, ()) = multiversx_sc::io::load_endpoint_args::< + Self::Api, + (multiversx_sc::types::BigUint, ()), + >(("initial_value", ())); + self.upgrade(initial_value); } #[inline] fn call_add(&self) { @@ -195,38 +310,55 @@ mod sample_adder { multiversx_sc::io::call_value_init::not_payable::(); let (value, ()) = multiversx_sc::io::load_endpoint_args::< Self::Api, - (multiversx_sc::types::BigInt, ()), + (multiversx_sc::types::BigUint, ()), >(("value", ())); self.add(value); } - fn call(&self, fn_name: &str) -> bool { if match fn_name { "callBack" => { - Adder::callback(self); + self::EndpointWrappers::callback(self); + return true; + }, + "init" + if ::external_view_init_override() => + { + multiversx_sc::external_view_contract::external_view_contract_constructor::< + Self::Api, + >(); return true; }, "getSum" => { - self.call_get_sum(); + self.call_sum(); true }, - "init" => { + "init" + if !::external_view_init_override() => + { self.call_init(); true }, + "upgrade" => { + self.call_upgrade(); + true + }, "add" => { self.call_add(); true }, - _other => false, + other => false, } { return true; } - if super::module_1::EndpointWrappers::call(self, fn_name) { - return true; - } false } + fn callback_selector( + &self, + mut ___cb_closure___: multiversx_sc::types::CallbackClosureForDeser, + ) -> multiversx_sc::types::CallbackSelectorResult { + multiversx_sc::types::CallbackSelectorResult::NotProcessed(___cb_closure___) + } + fn callback(&self) {} } impl EndpointWrappers for multiversx_sc::contract_base::UniversalContractObj where @@ -234,24 +366,224 @@ mod sample_adder { { } + pub struct AbiProvider {} + impl multiversx_sc::contract_base::ContractAbiProvider for AbiProvider { + type Api = multiversx_sc::api::uncallable::UncallableApi; + fn abi() -> multiversx_sc::abi::ContractAbi { + let mut contract_abi = multiversx_sc::abi::ContractAbi::new( + multiversx_sc::abi::BuildInfoAbi { + contract_crate: multiversx_sc::abi::ContractCrateBuildAbi { + name: "adder", + version: "0.0.0", + git_version: "", + }, + framework: multiversx_sc::abi::FrameworkBuildAbi::create(), + }, + &[ + "One of the simplest smart contracts possible,", + "it holds a single variable in storage, which anyone can increment.", + ], + "Adder", + false, + ); + let mut endpoint_abi = multiversx_sc::abi::EndpointAbi::new( + &[], + "getSum", + "sum", + false, + false, + multiversx_sc::abi::EndpointMutabilityAbi::Readonly, + multiversx_sc::abi::EndpointTypeAbi::Endpoint, + &[], + &[], + false, + ); + endpoint_abi + .add_output::< + SingleValueMapper>, + >(&[]); + contract_abi + .add_type_descriptions::< + SingleValueMapper>, + >(); + contract_abi.endpoints.push(endpoint_abi); + let mut endpoint_abi = multiversx_sc::abi::EndpointAbi::new( + &[], + "init", + "init", + false, + false, + multiversx_sc::abi::EndpointMutabilityAbi::Mutable, + multiversx_sc::abi::EndpointTypeAbi::Init, + &[], + &[], + false, + ); + endpoint_abi.add_input::>("initial_value"); + contract_abi.add_type_descriptions::>(); + contract_abi.constructors.push(endpoint_abi); + let mut endpoint_abi = multiversx_sc::abi::EndpointAbi::new( + &[], + "upgrade", + "upgrade", + false, + false, + multiversx_sc::abi::EndpointMutabilityAbi::Mutable, + multiversx_sc::abi::EndpointTypeAbi::Upgrade, + &[], + &[], + false, + ); + endpoint_abi.add_input::>("initial_value"); + contract_abi.add_type_descriptions::>(); + contract_abi.upgrade_constructors.push(endpoint_abi); + let mut endpoint_abi = multiversx_sc::abi::EndpointAbi::new( + &["Add desired amount to the storage variable."], + "add", + "add", + false, + false, + multiversx_sc::abi::EndpointMutabilityAbi::Mutable, + multiversx_sc::abi::EndpointTypeAbi::Endpoint, + &[], + &[], + false, + ); + endpoint_abi.add_input::>("value"); + contract_abi.add_type_descriptions::>(); + contract_abi.endpoints.push(endpoint_abi); + contract_abi + } + } + + #[allow(non_snake_case)] + pub mod endpoints { + use super::EndpointWrappers; + pub fn sum() + where + A: multiversx_sc::api::VMApi, + { + super::EndpointWrappers::call_sum( + &multiversx_sc::contract_base::UniversalContractObj::::new(), + ); + } + pub fn init() + where + A: multiversx_sc::api::VMApi, + { + super::EndpointWrappers::call_init( + &multiversx_sc::contract_base::UniversalContractObj::::new(), + ); + } + pub fn upgrade() + where + A: multiversx_sc::api::VMApi, + { + super::EndpointWrappers::call_upgrade( + &multiversx_sc::contract_base::UniversalContractObj::::new(), + ); + } + pub fn add() + where + A: multiversx_sc::api::VMApi, + { + super::EndpointWrappers::call_add( + &multiversx_sc::contract_base::UniversalContractObj::::new(), + ); + } + pub fn callBack() + where + A: multiversx_sc::api::VMApi, + { + super::EndpointWrappers::callback( + &multiversx_sc::contract_base::UniversalContractObj::::new(), + ); + } + } pub trait ProxyTrait: multiversx_sc::contract_base::ProxyObjBase + super::module_1::ProxyTrait { - fn get_sum( + #[allow(clippy::too_many_arguments)] + #[allow(clippy::type_complexity)] + fn sum( &mut self, - ) -> multiversx_sc::types::ContractCallNoPayment> { - let ___address___ = self.extract_address(); - multiversx_sc::types::ContractCallNoPayment::new(___address___, "get_sum") - } - fn add( + ) -> multiversx_sc::types::Tx< + multiversx_sc::types::TxScEnv, + (), + Self::To, + (), + (), + multiversx_sc::types::FunctionCall, + multiversx_sc::types::OriginalResultMarker< + SingleValueMapper>, + >, + > { + multiversx_sc::types::TxBaseWithEnv::new_tx_from_sc() + .to(self.extract_proxy_to()) + .original_result() + .raw_call("getSum") + } + #[allow(clippy::too_many_arguments)] + #[allow(clippy::type_complexity)] + fn init>>( &mut self, - amount: &BigInt, - ) -> multiversx_sc::types::ContractCallNoPayment { - let ___address___ = self.extract_address(); - let mut ___contract_call___ = - multiversx_sc::types::ContractCallNoPayment::new(___address___, "add"); - multiversx_sc::types::ContractCall::proxy_arg(&mut ___contract_call___, amount); - ___contract_call___ + initial_value: Arg0, + ) -> multiversx_sc::types::Tx< + multiversx_sc::types::TxScEnv, + (), + Self::To, + (), + (), + multiversx_sc::types::DeployCall, ()>, + multiversx_sc::types::OriginalResultMarker<()>, + > { + multiversx_sc::types::TxBaseWithEnv::new_tx_from_sc() + .raw_deploy() + .argument(&initial_value) + .original_result() + .to(self.extract_proxy_to()) + } + #[allow(clippy::too_many_arguments)] + #[allow(clippy::type_complexity)] + fn upgrade< + Arg0: multiversx_sc::types::ProxyArg>, + >( + &mut self, + initial_value: Arg0, + ) -> multiversx_sc::types::Tx< + multiversx_sc::types::TxScEnv, + (), + Self::To, + (), + (), + multiversx_sc::types::FunctionCall, + multiversx_sc::types::OriginalResultMarker<()>, + > { + multiversx_sc::types::TxBaseWithEnv::new_tx_from_sc() + .to(self.extract_proxy_to()) + .original_result() + .raw_call("upgrade") + .argument(&initial_value) + } + #[allow(clippy::too_many_arguments)] + #[allow(clippy::type_complexity)] + fn add>>( + &mut self, + value: Arg0, + ) -> multiversx_sc::types::Tx< + multiversx_sc::types::TxScEnv, + (), + Self::To, + (), + (), + multiversx_sc::types::FunctionCall, + multiversx_sc::types::OriginalResultMarker<()>, + > { + multiversx_sc::types::TxBaseWithEnv::new_tx_from_sc() + .to(self.extract_proxy_to()) + .original_result() + .raw_call("add") + .argument(&value) } } @@ -288,17 +620,21 @@ mod sample_adder { A: multiversx_sc::api::VMApi, { fn call(&self, fn_name: &str) -> bool { - EndpointWrappers::call( - &multiversx_sc::contract_base::UniversalContractObj::::new(), - fn_name, - ) + EndpointWrappers::call(self, fn_name) } } + pub fn contract_obj() -> ContractObj + where + A: multiversx_sc::api::VMApi, + { + ContractObj { + _phantom: core::marker::PhantomData, + } + } pub struct ContractBuilder; - - impl multiversx_sc::contract_base::CallableContractBuilder for ContractBuilder { - fn new_contract_obj( + impl multiversx_sc::contract_base::CallableContractBuilder for self::ContractBuilder { + fn new_contract_obj( &self, ) -> multiversx_sc::types::heap::Box { @@ -308,25 +644,6 @@ mod sample_adder { } } - pub struct AbiProvider {} - - impl multiversx_sc::contract_base::ContractAbiProvider for AbiProvider { - type Api = multiversx_sc::api::uncallable::UncallableApi; - - fn abi() -> multiversx_sc::abi::ContractAbi { - multiversx_sc::abi::ContractAbi::default() - } - } - - pub fn contract_obj() -> ContractObj - where - A: multiversx_sc::api::VMApi, - { - ContractObj { - _phantom: core::marker::PhantomData, - } - } - pub struct Proxy where A: multiversx_sc::api::VMApi + 'static, @@ -465,27 +782,24 @@ fn contract_without_macros_basic() { let adder = sample_adder::contract_obj::(); - adder.init(&BigInt::from(5)); - assert_eq!(BigInt::from(5), adder.get_sum()); + adder.init(multiversx_sc::types::BigUint::from(5u32)); + assert_eq!(multiversx_sc::types::BigUint::from(5u32), adder.sum().get()); - adder.add(BigInt::from(7)); - assert_eq!(BigInt::from(12), adder.get_sum()); - - adder.add(BigInt::from(-1)); - assert_eq!(BigInt::from(11), adder.get_sum()); + adder.add(multiversx_sc::types::BigUint::from(7u32)); + assert_eq!( + multiversx_sc::types::BigUint::from(12u32), + adder.sum().get() + ); assert_eq!(BigInt::from(100), adder.version()); - adder.add_version(); - assert_eq!(BigInt::from(111), adder.get_sum()); - assert!(!adder.call("invalid_endpoint")); - assert!(adder.call("version")); + assert!(adder.call("getSum")); let mut own_proxy = sample_adder::Proxy::::new_proxy_obj().contract(ManagedAddress::zero()); - let _ = own_proxy.get_sum(); + let _ = own_proxy.sum(); let _ = multiversx_sc_meta_lib::abi_json::contract_abi::(); }