From db86415cd7494778a967ecdeccc1f2b474e297bf Mon Sep 17 00:00:00 2001 From: Sergey Kambalin Date: Tue, 30 Jul 2024 09:43:57 +0600 Subject: [PATCH] fix: Make sure Cere Wallet address always stays in sync (#261) --- .../blockchain/src/Signer/CereWalletSigner.ts | 18 +++++++++++++++++- packages/ddc-client/src/DdcClient.ts | 13 +++++++++---- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/packages/blockchain/src/Signer/CereWalletSigner.ts b/packages/blockchain/src/Signer/CereWalletSigner.ts index 74e3d823..e2ede7e3 100644 --- a/packages/blockchain/src/Signer/CereWalletSigner.ts +++ b/packages/blockchain/src/Signer/CereWalletSigner.ts @@ -51,6 +51,12 @@ export class CereWalletSigner extends Web3Signer { this.injectedAccount = account; }; + private async syncAccount() { + const { accounts } = await this.extensionPromise; + + await accounts.get().then(this.setAccount); + } + protected async getInjector() { return this.extensionPromise; } @@ -59,7 +65,7 @@ export class CereWalletSigner extends Web3Signer { * @inheritdoc */ async isReady() { - await cryptoWaitReady(); + await Promise.all([this.extensionPromise, cryptoWaitReady()]); if (this.injectedAccount) { return true; @@ -69,12 +75,22 @@ export class CereWalletSigner extends Web3Signer { await this.connect(this.options.connectOptions); } + /** + * Make sure the account is synced with the extension. + */ + await this.syncAccount(); + return true; } async connect(options?: WalletConnectOptions) { await this.wallet.connect(options); + /** + * Make sure the account is synced with the extension after connecting the wallet. + */ + await this.syncAccount(); + return this; } } diff --git a/packages/ddc-client/src/DdcClient.ts b/packages/ddc-client/src/DdcClient.ts index f2417cc6..6add2255 100644 --- a/packages/ddc-client/src/DdcClient.ts +++ b/packages/ddc-client/src/DdcClient.ts @@ -127,7 +127,11 @@ export class DdcClient { * ``` * */ async getBalance() { - return this.blockchain.getAccountFreeBalance(this.signer.address); + this.logger.info('Getting the account balance %s', this.signer.address); + const balance = await this.blockchain.getAccountFreeBalance(this.signer.address); + this.logger.info('The account (%s) balance is %s', this.signer.address, balance); + + return balance; } /** @@ -176,9 +180,12 @@ export class DdcClient { * ``` * */ async getDeposit() { + this.logger.info('Getting the account deposit %s', this.signer.address); const info = await this.blockchain.ddcCustomers.getStackingInfo(this.signer.address); + const deposit = BigInt(info?.active || 0n); + this.logger.info('The account (%s) deposit is %s', this.signer.address, deposit); - return BigInt(info?.active || 0n); + return deposit; } /** @@ -201,7 +208,6 @@ export class DdcClient { */ async createBucket(clusterId: ClusterId, params: Partial = {}) { this.logger.info('Creating bucket on cluster %s', clusterId); - const defaultParams: BucketParams = { isPublic: false, }; @@ -257,7 +263,6 @@ export class DdcClient { async getBucketList() { this.logger.info('Getting bucket list'); const list = await this.blockchain.ddcCustomers.listBuckets(); - this.logger.info('Got bucket list of lenght %s', list.length); return list;