Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace LibplanetConsole.Client.Bank.Commands;

[CommandSummary("Bank Commands.")]
[CommandSummary("Provides bank-related commands for the node.")]
[Category("Bank")]
internal sealed class BankCommand(
IClient client,
Expand All @@ -20,6 +20,7 @@ internal sealed class BankCommand(
public static Address Address { get; set; }

[CommandMethod]
[CommandSummary("Transfers the specified amount to the recipient address.")]
public async Task TransferAsync(
[CommandParameterCompletion(nameof(GetAddresses))]
Address recipientAddress,
Expand All @@ -32,6 +33,8 @@ await bank.TransferAsync(
}

[CommandMethod]
[CommandSummary("Gets the balance of the specified currency for the client or " +
"specified address.")]
[CommandMethodProperty(nameof(Address))]
public async Task BalanceAsync(
[CommandParameterCompletion(nameof(GetCurrencyCodes))]
Expand All @@ -45,6 +48,7 @@ public async Task BalanceAsync(
}

[CommandMethod]
[CommandSummary("Displays information about a specific currency or lists all currency codes.")]
public void Currency(
[CommandParameterCompletion(nameof(GetCurrencyCodes))]
string code = "")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace LibplanetConsole.Console.Bank.Commands;

[CommandSummary("Bank Commands.")]
[CommandSummary("Provides bank-related commands.")]
[Category("Bank")]
internal sealed class BankCommand(
IConsole console,
Expand All @@ -22,6 +22,7 @@ internal sealed class BankCommand(
public static Address Address { get; set; }

[CommandMethod]
[CommandSummary("Transfers the specified amount to the recipient address.")]
public async Task TransferAsync(
[CommandParameterCompletion(nameof(GetAddresses))]
Address recipientAddress,
Expand All @@ -34,6 +35,8 @@ await bank.TransferAsync(
}

[CommandMethod]
[CommandSummary("Gets the balance of the specified currency for the console or " +
"specified address.")]
[CommandMethodProperty(nameof(Address))]
public async Task BalanceAsync(
[CommandParameterCompletion(nameof(GetCurrencyCodes))]
Expand All @@ -47,6 +50,7 @@ public async Task BalanceAsync(
}

[CommandMethod]
[CommandSummary("Displays information about a specific currency or lists all currency codes.")]
public void Currency(
[CommandParameterCompletion(nameof(GetCurrencyCodes))]
string code = "")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace LibplanetConsole.Console.Bank.Commands;

[CommandSummary("Provides bank-related commands for the client.")]
[Category("Bank")]
internal sealed partial class ClientBankCommand(
IServiceProvider serviceProvider,
ClientCommand clientCommand,
Expand All @@ -20,14 +22,15 @@ internal sealed partial class ClientBankCommand(
public static Address Address { get; set; }

[CommandMethod]
[CommandSummary("Transfers the specified amount to the recipient address.")]
[CommandMethodProperty(nameof(ClientAddress))]
public async Task TransferAsync(
[CommandParameterCompletion(nameof(GetAddresses))]
Address recipientAddress,
[FungibleAssetValue] string amount,
CancellationToken cancellationToken = default)
{
var client = GetClientOrCurrent(ClientAddress);
var client = CurrentClient;
var bank = client.GetRequiredKeyedService<IClientBank>(IClient.Key);
var amountValue = currencies.ToFungibleAssetValue(amount);
await bank.TransferAsync(
Expand All @@ -37,14 +40,14 @@ await bank.TransferAsync(
[CommandMethod]
[CommandMethodProperty(nameof(ClientAddress))]
[CommandMethodProperty(nameof(Address))]
[CommandSummary("Display balance of specific address.")]
[Category("Bank")]
[CommandSummary("Gets the balance of the specified currency for the client or " +
"specified address.")]
public async Task BalanceAsync(
[CommandParameterCompletion(nameof(GetCurrencyCodes))]
string currencyCode,
CancellationToken cancellationToken)
{
var client = GetClientOrCurrent(ClientAddress);
var client = CurrentClient;
var address = Address == default ? client.Address : Address;
var bank = client.GetRequiredKeyedService<IClientBank>(IClient.Key);
var currency = currencies[currencyCode];
Expand All @@ -53,6 +56,7 @@ public async Task BalanceAsync(
}

[CommandMethod]
[CommandSummary("Displays information about a specific currency or lists all currency codes.")]
public void Currency(
[CommandParameterCompletion(nameof(GetCurrencyCodes))]
string code = "")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace LibplanetConsole.Console.Bank.Commands;

[CommandSummary("Provides bank-related commands for the node.")]
[Category("Bank")]
internal sealed partial class NodeBankCommand(
IServiceProvider serviceProvider,
NodeCommand nodeCommand,
Expand All @@ -21,13 +23,14 @@ internal sealed partial class NodeBankCommand(

[CommandMethod]
[CommandMethodProperty(nameof(NodeAddress))]
[CommandSummary("Transfers the specified amount to the recipient address.")]
public async Task TransferAsync(
[CommandParameterCompletion(nameof(GetAddresses))]
Address recipientAddress,
[FungibleAssetValue] string amount,
CancellationToken cancellationToken = default)
{
var node = GetNodeOrCurrent(NodeAddress);
var node = CurrentNode;
var bank = node.GetRequiredKeyedService<INodeBank>(INode.Key);
var amountValue = currencies.ToFungibleAssetValue(amount);
await bank.TransferAsync(
Expand All @@ -37,14 +40,14 @@ await bank.TransferAsync(
[CommandMethod]
[CommandMethodProperty(nameof(NodeAddress))]
[CommandMethodProperty(nameof(Address))]
[CommandSummary("Display balance of specific address.")]
[Category("Bank")]
[CommandSummary("Gets the balance of the specified currency for the node or " +
"specified address.")]
public async Task BalanceAsync(
[CommandParameterCompletion(nameof(GetCurrencyCodes))]
string currencyCode,
CancellationToken cancellationToken)
{
var node = GetNodeOrCurrent(NodeAddress);
var node = CurrentNode;
var address = Address == default ? node.Address : Address;
var bank = node.GetRequiredKeyedService<INodeBank>(INode.Key);
var currencyValue = currencies[currencyCode];
Expand All @@ -53,6 +56,7 @@ public async Task BalanceAsync(
}

[CommandMethod]
[CommandSummary("Displays information about a specific currency or lists all currency codes.")]
public void Currency(
[CommandParameterCompletion(nameof(GetCurrencyCodes))]
string code = "")
Expand Down
2 changes: 1 addition & 1 deletion src/console/LibplanetConsole.Console/ClientCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ private void InsertClient(Client client)
var addresses = serviceProvider.GetRequiredService<IAddressCollection>();
if (client.Alias != string.Empty)
{
addresses.Add(client.Alias, client.Address, "client");
addresses.Add(client.Alias, client.Address, IClient.Tag);
}

_clientList.Add(client);
Expand Down
13 changes: 8 additions & 5 deletions src/console/LibplanetConsole.Console/Commands/ClientCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,13 @@ public async Task DetachAsync(CancellationToken cancellationToken = default)
[CommandMethodProperty(nameof(ClientAddress))]
[CommandSummary("Starts the client")]
public async Task StartAsync(
string nodeAddress = "", CancellationToken cancellationToken = default)
[CommandParameterCompletion(nameof(GetNodeAddresses))]
Address nodeAddress = default,
CancellationToken cancellationToken = default)
{
var nodes = _serviceProvider.GetRequiredService<NodeCollection>();
var clientAddress = addresses.ToAddress(ClientAddress);
var node = nodes.GetNodeOrCurrent(nodeAddress, addresses);
var client = clients.GetClientOrCurrent(clientAddress);
var node = nodes.GetNodeOrCurrent(nodeAddress);
var client = GetClientOrCurrent(ClientAddress);
await client.StartAsync(node, cancellationToken);
}

Expand All @@ -98,7 +99,7 @@ public async Task StopAsync(CancellationToken cancellationToken)
public void Current(
[CommandSummary("The address of the client")]
[CommandParameterCompletion(nameof(GetClientAddresses))]
string clientAddress = "")
Address clientAddress = default)
{
var client = GetClientOrDefault(clientAddress);
if (client is not null)
Expand Down Expand Up @@ -156,4 +157,6 @@ void IExecutable.Execute()

return TerminalColorType.BrightBlack;
}

private string[] GetNodeAddresses() => GetAddresses(INode.Tag);
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,23 @@ protected ClientCommandAsyncBase(
_serviceProvider = serviceProvider;
}

[CommandProperty("client", 'C', InitValue = "")]
[CommandProperty("current", 'C')]
[CommandSummary("Specifies the address of the client to use")]
[CommandPropertyCompletion(nameof(GetClientAddresses))]
public string ClientAddress { get; set; } = string.Empty;
public Address ClientAddress { get; set; }

protected IClient Client
{
get
{
var clients = _serviceProvider.GetRequiredService<IClientCollection>();
var addresses = _serviceProvider.GetRequiredService<IAddressCollection>();
var address = addresses.ToAddress(ClientAddress);
return clients.GetClientOrCurrent(address);
return clients.GetClientOrCurrent(ClientAddress);
}
}

protected string[] GetClientAddresses()
{
var clients = _serviceProvider.GetRequiredService<IClientCollection>();
return
[
.. clients.Where(item => item.Alias != string.Empty).Select(item => item.Alias),
.. clients.Select(client => $"{client.Address}"),
];
var addresses = _serviceProvider.GetRequiredService<IAddressCollection>();
return addresses.GetAddresses(IClient.Tag);
}
}
16 changes: 5 additions & 11 deletions src/console/LibplanetConsole.Console/Commands/ClientCommandBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,23 @@ protected ClientCommandBase(
_serviceProvider = serviceProvider;
}

[CommandProperty("client", 'C', InitValue = "")]
[CommandProperty("current", 'C')]
[CommandSummary("Specifies the address of the client to use")]
[CommandPropertyCompletion(nameof(GetClientAddresses))]
public string ClientAddress { get; set; } = string.Empty;
public Address ClientAddress { get; set; }

protected IClient Client
{
get
{
var clients = _serviceProvider.GetRequiredService<IClientCollection>();
var addresses = _serviceProvider.GetRequiredService<IAddressCollection>();
var address = addresses.ToAddress(ClientAddress);
return clients.GetClientOrCurrent(address);
return clients.GetClientOrCurrent(ClientAddress);
}
}

protected string[] GetClientAddresses()
{
var clients = _serviceProvider.GetRequiredService<IClientCollection>();
return
[
.. clients.Where(item => item.Alias != string.Empty).Select(item => item.Alias),
.. clients.Select(client => $"{client.Address}"),
];
var addresses = _serviceProvider.GetRequiredService<IAddressCollection>();
return addresses.GetAddresses(IClient.Tag);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,40 +33,35 @@ protected ClientCommandMethodBase(
_serviceProvider = serviceProvider;
}

[CommandProperty("client", 'C', InitValue = "")]
[CommandProperty("current", 'C')]
[CommandSummary("Specifies the address of the client to use")]
[CommandPropertyCompletion(nameof(GetClientAddresses))]
public string ClientAddress { get; set; } = string.Empty;
public Address ClientAddress { get; set; }

protected IClient GetClientOrCurrent(string clientAddress)
public IClient CurrentClient
{
get
{
var clients = _serviceProvider.GetRequiredService<IClientCollection>();
return clients.GetClientOrCurrent(ClientAddress);
}
}

protected IClient GetClientOrCurrent(Address clientAddress)
{
var clients = _serviceProvider.GetRequiredService<IClientCollection>();
var addresses = _serviceProvider.GetRequiredService<IAddressCollection>();
var address = addresses.ToAddress(clientAddress);
return clients.GetClientOrCurrent(address);
return clients.GetClientOrCurrent(clientAddress);
}

protected IClient? GetClientOrDefault(string clientAddress)
protected IClient? GetClientOrDefault(Address clientAddress)
{
if (clientAddress == string.Empty)
if (clientAddress == default)
{
return default;
}

var clients = _serviceProvider.GetRequiredService<IClientCollection>();
var addresses = _serviceProvider.GetRequiredService<IAddressCollection>();
var address = addresses.ToAddress(clientAddress);
return clients[address];
}

protected string[] GetClientAddresses()
{
var clients = _serviceProvider.GetRequiredService<IClientCollection>();
return
[
.. clients.Where(item => item.Alias != string.Empty).Select(item => item.Alias),
.. clients.Select(client => $"{client.Address}"),
];
return clients[clientAddress];
}

protected Address GetAddress(string address)
Expand All @@ -79,4 +74,12 @@ protected Address GetAddress(string address)
var addresses = _serviceProvider.GetRequiredService<IAddressCollection>();
return addresses.ToAddress(address);
}

protected string[] GetClientAddresses() => GetAddresses(IClient.Tag);

protected string[] GetAddresses(params string[] tags)
{
var addresses = _serviceProvider.GetRequiredService<IAddressCollection>();
return addresses.GetAddresses(tags);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public async Task StopAsync(CancellationToken cancellationToken = default)
public void Current(
[CommandSummary("The address of the node")]
[CommandParameterCompletion(nameof(GetNodeAddresses))]
string nodeAddress = "")
Address nodeAddress = default)
{
var node = GetNodeOrDefault(nodeAddress);
if (node is not null)
Expand Down
Loading
Loading