Skip to content

Commit

Permalink
fix: Make sure Cere Wallet address always stays in sync (#261)
Browse files Browse the repository at this point in the history
  • Loading branch information
skambalin authored Jul 30, 2024
1 parent 74f57fd commit db86415
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
18 changes: 17 additions & 1 deletion packages/blockchain/src/Signer/CereWalletSigner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
Expand All @@ -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;
}
}
13 changes: 9 additions & 4 deletions packages/ddc-client/src/DdcClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -201,7 +208,6 @@ export class DdcClient {
*/
async createBucket(clusterId: ClusterId, params: Partial<BucketParams> = {}) {
this.logger.info('Creating bucket on cluster %s', clusterId);

const defaultParams: BucketParams = {
isPublic: false,
};
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit db86415

Please sign in to comment.