alloy_provider/provider/eth_call/
call_many.rs

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
210
211
212
213
use std::{marker::PhantomData, sync::Arc, task::Poll};

use alloy_eips::BlockId;
use alloy_json_rpc::RpcRecv;
use alloy_network::Network;
use alloy_rpc_types_eth::{state::StateOverride, Bundle, StateContext, TransactionIndex};
use alloy_transport::TransportResult;
use futures::{future, FutureExt};

use crate::ProviderCall;

use super::{Caller, EthCallManyParams};

/// A builder for an `"eth_callMany"` RPC request.
#[derive(Clone)]
pub struct EthCallMany<'req, N, Resp: RpcRecv, Output = Resp, Map = fn(Resp) -> Output>
where
    N: Network,
    Resp: RpcRecv,
    Map: Fn(Resp) -> Output,
{
    caller: Arc<dyn Caller<N, Resp>>,
    params: EthCallManyParams<'req>,
    map: Map,
    _pd: PhantomData<fn() -> (Resp, Output)>,
}

impl<N, Resp, Output, Map> std::fmt::Debug for EthCallMany<'_, N, Resp, Output, Map>
where
    N: Network,
    Resp: RpcRecv,
    Map: Fn(Resp) -> Output,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("EthCallMany")
            .field("params", &self.params)
            .field("method", &"eth_callMany")
            .finish()
    }
}

impl<'req, N, Resp> EthCallMany<'req, N, Resp>
where
    N: Network,
    Resp: RpcRecv,
{
    /// Instantiates a new `EthCallMany` with the given parameters.
    pub fn new(caller: impl Caller<N, Resp> + 'static, bundles: &'req Vec<Bundle>) -> Self {
        Self {
            caller: Arc::new(caller),
            params: EthCallManyParams::new(bundles),
            map: std::convert::identity,
            _pd: PhantomData,
        }
    }
}

impl<'req, N, Resp, Output, Map> EthCallMany<'req, N, Resp, Output, Map>
where
    N: Network,
    Resp: RpcRecv,
    Map: Fn(Resp) -> Output,
{
    /// Set a mapping function to transform the response.
    pub fn map<NewOutput, NewMap>(
        self,
        map: NewMap,
    ) -> EthCallMany<'req, N, Resp, NewOutput, NewMap>
    where
        NewMap: Fn(Resp) -> NewOutput,
    {
        EthCallMany { caller: self.caller, params: self.params, map, _pd: PhantomData }
    }

    /// Set the [`BlockId`] in the [`StateContext`].
    pub fn block(mut self, block: BlockId) -> Self {
        self.params = self.params.with_block(block);
        self
    }

    /// Set the [`TransactionIndex`] in the [`StateContext`].
    pub fn transaction_index(mut self, tx_index: TransactionIndex) -> Self {
        self.params = self.params.with_transaction_index(tx_index);
        self
    }

    /// Set the [`StateContext`] for the call.
    pub fn context(mut self, context: &'req StateContext) -> Self {
        self.params = self.params.with_context(*context);
        self
    }

    /// Set the [`StateOverride`] for the call.
    pub fn overrides(mut self, overrides: &'req StateOverride) -> Self {
        self.params = self.params.with_overrides(overrides);
        self
    }

    /// Extend the bundles for the call.
    pub fn extend_bundles(mut self, bundles: &'req [Bundle]) -> Self {
        self.params.bundles_mut().extend_from_slice(bundles);
        self
    }
}

impl<'req, N, Resp, Output, Map> std::future::IntoFuture for EthCallMany<'req, N, Resp, Output, Map>
where
    N: Network,
    Resp: RpcRecv,
    Map: Fn(Resp) -> Output,
{
    type Output = TransportResult<Output>;

    type IntoFuture = CallManyFut<'req, N, Resp, Output, Map>;

    fn into_future(self) -> Self::IntoFuture {
        CallManyFut {
            inner: CallManyInnerFut::Preparing {
                caller: self.caller,
                params: self.params,
                map: self.map,
            },
        }
    }
}

/// Intermediate future for `"eth_callMany"` requests.
#[derive(Debug)]
#[doc(hidden)] // Not public API.
#[allow(unnameable_types)]
#[pin_project::pin_project]
pub struct CallManyFut<'req, N: Network, Resp: RpcRecv, Output, Map: Fn(Resp) -> Output> {
    inner: CallManyInnerFut<'req, N, Resp, Output, Map>,
}

impl<N, Resp, Output, Map> CallManyFut<'_, N, Resp, Output, Map>
where
    N: Network,
    Resp: RpcRecv,
    Map: Fn(Resp) -> Output,
{
    const fn is_preparing(&self) -> bool {
        matches!(self.inner, CallManyInnerFut::Preparing { .. })
    }

    const fn is_running(&self) -> bool {
        matches!(self.inner, CallManyInnerFut::Running { .. })
    }

    fn poll_preparing(&mut self, cx: &mut std::task::Context<'_>) -> Poll<TransportResult<Output>> {
        let CallManyInnerFut::Preparing { caller, params, map } =
            std::mem::replace(&mut self.inner, CallManyInnerFut::Polling)
        else {
            unreachable!("bad state");
        };

        let fut = caller.call_many(params)?;
        self.inner = CallManyInnerFut::Running { fut, map };
        self.poll_running(cx)
    }

    fn poll_running(&mut self, cx: &mut std::task::Context<'_>) -> Poll<TransportResult<Output>> {
        let CallManyInnerFut::Running { ref mut fut, ref map } = self.inner else {
            unreachable!("bad state");
        };

        fut.poll_unpin(cx).map(|res| res.map(map))
    }
}

impl<N, Resp, Output, Map> future::Future for CallManyFut<'_, N, Resp, Output, Map>
where
    N: Network,
    Resp: RpcRecv,
    Map: Fn(Resp) -> Output,
{
    type Output = TransportResult<Output>;

    fn poll(self: std::pin::Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> Poll<Self::Output> {
        let this = self.get_mut();

        if this.is_preparing() {
            this.poll_preparing(cx)
        } else if this.is_running() {
            this.poll_running(cx)
        } else {
            panic!("bad state");
        }
    }
}

enum CallManyInnerFut<'req, N: Network, Resp: RpcRecv, Output, Map: Fn(Resp) -> Output> {
    Preparing { caller: Arc<dyn Caller<N, Resp>>, params: EthCallManyParams<'req>, map: Map },
    Running { fut: ProviderCall<EthCallManyParams<'static>, Resp>, map: Map },
    Polling,
}

impl<N, Resp, Output, Map> std::fmt::Debug for CallManyInnerFut<'_, N, Resp, Output, Map>
where
    N: Network,
    Resp: RpcRecv,
    Map: Fn(Resp) -> Output,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            CallManyInnerFut::Preparing { params, .. } => {
                f.debug_tuple("Preparing").field(&params).finish()
            }
            CallManyInnerFut::Running { .. } => f.debug_tuple("Running").finish(),
            CallManyInnerFut::Polling => f.debug_tuple("Polling").finish(),
        }
    }
}