From 09521e404062b2ee1314be6918f0a766e80fe8ce Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Wed, 27 Mar 2024 06:59:20 +0200 Subject: [PATCH 1/3] AnnotatedValue refactor --- .../base/src/types/interaction/annotated.rs | 104 ++++++++++++++++-- .../types/interaction/expr/address_expr.rs | 17 +-- .../src/types/interaction/expr/sc_expr.rs | 17 +-- .../src/types/interaction/tx_call_deploy.rs | 4 +- .../base/src/types/interaction/tx_call_te.rs | 4 +- .../src/types/interaction/tx_call_upgrade.rs | 8 +- .../base/src/types/interaction/tx_payment.rs | 17 ++- .../interaction/tx_payment/tx_payment_egld.rs | 38 +++++-- .../tx_payment/tx_payment_egld_value.rs | 41 +------ .../tx_payment/tx_payment_multi_esdt.rs | 4 +- .../interaction/tx_payment/tx_payment_none.rs | 18 +-- .../tx_payment/tx_payment_other.rs | 4 +- .../tx_payment/tx_payment_single_esdt.rs | 2 +- .../tx_payment/tx_payment_single_esdt_ref.rs | 2 +- framework/base/src/types/interaction/tx_to.rs | 53 ++------- .../types/interaction/tx_to/tx_to_caller.rs | 13 +-- .../src/types/interaction/tx_to/tx_to_self.rs | 13 +-- .../scenario/src/facade/contract_info.rs | 9 +- .../src/facade/world_tx/expr/file_expr.rs | 2 +- .../src/facade/world_tx/expr/mxsc_expr.rs | 2 +- .../scenario/model/value/value_set_bytes.rs | 6 +- 21 files changed, 181 insertions(+), 197 deletions(-) diff --git a/framework/base/src/types/interaction/annotated.rs b/framework/base/src/types/interaction/annotated.rs index 8571b4669b..afaecf184e 100644 --- a/framework/base/src/types/interaction/annotated.rs +++ b/framework/base/src/types/interaction/annotated.rs @@ -2,13 +2,31 @@ use crate::types::{heap::Address, BigUint, ManagedAddress, ManagedBuffer}; use super::TxEnv; -pub trait AnnotatedValue +pub trait AnnotatedValue: Sized where Env: TxEnv, { fn annotation(&self, env: &Env) -> ManagedBuffer; - fn into_value(self, env: &Env) -> T; + /// Produces the value from a reference of the annotated type. Might involve a `.clone()` in some cases. + fn to_value(&self, env: &Env) -> T; + + /// Consumes annotated value to produce actual value. + /// + /// Override whenever it helps to avoid an unnecessary clone. + fn into_value(self, env: &Env) -> T { + self.to_value(env) + } + + /// Can be used when working with references only. + /// + /// Override whenever it helps to avoid an unnecessary clone. + fn with_value_ref(&self, env: &Env, f: F) -> R + where + F: FnOnce(&T) -> R, + { + f(&self.to_value(env)) + } } impl AnnotatedValue> for ManagedAddress @@ -19,9 +37,20 @@ where self.hex_expr() } + fn to_value(&self, env: &Env) -> ManagedAddress { + self.clone() + } + fn into_value(self, _env: &Env) -> ManagedAddress { self } + + fn with_value_ref(&self, env: &Env, f: F) -> R + where + F: FnOnce(&ManagedAddress) -> R, + { + f(self) + } } impl AnnotatedValue> for &ManagedAddress @@ -32,9 +61,20 @@ where self.hex_expr() } + fn to_value(&self, env: &Env) -> ManagedAddress { + (*self).clone() + } + fn into_value(self, _env: &Env) -> ManagedAddress { self.clone() } + + fn with_value_ref(&self, env: &Env, f: F) -> R + where + F: FnOnce(&ManagedAddress) -> R, + { + f(self) + } } impl AnnotatedValue> for Address @@ -45,8 +85,8 @@ where ManagedAddress::from(self).hex_expr() } - fn into_value(self, _env: &Env) -> ManagedAddress { - self.into() + fn to_value(&self, env: &Env) -> ManagedAddress { + ManagedAddress::from(self) } } @@ -58,8 +98,8 @@ where ManagedAddress::from(*self).hex_expr() } - fn into_value(self, _env: &Env) -> ManagedAddress { - self.into() + fn to_value(&self, env: &Env) -> ManagedAddress { + ManagedAddress::from(*self) } } @@ -71,9 +111,20 @@ where self.hex_expr() } + fn to_value(&self, _env: &Env) -> ManagedBuffer { + self.clone() + } + fn into_value(self, _env: &Env) -> ManagedBuffer { self } + + fn with_value_ref(&self, env: &Env, f: F) -> R + where + F: FnOnce(&ManagedBuffer) -> R, + { + f(self) + } } impl AnnotatedValue> for BigUint @@ -84,9 +135,20 @@ where self.to_display() } + fn to_value(&self, _env: &Env) -> BigUint { + self.clone() + } + fn into_value(self, _env: &Env) -> BigUint { self } + + fn with_value_ref(&self, env: &Env, f: F) -> R + where + F: FnOnce(&BigUint) -> R, + { + f(self) + } } impl AnnotatedValue> for &BigUint @@ -97,9 +159,20 @@ where self.to_display() } + fn to_value(&self, _env: &Env) -> BigUint { + (*self).clone() + } + fn into_value(self, _env: &Env) -> BigUint { self.clone() } + + fn with_value_ref(&self, env: &Env, f: F) -> R + where + F: FnOnce(&BigUint) -> R, + { + f(self) + } } impl AnnotatedValue> for u64 @@ -107,10 +180,23 @@ where Env: TxEnv, { fn annotation(&self, env: &Env) -> ManagedBuffer { - self.into_value(env).to_display() + self.to_value(env).to_display() } - fn into_value(self, _env: &Env) -> BigUint { - BigUint::::from(self) + fn to_value(&self, _env: &Env) -> BigUint { + BigUint::from(*self) + } +} + +impl AnnotatedValue> for () +where + Env: TxEnv, +{ + fn annotation(&self, env: &Env) -> ManagedBuffer { + ManagedBuffer::from("0") + } + + fn to_value(&self, _env: &Env) -> BigUint { + BigUint::zero() } } diff --git a/framework/base/src/types/interaction/expr/address_expr.rs b/framework/base/src/types/interaction/expr/address_expr.rs index ce2fa10fb5..dddfa5b53c 100644 --- a/framework/base/src/types/interaction/expr/address_expr.rs +++ b/framework/base/src/types/interaction/expr/address_expr.rs @@ -20,11 +20,12 @@ where result } - fn into_value(self, _env: &Env) -> ManagedAddress { + fn to_value(&self, _env: &Env) -> ManagedAddress { let expr: [u8; 32] = self.eval_to_array(); expr.into() } } + impl TxFrom for AddressExpr where Env: TxEnv, @@ -36,19 +37,7 @@ where } impl TxFromSpecified for AddressExpr where Env: TxEnv {} impl TxTo for AddressExpr where Env: TxEnv {} -impl TxToSpecified for AddressExpr -where - Env: TxEnv, -{ - fn with_address_ref(&self, _env: &Env, f: F) -> R - where - F: FnOnce(&ManagedAddress) -> R, - { - let expr: [u8; 32] = self.eval_to_array(); - let ma = expr.into(); - f(&ma) - } -} +impl TxToSpecified for AddressExpr where Env: TxEnv {} impl AddressExpr { pub const fn eval_to_array(&self) -> [u8; 32] { diff --git a/framework/base/src/types/interaction/expr/sc_expr.rs b/framework/base/src/types/interaction/expr/sc_expr.rs index 1613587402..c84f8df02f 100644 --- a/framework/base/src/types/interaction/expr/sc_expr.rs +++ b/framework/base/src/types/interaction/expr/sc_expr.rs @@ -22,11 +22,12 @@ where result } - fn into_value(self, _env: &Env) -> ManagedAddress { + fn to_value(&self, _env: &Env) -> ManagedAddress { let expr: [u8; 32] = self.eval_to_array(); expr.into() } } + impl<'a, Env> TxFrom for ScExpr<'a> where Env: TxEnv, @@ -38,19 +39,7 @@ where } impl<'a, Env> TxFromSpecified for ScExpr<'a> where Env: TxEnv {} impl<'a, Env> TxTo for ScExpr<'a> where Env: TxEnv {} -impl<'a, Env> TxToSpecified for ScExpr<'a> -where - Env: TxEnv, -{ - fn with_address_ref(&self, _env: &Env, f: F) -> R - where - F: FnOnce(&ManagedAddress) -> R, - { - let expr: [u8; 32] = self.eval_to_array(); - let ma = expr.into(); - f(&ma) - } -} +impl<'a, Env> TxToSpecified for ScExpr<'a> where Env: TxEnv {} impl<'a> ScExpr<'a> { pub const fn eval_to_array(&self) -> [u8; 32] { diff --git a/framework/base/src/types/interaction/tx_call_deploy.rs b/framework/base/src/types/interaction/tx_call_deploy.rs index 1b41ccb406..53814cd128 100644 --- a/framework/base/src/types/interaction/tx_call_deploy.rs +++ b/framework/base/src/types/interaction/tx_call_deploy.rs @@ -33,7 +33,7 @@ where fn execute_deploy_raw(self) -> (ManagedAddress, ManagedVec>, RH) { let gas_limit = self.gas.resolve_gas(&self.env); - let (new_address, raw_results) = self.payment.with_egld_value(|egld_value| { + let (new_address, raw_results) = self.payment.with_egld_value(&self.env, |egld_value| { SendRawWrapper::::new().deploy_contract( gas_limit, egld_value, @@ -71,7 +71,7 @@ where ) -> (ManagedAddress, ManagedVec>, RH) { let gas_limit = self.gas.resolve_gas(&self.env); - let (new_address, raw_results) = self.payment.with_egld_value(|egld_value| { + let (new_address, raw_results) = self.payment.with_egld_value(&self.env, |egld_value| { SendRawWrapper::::new().deploy_from_source_contract( gas_limit, egld_value, diff --git a/framework/base/src/types/interaction/tx_call_te.rs b/framework/base/src/types/interaction/tx_call_te.rs index 5cb81a6f0f..6e81e7b8bb 100644 --- a/framework/base/src/types/interaction/tx_call_te.rs +++ b/framework/base/src/types/interaction/tx_call_te.rs @@ -25,7 +25,7 @@ where pub fn transfer_execute(self) { let gas_limit: u64; if self.data.is_no_call() { - if self.payment.is_no_payment() { + if self.payment.is_no_payment(&self.env) { return; } else { gas_limit = 0; @@ -54,7 +54,7 @@ where /// /// Can only used for simple transfers. pub fn transfer_if_not_empty(self) { - if self.payment.is_no_payment() { + if self.payment.is_no_payment(&self.env) { return; } diff --git a/framework/base/src/types/interaction/tx_call_upgrade.rs b/framework/base/src/types/interaction/tx_call_upgrade.rs index 2d6af25fab..24438da3c3 100644 --- a/framework/base/src/types/interaction/tx_call_upgrade.rs +++ b/framework/base/src/types/interaction/tx_call_upgrade.rs @@ -33,7 +33,7 @@ where /// TODO: change return type to `!`. pub fn upgrade_async_call_and_exit(self) { let gas = self.gas.explicit_or_gas_left(&self.env); - self.payment.with_egld_value(|egld_value| { + self.payment.with_egld_value(&self.env, |egld_value| { SendRawWrapper::::new().upgrade_contract( &self.to, gas, @@ -68,7 +68,7 @@ where /// TODO: change return type to `!`. pub fn upgrade_async_call_and_exit(self) { let gas = self.gas.explicit_or_gas_left(&self.env); - self.payment.with_egld_value(|egld_value| { + self.payment.with_egld_value(&self.env, |egld_value| { SendRawWrapper::::new().upgrade_from_source_contract( &self.to, gas, @@ -98,7 +98,7 @@ where /// If another one was previously set in the `Tx` object, that one will be ignored. pub fn upgrade_contract(self, code: &ManagedBuffer, code_metadata: CodeMetadata) { let gas = self.gas.explicit_or_gas_left(&self.env); - self.payment.with_egld_value(|egld_value| { + self.payment.with_egld_value(&self.env, |egld_value| { SendRawWrapper::::new().upgrade_contract( &self.to, gas, @@ -123,7 +123,7 @@ where code_metadata: CodeMetadata, ) { let gas = self.gas.explicit_or_gas_left(&self.env); - self.payment.with_egld_value(|egld_value| { + self.payment.with_egld_value(&self.env, |egld_value| { SendRawWrapper::::new().upgrade_from_source_contract( &self.to, gas, diff --git a/framework/base/src/types/interaction/tx_payment.rs b/framework/base/src/types/interaction/tx_payment.rs index 4b2f54e29f..978190a67a 100644 --- a/framework/base/src/types/interaction/tx_payment.rs +++ b/framework/base/src/types/interaction/tx_payment.rs @@ -18,7 +18,7 @@ use crate::{ }, }; -use super::{FunctionCall, TxEnv, TxFrom, TxToSpecified}; +use super::{AnnotatedValue, FunctionCall, TxEnv, TxFrom, TxToSpecified}; /// Describes a payment that is part of a transaction. pub trait TxPayment @@ -26,7 +26,7 @@ where Env: TxEnv, { /// Returns true if payment indicates transfer of either non-zero EGLD or ESDT amounts. - fn is_no_payment(&self) -> bool; + fn is_no_payment(&self, env: &Env) -> bool; /// Transfer-execute calls have different APIs for different payments types. /// This method selects between them. @@ -57,15 +57,20 @@ where } /// Marks a payment object that only contains EGLD or nothing at all. -pub trait TxPaymentEgldOnly: TxPayment +pub trait TxPaymentEgldOnly: TxPayment + AnnotatedValue> where Env: TxEnv, { - fn with_egld_value(&self, f: F) -> R + fn with_egld_value(&self, env: &Env, f: F) -> R where - F: FnOnce(&BigUint) -> R; + F: FnOnce(&BigUint) -> R, + { + self.with_value_ref(env, f) + } - fn into_egld_payment(self, env: &Env) -> BigUint; + fn into_egld_payment(self, env: &Env) -> BigUint { + self.into_value(env) + } } #[derive(Clone)] diff --git a/framework/base/src/types/interaction/tx_payment/tx_payment_egld.rs b/framework/base/src/types/interaction/tx_payment/tx_payment_egld.rs index a4881bda05..2c730591cd 100644 --- a/framework/base/src/types/interaction/tx_payment/tx_payment_egld.rs +++ b/framework/base/src/types/interaction/tx_payment/tx_payment_egld.rs @@ -1,5 +1,6 @@ use crate::{ contract_base::SendRawWrapper, + proxy_imports::{AnnotatedValue, ManagedBuffer}, types::{BigUint, ManagedAddress, ManagedVec, TxFrom, TxToSpecified}, }; @@ -18,18 +19,18 @@ where Env: TxEnv, EgldValue: TxEgldValue, { - fn is_no_payment(&self) -> bool { - self.0.with_egld_value(|egld_value| egld_value == &0u32) + fn is_no_payment(&self, env: &Env) -> bool { + self.0.with_value_ref(env, |egld_value| egld_value == &0u32) } fn perform_transfer_execute( self, - _env: &Env, + env: &Env, to: &ManagedAddress, gas_limit: u64, fc: FunctionCall, ) { - self.0.with_egld_value(|egld_value| { + self.0.with_value_ref(env, |egld_value| { let _ = SendRawWrapper::::new().direct_egld_execute( to, egld_value, @@ -55,7 +56,7 @@ where { to.with_address_ref(env, |to_addr| { self.0 - .with_egld_value(|egld_value| f(to_addr, egld_value, &fc)) + .with_value_ref(env, |egld_value| f(to_addr, egld_value, &fc)) }) } @@ -67,19 +68,34 @@ where } } -impl TxPaymentEgldOnly for Egld +impl AnnotatedValue> for Egld where Env: TxEnv, EgldValue: TxEgldValue, { - fn with_egld_value(&self, f: F) -> R + fn annotation(&self, env: &Env) -> ManagedBuffer { + self.0.annotation(env) + } + + fn to_value(&self, env: &Env) -> BigUint { + self.0.to_value(env) + } + + fn into_value(self, env: &Env) -> BigUint { + self.0.into_value(env) + } + + fn with_value_ref(&self, env: &Env, f: F) -> R where F: FnOnce(&BigUint) -> R, { - self.0.with_egld_value(f) + self.0.with_value_ref(env, f) } +} - fn into_egld_payment(self, env: &Env) -> BigUint { - self.0.into_value(env) - } +impl TxPaymentEgldOnly for Egld +where + Env: TxEnv, + EgldValue: TxEgldValue, +{ } diff --git a/framework/base/src/types/interaction/tx_payment/tx_payment_egld_value.rs b/framework/base/src/types/interaction/tx_payment/tx_payment_egld_value.rs index 561235eef2..8fcaab3ceb 100644 --- a/framework/base/src/types/interaction/tx_payment/tx_payment_egld_value.rs +++ b/framework/base/src/types/interaction/tx_payment/tx_payment_egld_value.rs @@ -6,43 +6,8 @@ pub trait TxEgldValue: AnnotatedValue> where Env: TxEnv, { - fn with_egld_value(&self, f: F) -> R - where - F: FnOnce(&BigUint) -> R; } -impl TxEgldValue for BigUint -where - Env: TxEnv, -{ - fn with_egld_value(&self, f: F) -> R - where - F: FnOnce(&BigUint) -> R, - { - f(self) - } -} - -impl TxEgldValue for &BigUint -where - Env: TxEnv, -{ - fn with_egld_value(&self, f: F) -> R - where - F: FnOnce(&BigUint) -> R, - { - f(*self) - } -} - -impl TxEgldValue for u64 -where - Env: TxEnv, -{ - fn with_egld_value(&self, f: F) -> R - where - F: FnOnce(&BigUint) -> R, - { - f(&BigUint::::from(*self)) - } -} +impl TxEgldValue for BigUint where Env: TxEnv {} +impl TxEgldValue for &BigUint where Env: TxEnv {} +impl TxEgldValue for u64 where Env: TxEnv {} diff --git a/framework/base/src/types/interaction/tx_payment/tx_payment_multi_esdt.rs b/framework/base/src/types/interaction/tx_payment/tx_payment_multi_esdt.rs index 39f0e6a64c..78acfd87f7 100644 --- a/framework/base/src/types/interaction/tx_payment/tx_payment_multi_esdt.rs +++ b/framework/base/src/types/interaction/tx_payment/tx_payment_multi_esdt.rs @@ -9,7 +9,7 @@ impl TxPayment for &MultiEsdtPayment where Env: TxEnv, { - fn is_no_payment(&self) -> bool { + fn is_no_payment(&self, _env: &Env) -> bool { self.is_empty() } @@ -64,7 +64,7 @@ impl TxPayment for MultiEsdtPayment where Env: TxEnv, { - fn is_no_payment(&self) -> bool { + fn is_no_payment(&self, _env: &Env) -> bool { self.is_empty() } diff --git a/framework/base/src/types/interaction/tx_payment/tx_payment_none.rs b/framework/base/src/types/interaction/tx_payment/tx_payment_none.rs index d97143a2ad..54c82b3a3c 100644 --- a/framework/base/src/types/interaction/tx_payment/tx_payment_none.rs +++ b/framework/base/src/types/interaction/tx_payment/tx_payment_none.rs @@ -12,7 +12,7 @@ impl TxPayment for () where Env: TxEnv, { - fn is_no_payment(&self) -> bool { + fn is_no_payment(&self, _env: &Env) -> bool { true } @@ -47,18 +47,4 @@ where } } -impl TxPaymentEgldOnly for () -where - Env: TxEnv, -{ - fn with_egld_value(&self, f: F) -> R - where - F: FnOnce(&BigUint) -> R, - { - f(&BigUint::zero()) - } - - fn into_egld_payment(self, _env: &Env) -> BigUint { - BigUint::zero() - } -} +impl TxPaymentEgldOnly for () where Env: TxEnv {} diff --git a/framework/base/src/types/interaction/tx_payment/tx_payment_other.rs b/framework/base/src/types/interaction/tx_payment/tx_payment_other.rs index 5a28d80902..01d1c3a6bb 100644 --- a/framework/base/src/types/interaction/tx_payment/tx_payment_other.rs +++ b/framework/base/src/types/interaction/tx_payment/tx_payment_other.rs @@ -12,7 +12,7 @@ impl TxPayment for EgldOrEsdtTokenPayment where Env: TxEnv, { - fn is_no_payment(&self) -> bool { + fn is_no_payment(&self, _env: &Env) -> bool { self.amount == 0u32 } @@ -63,7 +63,7 @@ impl TxPayment for EgldOrMultiEsdtPayment where Env: TxEnv, { - fn is_no_payment(&self) -> bool { + fn is_no_payment(&self, _env: &Env) -> bool { self.is_empty() } diff --git a/framework/base/src/types/interaction/tx_payment/tx_payment_single_esdt.rs b/framework/base/src/types/interaction/tx_payment/tx_payment_single_esdt.rs index f5bf61c0f1..547ff12ba0 100644 --- a/framework/base/src/types/interaction/tx_payment/tx_payment_single_esdt.rs +++ b/framework/base/src/types/interaction/tx_payment/tx_payment_single_esdt.rs @@ -12,7 +12,7 @@ impl TxPayment for EsdtTokenPayment where Env: TxEnv, { - fn is_no_payment(&self) -> bool { + fn is_no_payment(&self, _env: &Env) -> bool { self.amount == 0u32 } diff --git a/framework/base/src/types/interaction/tx_payment/tx_payment_single_esdt_ref.rs b/framework/base/src/types/interaction/tx_payment/tx_payment_single_esdt_ref.rs index c0d568e11b..7128b0fc2b 100644 --- a/framework/base/src/types/interaction/tx_payment/tx_payment_single_esdt_ref.rs +++ b/framework/base/src/types/interaction/tx_payment/tx_payment_single_esdt_ref.rs @@ -13,7 +13,7 @@ impl<'a, Env> TxPayment for EsdtTokenPaymentRefs<'a, Env::Api> where Env: TxEnv, { - fn is_no_payment(&self) -> bool { + fn is_no_payment(&self, _env: &Env) -> bool { self.amount == &0u32 } diff --git a/framework/base/src/types/interaction/tx_to.rs b/framework/base/src/types/interaction/tx_to.rs index 6d964bd0b5..03f3105858 100644 --- a/framework/base/src/types/interaction/tx_to.rs +++ b/framework/base/src/types/interaction/tx_to.rs @@ -24,60 +24,21 @@ where /// /// Other than that, does thesame as `AnnotatedValue::into_value`. fn with_address_ref(&self, env: &Env, f: F) -> R - where - F: FnOnce(&ManagedAddress) -> R; -} - -impl TxTo for ManagedAddress where Env: TxEnv {} -impl TxToSpecified for ManagedAddress -where - Env: TxEnv, -{ - fn with_address_ref(&self, _env: &Env, f: F) -> R where F: FnOnce(&ManagedAddress) -> R, { - f(self) + self.with_value_ref(env, f) } } +impl TxTo for ManagedAddress where Env: TxEnv {} +impl TxToSpecified for ManagedAddress where Env: TxEnv {} + impl TxTo for &ManagedAddress where Env: TxEnv {} -impl TxToSpecified for &ManagedAddress -where - Env: TxEnv, -{ - fn with_address_ref(&self, _env: &Env, f: F) -> R - where - F: FnOnce(&ManagedAddress) -> R, - { - f(self) - } -} +impl TxToSpecified for &ManagedAddress where Env: TxEnv {} impl TxTo for Address where Env: TxEnv {} -impl TxToSpecified for Address -where - Env: TxEnv, -{ - fn with_address_ref(&self, _env: &Env, f: F) -> R - where - F: FnOnce(&ManagedAddress) -> R, - { - let managed_address = ManagedAddress::from(self); - f(&managed_address) - } -} +impl TxToSpecified for Address where Env: TxEnv {} impl TxTo for &Address where Env: TxEnv {} -impl TxToSpecified for &Address -where - Env: TxEnv, -{ - fn with_address_ref(&self, _env: &Env, f: F) -> R - where - F: FnOnce(&ManagedAddress) -> R, - { - let managed_address = ManagedAddress::from(*self); - f(&managed_address) - } -} +impl TxToSpecified for &Address where Env: TxEnv {} diff --git a/framework/base/src/types/interaction/tx_to/tx_to_caller.rs b/framework/base/src/types/interaction/tx_to/tx_to_caller.rs index 1e3b59db73..7217f47c8b 100644 --- a/framework/base/src/types/interaction/tx_to/tx_to_caller.rs +++ b/framework/base/src/types/interaction/tx_to/tx_to_caller.rs @@ -17,17 +17,11 @@ where self.with_address_ref(env, |addr_ref| addr_ref.hex_expr()) } - fn into_value(self, _env: &TxScEnv) -> ManagedAddress { + fn to_value(&self, _env: &TxScEnv) -> ManagedAddress { BlockchainWrapper::::new().get_caller() } -} -impl TxTo> for ToCaller where Api: CallTypeApi + BlockchainApi {} -impl TxToSpecified> for ToCaller -where - Api: CallTypeApi + BlockchainApi, -{ - fn with_address_ref(&self, env: &TxScEnv, f: F) -> R + fn with_value_ref(&self, env: &TxScEnv, f: F) -> R where F: FnOnce(&ManagedAddress) -> R, { @@ -36,3 +30,6 @@ where f(&ManagedAddress::from_handle(caller_handle)) } } + +impl TxTo> for ToCaller where Api: CallTypeApi + BlockchainApi {} +impl TxToSpecified> for ToCaller where Api: CallTypeApi + BlockchainApi {} diff --git a/framework/base/src/types/interaction/tx_to/tx_to_self.rs b/framework/base/src/types/interaction/tx_to/tx_to_self.rs index 87e068ae6f..72350c312e 100644 --- a/framework/base/src/types/interaction/tx_to/tx_to_self.rs +++ b/framework/base/src/types/interaction/tx_to/tx_to_self.rs @@ -17,17 +17,11 @@ where self.with_address_ref(env, |addr_ref| addr_ref.hex_expr()) } - fn into_value(self, _env: &TxScEnv) -> ManagedAddress { + fn to_value(&self, _env: &TxScEnv) -> ManagedAddress { BlockchainWrapper::::new().get_sc_address() } -} -impl TxTo> for ToSelf where Api: CallTypeApi + BlockchainApi {} -impl TxToSpecified> for ToSelf -where - Api: CallTypeApi + BlockchainApi, -{ - fn with_address_ref(&self, env: &TxScEnv, f: F) -> R + fn with_value_ref(&self, env: &TxScEnv, f: F) -> R where F: FnOnce(&ManagedAddress) -> R, { @@ -37,3 +31,6 @@ where f(&ManagedAddress::from_handle(sc_address_handle)) } } + +impl TxTo> for ToSelf where Api: CallTypeApi + BlockchainApi {} +impl TxToSpecified> for ToSelf where Api: CallTypeApi + BlockchainApi {} diff --git a/framework/scenario/src/facade/contract_info.rs b/framework/scenario/src/facade/contract_info.rs index 56095208e6..2652732494 100644 --- a/framework/scenario/src/facade/contract_info.rs +++ b/framework/scenario/src/facade/contract_info.rs @@ -117,7 +117,7 @@ where self.scenario_address_expr.original.as_str().into() } - fn into_value(self, _env: &Env) -> ManagedAddress { + fn to_value(&self, _env: &Env) -> ManagedAddress { (&self.scenario_address_expr.value).into() } } @@ -148,11 +148,4 @@ where Env: TxEnv, P: ProxyObjNew, { - fn with_address_ref(&self, _env: &Env, f: F) -> R - where - F: FnOnce(&ManagedAddress) -> R, - { - let ma: ManagedAddress = (&self.scenario_address_expr.value).into(); - f(&ma) - } } diff --git a/framework/scenario/src/facade/world_tx/expr/file_expr.rs b/framework/scenario/src/facade/world_tx/expr/file_expr.rs index f42f6e84e0..d9d3c77756 100644 --- a/framework/scenario/src/facade/world_tx/expr/file_expr.rs +++ b/framework/scenario/src/facade/world_tx/expr/file_expr.rs @@ -23,7 +23,7 @@ impl<'a> AnnotatedValue> for FileExp result } - fn into_value(self, env: &ScenarioTxEnvData) -> ManagedBuffer { + fn to_value(&self, env: &ScenarioTxEnvData) -> ManagedBuffer { let context = InterpreterContext::new().with_dir(env.context_path.clone()); let value = interpret_string(&format!("{FILE_PREFIX}{}", self.0), &context); value.into() diff --git a/framework/scenario/src/facade/world_tx/expr/mxsc_expr.rs b/framework/scenario/src/facade/world_tx/expr/mxsc_expr.rs index 6f95cf2b7a..c0f574e8cd 100644 --- a/framework/scenario/src/facade/world_tx/expr/mxsc_expr.rs +++ b/framework/scenario/src/facade/world_tx/expr/mxsc_expr.rs @@ -26,7 +26,7 @@ where result } - fn into_value(self, env: &Env) -> ManagedBuffer { + fn to_value(&self, env: &Env) -> ManagedBuffer { let context = InterpreterContext::new() .with_dir(env.env_data().context_path.clone()) .with_allowed_missing_files(); diff --git a/framework/scenario/src/scenario/model/value/value_set_bytes.rs b/framework/scenario/src/scenario/model/value/value_set_bytes.rs index eaed19cea0..627f2ace9c 100644 --- a/framework/scenario/src/scenario/model/value/value_set_bytes.rs +++ b/framework/scenario/src/scenario/model/value/value_set_bytes.rs @@ -145,8 +145,8 @@ where self.original.to_concatenated_string().into() } - fn into_value(self, _env: &Env) -> ManagedBuffer { - self.value.into() + fn to_value(&self, _env: &Env) -> ManagedBuffer { + self.value.clone().into() } } @@ -160,7 +160,7 @@ where self.original.to_concatenated_string().into() } - fn into_value(self, _env: &Env) -> ManagedBuffer { + fn to_value(&self, _env: &Env) -> ManagedBuffer { self.value.clone().into() } } From 0b229c24ec75ca89a76ae0d31d849fb3eb83b87f Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Wed, 27 Mar 2024 07:11:15 +0200 Subject: [PATCH 2/3] AnnotatedValue & TxEgldValue impl for ManagedRef of BigUint --- .../proxy-test-first/src/proxy-test-first.rs | 2 +- contracts/modules/src/esdt.rs | 2 +- .../base/src/types/interaction/annotated.rs | 29 ++++++++++++++++++- .../tx_payment/tx_payment_egld_value.rs | 6 +++- .../src/types/managed/wrapped/managed_ref.rs | 1 + 5 files changed, 36 insertions(+), 4 deletions(-) diff --git a/contracts/feature-tests/composability/proxy-test-first/src/proxy-test-first.rs b/contracts/feature-tests/composability/proxy-test-first/src/proxy-test-first.rs index 7d18e22e3b..26cc9eb679 100644 --- a/contracts/feature-tests/composability/proxy-test-first/src/proxy-test-first.rs +++ b/contracts/feature-tests/composability/proxy-test-first/src/proxy-test-first.rs @@ -81,7 +81,7 @@ pub trait ProxyTestFirst { .to(other_contract) .raw_upgrade() .argument(&456) - .egld(payment.clone_value()) + .egld(payment) .upgrade_contract(&code, CodeMetadata::UPGRADEABLE); } diff --git a/contracts/modules/src/esdt.rs b/contracts/modules/src/esdt.rs index fa02cfd263..f39e5fe650 100644 --- a/contracts/modules/src/esdt.rs +++ b/contracts/modules/src/esdt.rs @@ -66,7 +66,7 @@ pub trait EsdtModule { let egld_returned = self.call_value().egld_value(); self.tx() .to(&initial_caller) - .egld(&*egld_returned) + .egld(egld_returned) .transfer_if_not_empty(); }, } diff --git a/framework/base/src/types/interaction/annotated.rs b/framework/base/src/types/interaction/annotated.rs index afaecf184e..d6b75fc610 100644 --- a/framework/base/src/types/interaction/annotated.rs +++ b/framework/base/src/types/interaction/annotated.rs @@ -1,4 +1,7 @@ -use crate::types::{heap::Address, BigUint, ManagedAddress, ManagedBuffer}; +use crate::{ + proxy_imports::ManagedRef, + types::{heap::Address, BigUint, ManagedAddress, ManagedBuffer}, +}; use super::TxEnv; @@ -175,6 +178,30 @@ where } } +impl<'a, Env> AnnotatedValue> for ManagedRef<'a, Env::Api, BigUint> +where + Env: TxEnv, +{ + fn annotation(&self, _env: &Env) -> ManagedBuffer { + self.to_display() + } + + fn to_value(&self, _env: &Env) -> BigUint { + (*self).clone_value() + } + + fn into_value(self, _env: &Env) -> BigUint { + self.clone_value() + } + + fn with_value_ref(&self, env: &Env, f: F) -> R + where + F: FnOnce(&BigUint) -> R, + { + f(self) + } +} + impl AnnotatedValue> for u64 where Env: TxEnv, diff --git a/framework/base/src/types/interaction/tx_payment/tx_payment_egld_value.rs b/framework/base/src/types/interaction/tx_payment/tx_payment_egld_value.rs index 8fcaab3ceb..b047e584b2 100644 --- a/framework/base/src/types/interaction/tx_payment/tx_payment_egld_value.rs +++ b/framework/base/src/types/interaction/tx_payment/tx_payment_egld_value.rs @@ -1,4 +1,7 @@ -use crate::types::{AnnotatedValue, BigUint}; +use crate::{ + proxy_imports::ManagedRef, + types::{AnnotatedValue, BigUint}, +}; use super::TxEnv; @@ -10,4 +13,5 @@ where impl TxEgldValue for BigUint where Env: TxEnv {} impl TxEgldValue for &BigUint where Env: TxEnv {} +impl<'a, Env> TxEgldValue for ManagedRef<'a, Env::Api, BigUint> where Env: TxEnv {} impl TxEgldValue for u64 where Env: TxEnv {} diff --git a/framework/base/src/types/managed/wrapped/managed_ref.rs b/framework/base/src/types/managed/wrapped/managed_ref.rs index 748c704956..143d79e1d4 100644 --- a/framework/base/src/types/managed/wrapped/managed_ref.rs +++ b/framework/base/src/types/managed/wrapped/managed_ref.rs @@ -54,6 +54,7 @@ where M: ManagedTypeApi, T: ManagedType + Clone, { + /// Syntactic sugar for dereferencing and cloning the object. pub fn clone_value(&self) -> T { self.deref().clone() } From dc9c49b4178fdde0f4afedc0786e369c176b86c9 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Wed, 27 Mar 2024 07:40:20 +0200 Subject: [PATCH 3/3] AnnotatedValue impl split into several files --- .../base/src/types/interaction/annotated.rs | 200 +----------------- .../annotated/annotated_impl_big_uint.rs | 104 +++++++++ .../annotated_impl_managed_address.rs | 80 +++++++ .../annotated_impl_managed_buffer.rs | 30 +++ 4 files changed, 218 insertions(+), 196 deletions(-) create mode 100644 framework/base/src/types/interaction/annotated/annotated_impl_big_uint.rs create mode 100644 framework/base/src/types/interaction/annotated/annotated_impl_managed_address.rs create mode 100644 framework/base/src/types/interaction/annotated/annotated_impl_managed_buffer.rs diff --git a/framework/base/src/types/interaction/annotated.rs b/framework/base/src/types/interaction/annotated.rs index d6b75fc610..f3187d08ce 100644 --- a/framework/base/src/types/interaction/annotated.rs +++ b/framework/base/src/types/interaction/annotated.rs @@ -1,3 +1,7 @@ +mod annotated_impl_big_uint; +mod annotated_impl_managed_address; +mod annotated_impl_managed_buffer; + use crate::{ proxy_imports::ManagedRef, types::{heap::Address, BigUint, ManagedAddress, ManagedBuffer}, @@ -31,199 +35,3 @@ where f(&self.to_value(env)) } } - -impl AnnotatedValue> for ManagedAddress -where - Env: TxEnv, -{ - fn annotation(&self, _env: &Env) -> ManagedBuffer { - self.hex_expr() - } - - fn to_value(&self, env: &Env) -> ManagedAddress { - self.clone() - } - - fn into_value(self, _env: &Env) -> ManagedAddress { - self - } - - fn with_value_ref(&self, env: &Env, f: F) -> R - where - F: FnOnce(&ManagedAddress) -> R, - { - f(self) - } -} - -impl AnnotatedValue> for &ManagedAddress -where - Env: TxEnv, -{ - fn annotation(&self, _env: &Env) -> ManagedBuffer { - self.hex_expr() - } - - fn to_value(&self, env: &Env) -> ManagedAddress { - (*self).clone() - } - - fn into_value(self, _env: &Env) -> ManagedAddress { - self.clone() - } - - fn with_value_ref(&self, env: &Env, f: F) -> R - where - F: FnOnce(&ManagedAddress) -> R, - { - f(self) - } -} - -impl AnnotatedValue> for Address -where - Env: TxEnv, -{ - fn annotation(&self, _env: &Env) -> ManagedBuffer { - ManagedAddress::from(self).hex_expr() - } - - fn to_value(&self, env: &Env) -> ManagedAddress { - ManagedAddress::from(self) - } -} - -impl AnnotatedValue> for &Address -where - Env: TxEnv, -{ - fn annotation(&self, _env: &Env) -> ManagedBuffer { - ManagedAddress::from(*self).hex_expr() - } - - fn to_value(&self, env: &Env) -> ManagedAddress { - ManagedAddress::from(*self) - } -} - -impl AnnotatedValue> for ManagedBuffer -where - Env: TxEnv, -{ - fn annotation(&self, _env: &Env) -> ManagedBuffer { - self.hex_expr() - } - - fn to_value(&self, _env: &Env) -> ManagedBuffer { - self.clone() - } - - fn into_value(self, _env: &Env) -> ManagedBuffer { - self - } - - fn with_value_ref(&self, env: &Env, f: F) -> R - where - F: FnOnce(&ManagedBuffer) -> R, - { - f(self) - } -} - -impl AnnotatedValue> for BigUint -where - Env: TxEnv, -{ - fn annotation(&self, _env: &Env) -> ManagedBuffer { - self.to_display() - } - - fn to_value(&self, _env: &Env) -> BigUint { - self.clone() - } - - fn into_value(self, _env: &Env) -> BigUint { - self - } - - fn with_value_ref(&self, env: &Env, f: F) -> R - where - F: FnOnce(&BigUint) -> R, - { - f(self) - } -} - -impl AnnotatedValue> for &BigUint -where - Env: TxEnv, -{ - fn annotation(&self, _env: &Env) -> ManagedBuffer { - self.to_display() - } - - fn to_value(&self, _env: &Env) -> BigUint { - (*self).clone() - } - - fn into_value(self, _env: &Env) -> BigUint { - self.clone() - } - - fn with_value_ref(&self, env: &Env, f: F) -> R - where - F: FnOnce(&BigUint) -> R, - { - f(self) - } -} - -impl<'a, Env> AnnotatedValue> for ManagedRef<'a, Env::Api, BigUint> -where - Env: TxEnv, -{ - fn annotation(&self, _env: &Env) -> ManagedBuffer { - self.to_display() - } - - fn to_value(&self, _env: &Env) -> BigUint { - (*self).clone_value() - } - - fn into_value(self, _env: &Env) -> BigUint { - self.clone_value() - } - - fn with_value_ref(&self, env: &Env, f: F) -> R - where - F: FnOnce(&BigUint) -> R, - { - f(self) - } -} - -impl AnnotatedValue> for u64 -where - Env: TxEnv, -{ - fn annotation(&self, env: &Env) -> ManagedBuffer { - self.to_value(env).to_display() - } - - fn to_value(&self, _env: &Env) -> BigUint { - BigUint::from(*self) - } -} - -impl AnnotatedValue> for () -where - Env: TxEnv, -{ - fn annotation(&self, env: &Env) -> ManagedBuffer { - ManagedBuffer::from("0") - } - - fn to_value(&self, _env: &Env) -> BigUint { - BigUint::zero() - } -} diff --git a/framework/base/src/types/interaction/annotated/annotated_impl_big_uint.rs b/framework/base/src/types/interaction/annotated/annotated_impl_big_uint.rs new file mode 100644 index 0000000000..29f41042aa --- /dev/null +++ b/framework/base/src/types/interaction/annotated/annotated_impl_big_uint.rs @@ -0,0 +1,104 @@ +use crate::{ + proxy_imports::ManagedRef, + types::{heap::Address, BigUint, ManagedAddress, ManagedBuffer}, +}; + +use super::{AnnotatedValue, TxEnv}; + +impl AnnotatedValue> for BigUint +where + Env: TxEnv, +{ + fn annotation(&self, _env: &Env) -> ManagedBuffer { + self.to_display() + } + + fn to_value(&self, _env: &Env) -> BigUint { + self.clone() + } + + fn into_value(self, _env: &Env) -> BigUint { + self + } + + fn with_value_ref(&self, env: &Env, f: F) -> R + where + F: FnOnce(&BigUint) -> R, + { + f(self) + } +} + +impl AnnotatedValue> for &BigUint +where + Env: TxEnv, +{ + fn annotation(&self, _env: &Env) -> ManagedBuffer { + self.to_display() + } + + fn to_value(&self, _env: &Env) -> BigUint { + (*self).clone() + } + + fn into_value(self, _env: &Env) -> BigUint { + self.clone() + } + + fn with_value_ref(&self, env: &Env, f: F) -> R + where + F: FnOnce(&BigUint) -> R, + { + f(self) + } +} + +impl<'a, Env> AnnotatedValue> for ManagedRef<'a, Env::Api, BigUint> +where + Env: TxEnv, +{ + fn annotation(&self, _env: &Env) -> ManagedBuffer { + self.to_display() + } + + fn to_value(&self, _env: &Env) -> BigUint { + (*self).clone_value() + } + + fn into_value(self, _env: &Env) -> BigUint { + self.clone_value() + } + + fn with_value_ref(&self, env: &Env, f: F) -> R + where + F: FnOnce(&BigUint) -> R, + { + f(self) + } +} + +impl AnnotatedValue> for u64 +where + Env: TxEnv, +{ + fn annotation(&self, env: &Env) -> ManagedBuffer { + self.to_value(env).to_display() + } + + fn to_value(&self, _env: &Env) -> BigUint { + BigUint::from(*self) + } +} + +impl AnnotatedValue> for () +where + Env: TxEnv, +{ + fn annotation(&self, env: &Env) -> ManagedBuffer { + ManagedBuffer::from("0") + } + + fn to_value(&self, _env: &Env) -> BigUint { + BigUint::zero() + } +} diff --git a/framework/base/src/types/interaction/annotated/annotated_impl_managed_address.rs b/framework/base/src/types/interaction/annotated/annotated_impl_managed_address.rs new file mode 100644 index 0000000000..87aebbb82e --- /dev/null +++ b/framework/base/src/types/interaction/annotated/annotated_impl_managed_address.rs @@ -0,0 +1,80 @@ +use crate::{ + proxy_imports::ManagedRef, + types::{heap::Address, BigUint, ManagedAddress, ManagedBuffer}, +}; + +use super::{AnnotatedValue, TxEnv}; + +impl AnnotatedValue> for ManagedAddress +where + Env: TxEnv, +{ + fn annotation(&self, _env: &Env) -> ManagedBuffer { + self.hex_expr() + } + + fn to_value(&self, env: &Env) -> ManagedAddress { + self.clone() + } + + fn into_value(self, _env: &Env) -> ManagedAddress { + self + } + + fn with_value_ref(&self, env: &Env, f: F) -> R + where + F: FnOnce(&ManagedAddress) -> R, + { + f(self) + } +} + +impl AnnotatedValue> for &ManagedAddress +where + Env: TxEnv, +{ + fn annotation(&self, _env: &Env) -> ManagedBuffer { + self.hex_expr() + } + + fn to_value(&self, env: &Env) -> ManagedAddress { + (*self).clone() + } + + fn into_value(self, _env: &Env) -> ManagedAddress { + self.clone() + } + + fn with_value_ref(&self, env: &Env, f: F) -> R + where + F: FnOnce(&ManagedAddress) -> R, + { + f(self) + } +} + +impl AnnotatedValue> for Address +where + Env: TxEnv, +{ + fn annotation(&self, _env: &Env) -> ManagedBuffer { + ManagedAddress::from(self).hex_expr() + } + + fn to_value(&self, env: &Env) -> ManagedAddress { + ManagedAddress::from(self) + } +} + +impl AnnotatedValue> for &Address +where + Env: TxEnv, +{ + fn annotation(&self, _env: &Env) -> ManagedBuffer { + ManagedAddress::from(*self).hex_expr() + } + + fn to_value(&self, env: &Env) -> ManagedAddress { + ManagedAddress::from(*self) + } +} diff --git a/framework/base/src/types/interaction/annotated/annotated_impl_managed_buffer.rs b/framework/base/src/types/interaction/annotated/annotated_impl_managed_buffer.rs new file mode 100644 index 0000000000..bf15a3a262 --- /dev/null +++ b/framework/base/src/types/interaction/annotated/annotated_impl_managed_buffer.rs @@ -0,0 +1,30 @@ +use crate::{ + proxy_imports::ManagedRef, + types::{heap::Address, BigUint, ManagedAddress, ManagedBuffer}, +}; + +use super::{AnnotatedValue, TxEnv}; + +impl AnnotatedValue> for ManagedBuffer +where + Env: TxEnv, +{ + fn annotation(&self, _env: &Env) -> ManagedBuffer { + self.hex_expr() + } + + fn to_value(&self, _env: &Env) -> ManagedBuffer { + self.clone() + } + + fn into_value(self, _env: &Env) -> ManagedBuffer { + self + } + + fn with_value_ref(&self, env: &Env, f: F) -> R + where + F: FnOnce(&ManagedBuffer) -> R, + { + f(self) + } +}