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

Simplify creating and signing extrinsics #490

Merged
merged 13 commits into from
Mar 30, 2022
Merged
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ resides ([`CARGO_MANIFEST_DIR`](https://doc.rust-lang.org/cargo/reference/enviro
### Initializing the API client

```rust
use subxt::{ClientBuilder, DefaultConfig, DefaultExtra};
use subxt::{ClientBuilder, DefaultConfig, SubstrateExtrinsicParams};

let api = ClientBuilder::new()
.set_url("wss://rpc.polkadot.io:443")
.build()
.await?
.to_runtime_api::<node_runtime::RuntimeApi<DefaultConfig, DefaultExtra<DefaultConfig>>>();
.to_runtime_api::<node_runtime::RuntimeApi<DefaultConfig, SubstrateExtrinsicParams<DefaultConfig>>>();
```

The `RuntimeApi` type is generated by the `subxt` macro from the supplied metadata. This can be parameterized with user
Expand All @@ -53,7 +53,7 @@ chain.

### Querying Storage

Call the generated `RuntimeApi::storage()` method, followed by the `pallet_name()` and then the `storage_item_name()`.
Call the generated `RuntimeApi::storage()` method, followed by the `pallet_name()` and then the `storage_item_name()`.

So in order to query `Balances::TotalIssuance`:

Expand Down
2 changes: 1 addition & 1 deletion codegen/src/api/calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ pub fn generate_calls(
impl<'a, T, X> TransactionApi<'a, T, X>
where
T: ::subxt::Config,
X: ::subxt::SignedExtra<T>,
X: ::subxt::extrinsic::ExtrinsicParams<T>,
{
pub fn new(client: &'a ::subxt::Client<T>) -> Self {
Self { client, marker: ::core::marker::PhantomData }
Expand Down
6 changes: 3 additions & 3 deletions codegen/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ impl RuntimeGenerator {
impl<T, X> ::core::convert::From<::subxt::Client<T>> for RuntimeApi<T, X>
where
T: ::subxt::Config,
X: ::subxt::SignedExtra<T>
X: ::subxt::extrinsic::ExtrinsicParams<T>
{
fn from(client: ::subxt::Client<T>) -> Self {
Self { client, marker: ::core::marker::PhantomData }
Expand All @@ -298,7 +298,7 @@ impl RuntimeGenerator {
impl<'a, T, X> RuntimeApi<T, X>
where
T: ::subxt::Config,
X: ::subxt::SignedExtra<T>,
X: ::subxt::extrinsic::ExtrinsicParams<T>,
{
pub fn constants(&'a self) -> ConstantsApi {
ConstantsApi
Expand Down Expand Up @@ -368,7 +368,7 @@ impl RuntimeGenerator {
impl<'a, T, X> TransactionApi<'a, T, X>
where
T: ::subxt::Config,
X: ::subxt::SignedExtra<T>,
X: ::subxt::extrinsic::ExtrinsicParams<T>,
{
#(
pub fn #pallets_with_calls(&self) -> #pallets_with_calls::calls::TransactionApi<'a, T, X> {
Expand Down
10 changes: 5 additions & 5 deletions examples/examples/balance_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with subxt. If not, see <http://www.gnu.org/licenses/>.

//! To run this example, a local polkadot node should be running. Example verified against polkadot 0.9.13-82616422d0-aarch64-macos.
//! To run this example, a local polkadot node should be running. Example verified against polkadot 0.9.18-f6d6ab005d-aarch64-macos.
//!
//! E.g.
//! ```bash
Expand All @@ -26,8 +26,8 @@ use sp_keyring::AccountKeyring;
use subxt::{
ClientBuilder,
DefaultConfig,
DefaultExtra,
PairSigner,
PolkadotExtrinsicParams,
};

#[subxt::subxt(runtime_metadata_path = "examples/polkadot_metadata.scale")]
Expand All @@ -43,12 +43,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let api = ClientBuilder::new()
.build()
.await?
.to_runtime_api::<polkadot::RuntimeApi<DefaultConfig, DefaultExtra<DefaultConfig>>>();
.to_runtime_api::<polkadot::RuntimeApi<DefaultConfig, PolkadotExtrinsicParams<DefaultConfig>>>();
let hash = api
.tx()
.balances()
.transfer(dest, 10_000)
.sign_and_submit(&signer)
.transfer(dest, 123_456_789_012_345)
.sign_and_submit_default(&signer)
.await?;

println!("Balance transfer extrinsic submitted: {}", hash);
Expand Down
69 changes: 69 additions & 0 deletions examples/examples/balance_transfer_with_params.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright 2019-2022 Parity Technologies (UK) Ltd.
// This file is part of subxt.
//
// subxt is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// subxt is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with subxt. If not, see <http://www.gnu.org/licenses/>.

//! To run this example, a local polkadot node should be running. Example verified against polkadot 0.9.18-f6d6ab005d-aarch64-macos.
//!
//! E.g.
//! ```bash
//! curl "https://github.com/paritytech/polkadot/releases/download/v0.9.13/polkadot" --output /usr/local/bin/polkadot --location
//! polkadot --dev --tmp
//! ```

use sp_keyring::AccountKeyring;
use subxt::{
extrinsic::{
Era,
PlainTip,
},
ClientBuilder,
DefaultConfig,
PairSigner,
PolkadotExtrinsicParams,
PolkadotExtrinsicParamsBuilder as Params,
};

#[subxt::subxt(runtime_metadata_path = "examples/polkadot_metadata.scale")]
pub mod polkadot {}

#[async_std::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::init();

let signer = PairSigner::new(AccountKeyring::Alice.pair());
let dest = AccountKeyring::Bob.to_account_id().into();

let api = ClientBuilder::new()
.build()
.await?
.to_runtime_api::<polkadot::RuntimeApi<DefaultConfig, PolkadotExtrinsicParams<DefaultConfig>>>();

// Configure the transaction tip and era:
let tx_params = Params::new()
.tip(PlainTip::new(20_000_000_000))
.era(Era::Immortal, *api.client.genesis());

// Send the transaction:
let hash = api
.tx()
.balances()
.transfer(dest, 123_456_789_012_345)
.sign_and_submit(&signer, tx_params)
Comment on lines +62 to +63
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure whether this is possible/desirable, but did you consider adding a builder style method onto SubmittableExtrinsic which allows setting the params, and would use the default if not specified e.g.

let hash = api
        .tx()
        .balances()
        .transfer(dest, 123_456_789_012_345)
        .params(tx_params)
        .sign_and_submit(&signer)

It would avoid the requirement of the _default method equivalents, on the other hand would be less explicit if adding params is the common case.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooh interesting! I didn't consider that offhand, no.

One thing I like about having the _default and the param-taking methods is that it's up to the user whether the params actually do implement Default at all or not; the Default constraint is only on those _default methods now, so if for some reason somebody could not or did not want a default set of params for their chain, that'd work just fine.

Copy link
Collaborator Author

@jsdw jsdw Mar 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I say that but actually I think I left a Default clause somewhere I shouldn't have, so this is a good pointer to remove that! done!

.await?;

println!("Balance transfer extrinsic submitted: {}", hash);

Ok(())
}
8 changes: 4 additions & 4 deletions examples/examples/custom_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with subxt. If not, see <http://www.gnu.org/licenses/>.

//! To run this example, a local polkadot node should be running. Example verified against polkadot 0.9.13-82616422d0-aarch64-macos.
//! To run this example, a local polkadot node should be running. Example verified against polkadot 0.9.18-f6d6ab005d-aarch64-macos.
//!
//! E.g.
//! ```bash
Expand All @@ -27,8 +27,8 @@ use subxt::{
ClientBuilder,
Config,
DefaultConfig,
DefaultExtra,
PairSigner,
PolkadotExtrinsicParams,
};

#[subxt::subxt(runtime_metadata_path = "examples/polkadot_metadata.scale")]
Expand Down Expand Up @@ -60,7 +60,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let api = ClientBuilder::new()
.build()
.await?
.to_runtime_api::<polkadot::RuntimeApi<MyConfig, DefaultExtra<MyConfig>>>();
.to_runtime_api::<polkadot::RuntimeApi<MyConfig, PolkadotExtrinsicParams<MyConfig>>>();

let signer = PairSigner::new(AccountKeyring::Alice.pair());
let dest = AccountKeyring::Bob.to_account_id().into();
Expand All @@ -69,7 +69,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.tx()
.balances()
.transfer(dest, 10_000)
.sign_and_submit(&signer)
.sign_and_submit_default(&signer)
.await?;

println!("Balance transfer extrinsic submitted: {}", hash);
Expand Down
2 changes: 1 addition & 1 deletion examples/examples/custom_type_derives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with subxt. If not, see <http://www.gnu.org/licenses/>.

//! Example verified against polkadot 0.9.13-82616422d0-aarch64-macos.
//! Example verified against polkadot 0.9.18-f6d6ab005d-aarch64-macos.

#![allow(clippy::redundant_clone)]

Expand Down
6 changes: 3 additions & 3 deletions examples/examples/fetch_all_accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with subxt. If not, see <http://www.gnu.org/licenses/>.

//! To run this example, a local polkadot node should be running. Example verified against polkadot 0.9.13-82616422d0-aarch64-macos.
//! To run this example, a local polkadot node should be running. Example verified against polkadot 0.9.18-f6d6ab005d-aarch64-macos.
//!
//! E.g.
//! ```bash
Expand All @@ -25,7 +25,7 @@
use subxt::{
ClientBuilder,
DefaultConfig,
DefaultExtra,
PolkadotExtrinsicParams,
};

#[subxt::subxt(runtime_metadata_path = "examples/polkadot_metadata.scale")]
Expand All @@ -38,7 +38,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let api = ClientBuilder::new()
.build()
.await?
.to_runtime_api::<polkadot::RuntimeApi<DefaultConfig, DefaultExtra<DefaultConfig>>>();
.to_runtime_api::<polkadot::RuntimeApi<DefaultConfig, PolkadotExtrinsicParams<DefaultConfig>>>();

let mut iter = api.storage().system().account_iter(None).await?;

Expand Down
4 changes: 2 additions & 2 deletions examples/examples/fetch_staking_details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use subxt::{
sp_runtime::AccountId32,
ClientBuilder,
DefaultConfig,
DefaultExtra,
PolkadotExtrinsicParams,
};

#[subxt::subxt(runtime_metadata_path = "examples/polkadot_metadata.scale")]
Expand All @@ -44,7 +44,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let api = ClientBuilder::new()
.build()
.await?
.to_runtime_api::<polkadot::RuntimeApi<DefaultConfig, DefaultExtra<DefaultConfig>>>();
.to_runtime_api::<polkadot::RuntimeApi<DefaultConfig, PolkadotExtrinsicParams<DefaultConfig>>>();

let era = api.storage().staking().active_era(None).await?.unwrap();
println!(
Expand Down
Binary file modified examples/examples/polkadot_metadata.scale
Binary file not shown.
6 changes: 3 additions & 3 deletions examples/examples/rpc_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with subxt. If not, see <http://www.gnu.org/licenses/>.

//! To run this example, a local polkadot node should be running. Example verified against polkadot 0.9.13-82616422d0-aarch64-macos.
//! To run this example, a local polkadot node should be running. Example verified against polkadot 0.9.18-f6d6ab005d-aarch64-macos.
//!
//! E.g.
//! ```bash
Expand All @@ -25,7 +25,7 @@
use subxt::{
ClientBuilder,
DefaultConfig,
DefaultExtra,
PolkadotExtrinsicParams,
};

#[subxt::subxt(runtime_metadata_path = "examples/polkadot_metadata.scale")]
Expand All @@ -39,7 +39,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.set_url("wss://rpc.polkadot.io:443")
.build()
.await?
.to_runtime_api::<polkadot::RuntimeApi<DefaultConfig, DefaultExtra<DefaultConfig>>>();
.to_runtime_api::<polkadot::RuntimeApi<DefaultConfig, PolkadotExtrinsicParams<DefaultConfig>>>();

let block_number = 1u32;

Expand Down
16 changes: 8 additions & 8 deletions examples/examples/submit_and_watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with subxt. If not, see <http://www.gnu.org/licenses/>.

//! To run this example, a local polkadot node should be running. Example verified against polkadot 0.9.13-82616422d0-aarch64-macos.
//! To run this example, a local polkadot node should be running. Example verified against polkadot 0.9.18-f6d6ab005d-aarch64-macos.
//!
//! E.g.
//! ```bash
Expand All @@ -27,8 +27,8 @@ use sp_keyring::AccountKeyring;
use subxt::{
ClientBuilder,
DefaultConfig,
DefaultExtra,
PairSigner,
PolkadotExtrinsicParams,
};

#[subxt::subxt(runtime_metadata_path = "examples/polkadot_metadata.scale")]
Expand All @@ -55,13 +55,13 @@ async fn simple_transfer() -> Result<(), Box<dyn std::error::Error>> {
let api = ClientBuilder::new()
.build()
.await?
.to_runtime_api::<polkadot::RuntimeApi<DefaultConfig, DefaultExtra<_>>>();
.to_runtime_api::<polkadot::RuntimeApi<DefaultConfig, PolkadotExtrinsicParams<_>>>();

let balance_transfer = api
.tx()
.balances()
.transfer(dest, 10_000)
.sign_and_submit_then_watch(&signer)
.sign_and_submit_then_watch_default(&signer)
.await?
.wait_for_finalized_success()
.await?;
Expand All @@ -87,13 +87,13 @@ async fn simple_transfer_separate_events() -> Result<(), Box<dyn std::error::Err
let api = ClientBuilder::new()
.build()
.await?
.to_runtime_api::<polkadot::RuntimeApi<DefaultConfig, DefaultExtra<_>>>();
.to_runtime_api::<polkadot::RuntimeApi<DefaultConfig, PolkadotExtrinsicParams<_>>>();

let balance_transfer = api
.tx()
.balances()
.transfer(dest, 10_000)
.sign_and_submit_then_watch(&signer)
.sign_and_submit_then_watch_default(&signer)
.await?
.wait_for_finalized()
.await?;
Expand Down Expand Up @@ -138,13 +138,13 @@ async fn handle_transfer_events() -> Result<(), Box<dyn std::error::Error>> {
let api = ClientBuilder::new()
.build()
.await?
.to_runtime_api::<polkadot::RuntimeApi<DefaultConfig, DefaultExtra<_>>>();
.to_runtime_api::<polkadot::RuntimeApi<DefaultConfig, PolkadotExtrinsicParams<_>>>();

let mut balance_transfer_progress = api
.tx()
.balances()
.transfer(dest, 10_000)
.sign_and_submit_then_watch(&signer)
.sign_and_submit_then_watch_default(&signer)
.await?;

while let Some(ev) = balance_transfer_progress.next().await {
Expand Down
Loading