Account State
Low-level routines to interact with the a local Merkle tree
Sync the Local State
To update the account balance local state should be periodically synced with the relayer. Most routines (which are state dependent) have an associated parameter for updating state, but you may want to force update the account state (e.g. using a timer) to reduce sync delays and get balance updates in time.
async updateState(): Promise<boolean>
Returns
Promise
returns boolean indicating whether your local state has pending outgoing transactions
Example
await zkClient.updateState()
console.log('Your local state has been updated');
// output: Your local state has been updated
Check Transaction Sending Ability
You cannot send a new transaction while there is an outgoing one from your account queued by the relayer but not yet mined. Use the following function to check whether you can send a new transaction.
async isReadyToTransact(): Promise<boolean>
Returns
Promise
returns boolean indicating whether you can send a new transaction
Example
const res = await zkClient.isReadyToTransact();
console.log(`You can${res ? '' : 'not'} send a new transaction right now`);
// output: You can send a new transaction right now
Wait for Transaction Sending Ability
When the isReadyToTransact()
method returns false you can wait for the appropriate conditions to send a new transaction using the following method:
async waitReadyToTransact(): Promise<boolean>
Returns
Promise
returns boolean indicating whether you can send a new transaction
Example
if (await zkClient.waitReadyToTransact()) {
console.log('Let\'s send a transaction!');
} else {
console.log('Sorry, I cannot wait anymore');
}
// output: Let's send a transaction!
Get the Local State
Get the local Merkle tree index and root at the desired index
async getLocalState(index?: bigint): Promise<TreeState>
Parameters
index
- the index for which the root should be retrieved (use the latest index if undefined, should be multiple of 128)
Returns
Promise
returns TreeState
Example
const localState = await zkClient.getLocalState();
console.log(`The local state: ${localState.root} @ ${localState.index}`);
// output: The local state:
// 20259123668790783065614334268308370728058669663334716067122266810210516357394 @ 533760
Clean the Local State
Remove all local Merkle tree nodes. After the state is cleared a full sync must be performed for the state to be ready.
public async cleanState(): Promise<void>
Example
await zkClient.cleanState();
Rollback the Local State
Use the following method in to remove all state data after the desired index
async rollbackState(index: bigint): Promise<bigint>
Parameters
index
- the index after which the state should be cleared (should be multiple of 128)
Returns
Promise
returns new local Merkle tree index
Example
const newIndex = await zkClient.rollbackState(528000n);
console.log(`State rollbacked to index ${newIndex}`);
// output: State rollbacked to index 528000
Last updated
Was this helpful?