Skip to content

Commit

Permalink
disable pair producer and cleanup erc20 producer (#273)
Browse files Browse the repository at this point in the history
  • Loading branch information
shouc authored Oct 25, 2023
1 parent e9ad75b commit d361ba5
Show file tree
Hide file tree
Showing 7 changed files with 8 additions and 30 deletions.
12 changes: 2 additions & 10 deletions src/evm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ use onchain::flashloan::DummyPriceOracle;
use oracles::erc20::IERC20OracleFlashloan;
use oracles::v2_pair::PairBalanceOracle;
use producers::erc20::ERC20Producer;
use producers::pair::PairProducer;
use serde::Deserialize;
use std::cell::RefCell;
use std::collections::HashMap;
Expand Down Expand Up @@ -332,11 +331,10 @@ pub fn evm_main(args: EvmArgs) {
.map(|s| s.to_string())
.collect();
}
let pair_producer = Rc::new(RefCell::new(PairProducer::new()));
let erc20_producer = Rc::new(RefCell::new(ERC20Producer::new()));

let flashloan_oracle = Rc::new(RefCell::new({
IERC20OracleFlashloan::new(pair_producer.clone(), erc20_producer.clone())
IERC20OracleFlashloan::new(erc20_producer.clone())
}));

// let harness_code = "oracle_harness()";
Expand Down Expand Up @@ -391,19 +389,13 @@ pub fn evm_main(args: EvmArgs) {
> = vec![];

if args.pair_oracle {
oracles.push(Rc::new(RefCell::new(PairBalanceOracle::new(
pair_producer.clone(),
))));
oracles.push(Rc::new(RefCell::new(PairBalanceOracle::new())));
}

if args.ierc20_oracle {
oracles.push(flashloan_oracle.clone());
}

if args.ierc20_oracle || args.pair_oracle {
producers.push(pair_producer);
}

if args.ierc20_oracle {
producers.push(erc20_producer);
}
Expand Down
9 changes: 2 additions & 7 deletions src/evm/oracles/erc20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::evm::input::{ConciseEVMInput, EVMInput};
use crate::evm::oracle::EVMBugResult;
use crate::evm::oracles::ERC20_BUG_IDX;
use crate::evm::producers::erc20::ERC20Producer;
use crate::evm::producers::pair::PairProducer;
use crate::evm::types::{EVMAddress, EVMFuzzState, EVMOracleCtx, EVMU256, EVMU512};
#[cfg(feature = "flashloan_v2")]
use crate::evm::uniswap::TokenContext;
Expand Down Expand Up @@ -31,29 +30,25 @@ pub struct IERC20OracleFlashloan {
#[cfg(feature = "flashloan_v2")]
pub known_pair_reserve_slot: HashMap<EVMAddress, EVMU256>,
#[cfg(feature = "flashloan_v2")]
pub pair_producer: Rc<RefCell<PairProducer>>,
#[cfg(feature = "flashloan_v2")]
pub erc20_producer: Rc<RefCell<ERC20Producer>>,
}

impl IERC20OracleFlashloan {
#[cfg(not(feature = "flashloan_v2"))]
pub fn new(_: Rc<RefCell<PairProducer>>, _: Rc<RefCell<ERC20Producer>>) -> Self {
pub fn new(_: Rc<RefCell<ERC20Producer>>) -> Self {
Self {
balance_of: hex::decode("70a08231").unwrap(),
}
}

#[cfg(feature = "flashloan_v2")]
pub fn new(
pair_producer: Rc<RefCell<PairProducer>>,
erc20_producer: Rc<RefCell<ERC20Producer>>,
) -> Self {
Self {
balance_of: hex::decode("70a08231").unwrap(),
known_tokens: HashMap::new(),
known_pair_reserve_slot: HashMap::new(),
pair_producer,
erc20_producer,
}
}
Expand Down Expand Up @@ -117,7 +112,7 @@ impl
let liquidation_percent = EVMU256::from(liquidation_percent);
let mut liquidations_earned = Vec::new();

for ((caller, token), (prev_balance, new_balance)) in
for ((caller, token), new_balance) in
self.erc20_producer.deref().borrow().balances.iter()
{
let token_info = self.known_tokens.get(token).expect("Token not found");
Expand Down
1 change: 0 additions & 1 deletion src/evm/oracles/selfdestruct.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::evm::input::{ConciseEVMInput, EVMInput};
use crate::evm::oracle::{dummy_precondition, EVMBugResult};
use crate::evm::producers::pair::PairProducer;
use crate::evm::types::{EVMAddress, EVMFuzzState, EVMOracleCtx, EVMU256, ProjectSourceMapTy};
use crate::evm::vm::EVMState;
use crate::oracle::{Oracle, OracleCtx, Producer};
Expand Down
1 change: 0 additions & 1 deletion src/evm/oracles/state_comp.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::evm::input::{ConciseEVMInput, EVMInput};
use crate::evm::oracle::{dummy_precondition, EVMBugResult};
use crate::evm::producers::pair::PairProducer;
use crate::evm::types::{bytes_to_u64, EVMAddress, EVMFuzzState, EVMOracleCtx, EVMU256};
use crate::evm::vm::EVMState;
use crate::oracle::{Oracle, OracleCtx, Producer};
Expand Down
1 change: 0 additions & 1 deletion src/evm/oracles/typed_bug.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::evm::input::{ConciseEVMInput, EVMInput};
use crate::evm::oracle::{dummy_precondition, EVMBugResult};
use crate::evm::producers::pair::PairProducer;
use crate::evm::types::{EVMAddress, EVMFuzzState, EVMOracleCtx, EVMStagedVMState, EVMU256, ProjectSourceMapTy};
use crate::evm::vm::{EVMExecutor, EVMState};
use crate::oracle::{BugMetadata, Oracle, OracleCtx, Producer};
Expand Down
6 changes: 2 additions & 4 deletions src/evm/oracles/v2_pair.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::evm::input::{ConciseEVMInput, EVMInput};
use crate::evm::oracle::EVMBugResult;
use crate::evm::oracles::V2_PAIR_BUG_IDX;
use crate::evm::producers::pair::PairProducer;
use crate::evm::types::{EVMAddress, EVMFuzzState, EVMOracleCtx, EVMU256};
use crate::evm::vm::EVMState;
use crate::oracle::{Oracle, OracleCtx};
Expand All @@ -14,12 +13,11 @@ use std::hash::{Hash, Hasher};
use std::rc::Rc;

pub struct PairBalanceOracle {
pub pair_producer: Rc<RefCell<PairProducer>>,
}

impl PairBalanceOracle {
pub fn new(pair_producer: Rc<RefCell<PairProducer>>) -> Self {
Self { pair_producer }
pub fn new() -> Self {
Self { }
}
}

Expand Down
8 changes: 2 additions & 6 deletions src/evm/producers/erc20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::collections::HashMap;

pub struct ERC20Producer {
// (caller, token) -> (pre_balance, post_balance)
pub balances: HashMap<(EVMAddress, EVMAddress), (EVMU256, EVMU256)>,
pub balances: HashMap<(EVMAddress, EVMAddress), EVMU256>,
pub balance_of: Vec<u8>,
}

Expand Down Expand Up @@ -65,19 +65,15 @@ impl Producer<EVMState, EVMAddress, Bytecode, Bytes, EVMAddress, EVMU256, Vec<u8
}
).flatten().collect::<Vec<(EVMAddress, Bytes)>>();
let post_balance_res = ctx.call_post_batch(&query_balance_batch);
let pre_balance_res = ctx.call_pre_batch(&query_balance_batch);

let mut idx = 0;

for caller in &callers {
for token in &tokens {
let token = *token;
let pre_balance = &pre_balance_res[idx];
let post_balance = &post_balance_res[idx];
let prev_balance = EVMU256::try_from_be_slice(pre_balance.as_slice()).unwrap_or(EVMU256::ZERO);
let new_balance = EVMU256::try_from_be_slice(post_balance.as_slice()).unwrap_or(EVMU256::ZERO);

self.balances.insert((*caller, token), (prev_balance, new_balance));
self.balances.insert((*caller, token), new_balance);
idx += 1;
}
}
Expand Down

0 comments on commit d361ba5

Please sign in to comment.