Skip to content
Snippets Groups Projects
Commit 12ac2190 authored by Éloïs's avatar Éloïs
Browse files

fix(accounts): providers should be inc as soon as the account recv money

providers should be incremented as soon as the account receive funds
parent 39fb1d0a
No related branches found
No related tags found
No related merge requests found
...@@ -164,14 +164,12 @@ pub mod pallet { ...@@ -164,14 +164,12 @@ pub mod pallet {
let price = T::NewAccountPrice::get(); let price = T::NewAccountPrice::get();
if account_data.free >= T::ExistentialDeposit::get() + price { if account_data.free >= T::ExistentialDeposit::get() + price {
// The account can pay the new account price, we should: // The account can pay the new account price, we should:
// 1. Increment providers to create the account for frame_system point of view // 1. Withdraw the "new account price" amount
// 2. Withdraw the "new account price" amount // 2. Increment consumers to prevent the destruction of the account before
// 3. Increment consumers to prevent the destruction of the account before
// the random id is assigned // the random id is assigned
// 4. Manage the funds collected // 3. Manage the funds collected
// 5. Submit random id generation request // 4. Submit random id generation request
// 6. Save the id of the random generation request. // 5. Save the id of the random generation request.
frame_system::Pallet::<T>::inc_providers(&account_id);
let res = <pallet_balances::Pallet<T> as Currency<T::AccountId>>::withdraw( let res = <pallet_balances::Pallet<T> as Currency<T::AccountId>>::withdraw(
&account_id, &account_id,
price, price,
...@@ -287,10 +285,9 @@ where ...@@ -287,10 +285,9 @@ where
if !frame_system::Pallet::<T>::account_exists(account_id) { if !frame_system::Pallet::<T>::account_exists(account_id) {
// If the account does not exist, we should program its creation // If the account does not exist, we should program its creation
PendingNewAccounts::<T>::insert(account_id, ()); PendingNewAccounts::<T>::insert(account_id, ());
} else {
// If the account already exists, we should register increment providers directly
frame_system::Pallet::<T>::inc_providers(account_id);
} }
// We should increment the "balances" provider
frame_system::Pallet::<T>::inc_providers(account_id);
} else if was_providing && !is_providing { } else if was_providing && !is_providing {
match frame_system::Pallet::<T>::dec_providers(account_id)? { match frame_system::Pallet::<T>::dec_providers(account_id)? {
frame_system::DecRefStatus::Reaped => return Ok(result), frame_system::DecRefStatus::Reaped => return Ok(result),
......
...@@ -169,16 +169,22 @@ fn test_create_new_account_with_insufficient_balance() { ...@@ -169,16 +169,22 @@ fn test_create_new_account_with_insufficient_balance() {
)); ));
let events = System::events(); let events = System::events();
//println!("{:#?}", events); //println!("{:#?}", events);
assert_eq!(events.len(), 2); assert_eq!(events.len(), 3);
assert_eq!( assert_eq!(
System::events()[0].event, System::events()[0].event,
Event::System(frame_system::Event::NewAccount {
account: AccountKeyring::Eve.to_account_id(),
})
);
assert_eq!(
System::events()[1].event,
Event::Balances(pallet_balances::Event::Endowed { Event::Balances(pallet_balances::Event::Endowed {
account: AccountKeyring::Eve.to_account_id(), account: AccountKeyring::Eve.to_account_id(),
free_balance: 400, free_balance: 400,
}) })
); );
assert_eq!( assert_eq!(
System::events()[1].event, System::events()[2].event,
Event::Balances(pallet_balances::Event::Transfer { Event::Balances(pallet_balances::Event::Transfer {
from: AccountKeyring::Alice.to_account_id(), from: AccountKeyring::Alice.to_account_id(),
to: AccountKeyring::Eve.to_account_id(), to: AccountKeyring::Eve.to_account_id(),
...@@ -234,16 +240,22 @@ fn test_create_new_account() { ...@@ -234,16 +240,22 @@ fn test_create_new_account() {
)); ));
let events = System::events(); let events = System::events();
//println!("{:#?}", events); //println!("{:#?}", events);
assert_eq!(events.len(), 2); assert_eq!(events.len(), 3);
assert_eq!( assert_eq!(
System::events()[0].event, System::events()[0].event,
Event::System(frame_system::Event::NewAccount {
account: AccountKeyring::Eve.to_account_id(),
})
);
assert_eq!(
System::events()[1].event,
Event::Balances(pallet_balances::Event::Endowed { Event::Balances(pallet_balances::Event::Endowed {
account: AccountKeyring::Eve.to_account_id(), account: AccountKeyring::Eve.to_account_id(),
free_balance: 500, free_balance: 500,
}) })
); );
assert_eq!( assert_eq!(
System::events()[1].event, System::events()[2].event,
Event::Balances(pallet_balances::Event::Transfer { Event::Balances(pallet_balances::Event::Transfer {
from: AccountKeyring::Alice.to_account_id(), from: AccountKeyring::Alice.to_account_id(),
to: AccountKeyring::Eve.to_account_id(), to: AccountKeyring::Eve.to_account_id(),
...@@ -256,29 +268,23 @@ fn test_create_new_account() { ...@@ -256,29 +268,23 @@ fn test_create_new_account() {
run_to_block(3); run_to_block(3);
let events = System::events(); let events = System::events();
println!("{:#?}", events); println!("{:#?}", events);
assert_eq!(events.len(), 4); assert_eq!(events.len(), 3);
assert_eq!( assert_eq!(
System::events()[0].event, System::events()[0].event,
Event::System(frame_system::Event::NewAccount {
account: AccountKeyring::Eve.to_account_id(),
})
);
assert_eq!(
System::events()[1].event,
Event::Balances(pallet_balances::Event::Withdraw { Event::Balances(pallet_balances::Event::Withdraw {
who: AccountKeyring::Eve.to_account_id(), who: AccountKeyring::Eve.to_account_id(),
amount: 300, amount: 300,
}) })
); );
assert_eq!( assert_eq!(
System::events()[2].event, System::events()[1].event,
Event::Balances(pallet_balances::Event::Deposit { Event::Balances(pallet_balances::Event::Deposit {
who: Treasury::account_id(), who: Treasury::account_id(),
amount: 300, amount: 300,
}) })
); );
assert_eq!( assert_eq!(
System::events()[3].event, System::events()[2].event,
Event::Treasury(pallet_treasury::Event::Deposit { value: 300 }) Event::Treasury(pallet_treasury::Event::Deposit { value: 300 })
); );
assert_eq!( assert_eq!(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment