Skip to content
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

Handle existential deposit BelowMinimum error #3720

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

chungquantin
Copy link
Contributor

@chungquantin chungquantin commented Mar 17, 2024

Expand the error thrown on specific cases of decrease_balance method

  • TokeError::BelowMinimum if existential deposit case happens. This happens when Precision::Exact is provided and Preservation is Protect or Provided. If the check fails, FundsUnavailable should not be a right message as there might be a fund but it is below the existential deposit balance.

Issue: #2240

polkadot address: 19nSqFQorfF2HxD3oBzWM3oCh4SaCRKWt1yvmgaPYGCo71J

@chungquantin chungquantin requested a review from a team as a code owner March 17, 2024 07:49
Copy link
Contributor

@liamaharon liamaharon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please write in the pr comment each way the method could fail, and the corresponding err you're returning in each case?

Would be nice to also have a conformance test covering each of these cases with a comment explaining the rationale.

@@ -146,7 +146,7 @@ where
Preservation::Preserve,
Fortitude::Polite,
),
Err(TokenError::FundsUnavailable.into()),
Err(TokenError::BelowMinimum.into()),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also add a conformance test for FundsUnavaliable?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I will add one

Comment on lines +188 to +191
if reducible < amount {
if matches!(preservation, Preservation::Protect | Preservation::Preserve) {
return Err(TokenError::BelowMinimum.into());
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why to always return BelowMinimum here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I see, preservation is provided to ensure the balance does not fall below the existential deposit. Hence, if the reducible < amount we want to throw Token::BelowMinimum. There is a test case for this saying Decreasing the balance below the minimum when Precision::Exact should fail..

// Decreasing the balance below the minimum when Precision::Exact should fail.
	let amount = 11.into();
	assert_eq!(
		T::decrease_balance(
			&account_0,
			amount,
			Precision::Exact,
			Preservation::Preserve,
			Fortitude::Polite,
		),
		Err(TokenError::BelowMinimum.into()),
	);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

	/// Hold some funds in an account. If a hold for `reason` is already in place, then this
	/// will increase it.
	fn hold(reason: &Self::Reason, who: &AccountId, amount: Self::Balance) -> DispatchResult {
		// NOTE: This doesn't change the total balance of the account so there's no need to
		// check liquidity.

		Self::ensure_can_hold(reason, who, amount)?;
		// Should be infallible now, but we proceed softly anyway.
>>>>		Self::decrease_balance(who, amount, Exact, Protect, Force)?;
		Self::increase_balance_on_hold(reason, who, amount, BestEffort)?;
		Self::done_hold(reason, who, amount);
		Ok(())
	}

Because method hold() has the method decrease_balance called with Preservation::Protect, if it fails, it must throw TokenError::BelowMinimum instead

@paritytech-review-bot paritytech-review-bot bot requested a review from a team March 21, 2024 10:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants