Skip to content

Commit

Permalink
try_into for depositargs
Browse files Browse the repository at this point in the history
  • Loading branch information
hardyjosh committed Jan 10, 2024
1 parent a4ae1ec commit 6f3ad48
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
7 changes: 5 additions & 2 deletions crates/cli/src/commands/deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use alloy_ethers_typecast::transaction::ExecutableTransaction;
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)]
Expand All @@ -16,9 +17,11 @@ pub struct Deposit {
impl Deposit {
pub async fn execute(self) -> Result<()> {
let deposit_args: DepositArgs = self.deposit_args.into();
let call = deposit_args.to_deposit_call()?;
let deposit_call: depositCall = deposit_args.try_into()?;
let tx_args: TransactionArgs = self.transaction_args.into();
let request = tx_args.to_transaction_request_with_call(call).await?;
let request = tx_args
.to_transaction_request_with_call(deposit_call)
.await?;
let ledger_client = tx_args.to_ledger_client().await?;

let tx =
Expand Down
40 changes: 25 additions & 15 deletions crates/common/src/deposit.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
use alloy_primitives::{Address, U256};
use anyhow::Result;
use rain_orderbook_bindings::IOrderBookV3::depositCall;
use std::convert::TryInto;

pub struct DepositArgs {
pub token: String,
pub amount: u64,
pub vault_id: u64,
}

impl DepositArgs {
pub fn to_deposit_call(&self) -> Result<depositCall> {
impl TryInto<depositCall> for DepositArgs {
type Error = anyhow::Error;

fn try_into(self) -> Result<depositCall> {
let token_address = self.token.parse::<Address>()?;
let amount = U256::from(self.amount);
let vault_id = U256::from(self.vault_id);
Expand All @@ -27,23 +30,30 @@ mod tests {
use super::*;

#[test]
fn test_to_deposit_call_valid() {
fn test_deposit_args_try_into() {
let args = DepositArgs {
token: "0xdcdee0E7a58Bba7e305dB3Abc42F4887CE8EF729".to_string(),
token: "0x1234567890abcdef1234567890abcdef12345678".to_string(),
amount: 100,
vault_id: 1,
vault_id: 42,
};
let result = args.to_deposit_call();

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

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

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());
let deposit_call = result.unwrap();
assert_eq!(
deposit_call.token,
"0x1234567890abcdef1234567890abcdef12345678"
.parse::<Address>()
.unwrap()
);
assert_eq!(deposit_call.amount, U256::from(100));
assert_eq!(deposit_call.vaultId, U256::from(42));
}
}

0 comments on commit 6f3ad48

Please sign in to comment.