Split subxt (#102)

* Proc macro improvements.

* Use proc-macros.

* Update examples.

* Fix build.

* Run rustfmt.

* Fix total issuance test.

* Remove gas limit from put code call.

* Handle runtime errors.

* Fix tests.

* Make test more reliable.

* Revert "Handle runtime errors."

This reverts commit 26f30a9f4cfcfddfb3e49308cded46cfe6468697.

* Use expect instead of unwrap.

* Parse marker type.

* Fetch doesn't fail.
This commit is contained in:
David Craven
2020-05-12 13:25:22 +02:00
committed by GitHub
parent 825f3ab64c
commit f861f3fac4
21 changed files with 697 additions and 564 deletions
+18 -21
View File
@@ -15,31 +15,28 @@
// along with substrate-subxt. If not, see <http://www.gnu.org/licenses/>.
use substrate_subxt::{
system::System,
Error,
ClientBuilder,
KusamaRuntime,
};
fn main() {
async_std::task::block_on(async move {
env_logger::init();
#[async_std::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::init();
let block_hash = fetch_block_hash(1).await;
match block_hash {
Ok(Some(hash)) => println!("Block hash for block number 1: {}", hash),
Ok(None) => println!("Block number 1 not found."),
Err(_) => eprintln!("Failed to fetch block hash"),
}
});
}
async fn fetch_block_hash(
block_number: u32,
) -> Result<Option<<KusamaRuntime as System>::Hash>, Error> {
substrate_subxt::ClientBuilder::<KusamaRuntime>::new()
let client = ClientBuilder::<KusamaRuntime>::new()
.set_url("wss://kusama-rpc.polkadot.io")
.build()
.await?
.block_hash(Some(block_number.into()))
.await
.await?;
let block_number = 1;
let block_hash = client.block_hash(Some(block_number.into())).await?;
if let Some(hash) = block_hash {
println!("Block hash for block number {}: {}", block_number, hash);
} else {
println!("Block number {} not found.", block_number);
}
Ok(())
}
+14 -23
View File
@@ -16,36 +16,27 @@
use sp_keyring::AccountKeyring;
use substrate_subxt::{
balances,
Error,
balances::*,
ClientBuilder,
KusamaRuntime,
};
fn main() {
async_std::task::block_on(async move {
env_logger::init();
#[async_std::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::init();
let xt_result = transfer_balance().await;
match xt_result {
Ok(hash) => println!("Balance transfer extrinsic submitted: {}", hash),
Err(_) => eprintln!("Balance transfer extrinisic failed"),
}
});
}
async fn transfer_balance() -> Result<sp_core::H256, Error> {
let signer = AccountKeyring::Alice.pair();
let dest = AccountKeyring::Bob.to_account_id().into();
// note use of `KusamaRuntime`
substrate_subxt::ClientBuilder::<KusamaRuntime>::new()
.build()
.await?
let client = ClientBuilder::<KusamaRuntime>::new().build().await?;
let hash = client
.xt(signer, None)
.await?
.submit(balances::TransferCall {
to: &dest,
amount: 10_000,
})
.await
.transfer(&dest, 10_000)
.await?;
println!("Balance transfer extrinsic submitted: {}", hash);
Ok(())
}
+10 -12
View File
@@ -16,8 +16,9 @@
use sp_keyring::AccountKeyring;
use substrate_subxt::{
balances,
DefaultNodeRuntime as Runtime,
balances::*,
ClientBuilder,
DefaultNodeRuntime,
};
#[async_std::main]
@@ -27,18 +28,15 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let signer = AccountKeyring::Alice.pair();
let dest = AccountKeyring::Bob.to_account_id().into();
let cli = substrate_subxt::ClientBuilder::<Runtime>::new()
.build()
.await?;
let xt = cli.xt(signer, None).await?;
let xt_result = xt
let client = ClientBuilder::<DefaultNodeRuntime>::new().build().await?;
let result = client
.xt(signer, None)
.await?
.watch()
.submit(balances::TransferCall {
to: &dest,
amount: 10_000,
})
.transfer(&dest, 10_000)
.await?;
if let Some(event) = xt_result.find_event::<balances::TransferEvent<_>>()? {
if let Some(event) = result.transfer()? {
println!("Balance transfer success: value: {:?}", event.amount);
} else {
println!("Failed to find Balances::Transfer Event");