From e8075ae16c27c70d72696fa645f1c219b0f48c35 Mon Sep 17 00:00:00 2001 From: Josh Date: Wed, 10 Jan 2024 16:43:17 +0000 Subject: [PATCH] restructure --- Cargo.lock | 12 +++++ crates/cli/Cargo.toml | 3 +- crates/cli/src/commands/deposit.rs | 82 +++++++----------------------- crates/cli/src/transaction.rs | 12 +++++ crates/common/Cargo.toml | 16 ++++++ crates/common/src/deposit.rs | 49 ++++++++++++++++++ crates/common/src/lib.rs | 2 + crates/common/src/transaction.rs | 26 ++++++++++ 8 files changed, 138 insertions(+), 64 deletions(-) create mode 100644 crates/common/Cargo.toml create mode 100644 crates/common/src/deposit.rs create mode 100644 crates/common/src/lib.rs create mode 100644 crates/common/src/transaction.rs diff --git a/Cargo.lock b/Cargo.lock index 28ffe718f..eb62cceaa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2861,6 +2861,7 @@ dependencies = [ "graphql_client", "once_cell", "rain_orderbook_bindings", + "rain_orderbook_common", "rain_orderbook_subgraph_queries", "reqwest", "rust-bigint", @@ -2871,6 +2872,17 @@ dependencies = [ "tracing-subscriber", ] +[[package]] +name = "rain_orderbook_common" +version = "0.0.1" +dependencies = [ + "alloy-ethers-typecast", + "alloy-primitives", + "alloy-sol-types", + "anyhow", + "rain_orderbook_bindings", +] + [[package]] name = "rain_orderbook_subgraph_queries" version = "0.0.4" diff --git a/crates/cli/Cargo.toml b/crates/cli/Cargo.toml index eca545941..9f5d4aba0 100644 --- a/crates/cli/Cargo.toml +++ b/crates/cli/Cargo.toml @@ -9,9 +9,10 @@ homepage = "https://github.com/rainprotocol/rain.orderbook" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +alloy-ethers-typecast = { git = "https://github.com/rainlanguage/alloy-ethers-typecast", rev = "d35a8e08ef240c4a66177f6de4e1de6fcb165b5f" } rain_orderbook_subgraph_queries = { path = "../subgraph" } rain_orderbook_bindings = { path = "../bindings" } -alloy-ethers-typecast = { git = "https://github.com/rainlanguage/alloy-ethers-typecast", rev = "d35a8e08ef240c4a66177f6de4e1de6fcb165b5f" } +rain_orderbook_common = { path = "../common" } anyhow = "1.0.70" clap = { version = "4.2.5", features = ["cargo", "derive"] } graphql_client = "0.12.0" diff --git a/crates/cli/src/commands/deposit.rs b/crates/cli/src/commands/deposit.rs index 635428fa3..d04241f97 100644 --- a/crates/cli/src/commands/deposit.rs +++ b/crates/cli/src/commands/deposit.rs @@ -1,52 +1,38 @@ use crate::transaction::CliTransactionArgs; -use alloy_ethers_typecast::client::LedgerClient; -use alloy_ethers_typecast::request_shim::AlloyTransactionRequest; use alloy_ethers_typecast::transaction::ExecutableTransaction; -use alloy_primitives::{Address, U256, U64}; use alloy_sol_types::SolCall; use anyhow::Result; use clap::Args; use clap::Parser; -use rain_orderbook_bindings::IOrderBookV3::depositCall; +use rain_orderbook_common::{deposit::DepositArgs, transaction::TransactionArgs}; #[derive(Parser)] pub struct Deposit { #[clap(flatten)] - deposit_args: DepositArgs, + deposit_args: CliDepositArgs, #[clap(flatten)] transaction_args: CliTransactionArgs, } impl Deposit { pub async fn execute(self) -> Result<()> { - let call_data = self.deposit_args.to_deposit_call()?.abi_encode(); + let deposit_args: DepositArgs = self.deposit_args.into(); + let call_data = deposit_args.to_deposit_call()?.abi_encode(); + let tx_args: TransactionArgs = self.transaction_args.into(); + let request = tx_args.to_transaction_request(call_data).await?; + let ledger_client = tx_args.to_ledger_client().await?; - let tx = AlloyTransactionRequest::default() - .with_to(self.transaction_args.orderbook_address.parse::
()?) - .with_data(call_data.clone()) - .with_chain_id(U64::from(self.transaction_args.chain_id)); - - let ledger_client = LedgerClient::new( - self.transaction_args.derivation_path, - self.transaction_args.chain_id, - self.transaction_args.rpc_url.clone(), - ) - .await?; - - let transaction = - ExecutableTransaction::from_alloy_transaction_request(tx, ledger_client.client).await?; - - transaction - .execute() - .await - .map_err(|e| anyhow::anyhow!(e))?; + let tx = + ExecutableTransaction::from_alloy_transaction_request(request, ledger_client.client) + .await?; + tx.execute().await.map_err(|e| anyhow::anyhow!(e))?; Ok(()) } } #[derive(Args)] -pub struct DepositArgs { +pub struct CliDepositArgs { #[arg(short, long, help = "The token address in hex format")] token: String, @@ -57,42 +43,12 @@ pub struct DepositArgs { vault_id: u64, } -impl DepositArgs { - pub fn to_deposit_call(&self) -> Result { - let token_address = self.token.parse::
()?; - let amount = U256::from(self.amount); - let vault_id = U256::from(self.vault_id); - - Ok(depositCall { - token: token_address, - amount, - vaultId: vault_id, - }) - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_to_deposit_call_valid() { - let args = DepositArgs { - token: "0xdcdee0E7a58Bba7e305dB3Abc42F4887CE8EF729".to_string(), - amount: 100, - vault_id: 1, - }; - let result = args.to_deposit_call(); - assert!(result.is_ok()); - } - - #[test] - fn test_to_deposit_call_invalid_token() { - let args = DepositArgs { - token: "invalid".to_string(), - amount: 100, - vault_id: 1, - }; - assert!(args.to_deposit_call().is_err()); +impl Into for CliDepositArgs { + fn into(self) -> DepositArgs { + DepositArgs { + token: self.token, + amount: self.amount, + vault_id: self.vault_id, + } } } diff --git a/crates/cli/src/transaction.rs b/crates/cli/src/transaction.rs index 61cca1447..0f090cc24 100644 --- a/crates/cli/src/transaction.rs +++ b/crates/cli/src/transaction.rs @@ -1,4 +1,5 @@ use clap::Args; +use rain_orderbook_common::transaction::TransactionArgs; #[derive(Args)] pub struct CliTransactionArgs { @@ -14,3 +15,14 @@ pub struct CliTransactionArgs { #[arg(short, long, help = "RPC URL")] pub rpc_url: String, } + +impl Into for CliTransactionArgs { + fn into(self) -> TransactionArgs { + TransactionArgs { + orderbook_address: self.orderbook_address, + derivation_path: self.derivation_path, + chain_id: self.chain_id, + rpc_url: self.rpc_url, + } + } +} diff --git a/crates/common/Cargo.toml b/crates/common/Cargo.toml new file mode 100644 index 000000000..62e225a1d --- /dev/null +++ b/crates/common/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "rain_orderbook_common" +version = "0.0.1" +edition = "2021" +license = "CAL-1.0" +description = "Rain Orderbook CLI." +homepage = "https://github.com/rainprotocol/rain.orderbook" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +rain_orderbook_bindings = { path = "../bindings" } +alloy-primitives = "0.5.4" +alloy-ethers-typecast = { git = "https://github.com/rainlanguage/alloy-ethers-typecast", rev = "d35a8e08ef240c4a66177f6de4e1de6fcb165b5f" } +anyhow = "1.0.70" +alloy-sol-types = { version = "0.5.4" } diff --git a/crates/common/src/deposit.rs b/crates/common/src/deposit.rs new file mode 100644 index 000000000..68b69f0f1 --- /dev/null +++ b/crates/common/src/deposit.rs @@ -0,0 +1,49 @@ +use alloy_primitives::{Address, U256}; +use anyhow::Result; +use rain_orderbook_bindings::IOrderBookV3::depositCall; + +pub struct DepositArgs { + pub token: String, + pub amount: u64, + pub vault_id: u64, +} + +impl DepositArgs { + pub fn to_deposit_call(&self) -> Result { + let token_address = self.token.parse::
()?; + let amount = U256::from(self.amount); + let vault_id = U256::from(self.vault_id); + + Ok(depositCall { + token: token_address, + amount, + vaultId: vault_id, + }) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_to_deposit_call_valid() { + let args = DepositArgs { + token: "0xdcdee0E7a58Bba7e305dB3Abc42F4887CE8EF729".to_string(), + amount: 100, + vault_id: 1, + }; + let result = args.to_deposit_call(); + assert!(result.is_ok()); + } + + #[test] + fn test_to_deposit_call_invalid_token() { + let args = DepositArgs { + token: "invalid".to_string(), + amount: 100, + vault_id: 1, + }; + assert!(args.to_deposit_call().is_err()); + } +} diff --git a/crates/common/src/lib.rs b/crates/common/src/lib.rs new file mode 100644 index 000000000..89810a022 --- /dev/null +++ b/crates/common/src/lib.rs @@ -0,0 +1,2 @@ +pub mod deposit; +pub mod transaction; diff --git a/crates/common/src/transaction.rs b/crates/common/src/transaction.rs new file mode 100644 index 000000000..088ecce91 --- /dev/null +++ b/crates/common/src/transaction.rs @@ -0,0 +1,26 @@ +use alloy_ethers_typecast::{client::LedgerClient, request_shim::AlloyTransactionRequest}; +use alloy_primitives::{Address, U64}; +pub struct TransactionArgs { + pub orderbook_address: String, + pub derivation_path: Option, + pub chain_id: u64, + pub rpc_url: String, +} + +impl TransactionArgs { + pub async fn to_transaction_request( + &self, + call_data: Vec, + ) -> anyhow::Result { + let tx = AlloyTransactionRequest::default() + .with_to(self.orderbook_address.parse::
()?) + .with_data(call_data.clone()) + .with_chain_id(U64::from(self.chain_id)); + + Ok(tx) + } + + pub async fn to_ledger_client(self) -> anyhow::Result { + LedgerClient::new(self.derivation_path, self.chain_id, self.rpc_url.clone()).await + } +}