mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-13 17:31:05 +00:00
Merge branch 'master' into aj/update-substrate-crates
This commit is contained in:
+9
-5
@@ -13,7 +13,7 @@ description = "Submit extrinsics (transactions) to a substrate node via RPC"
|
||||
keywords = ["parity", "substrate", "blockchain"]
|
||||
|
||||
[features]
|
||||
default = ["jsonrpsee"]
|
||||
default = ["jsonrpsee-ws"]
|
||||
|
||||
# Activate this to expose functionality only used for integration testing.
|
||||
# The exposed functionality is subject to breaking changes at any point,
|
||||
@@ -22,7 +22,8 @@ integration-tests = []
|
||||
|
||||
# Jsonrpsee if the default RPC provider used in Subxt. However, it can be
|
||||
# swapped out for an alternative implementation, and so is optional.
|
||||
jsonrpsee = ["dep:jsonrpsee"]
|
||||
jsonrpsee-ws = ["jsonrpsee/async-client", "jsonrpsee/client-ws-transport"]
|
||||
jsonrpsee-web = ["jsonrpsee/async-wasm-client", "jsonrpsee/client-web-transport"]
|
||||
|
||||
[dependencies]
|
||||
bitvec = { version = "1.0.0", default-features = false, features = ["alloc"] }
|
||||
@@ -30,11 +31,11 @@ codec = { package = "parity-scale-codec", version = "3.0.0", default-features =
|
||||
scale-info = { version = "2.0.0", features = ["bit-vec"] }
|
||||
scale-value = "0.6.0"
|
||||
scale-decode = "0.4.0"
|
||||
futures = "0.3.13"
|
||||
futures = { version = "0.3.13", default-features = false }
|
||||
hex = "0.4.3"
|
||||
jsonrpsee = { version = "0.15.1", features = ["async-client", "client-ws-transport", "jsonrpsee-types"], optional = true }
|
||||
jsonrpsee = { version = "0.16", optional = true, features = ["jsonrpsee-types"] }
|
||||
serde = { version = "1.0.124", features = ["derive"] }
|
||||
serde_json = "1.0.64"
|
||||
serde_json = { version = "1.0.64", features = ["raw_value"] }
|
||||
thiserror = "1.0.24"
|
||||
tracing = "0.1.34"
|
||||
parking_lot = "0.12.0"
|
||||
@@ -49,5 +50,8 @@ sp-runtime = { version = "6.0.0", git = "https://github.com/paritytech/substrate
|
||||
frame-metadata = "15.0.0"
|
||||
derivative = "2.2.0"
|
||||
|
||||
[target.wasm32-unknown-unknown.dependencies]
|
||||
getrandom = { version = "0.2", features = ["js"] }
|
||||
|
||||
[dev-dependencies]
|
||||
tokio = { version = "1.8", features = ["macros", "time", "rt-multi-thread"] }
|
||||
|
||||
@@ -61,7 +61,10 @@ impl<T: Config> std::fmt::Debug for OnlineClient<T> {
|
||||
}
|
||||
|
||||
// The default constructors assume Jsonrpsee.
|
||||
#[cfg(feature = "jsonrpsee")]
|
||||
#[cfg(any(
|
||||
feature = "jsonrpsee-ws",
|
||||
all(feature = "jsonrpsee-web", target_arch = "wasm32")
|
||||
))]
|
||||
impl<T: Config> OnlineClient<T> {
|
||||
/// Construct a new [`OnlineClient`] using default settings which
|
||||
/// point to a locally running node on `ws://127.0.0.1:9944`.
|
||||
@@ -72,7 +75,7 @@ impl<T: Config> OnlineClient<T> {
|
||||
|
||||
/// Construct a new [`OnlineClient`], providing a URL to connect to.
|
||||
pub async fn from_url(url: impl AsRef<str>) -> Result<OnlineClient<T>, Error> {
|
||||
let client = jsonrpsee_helpers::ws_client(url.as_ref())
|
||||
let client = jsonrpsee_helpers::client(url.as_ref())
|
||||
.await
|
||||
.map_err(|e| crate::error::RpcError::ClientError(Box::new(e)))?;
|
||||
OnlineClient::from_rpc_client(Arc::new(client)).await
|
||||
@@ -346,7 +349,7 @@ impl Update {
|
||||
}
|
||||
|
||||
// helpers for a jsonrpsee specific OnlineClient.
|
||||
#[cfg(feature = "jsonrpsee")]
|
||||
#[cfg(feature = "jsonrpsee-ws")]
|
||||
mod jsonrpsee_helpers {
|
||||
pub use jsonrpsee::{
|
||||
client_transport::ws::{
|
||||
@@ -366,7 +369,7 @@ mod jsonrpsee_helpers {
|
||||
};
|
||||
|
||||
/// Build WS RPC client from URL
|
||||
pub async fn ws_client(url: &str) -> Result<Client, Error> {
|
||||
pub async fn client(url: &str) -> Result<Client, Error> {
|
||||
let (sender, receiver) = ws_transport(url).await?;
|
||||
Ok(ClientBuilder::default()
|
||||
.max_notifs_per_subscription(4096)
|
||||
@@ -383,3 +386,26 @@ mod jsonrpsee_helpers {
|
||||
.map_err(|e| Error::Transport(e.into()))
|
||||
}
|
||||
}
|
||||
|
||||
// helpers for a jsonrpsee specific OnlineClient.
|
||||
#[cfg(all(feature = "jsonrpsee-web", target_arch = "wasm32"))]
|
||||
mod jsonrpsee_helpers {
|
||||
pub use jsonrpsee::{
|
||||
client_transport::web,
|
||||
core::{
|
||||
client::{
|
||||
Client,
|
||||
ClientBuilder,
|
||||
},
|
||||
Error,
|
||||
},
|
||||
};
|
||||
|
||||
/// Build web RPC client from URL
|
||||
pub async fn client(url: &str) -> Result<Client, Error> {
|
||||
let (sender, receiver) = web::connect(url).await.unwrap();
|
||||
Ok(ClientBuilder::default()
|
||||
.max_notifs_per_subscription(4096)
|
||||
.build_with_wasm(sender, receiver))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@ impl DecodeWithMetadata for DecodedValueThunk {
|
||||
metadata: &Metadata,
|
||||
) -> Result<Self::Target, Error> {
|
||||
let mut v = Vec::with_capacity(bytes.len());
|
||||
v.extend_from_slice(*bytes);
|
||||
v.extend_from_slice(bytes);
|
||||
*bytes = &[];
|
||||
Ok(DecodedValueThunk {
|
||||
type_id,
|
||||
|
||||
@@ -68,7 +68,7 @@ where
|
||||
|
||||
let event_bytes = client
|
||||
.rpc()
|
||||
.storage(&*system_events_key().0, Some(block_hash))
|
||||
.storage(&system_events_key().0, Some(block_hash))
|
||||
.await?
|
||||
.map(|e| e.0)
|
||||
.unwrap_or_else(Vec::new);
|
||||
|
||||
@@ -140,6 +140,13 @@ use tokio as _;
|
||||
|
||||
pub use subxt_macro::subxt;
|
||||
|
||||
// Used to enable the js feature for wasm.
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
pub use getrandom as _;
|
||||
|
||||
#[cfg(all(feature = "jsonrpsee-ws", feature = "jsonrpsee-web"))]
|
||||
std::compile_error!("Both the features `jsonrpsee-ws` and `jsonrpsee-web` are enabled which are mutually exclusive");
|
||||
|
||||
pub mod blocks;
|
||||
pub mod client;
|
||||
pub mod config;
|
||||
|
||||
@@ -12,18 +12,24 @@ use futures::stream::{
|
||||
StreamExt,
|
||||
TryStreamExt,
|
||||
};
|
||||
use jsonrpsee::{
|
||||
core::client::{
|
||||
use jsonrpsee::core::{
|
||||
client::{
|
||||
Client,
|
||||
ClientT,
|
||||
SubscriptionClientT,
|
||||
},
|
||||
types::ParamsSer,
|
||||
};
|
||||
use serde_json::value::{
|
||||
RawValue,
|
||||
Value,
|
||||
traits::ToRpcParams,
|
||||
Error as JsonRpseeError,
|
||||
};
|
||||
use serde_json::value::RawValue;
|
||||
|
||||
struct Params(Option<Box<RawValue>>);
|
||||
|
||||
impl ToRpcParams for Params {
|
||||
fn to_rpc_params(self) -> Result<Option<Box<RawValue>>, JsonRpseeError> {
|
||||
Ok(self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl RpcClientT for Client {
|
||||
fn request_raw<'a>(
|
||||
@@ -32,8 +38,7 @@ impl RpcClientT for Client {
|
||||
params: Option<Box<RawValue>>,
|
||||
) -> RpcFuture<'a, Box<RawValue>> {
|
||||
Box::pin(async move {
|
||||
let params = prep_params_for_jsonrpsee(params);
|
||||
let res = ClientT::request(self, method, Some(params))
|
||||
let res = ClientT::request(self, method, Params(params))
|
||||
.await
|
||||
.map_err(|e| RpcError::ClientError(Box::new(e)))?;
|
||||
Ok(res)
|
||||
@@ -47,11 +52,10 @@ impl RpcClientT for Client {
|
||||
unsub: &'a str,
|
||||
) -> RpcFuture<'a, RpcSubscription> {
|
||||
Box::pin(async move {
|
||||
let params = prep_params_for_jsonrpsee(params);
|
||||
let sub = SubscriptionClientT::subscribe::<Box<RawValue>>(
|
||||
let sub = SubscriptionClientT::subscribe::<Box<RawValue>, _>(
|
||||
self,
|
||||
sub,
|
||||
Some(params),
|
||||
Params(params),
|
||||
unsub,
|
||||
)
|
||||
.await
|
||||
@@ -62,21 +66,3 @@ impl RpcClientT for Client {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// This is ugly; we have to encode to Value's to be compat with the jsonrpc interface.
|
||||
// Remove and simplify this once something like https://github.com/paritytech/jsonrpsee/issues/862 is in:
|
||||
fn prep_params_for_jsonrpsee(params: Option<Box<RawValue>>) -> ParamsSer<'static> {
|
||||
let params = match params {
|
||||
Some(params) => params,
|
||||
// No params? avoid any work and bail early.
|
||||
None => return ParamsSer::Array(Vec::new()),
|
||||
};
|
||||
let val = serde_json::to_value(¶ms).expect("RawValue guarantees valid JSON");
|
||||
let arr = match val {
|
||||
Value::Array(arr) => arr,
|
||||
_ => {
|
||||
panic!("RPC Params are expected to be an array but got {params}");
|
||||
}
|
||||
};
|
||||
ParamsSer::Array(arr)
|
||||
}
|
||||
|
||||
@@ -18,8 +18,8 @@ pub fn write_storage_address_root_bytes<Address: StorageAddress>(
|
||||
addr: &Address,
|
||||
out: &mut Vec<u8>,
|
||||
) {
|
||||
out.extend(&sp_core::twox_128(addr.pallet_name().as_bytes()));
|
||||
out.extend(&sp_core::twox_128(addr.entry_name().as_bytes()));
|
||||
out.extend(sp_core::twox_128(addr.pallet_name().as_bytes()));
|
||||
out.extend(sp_core::twox_128(addr.entry_name().as_bytes()));
|
||||
}
|
||||
|
||||
/// Outputs the [`storage_address_root_bytes`] as well as any additional bytes that represent
|
||||
|
||||
@@ -138,7 +138,7 @@ impl<'a> TxPayload for DynamicTxPayload<'a> {
|
||||
let pallet = metadata.pallet(&self.pallet_name)?;
|
||||
let call_id = pallet.call_ty_id().ok_or(MetadataError::CallNotFound)?;
|
||||
let call_value =
|
||||
Value::unnamed_variant(self.call_name.to_owned(), self.fields.clone());
|
||||
Value::unnamed_variant(self.call_name.clone(), self.fields.clone());
|
||||
|
||||
pallet.index().encode_to(out);
|
||||
scale_value::scale::encode_as_type(&call_value, call_id, metadata.types(), out)?;
|
||||
|
||||
Reference in New Issue
Block a user