1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
// Copyright 2022-2023, Offchain Labs, Inc.
// For licensing, see https://github.com/OffchainLabs/stylus-sdk-rs/blob/stylus/licenses/COPYRIGHT.md
use crate::storage::TopLevelStorage;
use super::{CallContext, MutatingCallContext, NonPayableCallContext, StaticCallContext};
use alloy_primitives::U256;
use cfg_if::cfg_if;
/// Enables configurable calls to other contracts.
#[derive(Debug, Clone)]
pub struct Call<S, const HAS_VALUE: bool = false> {
gas: u64,
value: Option<U256>,
storage: S,
}
impl<'a, S: TopLevelStorage> Call<&'a mut S, false>
where
S: TopLevelStorage + 'a,
{
/// 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.
///
/// ```no_run
/// 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) 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
/// }
/// ```
///
/// Projects that opt out of the [`StorageCache`] by disabling the `storage-cache` feature
/// may ignore this method.
///
/// [`StorageCache`]: crate::storage::StorageCache
/// [`flush`]: crate::storage::StorageCache::flush
/// [`clear`]: crate::storage::StorageCache::clear
/// [`new_in`]: Call::new_in
/// [`new`]: Call::new
pub fn new_in(storage: &'a mut S) -> Self {
Self {
gas: u64::MAX,
value: None,
storage,
}
}
}
impl<S, const HAS_VALUE: bool> Call<S, HAS_VALUE> {
/// Amount of gas to supply the call.
/// Values greater than the amount provided will be clipped to all gas left.
pub fn gas(self, gas: u64) -> Self {
Self { gas, ..self }
}
/// Amount of ETH in wei to give the other contract.
/// Note: adding value will prevent calls to non-payable methods.
pub fn value(self, value: U256) -> Call<S, true> {
Call {
value: Some(value),
gas: self.gas,
storage: self.storage,
}
}
}
impl<S, const HAS_VALUE: bool> CallContext for Call<S, HAS_VALUE> {
fn gas(&self) -> u64 {
self.gas
}
}
// allow &self as a context
impl<'a, T> CallContext for &'a T
where
T: TopLevelStorage,
{
fn gas(&self) -> u64 {
u64::MAX
}
}
// allow &mut self as a context
impl<T> CallContext for &mut T
where
T: TopLevelStorage,
{
fn gas(&self) -> u64 {
u64::MAX
}
}
// allow &self to be a `pure` and `static` call context
impl<'a, T> StaticCallContext for &'a T where T: TopLevelStorage {}
// allow &mut self to be a `pure` and `static` call context
impl<'a, T> StaticCallContext for &'a mut T where T: TopLevelStorage {}
// allow &mut self to be a `write` and `payable` call context
unsafe impl<T> MutatingCallContext for &mut T
where
T: TopLevelStorage,
{
fn value(&self) -> U256 {
U256::ZERO
}
}
// allow &mut self to be a `write`-only call context
impl<T> NonPayableCallContext for &mut T where T: TopLevelStorage {}
cfg_if! {
if #[cfg(all(feature = "storage-cache", feature = "reentrant"))] {
// The following impls safeguard state during reentrancy scenarios
impl<S: TopLevelStorage> StaticCallContext for Call<&S, false> {}
impl<S: TopLevelStorage> StaticCallContext for Call<&mut S, false> {}
impl<S: TopLevelStorage> NonPayableCallContext for Call<&mut S, false> {}
unsafe impl<S: TopLevelStorage, const HAS_VALUE: bool> MutatingCallContext
for Call<&mut S, HAS_VALUE>
{
fn value(&self) -> U256 {
self.value.unwrap_or_default()
}
}
} else {
// If there's no reentrancy, all calls are storage safe
impl<S> StaticCallContext for Call<S, false> {}
impl<S> NonPayableCallContext for Call<S, false> {}
unsafe impl<S, const HAS_VALUE: bool> MutatingCallContext for Call<S, HAS_VALUE> {
fn value(&self) -> U256 {
self.value.unwrap_or_default()
}
}
}
}
cfg_if! {
if #[cfg(any(all(feature = "storage-cache", feature = "reentrant"), feature = "docs"))] {
impl Default for Call<(), false> {
fn default() -> Self {
Self::new()
}
}
impl Call<(), false> {
/// Begin configuring a call, similar to how [`RawCall`](super::RawCall) and
/// [`std::fs::OpenOptions`][OpenOptions] work.
///
/// ```ignore
/// 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) 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
/// }
/// ```
///
/// [OpenOptions]: https://doc.rust-lang.org/stable/std/fs/struct.OpenOptions.html
pub fn new() -> Self {
Self {
gas: u64::MAX,
value: None,
storage: (),
}
}
}
}
}