Usefulness of Transaction fields

In the 3rd tutorial, section “Check Account Balances” when reading the “Transfer Tokens” transaction I’m a little puzzled as to the usefulness of the var temporaryVault: @ExampleToken.Vault.

It seems like the only reason there is a prepare section in this transaction is to initialize self.temporaryVault, but it can be more elegantly created in the execute section before let receiverRef.

Am I right and this transaction can be written more succinctly while being more readable with everything contained in execute or am I missing something?

Thanks!

It seems like the only reason there is a prepare section in this transaction is to initialize self.temporaryVault, but it can be more elegantly created in the execute section before let receiverRef.

Right, the prepare block is used to perform all actions that require access to the signer’s account, here, withdrawal. The vault that is withdrawn, temporaryVault, can only be created by having access to the signer’s account, which is only accessible in the prepare block.

The signer accounts are not available in the execute block, so no, it is not possible to perform everything in just the execute block. An alternative would be to at least get the vaultRef in the prepare block, and move the withdrawal to the execute block, but there’s no functional difference.

The idea here is that actions that are performed on the signer accounts are contained in the prepare block, and all other actions that don’t need access to them are in the execute block. This prevents accidental misuse of the signer accounts. Also, we hope to statically analyze and interpret to the user what actions a transaction is going to take.

2 Likes

Thanks @turbolent this makes total sense.