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.
asyncupdateState(): Promise<boolean>
Returns
Promise returns boolean indicating whether your local state has pending outgoing transactions
You cannot send a new transaction while there is an outgoing one from your account queued by the relayer but not yet mined.
Example
awaitzkClient.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.
asyncisReadyToTransact(): Promise<boolean>
The routine is currently identical to updateState() but other conditions can be introduced here in the future
Invoking this function causes a local state update
Returns
Promise returns boolean indicating whether you can send a new transaction
Example
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:
The routine attempts to update the local state periodically within a limited time interval (about 5 minutes). When all of attempts are exhausted the method returns false.
Returns
Promise returns boolean indicating whether you can send a new transaction
Example
Get the Local State
Get the local Merkle tree index and root at the desired index
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
async waitReadyToTransact(): Promise<boolean>
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!
const newIndex = await zkClient.rollbackState(528000n);
console.log(`State rollbacked to index ${newIndex}`);
// output: State rollbacked to index 528000