I want to add a check that once a function is called it should never be called again. I want initialization to be one-time only. Is this a good approach if I take a bool value and set it to false after initialization and check if it's false then don't run the function? Or is there a standard or better way?

P.S = In short I want a smart contract once initialized should never be able to initialize again.

1 Answer 1


in short you can create a PDA with a pre-defined seed to store a state when you init. if you are gonna call init again, it will error out because you already created a PDA, which will fail the init check. In anchor, this is done by using account macro 'init' for instance:

        seeds = [
    pub sample: Account<'info, Sample>

if you are try to call this instruction again, anchor will check if the provided account is initialized already. Since your instruction uses the same seed, it will not pass the init check thus this instruction will not be executed.

  • 1
    well, I am using init_if_needed instead of init as I have a scenario where I want to initialize on run time (not right after deployment on mainnet) if it's not initialized. But init_if_needed updates the variables as well. So I guess bool check is the best. Thanks for help. Nov 5, 2022 at 20:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.