Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[refactoring][wallet cli] - Wallet command refactoring #1882

Merged
merged 2 commits into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
make WalletCommand::execute self instead of &mut self and remov…
…ed unneeded cloning
  • Loading branch information
patrickkuo committed May 18, 2022
commit 768045e5a03dc76d87dae112187deb1e62d6cbbf
4 changes: 2 additions & 2 deletions sui/src/bin/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ async fn try_main() -> Result<(), anyhow::Error> {
);

shell.run_async(&mut out, &mut stderr()).await?;
} else if let Some(mut cmd) = options.cmd {
} else if let Some(cmd) = options.cmd {
cmd.execute(&mut context).await?.print(!options.json);
} else {
ClientOpt::command().print_long_help()?
Expand Down Expand Up @@ -209,7 +209,7 @@ async fn handle_command(
context: &mut WalletContext,
completion_cache: CompletionCache,
) -> Result<bool, anyhow::Error> {
let mut wallet_opts = wallet_opts?;
let wallet_opts = wallet_opts?;
let result = wallet_opts.command.execute(context).await?;

// Update completion cache
Expand Down
70 changes: 29 additions & 41 deletions sui/src/wallet_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ pub struct SimpleTransactionSigner {

impl WalletCommands {
pub async fn execute(
&mut self,
self,
context: &mut WalletContext,
) -> Result<WalletCommandResult, anyhow::Error> {
let ret = Ok(match self {
Expand All @@ -258,13 +258,13 @@ impl WalletCommands {
gas,
gas_budget,
} => {
let sender = context.try_get_object_owner(gas).await?;
let sender = context.try_get_object_owner(&gas).await?;
let sender = sender.unwrap_or(context.active_address()?);

let compiled_modules = build_move_package_to_bytes(Path::new(path), false)?;
let compiled_modules = build_move_package_to_bytes(Path::new(&path), false)?;
let data = context
.gateway
.publish(sender, compiled_modules, *gas, *gas_budget)
.publish(sender, compiled_modules, gas, gas_budget)
.await?;
let signature = context
.keystore
Expand All @@ -282,7 +282,7 @@ impl WalletCommands {

WalletCommands::Object { id } => {
// Fetch the object ref
let object_read = context.gateway.get_object_info(*id).await?;
let object_read = context.gateway.get_object_info(id).await?;
WalletCommandResult::Object(object_read)
}
WalletCommands::Call {
Expand All @@ -307,12 +307,12 @@ impl WalletCommands {
gas,
gas_budget,
} => {
let from = context.get_object_owner(object_id).await?;
let from = context.get_object_owner(&object_id).await?;
let time_start = Instant::now();

let data = context
.gateway
.transfer_coin(from, *object_id, *gas, *gas_budget, *to)
.transfer_coin(from, object_id, gas, gas_budget, to)
.await?;
let signature = context
.keystore
Expand All @@ -339,18 +339,12 @@ impl WalletCommands {
}

WalletCommands::Objects { address } => {
let address = match address {
Some(a) => *a,
None => context.active_address()?,
};
let address = address.unwrap_or(context.active_address()?);
WalletCommandResult::Objects(context.gateway.get_owned_objects(address).await?)
}

WalletCommands::SyncClientState { address } => {
let address = match address {
Some(a) => *a,
None => context.active_address()?,
};
let address = address.unwrap_or(context.active_address()?);
context.gateway.sync_account_state(address).await?;
WalletCommandResult::SyncClientState
}
Expand All @@ -361,10 +355,7 @@ impl WalletCommands {
WalletCommandResult::NewAddress(address)
}
WalletCommands::Gas { address } => {
let address = match address {
Some(a) => *a,
None => context.active_address()?,
};
let address = address.unwrap_or(context.active_address()?);
let coins = context
.gas_objects(address)
.await?
Expand All @@ -380,10 +371,10 @@ impl WalletCommands {
gas,
gas_budget,
} => {
let signer = context.get_object_owner(coin_id).await?;
let signer = context.get_object_owner(&coin_id).await?;
let data = context
.gateway
.split_coin(signer, *coin_id, amounts.clone(), *gas, *gas_budget)
.split_coin(signer, coin_id, amounts, gas, gas_budget)
.await?;
let signature = context
.keystore
Expand All @@ -403,10 +394,10 @@ impl WalletCommands {
gas,
gas_budget,
} => {
let signer = context.get_object_owner(primary_coin).await?;
let signer = context.get_object_owner(&primary_coin).await?;
let data = context
.gateway
.merge_coins(signer, *primary_coin, *coin_to_merge, *gas, *gas_budget)
.merge_coins(signer, primary_coin, coin_to_merge, gas, gas_budget)
.await?;
let signature = context
.keystore
Expand All @@ -423,29 +414,26 @@ impl WalletCommands {
}
WalletCommands::Switch { address, gateway } => {
if let Some(addr) = address {
if !context.config.accounts.contains(addr) {
if !context.config.accounts.contains(&addr) {
return Err(anyhow!("Address {} not managed by wallet", addr));
}
context.config.active_address = Some(*addr);
context.config.active_address = Some(addr);
context.config.save()?;
}

if let Some(gateway) = gateway {
if let Some(gateway) = &gateway {
// TODO: handle embedded gateway
context.config.gateway = GatewayType::RPC(gateway.clone());
context.config.save()?;
}

if Option::is_none(address) && Option::is_none(gateway) {
if Option::is_none(&address) && Option::is_none(&gateway) {
return Err(anyhow!(
"No address or gateway specified. Please Specify one."
));
}

WalletCommandResult::Switch(SwitchResponse {
address: *address,
gateway: gateway.clone(),
})
WalletCommandResult::Switch(SwitchResponse { address, gateway })
}
WalletCommands::ActiveAddress {} => {
WalletCommandResult::ActiveAddress(context.active_address().ok())
Expand All @@ -458,9 +446,9 @@ impl WalletCommands {
gas_budget,
} => {
let args_json = json!([
unwrap_or(name, EXAMPLE_NFT_NAME),
unwrap_or(description, EXAMPLE_NFT_DESCRIPTION),
unwrap_or(url, EXAMPLE_NFT_URL)
unwrap_or(&name, EXAMPLE_NFT_NAME),
unwrap_or(&description, EXAMPLE_NFT_DESCRIPTION),
unwrap_or(&url, EXAMPLE_NFT_URL)
]);
let mut args = vec![];
for a in args_json.as_array().unwrap() {
Expand All @@ -472,8 +460,8 @@ impl WalletCommands {
"mint",
&[],
gas,
&gas_budget.unwrap_or(3000),
&args,
gas_budget.unwrap_or(3000),
args,
context,
)
.await?;
Expand Down Expand Up @@ -698,20 +686,20 @@ async fn call_move(
args: &[SuiJsonValue],
context: &mut WalletContext,
) -> Result<(SuiCertifiedTransaction, SuiTransactionEffects), anyhow::Error> {
let gas_owner = context.try_get_object_owner(gas).await?;
let gas_owner = context.try_get_object_owner(&gas).await?;
let sender = gas_owner.unwrap_or(context.active_address()?);

let data = context
.gateway
.move_call(
sender,
*package,
package,
module.to_string(),
function.to_string(),
type_args.to_owned(),
args.to_vec(),
*gas,
*gas_budget,
gas,
gas_budget,
)
.await?;
let signature = context
Expand All @@ -734,7 +722,7 @@ async fn call_move(
Ok((cert, effects))
}

fn unwrap_or<'a>(val: &'a mut Option<String>, default: &'a str) -> &'a str {
fn unwrap_or<'a>(val: &'a Option<String>, default: &'a str) -> &'a str {
match val {
Some(v) => v,
None => default,
Expand Down