Skip to content

Conversation

@Krayt78
Copy link
Contributor

@Krayt78 Krayt78 commented Oct 16, 2025

This PR migrates the lottery pallet from the Currency trait to the fungible traits
There doesn't seem to be a need for runtime migration
This is part of the umbrella task #226

@Krayt78 Krayt78 requested a review from a team as a code owner October 16, 2025 21:19
Copy link
Contributor

@gui1117 gui1117 left a comment

Choose a reason for hiding this comment

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

LGTM, but you can remove the migration, I don't see anything to migrate here.

&winner,
lottery_balance,
KeepAlive,
Preservation::Preserve,
Copy link
Member

@ggwpez ggwpez Dec 16, 2025

Choose a reason for hiding this comment

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

I think the intention is more a transfer_all instead of manually getting the balance and transferring it.
Then you could also remove the pot function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah good call !

I also had a question regarding the pallet account.
Technically speaking if we dont want the pallet account to be deleted it needs to have ED right ? or is there some specificity to this that makes it ignore ED ?

Cause if it needs ED then either we need to give him ED from the start or reduce the first pot by ED amount to compensate.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unless i am mistaken I dont think there is currently a transfer_all type function in fungibles mutate
and the transfer_all function from pallet_balances does a pretty similar logic. I just need to be sure about the ED situation with the internal account to know if i keep alive or not.


assert_eq!(Balances::free_balance(&1), 100);
assert_eq!(
Balances::reducible_balance(&1, Preservation::Expendable, Fortitude::Polite),
Copy link
Member

Choose a reason for hiding this comment

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

Can you check that the payout still works when there is an additional consumer, provider or sufficient reference on the account?
Just to check that nobody can block the payout i this case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for this comment ! I had no idea of the existance of these so now i understand that you can technically have an account doesnt get reaped even tho he doesnt have ED !

Also do you mean adding a reference/consumer to the pallet account or the winner's account ?
I dont see how it would be an issue on the winner but i can see how it can be on the pallet account where the funds are held.

With all these info i have a new question !
For the sake of optimization (atm we delete the account by draining it of its funds and then recreating and minting so ED in it every lottery) wouldnt it be better to just give this lottery account some sufficient reference ? that way no need to detroy/mint again everytime ... that could even help in indexing or some external monitoring.

@Krayt78
Copy link
Contributor Author

Krayt78 commented Dec 19, 2025

/cmd label T2-pallets

@Krayt78
Copy link
Contributor Author

Krayt78 commented Dec 19, 2025

/cmd fmt

@paritytech-cmd-bot-polkadot-sdk paritytech-cmd-bot-polkadot-sdk bot added the T2-pallets This PR/Issue is related to a particular pallet. label Dec 19, 2025
@Krayt78
Copy link
Contributor Author

Krayt78 commented Dec 19, 2025

/cmd bench

@github-actions
Copy link
Contributor

Command "bench" has started 🚀 See logs here

@bkchr
Copy link
Member

bkchr commented Dec 22, 2025

/cmd bench --pallet pallet_lottery --runtime kitchensink-runtime

@bkchr
Copy link
Member

bkchr commented Dec 22, 2025

@Krayt78 still misses a prdoc.

@github-actions
Copy link
Contributor

Command "bench --pallet pallet_lottery --runtime kitchensink-runtime" has started 🚀 See logs here

@github-actions
Copy link
Contributor

Command "bench --pallet pallet_lottery --runtime kitchensink-runtime" has failed ❌! See logs here

@Krayt78
Copy link
Contributor Author

Krayt78 commented Dec 24, 2025

/cmd prdoc

Comment on lines 380 to 381
if frame_system::Pallet::<T>::sufficients(&lottery_account) == 0 {
frame_system::Pallet::<T>::inc_sufficients(&lottery_account);
Copy link
Member

@shawntabrizi shawntabrizi Dec 27, 2025

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 you increment only when sufficient is 0.

I would think this pallet add a reference counter when the lottery is started, and removes it when it is finished.

Otherwise, you do not know when that sufficient reference counter would be added or removed, since it is managed by other runtime logic

Copy link
Contributor Author

Choose a reason for hiding this comment

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

misunderstanding+ oversight on my part ! Thanks Shawn :)

Copy link
Member

Choose a reason for hiding this comment

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

looking closer to your intention, if the goal is to keep the lottery account alive, perhaps reference counters are not the right approach. Treasury uses logic like:

https://github.com/paritytech/polkadot-sdk/blob/master/substrate/frame/treasury/src/lib.rs#L999

Which ensures the lottery account is always in a non-zero balance.

Also using transfer functions like keep alive make sense to me.

I am not quite sure what @ggwpez is looking for with regard to behaviors with the reference counters.

…ely. Added cleanup for sufficient references when the lottery ends and streamlined the logic for incrementing sufficient references on lottery start.
@Krayt78
Copy link
Contributor Author

Krayt78 commented Dec 27, 2025

Context from a discussion with @shawntabrizi regarding this crate

I am exploring ways to keep the pallet’s account alive in order to avoid triggering account creation and deletion on every lottery round.

Previously, this was handled by minting the Existential Deposit (ED) into the account whenever its balance dropped below ED. This approach can be problematic on chains with a fixed or limited token supply.

This leaves us with two main alternatives:

  1. Use reference counters (providers / consumers / sufficients) to prevent the account from being reaped.

    • Downside: it is non-trivial to reliably clean up those reference counters when the pallet needs to be removed or disabled (only via a on_runtime_upgrade hook ?). And another pallet could remove the counter due to no ownership tracking of reference counters.
  2. Fund the account with ED from the first lottery payout, ensuring it always stays above ED.

    • Downside: this effectively reduces the first winner’s payout by ED, which feels slightly unfair (even if the amount is negligible).

At the moment, this is a tradeoff between system cleanliness and simplicity, and there is no single “perfect” solution, only different design choices with different drawbacks.

would love your opinion on the matter @ggwpez !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

T2-pallets This PR/Issue is related to a particular pallet.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants