Skip to content

Commit

Permalink
Merge branch '2024-01-24-cli-remove-abstraction' into 2024-01-11-cli-…
Browse files Browse the repository at this point in the history
…add-order
  • Loading branch information
mattyg committed Jan 24, 2024
2 parents 6463deb + d2fd150 commit c7ebed4
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 85 deletions.
4 changes: 2 additions & 2 deletions crates/cli/src/commands/vault/deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
execute::Execute, status::display_write_transaction_status,
transaction::CliTransactionCommandArgs,
};
use alloy_primitives::U256;
use alloy_primitives::{Address, U256};
use anyhow::Result;
use clap::Args;
use rain_orderbook_common::{deposit::DepositArgs, transaction::TransactionArgs};
Expand Down Expand Up @@ -35,7 +35,7 @@ impl Execute for Deposit {
#[derive(Args, Clone)]
pub struct CliDepositArgs {
#[arg(short, long, help = "The token address in hex format")]
token: String,
token: Address,

#[arg(short, long, help = "The ID of the vault")]
vault_id: U256,
Expand Down
4 changes: 2 additions & 2 deletions crates/cli/src/commands/vault/withdraw.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::status::display_write_transaction_status;
use crate::{execute::Execute, transaction::CliTransactionCommandArgs};
use alloy_primitives::U256;
use alloy_primitives::{Address, U256};
use anyhow::Result;
use clap::Args;
use rain_orderbook_common::transaction::TransactionArgs;
Expand All @@ -27,7 +27,7 @@ impl Execute for Withdraw {
#[derive(Args, Clone)]
pub struct CliWithdrawArgs {
#[arg(short, long, help = "The token address in hex format")]
token: String,
token: Address,

#[arg(short, long, help = "The ID of the vault")]
vault_id: U256,
Expand Down
4 changes: 2 additions & 2 deletions crates/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ pub enum Orderbook {
Order(Order),

#[command(subcommand)]
Vault(Vault),
Vault(Box<Vault>),
}

impl Orderbook {
pub async fn execute(self) -> Result<()> {
match self {
Orderbook::Order(order) => order.execute().await,
Orderbook::Vault(vault) => vault.execute().await,
Orderbook::Vault(vault) => (*vault).execute().await,
}
}
}
4 changes: 2 additions & 2 deletions crates/cli/src/transaction.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use alloy_primitives::U256;
use alloy_primitives::{Address, U256};
use clap::Args;
use clap::FromArgMatches;
use clap::Parser;
Expand All @@ -7,7 +7,7 @@ use rain_orderbook_common::transaction::TransactionArgs;
#[derive(Args, Clone)]
pub struct CliTransactionArgs {
#[arg(short, long, help = "Orderbook contract address")]
pub orderbook_address: String,
pub orderbook_address: Address,

#[arg(
short,
Expand Down
62 changes: 25 additions & 37 deletions crates/common/src/deposit.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,32 @@
use crate::error::WritableTransactionExecuteError;
use crate::transaction::TransactionArgs;
use alloy_ethers_typecast::transaction::WriteTransaction;
use alloy_ethers_typecast::{ethers_address_to_alloy, transaction::WriteTransactionStatus};
use alloy_primitives::{hex::FromHexError, Address, U256};
use alloy_ethers_typecast::transaction::{WriteTransaction, WriteTransactionStatus};
use alloy_primitives::{Address, U256};
use rain_orderbook_bindings::{IOrderBookV3::depositCall, IERC20::approveCall};
use serde::{Deserialize, Serialize};
use std::convert::TryInto;

#[derive(Clone, Serialize, Deserialize)]
pub struct DepositArgs {
pub token: String,
pub token: Address,
pub vault_id: U256,
pub amount: U256,
}

impl TryInto<depositCall> for DepositArgs {
type Error = FromHexError;

fn try_into(self) -> Result<depositCall, FromHexError> {
Ok(depositCall {
token: self.token.parse()?,
vaultId: self.vault_id,
amount: self.amount,
})
impl From<DepositArgs> for depositCall {
fn from(val: DepositArgs) -> Self {
depositCall {
token: val.token,
vaultId: val.vault_id,
amount: val.amount,
}
}
}

impl DepositArgs {
pub fn into_approve_call(self, spender: Address) -> approveCall {
/// Build ERC20 approve call
pub fn into_approve_call(&self, orderbook_address: Address) -> approveCall {
approveCall {
spender,
spender: orderbook_address,
amount: self.amount,
}
}
Expand All @@ -46,10 +43,9 @@ impl DepositArgs {
.await
.map_err(WritableTransactionExecuteError::TransactionArgs)?;

let ledger_address = ethers_address_to_alloy(ledger_client.client.address());
let approve_call = self.clone().into_approve_call(ledger_address);
let approve_call = self.into_approve_call(transaction_args.orderbook_address);
let params = transaction_args
.try_into_write_contract_parameters(approve_call)
.try_into_write_contract_parameters(approve_call, self.token)
.await
.map_err(WritableTransactionExecuteError::TransactionArgs)?;

Expand All @@ -73,13 +69,9 @@ impl DepositArgs {
.await
.map_err(WritableTransactionExecuteError::TransactionArgs)?;

let deposit_call: depositCall = self.clone().try_into().map_err(|_| {
WritableTransactionExecuteError::InvalidArgs(
"Failed to parse address String into Address".into(),
)
})?;
let deposit_call: depositCall = self.clone().into();
let params = transaction_args
.try_into_write_contract_parameters(deposit_call)
.try_into_write_contract_parameters(deposit_call, transaction_args.orderbook_address)
.await
.map_err(WritableTransactionExecuteError::TransactionArgs)?;

Expand All @@ -98,23 +90,17 @@ mod tests {
use alloy_primitives::{hex, Address};

#[test]
fn test_deposit_args_try_into() {
fn test_deposit_args_into() {
let args = DepositArgs {
token: "0x1234567890abcdef1234567890abcdef12345678".to_string(),
token: "0x1234567890abcdef1234567890abcdef12345678"
.parse::<Address>()
.unwrap(),
vault_id: U256::from(42),
amount: U256::from(100),
};

let result: Result<depositCall, _> = args.try_into();
let deposit_call: depositCall = args.into();

match result {
Ok(_) => (),
Err(e) => panic!("Unexpected error: {}", e),
}

assert!(result.is_ok());

let deposit_call = result.unwrap();
assert_eq!(
deposit_call.token,
"0x1234567890abcdef1234567890abcdef12345678"
Expand All @@ -128,7 +114,9 @@ mod tests {
#[test]
fn test_deposit_args_into_approve_call() {
let args = DepositArgs {
token: "0x1234567890abcdef1234567890abcdef12345678".to_string(),
token: "0x1234567890abcdef1234567890abcdef12345678"
.parse::<Address>()
.unwrap(),
vault_id: U256::from(42),
amount: U256::from(100),
};
Expand Down
14 changes: 4 additions & 10 deletions crates/common/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ use alloy_ethers_typecast::{
WriteContractParametersBuilder, WriteContractParametersBuilderError,
},
};
use alloy_primitives::{hex::FromHexError, Address, U256};
use alloy_primitives::{Address, U256};
use alloy_sol_types::SolCall;
use serde::{Deserialize, Serialize};
use thiserror::Error;

#[derive(Error, Debug)]
pub enum TransactionArgsError {
#[error("Parse orderbook address error: {0}")]
ParseOrderbookAddress(#[from] FromHexError),
#[error("Build parameters error: {0}")]
BuildParameters(#[from] WriteContractParametersBuilderError),
#[error("Parse Chain ID U256 to u64 error")]
Expand All @@ -28,7 +26,7 @@ pub enum TransactionArgsError {

#[derive(Clone, Serialize, Deserialize)]
pub struct TransactionArgs {
pub orderbook_address: String,
pub orderbook_address: Address,
pub derivation_index: Option<usize>,
pub chain_id: Option<u64>,
pub rpc_url: String,
Expand All @@ -40,14 +38,10 @@ impl TransactionArgs {
pub async fn try_into_write_contract_parameters<T: SolCall + Clone>(
&self,
call: T,
contract: Address,
) -> Result<WriteContractParameters<T>, TransactionArgsError> {
let orderbook_address = self
.orderbook_address
.parse::<Address>()
.map_err(TransactionArgsError::ParseOrderbookAddress)?;

WriteContractParametersBuilder::default()
.address(orderbook_address)
.address(contract)
.call(call)
.max_priority_fee_per_gas(self.max_priority_fee_per_gas)
.max_fee_per_gas(self.max_fee_per_gas)
Expand Down
46 changes: 16 additions & 30 deletions crates/common/src/withdraw.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
use crate::{error::WritableTransactionExecuteError, transaction::TransactionArgs};
use alloy_ethers_typecast::transaction::{WriteTransaction, WriteTransactionStatus};
use alloy_primitives::{hex::FromHexError, Address, U256};
use alloy_primitives::{Address, U256};
use rain_orderbook_bindings::IOrderBookV3::withdrawCall;
use serde::{Deserialize, Serialize};
use std::convert::TryInto;

#[derive(Clone, Deserialize, Serialize, Debug)]
pub struct WithdrawArgs {
pub token: String,
pub token: Address,
pub vault_id: U256,
pub target_amount: U256,
}

impl TryInto<withdrawCall> for WithdrawArgs {
type Error = FromHexError;

fn try_into(self) -> Result<withdrawCall, FromHexError> {
Ok(withdrawCall {
token: self.token.parse::<Address>()?,
vaultId: self.vault_id,
targetAmount: self.target_amount,
})
impl From<WithdrawArgs> for withdrawCall {
fn from(val: WithdrawArgs) -> Self {
withdrawCall {
token: val.token,
vaultId: val.vault_id,
targetAmount: val.target_amount,
}
}
}

Expand All @@ -37,13 +34,9 @@ impl WithdrawArgs {
.await
.map_err(WritableTransactionExecuteError::TransactionArgs)?;

let withdraw_call: withdrawCall = self.clone().try_into().map_err(|_| {
WritableTransactionExecuteError::InvalidArgs(
"Failed to parse address String into Address".into(),
)
})?;
let withdraw_call: withdrawCall = self.clone().into();
let params = transaction_args
.try_into_write_contract_parameters(withdraw_call)
.try_into_write_contract_parameters(withdraw_call, transaction_args.orderbook_address)
.await
.map_err(WritableTransactionExecuteError::TransactionArgs)?;

Expand All @@ -61,23 +54,16 @@ mod tests {
use super::*;

#[test]
fn test_withdraw_args_try_into() {
fn test_withdraw_args_into() {
let args = WithdrawArgs {
token: "0x1234567890abcdef1234567890abcdef12345678".to_string(),
token: "0x1234567890abcdef1234567890abcdef12345678"
.parse::<Address>()
.unwrap(),
vault_id: U256::from(42),
target_amount: U256::from(100),
};

let result: Result<withdrawCall, _> = args.try_into();

match result {
Ok(_) => (),
Err(e) => panic!("Unexpected error: {}", e),
}

assert!(result.is_ok());

let withdraw_call = result.unwrap();
let withdraw_call: withdrawCall = args.into();
assert_eq!(
withdraw_call.token,
"0x1234567890abcdef1234567890abcdef12345678"
Expand Down

0 comments on commit c7ebed4

Please sign in to comment.