'Close an account and return what was transferred to several accounts

I am working on the Anchor and Solana framework. I have the following problem:

  • I have a raffle. The giveaway ends if it is canceled by whoever created it or all the pieces are sold.
  • Users (accounts) can buy zero, one or several pieces. each time a part is purchased, the user's Pubkey is stored in a list, along with the part they purchased.
  • Only who defines the draw, receives the money from him when the draw ends. here I am making use of smart contracts and PDA.
  • If the draw is cancelled, the money must be returned to each participant and the ownership of each piece reverts to its original owner, that is, who defined the draw.

My question is: how can I program the last condition?

  1. Using the close function, do I close the PDA accounts and automatically return the statuses of the user accounts to the way they were?
  2. Using the Pubkey of each user, I search for the associated account and make a transfer from the PDA to each account?


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source