Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Siddharth2207 committed Dec 27, 2023
1 parent 8be3a1f commit ff9689c
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 72 deletions.
36 changes: 17 additions & 19 deletions orderbook-rs/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,43 +1,41 @@
use thiserror::Error;
use url::ParseError;
use ethers::contract::ContractError;
use ethers::middleware::signer::SignerMiddlewareError;
use ethers::providers::ProviderError;
use ethers::providers::{Http, Provider};
use rustc_hex::FromHexError;
use ethers::middleware::signer::SignerMiddlewareError;
use ethers::signers::Ledger;

use rustc_hex::FromHexError;
use thiserror::Error;
use url::ParseError;

/// RainOrderbookError
/// Enum representing errors thrown by the crate
#[derive(Error, Debug)]
pub enum RainOrderbookError{
#[derive(Error, Debug)]
pub enum RainOrderbookError {
#[error("Invalid RPC URL")]
InvalidRPC{
InvalidRPC {
#[from]
source: ParseError
source: ParseError,
},
#[error("Invalid Contract Function Call")]
InvalidContractFunctionCall{
InvalidContractFunctionCall {
#[from]
source: ContractError<Provider<Http>>
source: ContractError<Provider<Http>>,
},
#[error("Invalid Address")]
InvalidAddress{
InvalidAddress {
#[from]
source: FromHexError
source: FromHexError,
},
#[error("Failed to confirm transaction")]
TransactionConfirmationError{
TransactionConfirmationError {
#[from]
source: ProviderError
source: ProviderError,
},
#[error("Error in Transaction")]
TransactionError{
TransactionError {
#[from]
source: SignerMiddlewareError<Provider<Http>, Ledger>
source: SignerMiddlewareError<Provider<Http>, Ledger>,
},
#[error("Failed to fetch Transaction Receipt")]
TransactionReceiptError,

}
}
9 changes: 5 additions & 4 deletions orderbook-rs/src/gasoracle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use reqwest::{header::AUTHORIZATION, Client};
use url::Url;

/// Bloacknative Base Url for fetching blockprices
static BLOCKNATIVE_BLOCKPRICES_URL: &'static str = "https://api.blocknative.com/gasprices/blockprices";
static BLOCKNATIVE_BLOCKPRICES_URL: &'static str =
"https://api.blocknative.com/gasprices/blockprices";

/// Blocknative Gas Oracle.
/// Returns max priority fee and max fee from blocknative api.
Expand All @@ -17,9 +18,9 @@ pub async fn gas_price_oracle(
api_key: Option<String>,
chain_id: u64,
) -> anyhow::Result<(f64, f64)> {
let client = Client::new();
let mut url = Url::parse(BLOCKNATIVE_BLOCKPRICES_URL.into())? ;
url.set_query(Some(format!("chainid={}",chain_id).as_str()));
let client = Client::new();
let mut url = Url::parse(BLOCKNATIVE_BLOCKPRICES_URL.into())?;
url.set_query(Some(format!("chainid={}", chain_id).as_str()));
let mut request = client.get(url);
if let Some(api_key) = api_key.as_ref() {
request = request.header(AUTHORIZATION, api_key);
Expand Down
45 changes: 16 additions & 29 deletions orderbook-rs/src/interpreter/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::{registry::{IExpressionDeployerV3, IParserV1}, errors::RainOrderbookError};
use crate::{
errors::RainOrderbookError,
registry::{IExpressionDeployerV3, IParserV1},
};
use alloy_primitives::{Address, Bytes, U256};
use ethers::{
providers::{Http, Provider},
Expand All @@ -16,40 +19,30 @@ use std::sync::Arc;
pub async fn get_disp(
deployer_npe2: Address,
rpc_url: String,
) -> Result<(Address, Address, Address),RainOrderbookError> {
) -> Result<(Address, Address, Address), RainOrderbookError> {
let provider = match Provider::<Http>::try_from(rpc_url.clone()) {
Ok(provider) => provider,
Err(err) => {
return Err(RainOrderbookError::InvalidRPC { source: err })
}
Ok(provider) => provider,
Err(err) => return Err(RainOrderbookError::InvalidRPC { source: err }),
};

let deployer_npe2_address = match H160::from_str(&deployer_npe2.to_string()){
let deployer_npe2_address = match H160::from_str(&deployer_npe2.to_string()) {
Ok(deployer) => deployer,
Err(err) => {
return Err(RainOrderbookError::InvalidAddress { source: err })
}
Err(err) => return Err(RainOrderbookError::InvalidAddress { source: err }),
};
let deployer_npe2 =
IExpressionDeployerV3::new(deployer_npe2_address, Arc::new(provider.clone()));

let interpreter = match deployer_npe2.i_interpreter().call().await {
Ok(i_interpreter) => i_interpreter,
Err(err) => {
return Err(RainOrderbookError::InvalidContractFunctionCall { source: err })
}
Err(err) => return Err(RainOrderbookError::InvalidContractFunctionCall { source: err }),
};
let store = match deployer_npe2.i_store().call().await {
Ok(i_store) => i_store,
Err(err) => {
return Err(RainOrderbookError::InvalidContractFunctionCall { source: err })
}
Err(err) => return Err(RainOrderbookError::InvalidContractFunctionCall { source: err }),
};
let parser = match deployer_npe2.i_parser().call().await {
Ok(i_parser) => i_parser,
Err(err) => {
return Err(RainOrderbookError::InvalidContractFunctionCall { source: err })
}
Err(err) => return Err(RainOrderbookError::InvalidContractFunctionCall { source: err }),
};

let store = Address::new(store.to_fixed_bytes());
Expand All @@ -69,19 +62,15 @@ pub async fn parse_rainstring(
parser_address: Address,
rainstring: String,
rpc_url: String,
) -> Result<(Bytes, Vec<U256>),RainOrderbookError> {
) -> Result<(Bytes, Vec<U256>), RainOrderbookError> {
let provider = match Provider::<Http>::try_from(rpc_url.clone()) {
Ok(provider) => provider,
Err(err) => {
return Err(RainOrderbookError::InvalidRPC { source: err })
}
Err(err) => return Err(RainOrderbookError::InvalidRPC { source: err }),
};

let parser_address = match H160::from_str(&parser_address.to_string()) {
Ok(parser) => parser,
Err(err) => {
return Err(RainOrderbookError::InvalidAddress { source: err })
}
Err(err) => return Err(RainOrderbookError::InvalidAddress { source: err }),
};
let rain_parser = IParserV1::new(parser_address, Arc::new(provider.clone()));

Expand All @@ -91,9 +80,7 @@ pub async fn parse_rainstring(
.await
{
Ok(parse_result) => parse_result,
Err(err) => {
return Err(RainOrderbookError::InvalidContractFunctionCall { source: err })
}
Err(err) => return Err(RainOrderbookError::InvalidContractFunctionCall { source: err }),
};

let bytecode_npe2 = Bytes::from(sources.to_vec());
Expand Down
2 changes: 1 addition & 1 deletion orderbook-rs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub mod errors;
pub mod gasoracle;
pub mod interpreter;
pub mod orderbook;
pub mod registry;
pub mod transaction;
pub mod errors;
14 changes: 5 additions & 9 deletions orderbook-rs/src/orderbook/add_order/v3.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::{
errors::RainOrderbookError,
interpreter::{get_disp, parse_rainstring},
registry::IOrderBookV3::{self, EvaluableConfigV3, OrderConfigV2, IO}, errors::RainOrderbookError,
registry::IOrderBookV3::{self, EvaluableConfigV3, OrderConfigV2, IO},
};
use alloy_primitives::Address;
use alloy_sol_types::SolCall;


/// Returns [IOrderBookV3::addOrderCall] transaction data encoded with
/// the function selector, encoding [IOrderBookV3::OrderConfigV2] built from
/// the function parameters : Input Vaults, Output Vaults, and Order expression rainlang string.
Expand All @@ -23,19 +23,15 @@ pub async fn add_ob_order(
output_vaults: Vec<IO>,
rainlang_order_string: String,
rpc_url: String,
) -> Result<Vec<u8>,RainOrderbookError> {
) -> Result<Vec<u8>, RainOrderbookError> {
let (_, _, rain_parser) = match get_disp(deployer_address.clone(), rpc_url.clone()).await {
Ok(parse_result) => parse_result,
Err(err) => {
return Err(err)
}
Err(err) => return Err(err),
};
let (bytecode, constants) =
match parse_rainstring(rain_parser, rainlang_order_string, rpc_url.clone()).await {
Ok(parse_result) => parse_result,
Err(err) => {
return Err(err)
}
Err(err) => return Err(err),
};

let evaluable_config = EvaluableConfigV3 {
Expand Down
14 changes: 4 additions & 10 deletions orderbook-rs/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,10 @@ pub async fn execute_transaction(
rpc_url: String,
wallet: Ledger,
blocknative_api_key: Option<String>,
) -> Result<TransactionReceipt,RainOrderbookError> {
) -> Result<TransactionReceipt, RainOrderbookError> {
let provider = match Provider::<Http>::try_from(rpc_url.clone()) {
Ok(provider) => provider,
Err(err) => {
return Err(RainOrderbookError::InvalidRPC { source: err })
}
Err(err) => return Err(RainOrderbookError::InvalidRPC { source: err }),
};

let chain_id = provider.clone().get_chainid().await.unwrap().as_u64();
Expand Down Expand Up @@ -74,9 +72,7 @@ pub async fn execute_transaction(
let approve_receipt = match tx_result.confirmations(1).await {
Ok(receipt) => match receipt {
Some(receipt) => receipt,
None => {
return Err(RainOrderbookError::TransactionReceiptError)
}
None => return Err(RainOrderbookError::TransactionReceiptError),
},
Err(err) => {
return Err(RainOrderbookError::TransactionConfirmationError { source: err })
Expand All @@ -89,9 +85,7 @@ pub async fn execute_transaction(
);
approve_receipt
}
Err(err) => {
return Err(RainOrderbookError::TransactionError { source: err })
}
Err(err) => return Err(RainOrderbookError::TransactionError { source: err }),
};

Ok(receipt)
Expand Down

0 comments on commit ff9689c

Please sign in to comment.