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

feat(new-RPC): Add get_wallet RPC for Retrieving Active Wallet #2198

Closed
wants to merge 1 commit into from

Conversation

CharlVS
Copy link
Member

@CharlVS CharlVS commented Aug 26, 2024

Description: Add get_wallet RPC Method for Retrieving Active Wallet Information

This PR introduces the get_wallet RPC method, which allows clients to retrieve information about the currently active wallet. While the initial implementation returns the wallet name, the design is extensible to support additional wallet properties in future updates.

Key Features:

  • get_wallet_rpc Method: Retrieves the active wallet's name and can be expanded to return more wallet-specific data.
  • Flexible Request Handling: The GetWalletRequest struct is currently empty but prepared for future request parameters.
  • Extensible Response Structure: The GetWalletResponse struct currently includes the wallet name but is designed to be expanded for future needs.

- Implemented the `get_wallet` function to retrieve the active wallet from the context (Currently only includes wallet_name).
- Introduced a `GetWalletRequest` struct for future extensibility with request parameters.
- Updated the dispatcher to handle the `get_wallet` RPC method correctly, including parameter handling.
@CharlVS CharlVS changed the title feat(new-RPC): Add get_wallet RPC Method for Retrieving Active Wallet feat(new-RPC): Add get_wallet RPC for Retrieving Active Wallet Aug 26, 2024
Copy link
Member

@laruh laruh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, for the PR. did a brief review

Comment on lines +550 to +560
/// # Parameters
///
/// - `ctx`: The context.
/// - `req`: The request structure (optional).
///
/// # Returns
///
/// A `Result` type containing:
///
/// * `Ok(GetWalletResponse)` - The wallet name.
/// * `MmError<GetWalletError>` - Error indicating the wallet name is not initialized.
Copy link
Member

@laruh laruh Aug 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is no need to add params and return sections in doc comment for get_wallet_rpc function.

we prefer to add doc comms right above the params and return types: GetWalletRequest, GetWalletResponse, GetWalletError

so you can remove Parameters and Returns and Examples :)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess @CharlVS was following the get_mnemonic_rpc example, I was trying different things back then when I implemented it. I wanted also to have request/response examples in docs e.g.

/// `GetMnemonicRequest` is a struct representing a request to get a mnemonic.
///
/// It contains a single field, `mnemonic_format`, which is an instance of the `MnemonicFormat` enum.
/// The `#[serde(flatten)]` attribute is used so that the fields of the `MnemonicFormat` enum are included
/// directly in the `GetMnemonicRequest` when it is deserialized, rather than nested under a
/// `mnemonic_format` field.
///
/// # Examples
///
/// For a `GetMnemonicRequest` where the `MnemonicFormat` is `Encrypted`, the JSON representation would be:
/// ```json
/// {
/// "format": "encrypted"
/// }
/// ```
///
/// For a `GetMnemonicRequest` where the `MnemonicFormat` is `PlainText` with a password of "password123", the JSON representation would be:
/// ```json
/// {
/// "format": "plaintext",
/// "password": "password123"
/// }
/// ```
#[derive(Debug, Deserialize)]
pub struct GetMnemonicRequest {
#[serde(flatten)]
pub mnemonic_format: MnemonicFormat,
}
But I guess API docs will be moved from docs repo to KDF and we won't need this. Thanks for trying to keep docs consistent @laruh we need to have a consistent way of writing docs across all modules. I am working on this implementation in a different branch anyway and will be opening another PR this week, just wanted to clarify this point here :)

/// The wallet name has not been initialized in the context.
#[display(fmt = "Wallet name is not initialized")]
WalletNameNotInitialized,

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should remove empty line

Comment on lines +573 to +578
pub async fn get_wallet_rpc(
ctx: MmArc,
req: Option<GetWalletRequest>, // Allowing `None` for when `params` is absent
) -> MmResult<GetWalletResponse, GetWalletError> {
// If request is None (params missing), use the default value
let _req = req.unwrap_or_default();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can leave param like this, no need to define new variable if you dont use it.

pub async fn get_wallet_rpc(
    ctx: MmArc,
    _req: Option<GetWalletRequest>, // Just pass it through
) -> MmResult<GetWalletResponse, GetWalletError> {
    let wallet_name = ctx
        .wallet_name
        .clone_or(None)
        .ok_or(GetWalletError::WalletNameNotInitialized)?;

    Ok(GetWalletResponse { wallet_name })
}

@@ -190,6 +191,7 @@ async fn dispatcher_v2(request: MmRpcRequest, ctx: MmArc) -> DispatcherResult<Re
"get_raw_transaction" => handle_mmrpc(ctx, request, get_raw_transaction).await,
"get_shared_db_id" => handle_mmrpc(ctx, request, get_shared_db_id).await,
"get_staking_infos" => handle_mmrpc(ctx, request, get_staking_infos).await,
"get_wallet" => handle_mmrpc(ctx, request, get_wallet_rpc).await,
Copy link
Member

@laruh laruh Aug 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the purpose is to get wallet info, I suggest to use a clearer RPC name, eg get_wallet_info.

Description: Add get_wallet RPC Method for Retrieving Active Wallet Information

same for GetWalletError and GetWalletRequest types. You can have GetWalletInfoError and GetWalletInfoReq

@shamardy
Copy link
Collaborator

Superseded by #2202

@shamardy shamardy closed this Aug 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants