Skip to content

Commit

Permalink
NumExpr & GasLeft
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-marinica committed Mar 27, 2024
1 parent 45c431a commit 928ca50
Show file tree
Hide file tree
Showing 15 changed files with 133 additions and 41 deletions.
4 changes: 2 additions & 2 deletions contracts/examples/adder/interact/src/basic_interact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use multiversx_sc_snippets::{
scenario_format::interpret_trait::{InterpretableFrom, InterpreterContext},
scenario_model::{BytesValue, ScDeployStep, Scenario},
standalone::retrieve_account_as_scenario_set_state,
test_wallets, ContractInfo, WithRawTxResponse,
test_wallets, ContractInfo, NumExpr, WithRawTxResponse,
},
tokio, Interactor, InteractorPrepareAsync, StepBuffer,
};
Expand Down Expand Up @@ -170,7 +170,7 @@ impl AdderInteract {
.tx()
.from(&self.wallet_address)
.to(self.state.adder().to_address())
.egld(50000000000000000u64) // TODO: annotate "0,050000000000000000"
.egld(NumExpr("0,050000000000000000"))
.prepare_async()
.run()
.await;
Expand Down
8 changes: 4 additions & 4 deletions contracts/examples/multisig/interact/src/multisig_interact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use multiversx_sc_scenario::{
multiversx_sc::types::{BigUint, ReturnsNewAddress, ReturnsResult},
scenario_format::interpret_trait::InterpretableFrom,
standalone::retrieve_account_as_scenario_set_state,
test_wallets,
test_wallets, NumExpr,
};
use multiversx_sc_snippets::{
dns_address_for_name, env_logger,
Expand Down Expand Up @@ -167,7 +167,7 @@ impl MultisigInteract {
.typed(multisig_proxy::MultisigProxy)
.init(&Config::load_config().quorum(), board)
.code(&self.multisig_code)
.with_gas_limit(100_000_000u64)
.gas(NumExpr("100,000,000"))
.returns(ReturnsNewAddress)
.prepare_async()
.run()
Expand Down Expand Up @@ -260,7 +260,7 @@ impl MultisigInteract {
.tx()
.from(&self.wallet_address)
.to(self.state.multisig().to_address())
.with_gas_limit(gas_expr)
.gas(gas_expr)
.typed(multisig_proxy::MultisigProxy)
.perform_action_endpoint(action_id)
.prepare_async()
Expand Down Expand Up @@ -371,7 +371,7 @@ impl MultisigInteract {
.tx()
.from(&self.wallet_address)
.to(self.state.multisig().to_address())
.with_gas_limit(30_000_000u64)
.gas(NumExpr("30,000,000"))
.typed(multisig_proxy::MultisigProxy)
.dns_register(dns_address, name)
.prepare_async()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use multiversx_sc_scenario::{
codec::Empty,
types::{FunctionCall, ReturnsResult},
},
ReturnsNewTokenIdentifier,
NumExpr, ReturnsNewTokenIdentifier,
};

use super::*;
Expand Down Expand Up @@ -45,7 +45,7 @@ impl MultisigInteract {
.tx()
.from(&self.wallet_address)
.to(self.state.multisig().to_address())
.with_gas_limit(10_000_000u64)
.gas(NumExpr("10,000,000"))
.typed(multisig_proxy::MultisigProxy)
.propose_async_call(
system_sc_address,
Expand Down Expand Up @@ -81,7 +81,7 @@ impl MultisigInteract {
.tx()
.from(&self.wallet_address)
.to(&self.state.multisig().to_address())
.with_gas_limit(80_000_000u64)
.gas(NumExpr("80,000,000"))
.typed(multisig_proxy::MultisigProxy)
.perform_action_endpoint(action_id)
.returns(ReturnsNewTokenIdentifier)
Expand All @@ -103,7 +103,7 @@ impl MultisigInteract {
.tx()
.from(&self.wallet_address)
.to(&self.state.multisig().to_address())
.with_gas_limit(10_000_000u64)
.gas(NumExpr("10,000,000"))
.typed(multisig_proxy::MultisigProxy)
.propose_async_call(
system_sc_address,
Expand Down Expand Up @@ -136,7 +136,7 @@ impl MultisigInteract {
.tx()
.from(&self.wallet_address)
.to(&self.state.multisig().to_address())
.with_gas_limit(80_000_000u64)
.gas(NumExpr("80,000,000"))
.typed(multisig_proxy::MultisigProxy)
.perform_action_endpoint(action_id)
.returns(ReturnsNewTokenIdentifier)
Expand All @@ -158,7 +158,7 @@ impl MultisigInteract {
.tx()
.from(&self.wallet_address)
.to(&self.state.multisig().to_address())
.with_gas_limit(10_000_000u64)
.gas(NumExpr("10,000,000"))
.typed(multisig_proxy::MultisigProxy)
.propose_async_call(
&self.system_sc_address,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::time::Duration;

use multiversx_sc_scenario::multiversx_sc::types::{ContractCallBase, FunctionCall, ReturnsResult};
use multiversx_sc_scenario::{
multiversx_sc::types::{ContractCallBase, FunctionCall, ReturnsResult},
NumExpr,
};
#[allow(unused_imports)]
use multiversx_sc_snippets::multiversx_sc::types::{
EsdtTokenPayment, MultiValueEncoded, TokenIdentifier,
Expand Down Expand Up @@ -65,7 +68,7 @@ impl MultisigInteract {
.tx()
.from(&self.wallet_address)
.to(&self.state.multisig().to_address())
.with_gas_limit(10_000_000u64)
.gas(NumExpr("10,000,000"))
.typed(multisig_proxy::MultisigProxy)
.propose_async_call(
bech32::decode(WEGLD_SWAP_SC_BECH32),
Expand Down Expand Up @@ -98,7 +101,7 @@ impl MultisigInteract {
.tx()
.from(&self.wallet_address)
.to(&self.state.multisig().to_address())
.with_gas_limit(10_000_000u64)
.gas(NumExpr("10,000,000"))
.typed(multisig_proxy::MultisigProxy)
.propose_async_call(
contract_call.basic.to,
Expand Down
6 changes: 3 additions & 3 deletions framework/base/src/contract_base/wrappers/send_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
},
codec,
esdt::ESDTSystemSmartContractProxy,
proxy_imports::{ReturnsRawResult, ToSelf},
proxy_imports::{GasLeft, ReturnsRawResult, ToSelf},
types::{
BigUint, ContractCall, ContractCallNoPayment, EgldOrEsdtTokenIdentifier, EsdtTokenPayment,
ManagedAddress, ManagedArgBuffer, ManagedBuffer, ManagedType, ManagedVec, TokenIdentifier,
Expand Down Expand Up @@ -386,7 +386,7 @@ where
) -> ManagedVec<A, ManagedBuffer<A>> {
Tx::new_tx_from_sc()
.to(ToSelf)
.with_gas_limit(gas)
.gas(gas)
.raw_call()
.function_name(endpoint_name)
.arguments_raw(arg_buffer)
Expand All @@ -401,7 +401,7 @@ where
) {
Tx::new_tx_from_sc()
.to(ToSelf)
.with_gas_limit(A::blockchain_api_impl().get_gas_left())
.gas(GasLeft)
.raw_call()
.function_name(function_name)
.arguments_raw(arg_buffer)
Expand Down
13 changes: 12 additions & 1 deletion framework/base/src/types/interaction/annotated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ mod annotated_impl_managed_buffer;
mod annotated_impl_u64;

use crate::{
proxy_imports::ManagedRef,
formatter::FormatBuffer,
proxy_imports::{ManagedBufferCachedBuilder, ManagedRef, ManagedTypeApi},
types::{heap::Address, BigUint, ManagedAddress, ManagedBuffer},
};

Expand Down Expand Up @@ -36,3 +37,13 @@ where
f(&self.to_value(env))
}
}

/// Useful for u64 display in several places.
pub(super) fn display_u64<Api>(n: u64) -> ManagedBuffer<Api>
where
Api: ManagedTypeApi,
{
let mut result = ManagedBufferCachedBuilder::new_from_slice(&[]);
result.append_display(&n);
result.into_managed_buffer()
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,7 @@ use crate::{
},
};

use super::{AnnotatedValue, TxEnv};

pub(crate) fn display_u64<Api>(n: u64) -> ManagedBuffer<Api>
where
Api: ManagedTypeApi,
{
let mut result = ManagedBufferCachedBuilder::new_from_slice(&[]);
result.append_display(&n);
result.into_managed_buffer()
}
use super::{display_u64, AnnotatedValue, TxEnv};

impl<Env> AnnotatedValue<Env, u64> for u64
where
Expand Down
7 changes: 7 additions & 0 deletions framework/base/src/types/interaction/markers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
mod gas_left;
mod to_caller;
mod to_self;

pub use gas_left::GasLeft;
pub use to_caller::ToCaller;
pub use to_self::ToSelf;
29 changes: 29 additions & 0 deletions framework/base/src/types/interaction/markers/gas_left.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use crate::{
api::{BlockchainApi, BlockchainApiImpl, ManagedTypeApi},
formatter::FormatBuffer,
types::{
interaction::display_u64, AnnotatedValue, BigUint, ManagedAddress, ManagedBuffer,
ManagedBufferCachedBuilder, TxCodeValue, TxEgldValue, TxEnv, TxFrom, TxFromSpecified,
TxGasValue, TxTo, TxToSpecified,
},
};

/// Indicates that all remaining gas should be sent to a transaction.
///
/// Usually unwise, other than for synchronous calls, you always want to have some gas left in the contract after the call.
pub struct GasLeft;

impl<Env> AnnotatedValue<Env, u64> for GasLeft
where
Env: TxEnv,
{
fn annotation(&self, env: &Env) -> ManagedBuffer<Env::Api> {
display_u64(self.to_value(env))
}

fn to_value(&self, env: &Env) -> u64 {
Env::Api::blockchain_api_impl().get_gas_left()
}
}

impl<Env> TxGasValue<Env> for GasLeft where Env: TxEnv {}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::{
api::{const_handles, use_raw_handle, BlockchainApi, BlockchainApiImpl, CallTypeApi},
contract_base::BlockchainWrapper,
types::{AnnotatedValue, ManagedAddress, ManagedBuffer, ManagedType, TxScEnv},
types::{
AnnotatedValue, ManagedAddress, ManagedBuffer, ManagedType, TxScEnv, TxTo, TxToSpecified,
},
};

use super::{TxTo, TxToSpecified};

/// Indicates that transaction should be sent to the caller (the sender of the current transaction).
pub struct ToCaller;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::{
api::{const_handles, use_raw_handle, BlockchainApi, BlockchainApiImpl, CallTypeApi},
contract_base::BlockchainWrapper,
types::{AnnotatedValue, ManagedAddress, ManagedBuffer, ManagedType, TxScEnv},
types::{
AnnotatedValue, ManagedAddress, ManagedBuffer, ManagedType, TxScEnv, TxTo, TxToSpecified,
},
};

use super::{TxTo, TxToSpecified};

/// Indicates that transaction should be sent to itself.
pub struct ToSelf;

Expand Down
2 changes: 2 additions & 0 deletions framework/base/src/types/interaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ mod deploy_call;
mod expr;
mod function_call;
mod managed_arg_buffer;
mod markers;
mod tx;
mod tx_call_async;
mod tx_call_async_promises;
Expand Down Expand Up @@ -60,6 +61,7 @@ pub use deploy_call::*;
pub use expr::*;
pub use function_call::FunctionCall;
pub use managed_arg_buffer::ManagedArgBuffer;
pub use markers::*;
pub use tx::*;
pub use tx_call_async::*;
pub use tx_call_async_promises::*;
Expand Down
6 changes: 0 additions & 6 deletions framework/base/src/types/interaction/tx_to.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
mod tx_to_caller;
mod tx_to_self;

pub use tx_to_caller::ToCaller;
pub use tx_to_self::ToSelf;

use crate::types::{heap::Address, ManagedAddress};

use super::{AnnotatedValue, TxEnv};
Expand Down
2 changes: 2 additions & 0 deletions framework/scenario/src/facade/world_tx/expr.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
mod file_expr;
mod mxsc_expr;
mod num_expr;

pub use file_expr::FileExpr;
pub use mxsc_expr::MxscExpr;
pub use num_expr::NumExpr;
53 changes: 53 additions & 0 deletions framework/scenario/src/facade/world_tx/expr/num_expr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use crate::{api::StaticApi, ScenarioEnvExec, ScenarioTxEnv, ScenarioTxEnvData};
use core::ptr;
use multiversx_chain_scenario_format::{
interpret_trait::InterpreterContext, value_interpreter::interpret_string,
};
use multiversx_sc::{
api::ManagedTypeApi,
types::{
AnnotatedValue, BigUint, ManagedAddress, ManagedBuffer, TxCodeValue, TxEgldValue, TxEnv,
TxFrom, TxFromSpecified, TxGasValue, TxTo, TxToSpecified,
},
};
use std::path::PathBuf;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct NumExpr<'a>(pub &'a str);

fn interpret_big_uint<Api>(s: &str) -> BigUint<Api>
where
Api: ManagedTypeApi,
{
let bytes = interpret_string(s, &InterpreterContext::new());
BigUint::from_bytes_be(&bytes)
}

impl<'a, Env> AnnotatedValue<Env, BigUint<Env::Api>> for NumExpr<'a>
where
Env: ScenarioTxEnv,
{
fn annotation(&self, _env: &Env) -> ManagedBuffer<Env::Api> {
self.0.into()
}

fn to_value(&self, env: &Env) -> BigUint<Env::Api> {
interpret_big_uint(self.0)
}
}

impl<'a, Env> AnnotatedValue<Env, u64> for NumExpr<'a>
where
Env: ScenarioTxEnv,
{
fn annotation(&self, _env: &Env) -> ManagedBuffer<Env::Api> {
self.0.into()
}

fn to_value(&self, env: &Env) -> u64 {
interpret_big_uint::<Env::Api>(self.0).to_u64().unwrap()
}
}

impl<'a, Env> TxEgldValue<Env> for NumExpr<'a> where Env: ScenarioTxEnv {}
impl<'a, Env> TxGasValue<Env> for NumExpr<'a> where Env: ScenarioTxEnv {}

0 comments on commit 928ca50

Please sign in to comment.