diff --git a/integration-tests/tests/features/balance_transfer.feature b/integration-tests/features/balance_transfer.feature similarity index 82% rename from integration-tests/tests/features/balance_transfer.feature rename to integration-tests/features/balance_transfer.feature index 42e4b366955390d8e580576d6a9ed9618f697ffd..ecddb1d6655adce4ac683a112edf3c7a4f95d148 100644 --- a/integration-tests/tests/features/balance_transfer.feature +++ b/integration-tests/features/balance_transfer.feature @@ -3,4 +3,4 @@ Feature: Balance transfer Scenario: If alice sends 5 ÄžD to Dave, Dave will get 5 ÄžD Given alice have 10 ÄžD When alice send 5 ÄžD to dave - Then dave have 5 ÄžD + Then dave should have 5 ÄžD diff --git a/integration-tests/tests/features/transfer_all.feature b/integration-tests/features/transfer_all.feature similarity index 83% rename from integration-tests/tests/features/transfer_all.feature rename to integration-tests/features/transfer_all.feature index 1c657eb1f67d4d74a5d22254c05df1fc7d865640..313014ee58fa743b700e3a904acd3802dd38dc1d 100644 --- a/integration-tests/tests/features/transfer_all.feature +++ b/integration-tests/features/transfer_all.feature @@ -3,4 +3,4 @@ Feature: Balance transfer all Scenario: If alice sends all her ÄžDs to Dave, Dave will get 10 ÄžD Given alice have 10 ÄžD When alice sends all her ÄžDs to dave - Then dave have 10 ÄžD + Then dave should have 10 ÄžD diff --git a/integration-tests/tests/common/balances.rs b/integration-tests/tests/common/balances.rs index 672e95d0328aff9a937ce872891b229c3089ff6a..5ed57f80015dd4f7d9dedf5aa4051ad0894bc1d1 100644 --- a/integration-tests/tests/common/balances.rs +++ b/integration-tests/tests/common/balances.rs @@ -15,8 +15,36 @@ // along with Substrate-Libre-Currency. If not, see <https://www.gnu.org/licenses/>. use super::*; +use super::node_runtime::runtime_types::gdev_runtime; +use super::node_runtime::runtime_types::pallet_balances; use sp_keyring::AccountKeyring; -use subxt::PairSigner; +use subxt::{sp_runtime::MultiAddress,PairSigner}; + +pub async fn set_balance( + api: &Api, + client: &Client, + who: AccountKeyring, + amount: u64, +) -> Result<()> { + let _events = create_block_with_extrinsic( + &client, + api + .tx() + .sudo() + .sudo(gdev_runtime::Call::Balances( + pallet_balances::pallet::Call::set_balance { + who: MultiAddress::Id(who.to_account_id()), + new_free: amount, + new_reserved: 0, + }, + )) + .create_signed(&PairSigner::new(SUDO_ACCOUNT.pair()), ()) + .await?, + ) + .await?; + + Ok(()) +} pub async fn transfer( api: &Api, @@ -40,3 +68,49 @@ pub async fn transfer( Ok(()) } + +pub async fn transfer_all( + api: &Api, + client: &Client, + from: AccountKeyring, + to: AccountKeyring, +) -> Result<()> { + let from = PairSigner::new(from.pair()); + let to = to.to_account_id(); + + let _events = create_block_with_extrinsic( + &client, + api + .tx() + .balances() + .transfer_all(to.clone().into(), false) + .create_signed(&from, ()) + .await?, + ) + .await?; + + Ok(()) +} + +pub async fn transfer_ud( + api: &Api, + client: &Client, + from: AccountKeyring, + amount: u64, + to: AccountKeyring, +) -> Result<()> { + let from = PairSigner::new(from.pair()); + let to = to.to_account_id(); + + let _events = create_block_with_extrinsic( + client, + api.tx() + .universal_dividend() + .transfer_ud(to.clone().into(), amount) + .create_signed(&from, ()) + .await?, + ) + .await?; + + Ok(()) +} diff --git a/integration-tests/tests/cucumber_tests.rs b/integration-tests/tests/cucumber_tests.rs index 1d9484bf19569d966f2c5279eaa269b0951055db..03e71c0d738b114ca82cb1d6e52bd5eb5529d151 100644 --- a/integration-tests/tests/cucumber_tests.rs +++ b/integration-tests/tests/cucumber_tests.rs @@ -17,14 +17,11 @@ mod common; use async_trait::async_trait; -use common::node_runtime::runtime_types::gdev_runtime; -use common::node_runtime::runtime_types::pallet_balances; use common::*; use cucumber::{given, then, when, World, WorldInit}; use sp_keyring::AccountKeyring; use std::convert::Infallible; use std::str::FromStr; -use subxt::{sp_runtime::MultiAddress, PairSigner}; #[derive(WorldInit)] pub struct DuniterWorld { @@ -54,75 +51,59 @@ impl World for DuniterWorld { } } -#[given(regex = r"([a-zA-Z]+) have (\d+) ÄžD")] -async fn who_have(world: &mut DuniterWorld, who: String, amount: u64) -> Result<()> { +fn parse_amount(amount: u64, unit: &str) -> (u64, bool) { + match unit { + "ÄžD" => (amount * 100, false), + "cÄžD" => (amount, false), + "UD" => (amount * 1_000, true), + "mUD" => (amount, true), + _ => unreachable!(), + } +} + +#[given(regex = r"([a-zA-Z]+) have (\d+) (ÄžD|cÄžD|UD|mUD)")] +async fn who_have(world: &mut DuniterWorld, who: String, amount: u64, unit: String) -> Result<()> { // Parse inputs let who = AccountKeyring::from_str(&who) - .expect("unknown to") - .to_account_id(); - let amount = amount * 100; + .expect("unknown to"); + let (mut amount, is_ud) = parse_amount(amount, &unit); + + if is_ud { + let current_ud_amount = world.api.storage().universal_dividend().current_ud_storage(None).await?; + amount = (amount * current_ud_amount) / 1_000; + } // Create {amount} ÄžD for {who} - let _events = create_block_with_extrinsic( - &world.client, - world - .api - .tx() - .sudo() - .sudo(gdev_runtime::Call::Balances( - pallet_balances::pallet::Call::set_balance { - who: MultiAddress::Id(who), - new_free: amount, - new_reserved: 0, - }, - )) - .create_signed(&PairSigner::new(SUDO_ACCOUNT.pair()), ()) - .await?, - ) - .await?; + common::balances::set_balance(&world.api, &world.client, who, amount).await?; Ok(()) } -#[when(regex = r"([a-zA-Z]+) send (\d+) ÄžD to ([a-zA-Z]+)")] -async fn transfer(world: &mut DuniterWorld, from: String, amount: u64, to: String) -> Result<()> { +#[when(regex = r"([a-zA-Z]+) send (\d+) (ÄžD|cÄžD|UD|mUD) to ([a-zA-Z]+)")] +async fn transfer(world: &mut DuniterWorld, from: String, amount: u64, unit: String, to: String) -> Result<()> { // Parse inputs let from = AccountKeyring::from_str(&from).expect("unknown from"); - let amount = amount * 100; let to = AccountKeyring::from_str(&to).expect("unknown to"); + let (amount, is_ud) = parse_amount(amount, &unit); - common::balances::transfer(&world.api, &world.client, from, amount, to).await + if is_ud { + common::balances::transfer_ud(&world.api, &world.client, from, amount, to).await + } else { + common::balances::transfer(&world.api, &world.client, from, amount, to).await + } } -#[when(regex = r"([a-zA-Z]+) sends all (?:his|her) ÄžDs? to ([a-zA-Z]+)")] +#[when(regex = r"([a-zA-Z]+) sends all (?:his|her) (?:ÄžDs?|DUs?) to ([a-zA-Z]+)")] async fn send_all_to(world: &mut DuniterWorld, from: String, to: String) -> Result<()> { // Parse inputs - let from = PairSigner::new( - AccountKeyring::from_str(&from) - .expect("unknown from") - .pair(), - ); - let to = AccountKeyring::from_str(&to) - .expect("unknown to") - .to_account_id(); - - let _events = create_block_with_extrinsic( - &world.client, - world - .api - .tx() - .balances() - .transfer_all(to.clone().into(), false) - .create_signed(&from, ()) - .await?, - ) - .await?; + let from = AccountKeyring::from_str(&from).expect("unknown from"); + let to = AccountKeyring::from_str(&to).expect("unknown to"); - Ok(()) + common::balances::transfer_all(&world.api, &world.client, from, to).await } -#[then(regex = r"([a-zA-Z]+) have (\d+) ÄžD")] -async fn assert_who_have(world: &mut DuniterWorld, who: String, amount: u64) -> Result<()> { +#[then(regex = r"([a-zA-Z]+) should have (\d+) ÄžD")] +async fn should_have(world: &mut DuniterWorld, who: String, amount: u64) -> Result<()> { // Parse inputs let who = AccountKeyring::from_str(&who) .expect("unknown to") @@ -134,9 +115,19 @@ async fn assert_who_have(world: &mut DuniterWorld, who: String, amount: u64) -> Ok(()) } +#[then(regex = r"current UD amount should be (\d+).(\d+)")] +async fn current_ud_amount_should_be(world: &mut DuniterWorld, amount: u64, cents: u64) -> Result<()> { + // Parse inputs + let expected_amount = amount + (cents * 100); + + let current_ud_amount = world.api.storage().universal_dividend().current_ud_storage(None).await?; + assert_eq!(current_ud_amount, expected_amount); + Ok(()) +} + #[tokio::main(flavor = "current_thread")] async fn main() { //env_logger::init(); - DuniterWorld::run("tests/features").await + DuniterWorld::run("features").await }