Skip to content

Commit

Permalink
fix: register account waits for it to be synched
Browse files Browse the repository at this point in the history
  • Loading branch information
alexghr committed Nov 6, 2023
1 parent 0026b7e commit cc38e45
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion yarn-project/aztec.js/src/account/manager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
DeployMethod,
WaitOpts,
generatePublicKey,
retryUntil,
} from '../../index.js';
import { AccountContract, Salt } from '../index.js';
import { AccountInterface } from '../interface.js';
Expand Down Expand Up @@ -88,11 +89,13 @@ export class AccountManager {
* Registers this account in the PXE Service and returns the associated wallet. Registering
* the account on the PXE Service is required for managing private state associated with it.
* Use the returned wallet to create Contract instances to be interacted with from this account.
* @param opts - Options to wait for the account to be registered.
* @returns A Wallet instance.
*/
public async register(): Promise<AccountWalletWithPrivateKey> {
public async register(opts: WaitOpts = {}): Promise<AccountWalletWithPrivateKey> {
const completeAddress = await this.getCompleteAddress();
await this.pxe.registerAccount(this.encryptionPrivateKey, completeAddress.partialAddress);
await this.waitSynch(opts);
return this.getWallet();
}

Expand Down Expand Up @@ -142,4 +145,29 @@ export class AccountManager {
await this.deploy().then(tx => tx.wait(opts));
return this.getWallet();
}

/**
* Waits for the account to finish synchronizing with the PXE Service.
* @param opts - Options to wait for account to finish synchronizing
* @returns A wallet instance
*/
public async waitSynch({ interval, timeout }: WaitOpts): Promise<AccountWalletWithPrivateKey> {
const address = (await this.getCompleteAddress()).address;
await retryUntil(
async () => {
const status = await this.pxe.getSyncStatus();
const accountSynchedToBlock = status.notes[address.toString()];
if (!accountSynchedToBlock) {
return false;
} else {
return accountSynchedToBlock >= status.blocks;
}
},
'waitSynch',
interval,
timeout,
);

return this.getWallet();
}
}

0 comments on commit cc38e45

Please sign in to comment.