Skip to content

Commit

Permalink
Rust improvement (#9)
Browse files Browse the repository at this point in the history
* Improved rust endpoints for liquidation marketplace

* Update sample config.yaml

* Add python requirements.txt

* Fix pre-commit

* Remove unused imports and improve readability

* Use the built abi from the solidity folder

* Rename order to opportunity

* Remove prices field from python side

* Store a single opportunity per permission key

* Consistent naming
  • Loading branch information
m30m authored Jan 23, 2024
1 parent 5fc5133 commit 44cf436
Show file tree
Hide file tree
Showing 16 changed files with 553 additions and 5,933 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Python cache files
__pycache__
*.pyc
venv

# any config files
config.yaml
Expand All @@ -14,4 +15,3 @@ api_keys.py
# env files
*.env
per_multicall/latestEnvironment.json

2 changes: 1 addition & 1 deletion auction-server/config.sample.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
chains:
development:
geth_rpc_addr: http://localhost:8545
contract_addr: 0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512
contract_addr: 0xa513E6E4b8f2a923D98304ec87F64353C4D5C853
legacy_tx: false
5,664 changes: 0 additions & 5,664 deletions auction-server/src/PERMulticall.json

This file was deleted.

39 changes: 31 additions & 8 deletions auction-server/src/api.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
use {
crate::{
api::rest::Bid,
api::{
marketplace::{
LiquidationOpportunity,
OpportunityBid,
TokenQty,
},
rest::Bid,
},
auction::run_submission_loop,
config::{
ChainId,
Expand All @@ -9,6 +16,7 @@ use {
},
state::{
ChainStore,
LiquidationStore,
Store,
},
},
Expand Down Expand Up @@ -39,7 +47,6 @@ use {
LocalWallet,
Signer,
},
types::Address,
},
futures::future::join_all,
std::{
Expand Down Expand Up @@ -73,6 +80,7 @@ async fn root() -> String {
format!("PER Auction Server API {}", crate_version!())
}

pub(crate) mod marketplace;
mod rest;

#[derive(ToResponse, ToSchema)]
Expand All @@ -82,6 +90,8 @@ pub enum RestError {
BadParameters(String),
/// The chain id is not supported
InvalidChainId,
/// The order was not found
OpportunityNotFound,
/// The server cannot currently communicate with the blockchain, so is not able to verify
/// which random values have been requested.
TemporarilyUnavailable,
Expand All @@ -98,6 +108,11 @@ impl IntoResponse for RestError {
RestError::InvalidChainId => {
(StatusCode::BAD_REQUEST, "The chain id is not supported").into_response()
}
RestError::OpportunityNotFound => (
StatusCode::NOT_FOUND,
"Order with the specified id was not found",
)
.into_response(),

RestError::TemporarilyUnavailable => (
StatusCode::SERVICE_UNAVAILABLE,
Expand Down Expand Up @@ -125,9 +140,11 @@ pub async fn start_server(run_options: RunOptions) -> Result<()> {
#[openapi(
paths(
rest::bid,
marketplace::submit_opportunity,
marketplace::fetch_opportunities,
),
components(
schemas(Bid), responses(RestError)
schemas(Bid),schemas(LiquidationOpportunity),schemas(OpportunityBid), schemas(TokenQty),responses(RestError)
),
tags(
(name = "PER Auction", description = "Pyth Express Relay Auction Server")
Expand Down Expand Up @@ -165,7 +182,6 @@ pub async fn start_server(run_options: RunOptions) -> Result<()> {
network_id: id,
bids: Default::default(),
config: chain_config.clone(),
opps: Default::default(),
},
))
},
Expand All @@ -175,8 +191,9 @@ pub async fn start_server(run_options: RunOptions) -> Result<()> {
.collect();

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

let server_store = store.clone();
Expand All @@ -187,8 +204,14 @@ pub async fn start_server(run_options: RunOptions) -> Result<()> {
.merge(SwaggerUi::new("/docs").url("/docs/openapi.json", ApiDoc::openapi()))
.route("/", get(root))
.route("/bid", post(rest::bid))
.route("/surface", post(rest::surface))
.route("/getOpps", get(rest::get_opps))
.route(
"/liquidation/submit_opportunity",
post(marketplace::submit_opportunity),
)
.route(
"/liquidation/fetch_opportunities",
get(marketplace::fetch_opportunities),
)
.layer(CorsLayer::permissive())
.with_state(server_store);

Expand Down
232 changes: 232 additions & 0 deletions auction-server/src/api/marketplace.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
use {
crate::{
api::{
rest::handle_bid,
RestError,
},
liquidation_adapter::make_liquidator_calldata,
state::Store,
},
axum::{
extract::State,
Json,
},
ethers::{
abi::Address,
core::types::Signature,
types::{
Bytes,
U256,
},
},
serde::{
Deserialize,
Serialize,
},
std::sync::Arc,
utoipa::ToSchema,
uuid::Uuid,
};

#[derive(Serialize, Deserialize, ToSchema, Clone)]
pub struct TokenQty {
/// Token contract address
#[schema(example = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",value_type=String)]
contract: Address,
/// Token amount
#[schema(example = "1000")]
amount: String,
}

/// A liquidation opportunity ready to be executed.
/// If a searcher signs the opportunity and have approved enough tokens to liquidation adapter, by calling this contract with the given calldata and structures, they will receive the tokens specified in the receipt_tokens field, and will send the tokens specified in the repay_tokens field.
#[derive(Serialize, Deserialize, ToSchema, Clone)]
pub struct LiquidationOpportunity {
/// The permission key required for succesful execution of the liquidation.
#[schema(example = "0xdeadbeefcafe", value_type=String)]
permission_key: Bytes,
/// The chain id where the liquidation will be executed.
#[schema(example = "sepolia")]
chain_id: String,
/// The contract address to call for execution of the liquidation.
#[schema(example = "0xcA11bde05977b3631167028862bE2a173976CA11", value_type=String)]
contract: Address,
/// Calldata for the contract call.
#[schema(example = "0xdeadbeef", value_type=String)]
calldata: Bytes,

repay_tokens: Vec<TokenQty>,
receipt_tokens: Vec<TokenQty>,
}

impl From<(Address, U256)> for TokenQty {
fn from(token: (Address, U256)) -> Self {
TokenQty {
contract: token.0,
amount: token.1.to_string(),
}
}
}

impl TryFrom<TokenQty> for (Address, U256) {
type Error = RestError;

fn try_from(token: TokenQty) -> Result<Self, Self::Error> {
let amount = U256::from_dec_str(token.amount.as_str())
.map_err(|_| RestError::BadParameters("Invalid token amount".to_string()))?;
Ok((token.contract, amount))
}
}

fn parse_tokens(tokens: Vec<TokenQty>) -> Result<Vec<(Address, U256)>, RestError> {
tokens.into_iter().map(|token| token.try_into()).collect()
}

/// Submit a liquidation opportunity ready to be executed.
///
/// The opportunity will be verified by the server. If the opportunity is valid, it will be stored in the database and will be available for bidding.
#[utoipa::path(post, path = "/liquidation/submit_opportunity", request_body = LiquidationOpportunity, responses(
(status = 200, description = "Opportunity was stored succesfuly", body = String),
(status = 400, response=RestError)
),)]
pub async fn submit_opportunity(
State(store): State<Arc<Store>>,
Json(opportunity): Json<LiquidationOpportunity>,
) -> Result<String, RestError> {
store
.chains
.get(&opportunity.chain_id)
.ok_or(RestError::InvalidChainId)?;

let repay_tokens = parse_tokens(opportunity.repay_tokens)?;
let receipt_tokens = parse_tokens(opportunity.receipt_tokens)?;

//TODO: Verify if the call actually works

store.liquidation_store.opportunities.write().await.insert(
opportunity.permission_key.clone(),
crate::state::VerifiedLiquidationOpportunity {
id: Uuid::new_v4(),
chain_id: opportunity.chain_id.clone(),
permission_key: opportunity.permission_key,
contract: opportunity.contract,
calldata: opportunity.calldata,
repay_tokens,
receipt_tokens,
},
);

Ok("OK".to_string())
}

/// Fetch all liquidation opportunities ready to be exectued.
#[utoipa::path(get, path = "/liquidation/fetch_opportunities", responses(
(status = 200, description = "Array of liquidation opportunities ready for bidding", body = Vec<LiquidationOpportunity>),
(status = 400, response=RestError)
),)]
pub async fn fetch_opportunities(
State(store): State<Arc<Store>>,
) -> Result<axum::Json<Vec<LiquidationOpportunity>>, RestError> {
let opportunities: Vec<LiquidationOpportunity> = store
.liquidation_store
.opportunities
.read()
.await
.values()
.cloned()
.map(|opportunity| LiquidationOpportunity {
permission_key: opportunity.permission_key,
chain_id: opportunity.chain_id,
contract: opportunity.contract,
calldata: opportunity.calldata,
repay_tokens: opportunity
.repay_tokens
.into_iter()
.map(TokenQty::from)
.collect(),
receipt_tokens: opportunity
.receipt_tokens
.into_iter()
.map(TokenQty::from)
.collect(),
})
.collect();

Ok(opportunities.into())
}

#[derive(Serialize, Deserialize, ToSchema, Clone)]
pub struct OpportunityBid {
/// The opportunity id to bid on.
#[schema(example = "f47ac10b-58cc-4372-a567-0e02b2c3d479",value_type=String)]
opportunity_id: Uuid,
/// The opportunity permission key
#[schema(example = "0xdeadbeefcafe", value_type=String)]
permission_key: Bytes,
/// The bid amount in wei.
#[schema(example = "1000000000000000000")]
bid_amount: String,
/// How long the bid will be valid for.
#[schema(example = "1000000000000000000")]
valid_until: String,
/// Liquidator address
#[schema(example = "0x5FbDB2315678afecb367f032d93F642f64180aa2", value_type=String)]
liquidator: Address,
#[schema(
example = "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef12"
,value_type=String)]
signature: Signature,
}

#[derive(Clone, Copy)]
pub struct VerifiedOpportunityBid {
pub opportunity_id: Uuid,
pub bid_amount: U256,
pub valid_until: U256,
pub liquidator: Address,
pub signature: Signature,
}

pub async fn bid_opportunity(
store: Arc<Store>,
Json(opportunity_bid): Json<OpportunityBid>,
) -> Result<String, RestError> {
let opportunities = store.liquidation_store.opportunities.read().await;

let liquidation = opportunities
.get(&opportunity_bid.permission_key)
.ok_or(RestError::OpportunityNotFound)?;

if liquidation.id != opportunity_bid.opportunity_id {
return Err(RestError::BadParameters(
"Invalid opportunity_id".to_string(),
));
}
let bid_amount = U256::from_dec_str(opportunity_bid.bid_amount.as_str())
.map_err(|_| RestError::BadParameters("Invalid bid_amount".to_string()))?;
let valid_until = U256::from_dec_str(opportunity_bid.valid_until.as_str())
.map_err(|_| RestError::BadParameters("Invalid valid_until".to_string()))?;

let verified_liquidation_bid = VerifiedOpportunityBid {
opportunity_id: opportunity_bid.opportunity_id,
bid_amount,
valid_until,
liquidator: opportunity_bid.liquidator,
signature: opportunity_bid.signature,
};

let per_calldata = make_liquidator_calldata(liquidation.clone(), verified_liquidation_bid)
.map_err(|e| RestError::BadParameters(e.to_string()))?;

handle_bid(
store.clone(),
crate::api::rest::ParsedBid {
permission_key: liquidation.permission_key.clone(),
chain_id: liquidation.chain_id.clone(),
contract: liquidation.contract,
calldata: per_calldata,
bid_amount: verified_liquidation_bid.bid_amount,
},
)
.await
}
Loading

0 comments on commit 44cf436

Please sign in to comment.