Skip to content

Commit

Permalink
fix uniswap usdc swap
Browse files Browse the repository at this point in the history
  • Loading branch information
rakita committed Sep 20, 2024
1 parent 2f3f6d1 commit f47bb7f
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 33 deletions.
15 changes: 15 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ members = [
# examples
"examples/block_traces",
"examples/contract_deployment",
#"examples/custom_opcodes",
"examples/database_components",
"examples/database_ref",
"examples/uniswap_get_reserves",
#"examples/uniswap_v2_usdc_swap",
"examples/uniswap_v2_usdc_swap",
#"examples/custom_opcodes",
]

[workspace.dependencies]
Expand Down
2 changes: 1 addition & 1 deletion bins/revme/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ repository.workspace = true
# revm
database.workspace = true
revm = { workspace = true, features = ["std", "hashbrown", "c-kzg", "blst"] }
inspector = { workspace = true, features = ["std", "serde", "serde-json"] }
inspector = { workspace = true, features = ["std", "serde-json"] }

hash-db = "0.15"
hex = "0.4"
Expand Down
6 changes: 1 addition & 5 deletions examples/uniswap_get_reserves/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,5 @@ alloy-sol-types = { version = "0.8.2", default-features = false, features = [
alloy-eips = "0.3.6"
alloy-provider = "0.3"

# criterion = "0.5"
# indicatif = "0.17"
# reqwest = { version = "0.12" }
# rstest = "0.22.0"

# mics
anyhow = "1.0.89"
2 changes: 0 additions & 2 deletions examples/uniswap_v2_usdc_swap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,5 @@ alloy-sol-types = { version = "0.8.2", default-features = false, features = [
alloy-eips = "0.3.6"
alloy-transport-http = "0.3"
alloy-provider = "0.3"
indicatif = "0.17"
reqwest = { version = "0.12" }
rstest = "0.22.0"
anyhow = "1.0.89"
53 changes: 30 additions & 23 deletions examples/uniswap_v2_usdc_swap/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
//! Example of uniswap getReserves() call emulation.
#![cfg_attr(not(test), warn(unused_crate_dependencies))]

use alloy_provider::{network::Ethereum, ProviderBuilder, RootProvider};
use alloy_eips::BlockId;
use alloy_provider::{network::Ethereum, ProviderBuilder, RootProvider};
use alloy_sol_types::{sol, SolCall, SolValue};
use alloy_transport_http::Http;
use anyhow::{anyhow, Result};
use reqwest::Client;
use database::{AlloyDB, CacheDB};
use reqwest::Client;
use revm::{
primitives::{
address, keccak256, Address, Bytes, TxKind, U256,
primitives::{address, keccak256, Address, Bytes, TxKind, U256},
state::AccountInfo,
wiring::{
result::{ExecutionResult, Output},
EthereumWiring,
},
state::{AccountInfo},
wiring::{result::{ExecutionResult,Output}},
database_interface::Database,
Evm,
};
use std::ops::Div;
use std::sync::Arc;

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

#[tokio::main]
async fn main() -> Result<()> {
let client = ProviderBuilder::new().on_http(
"https://mainnet.infura.io/v3/c60b0bb42f8a4c6481ecd229eddaca27"
.parse()
.unwrap(),
);
let client = Arc::new(client);
let mut cache_db = CacheDB::new(AlloyDB::new(client, BlockId::default()));
// Set up the HTTP transport which is consumed by the RPC client.
let rpc_url = "https://mainnet.infura.io/v3/c60b0bb42f8a4c6481ecd229eddaca27".parse()?;

// create ethers client and wrap it in Arc<M>
let client = ProviderBuilder::new().on_http(rpc_url);

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

// Random empty account
let account = address!("18B06aaF27d44B756FCF16Ca20C1f183EB49111f");
Expand Down Expand Up @@ -86,18 +86,20 @@ async fn main() -> Result<()> {
let acc_usdc_balance_after = balance_of(usdc, account, &mut cache_db)?;
println!("USDC balance after swap: {}", acc_usdc_balance_after);

println!("OK");
Ok(())
}

fn balance_of(token: Address, address: Address, cache_db: &mut AlloyCacheDB) -> Result<U256> {
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 = Evm::builder()
.with_db(cache_db)
let mut evm = Evm::<EthereumWiring<&mut AlloyCacheDB, ()>>::builder()
.with_db(alloy_db)
.with_default_ext_ctx()
.modify_tx_env(|tx| {
// 0x1 because calling USDC proxy from zero address fails
tx.caller = address!("0000000000000000000000000000000000000001");
Expand Down Expand Up @@ -141,8 +143,9 @@ async fn get_amount_out(
}
.abi_encode();

let mut evm = Evm::builder()
let mut evm = Evm::<EthereumWiring<&mut AlloyCacheDB, ()>>::builder()
.with_db(cache_db)
.with_default_ext_ctx()
.modify_tx_env(|tx| {
tx.caller = address!("0000000000000000000000000000000000000000");
tx.transact_to = TxKind::Call(uniswap_v2_router);
Expand Down Expand Up @@ -174,8 +177,9 @@ fn get_reserves(pair_address: Address, cache_db: &mut AlloyCacheDB) -> Result<(U

let encoded = getReservesCall {}.abi_encode();

let mut evm = Evm::builder()
let mut evm = Evm::<EthereumWiring<&mut AlloyCacheDB, ()>>::builder()
.with_db(cache_db)
.with_default_ext_ctx()
.modify_tx_env(|tx| {
tx.caller = address!("0000000000000000000000000000000000000000");
tx.transact_to = TxKind::Call(pair_address);
Expand Down Expand Up @@ -223,13 +227,15 @@ fn swap(
}
.abi_encode();

let mut evm = Evm::builder()
let mut evm = Evm::<EthereumWiring<&mut AlloyCacheDB, ()>>::builder()
.with_db(cache_db)
.with_default_ext_ctx()
.modify_tx_env(|tx| {
tx.caller = from;
tx.transact_to = TxKind::Call(pool_address);
tx.data = encoded.into();
tx.value = U256::from(0);
tx.nonce = 1;
})
.build();

Expand All @@ -256,8 +262,9 @@ fn transfer(

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

let mut evm = Evm::builder()
let mut evm = Evm::<EthereumWiring<&mut AlloyCacheDB, ()>>::builder()
.with_db(cache_db)
.with_default_ext_ctx()
.modify_tx_env(|tx| {
tx.caller = from;
tx.transact_to = TxKind::Call(token);
Expand Down

0 comments on commit f47bb7f

Please sign in to comment.