#![deny(
bad_style,
const_err,
improper_ctypes,
missing_docs,
non_shorthand_field_patterns,
no_mangle_generic_items,
overflowing_literals,
path_statements,
patterns_in_fns_without_body,
private_in_public,
unconditional_recursion,
unused_allocation,
unused_comparisons,
unused_parens,
while_true,
trivial_casts,
trivial_numeric_casts,
unused_extern_crates
)]
#![allow(clippy::type_complexity)]
#[macro_use]
extern crate substrate_subxt_proc_macro;
pub use sp_core;
pub use sp_runtime;
use std::{
convert::TryFrom,
marker::PhantomData,
};
use codec::{
Codec,
Encode,
};
use futures::future;
use jsonrpsee::client::Subscription;
use sc_rpc_api::state::ReadProof;
use sp_core::{
storage::{
StorageChangeSet,
StorageKey,
},
Pair,
};
use sp_runtime::{
generic::{
SignedPayload,
UncheckedExtrinsic,
},
traits::{
IdentifyAccount,
SignedExtension,
Verify,
},
MultiSignature,
};
use sp_version::RuntimeVersion;
mod error;
mod events;
mod extrinsic;
mod frame;
mod metadata;
mod rpc;
mod runtimes;
pub use crate::{
error::Error,
events::{
EventsDecoder,
EventsError,
RawEvent,
},
extrinsic::*,
frame::*,
metadata::{
Metadata,
MetadataError,
},
rpc::{
BlockNumber,
ExtrinsicSuccess,
},
runtimes::*,
substrate_subxt_proc_macro::*,
};
use crate::{
frame::{
balances::Balances,
system::{
AccountStoreExt,
Phase,
System,
SystemEvent,
},
},
rpc::{
ChainBlock,
Rpc,
},
};
#[derive(Default)]
pub struct ClientBuilder<T: System, S = MultiSignature, E = DefaultExtra<T>> {
_marker: std::marker::PhantomData<(T, S, E)>,
url: Option<String>,
client: Option<jsonrpsee::Client>,
}
impl<T: System, S, E> ClientBuilder<T, S, E> {
pub fn new() -> Self {
Self {
_marker: std::marker::PhantomData,
url: None,
client: None,
}
}
pub fn set_client<P: Into<jsonrpsee::Client>>(mut self, client: P) -> Self {
self.client = Some(client.into());
self
}
pub fn set_url<P: Into<String>>(mut self, url: P) -> Self {
self.url = Some(url.into());
self
}
pub async fn build(self) -> Result<Client<T, S, E>, Error> {
let client = if let Some(client) = self.client {
client
} else {
let url = self
.url
.as_ref()
.map(|s| &**s)
.unwrap_or("ws://127.0.0.1:9944");
if url.starts_with("ws://") || url.starts_with("wss://") {
jsonrpsee::ws_client(url).await?
} else {
jsonrpsee::http_client(url)
}
};
let rpc = Rpc::new(client);
let (metadata, genesis_hash, runtime_version) = future::join3(
rpc.metadata(),
rpc.genesis_hash(),
rpc.runtime_version(None),
)
.await;
Ok(Client {
rpc,
genesis_hash: genesis_hash?,
metadata: metadata?,
runtime_version: runtime_version?,
_marker: PhantomData,
})
}
}
pub struct Client<T: System, S = MultiSignature, E = DefaultExtra<T>> {
rpc: Rpc<T>,
genesis_hash: T::Hash,
metadata: Metadata,
runtime_version: RuntimeVersion,
_marker: PhantomData<(fn() -> S, E)>,
}
impl<T: System, S, E> Clone for Client<T, S, E> {
fn clone(&self) -> Self {
Self {
rpc: self.rpc.clone(),
genesis_hash: self.genesis_hash,
metadata: self.metadata.clone(),
runtime_version: self.runtime_version.clone(),
_marker: PhantomData,
}
}
}
impl<T: System, S, E> Client<T, S, E> {
pub fn metadata(&self) -> &Metadata {
&self.metadata
}
pub async fn fetch<F: Store<T>>(
&self,
store: F,
hash: Option<T::Hash>,
) -> Result<F::Returns, Error> {
let key = store.key(&self.metadata)?;
let value = self.rpc.storage::<F::Returns>(key, hash).await?;
if let Some(v) = value {
Ok(v)
} else {
Ok(store.default(&self.metadata)?)
}
}
pub async fn query_storage(
&self,
keys: Vec<StorageKey>,
from: T::Hash,
to: Option<T::Hash>,
) -> Result<Vec<StorageChangeSet<<T as System>::Hash>>, Error> {
self.rpc.query_storage(keys, from, to).await
}
pub async fn header<H>(&self, hash: Option<H>) -> Result<Option<T::Header>, Error>
where
H: Into<T::Hash> + 'static,
{
let header = self.rpc.header(hash.map(|h| h.into())).await?;
Ok(header)
}
pub async fn block_hash(
&self,
block_number: Option<BlockNumber<T>>,
) -> Result<Option<T::Hash>, Error> {
let hash = self.rpc.block_hash(block_number).await?;
Ok(hash)
}
pub async fn finalized_head(&self) -> Result<T::Hash, Error> {
let head = self.rpc.finalized_head().await?;
Ok(head)
}
pub async fn block<H>(&self, hash: Option<H>) -> Result<Option<ChainBlock<T>>, Error>
where
H: Into<T::Hash> + 'static,
{
let block = self.rpc.block(hash.map(|h| h.into())).await?;
Ok(block)
}
pub async fn read_proof<H>(
&self,
keys: Vec<StorageKey>,
hash: Option<H>,
) -> Result<ReadProof<T::Hash>, Error>
where
H: Into<T::Hash> + 'static,
{
let proof = self.rpc.read_proof(keys, hash.map(|h| h.into())).await?;
Ok(proof)
}
pub async fn submit_extrinsic<X: Encode>(
&self,
extrinsic: X,
) -> Result<T::Hash, Error> {
let xt_hash = self.rpc.submit_extrinsic(extrinsic).await?;
Ok(xt_hash)
}
pub async fn submit_and_watch_extrinsic<X: Encode + 'static>(
self,
extrinsic: X,
decoder: EventsDecoder<T>,
) -> Result<ExtrinsicSuccess<T>, Error> {
let success = self
.rpc
.submit_and_watch_extrinsic(extrinsic, decoder)
.await?;
Ok(success)
}
pub async fn subscribe_events(
&self,
) -> Result<Subscription<StorageChangeSet<T::Hash>>, Error> {
let events = self.rpc.subscribe_events().await?;
Ok(events)
}
pub async fn subscribe_blocks(&self) -> Result<Subscription<T::Header>, Error> {
let headers = self.rpc.subscribe_blocks().await?;
Ok(headers)
}
pub async fn subscribe_finalized_blocks(
&self,
) -> Result<Subscription<T::Header>, Error> {
let headers = self.rpc.subscribe_finalized_blocks().await?;
Ok(headers)
}
}
impl<T, S, E> Client<T, S, E>
where
T: System + Balances + Send + Sync + 'static,
S: 'static,
E: SignedExtra<T> + SignedExtension + 'static,
{
pub async fn create_raw_payload<C: Call<T>>(
&self,
account_id: &<T as System>::AccountId,
call: C,
) -> Result<SignedPayload<Encoded, <E as SignedExtra<T>>::Extra>, Error> {
let account_nonce = self.account(account_id).await?.nonce;
let version = self.runtime_version.spec_version;
let genesis_hash = self.genesis_hash;
let call = self
.metadata()
.module_with_calls(C::MODULE)
.and_then(|module| module.call(C::FUNCTION, call))?;
let extra: E = E::new(version, account_nonce, genesis_hash);
let raw_payload = SignedPayload::new(call, extra.extra())?;
Ok(raw_payload)
}
pub async fn xt<P>(
&self,
signer: P,
nonce: Option<T::Index>,
) -> Result<XtBuilder<T, P, S, E>, Error>
where
P: Pair,
P::Signature: Codec,
S: Verify,
S::Signer: From<P::Public> + IdentifyAccount<AccountId = T::AccountId>,
{
let account_id = S::Signer::from(signer.public()).into_account();
let nonce = match nonce {
Some(nonce) => nonce,
None => self.account(&account_id).await?.nonce,
};
let genesis_hash = self.genesis_hash;
let runtime_version = self.runtime_version.clone();
Ok(XtBuilder {
client: self.clone(),
nonce,
runtime_version,
genesis_hash,
signer,
})
}
}
#[derive(Clone)]
pub struct XtBuilder<T: System, P, S, E> {
client: Client<T, S, E>,
nonce: T::Index,
runtime_version: RuntimeVersion,
genesis_hash: T::Hash,
signer: P,
}
impl<T: System, P, S, E> XtBuilder<T, P, S, E> {
pub fn metadata(&self) -> &Metadata {
self.client.metadata()
}
pub fn nonce(&self) -> T::Index {
self.nonce
}
pub fn set_nonce(&mut self, nonce: T::Index) -> &mut XtBuilder<T, P, S, E> {
self.nonce = nonce;
self
}
pub fn increment_nonce(&mut self) -> &mut XtBuilder<T, P, S, E> {
self.set_nonce(self.nonce() + 1.into());
self
}
}
impl<T: System + Send + Sync + 'static, P, S: 'static, E> XtBuilder<T, P, S, E>
where
P: Pair,
S: Verify + Codec + From<P::Signature>,
S::Signer: From<P::Public> + IdentifyAccount<AccountId = T::AccountId>,
T::Address: From<T::AccountId>,
E: SignedExtra<T> + SignedExtension + 'static,
{
pub fn create_and_sign<C: Call<T>>(
&self,
call: C,
) -> Result<
UncheckedExtrinsic<T::Address, Encoded, S, <E as SignedExtra<T>>::Extra>,
Error,
> {
let signer = self.signer.clone();
let account_nonce = self.nonce;
let version = self.runtime_version.spec_version;
let genesis_hash = self.genesis_hash;
let call = self
.metadata()
.module_with_calls(C::MODULE)
.and_then(|module| module.call(C::FUNCTION, call))?;
log::info!(
"Creating Extrinsic with genesis hash {:?} and account nonce {:?}",
genesis_hash,
account_nonce
);
let extra = E::new(version, account_nonce, genesis_hash);
let xt = extrinsic::create_and_sign::<_, _, _, S, _>(signer, call, extra)?;
Ok(xt)
}
pub async fn submit<C: Call<T>>(&self, call: C) -> Result<T::Hash, Error> {
let extrinsic = self.create_and_sign(call)?;
let xt_hash = self.client.submit_extrinsic(extrinsic).await?;
Ok(xt_hash)
}
pub fn watch(self) -> EventsSubscriber<T, P, S, E> {
let metadata = self.client.metadata().clone();
let decoder = EventsDecoder::try_from(metadata).map_err(Into::into);
EventsSubscriber {
client: self.client.clone(),
builder: self,
decoder,
}
}
}
pub struct EventsSubscriber<T: System, P, S, E> {
client: Client<T, S, E>,
builder: XtBuilder<T, P, S, E>,
decoder: Result<EventsDecoder<T>, EventsError>,
}
impl<T: System, P, S, E> EventsSubscriber<T, P, S, E> {
pub fn events_decoder<
F: FnOnce(&mut EventsDecoder<T>) -> Result<usize, EventsError>,
>(
self,
f: F,
) -> Self {
let mut this = self;
if let Ok(ref mut decoder) = this.decoder {
if let Err(err) = f(decoder) {
this.decoder = Err(err)
}
}
this
}
}
impl<T: System + Send + Sync + 'static, P, S: 'static, E> EventsSubscriber<T, P, S, E>
where
P: Pair,
S: Verify + Codec + From<P::Signature>,
S::Signer: From<P::Public> + IdentifyAccount<AccountId = T::AccountId>,
T::Address: From<T::AccountId>,
E: SignedExtra<T> + SignedExtension + 'static,
{
pub async fn submit<C: Call<T>>(self, call: C) -> Result<ExtrinsicSuccess<T>, Error> {
let mut decoder = self.decoder?;
C::events_decoder(&mut decoder)?;
let extrinsic = self.builder.create_and_sign(call)?;
let xt_success = self
.client
.submit_and_watch_extrinsic(extrinsic, decoder)
.await?;
Ok(xt_success)
}
}
#[derive(Clone, Debug)]
pub struct Encoded(pub Vec<u8>);
impl codec::Encode for Encoded {
fn encode(&self) -> Vec<u8> {
self.0.to_owned()
}
}
#[cfg(test)]
mod tests {
use sp_core::storage::{
well_known_keys,
StorageKey,
};
use sp_keyring::{
AccountKeyring,
Ed25519Keyring,
};
use super::*;
pub(crate) async fn test_client() -> Client<crate::DefaultNodeRuntime> {
ClientBuilder::new()
.build()
.await
.expect("Error creating client")
}
#[async_std::test]
#[ignore]
async fn test_tx_transfer_balance() {
env_logger::try_init().ok();
let signer = AccountKeyring::Alice.pair();
let dest = AccountKeyring::Bob.to_account_id().into();
let client = test_client().await;
let mut xt = client.xt(signer, None).await.unwrap();
let _ = xt
.submit(balances::TransferCall {
to: &dest,
amount: 10_000,
})
.await
.unwrap();
xt.increment_nonce()
.submit(balances::TransferCall {
to: &dest,
amount: 10_000,
})
.await
.unwrap();
}
#[async_std::test]
#[ignore]
async fn test_getting_hash() {
let client = test_client().await;
client.block_hash(None).await.unwrap();
}
#[async_std::test]
#[ignore]
async fn test_getting_block() {
let client = test_client().await;
let block_hash = client.block_hash(None).await.unwrap();
client.block(block_hash).await.unwrap();
}
#[async_std::test]
#[ignore]
async fn test_getting_read_proof() {
let client = test_client().await;
let block_hash = client.block_hash(None).await.unwrap();
client
.read_proof(
vec![
StorageKey(well_known_keys::HEAP_PAGES.to_vec()),
StorageKey(well_known_keys::EXTRINSIC_INDEX.to_vec()),
],
block_hash,
)
.await
.unwrap();
}
#[async_std::test]
#[ignore]
async fn test_chain_subscribe_blocks() {
let client = test_client().await;
let mut blocks = client.subscribe_blocks().await.unwrap();
blocks.next().await;
}
#[async_std::test]
#[ignore]
async fn test_chain_subscribe_finalized_blocks() {
let client = test_client().await;
let mut blocks = client.subscribe_finalized_blocks().await.unwrap();
blocks.next().await;
}
#[async_std::test]
#[ignore]
async fn test_create_raw_payload() {
let signer_pair = Ed25519Keyring::Alice.pair();
let signer_account_id = Ed25519Keyring::Alice.to_account_id();
let dest = AccountKeyring::Bob.to_account_id().into();
let client = test_client().await;
let raw_payload = client
.create_raw_payload(
&signer_account_id,
balances::TransferCall {
to: &dest,
amount: 10_000,
},
)
.await
.unwrap();
let raw_signature = signer_pair.sign(raw_payload.encode().as_slice());
let raw_multisig = MultiSignature::from(raw_signature);
let xt = client.xt(signer_pair.clone(), None).await.unwrap();
let xt_multi_sig = xt
.create_and_sign(balances::TransferCall {
to: &dest,
amount: 10_000,
})
.unwrap()
.signature
.unwrap()
.1;
assert_eq!(raw_multisig, xt_multi_sig);
}
}