Skip to content

Commit

Permalink
add tests for rpc client
Browse files Browse the repository at this point in the history
  • Loading branch information
haerdib committed Nov 6, 2023
1 parent 8770206 commit e3c5206
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,10 @@ jobs:
pallet_balances_tests,
pallet_transaction_payment_tests,
state_tests,
tungstenite_client_test,
state_tests,
runtime_update_sync,
runtime_update_async,
ws_client_test,
]
steps:
- uses: actions/checkout@v3
Expand Down
63 changes: 63 additions & 0 deletions testing/examples/tungstenite_client_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
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_core::{
crypto::{Pair, Ss58Codec},
sr25519,
};
use sp_runtime::MultiAddress;
use substrate_api_client::{
ac_node_api::error::{DispatchError, TokenError},
ac_primitives::{AssetRuntimeConfig, ExtrinsicSigner},
extrinsic::BalancesExtrinsics,
rpc::TungsteniteRpcClient,
Api, GetAccountInformation, SubmitAndWatch, XtStatus,
};

fn main() {
// Setup
let alice: sr25519::Pair = Pair::from_string(
"0xe5be9a5092b81bca64be81d212e7f2f9eba183bb7a90954f7b76361f6edb5c0a",
None,
)
.unwrap();
let client = TungsteniteRpcClient::with_default_url(100);
let mut api = Api::<AssetRuntimeConfig, _>::new(client).unwrap();
api.set_signer(ExtrinsicSigner::<AssetRuntimeConfig>::new(alice.clone()));

let bob = sr25519::Public::from_ss58check("5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty")
.unwrap();
let bob_balance = api.get_account_data(&bob.into()).unwrap().unwrap_or_default().free;

// Check for failed extrinsic failed onchain
let xt = api.balance_transfer_allow_death(MultiAddress::Id(bob.into()), bob_balance + 1);
let result = api.submit_and_watch_extrinsic_until(xt.clone(), XtStatus::InBlock);
assert_eq!(result, Err(DispatchError::Token(TokenError::FundsUnavailable)));

// Check directly failed extrinsic (before actually submitted to a block)
let result = api.submit_and_watch_extrinsic_until(xt, XtStatus::InBlock);
assert!(result.is_err());
assert!(format!("{:?}", result).contains("ExtrinsicFailed"));

// Check for successful extrinisc
let xt = api.balance_transfer_allow_death(MultiAddress::Id(bob.into()), bob_balance / 2);
let block_hash = api
.submit_and_watch_extrinsic_until(xt, XtStatus::InBlock)
.unwrap()
.block_hash
.unwrap();
let bob_new_balance = api.get_account_data(&bob.into()).unwrap().unwrap().free;
assert!(bob_new_balance > bob_balance);
}
63 changes: 63 additions & 0 deletions testing/examples/ws_client_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
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_core::{
crypto::{Pair, Ss58Codec},
sr25519,
};
use sp_runtime::MultiAddress;
use substrate_api_client::{
ac_node_api::error::{DispatchError, TokenError},
ac_primitives::{AssetRuntimeConfig, ExtrinsicSigner},
extrinsic::BalancesExtrinsics,
rpc::WsRpcClient,
Api, GetAccountInformation, SubmitAndWatch, XtStatus,
};

fn main() {
// Setup
let alice: sr25519::Pair = Pair::from_string(
"0xe5be9a5092b81bca64be81d212e7f2f9eba183bb7a90954f7b76361f6edb5c0a",
None,
)
.unwrap();
let client = WsRpcClient::with_default_url();
let mut api = Api::<AssetRuntimeConfig, _>::new(client).unwrap();
api.set_signer(ExtrinsicSigner::<AssetRuntimeConfig>::new(alice.clone()));

let bob = sr25519::Public::from_ss58check("5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty")
.unwrap();
let bob_balance = api.get_account_data(&bob.into()).unwrap().unwrap_or_default().free;

// Check for failed extrinsic failed onchain
let xt = api.balance_transfer_allow_death(MultiAddress::Id(bob.into()), bob_balance + 1);
let result = api.submit_and_watch_extrinsic_until(xt.clone(), XtStatus::InBlock);
assert_eq!(result, Err(DispatchError::Token(TokenError::FundsUnavailable)));

// Check directly failed extrinsic (before actually submitted to a block)
let result = api.submit_and_watch_extrinsic_until(xt, XtStatus::InBlock);
assert!(result.is_err());
assert!(format!("{:?}", result).contains("ExtrinsicFailed"));

// Check for successful extrinisc
let xt = api.balance_transfer_allow_death(MultiAddress::Id(bob.into()), bob_balance / 2);
let block_hash = api
.submit_and_watch_extrinsic_until(xt, XtStatus::InBlock)
.unwrap()
.block_hash
.unwrap();
let bob_new_balance = api.get_account_data(&bob.into()).unwrap().unwrap().free;
assert!(bob_new_balance > bob_balance);
}

0 comments on commit e3c5206

Please sign in to comment.