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
//! A middleware supporting development-specific JSON RPC methods
//!
//! # Example
//!
//! ```no_run
//! use ethers_providers::{Provider, Http, Middleware, DevRpcMiddleware};
//! use ethers_core::types::TransactionRequest;
//! use ethers_core::utils::Anvil;
//!
//! # async fn foo() -> Result<(), Box<dyn std::error::Error>> {
//! let anvil = Anvil::new().spawn();
//! let provider = Provider::<Http>::try_from(anvil.endpoint())?;
//! let client = DevRpcMiddleware::new(provider);
//!
//! // snapshot the initial state
//! let block0 = client.get_block_number().await?;
//! let snap_id = client.snapshot().await?;
//!
//! // send a transaction
//! let accounts = client.get_accounts().await?;
//! let from = accounts[0];
//! let to = accounts[1];
//! let balance_before = client.get_balance(to, None).await?;
//! let tx = TransactionRequest::new().to(to).value(1000).from(from);
//! client.send_transaction(tx, None).await?.await?;
//! let balance_after = client.get_balance(to, None).await?;
//! assert_eq!(balance_after, balance_before + 1000);
//!
//! // revert to snapshot
//! client.revert_to_snapshot(snap_id).await?;
//! let balance_after_revert = client.get_balance(to, None).await?;
//! assert_eq!(balance_after_revert, balance_before);
//! # Ok(()) }
//! ```

use crate::{Middleware, MiddlewareError, ProviderError};
use async_trait::async_trait;
use ethers_core::types::U256;
use thiserror::Error;

use std::fmt::Debug;

/// `DevRpcMiddleware`
#[derive(Clone, Debug)]
pub struct DevRpcMiddleware<M>(M);

/// DevRpcMiddleware Errors
#[derive(Error, Debug)]
pub enum DevRpcMiddlewareError<M: Middleware> {
    /// Internal Middleware error
    #[error("{0}")]
    MiddlewareError(M::Error),

    /// Internal Provider error
    #[error("{0}")]
    ProviderError(ProviderError),

    /// Attempted to revert to unavailable snapshot
    #[error("Could not revert to snapshot")]
    NoSnapshot,
}

#[async_trait]
impl<M: Middleware> Middleware for DevRpcMiddleware<M> {
    type Error = DevRpcMiddlewareError<M>;
    type Provider = M::Provider;
    type Inner = M;

    fn inner(&self) -> &M {
        &self.0
    }
}

impl<M: Middleware> MiddlewareError for DevRpcMiddlewareError<M> {
    type Inner = M::Error;

    fn from_err(src: M::Error) -> DevRpcMiddlewareError<M> {
        DevRpcMiddlewareError::MiddlewareError(src)
    }

    fn as_inner(&self) -> Option<&Self::Inner> {
        match self {
            DevRpcMiddlewareError::MiddlewareError(e) => Some(e),
            _ => None,
        }
    }
}

impl<M> From<ProviderError> for DevRpcMiddlewareError<M>
where
    M: Middleware,
{
    fn from(src: ProviderError) -> Self {
        Self::ProviderError(src)
    }
}

impl<M: Middleware> DevRpcMiddleware<M> {
    /// Instantiate a new `DevRpcMiddleware`
    pub fn new(inner: M) -> Self {
        Self(inner)
    }

    /// Create a new snapshot on the DevRpc node. Return the Snapshot ID
    ///
    /// ### Note
    ///
    /// Ganache, Hardhat and Anvil increment snapshot ID even if no state has changed
    pub async fn snapshot(&self) -> Result<U256, DevRpcMiddlewareError<M>> {
        self.provider().request::<(), U256>("evm_snapshot", ()).await.map_err(From::from)
    }

    /// Revert the state of the DevRpc node to the Snapshot, specified by its ID
    pub async fn revert_to_snapshot(&self, id: U256) -> Result<(), DevRpcMiddlewareError<M>> {
        let ok = self
            .provider()
            .request::<[U256; 1], bool>("evm_revert", [id])
            .await
            .map_err(DevRpcMiddlewareError::ProviderError)?;
        if ok {
            Ok(())
        } else {
            Err(DevRpcMiddlewareError::NoSnapshot)
        }
    }
}

#[cfg(test)]
// Celo blocks can not get parsed when used with Ganache
#[cfg(not(feature = "celo"))]
mod tests {
    use super::*;
    use crate::{Http, Provider};
    use ethers_core::utils::Anvil;
    use std::convert::TryFrom;

    #[tokio::test]
    async fn test_snapshot() {
        let anvil = Anvil::new().spawn();
        let provider = Provider::<Http>::try_from(anvil.endpoint()).unwrap();
        let client = DevRpcMiddleware::new(provider);

        // snapshot initial state
        let block0 = client.get_block_number().await.unwrap();
        let time0 = client.get_block(block0).await.unwrap().unwrap().timestamp;
        let snap_id0 = client.snapshot().await.unwrap();

        // mine a new block
        client.provider().mine(1).await.unwrap();

        // snapshot state
        let block1 = client.get_block_number().await.unwrap();
        let time1 = client.get_block(block1).await.unwrap().unwrap().timestamp;
        let snap_id1 = client.snapshot().await.unwrap();

        // mine some blocks
        client.provider().mine(5).await.unwrap();

        // snapshot state
        let block2 = client.get_block_number().await.unwrap();
        let time2 = client.get_block(block2).await.unwrap().unwrap().timestamp;
        let snap_id2 = client.snapshot().await.unwrap();

        // mine some blocks
        client.provider().mine(5).await.unwrap();

        // revert_to_snapshot should reset state to snap id
        client.revert_to_snapshot(snap_id2).await.unwrap();
        let block = client.get_block_number().await.unwrap();
        let time = client.get_block(block).await.unwrap().unwrap().timestamp;
        assert_eq!(block, block2);
        assert_eq!(time, time2);

        client.revert_to_snapshot(snap_id1).await.unwrap();
        let block = client.get_block_number().await.unwrap();
        let time = client.get_block(block).await.unwrap().unwrap().timestamp;
        assert_eq!(block, block1);
        assert_eq!(time, time1);

        // revert_to_snapshot should throw given non-existent or
        // previously used snapshot
        let result = client.revert_to_snapshot(snap_id1).await;
        assert!(result.is_err());

        client.revert_to_snapshot(snap_id0).await.unwrap();
        let block = client.get_block_number().await.unwrap();
        let time = client.get_block(block).await.unwrap().unwrap().timestamp;
        assert_eq!(block, block0);
        assert_eq!(time, time0);
    }
}