Skip to content

Commit 7f9fd76

Browse files
authored
Migrate IUniswapV3Factory to alloy (#3805)
1 parent 7abfda1 commit 7f9fd76

File tree

8 files changed

+64
-64
lines changed

8 files changed

+64
-64
lines changed

crates/autopilot/src/run.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ use {
2626
},
2727
chain::Chain,
2828
clap::Parser,
29-
contracts::{IUniswapV3Factory, alloy::BalancerV2Vault},
30-
ethcontract::{BlockNumber, H160, common::DeploymentInformation, errors::DeployError},
29+
contracts::alloy::{BalancerV2Vault, IUniswapV3Factory, InstanceExt},
30+
ethcontract::{BlockNumber, H160, common::DeploymentInformation},
3131
ethrpc::{
3232
Web3,
3333
alloy::conversions::IntoLegacy,
@@ -247,13 +247,11 @@ pub async fn run(args: Arguments, shutdown_controller: ShutdownController) {
247247
let vault =
248248
vault_address.map(|address| BalancerV2Vault::Instance::new(address, web3.alloy.clone()));
249249

250-
let uniswapv3_factory = match IUniswapV3Factory::deployed(&web3)
250+
let uniswapv3_factory = IUniswapV3Factory::Instance::deployed(&web3.alloy)
251251
.instrument(info_span!("uniswapv3_deployed"))
252252
.await
253-
{
254-
Err(DeployError::NotFound(_)) => None,
255-
other => Some(other.unwrap()),
256-
};
253+
.inspect_err(|err| tracing::warn!(%err, "error while fetching IUniswapV3Factory instance"))
254+
.ok();
257255

258256
let chain = Chain::try_from(chain_id).expect("incorrect chain ID");
259257

crates/contracts/build.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -232,22 +232,6 @@ fn main() {
232232
.add_network_str(POLYGON, "0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270")
233233
.add_network_str(LENS, "0x6bDc36E20D267Ff0dd6097799f82e78907105e2F")
234234
});
235-
generate_contract_with_config("IUniswapV3Factory", |builder| {
236-
// <https://github.com/Uniswap/v3-periphery/blob/697c2474757ea89fec12a4e6db16a574fe259610/deploys.md>
237-
builder
238-
.add_network_str(MAINNET, "0x1F98431c8aD98523631AE4a59f267346ea31F984")
239-
.add_network_str(GOERLI, "0x1F98431c8aD98523631AE4a59f267346ea31F984")
240-
.add_network_str(SEPOLIA, "0x1F98431c8aD98523631AE4a59f267346ea31F984")
241-
.add_network_str(ARBITRUM_ONE, "0x1F98431c8aD98523631AE4a59f267346ea31F984")
242-
.add_network_str(BASE, "0x33128a8fC17869897dcE68Ed026d694621f6FDfD")
243-
.add_network_str(AVALANCHE, "0x740b1c1de25031C31FF4fC9A62f554A55cdC1baD")
244-
.add_network_str(BNB, "0xdB1d10011AD0Ff90774D0C6Bb92e5C5c8b4461F7")
245-
.add_network_str(OPTIMISM, "0x1F98431c8aD98523631AE4a59f267346ea31F984")
246-
.add_network_str(POLYGON, "0x1F98431c8aD98523631AE4a59f267346ea31F984")
247-
// not official
248-
.add_network_str(LENS, "0xc3A5b857Ba82a2586A45a8B59ECc3AA50Bc3D0e3")
249-
// Not available on Gnosis Chain
250-
});
251235
generate_contract_with_config("CowProtocolToken", |builder| {
252236
builder
253237
.add_network_str(MAINNET, "0xDEf1CA1fb7FBcDC777520aa7f396b4E015F497aB")

crates/contracts/src/alloy.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,23 @@ crate::bindings!(
494494
// Not available on Gnosis Chain
495495
}
496496
);
497+
crate::bindings!(
498+
IUniswapV3Factory,
499+
crate::deployments! {
500+
// <https://github.com/Uniswap/v3-periphery/blob/697c2474757ea89fec12a4e6db16a574fe259610/deploys.md>
501+
MAINNET => address!( "0x1F98431c8aD98523631AE4a59f267346ea31F984"),
502+
SEPOLIA => address!( "0x1F98431c8aD98523631AE4a59f267346ea31F984"),
503+
ARBITRUM_ONE => address!( "0x1F98431c8aD98523631AE4a59f267346ea31F984"),
504+
BASE => address!( "0x33128a8fC17869897dcE68Ed026d694621f6FDfD"),
505+
AVALANCHE => address!( "0x740b1c1de25031C31FF4fC9A62f554A55cdC1baD"),
506+
BNB => address!( "0xdB1d10011AD0Ff90774D0C6Bb92e5C5c8b4461F7"),
507+
OPTIMISM => address!( "0x1F98431c8aD98523631AE4a59f267346ea31F984"),
508+
POLYGON => address!( "0x1F98431c8aD98523631AE4a59f267346ea31F984"),
509+
// not official
510+
LENS => address!( "0xc3A5b857Ba82a2586A45a8B59ECc3AA50Bc3D0e3"),
511+
// Not available on Gnosis Chain
512+
}
513+
);
497514

498515
crate::bindings!(
499516
HooksTrampoline,

crates/contracts/src/lib.rs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,12 @@ include_contracts! {
5757
ERC20;
5858
GPv2AllowListAuthentication;
5959
GPv2Settlement;
60-
IUniswapV3Factory;
6160
WETH9;
6261
}
6362

6463
#[cfg(test)]
6564
mod tests {
66-
use crate::alloy::networks::{
67-
ARBITRUM_ONE, AVALANCHE, BASE, BNB, GNOSIS, LENS, MAINNET, OPTIMISM, POLYGON, SEPOLIA,
68-
};
65+
use crate::alloy::networks::{ARBITRUM_ONE, GNOSIS, MAINNET, SEPOLIA};
6966
use {
7067
super::*,
7168
ethcontract::{
@@ -137,18 +134,6 @@ mod tests {
137134
for network in &[MAINNET, GNOSIS, SEPOLIA] {
138135
assert_has_deployment_address!(CowProtocolToken for *network);
139136
}
140-
for network in &[
141-
MAINNET,
142-
ARBITRUM_ONE,
143-
POLYGON,
144-
OPTIMISM,
145-
BASE,
146-
AVALANCHE,
147-
BNB,
148-
LENS,
149-
] {
150-
assert_has_deployment_address!(IUniswapV3Factory for *network);
151-
}
152137
for network in &[MAINNET, ARBITRUM_ONE] {
153138
assert!(
154139
alloy::BalancerV2WeightedPool2TokensFactory::deployment_address(network).is_some()

crates/orderbook/src/run.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,16 @@ use {
1414
clap::Parser,
1515
contracts::{
1616
GPv2Settlement,
17-
IUniswapV3Factory,
1817
WETH9,
1918
alloy::{
2019
BalancerV2Vault,
2120
ChainalysisOracle,
2221
HooksTrampoline,
22+
IUniswapV3Factory,
2323
InstanceExt,
2424
support::Balances,
2525
},
2626
},
27-
ethcontract::errors::DeployError,
2827
ethrpc::alloy::conversions::{IntoAlloy, IntoLegacy},
2928
futures::{FutureExt, StreamExt},
3029
model::{DomainSeparator, order::BUY_ETH_ADDRESS},
@@ -237,10 +236,10 @@ pub async fn run(args: Arguments) {
237236
allowed_tokens.push(BUY_ETH_ADDRESS);
238237
let unsupported_tokens = args.unsupported_tokens.clone();
239238

240-
let uniswapv3_factory = match IUniswapV3Factory::deployed(&web3).await {
241-
Err(DeployError::NotFound(_)) => None,
242-
other => Some(other.unwrap()),
243-
};
239+
let uniswapv3_factory = IUniswapV3Factory::Instance::deployed(&web3.alloy)
240+
.await
241+
.inspect_err(|err| tracing::warn!(%err, "error while fetching IUniswapV3Factory instance"))
242+
.ok();
244243

245244
let finder = token_owner_finder::init(
246245
&args.token_owner_finder,

crates/shared/src/bad_token/token_owner_finder/liquidity.rs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
use {
44
super::TokenOwnerProposing,
55
crate::sources::{uniswap_v2::pair_provider::PairProvider, uniswap_v3_pair_provider},
6+
alloy::eips::BlockNumberOrTag,
67
anyhow::Result,
7-
contracts::{IUniswapV3Factory, alloy::BalancerV2Vault},
8-
ethcontract::{BlockNumber, H160},
8+
contracts::alloy::{BalancerV2Vault, IUniswapV3Factory},
9+
ethcontract::H160,
910
ethrpc::alloy::conversions::IntoLegacy,
1011
model::TokenPair,
1112
};
@@ -38,7 +39,7 @@ impl TokenOwnerProposing for BalancerVaultFinder {
3839
}
3940

4041
pub struct UniswapV3Finder {
41-
pub factory: IUniswapV3Factory,
42+
pub factory: IUniswapV3Factory::Instance,
4243
pub base_tokens: Vec<H160>,
4344
fee_values: Vec<u32>,
4445
}
@@ -55,7 +56,7 @@ pub enum FeeValues {
5556

5657
impl UniswapV3Finder {
5758
pub async fn new(
58-
factory: IUniswapV3Factory,
59+
factory: IUniswapV3Factory::Instance,
5960
base_tokens: Vec<H160>,
6061
fee_values: FeeValues,
6162
) -> Result<Self> {
@@ -75,18 +76,25 @@ impl UniswapV3Finder {
7576

7677
// Possible fee values as given by
7778
// https://github.com/Uniswap/v3-core/blob/9161f9ae4aaa109f7efdff84f1df8d4bc8bfd042/contracts/UniswapV3Factory.sol#L26
78-
async fn fee_values(factory: &IUniswapV3Factory) -> Result<Vec<u32>> {
79+
async fn fee_values(factory: &IUniswapV3Factory::Instance) -> Result<Vec<u32>> {
7980
// We expect there to be few of these kind of events (currently there are 4) so
8081
// fetching all of them is fine. Alternatively we could index these
8182
// events in the database.
8283
let events = factory
83-
.events()
84-
.fee_amount_enabled()
85-
.from_block(BlockNumber::Earliest)
86-
.to_block(BlockNumber::Latest)
84+
.FeeAmountEnabled_filter()
85+
.from_block(BlockNumberOrTag::Earliest)
86+
.to_block(BlockNumberOrTag::Latest)
8787
.query()
8888
.await?;
89-
let fee_values = events.into_iter().map(|event| event.data.fee).collect();
89+
let fee_values = events
90+
.into_iter()
91+
.map(|(enabled, _)| {
92+
enabled
93+
.fee
94+
.try_into()
95+
.expect("uint24 always fits inside u32")
96+
})
97+
.collect();
9098
Ok(fee_values)
9199
}
92100
}
@@ -100,7 +108,11 @@ impl TokenOwnerProposing for UniswapV3Finder {
100108
.filter_map(|base_token| TokenPair::new(*base_token, token))
101109
.flat_map(|pair| self.fee_values.iter().map(move |fee| (pair, *fee)))
102110
.map(|(pair, fee)| {
103-
uniswap_v3_pair_provider::pair_address(&self.factory.address(), &pair, fee)
111+
uniswap_v3_pair_provider::pair_address(
112+
&self.factory.address().into_legacy(),
113+
&pair,
114+
fee,
115+
)
104116
})
105117
.collect())
106118
}

crates/shared/src/bad_token/token_owner_finder/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ use {
3131
},
3232
anyhow::{Context, Result},
3333
chain::Chain,
34-
contracts::{ERC20, IUniswapV3Factory, alloy::BalancerV2Vault, errors::EthcontractErrorType},
34+
contracts::{
35+
ERC20,
36+
alloy::{BalancerV2Vault, IUniswapV3Factory},
37+
errors::EthcontractErrorType,
38+
},
3539
ethcontract::U256,
3640
futures::{Stream, StreamExt as _},
3741
primitive_types::H160,
@@ -283,7 +287,7 @@ pub async fn init(
283287
http_factory: &HttpClientFactory,
284288
pair_providers: &[PairProvider],
285289
vault: Option<&BalancerV2Vault::Instance>,
286-
uniswapv3_factory: Option<&IUniswapV3Factory>,
290+
uniswapv3_factory: Option<&IUniswapV3Factory::Instance>,
287291
base_tokens: &BaseTokens,
288292
settlement_contract: H160,
289293
) -> Result<Arc<dyn TokenOwnerFinding>> {

crates/shared/src/bad_token/trace_call.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -378,10 +378,7 @@ mod tests {
378378
sources::{BaselineSource, uniswap_v2},
379379
},
380380
chain::Chain,
381-
contracts::{
382-
IUniswapV3Factory,
383-
alloy::{BalancerV2Vault, InstanceExt},
384-
},
381+
contracts::alloy::{BalancerV2Vault, IUniswapV3Factory, InstanceExt},
385382
ethrpc::Web3,
386383
hex_literal::hex,
387384
std::{env, time::Duration},
@@ -741,7 +738,9 @@ mod tests {
741738
)),
742739
Arc::new(
743740
UniswapV3Finder::new(
744-
IUniswapV3Factory::deployed(&web3).await.unwrap(),
741+
IUniswapV3Factory::Instance::deployed(&web3.alloy)
742+
.await
743+
.unwrap(),
745744
base_tokens.to_vec(),
746745
FeeValues::Static,
747746
)
@@ -779,7 +778,9 @@ mod tests {
779778
let web3 = Web3::new_from_env();
780779
let base_tokens = vec![testlib::tokens::WETH];
781780
let settlement = contracts::GPv2Settlement::deployed(&web3).await.unwrap();
782-
let factory = IUniswapV3Factory::deployed(&web3).await.unwrap();
781+
let factory = IUniswapV3Factory::Instance::deployed(&web3.alloy)
782+
.await
783+
.unwrap();
783784
let univ3 = Arc::new(
784785
UniswapV3Finder::new(factory, base_tokens, FeeValues::Dynamic)
785786
.await

0 commit comments

Comments
 (0)