example_erc20_gas/
main.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
//! Example of a custom handler for ERC20 gas calculation.
//!
//! Gas is going to be deducted from ERC20 token.

#![cfg_attr(not(test), warn(unused_crate_dependencies))]

use alloy_provider::{network::Ethereum, ProviderBuilder, RootProvider};
use alloy_sol_types::{sol, SolCall, SolValue};
use alloy_transport_http::Http;
use anyhow::{anyhow, Result};
use database::{AlloyDB, BlockId, CacheDB};
use reqwest::{Client, Url};
use revm::{
    context_interface::{
        result::{ExecutionResult, InvalidHeader, InvalidTransaction, Output},
        Journal, JournalDBError, JournalGetter,
    },
    database_interface::WrapDatabaseAsync,
    handler::EthExecution,
    precompile::PrecompileErrors,
    primitives::{address, keccak256, Address, Bytes, TxKind, U256},
    state::{AccountInfo, EvmStorageSlot},
    Context, EvmCommit, MainEvm,
};

pub mod handlers;
use handlers::{CustomEvm, CustomHandler, Erc20PostExecution, Erc20PreExecution, Erc20Validation};

type AlloyCacheDB =
    CacheDB<WrapDatabaseAsync<AlloyDB<Http<Client>, Ethereum, RootProvider<Http<Client>>>>>;

// Constants
pub const TOKEN: Address = address!("1234567890123456789012345678901234567890");
pub const TREASURY: Address = address!("0000000000000000000000000000000000000001");

#[tokio::main]
async fn main() -> Result<()> {
    // Set up the HTTP transport which is consumed by the RPC client.
    let rpc_url: Url = "https://mainnet.infura.io/v3/c60b0bb42f8a4c6481ecd229eddaca27".parse()?;

    let client = ProviderBuilder::new().on_http(rpc_url);

    let alloy = WrapDatabaseAsync::new(AlloyDB::new(client, BlockId::latest())).unwrap();
    let mut cache_db = CacheDB::new(alloy);

    // Random empty account: From
    let account = address!("18B06aaF27d44B756FCF16Ca20C1f183EB49111f");
    // Random empty account: To
    let account_to = address!("21a4B6F62E51e59274b6Be1705c7c68781B87C77");

    let usdc = address!("a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48");

    // USDC has 6 decimals
    let hundred_tokens = U256::from(100_000_000_000_000_000u128);

    let balance_slot = keccak256((account, U256::from(3)).abi_encode()).into();

    cache_db
        .insert_account_storage(usdc, balance_slot, hundred_tokens)
        .unwrap();
    cache_db.insert_account_info(
        account,
        AccountInfo {
            nonce: 0,
            balance: hundred_tokens,
            code_hash: keccak256(Bytes::new()),
            code: None,
        },
    );

    let balance_before = balance_of(usdc, account, &mut cache_db).unwrap();

    // Transfer 100 tokens from account to account_to
    // Magic happens here with custom handlers
    transfer(account, account_to, hundred_tokens, usdc, &mut cache_db)?;

    let balance_after = balance_of(usdc, account, &mut cache_db)?;

    println!("Balance before: {balance_before}");
    println!("Balance after: {balance_after}");

    Ok(())
}

/// Helpers
pub fn token_operation<CTX, ERROR>(
    context: &mut CTX,
    sender: Address,
    recipient: Address,
    amount: U256,
) -> Result<(), ERROR>
where
    CTX: JournalGetter,
    ERROR: From<InvalidTransaction>
        + From<InvalidHeader>
        + From<JournalDBError<CTX>>
        + From<PrecompileErrors>,
{
    let token_account = context.journal().load_account(TOKEN)?.data;

    let sender_balance_slot: U256 = keccak256((sender, U256::from(3)).abi_encode()).into();
    let sender_balance = token_account
        .storage
        .get(&sender_balance_slot)
        .expect("Balance slot not found")
        .present_value();

    if sender_balance < amount {
        return Err(ERROR::from(
            InvalidTransaction::MaxFeePerBlobGasNotSupported,
        ));
    }
    // Subtract the amount from the sender's balance
    let sender_new_balance = sender_balance.saturating_sub(amount);
    token_account.storage.insert(
        sender_balance_slot,
        EvmStorageSlot::new_changed(sender_balance, sender_new_balance),
    );

    // Add the amount to the recipient's balance
    let recipient_balance_slot: U256 = keccak256((recipient, U256::from(3)).abi_encode()).into();
    let recipient_balance = token_account
        .storage
        .get(&recipient_balance_slot)
        .expect("To balance slot not found")
        .present_value();
    let recipient_new_balance = recipient_balance.saturating_add(amount);
    token_account.storage.insert(
        recipient_balance_slot,
        EvmStorageSlot::new_changed(recipient_balance, recipient_new_balance),
    );

    Ok(())
}

fn balance_of(token: Address, address: Address, alloy_db: &mut AlloyCacheDB) -> Result<U256> {
    sol! {
        function balanceOf(address account) public returns (uint256);
    }

    let encoded = balanceOfCall { account: address }.abi_encode();

    let mut evm = MainEvm::new(
        Context::builder()
            .with_db(alloy_db)
            .modify_tx_chained(|tx| {
                // 0x1 because calling USDC proxy from zero address fails
                tx.caller = address!("0000000000000000000000000000000000000001");
                tx.kind = TxKind::Call(token);
                tx.data = encoded.into();
                tx.value = U256::from(0);
            }),
        CustomHandler::default(),
    );

    let ref_tx = evm.exec_commit().unwrap();
    let value = match ref_tx {
        ExecutionResult::Success {
            output: Output::Call(value),
            ..
        } => value,
        result => return Err(anyhow!("'balanceOf' execution failed: {result:?}")),
    };

    let balance = <U256>::abi_decode(&value, false)?;

    Ok(balance)
}

fn transfer(
    from: Address,
    to: Address,
    amount: U256,
    token: Address,
    cache_db: &mut AlloyCacheDB,
) -> Result<()> {
    sol! {
        function transfer(address to, uint amount) external returns (bool);
    }

    let encoded = transferCall { to, amount }.abi_encode();

    let mut evm = CustomEvm::new(
        Context::builder()
            .with_db(cache_db)
            .modify_tx_chained(|tx| {
                tx.caller = from;
                tx.kind = TxKind::Call(token);
                tx.data = encoded.into();
                tx.value = U256::from(0);
            }),
        CustomHandler::new(
            Erc20Validation::new(),
            Erc20PreExecution::new(),
            EthExecution::new(),
            Erc20PostExecution::new(),
        ),
    );
    let ref_tx = evm.exec_commit().unwrap();
    let success: bool = match ref_tx {
        ExecutionResult::Success {
            output: Output::Call(value),
            ..
        } => <bool>::abi_decode(&value, false)?,
        result => return Err(anyhow!("'transfer' execution failed: {result:?}")),
    };

    if !success {
        return Err(anyhow!("'transfer' failed"));
    }

    Ok(())
}