Skip to content

Commit daddb4b

Browse files
committed
refactor: make wallet/account initialization sync
1 parent f35d4d8 commit daddb4b

File tree

2 files changed

+8
-36
lines changed

2 files changed

+8
-36
lines changed

packages/multichain-account-api/src/api.ts

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ export class MultichainAccountAdapter implements MultichainAccount {
111111

112112
readonly #providers: AccountProvider[];
113113

114-
#accounts: InternalAccount[];
114+
readonly #accounts: InternalAccount[];
115115

116116
constructor({
117117
groupIndex,
@@ -127,9 +127,7 @@ export class MultichainAccountAdapter implements MultichainAccount {
127127
this.#wallet = wallet;
128128
this.#providers = providers;
129129
this.#accounts = [];
130-
}
131130

132-
async init(): Promise<void> {
133131
let accounts: InternalAccount[] = [];
134132

135133
for (const provider of this.#providers) {
@@ -144,18 +142,6 @@ export class MultichainAccountAdapter implements MultichainAccount {
144142
this.#accounts = accounts;
145143
}
146144

147-
static async from(args: {
148-
groupIndex: number;
149-
wallet: MultichainAccountWallet;
150-
providers: AccountProvider[];
151-
}): Promise<MultichainAccount> {
152-
const multichainAccount = new MultichainAccountAdapter(args);
153-
154-
await multichainAccount.init();
155-
156-
return multichainAccount;
157-
}
158-
159145
get id(): MultichainAccountId {
160146
return this.#id;
161147
}
@@ -269,11 +255,8 @@ export class MultichainAccountWalletAdapter implements MultichainAccountWallet {
269255
this.#id = toMultichainAccountWalletId(entropySource);
270256
this.#providers = providers;
271257
this.#entropySource = entropySource;
272-
273258
this.#accounts = new Map();
274-
}
275259

276-
async init(): Promise<void> {
277260
let index = 0;
278261
let hasAccounts = false;
279262

@@ -292,7 +275,7 @@ export class MultichainAccountWalletAdapter implements MultichainAccountWallet {
292275
});
293276

294277
if (hasAccounts) {
295-
const multichainAccount = await MultichainAccountAdapter.from({
278+
const multichainAccount = new MultichainAccountAdapter({
296279
groupIndex,
297280
wallet: this,
298281
providers: this.#providers,
@@ -317,10 +300,8 @@ export class MultichainAccountWalletAdapter implements MultichainAccountWallet {
317300
return Array.from(this.#accounts.values()); // TODO: Prevent copy here.
318301
}
319302

320-
async #createMultichainAccount(
321-
groupIndex: number,
322-
): Promise<MultichainAccount> {
323-
const multichainAccount = await MultichainAccountAdapter.from({
303+
#createMultichainAccount(groupIndex: number): MultichainAccount {
304+
const multichainAccount = new MultichainAccountAdapter({
324305
wallet: this,
325306
providers: this.#providers,
326307
groupIndex,
@@ -357,7 +338,7 @@ export class MultichainAccountWalletAdapter implements MultichainAccountWallet {
357338
// Re-create and "refresh" the multichain account (we assume all account creations are
358339
// idempotent, so we should get the same accounts and potentially some new accounts (if
359340
// some account providers decide to return more of them this time).
360-
return await this.#createMultichainAccount(groupIndex);
341+
return this.#createMultichainAccount(groupIndex);
361342
}
362343

363344
async createNextMultichainAccount(): Promise<MultichainAccount> {
@@ -404,9 +385,7 @@ export class MultichainAccountWalletAdapter implements MultichainAccountWallet {
404385
}
405386

406387
// We've got all the accounts now, we can create our multichain account.
407-
multichainAccounts.push(
408-
await this.#createMultichainAccount(groupIndex),
409-
);
388+
multichainAccounts.push(this.#createMultichainAccount(groupIndex));
410389

411390
// We have accounts, we need to check the next index.
412391
groupIndex += 1;

packages/multichain-account-api/src/index.test.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -224,14 +224,11 @@ async function setupMultichainAccount({
224224
groupIndex?: number;
225225
providers?: AccountProvider[];
226226
}): Promise<MultichainAccount> {
227-
const multichainAccount = new MultichainAccountAdapter({
227+
return new MultichainAccountAdapter({
228228
wallet,
229229
groupIndex,
230230
providers,
231231
});
232-
233-
await multichainAccount.init();
234-
return multichainAccount;
235232
}
236233

237234
async function setupMultichainAccountWallet({
@@ -242,14 +239,10 @@ async function setupMultichainAccountWallet({
242239
providers?: AccountProvider[];
243240
init?: boolean;
244241
} = {}): Promise<MultichainAccountWallet> {
245-
const wallet = new MultichainAccountWalletAdapter({
242+
return new MultichainAccountWalletAdapter({
246243
providers,
247244
entropySource,
248245
});
249-
250-
await wallet.init();
251-
252-
return wallet;
253246
}
254247

255248
describe('index', () => {

0 commit comments

Comments
 (0)