description |
---|
Low-level routines to interact with the a local Merkle tree |
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>
Promise
returns boolean indicating whether your local state has pending outgoing transactions
{% hint style="info" %} You cannot send a new transaction while there is an outgoing one from your account queued by the relayer but not yet mined. {% endhint %}
await zkClient.updateState()
console.log('Your local state has been updated');
// output: Your local state has been updated
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>
{% hint style="info" %}
The routine is currently identical to updateState()
but other conditions can be introduced here in the future
{% endhint %}
{% hint style="info" %} Invoking this function causes a local state update {% endhint %}
Promise
returns boolean indicating whether you can send a new transaction
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
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>
{% hint style="info" %}
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.
{% endhint %}
Promise
returns boolean indicating whether you can send a new transaction
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 Merkle tree index and root at the desired index
async getLocalState(index?: bigint): Promise<TreeState>
Related Methods
index
- the index for which the root should be retrieved (use the latest index if undefined, should be multiple of 128)
Promise
returns TreeState
const localState = await zkClient.getLocalState();
console.log(`The local state: ${localState.root} @ ${localState.index}`);
// output: The local state:
// 20259123668790783065614334268308370728058669663334716067122266810210516357394 @ 533760
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>
await zkClient.cleanState();
Use the following method in to remove all state data after the desired index
async rollbackState(index: bigint): Promise<bigint>
index
- the index after which the state should be cleared (should be multiple of 128)
Promise
returns new local Merkle tree index
{% hint style="info" %} In some cases the new Merkle tree index may not match the desired rollback index (e.g. in the case of a partial tree) {% endhint %}
const newIndex = await zkClient.rollbackState(528000n);
console.log(`State rollbacked to index ${newIndex}`);
// output: State rollbacked to index 528000