Skip to content

Commit

Permalink
restructure
Browse files Browse the repository at this point in the history
  • Loading branch information
hardyjosh committed Jan 10, 2024
1 parent 9c1ce09 commit e8075ae
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 64 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
82 changes: 19 additions & 63 deletions crates/cli/src/commands/deposit.rs
Original file line number Diff line number Diff line change
@@ -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::<Address>()?)
.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,

Expand All @@ -57,42 +43,12 @@ pub struct DepositArgs {
vault_id: u64,
}

impl DepositArgs {
pub fn to_deposit_call(&self) -> Result<depositCall> {
let token_address = self.token.parse::<Address>()?;
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<DepositArgs> for CliDepositArgs {
fn into(self) -> DepositArgs {
DepositArgs {
token: self.token,
amount: self.amount,
vault_id: self.vault_id,
}
}
}
12 changes: 12 additions & 0 deletions crates/cli/src/transaction.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use clap::Args;
use rain_orderbook_common::transaction::TransactionArgs;

#[derive(Args)]
pub struct CliTransactionArgs {
Expand All @@ -14,3 +15,14 @@ pub struct CliTransactionArgs {
#[arg(short, long, help = "RPC URL")]
pub rpc_url: String,
}

impl Into<TransactionArgs> 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,
}
}
}
16 changes: 16 additions & 0 deletions crates/common/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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" }
49 changes: 49 additions & 0 deletions crates/common/src/deposit.rs
Original file line number Diff line number Diff line change
@@ -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<depositCall> {
let token_address = self.token.parse::<Address>()?;
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());
}
}
2 changes: 2 additions & 0 deletions crates/common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod deposit;
pub mod transaction;
26 changes: 26 additions & 0 deletions crates/common/src/transaction.rs
Original file line number Diff line number Diff line change
@@ -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<usize>,
pub chain_id: u64,
pub rpc_url: String,
}

impl TransactionArgs {
pub async fn to_transaction_request(
&self,
call_data: Vec<u8>,
) -> anyhow::Result<AlloyTransactionRequest> {
let tx = AlloyTransactionRequest::default()
.with_to(self.orderbook_address.parse::<Address>()?)
.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> {
LedgerClient::new(self.derivation_path, self.chain_id, self.rpc_url.clone()).await
}
}

0 comments on commit e8075ae

Please sign in to comment.