Struct Call

Source
pub struct Call<S, const HAS_VALUE: bool = false> { /* private fields */ }
๐Ÿ‘ŽDeprecated since 0.8.0: Use the Call struct defined in stylus_core::calls::context instead
Expand description

Enables configurable calls to other contracts.

Implementationsยง

Sourceยง

impl<'a, S> Call<&'a mut S, false>
where S: TopLevelStorage + 'a,

Source

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>

Source

pub fn gas(self, gas: u64) -> Self

Amount of gas to supply the call. Values greater than the amount provided will be clipped to all gas left.

Source

pub fn value(self, value: U256) -> Call<S, true>

Amount of ETH in wei to give the other contract. Note: adding value will prevent calls to non-payable methods.

Sourceยง

impl Call<(), false>

Source

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>

Sourceยง

fn gas(&self) -> u64

Amount of gas to supply the call. Note: values are clipped to the amount of gas remaining.
Sourceยง

impl<S: Clone, const HAS_VALUE: bool> Clone for Call<S, HAS_VALUE>

Sourceยง

fn clone(&self) -> Call<S, HAS_VALUE>

Returns a copy of the value. Read more
1.0.0 ยท Sourceยง

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Sourceยง

impl<S: Debug, const HAS_VALUE: bool> Debug for Call<S, HAS_VALUE>

Sourceยง

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Sourceยง

impl Default for Call<(), false>

Sourceยง

fn default() -> Self

Returns the โ€œdefault valueโ€ for a type. Read more
Sourceยง

impl<S, const HAS_VALUE: bool> MutatingCallContext for Call<S, HAS_VALUE>

Sourceยง

fn value(&self) -> U256

Amount of ETH in wei to give the other contract.
Sourceยง

impl<S> NonPayableCallContext for Call<S, false>

Sourceยง

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> Any for T
where T: 'static + ?Sized,

Sourceยง

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Sourceยง

impl<T> Borrow<T> for T
where T: ?Sized,

Sourceยง

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Sourceยง

impl<T> BorrowMut<T> for T
where T: ?Sized,

Sourceยง

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Sourceยง

impl<T> CloneToUninit for T
where T: Clone,

Sourceยง

unsafe fn clone_to_uninit(&self, dest: *mut u8)

๐Ÿ”ฌThis is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Sourceยง

impl<T> DynClone for T
where T: Clone,

Sourceยง

fn __clone_box(&self, _: Private) -> *mut ()

Sourceยง

impl<T> From<T> for T

Sourceยง

fn from(t: T) -> T

Returns the argument unchanged.

Sourceยง

impl<T> Instrument for T

Sourceยง

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Sourceยง

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Sourceยง

impl<T, U> Into<U> for T
where U: From<T>,

Sourceยง

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Sourceยง

impl<T> IntoEither for T

Sourceยง

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Sourceยง

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Sourceยง

impl<T> Same for T

Sourceยง

type Output = T

Should always be Self
Sourceยง

impl<T> ToOwned for T
where T: Clone,

Sourceยง

type Owned = T

The resulting type after obtaining ownership.
Sourceยง

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Sourceยง

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Sourceยง

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Sourceยง

type Error = Infallible

The type returned in the event of a conversion error.
Sourceยง

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Sourceยง

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Sourceยง

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Sourceยง

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Sourceยง

impl<T> WithSubscriber for T

Sourceยง

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Sourceยง

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Sourceยง

impl<T> ErasedDestructor for T
where T: 'static,

Sourceยง

impl<T> MaybeSendSync for T