Skip to content

Commit

Permalink
clippy fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Siddharth2207 committed Dec 29, 2023
1 parent f129759 commit 52533d7
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 93 deletions.
5 changes: 4 additions & 1 deletion orderbook-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ alloy-dyn-abi = { version = "0.5.3"}
hex = "0.4.3"
rustc-hex = "2.1.0"
url = "2.5.0"
thiserror = "1.0.52"
thiserror = "1.0.52"

[lints.clippy]
all= "warn"
32 changes: 11 additions & 21 deletions orderbook-rs/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,20 @@ use url::ParseError;
#[derive(Error, Debug)]
pub enum RainOrderbookError {
#[error("Invalid RPC URL")]
InvalidRPC {
#[from]
source: ParseError,
},
InvalidRPC(#[from] ParseError),

#[error("Invalid Contract Function Call")]
InvalidContractFunctionCall {
#[from]
source: ContractError<Provider<Http>>,
},
InvalidContractFunctionCall(#[from] ContractError<Provider<Http>>),

#[error("Invalid Address")]
InvalidAddress {
#[from]
source: FromHexError,
},
InvalidAddress(#[from] FromHexError),

#[error("Failed to confirm transaction")]
TransactionConfirmationError {
#[from]
source: ProviderError,
},
TransactionConfirmationError(#[from] ProviderError),

#[error("Error in Transaction")]
TransactionError {
#[from]
source: SignerMiddlewareError<Provider<Http>, Ledger>,
},
TransactionError(#[from] SignerMiddlewareError<Provider<Http>, Ledger>),

#[error("Failed to fetch Transaction Receipt")]
TransactionReceiptError,
TransactionReceiptError
}
4 changes: 2 additions & 2 deletions orderbook-rs/src/gasoracle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use reqwest::{header::AUTHORIZATION, Client};
use url::Url;

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

/// Blocknative Gas Oracle.
Expand All @@ -19,7 +19,7 @@ pub async fn gas_price_oracle(
chain_id: u64,
) -> anyhow::Result<(f64, f64)> {
let client = Client::new();
let mut url = Url::parse(BLOCKNATIVE_BLOCKPRICES_URL.into())?;
let mut url = Url::parse(BLOCKNATIVE_BLOCKPRICES_URL)?;
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() {
Expand Down
44 changes: 10 additions & 34 deletions orderbook-rs/src/interpreter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,16 @@ pub async fn get_disp(
deployer_npe2: Address,
rpc_url: String,
) -> 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 }),
};

let deployer_npe2_address = match H160::from_str(&deployer_npe2.to_string()) {
Ok(deployer) => deployer,
Err(err) => return Err(RainOrderbookError::InvalidAddress { source: err }),
};
let provider = Provider::<Http>::try_from(rpc_url.clone())?;

let deployer_npe2_address = H160::from_str(&deployer_npe2.to_string())?;
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 }),
};
let store = match deployer_npe2.i_store().call().await {
Ok(i_store) => i_store,
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 }),
};
let interpreter = deployer_npe2.i_interpreter().call().await?;
let store = deployer_npe2.i_store().call().await?;
let parser = deployer_npe2.i_parser().call().await?;

let store = Address::new(store.to_fixed_bytes());
let intepreter = Address::new(interpreter.to_fixed_bytes());
Expand All @@ -63,25 +49,15 @@ pub async fn parse_rainstring(
rainstring: String,
rpc_url: String,
) -> 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 }),
};
let provider = Provider::<Http>::try_from(rpc_url.clone())?;

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

let (sources, constants) = match rain_parser
let (sources, constants) = rain_parser
.parse(ethers::types::Bytes::from(rainstring.as_bytes().to_vec()))
.call()
.await
{
Ok(parse_result) => parse_result,
Err(err) => return Err(RainOrderbookError::InvalidContractFunctionCall { source: err }),
};
.await?;

let bytecode_npe2 = Bytes::from(sources.to_vec());

Expand Down
12 changes: 3 additions & 9 deletions orderbook-rs/src/orderbook/add_order/v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,14 @@ pub async fn add_ob_order(
rainlang_order_string: String,
rpc_url: String,
) -> 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),
};
let (_, _, rain_parser) = get_disp(deployer_address, rpc_url.clone()).await?;
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),
};
parse_rainstring(rain_parser, rainlang_order_string, rpc_url.clone()).await?;

let evaluable_config = EvaluableConfigV3 {
deployer: deployer_address,
bytecode: bytecode.to_vec(),
constants: constants,
constants,
};

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

let chain_id = provider.clone().get_chainid().await.unwrap().as_u64();
let client = SignerMiddleware::new_with_provider_chain(provider, wallet).await?;
Expand Down Expand Up @@ -64,29 +61,21 @@ pub async fn execute_transaction(
}
};

let tx_result = client.send_transaction(tx, None).await;
let pending_tx = client.send_transaction(tx, None).await?;

let receipt = match tx_result {
Ok(tx_result) => {
info!("Transaction submitted. Awaiting block confirmations...");
let approve_receipt = match tx_result.confirmations(1).await {
Ok(receipt) => match receipt {
Some(receipt) => receipt,
None => return Err(RainOrderbookError::TransactionReceiptError),
},
Err(err) => {
return Err(RainOrderbookError::TransactionConfirmationError { source: err })
}
};
info!("Transaction Confirmed!!");
info!(
"✅ Hash : 0x{}",
hex::encode(approve_receipt.transaction_hash.as_bytes().to_vec())
);
approve_receipt
}
Err(err) => return Err(RainOrderbookError::TransactionError { source: err }),
info!("Transaction submitted. Awaiting block confirmations...");
let tx_confimration = pending_tx.confirmations(1).await?;

let tx_receipt = match tx_confimration {
Some(receipt) => receipt,
None => return Err(RainOrderbookError::TransactionReceiptError),
};
info!("Transaction Confirmed!!");
info!(
"✅ Hash : 0x{}",
hex::encode(tx_receipt.transaction_hash.as_bytes())
);
Ok(tx_receipt)

Ok(receipt)

}

0 comments on commit 52533d7

Please sign in to comment.