Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use internalAccount instead of account address for granular updates #4693

Merged
merged 3 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1104,7 +1104,7 @@ describe('user-storage/user-storage-controller - saveInternalAccountToUserStorag
});

await controller.saveInternalAccountToUserStorage(
MOCK_INTERNAL_ACCOUNTS.ONE[0].address,
MOCK_INTERNAL_ACCOUNTS.ONE[0] as InternalAccount,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just want to make sure, is this assertion valid? If so then all good!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes definitely! It's just that my data doesn't 100% intersect with type InternalAccount since it misses some keys we don't really care about, and I didn't want to bloat up the fixtures.

);

expect(
Expand Down Expand Up @@ -1132,7 +1132,7 @@ describe('user-storage/user-storage-controller - saveInternalAccountToUserStorag
});

await controller.saveInternalAccountToUserStorage(
MOCK_INTERNAL_ACCOUNTS.ONE[0].address,
MOCK_INTERNAL_ACCOUNTS.ONE[0] as InternalAccount,
);

expect(mockAPI.isDone()).toBe(false);
Expand All @@ -1158,7 +1158,7 @@ describe('user-storage/user-storage-controller - saveInternalAccountToUserStorag
});

await controller.saveInternalAccountToUserStorage(
MOCK_INTERNAL_ACCOUNTS.ONE[0].address,
MOCK_INTERNAL_ACCOUNTS.ONE[0] as InternalAccount,
);

expect(mockAPI.isDone()).toBe(true);
Expand Down Expand Up @@ -1186,7 +1186,7 @@ describe('user-storage/user-storage-controller - saveInternalAccountToUserStorag

await expect(
controller.saveInternalAccountToUserStorage(
MOCK_INTERNAL_ACCOUNTS.ONE[0].address,
MOCK_INTERNAL_ACCOUNTS.ONE[0] as InternalAccount,
),
).rejects.toThrow(expect.any(Error));
});
Expand All @@ -1212,7 +1212,7 @@ describe('user-storage/user-storage-controller - saveInternalAccountToUserStorag
);

expect(mockSaveInternalAccountToUserStorage).toHaveBeenCalledWith(
MOCK_INTERNAL_ACCOUNTS.ONE[0].address,
MOCK_INTERNAL_ACCOUNTS.ONE[0],
);
});

Expand All @@ -1237,7 +1237,7 @@ describe('user-storage/user-storage-controller - saveInternalAccountToUserStorag
);

expect(mockSaveInternalAccountToUserStorage).toHaveBeenCalledWith(
MOCK_INTERNAL_ACCOUNTS.ONE[0].address,
MOCK_INTERNAL_ACCOUNTS.ONE[0],
);
});
});
Expand Down Expand Up @@ -1274,7 +1274,6 @@ function mockUserStorageMessenger(options?: {
'NotificationServicesController:selectIsNotificationServicesEnabled',
'AccountsController:listAccounts',
'AccountsController:updateAccountMetadata',
'AccountsController:getAccountByAddress',
'KeyringController:addNewAccount',
],
allowedEvents: [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type {
AccountsControllerListAccountsAction,
AccountsControllerUpdateAccountMetadataAction,
AccountsControllerGetAccountByAddressAction,
AccountsControllerAccountRenamedEvent,
AccountsControllerAccountAddedEvent,
} from '@metamask/accounts-controller';
Expand Down Expand Up @@ -176,7 +175,6 @@ export type AllowedActions =
| NotificationServicesControllerSelectIsNotificationServicesEnabled
// Account syncing
| AccountsControllerListAccountsAction
| AccountsControllerGetAccountByAddressAction
| AccountsControllerUpdateAccountMetadataAction
| KeyringControllerAddNewAccountAction;

Expand Down Expand Up @@ -285,7 +283,7 @@ export default class UserStorageController extends BaseController<
if (this.#accounts.isAccountSyncingInProgress) {
return;
}
await this.saveInternalAccountToUserStorage(account.address);
await this.saveInternalAccountToUserStorage(account);
},
);

Expand All @@ -296,16 +294,10 @@ export default class UserStorageController extends BaseController<
if (this.#accounts.isAccountSyncingInProgress) {
return;
}
await this.saveInternalAccountToUserStorage(account.address);
await this.saveInternalAccountToUserStorage(account);
},
);
},
getInternalAccountByAddress: async (address: string) => {
return this.messagingSystem.call(
'AccountsController:getAccountByAddress',
address,
);
},
getInternalAccountsList: async (): Promise<InternalAccount[]> => {
return this.messagingSystem.call('AccountsController:listAccounts');
},
Expand All @@ -320,21 +312,15 @@ export default class UserStorageController extends BaseController<
null
);
},
saveInternalAccountToUserStorage: async (address: string) => {
const internalAccount = await this.#accounts.getInternalAccountByAddress(
address,
);

if (!internalAccount) {
return;
}

saveInternalAccountToUserStorage: async (
internalAccount: InternalAccount,
) => {
// Map the internal account to the user storage account schema
const mappedAccount =
mapInternalAccountToUserStorageAccount(internalAccount);

await this.performSetStorage(
`accounts.${address}`,
`accounts.${internalAccount.address}`,
JSON.stringify(mappedAccount),
);
},
Expand Down Expand Up @@ -782,7 +768,7 @@ export default class UserStorageController extends BaseController<

if (!userStorageAccount) {
await this.#accounts.saveInternalAccountToUserStorage(
internalAccount.address,
internalAccount,
);
continue;
}
Expand Down Expand Up @@ -812,7 +798,7 @@ export default class UserStorageController extends BaseController<
// Internal account has custom name but user storage account has default name
if (isUserStorageAccountNameDefault) {
await this.#accounts.saveInternalAccountToUserStorage(
internalAccount.address,
internalAccount,
);
continue;
}
Expand All @@ -829,7 +815,7 @@ export default class UserStorageController extends BaseController<

if (isInternalAccountNameNewer) {
await this.#accounts.saveInternalAccountToUserStorage(
internalAccount.address,
internalAccount,
);
continue;
}
Expand All @@ -847,7 +833,7 @@ export default class UserStorageController extends BaseController<
continue;
} else if (internalAccount.metadata.nameLastUpdatedAt !== undefined) {
await this.#accounts.saveInternalAccountToUserStorage(
internalAccount.address,
internalAccount,
);
continue;
}
Expand All @@ -864,17 +850,17 @@ export default class UserStorageController extends BaseController<

/**
* Saves an individual internal account to the user storage.
* @param address - The address of the internal account to save
* @param internalAccount - The internal account to save
*/
async saveInternalAccountToUserStorage(address: string): Promise<void> {
async saveInternalAccountToUserStorage(
internalAccount: InternalAccount,
): Promise<void> {
if (!this.#accounts.canSync()) {
return;
}

try {
this.#assertProfileSyncingEnabled();

await this.#accounts.saveInternalAccountToUserStorage(address);
await this.#accounts.saveInternalAccountToUserStorage(internalAccount);
} catch (e) {
const errorMessage = e instanceof Error ? e.message : JSON.stringify(e);
throw new Error(
Expand Down
Loading