mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-13 19:51:05 +00:00
Merge branch 'master' into staking
This commit is contained in:
+96
-26
@@ -110,36 +110,58 @@ pub struct TransferEvent<T: Balances> {
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::{
|
||||
system::{
|
||||
AccountStore,
|
||||
AccountStoreExt,
|
||||
error::{
|
||||
Error,
|
||||
RuntimeError,
|
||||
},
|
||||
tests::test_client,
|
||||
events::EventsDecoder,
|
||||
signer::{
|
||||
PairSigner,
|
||||
Signer,
|
||||
},
|
||||
subscription::EventSubscription,
|
||||
system::AccountStoreExt,
|
||||
tests::{
|
||||
test_client,
|
||||
TestRuntime,
|
||||
},
|
||||
};
|
||||
use sp_core::{
|
||||
sr25519::Pair,
|
||||
Pair as _,
|
||||
};
|
||||
use sp_keyring::AccountKeyring;
|
||||
|
||||
subxt_test!({
|
||||
name: test_transfer,
|
||||
step: {
|
||||
state: {
|
||||
alice: AccountStore { account_id: &alice },
|
||||
bob: AccountStore { account_id: &bob },
|
||||
},
|
||||
call: TransferCall {
|
||||
to: &bob.clone().into(),
|
||||
amount: 10_000,
|
||||
},
|
||||
event: TransferEvent {
|
||||
from: alice.clone(),
|
||||
to: bob.clone(),
|
||||
amount: 10_000,
|
||||
},
|
||||
assert: {
|
||||
assert!(pre.alice.data.free - 10_000 >= post.alice.data.free);
|
||||
assert_eq!(pre.bob.data.free + 10_000, post.bob.data.free);
|
||||
},
|
||||
},
|
||||
});
|
||||
#[async_std::test]
|
||||
async fn test_transfer() {
|
||||
env_logger::try_init().ok();
|
||||
let alice = PairSigner::<TestRuntime, _>::new(AccountKeyring::Alice.pair());
|
||||
let bob = PairSigner::<TestRuntime, _>::new(AccountKeyring::Bob.pair());
|
||||
let (client, _) = test_client().await;
|
||||
|
||||
let alice_pre = client.account(alice.account_id(), None).await.unwrap();
|
||||
let bob_pre = client.account(bob.account_id(), None).await.unwrap();
|
||||
|
||||
let event = client
|
||||
.transfer_and_watch(&alice, &bob.account_id(), 10_000)
|
||||
.await
|
||||
.unwrap()
|
||||
.transfer()
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
let expected_event = TransferEvent {
|
||||
from: alice.account_id().clone(),
|
||||
to: bob.account_id().clone(),
|
||||
amount: 10_000,
|
||||
};
|
||||
assert_eq!(event, expected_event);
|
||||
|
||||
let alice_post = client.account(alice.account_id(), None).await.unwrap();
|
||||
let bob_post = client.account(bob.account_id(), None).await.unwrap();
|
||||
|
||||
assert!(alice_pre.data.free - 10_000 >= alice_post.data.free);
|
||||
assert_eq!(bob_pre.data.free + 10_000, bob_post.data.free);
|
||||
}
|
||||
|
||||
#[async_std::test]
|
||||
async fn test_state_total_issuance() {
|
||||
@@ -157,4 +179,52 @@ mod tests {
|
||||
let info = client.account(&account, None).await.unwrap();
|
||||
assert_ne!(info.data.free, 0);
|
||||
}
|
||||
|
||||
#[async_std::test]
|
||||
async fn test_transfer_error() {
|
||||
env_logger::try_init().ok();
|
||||
let alice = PairSigner::new(AccountKeyring::Alice.pair());
|
||||
let hans = PairSigner::new(Pair::generate().0);
|
||||
let (client, _) = test_client().await;
|
||||
client
|
||||
.transfer_and_watch(&alice, hans.account_id(), 100_000_000_000)
|
||||
.await
|
||||
.unwrap();
|
||||
let res = client
|
||||
.transfer_and_watch(&hans, alice.account_id(), 100_000_000_000)
|
||||
.await;
|
||||
if let Err(Error::Runtime(error)) = res {
|
||||
let error2 = RuntimeError {
|
||||
module: "Balances".into(),
|
||||
error: "InsufficientBalance".into(),
|
||||
};
|
||||
assert_eq!(error, error2);
|
||||
} else {
|
||||
panic!("expected an error");
|
||||
}
|
||||
}
|
||||
|
||||
#[async_std::test]
|
||||
async fn test_transfer_subscription() {
|
||||
env_logger::try_init().ok();
|
||||
let alice = PairSigner::new(AccountKeyring::Alice.pair());
|
||||
let bob = AccountKeyring::Bob.to_account_id();
|
||||
let (client, _) = test_client().await;
|
||||
let sub = client.subscribe_events().await.unwrap();
|
||||
let mut decoder = EventsDecoder::<TestRuntime>::new(client.metadata().clone());
|
||||
decoder.with_balances();
|
||||
let mut sub = EventSubscription::<TestRuntime>::new(sub, decoder);
|
||||
sub.filter_event::<TransferEvent<_>>();
|
||||
client.transfer(&alice, &bob, 10_000).await.unwrap();
|
||||
let raw = sub.next().await.unwrap().unwrap();
|
||||
let event = TransferEvent::<TestRuntime>::decode(&mut &raw.data[..]).unwrap();
|
||||
assert_eq!(
|
||||
event,
|
||||
TransferEvent {
|
||||
from: alice.account_id().clone(),
|
||||
to: bob.clone(),
|
||||
amount: 10_000,
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,6 +33,7 @@ pub mod balances;
|
||||
pub mod contracts;
|
||||
pub mod session;
|
||||
pub mod staking;
|
||||
pub mod sudo;
|
||||
pub mod system;
|
||||
|
||||
/// Store trait.
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
// Copyright 2019-2020 Parity Technologies (UK) Ltd.
|
||||
// This file is part of substrate-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 substrate-subxt. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Implements support for the frame_sudo module.
|
||||
|
||||
use crate::{
|
||||
frame::system::{
|
||||
System,
|
||||
SystemEventsDecoder,
|
||||
},
|
||||
Encoded,
|
||||
};
|
||||
use codec::Encode;
|
||||
use core::marker::PhantomData;
|
||||
|
||||
/// The subset of the `frame_sudo::Trait` that a client must implement.
|
||||
#[module]
|
||||
pub trait Sudo: System {}
|
||||
|
||||
/// Execute a transaction with sudo permissions.
|
||||
#[derive(Clone, Debug, Eq, PartialEq, Call, Encode)]
|
||||
pub struct SudoCall<'a, T: Sudo> {
|
||||
/// Runtime marker.
|
||||
pub _runtime: PhantomData<T>,
|
||||
/// Encoded transaction.
|
||||
pub call: &'a Encoded,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::{
|
||||
error::Error,
|
||||
frame::balances::TransferCall,
|
||||
signer::PairSigner,
|
||||
tests::{
|
||||
test_client,
|
||||
TestRuntime,
|
||||
},
|
||||
};
|
||||
use sp_keyring::AccountKeyring;
|
||||
|
||||
#[async_std::test]
|
||||
async fn test_sudo() {
|
||||
env_logger::try_init().ok();
|
||||
let alice = PairSigner::<TestRuntime, _>::new(AccountKeyring::Alice.pair());
|
||||
let (client, _) = test_client().await;
|
||||
|
||||
let call = client
|
||||
.encode(TransferCall {
|
||||
to: &AccountKeyring::Bob.to_account_id(),
|
||||
amount: 10_000,
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
let res = client.sudo_and_watch(&alice, &call).await;
|
||||
assert!(
|
||||
if let Err(Error::BadOrigin) = res {
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
+41
-15
@@ -157,21 +157,6 @@ pub struct SetCodeCall<'a, T: System> {
|
||||
pub code: &'a [u8],
|
||||
}
|
||||
|
||||
/// Event for the System module.
|
||||
#[derive(Clone, Debug, Eq, PartialEq, Decode)]
|
||||
pub enum SystemEvent<T: System> {
|
||||
/// An extrinsic completed successfully.
|
||||
ExtrinsicSuccess(DispatchInfo),
|
||||
/// An extrinsic failed.
|
||||
ExtrinsicFailed(DispatchError, DispatchInfo),
|
||||
/// `:code` was updated.
|
||||
CodeUpdated,
|
||||
/// A new account was created.
|
||||
NewAccount(T::AccountId),
|
||||
/// An account was reaped.
|
||||
KilledAccount(T::AccountId),
|
||||
}
|
||||
|
||||
/// A phase of a block's execution.
|
||||
#[derive(Clone, Debug, Eq, PartialEq, Decode)]
|
||||
pub enum Phase {
|
||||
@@ -180,3 +165,44 @@ pub enum Phase {
|
||||
/// The end.
|
||||
Finalization,
|
||||
}
|
||||
|
||||
/// An extrinsic completed successfully.
|
||||
#[derive(Clone, Debug, Eq, PartialEq, Event, Decode)]
|
||||
pub struct ExtrinsicSuccessEvent<T: System> {
|
||||
/// Runtime marker.
|
||||
pub _runtime: PhantomData<T>,
|
||||
/// The dispatch info.
|
||||
pub info: DispatchInfo,
|
||||
}
|
||||
|
||||
/// An extrinsic failed.
|
||||
#[derive(Clone, Debug, Eq, PartialEq, Event, Decode)]
|
||||
pub struct ExtrinsicFailedEvent<T: System> {
|
||||
/// Runtime marker.
|
||||
pub _runtime: PhantomData<T>,
|
||||
/// The dispatch error.
|
||||
pub error: DispatchError,
|
||||
/// The dispatch info.
|
||||
pub info: DispatchInfo,
|
||||
}
|
||||
|
||||
/// `:code` was updated.
|
||||
#[derive(Clone, Debug, Eq, PartialEq, Event, Decode)]
|
||||
pub struct CodeUpdatedEvent<T: System> {
|
||||
/// Runtime marker.
|
||||
pub _runtime: PhantomData<T>,
|
||||
}
|
||||
|
||||
/// A new account was created.
|
||||
#[derive(Clone, Debug, Eq, PartialEq, Event, Decode)]
|
||||
pub struct NewAccountEvent<T: System> {
|
||||
/// Created account id.
|
||||
pub account: T::AccountId,
|
||||
}
|
||||
|
||||
/// An account was reaped.
|
||||
#[derive(Clone, Debug, Eq, PartialEq, Event, Decode)]
|
||||
pub struct KilledAccountEvent<T: System> {
|
||||
/// Killed account id.
|
||||
pub account: T::AccountId,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user