-
Notifications
You must be signed in to change notification settings - Fork 125
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
Add EventReport and failed xt check to examples #615
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
122d6c9
update author tests
haerdib 8163388
fix comment
haerdib f4da3ec
use submit_and_watch_extrinsic_until instead of without_events in exa…
haerdib cc66779
add Txstatus
haerdib 98e4c44
add .clone
haerdib 4fe5840
update examples
haerdib 4a93237
rename files
haerdib bbc1954
fix file comments
haerdib 35181d3
also update custom_nonce
haerdib 1f899ad
fix typos
haerdib 408aee3
fix tests
haerdib c8f2b5d
add some tests
haerdib 208cf2d
fix typo
haerdib File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
/* | ||
Copyright 2019 Supercomputing Systems AG | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
use sp_keyring::AccountKeyring; | ||
use substrate_api_client::{ | ||
ac_node_api::EventDetails, | ||
ac_primitives::{AssetRuntimeConfig, Config, ExtrinsicSigner}, | ||
extrinsic::BalancesExtrinsics, | ||
rpc::JsonrpseeClient, | ||
Api, GetAccountInformation, SubmitAndWatch, TransactionStatus, XtStatus, | ||
}; | ||
|
||
// To test this example with CI we run it against the Substrate kitchensink node, which uses the asset pallet. | ||
// Therefore, we need to use the `AssetRuntimeConfig` in this example. | ||
// ! However, most Substrate runtimes do not use the asset pallet at all. So if you run an example against your own node | ||
// you most likely should use `DefaultRuntimeConfig` instead. | ||
|
||
type Hash = <AssetRuntimeConfig as Config>::Hash; | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
env_logger::init(); | ||
|
||
// Initialize api and set the signer (sender) that is used to sign the extrinsics. | ||
let alice_signer = AccountKeyring::Alice.pair(); | ||
let client = JsonrpseeClient::with_default_url().unwrap(); | ||
let mut api = Api::<AssetRuntimeConfig, _>::new(client).unwrap(); | ||
api.set_signer(ExtrinsicSigner::<AssetRuntimeConfig>::new(alice_signer)); | ||
|
||
let alice = AccountKeyring::Alice.to_account_id(); | ||
let balance_of_alice = api.get_account_data(&alice).unwrap().unwrap().free; | ||
println!("[+] Alice's Free Balance is {balance_of_alice}\n"); | ||
|
||
let bob = AccountKeyring::Bob.to_account_id(); | ||
let balance_of_bob = api.get_account_data(&bob).unwrap().unwrap_or_default().free; | ||
println!("[+] Bob's Free Balance is {balance_of_bob}\n"); | ||
|
||
// First we want to see the events of a failed extrinsic. | ||
// So lets create an extrinsic that will not succeed: | ||
// Alice tries so transfer all her balance, but that will not work, because | ||
// she will not have enough balance left to pay the fees. | ||
let bad_transfer_extrinsic = | ||
api.balance_transfer_allow_death(bob.clone().into(), balance_of_alice); | ||
println!("[+] Composed extrinsic: {bad_transfer_extrinsic:?}\n",); | ||
|
||
// Send and watch extrinsic until InBlock. | ||
let result = api.submit_and_watch_extrinsic_until(bad_transfer_extrinsic, XtStatus::InBlock); | ||
println!("[+] Sent the transfer extrinsic. Result {result:?}"); | ||
|
||
// Check if the transfer really has failed: | ||
match result { | ||
Ok(_report) => { | ||
panic!("Exptected the call to fail."); | ||
}, | ||
Err(e) => { | ||
println!("[+] Couldn't execute the extrinsic due to {e:?}\n"); | ||
let string_error = format!("{e:?}"); | ||
assert!(string_error.contains("FundsUnavailable")); | ||
}, | ||
}; | ||
|
||
// Verify that Bob's free Balance hasn't changed. | ||
let new_balance_of_bob = api.get_account_data(&bob).unwrap().unwrap().free; | ||
println!("[+] Bob's Free Balance is now {}\n", new_balance_of_bob); | ||
assert_eq!(balance_of_bob, new_balance_of_bob); | ||
|
||
// Verify that Alice's free Balance decreased: paid fees. | ||
let new_balance_of_alice = api.get_account_data(&alice).unwrap().unwrap().free; | ||
println!("[+] Alice's Free Balance is now {}\n", new_balance_of_alice); | ||
assert!(balance_of_alice > new_balance_of_alice); | ||
|
||
// Next, we send an extrinsic that should succeed: | ||
let balance_to_transfer = 1000; | ||
let good_transfer_extrinsic = | ||
api.balance_transfer_allow_death(bob.clone().into(), balance_to_transfer); | ||
// Send and watch extrinsic until InBlock. | ||
let result = api.submit_and_watch_extrinsic_until(good_transfer_extrinsic, XtStatus::InBlock); | ||
println!("[+] Sent the transfer extrinsic."); | ||
|
||
// Check if the transfer really was successful: | ||
match result { | ||
Ok(report) => { | ||
let extrinsic_hash = report.extrinsic_hash; | ||
let block_hash = report.block_hash.unwrap(); | ||
let extrinsic_status = report.status; | ||
let extrinsic_events = report.events.unwrap(); | ||
|
||
println!("[+] Extrinsic with hash {extrinsic_hash:?} was successfully executed.",); | ||
println!("[+] Extrinsic got included in block with hash {block_hash:?}"); | ||
println!("[+] Watched extrinsic until it reached the status {extrinsic_status:?}"); | ||
println!("[+] The following events were thrown when the extrinsic was executed: {extrinsic_events:?}"); | ||
|
||
assert!(matches!(extrinsic_status, TransactionStatus::InBlock(_block_hash))); | ||
assert_associated_events_match_expected(extrinsic_events); | ||
}, | ||
Err(e) => { | ||
panic!("Expected the transfer to succeed. Instead, it failed due to {e:?}"); | ||
}, | ||
}; | ||
|
||
// Verify that Bob release has received the transferred amount. | ||
let new_balance_of_bob = api.get_account_data(&bob).unwrap().unwrap().free; | ||
println!("[+] Bob's Free Balance is now {}\n", new_balance_of_bob); | ||
let expected_balance_of_bob = balance_of_bob + balance_to_transfer; | ||
assert_eq!(expected_balance_of_bob, new_balance_of_bob); | ||
} | ||
|
||
fn assert_associated_events_match_expected(events: Vec<EventDetails<Hash>>) { | ||
// First event | ||
assert_eq!(events[0].pallet_name(), "Balances"); | ||
assert_eq!(events[0].variant_name(), "Withdraw"); | ||
|
||
assert_eq!(events[1].pallet_name(), "Balances"); | ||
assert_eq!(events[1].variant_name(), "Transfer"); | ||
|
||
assert_eq!(events[2].pallet_name(), "Balances"); | ||
assert_eq!(events[2].variant_name(), "Deposit"); | ||
|
||
assert_eq!(events[3].pallet_name(), "Treasury"); | ||
assert_eq!(events[3].variant_name(), "Deposit"); | ||
|
||
assert_eq!(events[4].pallet_name(), "Balances"); | ||
assert_eq!(events[4].variant_name(), "Deposit"); | ||
|
||
assert_eq!(events[5].pallet_name(), "TransactionPayment"); | ||
assert_eq!(events[5].variant_name(), "TransactionFeePaid"); | ||
|
||
assert_eq!(events[6].pallet_name(), "System"); | ||
assert_eq!(events[6].variant_name(), "ExtrinsicSuccess"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How do you want to do it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good Input - let me do something about that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #617: I think that should be done in a different PR, as it affects the API. What do you think?
Regarding your point of
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#617 makes sense. Adding the report to the Error gives access to all the information we have collected.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not showing any examples is just a way of forcing the user not to use it. Is that really what we want?
I'm not sure the comments are clear enough. Because we're talking about an error when an extrinsic has failed on the chain, and not an event, the user may think that
submit_and_watch_extrinsic_until_without_event
informs her when an extrinsic fails on the chain. Without studying the code, they may miss this point.Maybe add an example
check_extrinsic_without_events
similar tocheck_extrinsic_events
? We could do it in another issue. Maybe I'm too picky, so it's up to you to decide.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Created a follow up issue in #627