pub struct Call<S, const HAS_VALUE: bool = false> { /* private fields */ }
Expand description
Enables configurable calls to other contracts.
Implementationsยง
Sourceยงimpl<'a, S> Call<&'a mut S, false>where
S: TopLevelStorage + 'a,
impl<'a, S> Call<&'a mut S, false>where
S: TopLevelStorage + 'a,
Sourcepub fn new_in(storage: &'a mut S) -> Self
pub fn new_in(storage: &'a mut S) -> Self
Similar to new
, but intended for projects and libraries using reentrant patterns.
new_in
safeguards persistent storage by requiring a reference to a TopLevelStorage
struct
.
Recall that TopLevelStorage
is special in that a reference to it represents access to the entire
contractโs state. So that itโs sound to flush
or clear
the StorageCache
when calling out
to other contracts, calls that may induce reentrancy require an &
or &mut
to one.
Although this reference to TopLevelStorage
is not used, the lifetime is still required
to ensure safety of the storage cache.
use stylus_sdk::call::{Call, Error};
use stylus_sdk::{prelude::*, evm, msg, alloy_primitives::Address};
use stylus_core::storage::TopLevelStorage;
extern crate alloc;
sol_interface! {
interface IService {
function makePayment(address user) external payable returns (string);
}
}
pub fn do_call(
storage: &mut impl TopLevelStorage, // can be generic, but often just &mut self
account: IService, // serializes as an Address
user: Address,
) -> Result<String, Error> {
let config = Call::new_in(storage)
.gas(evm::gas_left() / 2) // limit to half the gas left
.value(msg::value()); // set the callvalue
account.make_payment(config, user) // note the snake case
}
Sourceยงimpl<S, const HAS_VALUE: bool> Call<S, HAS_VALUE>
impl<S, const HAS_VALUE: bool> Call<S, HAS_VALUE>
Sourceยงimpl Call<(), false>
impl Call<(), false>
Sourcepub fn new() -> Self
pub fn new() -> Self
Begin configuring a call, similar to how RawCall
and
std::fs::OpenOptions
work.
This is not available if reentrant
feature is enabled, as it may lead to
vulnerability to reentrancy attacks. See Call::new_in
.
use stylus_sdk::call::{Call, Error};
use stylus_sdk::{prelude::*, evm, msg, alloy_primitives::Address};
extern crate alloc;
sol_interface! {
interface IService {
function makePayment(address user) external payable returns (string);
}
}
pub fn do_call(account: IService, user: Address) -> Result<String, Error> {
let config = Call::new()
.gas(evm::gas_left() / 2) // limit to half the gas left
.value(msg::value()); // set the callvalue
account.make_payment(config, user) // note the snake case
}
Trait Implementationsยง
Sourceยงimpl<S, const HAS_VALUE: bool> CallContext for Call<S, HAS_VALUE>
impl<S, const HAS_VALUE: bool> CallContext for Call<S, HAS_VALUE>
Sourceยงimpl<S, const HAS_VALUE: bool> MutatingCallContext for Call<S, HAS_VALUE>
impl<S, const HAS_VALUE: bool> MutatingCallContext for Call<S, HAS_VALUE>
impl<S> NonPayableCallContext for Call<S, false>
impl<S> StaticCallContext for Call<S, false>
Auto Trait Implementationsยง
impl<S, const HAS_VALUE: bool> Freeze for Call<S, HAS_VALUE>where
S: Freeze,
impl<S, const HAS_VALUE: bool> RefUnwindSafe for Call<S, HAS_VALUE>where
S: RefUnwindSafe,
impl<S, const HAS_VALUE: bool> Send for Call<S, HAS_VALUE>where
S: Send,
impl<S, const HAS_VALUE: bool> Sync for Call<S, HAS_VALUE>where
S: Sync,
impl<S, const HAS_VALUE: bool> Unpin for Call<S, HAS_VALUE>where
S: Unpin,
impl<S, const HAS_VALUE: bool> UnwindSafe for Call<S, HAS_VALUE>where
S: UnwindSafe,
Blanket Implementationsยง
Sourceยงimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Sourceยงfn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Sourceยงimpl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Sourceยงimpl<T> Instrument for T
impl<T> Instrument for T
Sourceยงfn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Sourceยงfn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Sourceยงimpl<T> IntoEither for T
impl<T> IntoEither for T
Sourceยงfn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSourceยงfn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more