Skip to content

Commit

Permalink
refactor(gsdk): use Into<Option<T>> instead of AsOption (#4045)
Browse files Browse the repository at this point in the history
  • Loading branch information
clearloop authored Jul 7, 2024
1 parent ee798ca commit cc0f995
Show file tree
Hide file tree
Showing 13 changed files with 56 additions and 71 deletions.
8 changes: 6 additions & 2 deletions gcli/tests/cmd/claim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion gcli/tests/cmd/reply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion gcli/tests/cmd/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion gcli/tests/cmd/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion gcli/tests/cmd/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion gclient/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl GearApi {
pub async fn init_with(address: WSAddress, suri: impl AsRef<str>) -> Result<Self> {
let mut suri = suri.as_ref().splitn(2, ':');

Api::new(address.url())
Api::new(address.url().as_str())
.await
.and_then(|api| {
Ok(Self(
Expand Down
12 changes: 6 additions & 6 deletions gsdk/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.

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;
Expand All @@ -37,8 +37,8 @@ pub struct Api {

impl Api {
/// Create new API client.
pub async fn new(url: impl AsOption<str>) -> Result<Self> {
Self::new_with_timeout(url, None).await
pub async fn new(url: impl Into<Option<&str>>) -> Result<Self> {
Self::new_with_timeout(url.into(), None).await
}

/// Gear RPC Client
Expand All @@ -48,8 +48,8 @@ impl Api {

/// Create new API client with timeout.
pub async fn new_with_timeout(
url: impl AsOption<str>,
timeout: impl AsOption<u64>,
url: impl Into<Option<&str>>,
timeout: impl Into<Option<u64>>,
) -> Result<Self> {
let rpc = Rpc::new(url, timeout).await?;

Expand Down
15 changes: 10 additions & 5 deletions gsdk/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use crate::{
config::GearConfig,
result::{Error, Result},
utils::AsOption,
};
use futures_util::{StreamExt, TryStreamExt};
use jsonrpsee::{
Expand Down Expand Up @@ -66,10 +65,13 @@ pub enum RpcClient {

impl RpcClient {
/// Create RPC client from url and timeout.
pub async fn new(url: impl AsOption<str>, timeout: impl AsOption<u64>) -> Result<Self> {
pub async fn new(
url: impl Into<Option<&str>>,
timeout: impl Into<Option<u64>>,
) -> Result<Self> {
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} ...");
Expand Down Expand Up @@ -168,7 +170,10 @@ pub struct Rpc {

impl Rpc {
/// Create RPC client from url and timeout.
pub async fn new(url: impl AsOption<str>, timeout: impl AsOption<u64>) -> Result<Self> {
pub async fn new(
url: impl Into<Option<&str>>,
timeout: impl Into<Option<u64>>,
) -> Result<Self> {
let rpc = SubxtRpcClient::new(RpcClient::new(url, timeout).await?);
let methods = LegacyRpcMethods::new(rpc.clone());
Ok(Self { rpc, methods })
Expand Down
1 change: 0 additions & 1 deletion gsdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
41 changes: 0 additions & 41 deletions gsdk/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,44 +107,3 @@ pub(crate) fn storage_address_bytes<Address: StorageAddress>(
addr.append_entry_bytes(metadata, &mut bytes)?;
Ok(bytes)
}

/// Interface for adapting optional values
pub trait AsOption<T: ?Sized> {
fn as_option(&self) -> Option<&T>;
}

impl AsOption<str> for &str {
fn as_option(&self) -> Option<&str> {
Some(self)
}
}

impl AsOption<str> for Option<&str> {
fn as_option(&self) -> Option<&str> {
self.as_deref()
}
}

impl AsOption<str> for &String {
fn as_option(&self) -> Option<&str> {
Some(self.as_ref())
}
}

impl AsOption<str> for String {
fn as_option(&self) -> Option<&str> {
Some(self)
}
}

impl AsOption<u64> for u64 {
fn as_option(&self) -> Option<&u64> {
Some(self)
}
}

impl AsOption<u64> for Option<u64> {
fn as_option(&self) -> Option<&u64> {
self.as_ref()
}
}
2 changes: 1 addition & 1 deletion gsdk/tests/backtrace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
30 changes: 21 additions & 9 deletions gsdk/tests/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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();

Expand All @@ -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())
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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?;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion utils/node-loader/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ async fn run(params: Params) -> Result<()> {
///
/// Broadcast new events to receivers.
async fn listen_events(tx: Sender<subxt::events::Events<GearConfig>>, 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 {
Expand Down

0 comments on commit cc0f995

Please sign in to comment.