Skip to content

Commit

Permalink
fix: rAddress discrepancies from OverwriteFactory
Browse files Browse the repository at this point in the history
- We are limited by what is expected in simulation.rs: Thus, it makes more sense to return an revm::primitives::Address type as the overwrite hashmap from the overwrite factory, instead of having to re-convert it through different levels.
- We also had to convert the SlotHash type to match what was expected by simulation.rs
  • Loading branch information
TAMARA LIPOWSKI committed Oct 29, 2024
1 parent 20aba87 commit ffb4cb3
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 84 deletions.
11 changes: 6 additions & 5 deletions src/protocol/vm/adapter_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
use crate::{
evm::account_storage::StateUpdate,
protocol::vm::{
errors::ProtosimError, models::Capability, protosim_contract::ProtosimContract,
erc20_overwrite_factory::Overwrites, errors::ProtosimError, models::Capability,
protosim_contract::ProtosimContract,
},
};
use ethers::{
Expand Down Expand Up @@ -45,7 +46,7 @@ where
buy_token: Address,
amounts: Vec<U256>,
block: u64,
overwrites: Option<HashMap<rAddress, HashMap<U256, U256>>>,
overwrites: Option<HashMap<rAddress, Overwrites>>,
) -> Result<Vec<f64>, ProtosimError> {
let args = vec![
self.hexstring_to_bytes(&pair_id)?,
Expand All @@ -54,7 +55,7 @@ where
Token::Array(
amounts
.into_iter()
.map(|a| Token::Uint(U256::from(a)))
.map(Token::Uint)
.collect(),
),
];
Expand Down Expand Up @@ -128,9 +129,9 @@ where

if let Some(Token::Array(inner)) = res.first() {
if let (Some(Token::Uint(value1)), Some(Token::Uint(value2))) =
(inner.get(0), inner.get(1))
(inner.first(), inner.get(1))
{
return Ok((value1.clone(), value2.clone()));
return Ok((*value1, *value2));
}
}

Expand Down
35 changes: 21 additions & 14 deletions src/protocol/vm/erc20_overwrite_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use crate::protocol::vm::{
errors::FileError,
utils::{get_contract_bytecode, get_storage_slot_index_at_key, SlotHash},
};
use ethers::{addressbook::Address, prelude::U256};
use ethers::{abi::Address, types::U256};
use revm::primitives::Address as rAddress;
use std::{collections::HashMap, path::Path};

pub struct GethOverwrite {
Expand All @@ -17,21 +18,21 @@ pub struct GethOverwrite {
pub type Overwrites = HashMap<SlotHash, U256>;

pub struct ERC20OverwriteFactory {
token_address: Address,
token_address: rAddress,
overwrites: Overwrites,
balance_slot: SlotHash,
allowance_slot: SlotHash,
total_supply_slot: SlotHash,
}

impl ERC20OverwriteFactory {
pub fn new(token_address: Address, token_slots: (SlotHash, SlotHash)) -> Self {
pub fn new(token_address: rAddress, token_slots: (SlotHash, SlotHash)) -> Self {
ERC20OverwriteFactory {
token_address,
overwrites: HashMap::new(),
balance_slot: token_slots.0,
allowance_slot: token_slots.1,
total_supply_slot: SlotHash::from_low_u64_be(2),
total_supply_slot: SlotHash::from(2),
}
}

Expand All @@ -53,21 +54,23 @@ impl ERC20OverwriteFactory {
.insert(self.total_supply_slot, supply);
}

pub fn get_protosim_overwrites(&self) -> HashMap<Address, Overwrites> {
pub fn get_protosim_overwrites(&self) -> HashMap<rAddress, Overwrites> {
let mut result = HashMap::new();
result.insert(self.token_address, self.overwrites.clone());
result
}

pub fn get_geth_overwrites(&self) -> Result<HashMap<Address, GethOverwrite>, FileError> {
pub fn get_geth_overwrites(&self) -> Result<HashMap<rAddress, GethOverwrite>, FileError> {
let mut formatted_overwrites = HashMap::new();

for (key, val) in &self.overwrites {
let hex_key = hex::encode(key.as_bytes());
let mut key_bytes = [0u8; 32];
key.to_big_endian(&mut key_bytes);
let hex_key = hex::encode(key_bytes);

let mut bytes = [0u8; 32];
val.to_big_endian(&mut bytes);
let hex_val = format!("0x{:0>64}", hex::encode(bytes));
let mut value_bytes = [0u8; 32];
val.to_big_endian(&mut value_bytes);
let hex_val = format!("0x{:0>64}", hex::encode(value_bytes));

formatted_overwrites.insert(hex_key, hex_val);
}
Expand Down Expand Up @@ -107,9 +110,13 @@ mod tests {
use crate::protocol::vm::utils::SlotHash;

fn setup_factory() -> ERC20OverwriteFactory {
let token_address = Address::random();
let balance_slot = SlotHash::random();
let allowance_slot = SlotHash::random();
let token_address = rAddress::parse_checksummed(
String::from("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"),
None,
)
.expect("Failed to parse address");
let balance_slot = SlotHash::from(5);
let allowance_slot = SlotHash::from(6);
ERC20OverwriteFactory::new(token_address, (balance_slot, allowance_slot))
}

Expand Down Expand Up @@ -173,7 +180,7 @@ mod tests {
fn test_get_geth_overwrites() {
let mut factory = setup_factory();

let storage_slot = SlotHash::from_low_u64_be(1);
let storage_slot = SlotHash::from(1);
let val = U256::from(123456);
factory
.overwrites
Expand Down
3 changes: 2 additions & 1 deletion src/protocol/vm/protosim_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::{
evm::simulation::{SimulationEngine, SimulationParameters, SimulationResult},
protocol::vm::{
constants::EXTERNAL_ACCOUNT,
erc20_overwrite_factory::Overwrites,
errors::ProtosimError,
utils::{load_swap_abi, maybe_coerce_error},
},
Expand Down Expand Up @@ -142,7 +143,7 @@ where
args: Vec<Token>,
block_number: u64,
timestamp: Option<u64>,
overrides: Option<HashMap<Address, HashMap<U256, U256>>>,
overrides: Option<HashMap<Address, Overwrites>>,
caller: Option<Address>,
value: U256,
) -> Result<ProtoSimResponse, ProtosimError> {
Expand Down
Loading

0 comments on commit ffb4cb3

Please sign in to comment.