Prepare phase

The prepare phase is used when access to the private AuthAccount object of signing accounts is required for your transaction.

Direct access to signing accounts is only possible inside the prepare phase.

For each signer of the transaction the signing account is passed as an argument to the prepare phase. For example, if the transaction has three signers, the prepare must have three parameters of type AuthAccount.

 prepare(signer1: AuthAccount) {
      // ...
 }

As a best practice, only use the prepare phase to define and execute logic that requires access to the AuthAccount objects of signing accounts, and move all other logic elsewhere. Modifications to accounts can have significant implications, so keep this phase clear of unrelated logic to ensure users of your contract are able to easily read and understand logic related to their private account objects.

The prepare phase serves a similar purpose as the initializer of a contract/resource/structure.

For example, if a transaction performs a token transfer, put the withdrawal in the prepare phase, as it requires access to the account storage, but perform the deposit in the execute phase.

AuthAccount objects have the permissions to read from and write to the /storage/ and /private/ areas of the account, which cannot be directly accessed anywhere else. They also have the permission to create and delete capabilities that use these areas.

Last updated