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

Implement moduleAddress rpc method #796

Merged
merged 6 commits into from
Sep 7, 2023
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
2 changes: 1 addition & 1 deletion module-system/sov-modules-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ where

/// All the methods have a default implementation that can't be invoked (because they take `NonInstantiable` parameter).
/// This allows developers to override only some of the methods in their implementation and safely ignore the others.
pub trait Module {
pub trait Module: Default {
/// Execution context.
type Context: Context;

Expand Down
8 changes: 7 additions & 1 deletion module-system/sov-modules-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,20 @@ pub fn codec(input: TokenStream) -> TokenStream {
/// }
///
/// #[jsonrpsee::proc_macros::rpc(client, server, namespace ="myNamespace")]
/// pub trait MyModuleRpc {
/// pub trait MyModuleRpc<C: Context> {
/// #[method(name = "myMethod")]
/// fn my_method(&self, param: u32) ->RpcResult<u32>;
///
/// #[method(name = "health")]
/// fn health(&self) -> RpcResult<()> {
/// Ok(())
/// }
///
/// #[method(name = "moduleAddress")]
/// fn module_address(&self) -> ::jsonrpsee::core::RpcResult<String> {
/// Ok(<MyModule<C> as ModuleInfo>::address(&<MyModule<C> as ::core::default::Default>::default()).to_string())
/// }
///
/// }
/// ```
///
Expand Down
8 changes: 7 additions & 1 deletion module-system/sov-modules-macros/src/rpc/rpc_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ fn build_rpc_trait(
};

let doc_string = format!("Generated RPC trait for {}", type_name);
let where_clause = &generics.where_clause;
let (_, ty_generics, where_clause) = generics.split_for_impl();

let rpc_output = quote! {
#simplified_impl
Expand All @@ -342,6 +342,12 @@ fn build_rpc_trait(
Ok(())
}

/// Get the address of this module
#[method(name = "moduleAddress")]
fn module_address(&self) -> ::jsonrpsee::core::RpcResult<String> {
Ok(<#type_name #ty_generics as ::sov_modules_api::ModuleInfo>::address(&<#type_name #ty_generics as ::core::default::Default>::default()).to_string())
}

}
};
Ok(rpc_output)
Expand Down
10 changes: 10 additions & 0 deletions module-system/sov-modules-macros/tests/rpc/derive_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,15 @@ fn main() {
assert_eq!(result, ());
}

{
let result =
<RpcStorage<ZkDefaultContext> as TestStructRpcServer<ZkDefaultContext>>::module_address(&r)
.unwrap();
assert_eq!(
result,
"sov1y34qkrqwffa3hmpdzvj0fqc0ahmlgrjf5ltfan9ugt82v5ej6lkshg9ypu"
);
}

println!("All tests passed!");
}