Skip to content

Commit

Permalink
refactor: move command, and e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcin Nowak-Liebiediew committed Jan 18, 2024
1 parent 5826b8b commit 0369c9d
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 45 deletions.
42 changes: 41 additions & 1 deletion e2e/tests-dfx/cycles-ledger.bash
Original file line number Diff line number Diff line change
Expand Up @@ -544,4 +544,44 @@ current_time_nanoseconds() {
assert_command dfx canister delete "${FRONTEND_ID}"
assert_command dfx cycles balance
assert_eq "22.379 TC (trillion cycles)."
}
}

@test "redeem-faucet-coupon can set a new wallet and top up an existing one" {
dfx_new hello
install_asset faucet
dfx deploy
dfx ledger fabricate-cycles --canister faucet --t 1000

dfx identity new --storage-mode plaintext faucet_testing
dfx identity use faucet_testing

# prepare wallet to hand out
dfx wallet balance # this creates a new wallet with user faucet_testing as controller
dfx canister call faucet set_wallet_to_hand_out "(principal \"$(dfx identity get-wallet)\")" # register the wallet as the wallet that the faucet will return
rm "$E2E_SHARED_LOCAL_NETWORK_DATA_DIRECTORY/wallets.json" # forget about the currently configured wallet

# assert: no wallet configured
export DFX_DISABLE_AUTO_WALLET=1
assert_command_fail dfx wallet balance
assert_match "No wallet configured"

assert_command dfx cycles redeem-faucet-coupon --faucet "$(dfx canister id faucet)" 'valid-coupon'
assert_match "Redeemed coupon valid-coupon for a new wallet"
assert_match "New wallet set."

# only succeeds if wallet is correctly set
assert_command dfx wallet balance
# checking only balance before the dot, rest may fluctuate
# balance may be 99.??? TC if cycles accounting is done, or 100.000 TC if not
assert_match "99\.|100\."

unset DFX_DISABLE_AUTO_WALLET

assert_command dfx cycles redeem-faucet-coupon --faucet "$(dfx canister id faucet)" 'another-valid-coupon'
assert_match "Redeemed coupon code another-valid-coupon for 10.000 TC"

assert_command dfx wallet balance
# checking only balance before the dot, rest may fluctuate
# balance may be 109.??? TC if cycles accounting is done, or 110.000 TC if not
assert_match "109\.|110\."
}
40 changes: 0 additions & 40 deletions e2e/tests-dfx/wallet.bash
Original file line number Diff line number Diff line change
Expand Up @@ -184,43 +184,3 @@ teardown() {
assert_match "There is no wallet defined for identity 'default' on network 'local'. Nothing to do."
}

@test "redeem-faucet-coupon can set a new wallet and top up an existing one" {
dfx_new hello
dfx_start
install_asset faucet
dfx deploy
dfx ledger fabricate-cycles --canister faucet --t 1000

dfx identity new --storage-mode plaintext faucet_testing
dfx identity use faucet_testing

# prepare wallet to hand out
dfx wallet balance # this creates a new wallet with user faucet_testing as controller
dfx canister call faucet set_wallet_to_hand_out "(principal \"$(dfx identity get-wallet)\")" # register the wallet as the wallet that the faucet will return
rm "$E2E_SHARED_LOCAL_NETWORK_DATA_DIRECTORY/wallets.json" # forget about the currently configured wallet

# assert: no wallet configured
export DFX_DISABLE_AUTO_WALLET=1
assert_command_fail dfx wallet balance
assert_match "No wallet configured"

assert_command dfx wallet redeem-faucet-coupon --faucet "$(dfx canister id faucet)" 'valid-coupon'
assert_match "Redeemed coupon valid-coupon for a new wallet"
assert_match "New wallet set."

# only succeeds if wallet is correctly set
assert_command dfx wallet balance
# checking only balance before the dot, rest may fluctuate
# balance may be 99.??? TC if cycles accounting is done, or 100.000 TC if not
assert_match "99\.|100\."

unset DFX_DISABLE_AUTO_WALLET

assert_command dfx wallet redeem-faucet-coupon --faucet "$(dfx canister id faucet)" 'another-valid-coupon'
assert_match "Redeemed coupon code another-valid-coupon for 10.000 TC"

assert_command dfx wallet balance
# checking only balance before the dot, rest may fluctuate
# balance may be 109.??? TC if cycles accounting is done, or 110.000 TC if not
assert_match "109\.|110\."
}
3 changes: 3 additions & 0 deletions src/dfx/src/commands/cycles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use clap::Parser;
use tokio::runtime::Runtime;

mod balance;
mod redeem_faucet_coupon;
pub mod top_up;
mod transfer;

Expand All @@ -25,6 +26,7 @@ enum SubCommand {
Balance(balance::CyclesBalanceOpts),
TopUp(top_up::TopUpOpts),
Transfer(transfer::TransferOpts),
RedeemFaucetCoupon(redeem_faucet_coupon::RedeemFaucetCouponOpts),
}

pub fn exec(env: &dyn Environment, opts: CyclesOpts) -> DfxResult {
Expand All @@ -35,6 +37,7 @@ pub fn exec(env: &dyn Environment, opts: CyclesOpts) -> DfxResult {
SubCommand::Balance(v) => balance::exec(&agent_env, v).await,
SubCommand::TopUp(v) => top_up::exec(&agent_env, v).await,
SubCommand::Transfer(v) => transfer::exec(&agent_env, v).await,
SubCommand::RedeemFaucetCoupon(v) => redeem_faucet_coupon::exec(&agent_env, v).await,
}
})
}
5 changes: 1 addition & 4 deletions src/dfx/src/commands/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ mod custodians;
mod deauthorize;
mod list_addresses;
mod name;
mod redeem_faucet_coupon;
mod remove_controller;
mod send;
mod set_name;
Expand All @@ -48,7 +47,6 @@ enum SubCommand {
Custodians(custodians::CustodiansOpts),
Deauthorize(deauthorize::DeauthorizeOpts),
Name(name::NameOpts),
RedeemFaucetCoupon(redeem_faucet_coupon::RedeemFaucetCouponOpts),
RemoveController(remove_controller::RemoveControllerOpts),
Send(send::SendOpts),
SetName(set_name::SetNameOpts),
Expand All @@ -68,7 +66,6 @@ pub fn exec(env: &dyn Environment, opts: WalletOpts) -> DfxResult {
SubCommand::Custodians(v) => custodians::exec(&agent_env, v).await,
SubCommand::Deauthorize(v) => deauthorize::exec(&agent_env, v).await,
SubCommand::Name(v) => name::exec(&agent_env, v).await,
SubCommand::RedeemFaucetCoupon(v) => redeem_faucet_coupon::exec(&agent_env, v).await,
SubCommand::RemoveController(v) => remove_controller::exec(&agent_env, v).await,
SubCommand::Send(v) => send::exec(&agent_env, v).await,
SubCommand::SetName(v) => set_name::exec(&agent_env, v).await,
Expand Down Expand Up @@ -118,7 +115,7 @@ where
}

#[context("Failed to setup wallet caller.")]
async fn get_wallet(env: &dyn Environment) -> DfxResult<WalletCanister<'_>> {
pub(crate) async fn get_wallet(env: &dyn Environment) -> DfxResult<WalletCanister<'_>> {
let identity_name = env
.get_selected_identity()
.expect("No selected identity.")
Expand Down

0 comments on commit 0369c9d

Please sign in to comment.