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

feat: group addresses #1732

Merged
merged 13 commits into from
Sep 15, 2023
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
12 changes: 12 additions & 0 deletions __tests__/migration/contactMigration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ const data: { preference: PreferenceStore; contactBook } = {
walletSavedList: [],
watchAddressPreference: {},
testnetBalanceMap: {},
addressSortStore: {
search: '',
sortType: 'usd',
},
},
contactBook: {
'0x10b26700b0a2d3f5ef12fa250aba818ee3b43bf4': {
Expand Down Expand Up @@ -188,6 +192,10 @@ test('should migrate when no alians', () => {
walletSavedList: [],
watchAddressPreference: {},
testnetBalanceMap: {},
addressSortStore: {
search: '',
sortType: 'usd',
},
},
contactBook: {
'0x10b26700b0a2d3f5ef12fa250aba818ee3b43bf4': {
Expand Down Expand Up @@ -316,6 +324,10 @@ test('should migrate when no contacts', () => {
walletSavedList: [],
watchAddressPreference: {},
testnetBalanceMap: {},
addressSortStore: {
search: '',
sortType: 'usd',
},
},
contactBook: {},
};
Expand Down
6 changes: 5 additions & 1 deletion _raw/locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,11 @@
"backup-seed-phrase": "Backup Seed Phrase",
"delete-all-addresses-but-keep-the-seed-phrase": "Delete all addresses, but keep the seed phrase",
"delete-all-addresses-and-the-seed-phrase": "Delete all addresses and the seed phrase",
"seed-phrase-delete-title": "Delete seed phrase?"
"seed-phrase-delete-title": "Delete seed phrase?",
"sort-by-balance": "Sort by balance",
"sort-by-address-type": "Sort by address type",
"sort-by-address-note": "Sort by address note",
"sort-address": "Sort Address"
},
"dashboard": {
"home": {
Expand Down
6 changes: 5 additions & 1 deletion _raw/locales/zh_CN/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,11 @@
"seed-phrase-delete-title": "删除助记词?",
"update-balance-data": "刷新所有余额",
"watch-address": "观察地址",
"whitelisted-address": "白名单地址"
"whitelisted-address": "白名单地址",
"sort-by-balance": "按地址金额排序",
"sort-by-address-type": "按地址类型排序",
"sort-by-address-note": "按地址备注首字母排序",
"sort-address": "地址排序"
},
"receive": {
"title": "在 {{chain}} 上接收{{token}}",
Expand Down
3 changes: 3 additions & 0 deletions src/background/controller/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1110,6 +1110,9 @@ export class WalletController extends BaseController {
getLastSelectedGasTopUpChain = preferenceService.getLastSelectedGasTopUpChain;
setLastSelectedGasTopUpChain = preferenceService.setLastSelectedGasTopUpChain;

getAddressSortStoreValue = preferenceService.getAddressSortStoreValue;
setAddressSortStoreValue = preferenceService.setAddressSortStoreValue;

getLastSelectedSwapChain = swapService.getSelectedChain;
setLastSelectedSwapChain = swapService.setSelectedChain;
getSwap = swapService.getSwap;
Expand Down
31 changes: 31 additions & 0 deletions src/background/service/preference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,20 @@ export interface PreferenceStore {
autoLockTime?: number;
hiddenBalance?: boolean;
isShowTestnet?: boolean;
addressSortStore: AddressSortStore;
}

export interface AddressSortStore {
search: string;
sortType: 'usd' | 'addressType' | 'alphabet';
lastCurrent?: string;
}

const defaultAddressSortStore: AddressSortStore = {
search: '',
sortType: 'usd',
};

class PreferenceService {
store!: PreferenceStore;
popupOpen = false;
Expand Down Expand Up @@ -133,8 +145,14 @@ class PreferenceService {
collectionStarred: [],
hiddenBalance: false,
isShowTestnet: false,
addressSortStore: {
...defaultAddressSortStore,
},
},
});
//lifetime in background
this.store.addressSortStore = { ...defaultAddressSortStore };

if (
!this.store.locale ||
!LANGS.find((item) => item.code === this.store.locale)
Expand Down Expand Up @@ -691,6 +709,19 @@ class PreferenceService {
resetCurrentCoboSafeAddress = async () => {
this.setCurrentAccount(this.currentCoboSafeAddress ?? null);
};

getAddressSortStoreValue = (key: keyof AddressSortStore) =>
this.store.addressSortStore[key];

setAddressSortStoreValue = <K extends keyof AddressSortStore>(
key: K,
value: AddressSortStore[K]
) => {
this.store.addressSortStore = {
...this.store.addressSortStore,
[key]: value,
};
};
}

export default new PreferenceService();
2 changes: 1 addition & 1 deletion src/constant/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ export const KEYRING_PURPLE_LOGOS = {
};

export const KEYRINGS_LOGOS = {
[KEYRING_CLASS.MNEMONIC]: LogoMnemonic,
[KEYRING_CLASS.MNEMONIC]: IconMnemonicWhite,
[KEYRING_CLASS.PRIVATE_KEY]: LogoPrivateKey,
[KEYRING_CLASS.WATCH]: IconWatchWhite,
[HARDWARE_KEYRING_TYPES.BitBox02.type]: IconBitBox02WithBorder,
Expand Down
5 changes: 5 additions & 0 deletions src/ui/assets/address/checked.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions src/ui/assets/address/sort-by-alphabet.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions src/ui/assets/address/sort-by-type.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions src/ui/assets/address/sort-by-usd-l.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions src/ui/assets/address/sort-by-usd.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 8 additions & 2 deletions src/ui/assets/walletlogo/IconMnemonic-white.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 29 additions & 4 deletions src/ui/models/accountToDisplay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import { RootModel } from '.';
import { DisplayedKeryring } from '@/background/service/keyring';
import { sortAccountsByBalance } from '../utils/account';
import PQueue from 'p-queue';
import { TotalBalanceResponse } from '@/background/service/openapi';

type IDisplayedAccount = Required<DisplayedKeryring['accounts'][number]>;
export type IDisplayedAccountWithBalance = IDisplayedAccount & {
balance: number;
byImport?: boolean;
publicKey?: string;
hdPathBasePublicKey?: string;
hdPathType?: string;
};

type IState = {
Expand Down Expand Up @@ -42,7 +45,6 @@ export const accountToDisplay = createModel<RootModel>()({
store.app.wallet.getAllVisibleAccounts(),
store.app.wallet.getAllAlianNameByMap(),
]);

const result = await Promise.all<IDisplayedAccountWithBalance>(
displayedKeyrings
.map((item) => {
Expand All @@ -60,9 +62,30 @@ export const accountToDisplay = createModel<RootModel>()({
})
.flat(1)
.map(async (item) => {
let balance = await store.app.wallet.getAddressCacheBalance(
item?.address
);
let balance: TotalBalanceResponse | null = null;

let accountInfo = {} as {
hdPathBasePublicKey?: string;
hdPathType?: string;
};

await Promise.allSettled([
store.app.wallet.getAddressCacheBalance(item?.address),
store.app.wallet.requestKeyring(
item.type,
'getAccountInfo',
null,
item.address
),
]).then(([res1, res2]) => {
if (res1.status === 'fulfilled') {
balance = res1.value;
}
if (res2.status === 'fulfilled') {
accountInfo = res2.value;
}
});

if (!balance) {
balance = {
total_usd_value: 0,
Expand All @@ -72,6 +95,8 @@ export const accountToDisplay = createModel<RootModel>()({
return {
...item,
balance: balance?.total_usd_value || 0,
hdPathBasePublicKey: accountInfo?.hdPathBasePublicKey,
hdPathType: accountInfo?.hdPathType,
};
})
);
Expand Down
21 changes: 20 additions & 1 deletion src/ui/models/preference.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { createModel } from '@rematch/core';
import { RootModel } from '.';
import { TokenItem } from 'background/service/openapi';
import { GasCache, addedToken } from 'background/service/preference';
import {
AddressSortStore,
GasCache,
addedToken,
} from 'background/service/preference';
import { CHAINS_ENUM } from 'consts';
import i18n from '@/i18n';

Expand All @@ -22,6 +26,7 @@ interface PreferenceState {
autoLockTime: number;
hiddenBalance: boolean;
isShowTestnet: boolean;
addressSortStore: AddressSortStore;
}

export const preference = createModel<RootModel>()({
Expand All @@ -44,6 +49,7 @@ export const preference = createModel<RootModel>()({
autoLockTime: 0,
hiddenBalance: false,
isShowTestnet: false,
addressSortStore: {} as AddressSortStore,
} as PreferenceState,

reducers: {
Expand Down Expand Up @@ -179,6 +185,19 @@ export const preference = createModel<RootModel>()({
dispatch.preference.getPreference('locale');
},

async getAddressSortStoreValue(key: keyof AddressSortStore, store?) {
const value = await store.app.wallet.getAddressSortStoreValue(key);
return value;
},

async setAddressSortStoreValue<K extends keyof AddressSortStore>(
{ key, value }: { key: K; value: AddressSortStore[K] },
store?
) {
await store.app.wallet.setAddressSortStoreValue(key, value);
dispatch.preference.getPreference('addressSortStore');
},

// async setOpenapiHost(value: string, store?) {
// dispatch.preference.setField({
// isShowTestnet: value,
Expand Down
Loading