Skip to content

Commit

Permalink
Merge pull request #2320 from dusk-network/moonlight-wallet-support
Browse files Browse the repository at this point in the history
Moonlight wallet support
  • Loading branch information
Daksh14 authored Sep 9, 2024
2 parents da74081 + b9cae97 commit 3bab53c
Show file tree
Hide file tree
Showing 13 changed files with 625 additions and 168 deletions.
128 changes: 96 additions & 32 deletions rusk-wallet/src/bin/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ pub(crate) enum Command {
file: Option<WalletPath>,
},

/// Check your current balance
Balance {
/// Check your current phoenix balance
PhoenixBalance {
/// Address
#[clap(short, long)]
addr: Option<Address>,
Expand All @@ -59,6 +59,13 @@ pub(crate) enum Command {
spendable: bool,
},

/// Check your current moonlight balance
MoonlightBalance {
/// Address
#[clap(short, long)]
addr: Option<Address>,
},

/// List your existing addresses and generate new ones
Addresses {
/// Create new address
Expand All @@ -67,19 +74,42 @@ pub(crate) enum Command {
},

/// Show address transaction history
History {
PhoenixHistory {
/// Address for which you want to see the history
#[clap(short, long)]
addr: Option<Address>,
},

/// Send DUSK through the network
Transfer {
/// Address from which to send DUSK [default: first address]
/// Send DUSK privately through the network using Phoenix
PhoenixTransfer {
/// Phoenix Address from which to send DUSK [default: first address]
#[clap(short, long)]
sndr: Option<Address>,

/// Phoenix Receiver address
#[clap(short, long)]
rcvr: Address,

/// Amount of DUSK to send
#[clap(short, long)]
amt: Dusk,

/// Max amt of gas for this transaction
#[clap(short = 'l', long, default_value_t= DEFAULT_LIMIT)]
gas_limit: u64,

/// Price you're going to pay for each gas unit (in LUX)
#[clap(short = 'p', long, default_value_t= DEFAULT_PRICE)]
gas_price: Lux,
},

/// Send DUSK through the network using moonlight
MoonlightTransfer {
/// Bls Address from which to send DUSK [default: first address]
#[clap(short, long)]
sndr: Option<Address>,

/// Receiver address
/// Bls Receiver address
#[clap(short, long)]
rcvr: Address,

Expand All @@ -96,9 +126,9 @@ pub(crate) enum Command {
gas_price: Lux,
},

/// Start staking DUSK
Stake {
/// Address from which to stake DUSK [default: first address]
/// Start staking DUSK through phoenix
PhoenixStake {
/// Phoenix Address from which to stake DUSK [default: first address]
#[clap(short = 's', long)]
addr: Option<Address>,

Expand Down Expand Up @@ -126,9 +156,10 @@ pub(crate) enum Command {
reward: bool,
},

/// Unstake a key's stake
Unstake {
/// Address from which your DUSK was staked [default: first address]
/// Phoeinx Unstake a key's stake
PhoenixUnstake {
/// Phoenix Address from which your DUSK was staked [default: first
/// address]
#[clap(short, long)]
addr: Option<Address>,

Expand All @@ -141,9 +172,10 @@ pub(crate) enum Command {
gas_price: Lux,
},

/// Withdraw accumulated reward for a stake key
Withdraw {
/// Address from which your DUSK was staked [default: first address]
/// Phoenix Withdraw accumulated reward for a stake key
PhoenixWithdraw {
/// Phoenix Address from which your DUSK was staked [default: first
/// address]
#[clap(short, long)]
addr: Option<Address>,

Expand Down Expand Up @@ -184,7 +216,7 @@ impl Command {
settings: &Settings,
) -> anyhow::Result<RunResult> {
match self {
Command::Balance { addr, spendable } => {
Command::PhoenixBalance { addr, spendable } => {
let sync_result = wallet.sync().await;
if let Err(e) = sync_result {
// Sync error should be reported only if wallet is online
Expand All @@ -198,8 +230,18 @@ impl Command {
None => wallet.default_address(),
};

let balance = wallet.get_balance(addr).await?;
Ok(RunResult::Balance(balance, spendable))
let balance = wallet.get_phoenix_balance(addr).await?;
Ok(RunResult::PhoenixBalance(balance, spendable))
}
Command::MoonlightBalance { addr } => {
let addr = match addr {
Some(addr) => wallet.claim_as_address(addr)?,
None => wallet.default_address(),
};

Ok(RunResult::MoonlightBalance(
wallet.get_moonlight_balance(addr)?,
))
}
Command::Addresses { new } => {
if new {
Expand All @@ -217,7 +259,7 @@ impl Command {
Ok(RunResult::Addresses(wallet.addresses().clone()))
}
}
Command::Transfer {
Command::PhoenixTransfer {
sndr,
rcvr,
amt,
Expand All @@ -235,18 +277,36 @@ impl Command {
wallet.phoenix_transfer(sender, &rcvr, amt, gas).await?;
Ok(RunResult::Tx(tx.hash()))
}
Command::Stake {
Command::MoonlightTransfer {
sndr,
rcvr,
amt,
gas_limit,
gas_price,
} => {
let gas = Gas::new(gas_limit).with_price(gas_price);
let sender = match sndr {
Some(addr) => wallet.claim_as_address(addr)?,
None => wallet.default_address(),
};

let tx =
wallet.moonlight_transfer(sender, &rcvr, amt, gas).await?;

Ok(RunResult::Tx(tx.hash()))
}
Command::PhoenixStake {
addr,
amt,
gas_limit,
gas_price,
} => {
wallet.sync().await?;
let gas = Gas::new(gas_limit).with_price(gas_price);
let addr = match addr {
Some(addr) => wallet.claim_as_address(addr)?,
None => wallet.default_address(),
};
let gas = Gas::new(gas_limit).with_price(gas_price);

let tx = wallet.phoenix_stake(addr, amt, gas).await?;
Ok(RunResult::Tx(tx.hash()))
Expand All @@ -257,13 +317,13 @@ impl Command {
None => wallet.default_address(),
};
let si = wallet
.stake_info(addr.index)
.stake_info(addr.index()?)
.await?
.ok_or(Error::NotStaked)?;

Ok(RunResult::StakeInfo(si, reward))
}
Command::Unstake {
Command::PhoenixUnstake {
addr,
gas_limit,
gas_price,
Expand All @@ -279,7 +339,7 @@ impl Command {
let tx = wallet.phoenix_unstake(addr, gas).await?;
Ok(RunResult::Tx(tx.hash()))
}
Command::Withdraw {
Command::PhoenixWithdraw {
addr,
gas_limit,
gas_price,
Expand Down Expand Up @@ -312,7 +372,7 @@ impl Command {

Ok(RunResult::ExportedKeys(pub_key, key_pair))
}
Command::History { addr } => {
Command::PhoenixHistory { addr } => {
wallet.sync().await?;
let addr = match addr {
Some(addr) => wallet.claim_as_address(addr)?,
Expand All @@ -323,7 +383,7 @@ impl Command {
let transactions =
history::transaction_from_notes(settings, notes).await?;

Ok(RunResult::History(transactions))
Ok(RunResult::PhoenixHistory(transactions))
}
Command::Create { .. } => Ok(RunResult::Create()),
Command::Restore { .. } => Ok(RunResult::Restore()),
Expand All @@ -335,29 +395,33 @@ impl Command {
/// Possible results of running a command in interactive mode
pub enum RunResult {
Tx(BlsScalar),
Balance(BalanceInfo, bool),
PhoenixBalance(BalanceInfo, bool),
MoonlightBalance(Dusk),
StakeInfo(StakeData, bool),
Address(Box<Address>),
Addresses(Vec<Address>),
ExportedKeys(PathBuf, PathBuf),
Create(),
Restore(),
Settings(),
History(Vec<TransactionHistory>),
PhoenixHistory(Vec<TransactionHistory>),
}

impl fmt::Display for RunResult {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use RunResult::*;
match self {
Balance(balance, _) => {
PhoenixBalance(balance, _) => {
write!(
f,
"> Total balance is: {} DUSK\n> Maximum spendable per TX is: {} DUSK",
"> Total Phoenix balance is: {} DUSK\n> Maximum spendable per TX is: {} DUSK",
Dusk::from(balance.value),
Dusk::from(balance.spendable)
)
}
MoonlightBalance(balance) => {
write!(f, "> Total Moonlight balance is: {} DUSK", balance)
}
Address(addr) => {
write!(f, "> {}", addr)
}
Expand Down Expand Up @@ -398,7 +462,7 @@ impl fmt::Display for RunResult {
kp.display()
)
}
History(transactions) => {
PhoenixHistory(transactions) => {
writeln!(f, "{}", TransactionHistory::header())?;
for th in transactions {
writeln!(f, "{th}")?;
Expand Down
Loading

0 comments on commit 3bab53c

Please sign in to comment.