Cadence transactions use phases to make the transaction's code / intent more readable and to provide a way for developer to separate potentially 'unsafe' account modifying code from regular transaction logic, as well as provide a way to check for error prior / after transaction execution, and abort the transaction if any are found.
The following is a brief summary of how to use the prepare, pre, execute, and post phases in a Cadence transaction.
transaction {
prepare(signer1: AuthAccount) {
// Access signing accounts for this transaction.
//
// Avoid logic that does not need access to signing accounts.
//
// Signing accounts can't be accesed anywhere else in the transaction.
}
pre {
// Define conditions that must be true
// for this transaction to execute.
}
execute {
// The main transaction logic goes here, but you can access
// any public information or resources published by any account.
}
post {
// Define the expected state of things
// as they should be after the transaction executed.
//
// Also used to provide information about what changes
// this transaction will make to accounts in this transaction.
}
}