From cc0f9955184129e711d94b11dc1caaa4b947dc59 Mon Sep 17 00:00:00 2001 From: clearloop Date: Sun, 7 Jul 2024 22:47:49 +0800 Subject: [PATCH] refactor(gsdk): use `Into>` instead of `AsOption` (#4045) --- gcli/tests/cmd/claim.rs | 8 +++++-- gcli/tests/cmd/reply.rs | 4 +++- gcli/tests/cmd/send.rs | 4 +++- gcli/tests/cmd/transfer.rs | 2 +- gcli/tests/cmd/upload.rs | 4 +++- gclient/src/api/mod.rs | 2 +- gsdk/src/api.rs | 12 +++++----- gsdk/src/client.rs | 15 ++++++++----- gsdk/src/lib.rs | 1 - gsdk/src/utils.rs | 41 ----------------------------------- gsdk/tests/backtrace.rs | 2 +- gsdk/tests/rpc.rs | 30 +++++++++++++++++-------- utils/node-loader/src/main.rs | 2 +- 13 files changed, 56 insertions(+), 71 deletions(-) diff --git a/gcli/tests/cmd/claim.rs b/gcli/tests/cmd/claim.rs index cb5924e78f1..0ef3092e3e6 100644 --- a/gcli/tests/cmd/claim.rs +++ b/gcli/tests/cmd/claim.rs @@ -33,7 +33,9 @@ async fn test_command_claim_works() -> Result<()> { let node = common::dev()?; // Get balance of the testing address - let signer = Api::new(node.ws()).await?.signer("//Alice//stash", None)?; + let signer = Api::new(node.ws().as_str()) + .await? + .signer("//Alice//stash", None)?; ( signer.api().get_balance(ADDRESS).await.unwrap_or(0), signer @@ -54,7 +56,9 @@ async fn test_command_claim_works() -> Result<()> { initial_balance -= ED; // Check the mailbox of the testing account - let signer = Api::new(node.ws()).await?.signer("//Alice//stash", None)?; + let signer = Api::new(node.ws().as_str()) + .await? + .signer("//Alice//stash", None)?; let mailbox = signer .api() .mailbox(Some(common::alice_account_id()), 10) diff --git a/gcli/tests/cmd/reply.rs b/gcli/tests/cmd/reply.rs index 65ae0935024..e2585e15a65 100644 --- a/gcli/tests/cmd/reply.rs +++ b/gcli/tests/cmd/reply.rs @@ -26,7 +26,9 @@ async fn test_command_reply_works() -> Result<()> { let node = common::create_messager().await?; // Get balance of the testing address - let signer = Api::new(node.ws()).await?.signer("//Alice", None)?; + let signer = Api::new(node.ws().as_str()) + .await? + .signer("//Alice", None)?; let mailbox = signer .api() .mailbox(Some(common::alice_account_id()), 10) diff --git a/gcli/tests/cmd/send.rs b/gcli/tests/cmd/send.rs index 9ffff2ae260..aff166e8a8c 100644 --- a/gcli/tests/cmd/send.rs +++ b/gcli/tests/cmd/send.rs @@ -26,7 +26,9 @@ async fn test_command_send_works() -> Result<()> { let node = common::create_messager().await?; // Get balance of the testing address - let signer = Api::new(node.ws()).await?.signer("//Alice", None)?; + let signer = Api::new(node.ws().as_str()) + .await? + .signer("//Alice", None)?; let mailbox = signer .api() .mailbox(Some(common::alice_account_id()), 10) diff --git a/gcli/tests/cmd/transfer.rs b/gcli/tests/cmd/transfer.rs index f2d7ca1ef90..8118e6df7e6 100644 --- a/gcli/tests/cmd/transfer.rs +++ b/gcli/tests/cmd/transfer.rs @@ -30,7 +30,7 @@ async fn test_command_transfer_works() -> Result<()> { let node = common::dev()?; // Get balance of the testing address - let signer = Api::new(node.ws()).await?.signer(SURI, None)?; + let signer = Api::new(node.ws().as_str()).await?.signer(SURI, None)?; let before = signer.api().get_balance(ADDRESS).await.unwrap_or(0); // Run command transfer diff --git a/gcli/tests/cmd/upload.rs b/gcli/tests/cmd/upload.rs index c4344d55d13..611c04959cb 100644 --- a/gcli/tests/cmd/upload.rs +++ b/gcli/tests/cmd/upload.rs @@ -28,7 +28,9 @@ use gsdk::Api; #[tokio::test] async fn test_command_upload_works() -> Result<()> { let node = common::dev()?; - let signer = Api::new(node.ws()).await?.signer("//Alice", None)?; + let signer = Api::new(node.ws().as_str()) + .await? + .signer("//Alice", None)?; let code_id = CodeId::generate(demo_new_meta::WASM_BINARY); assert!( signer.api().code_storage(code_id).await.is_err(), diff --git a/gclient/src/api/mod.rs b/gclient/src/api/mod.rs index 879e004a4b3..e07cbb26590 100644 --- a/gclient/src/api/mod.rs +++ b/gclient/src/api/mod.rs @@ -52,7 +52,7 @@ impl GearApi { pub async fn init_with(address: WSAddress, suri: impl AsRef) -> Result { let mut suri = suri.as_ref().splitn(2, ':'); - Api::new(address.url()) + Api::new(address.url().as_str()) .await .and_then(|api| { Ok(Self( diff --git a/gsdk/src/api.rs b/gsdk/src/api.rs index 9d7c3c60ec6..c1ffba0c6c1 100644 --- a/gsdk/src/api.rs +++ b/gsdk/src/api.rs @@ -17,8 +17,8 @@ // along with this program. If not, see . use crate::{ - client::Rpc, config::GearConfig, metadata::Event, signer::Signer, AsOption, Blocks, Events, - Result, TxInBlock, + client::Rpc, config::GearConfig, metadata::Event, signer::Signer, Blocks, Events, Result, + TxInBlock, }; use core::ops::{Deref, DerefMut}; use std::result::Result as StdResult; @@ -37,8 +37,8 @@ pub struct Api { impl Api { /// Create new API client. - pub async fn new(url: impl AsOption) -> Result { - Self::new_with_timeout(url, None).await + pub async fn new(url: impl Into>) -> Result { + Self::new_with_timeout(url.into(), None).await } /// Gear RPC Client @@ -48,8 +48,8 @@ impl Api { /// Create new API client with timeout. pub async fn new_with_timeout( - url: impl AsOption, - timeout: impl AsOption, + url: impl Into>, + timeout: impl Into>, ) -> Result { let rpc = Rpc::new(url, timeout).await?; diff --git a/gsdk/src/client.rs b/gsdk/src/client.rs index d9748dc4f32..993c7823589 100644 --- a/gsdk/src/client.rs +++ b/gsdk/src/client.rs @@ -20,7 +20,6 @@ use crate::{ config::GearConfig, result::{Error, Result}, - utils::AsOption, }; use futures_util::{StreamExt, TryStreamExt}; use jsonrpsee::{ @@ -66,10 +65,13 @@ pub enum RpcClient { impl RpcClient { /// Create RPC client from url and timeout. - pub async fn new(url: impl AsOption, timeout: impl AsOption) -> Result { + pub async fn new( + url: impl Into>, + timeout: impl Into>, + ) -> Result { let (url, timeout) = ( - url.as_option().unwrap_or(DEFAULT_GEAR_ENDPOINT), - *timeout.as_option().unwrap_or(&DEFAULT_TIMEOUT), + url.into().unwrap_or(DEFAULT_GEAR_ENDPOINT), + timeout.into().unwrap_or(DEFAULT_TIMEOUT), ); log::info!("Connecting to {url} ..."); @@ -168,7 +170,10 @@ pub struct Rpc { impl Rpc { /// Create RPC client from url and timeout. - pub async fn new(url: impl AsOption, timeout: impl AsOption) -> Result { + pub async fn new( + url: impl Into>, + timeout: impl Into>, + ) -> Result { let rpc = SubxtRpcClient::new(RpcClient::new(url, timeout).await?); let methods = LegacyRpcMethods::new(rpc.clone()); Ok(Self { rpc, methods }) diff --git a/gsdk/src/lib.rs b/gsdk/src/lib.rs index 8c288559ebb..b8e32a68663 100644 --- a/gsdk/src/lib.rs +++ b/gsdk/src/lib.rs @@ -27,7 +27,6 @@ pub use crate::{ result::{Error, Result}, signer::PairSigner, subscription::{Blocks, Events}, - utils::AsOption, }; pub use gear_core::gas::GasInfo; pub use subxt::dynamic::Value; diff --git a/gsdk/src/utils.rs b/gsdk/src/utils.rs index bfcee5d84b3..71cef3bcb76 100644 --- a/gsdk/src/utils.rs +++ b/gsdk/src/utils.rs @@ -107,44 +107,3 @@ pub(crate) fn storage_address_bytes( addr.append_entry_bytes(metadata, &mut bytes)?; Ok(bytes) } - -/// Interface for adapting optional values -pub trait AsOption { - fn as_option(&self) -> Option<&T>; -} - -impl AsOption for &str { - fn as_option(&self) -> Option<&str> { - Some(self) - } -} - -impl AsOption for Option<&str> { - fn as_option(&self) -> Option<&str> { - self.as_deref() - } -} - -impl AsOption for &String { - fn as_option(&self) -> Option<&str> { - Some(self.as_ref()) - } -} - -impl AsOption for String { - fn as_option(&self) -> Option<&str> { - Some(self) - } -} - -impl AsOption for u64 { - fn as_option(&self) -> Option<&u64> { - Some(self) - } -} - -impl AsOption for Option { - fn as_option(&self) -> Option<&u64> { - self.as_ref() - } -} diff --git a/gsdk/tests/backtrace.rs b/gsdk/tests/backtrace.rs index 16bcfaae751..8f912a2c4ee 100644 --- a/gsdk/tests/backtrace.rs +++ b/gsdk/tests/backtrace.rs @@ -24,7 +24,7 @@ mod utils; #[tokio::test] async fn transfer_backtrace() -> Result<()> { let node = dev_node(); - let api = Api::new(node.ws()).await?; + let api = Api::new(node.ws().as_str()).await?; let signer = Signer::new(api, "//Alice", None)?; let alice: [u8; 32] = *alice_account_id().as_ref(); diff --git a/gsdk/tests/rpc.rs b/gsdk/tests/rpc.rs index b97d8b1cf07..941f614bbdf 100644 --- a/gsdk/tests/rpc.rs +++ b/gsdk/tests/rpc.rs @@ -35,7 +35,7 @@ mod utils; #[tokio::test] async fn pallet_errors_formatting() -> Result<()> { let node = dev_node(); - let api = Api::new(node.ws()).await?; + let api = Api::new(node.ws().as_str()).await?; let err = api .calculate_upload_gas( @@ -65,7 +65,7 @@ async fn pallet_errors_formatting() -> Result<()> { #[tokio::test] async fn test_calculate_upload_gas() -> Result<()> { let node = dev_node(); - let api = Api::new(node.ws()).await?; + let api = Api::new(node.ws().as_str()).await?; let alice: [u8; 32] = *alice_account_id().as_ref(); @@ -87,7 +87,9 @@ async fn test_calculate_create_gas() -> Result<()> { let node = dev_node(); // 1. upload code. - let signer = Api::new(node.ws()).await?.signer("//Alice", None)?; + let signer = Api::new(node.ws().as_str()) + .await? + .signer("//Alice", None)?; signer .calls .upload_code(demo_messenger::WASM_BINARY.to_vec()) @@ -116,7 +118,9 @@ async fn test_calculate_handle_gas() -> Result<()> { let pid = ProgramId::generate_from_user(CodeId::generate(demo_messenger::WASM_BINARY), &salt); // 1. upload program. - let signer = Api::new(node.ws()).await?.signer("//Alice", None)?; + let signer = Api::new(node.ws().as_str()) + .await? + .signer("//Alice", None)?; signer .calls @@ -160,7 +164,9 @@ async fn test_calculate_reply_gas() -> Result<()> { let payload = demo_waiter::Command::SendUpTo(alice, 10); // 1. upload program. - let signer = Api::new(node.ws()).await?.signer("//Alice", None)?; + let signer = Api::new(node.ws().as_str()) + .await? + .signer("//Alice", None)?; signer .calls .upload_program( @@ -242,7 +248,7 @@ async fn test_runtime_wasm_blob_version() -> Result<()> { assert_ne!(git_commit_hash, "unknown"); let node = dev_node(); - let api = Api::new(node.ws()).await?; + let api = Api::new(node.ws().as_str()).await?; let mut finalized_blocks = api.subscribe_finalized_blocks().await?; let wasm_blob_version_1 = api.runtime_wasm_blob_version(None).await?; @@ -303,7 +309,9 @@ async fn test_original_code_storage() -> Result<()> { let salt = vec![]; let pid = ProgramId::generate_from_user(CodeId::generate(demo_messenger::WASM_BINARY), &salt); - let signer = Api::new(node.ws()).await?.signer("//Alice", None)?; + let signer = Api::new(node.ws().as_str()) + .await? + .signer("//Alice", None)?; signer .calls @@ -359,7 +367,9 @@ async fn test_calculate_reply_for_handle() -> Result<()> { let pid = ProgramId::generate_from_user(CodeId::generate(demo_new_meta::WASM_BINARY), &salt); // 1. upload program. - let signer = Api::new(node.ws()).await?.signer("//Alice", None)?; + let signer = Api::new(node.ws().as_str()) + .await? + .signer("//Alice", None)?; signer .calls @@ -418,7 +428,9 @@ async fn test_calculate_reply_for_handle_does_not_change_state() -> Result<()> { let pid = ProgramId::generate_from_user(CodeId::generate(demo_vec::WASM_BINARY), &salt); // 1. upload program. - let signer = Api::new(node.ws()).await?.signer("//Alice", None)?; + let signer = Api::new(node.ws().as_str()) + .await? + .signer("//Alice", None)?; signer .calls diff --git a/utils/node-loader/src/main.rs b/utils/node-loader/src/main.rs index f63a6c075a9..ecd91d02bcc 100644 --- a/utils/node-loader/src/main.rs +++ b/utils/node-loader/src/main.rs @@ -37,7 +37,7 @@ async fn run(params: Params) -> Result<()> { /// /// Broadcast new events to receivers. async fn listen_events(tx: Sender>, node: String) -> Result<()> { - let api = gsdk::Api::new(node).await?; + let api = gsdk::Api::new(node.as_str()).await?; let mut event_listener = api.subscribe_finalized_blocks().await?; while let Some(events) = event_listener.next_events().await {