Account Updates
AccountUpdates are the fundamental building blocks of zkApp transactions. They represent changes to account state and enable complex, multi-account operations within a single transaction. Understanding AccountUpdates is crucial for building sophisticated zkApps.
What are AccountUpdates?
Contrary to what the name might suggest, AccountUpdates are not just about updating account information. They function more like CRUD operations on ZkApp account state.
With ZkApps, you can't simply read state and use it in a circuit. You need to read the state and prove that you read it correctly. That's why an AccountUpdate is required, even for reads.
AccountUpdates are the interface through which all ZkApp interactions on Mina occur. The various helper methods and DSL tools provided by o1js, with respect to smart contracts and on-chain functionality, are all wrappers around AccountUpdates.
- Changes to account balances, and assertions about them
- State updates, and assertions about current state
- Permissions and authorization
- Calling ZkApp methods, including cross-contract calls
- Creating new accounts
- And more...
Basic AccountUpdate Usage
Here's a simple example of creating and using AccountUpdates:
// All AccountUpdates map to a single account - let's generate a random one
const randomPublicKey = PrivateKey.random().toPublicKey();
const au = AccountUpdate.create(randomPublicKey);
// Now we can edit the assertions and updates
// The AccountUpdate is only valid during the first hundred blocks
au.body.preconditions.network.blockchainLength.value = {
lower: UInt32.from(0),
upper: UInt32.from(100),
};
// Pay one Mina from this account
// NOTE: A transaction will fail if the balance changes of all the AccountUpdates do not sum to zero
au.body.balanceChange.sub(UInt64.from(1e9));
// Set the first Field of app state on chain for this account to 1234
au.body.update.appState[0] = {
isSome: Bool(true),
value: Field(1234),
};
API Reference
For detailed information about AccountUpdates, see: AccountUpdate API reference