Skip to content

Commit

Permalink
Merge pull request #162 from cspr-rad/e2e-test-withdraw
Browse files Browse the repository at this point in the history
E2e test withdraw
  • Loading branch information
koxu1996 authored Jul 25, 2024
2 parents 5845080 + a06ae3f commit b7163b2
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 18 deletions.
25 changes: 23 additions & 2 deletions kairos-cli/src/commands/fetch.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use clap::Parser;
use clap::{Parser, ValueEnum};
use reqwest::Url;

use crate::client;
use crate::error::CliError;
use kairos_data::transaction::TransactionFilter;
use kairos_data::transaction::{Transaction, TransactionFilter};

use chrono::NaiveDateTime;

Expand All @@ -21,6 +21,25 @@ pub struct Args {
max_amount: Option<u64>,
#[arg(long, short, value_name = "PUBLIC_KEY_HEX")]
recipient: Option<String>,
#[arg(long, short, value_name = "TRANSACTION_TYPE", value_enum)]
transaction_type: Option<TransactionType>,
}

#[derive(ValueEnum, Debug, Clone)] // ArgEnum here
pub enum TransactionType {
Deposit,
Transfer,
Withdrawal,
}

impl From<TransactionType> for Transaction {
fn from(t: TransactionType) -> Transaction {
match t {
TransactionType::Deposit => Transaction::Deposit,
TransactionType::Transfer => Transaction::Transfer,
TransactionType::Withdrawal => Transaction::Withdrawal,
}
}
}

pub fn run(
Expand All @@ -31,6 +50,7 @@ pub fn run(
min_amount,
max_amount,
recipient,
transaction_type,
}: Args,
kairos_server_address: Url,
) -> Result<String, CliError> {
Expand All @@ -41,6 +61,7 @@ pub fn run(
min_amount,
max_amount,
recipient,
transaction_type: transaction_type.map(Into::into),
};
let transactions = client::fetch(&kairos_server_address, &transaction_filter)
.map_err(Into::<CliError>::into)?;
Expand Down
4 changes: 4 additions & 0 deletions kairos-data/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub struct TransactionFilter {
pub min_amount: Option<u64>,
pub max_amount: Option<u64>,
pub recipient: Option<String>,
pub transaction_type: Option<Transaction>,
}

pub async fn get(
Expand Down Expand Up @@ -67,6 +68,9 @@ pub async fn get(
if let Some(recipient) = filter.recipient {
query = query.filter(transactions::recipient.eq(recipient));
}
if let Some(transaction_type) = filter.transaction_type {
query = query.filter(transactions::trx.eq(transaction_type));
}

query
.select(Transactions::as_select())
Expand Down
28 changes: 12 additions & 16 deletions nixos/tests/end-to-end.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{ nixosTest
, mkKairosHostConfig
, kairos
, testResources ? ../../kairos-cli/tests/fixtures
, kairos-contracts
, cctlModule
, fetchurl
Expand Down Expand Up @@ -104,11 +103,11 @@ nixosTest {
raise Exception("Success key not found in JSON")
@backoff.on_exception(backoff.expo, Exception, max_tries=5, jitter=backoff.full_jitter)
def wait_for_deposit(depositor, amount):
transactions_result = client.succeed("kairos-cli --kairos-server-address http://kairos fetch --sender {}".format(depositor))
def wait_for_transaction(sender, transaction_type, amount):
transactions_result = client.succeed("kairos-cli --kairos-server-address http://kairos fetch --sender {} --transaction-type {}".format(sender, transaction_type))
transactions = json.loads(transactions_result)
if not any(transaction.get("public_key") == depositor and transaction.get("amount") == str(amount) for transaction in transactions):
raise Exception("Couldn't find deposit for depositor {} with amount {} in transactions\n:{}".format(depositor, amount, transactions))
if not any(transaction.get("public_key") == sender and transaction.get("amount") == str(amount) for transaction in transactions):
raise Exception("Couldn't find {} for sender {} with amount {} in transactions\n:{}".format(transaction_type, sender, amount, transactions))
# Test
start_all()
Expand Down Expand Up @@ -138,7 +137,7 @@ nixosTest {
wait_for_successful_deploy(deposit_deploy_hash)
wait_for_deposit(depositor, 3000000000)
wait_for_transaction(depositor, "deposit", 3000000000)
# transfer
beneficiary = client.succeed("cat ${clientUsersDirectory}/user-3/public_key_hex")
Expand All @@ -150,18 +149,15 @@ nixosTest {
transactions = json.loads(transactions_result)
assert any(transaction.get("recipient") == beneficiary and transaction.get("amount") == str(1000) for transaction in transactions), "Couldn't find the transfer in the L2's DA: {}".format(transactions)
# TODO test withdraw
# withdraw
withdrawer = client.succeed("cat ${clientUsersDirectory}/user-3/public_key_hex")
withdrawer_private_key = "${clientUsersDirectory}/user-3/secret_key.pem"
withdraw_output = client.succeed("kairos-cli --kairos-server-address http://kairos withdraw --amount 800 --private-key {}".format(withdrawer_private_key))
assert "Withdrawal successfully sent to L2\n" in withdraw_output, "The withdraw command was not successful: {}".format(withdraw_output)
# TODO cctl does not provide any secp256k1 keys
# CLI with secp256k1
# cli_output = client.succeed("kairos-cli --kairos-server-address http://kairos deposit --amount 1000 --private-key ${testResources}/secp256k1/secret_key.pem")
# assert "ok\n" in cli_output
wait_for_transaction(withdrawer, "withdrawal", 800)
# cli_output = client.succeed("kairos-cli transfer --recipient '01a26419a7d82b2263deaedea32d35eee8ae1c850bd477f62a82939f06e80df356' --amount 1000 --private-key ${testResources}/secp256k1/secret_key.pem")
# assert "ok\n" in cli_output
# cli_output = client.succeed("kairos-cli withdraw --amount 1000 --private-key ${testResources}/secp256k1/secret_key.pem")
# assert "ok\n" in cli_output
# TODO cctl does not provide any secp256k1 keys, once support is added it should be tested here
'';
}

0 comments on commit b7163b2

Please sign in to comment.