Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unified shrink 2 (ManagedRef) #1568

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ where
self
}

#[inline]
fn with_value_ref<F, R>(&self, _env: &Env, f: F) -> R
where
F: FnOnce(&BigUint<Env::Api>) -> R,
Expand All @@ -38,10 +39,12 @@ where
(*self).clone()
}

#[inline]
fn into_value(self, _env: &Env) -> BigUint<Env::Api> {
self.clone()
}

#[inline]
fn with_value_ref<F, R>(&self, _env: &Env, f: F) -> R
where
F: FnOnce(&BigUint<Env::Api>) -> R,
Expand All @@ -58,6 +61,7 @@ where
self.to_display()
}

#[inline]
fn to_value(&self, _env: &Env) -> BigUint<Env::Api> {
(*self).clone_value()
}
Expand All @@ -66,6 +70,7 @@ where
self.clone_value()
}

#[inline]
fn with_value_ref<F, R>(&self, _env: &Env, f: F) -> R
where
F: FnOnce(&BigUint<Env::Api>) -> R,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ where
self.hex_expr()
}

#[inline]
fn to_value(&self, _env: &Env) -> ManagedBuffer<Env::Api> {
self.clone()
}

#[inline]
fn into_value(self, _env: &Env) -> ManagedBuffer<Env::Api> {
self
}

#[inline]
fn with_value_ref<F, R>(&self, _env: &Env, f: F) -> R
where
F: FnOnce(&ManagedBuffer<Env::Api>) -> R,
Expand Down
2 changes: 1 addition & 1 deletion framework/base/src/types/interaction/markers/to_caller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ where
Api: CallTypeApi + BlockchainApi,
{
fn annotation(&self, env: &TxScEnv<Api>) -> ManagedBuffer<Api> {
self.with_address_ref(env, |addr_ref| addr_ref.hex_expr())
self.with_value_ref(env, |addr_ref| addr_ref.hex_expr())
}

fn to_value(&self, _env: &TxScEnv<Api>) -> ManagedAddress<Api> {
Expand Down
3 changes: 2 additions & 1 deletion framework/base/src/types/interaction/markers/to_self.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ where
Api: CallTypeApi + BlockchainApi,
{
fn annotation(&self, env: &TxScEnv<Api>) -> ManagedBuffer<Api> {
self.with_address_ref(env, |addr_ref| addr_ref.hex_expr())
self.with_value_ref(env, |addr_ref| addr_ref.hex_expr())
}

#[inline]
fn to_value(&self, _env: &TxScEnv<Api>) -> ManagedAddress<Api> {
BlockchainWrapper::<Api>::new().get_sc_address()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ where
token_ticker: &ManagedBuffer<Env::Api>,
properties: NonFungibleTokenProperties,
) -> IssueCall<Env, From, To, Gas> {
let zero = BigUint::zero();
let zero = BigUint::zero_ref();
self.issue(
issue_cost,
EsdtTokenType::NonFungible,
Expand Down
2 changes: 1 addition & 1 deletion framework/base/src/types/interaction/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -888,7 +888,7 @@ where
ContractDeploy {
_phantom: core::marker::PhantomData,
to: ManagedOption::none(),
egld_payment: value.payment.into_egld_payment(&value.env),
egld_payment: value.payment.into_value(&value.env),
explicit_gas_limit: UNSPECIFIED_GAS_LIMIT,
arg_buffer: value.data.arg_buffer,
_return_type: core::marker::PhantomData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ where
fn execute_deploy_raw(self) -> (ManagedAddress<Api>, ManagedVec<Api, ManagedBuffer<Api>>, RH) {
let gas_limit = self.gas.gas_value(&self.env);

let (new_address, raw_results) = self.payment.with_egld_value(&self.env, |egld_value| {
let (new_address, raw_results) = self.payment.with_value_ref(&self.env, |egld_value| {
SendRawWrapper::<Api>::new().deploy_contract(
gas_limit,
egld_value,
Expand Down Expand Up @@ -69,7 +69,7 @@ where
) -> (ManagedAddress<Api>, ManagedVec<Api, ManagedBuffer<Api>>, RH) {
let gas_limit = self.gas.gas_value(&self.env);

let (new_address, raw_results) = self.payment.with_egld_value(&self.env, |egld_value| {
let (new_address, raw_results) = self.payment.with_value_ref(&self.env, |egld_value| {
SendRawWrapper::<Api>::new().deploy_from_source_contract(
gas_limit,
egld_value,
Expand Down Expand Up @@ -235,7 +235,7 @@ where
)]
pub fn upgrade_contract(self, code: &ManagedBuffer<Api>, code_metadata: CodeMetadata) {
let gas = self.gas.explicit_or_gas_left(&self.env);
self.payment.with_egld_value(&self.env, |egld_value| {
self.payment.with_value_ref(&self.env, |egld_value| {
SendRawWrapper::<Api>::new().upgrade_contract(
&self.to,
gas,
Expand Down Expand Up @@ -268,7 +268,7 @@ where
code_metadata: CodeMetadata,
) {
let gas = self.gas.explicit_or_gas_left(&self.env);
self.payment.with_egld_value(&self.env, |egld_value| {
self.payment.with_value_ref(&self.env, |egld_value| {
SendRawWrapper::<Api>::new().upgrade_from_source_contract(
&self.to,
gas,
Expand Down
2 changes: 1 addition & 1 deletion framework/base/src/types/interaction/tx_exec/tx_exec_te.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ where
RH: TxEmptyResultHandler<TxScEnv<Api>>,
{
fn transfer_execute_with_gas(self, gas_limit: u64) {
self.to.with_address_ref(&self.env, |to| {
self.to.with_value_ref(&self.env, |to| {
self.payment
.perform_transfer_execute(&self.env, to, gas_limit, self.data.into());
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,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(&self.env, |egld_value| {
self.payment.with_value_ref(&self.env, |egld_value| {
SendRawWrapper::<Api>::new().upgrade_contract(
&self.to,
gas,
Expand Down Expand Up @@ -64,7 +64,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(&self.env, |egld_value| {
self.payment.with_value_ref(&self.env, |egld_value| {
SendRawWrapper::<Api>::new().upgrade_from_source_contract(
&self.to,
gas,
Expand Down Expand Up @@ -98,7 +98,7 @@ where
)]
pub fn upgrade_contract(self, code: &ManagedBuffer<Api>, code_metadata: CodeMetadata) {
let gas = self.gas.explicit_or_gas_left(&self.env);
self.payment.with_egld_value(&self.env, |egld_value| {
self.payment.with_value_ref(&self.env, |egld_value| {
SendRawWrapper::<Api>::new().upgrade_contract(
&self.to,
gas,
Expand Down Expand Up @@ -127,7 +127,7 @@ where
code_metadata: CodeMetadata,
) {
let gas = self.gas.explicit_or_gas_left(&self.env);
self.payment.with_egld_value(&self.env, |egld_value| {
self.payment.with_value_ref(&self.env, |egld_value| {
SendRawWrapper::<Api>::new().upgrade_from_source_contract(
&self.to,
gas,
Expand Down
1 change: 1 addition & 0 deletions framework/base/src/types/interaction/tx_from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ impl<Env> TxFrom<Env> for ()
where
Env: TxEnv,
{
#[inline]
fn resolve_address(&self, env: &Env) -> ManagedAddress<Env::Api> {
env.resolve_sender_address()
}
Expand Down
10 changes: 0 additions & 10 deletions framework/base/src/types/interaction/tx_payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,6 @@ pub trait TxPaymentEgldOnly<Env>: TxPayment<Env> + AnnotatedValue<Env, BigUint<E
where
Env: TxEnv,
{
fn with_egld_value<F, R>(&self, env: &Env, f: F) -> R
where
F: FnOnce(&BigUint<Env::Api>) -> R,
{
self.with_value_ref(env, f)
}

fn into_egld_payment(self, env: &Env) -> BigUint<Env::Api> {
self.into_value(env)
}
}

#[derive(Clone)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ where
})
}

#[inline]
fn with_normalized<From, To, F, R>(
self,
env: &Env,
Expand All @@ -55,7 +56,7 @@ where
To: TxToSpecified<Env>,
F: FnOnce(&ManagedAddress<Env::Api>, &BigUint<Env::Api>, &FunctionCall<Env::Api>) -> R,
{
to.with_address_ref(env, |to_addr| {
to.with_value_ref(env, |to_addr| {
self.0
.with_value_ref(env, |egld_value| f(to_addr, egld_value, &fc))
})
Expand All @@ -78,14 +79,17 @@ where
self.0.annotation(env)
}

#[inline]
fn to_value(&self, env: &Env) -> BigUint<Env::Api> {
self.0.to_value(env)
}

#[inline]
fn into_value(self, env: &Env) -> BigUint<Env::Api> {
self.0.into_value(env)
}

#[inline]
fn with_value_ref<F, R>(&self, env: &Env, f: F) -> R
where
F: FnOnce(&BigUint<Env::Api>) -> R,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,12 @@ impl<Env> TxPayment<Env> for EgldOrEsdtTokenPayment<Env::Api>
where
Env: TxEnv,
{
#[inline]
fn is_no_payment(&self, env: &Env) -> bool {
(&self).is_no_payment(env)
}

#[inline]
fn perform_transfer_execute(
self,
env: &Env,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::types::{BigUint, EgldOrMultiEsdtPayment, ManagedAddress, TxFrom, TxToSpecified};

use super::{Egld, FullPaymentData, FunctionCall, TxEnv, TxPayment};
use super::{FullPaymentData, FunctionCall, TxEnv, TxPayment};

impl<Env> TxPayment<Env> for EgldOrMultiEsdtPayment<Env::Api>
where
Expand All @@ -10,23 +10,19 @@ where
self.is_empty()
}

#[inline]
fn perform_transfer_execute(
self,
env: &Env,
to: &ManagedAddress<Env::Api>,
gas_limit: u64,
fc: FunctionCall<Env::Api>,
) {
match self {
EgldOrMultiEsdtPayment::Egld(egld_amount) => {
Egld(egld_amount).perform_transfer_execute(env, to, gas_limit, fc)
},
EgldOrMultiEsdtPayment::MultiEsdt(multi_esdt_payment) => {
multi_esdt_payment.perform_transfer_execute(env, to, gas_limit, fc)
},
}
self.as_refs()
.perform_transfer_execute(env, to, gas_limit, fc)
}

#[inline]
fn with_normalized<From, To, F, R>(
self,
env: &Env,
Expand All @@ -40,25 +36,12 @@ where
To: TxToSpecified<Env>,
F: FnOnce(&ManagedAddress<Env::Api>, &BigUint<Env::Api>, &FunctionCall<Env::Api>) -> R,
{
match self {
EgldOrMultiEsdtPayment::Egld(egld_amount) => {
Egld(egld_amount).with_normalized(env, from, to, fc, f)
},
EgldOrMultiEsdtPayment::MultiEsdt(multi_esdt_payment) => {
multi_esdt_payment.with_normalized(env, from, to, fc, f)
},
}
self.as_refs().with_normalized(env, from, to, fc, f)
}

#[inline]
fn into_full_payment_data(self, env: &Env) -> FullPaymentData<Env::Api> {
match self {
EgldOrMultiEsdtPayment::Egld(egld_amount) => {
TxPayment::<Env>::into_full_payment_data(Egld(egld_amount), env)
},
EgldOrMultiEsdtPayment::MultiEsdt(multi_esdt_payment) => {
TxPayment::<Env>::into_full_payment_data(multi_esdt_payment, env)
},
}
self.as_refs().into_full_payment_data(env)
}
}

Expand All @@ -70,23 +53,19 @@ where
self.is_empty()
}

#[inline]
fn perform_transfer_execute(
self,
env: &Env,
to: &ManagedAddress<Env::Api>,
gas_limit: u64,
fc: FunctionCall<Env::Api>,
) {
match self {
EgldOrMultiEsdtPayment::Egld(egld_amount) => {
Egld(egld_amount).perform_transfer_execute(env, to, gas_limit, fc)
},
EgldOrMultiEsdtPayment::MultiEsdt(multi_esdt_payment) => {
multi_esdt_payment.perform_transfer_execute(env, to, gas_limit, fc)
},
}
self.as_refs()
.perform_transfer_execute(env, to, gas_limit, fc)
}

#[inline]
fn with_normalized<From, To, F, R>(
self,
env: &Env,
Expand All @@ -100,24 +79,11 @@ where
To: TxToSpecified<Env>,
F: FnOnce(&ManagedAddress<Env::Api>, &BigUint<Env::Api>, &FunctionCall<Env::Api>) -> R,
{
match self {
EgldOrMultiEsdtPayment::Egld(egld_amount) => {
Egld(egld_amount).with_normalized(env, from, to, fc, f)
},
EgldOrMultiEsdtPayment::MultiEsdt(multi_esdt_payment) => {
multi_esdt_payment.with_normalized(env, from, to, fc, f)
},
}
self.as_refs().with_normalized(env, from, to, fc, f)
}

#[inline]
fn into_full_payment_data(self, env: &Env) -> FullPaymentData<Env::Api> {
match self {
EgldOrMultiEsdtPayment::Egld(egld_amount) => {
TxPayment::<Env>::into_full_payment_data(Egld(egld_amount), env)
},
EgldOrMultiEsdtPayment::MultiEsdt(multi_esdt_payment) => {
TxPayment::<Env>::into_full_payment_data(multi_esdt_payment, env)
},
}
self.as_refs().into_full_payment_data(env)
}
}
Loading
Loading