Skip to content

Commit

Permalink
Setup formatting and pre-commit
Browse files Browse the repository at this point in the history
  • Loading branch information
m30m committed Jan 11, 2024
1 parent 11f5c4f commit e2a7ceb
Show file tree
Hide file tree
Showing 10 changed files with 333 additions and 115 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/ci-pre-commit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Pre-commit checks

on:
pull_request:
push:
branches: [main]

jobs:
pre-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
# Need to grab the history of the PR
fetch-depth: 0
- uses: actions/setup-python@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly-2023-03-01
components: rustfmt, clippy
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly-2023-07-23
components: rustfmt, clippy
- uses: pre-commit/[email protected]
if: ${{ github.event_name == 'pull_request' }}
with:
# Run only on files changed in the PR
extra_args: --from-ref ${{ github.event.pull_request.base.sha }} --to-ref ${{ github.event.pull_request.head.sha }}
- uses: pre-commit/[email protected]
if: ${{ github.event_name != 'pull_request' }}
26 changes: 26 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.2.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-added-large-files
exclude: package-lock.json
# Hook to format many type of files in the repo
# including solidity contracts.
- repo: https://github.com/pre-commit/mirrors-prettier
rev: "v2.7.1"
hooks:
- id: prettier
additional_dependencies:
- "[email protected]"
- "[email protected]"
- repo: local
hooks:
# Hooks for auction server
- id: cargo-fmt-auction-server
name: Cargo format for auction server
language: "rust"
entry: cargo +nightly-2023-07-23 fmt --manifest-path ./auction-server/Cargo.toml --all -- --config-path rustfmt.toml
pass_filenames: false
files: auction-server
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@ $ source pythresearch/per/.env
$ set +a
```

The updated enviornment variables can then be seen via `env`. We can then run the appropriate forge tests which will pull the relevant bundle information from the environment variables. To do this, run `forge test -vvv --via-ir --match-test {TestToBeRun}`. Note that you need to `source` the `.env` file in the same session as the one in which you run the forge tests.
The updated enviornment variables can then be seen via `env`. We can then run the appropriate forge tests which will pull the relevant bundle information from the environment variables. To do this, run `forge test -vvv --via-ir --match-test {TestToBeRun}`. Note that you need to `source` the `.env` file in the same session as the one in which you run the forge tests.

### pre-commit hooks

pre-commit is a tool that checks and fixes simple issues (formatting, ...) before each commit. You can install it by following [their website](https://pre-commit.com/). In order to enable checks for this repo run `pre-commit install` from command-line in the root of this repo.

The checks are also performed in the CI to ensure the code follows consistent formatting.
88 changes: 63 additions & 25 deletions auction-server/src/api.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,65 @@
use std::collections::HashMap;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;

use axum::{
routing::{get, post},
Router,
use {
crate::{
api::rest::Bid,
auction::run_submission_loop,
config::{
ChainId,
Config,
RunOptions,
},
state::{
ChainStore,
Store,
},
},
anyhow::{
anyhow,
Result,
},
axum::{
http::StatusCode,
response::{
IntoResponse,
Response,
},
routing::{
get,
post,
},
Router,
},
clap::crate_version,
ethers::{
providers::{
Http,
Middleware,
Provider,
},
signers::{
LocalWallet,
Signer,
},
types::Address,
},
futures::future::join_all,
std::{
collections::HashMap,
sync::{
atomic::{
AtomicBool,
Ordering,
},
Arc,
},
},
tower_http::cors::CorsLayer,
utoipa::{
OpenApi,
ToResponse,
ToSchema,
},
utoipa_swagger_ui::SwaggerUi,
};
use clap::crate_version;
use ethers::providers::{Http, Middleware, Provider};
use ethers::types::Address;
use futures::future::join_all;
use tower_http::cors::CorsLayer;
use utoipa::{OpenApi, ToResponse, ToSchema};
use utoipa_swagger_ui::SwaggerUi;

use crate::api::rest::Bid;
use crate::auction::run_submission_loop;
use crate::config::{ChainId, Config, RunOptions};
use crate::state::{ChainStore, Store};
use anyhow::{anyhow, Result};
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use ethers::signers::{LocalWallet, Signer};

// A static exit flag to indicate to running threads that we're shutting down. This is used to
// gracefully shutdown the application.
Expand Down Expand Up @@ -127,7 +165,7 @@ pub async fn start_server(run_options: RunOptions) -> Result<()> {
network_id: id,
bids: Default::default(),
config: chain_config.clone(),
opps: Default::default()
opps: Default::default(),
},
))
},
Expand All @@ -137,7 +175,7 @@ pub async fn start_server(run_options: RunOptions) -> Result<()> {
.collect();

let store = Arc::new(Store {
chains: chain_store?,
chains: chain_store?,
per_operator: wallet,
});

Expand Down
93 changes: 64 additions & 29 deletions auction-server/src/api/rest.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,42 @@
use crate::api::RestError;
use crate::auction::per::MulticallStatus;
use crate::auction::simulate_bids;
use crate::state::{GetOppsParams, Opportunity, SimulatedBid, Store};
use axum::{extract::State, Json};
use ethers::abi::Address;
use ethers::contract::EthError;
use ethers::middleware::contract::ContractError;

use axum::extract::Query;
use ethers::signers::Signer;
use ethers::types::{Bytes, U256};
use ethers::utils::hex::FromHex;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use utoipa::ToSchema;
use {
crate::{
api::RestError,
auction::{
per::MulticallStatus,
simulate_bids,
},
state::{
GetOppsParams,
Opportunity,
SimulatedBid,
Store,
},
},
axum::{
extract::{
Query,
State,
},
Json,
},
ethers::{
abi::Address,
contract::EthError,
middleware::contract::ContractError,
signers::Signer,
types::{
Bytes,
U256,
},
utils::hex::FromHex,
},
serde::{
Deserialize,
Serialize,
},
std::sync::Arc,
utoipa::ToSchema,
};

#[derive(Serialize, Deserialize, ToSchema, Clone)]
pub struct Bid {
Expand All @@ -22,16 +45,16 @@ pub struct Bid {
permission_key: String,
/// The chain id to bid on.
#[schema(example = "sepolia")]
chain_id: String,
chain_id: String,
/// The contract address to call.
#[schema(example = "0xcA11bde05977b3631167028862bE2a173976CA11")]
contract: String,
contract: String,
/// Calldata for the contract call.
#[schema(example = "0xdeadbeef")]
calldata: String,
calldata: String,
/// Amount of bid in wei.
#[schema(example = "1000000000000000000")]
bid: String,
bid: String,
}

/// Bid on a specific permission key for a specific chain.
Expand Down Expand Up @@ -137,7 +160,7 @@ pub async fn surface(
.chains
.get(&opp.chain_id)
.ok_or(RestError::InvalidChainId)?;

let contract = opp
.contract
.parse::<Address>()
Expand All @@ -157,7 +180,7 @@ pub async fn surface(
/// Get liquidation opportunities
///
// #[axum_macros::debug_handler]
#[utoipa::path(get, path = "/getOpps",
#[utoipa::path(get, path = "/getOpps",
params(
("chain_id" = String, Query, description = "Chain ID to retrieve opportunities for"),
("contract" = Option<String>, Query, description = "Contract address to filter by")
Expand All @@ -169,11 +192,11 @@ pub async fn surface(
,)]
pub async fn get_opps(
State(store): State<Arc<Store>>,
Query(params): Query<GetOppsParams>
) -> Result<Json<Vec<Opportunity>>, RestError> {
Query(params): Query<GetOppsParams>,
) -> Result<Json<Vec<Opportunity>>, RestError> {
let chain_id = params.chain_id;
let contract = params.contract;

let chain_store = store
.chains
.get(&chain_id)
Expand All @@ -187,16 +210,28 @@ pub async fn get_opps(
.parse::<Address>()
.map_err(|_| RestError::BadParameters("Invalid contract address".to_string()))?;

opps = chain_store.opps.write().await.entry(key).or_default().to_vec();
},
opps = chain_store
.opps
.write()
.await
.entry(key)
.or_default()
.to_vec();
}
None => {
let opps_dict = chain_store.opps.write().await;

for key in opps_dict.keys() {
let opps_key = chain_store.opps.write().await.entry(key.clone()).or_default().clone();
let opps_key = chain_store
.opps
.write()
.await
.entry(key.clone())
.or_default()
.clone();
opps.extend(opps_key);
}
}
}
Ok(Json(opps))
}
}
61 changes: 43 additions & 18 deletions auction-server/src/auction.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,50 @@
use anyhow::anyhow;
use std::{
sync::{atomic::Ordering, Arc},
time::Duration,
};

use ethers::{
contract::{abigen, ContractError},
middleware::{
transformer::{Transformer, TransformerError},
SignerMiddleware, TransformerMiddleware,
use {
crate::{
api::SHOULD_EXIT,
config::EthereumConfig,
state::Store,
},
providers::{Http, Provider, ProviderError},
signers::{LocalWallet, Signer},
types::{
transaction::eip2718::TypedTransaction, Address, Bytes, TransactionReceipt,
TransactionRequest, U256,
anyhow::anyhow,
ethers::{
contract::{
abigen,
ContractError,
},
middleware::{
transformer::{
Transformer,
TransformerError,
},
SignerMiddleware,
TransformerMiddleware,
},
providers::{
Http,
Provider,
ProviderError,
},
signers::{
LocalWallet,
Signer,
},
types::{
transaction::eip2718::TypedTransaction,
Address,
Bytes,
TransactionReceipt,
TransactionRequest,
U256,
},
},
std::{
sync::{
atomic::Ordering,
Arc,
},
time::Duration,
},
};

use crate::{api::SHOULD_EXIT, config::EthereumConfig, state::Store};

abigen!(PER, "src/PERMulticall.json");
pub type PERContract = PER<Provider<Http>>;
pub type SignableProvider =
Expand Down
Loading

0 comments on commit e2a7ceb

Please sign in to comment.