Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions crates/e2e/tests/e2e/eip4626.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,18 @@ use {
e2e::setup::*,
ethrpc::alloy::CallBuilderExt,
futures::{FutureExt, future::BoxFuture},
model::quote::{OrderQuoteRequest, OrderQuoteSide, SellAmount},
model::{
order::BUY_ETH_ADDRESS,
quote::{OrderQuoteRequest, OrderQuoteSide, SellAmount},
},
number::units::EthUnit,
price_estimation::{
HEALTHY_PRICE_ESTIMATION_TIME,
native::{Eip4626, NativePriceEstimateResult, NativePriceEstimating},
},
shared::web3::Web3,
std::time::Duration,
testlib::tokens::GNO,
};

/// The block number from which we will fetch state for the forked test.
Expand Down Expand Up @@ -265,11 +269,14 @@ async fn eip4626_empty_revert_terminal_token_test(web3: Web3) {
let expected_price = 0.0001;
let inner = FixedPrice(expected_price);
let estimator = Eip4626::new(Box::new(inner), web3.provider);
let price = estimator
.estimate_native_price(USDC, HEALTHY_PRICE_ESTIMATION_TIME)
.await
.expect("empty-revert on terminal token must not abort the unwrap");
assert_eq!(price, expected_price);

for token in [BUY_ETH_ADDRESS, USDC, GNO] {
let price = estimator
.estimate_native_price(token, HEALTHY_PRICE_ESTIMATION_TIME)
.await
.expect("empty-revert on terminal token must not abort the unwrap");
assert_eq!(price, expected_price);
}
}

struct FixedPrice(f64);
Expand Down
1 change: 1 addition & 0 deletions crates/price-estimation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ tracing = { workspace = true }
url = { workspace = true }

[dev-dependencies]
ethrpc = { workspace = true, features = ["test-util"] }
hex-literal = { workspace = true }
maplit = { workspace = true }
mockall = { workspace = true }
Expand Down
42 changes: 41 additions & 1 deletion crates/price-estimation/src/native/eip4626.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use {
dashmap::DashSet,
ethrpc::{AlloyProvider, alloy::errors::ContractErrorExt},
futures::{FutureExt, future::BoxFuture},
model::order::BUY_ETH_ADDRESS,
num::{BigInt, BigRational, ToPrimitive},
number::conversions::u256_to_big_rational,
std::time::{Duration, Instant},
Expand Down Expand Up @@ -37,7 +38,9 @@ impl Eip4626 {
Self {
inner,
provider,
non_vault_tokens: DashSet::new(),
// BUY_ETH_ADDRESS is not ERC-20, but it is a valid estimation address
// so we need to make sure it bypasses the EIP-4626 estimator
non_vault_tokens: DashSet::from_iter([BUY_ETH_ADDRESS]),
}
}

Expand Down Expand Up @@ -224,6 +227,8 @@ mod tests {
use {
super::*,
crate::{HEALTHY_PRICE_ESTIMATION_TIME, native::MockNativePriceEstimating},
alloy::providers::mock::Asserter,
std::borrow::Cow,
};

#[test]
Expand Down Expand Up @@ -253,6 +258,41 @@ mod tests {
assert!((rate - 2.0).abs() < 1e-9, "rate={rate}");
}

/// Tests two (related) things:
/// * Cached tokens bypass the EIP-4626 provider calls — i.e. calling
/// decimals, assets, etc
/// * That the BUY_ETH_ADDRESS is cached by default (and the previous
/// applies to it)
#[tokio::test]
async fn buy_eth_address_bypasses_eth_calls() {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: the second eth means eth_call but, reads ambiguously next to BUY_ETH. imo buy_eth_address_bypasses_rpc_calls could be cleaner.

let mut inner = MockNativePriceEstimating::new();
let token = BUY_ETH_ADDRESS;
let expected_price = 1.5;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: the native price of ETH is by definition 1 so it's strange to use 1.5 here. Doesn't change the correctness of the test, though.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll argue that if it returns 1.5 it's definitely not by chance 😛

inner
.expect_estimate_native_price()
.withf(move |t, _| *t == token)
.returning(move |_, _| Box::pin(async move { Ok(expected_price) }));

let asserter = Asserter::new();
asserter.push_failure_msg(Cow::from("calls are not being bypassed"));
let web3 = ethrpc::Web3::with_asserter(asserter);

let estimator = Eip4626::new(Box::new(inner), web3.provider);

let result = estimator
.estimate(token, HEALTHY_PRICE_ESTIMATION_TIME)
.await;
assert_eq!(result.unwrap(), expected_price);

let result = estimator
.estimate(Address::random(), HEALTHY_PRICE_ESTIMATION_TIME)
.await;
assert!(
matches!(result, Err(PriceEstimationError::EstimatorInternal(_))),
"{result:?}"
);
}

#[tokio::test]
async fn non_vault_tokens_delegate_to_inner() {
let mut inner = MockNativePriceEstimating::new();
Expand Down
Loading