Skip to content

Commit

Permalink
Merge branch 'main' into orderbook-with-profiler
Browse files Browse the repository at this point in the history
  • Loading branch information
squadgazzz authored Jun 6, 2024
2 parents 7b5babf + baf9a1a commit 81d992b
Show file tree
Hide file tree
Showing 32 changed files with 410 additions and 639 deletions.
87 changes: 87 additions & 0 deletions crates/autopilot/src/infra/blockchain/authenticator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
use {
crate::{
domain::{self, eth},
infra::blockchain::{
contracts::{deployment_address, Contracts},
ChainId,
},
},
ethcontract::{dyns::DynWeb3, GasPrice},
};

#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct Manager {
/// The authenticator contract that decides which solver is allowed to
/// submit settlements.
authenticator: contracts::GPv2AllowListAuthentication,
/// The safe module that is used to provide special role to EOA.
authenticator_role: contracts::Roles,
/// The EOA that is allowed to remove solvers.
authenticator_eoa: ethcontract::Account,
}

impl Manager {
/// Creates an authenticator which can remove solvers from the allow-list
pub async fn new(
web3: DynWeb3,
chain: ChainId,
contracts: Contracts,
authenticator_pk: eth::H256,
) -> Self {
let authenticator_role = contracts::Roles::at(
&web3,
deployment_address(contracts::Roles::raw_contract(), &chain).expect("roles address"),
);

Self {
authenticator: contracts.authenticator().clone(),
authenticator_role,
authenticator_eoa: ethcontract::Account::Offline(
ethcontract::PrivateKey::from_raw(authenticator_pk.0).unwrap(),
None,
),
}
}

/// Fire and forget: Removes solver from the allow-list in the authenticator
/// contract. This solver will no longer be able to settle.
#[allow(dead_code)]
fn remove_solver(&self, solver: domain::eth::Address) {
let calldata = self
.authenticator
.methods()
.remove_solver(solver.into())
.tx
.data
.expect("missing calldata");
let authenticator_eoa = self.authenticator_eoa.clone();
let authenticator_address = self.authenticator.address();
let authenticator_role = self.authenticator_role.clone();
tokio::task::spawn(async move {
// This value comes from the TX posted in the issue: https://github.com/cowprotocol/services/issues/2667
let mut byte_array = [0u8; 32];
byte_array[31] = 1;
authenticator_role
.methods()
.exec_transaction_with_role(
authenticator_address,
0.into(),
ethcontract::Bytes(calldata.0),
0,
ethcontract::Bytes(byte_array),
true,
)
.from(authenticator_eoa)
.gas_price(GasPrice::Eip1559 {
// These are arbitrary high numbers that should be enough for a tx to be settled
// anytime.
max_fee_per_gas: 1000.into(),
max_priority_fee_per_gas: 5.into(),
})
.send()
.await
.inspect_err(|err| tracing::error!(?solver, ?err, "failed to remove the solver"))
});
}
}
6 changes: 4 additions & 2 deletions crates/autopilot/src/infra/blockchain/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ pub struct Contracts {
settlement: contracts::GPv2Settlement,
weth: contracts::WETH9,
chainalysis_oracle: Option<contracts::ChainalysisOracle>,
authenticator: contracts::GPv2AllowListAuthentication,

/// The authenticator contract that decides which solver is allowed to
/// submit settlements.
authenticator: contracts::GPv2AllowListAuthentication,
/// The domain separator for settlement contract used for signing orders.
settlement_domain_separator: domain::eth::DomainSeparator,
}

#[derive(Debug, Default, Clone, Copy)]
#[derive(Debug, Clone)]
pub struct Addresses {
pub settlement: Option<H160>,
pub weth: Option<H160>,
Expand Down
15 changes: 13 additions & 2 deletions crates/autopilot/src/infra/blockchain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use {
url::Url,
};

pub mod authenticator;
pub mod contracts;

/// Chain ID as defined by EIP-155.
Expand Down Expand Up @@ -62,6 +63,11 @@ impl Rpc {
pub fn web3(&self) -> &DynWeb3 {
&self.web3
}

/// Returns a reference to the underlying RPC URL.
pub fn url(&self) -> &Url {
&self.url
}
}

/// The Ethereum blockchain.
Expand All @@ -80,8 +86,13 @@ impl Ethereum {
///
/// Since this type is essential for the program this method will panic on
/// any initialization error.
pub async fn new(rpc: Rpc, addresses: contracts::Addresses, poll_interval: Duration) -> Self {
let Rpc { web3, chain, url } = rpc;
pub async fn new(
web3: DynWeb3,
chain: ChainId,
url: Url,
addresses: contracts::Addresses,
poll_interval: Duration,
) -> Self {
let contracts = Contracts::new(&web3, &chain, addresses).await;

Self {
Expand Down
19 changes: 13 additions & 6 deletions crates/autopilot/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ use {
},
domain,
event_updater::EventUpdater,
infra::{self},
infra::{self, blockchain::ChainId},
run_loop::RunLoop,
shadow,
solvable_orders::SolvableOrdersCache,
},
clap::Parser,
contracts::{BalancerV2Vault, IUniswapV3Factory},
ethcontract::{errors::DeployError, BlockNumber},
ethcontract::{dyns::DynWeb3, errors::DeployError, BlockNumber},
ethrpc::current_block::block_number_to_block_number_hash,
futures::StreamExt,
model::DomainSeparator,
Expand Down Expand Up @@ -87,11 +87,13 @@ async fn ethrpc(url: &Url) -> infra::blockchain::Rpc {
}

async fn ethereum(
ethrpc: infra::blockchain::Rpc,
web3: DynWeb3,
chain: ChainId,
url: Url,
contracts: infra::blockchain::contracts::Addresses,
poll_interval: Duration,
) -> infra::Ethereum {
infra::Ethereum::new(ethrpc, contracts, poll_interval).await
infra::Ethereum::new(web3, chain, url, contracts, poll_interval).await
}

pub async fn start(args: impl Iterator<Item = String>) {
Expand Down Expand Up @@ -149,13 +151,18 @@ pub async fn run(args: Arguments) {
}

let ethrpc = ethrpc(&args.shared.node_url).await;
let chain = ethrpc.chain();
let web3 = ethrpc.web3().clone();
let url = ethrpc.url().clone();
let contracts = infra::blockchain::contracts::Addresses {
settlement: args.shared.settlement_contract_address,
weth: args.shared.native_token_address,
};
let eth = ethereum(
ethrpc,
contracts,
web3.clone(),
chain,
url,
contracts.clone(),
args.shared.current_block.block_stream_poll_interval,
)
.await;
Expand Down
1 change: 1 addition & 0 deletions crates/contracts/artifacts/Roles.json

Large diffs are not rendered by default.

40 changes: 38 additions & 2 deletions crates/contracts/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,42 @@ fn main() {
});
generate_contract("GnosisSafeProxy");
generate_contract("GnosisSafeProxyFactory");
generate_contract_with_config("Roles", |builder| {
builder
.contract_mod_override("roles")
.add_network(
MAINNET,
Network {
address: addr("0x9646fDAD06d3e24444381f44362a3B0eB343D337"),
// <https://etherscan.io/tx/0x351ecf2966f8cdd54e1de1d4cb326217fa89f6064231dfc1fe56417b9b48e942>
deployment_information: Some(DeploymentInformation::BlockNumber(18692162)),
},
)
.add_network(
GNOSIS,
Network {
address: addr("0x9646fDAD06d3e24444381f44362a3B0eB343D337"),
// <https://gnosisscan.io/tx/0x4b1ec57c4048afd40904ea9b91dad38ec18d69ea0db965d624ffdd4abd284c96>
deployment_information: Some(DeploymentInformation::BlockNumber(31222929)),
},
)
.add_network(
SEPOLIA,
Network {
address: addr("0x9646fDAD06d3e24444381f44362a3B0eB343D337"),
// <https://sepolia.etherscan.io/tx/0x516f0f6b8ac669cb5ca3962833e520274169c1463da354be9faa2cb0e6afa8a6>
deployment_information: Some(DeploymentInformation::BlockNumber(4884885)),
},
)
.add_network(
ARBITRUM_ONE,
Network {
address: addr("0x9646fDAD06d3e24444381f44362a3B0eB343D337"),
// <https://arbiscan.io/tx/0x3860d6091e1baf8a9ba16e58ec437ec3644db2f4c0d9e2ba7fe37cfa0a4fa748>
deployment_information: Some(DeploymentInformation::BlockNumber(176504820)),
},
)
});
generate_contract_with_config("HoneyswapRouter", |builder| {
builder.add_network_str(GNOSIS, "0x1C232F01118CB8B424793ae03F870aa7D0ac7f77")
});
Expand Down Expand Up @@ -684,7 +720,7 @@ fn main() {
.add_network_str(MAINNET, "0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f")
.add_network_str(GOERLI, "0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f")
.add_network_str(GNOSIS, "0xA818b4F111Ccac7AA31D0BCc0806d64F2E0737D7")
.add_network_str(ARBITRUM_ONE, "0x6554AD1Afaa3f4ce16dc31030403590F467417A6")
.add_network_str(ARBITRUM_ONE, "0xf1D7CC64Fb4452F05c498126312eBE29f30Fbcf9")
// Not available on Sepolia
});
generate_contract_with_config("UniswapV2Router02", |builder| {
Expand All @@ -693,7 +729,7 @@ fn main() {
.add_network_str(MAINNET, "0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D")
.add_network_str(GOERLI, "0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D")
.add_network_str(GNOSIS, "0x1C232F01118CB8B424793ae03F870aa7D0ac7f77")
.add_network_str(ARBITRUM_ONE, "0xaedE1EFe768bD8A1663A7608c63290C60B85e71c")
.add_network_str(ARBITRUM_ONE, "0x4752ba5dbc23f44d87826276bf6fd6b1c372ad24")
// Not available on Sepolia
});
generate_contract_with_config("UniswapV3SwapRouter", |builder| {
Expand Down
1 change: 1 addition & 0 deletions crates/contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ include_contracts! {
GnosisSafeCompatibilityFallbackHandler;
GnosisSafeProxy;
GnosisSafeProxyFactory;
Roles;
HoneyswapRouter;
HooksTrampoline;
ISwaprPair;
Expand Down
7 changes: 4 additions & 3 deletions crates/driver/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -390,9 +390,10 @@ components:
description: Request to the settle and reveal endpoint.
type: object
properties:
auctionId:
description: Id of the auction that should be executed.
type: integer
solutionId:
description: Id of the solution that should be executed.
type: string
example: "123"
RevealedResponse:
description: Response of the reveal endpoint.
type: object
Expand Down
2 changes: 0 additions & 2 deletions crates/driver/src/boundary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
//! Software (2014)
pub mod liquidity;
pub mod settlement;

// The [`anyhow::Error`] type is re-exported because the legacy code mostly
// returns that error. This will change as the legacy code gets refactored away.
Expand All @@ -32,7 +31,6 @@ pub use {
anyhow::{Error, Result},
contracts,
model::order::OrderData,
settlement::Settlement,
shared::ethrpc::Web3,
};

Expand Down
Loading

0 comments on commit 81d992b

Please sign in to comment.