Skip to content

Commit

Permalink
fix: Don't hardcode ADAPTER_ADDRESS, make it depend on the protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
dianacarvalho1 committed Dec 12, 2024
1 parent 337a3ee commit 58f6294
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 29 deletions.
4 changes: 0 additions & 4 deletions src/evm/protocol/vm/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,4 @@ lazy_static! {
.expect("Invalid string for external account address"),
);
pub static ref MAX_BALANCE: U256 = U256::MAX / U256::from(2);
pub static ref ADAPTER_ADDRESS: Address = Address::from_slice(
&hex::decode("A2C5C98A892fD6656a7F39A2f63228C0Bc846270")
.expect("Invalid string for adapter address"),
);
}
14 changes: 6 additions & 8 deletions src/evm/protocol/vm/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use crate::{
};

use super::{
constants::{ADAPTER_ADDRESS, EXTERNAL_ACCOUNT, MAX_BALANCE},
constants::{EXTERNAL_ACCOUNT, MAX_BALANCE},
erc20_token::{ERC20OverwriteFactory, ERC20Slots, Overwrites},
models::Capability,
tycho_simulation_contract::TychoSimulationContract,
Expand Down Expand Up @@ -243,12 +243,8 @@ where

overwrites.set_balance(max_amount, Address::from_slice(&*EXTERNAL_ACCOUNT.0));

// Set allowance for ADAPTER_ADDRESS to max_amount
overwrites.set_allowance(
max_amount,
Address::from_slice(&*ADAPTER_ADDRESS.0),
Address::from_slice(&*EXTERNAL_ACCOUNT.0),
);
// Set allowance for adapter_address to max_amount
overwrites.set_allowance(max_amount, self.adapter_contract.address, *EXTERNAL_ACCOUNT);

res.push(overwrites.get_overwrites());

Expand Down Expand Up @@ -613,8 +609,10 @@ mod tests {
(dai_addr(), U256::from_str("178754012737301807104").unwrap()),
(bal_addr(), U256::from_str("91082987763369885696").unwrap()),
]);
let adapter_address =
Address::from_str("0xA2C5C98A892fD6656a7F39A2f63228C0Bc846270").unwrap();

EVMPoolStateBuilder::new(pool_id, tokens, balances, block)
EVMPoolStateBuilder::new(pool_id, tokens, balances, block, adapter_address)
.balance_owner(Address::from_str("0xBA12222222228d8Ba445958a75a0704d566BF2C8").unwrap())
.adapter_contract_path(PathBuf::from(
"src/evm/protocol/vm/assets/BalancerV2SwapAdapter.evm.runtime".to_string(),
Expand Down
18 changes: 12 additions & 6 deletions src/evm/protocol/vm/state_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use crate::{
use tycho_core::Bytes as TychoBytes;

use super::{
constants::{ADAPTER_ADDRESS, EXTERNAL_ACCOUNT, MAX_BALANCE},
constants::{EXTERNAL_ACCOUNT, MAX_BALANCE},
erc20_token::{brute_force_slots, ERC20Slots},
models::Capability,
state::EVMPoolState,
Expand Down Expand Up @@ -83,6 +83,7 @@ where
tokens: Vec<TychoBytes>,
block: BlockHeader,
balances: HashMap<Address, U256>,
adapter_address: Address,
balance_owner: Option<Address>,
capabilities: Option<HashSet<Capability>>,
involved_contracts: Option<HashSet<Address>>,
Expand All @@ -106,12 +107,14 @@ where
tokens: Vec<TychoBytes>,
balances: HashMap<Address, U256>,
block: BlockHeader,
adapter_address: Address,
) -> Self {
Self {
id,
tokens,
balances,
block,
adapter_address,
balance_owner: None,
capabilities: None,
involved_contracts: None,
Expand Down Expand Up @@ -191,7 +194,7 @@ where

if self.adapter_contract.is_none() {
self.adapter_contract = Some(TychoSimulationContract::new_swap_adapter(
*ADAPTER_ADDRESS,
self.adapter_address,
self.adapter_contract_path
.as_ref()
.ok_or_else(|| {
Expand Down Expand Up @@ -459,9 +462,10 @@ mod tests {
vec![TychoBytes::from_str("0000000000000000000000000000000000000000").unwrap()];
let balances = HashMap::new();
let block = BlockHeader { number: 1, hash: B256::default(), timestamp: 234 };

let adapter_address =
Address::from_str("0xA2C5C98A892fD6656a7F39A2f63228C0Bc846270").unwrap();
let result = tokio_test::block_on(
EVMPoolStateBuilder::<PreCachedDB>::new(id, tokens, balances, block)
EVMPoolStateBuilder::<PreCachedDB>::new(id, tokens, balances, block, adapter_address)
.build(SHARED_TYCHO_DB.clone()),
);

Expand All @@ -482,8 +486,10 @@ mod tests {
let tokens = vec![token2.clone(), token3.clone()];
let block = BlockHeader { number: 1, hash: B256::default(), timestamp: 234 };
let balances = HashMap::new();

let builder = EVMPoolStateBuilder::<PreCachedDB>::new(id, tokens, balances, block);
let adapter_address =
Address::from_str("0xA2C5C98A892fD6656a7F39A2f63228C0Bc846270").unwrap();
let builder =
EVMPoolStateBuilder::<PreCachedDB>::new(id, tokens, balances, block, adapter_address);

let engine =
tokio_test::block_on(builder.get_default_engine(SHARED_TYCHO_DB.clone())).unwrap();
Expand Down
26 changes: 18 additions & 8 deletions src/evm/protocol/vm/tycho_decoder.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use alloy_primitives::{Address, B256, U256};
use std::{
collections::HashMap,
path::PathBuf,
str::FromStr,
time::{SystemTime, UNIX_EPOCH},
};

use alloy_primitives::{Address, B256, U256};

use tycho_client::feed::{synchronizer::ComponentWithState, Header};
use tycho_core::Bytes;

Expand Down Expand Up @@ -132,13 +134,21 @@ impl TryFromWithBlock<ComponentWithState> for EVMPoolState<PreCachedDB> {
let adapter_file_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("src/evm/protocol/vm/assets")
.join(to_adapter_file_name(protocol_name));

let mut pool_state_builder =
EVMPoolStateBuilder::new(id.clone(), tokens.clone(), balances, block)
.adapter_contract_path(adapter_file_path)
.involved_contracts(involved_contracts)
.stateless_contracts(stateless_contracts)
.manual_updates(manual_updates);
let adapter_contract_address =
Address::from_str(&format!("{:0>40}", hex::encode(protocol_name)))
.expect("Can't convert protocol name to address");

let mut pool_state_builder = EVMPoolStateBuilder::new(
id.clone(),
tokens.clone(),
balances,
block,
adapter_contract_address,
)
.adapter_contract_path(adapter_file_path)
.involved_contracts(involved_contracts)
.stateless_contracts(stateless_contracts)
.manual_updates(manual_updates);

if let Some(balance_owner) = balance_owner {
pool_state_builder = pool_state_builder.balance_owner(balance_owner)
Expand Down
6 changes: 3 additions & 3 deletions src/evm/protocol/vm/tycho_simulation_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
};

use super::{
constants::{ADAPTER_ADDRESS, EXTERNAL_ACCOUNT, MAX_BALANCE},
constants::{EXTERNAL_ACCOUNT, MAX_BALANCE},
utils::{coerce_error, get_contract_bytecode},
};

Expand Down Expand Up @@ -53,7 +53,7 @@ where
<D as DatabaseRef>::Error: std::fmt::Debug,
<D as EngineDatabaseInterface>::Error: std::fmt::Debug,
{
address: Address,
pub(crate) address: Address,
pub(crate) engine: SimulationEngine<D>, /* TODO: Should we expose it directly or make some
* getter functions? */
}
Expand All @@ -77,7 +77,7 @@ where
.map_err(|err| SimulationError::FatalError(err.to_string()))?;

engine.state.init_account(
*ADAPTER_ADDRESS,
address,
AccountInfo {
balance: *MAX_BALANCE,
nonce: 0,
Expand Down

0 comments on commit 58f6294

Please sign in to comment.