Skip to content

Commit

Permalink
feat: Add network as criteria of manage token list
Browse files Browse the repository at this point in the history
  • Loading branch information
jinoosss committed Aug 25, 2023
1 parent 41816c3 commit c3362fa
Show file tree
Hide file tree
Showing 32 changed files with 509 additions and 89 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const HISTORY_FETCH_INTERVAL_TIME = 3 * 1000;
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ interface WalletContextProps {
export const WalletContext = createContext<WalletContextProps | null>(null);

export const WalletProvider: React.FC<React.PropsWithChildren<unknown>> = ({ children }) => {
const { walletService, transactionService, balanceService, accountService, chainService, tokenService } =
const { walletService, transactionService, balanceService, accountService, chainService, tokenService, transactionHistoryService } =
useAdenaContext();

const [gnoProvider, setGnoProvider] = useState<GnoProvider>();
Expand Down Expand Up @@ -136,6 +136,8 @@ export const WalletProvider: React.FC<React.PropsWithChildren<unknown>> = ({ chi
accountService.setGnoProvider(gnoProvider);
balanceService.setGnoProvider(gnoProvider);
transactionService.setGnoProvider(gnoProvider);
tokenService.setNetworkMetainfo(networkMetainfo);
transactionHistoryService.setNetworkMetainfo(networkMetainfo);
return networkMetainfo;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe('TransferInput Component', () => {
main: true,
display: false,
tokenId: 'Gnoland',
networkId: 'DEFAULT',
name: 'Gnoland',
image: '',
symbol: 'GNOT',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const Default: StoryObj<TransferInputProps> = {
main: true,
display: false,
tokenId: 'Gnoland',
networkId: 'DEFAULT',
name: 'Gnoland',
image: '',
symbol: 'GNOT',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ describe('TransferSummary Component', () => {
tokenMetainfo: {
main: true,
tokenId: 'Gnoland',
networkId: 'DEFAULT',
name: 'Gnoland',
image: '',
symbol: 'GNOT',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const Default: StoryObj<TransferSummaryProps> = {
main: true,
display: false,
tokenId: 'Gnoland',
networkId: 'DEFAULT',
name: 'Gnoland',
image: '',
symbol: 'GNOT',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import UnknownTokenIcon from '@assets/common-unknown-token.svg';
import { useNetwork } from '@hooks/use-network';
import useScrollHistory from '@hooks/use-scroll-history';
import BigNumber from 'bignumber.js';
import { HISTORY_FETCH_INTERVAL_TIME } from '@common/constants/interval.constant';

const HistoryContainer: React.FC = () => {
const navigate = useNavigate();
Expand Down Expand Up @@ -43,8 +44,8 @@ const HistoryContainer: React.FC = () => {
useEffect(() => {
if (currentAddress) {
const historyFetchTimer = setInterval(() => {
refetch({ refetchPage: (page, index) => index === 0 })
}, 10 * 1000);
refetch({ refetchPage: (_, index) => index === 0 })
}, HISTORY_FETCH_INTERVAL_TIME);
return () => clearInterval(historyFetchTimer);
}
}, [currentAddress, refetch]);
Expand Down Expand Up @@ -76,7 +77,7 @@ const HistoryContainer: React.FC = () => {
};

const fetchTokenHistories = async (pageParam: number) => {
if (!currentAddress || currentNetwork.networkId !== 'test3') {
if (!currentAddress) {
return {
hits: 0,
next: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const ManageTokenSearchContainer: React.FC = () => {
const [isClose, setIsClose] = useState(false);
const { tokenMetainfos } = useTokenMetainfo();
const { currentAccount } = useCurrentAccount();
const { tokenBalances, toggleDisplayOption, updateTokenBalanceInfos } = useTokenBalance();
const { displayTokenBalances: tokenBalances, toggleDisplayOption, updateTokenBalanceInfos } = useTokenBalance();

useEffect(() => {
if (currentAccount) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { useNavigate } from 'react-router-dom';
import { RoutePath } from '@router/path';
import { TransactionHistoryMapper } from '@repositories/transaction/mapper/transaction-history-mapper';
import UnknownTokenIcon from '@assets/common-unknown-token.svg';
import { HISTORY_FETCH_INTERVAL_TIME } from '@common/constants/interval.constant';

const TokenDetailsContainer: React.FC = () => {
const navigate = useNavigate();
Expand Down Expand Up @@ -38,8 +39,8 @@ const TokenDetailsContainer: React.FC = () => {
useEffect(() => {
if (currentAddress) {
const historyFetchTimer = setInterval(() => {
refetch({ refetchPage: (page, index) => index === 0 })
}, 10 * 1000);
refetch({ refetchPage: (_, index) => index === 0 })
}, HISTORY_FETCH_INTERVAL_TIME);
return () => clearInterval(historyFetchTimer);
}
}, [currentAddress, refetch]);
Expand Down
8 changes: 2 additions & 6 deletions packages/adena-extension/src/hooks/use-network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { NetworkState } from '@states/index';
import { EventMessage } from '@inject/message';
import { useCallback } from 'react';
import { useEvent } from './use-event';
import { useTokenMetainfo } from './use-token-metainfo';

interface NetworkResponse {
networks: NetworkMetainfo[];
Expand All @@ -16,6 +17,7 @@ interface NetworkResponse {
const DEFAULT_NETWORK: NetworkMetainfo = {
id: 'test3',
default: true,
main: true,
chainId: 'GNOLAND',
chainName: 'GNO.LAND',
networkId: 'test3',
Expand All @@ -25,12 +27,6 @@ const DEFAULT_NETWORK: NetworkMetainfo = {
gnoUrl: 'https://test3.gno.land',
apiUrl: 'https://api.adena.app',
linkUrl: 'https://gnoscan.io',
token: {
denom: 'gnot',
unit: 1,
minimalDenom: 'ugnot',
minimalUnit: 0.000001,
},
};

export const useNetwork = (): NetworkResponse => {
Expand Down
13 changes: 10 additions & 3 deletions packages/adena-extension/src/hooks/use-token-balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,22 @@ export const useTokenBalance = (): {
}, [getTokenBalancesByAccount, currentAccount]);

const getDisplayTokenBalances = useCallback(() => {
return getCurrentTokenBalances().filter((token) => token.display);
}, [getTokenBalancesByAccount, currentAccount]);
return getCurrentTokenBalances().filter(
(token) => token.networkId === currentNetwork.networkId || token.networkId === 'DEFAULT',
);
}, [getTokenBalancesByAccount, currentAccount?.id, currentNetwork.networkId]);

function matchNetworkId(accountTokenBalance: AccountTokenBalance) {
return accountTokenBalance.networkId === currentNetwork?.id;
}

function matchCurrentAccount(account: Account | null, accountTokenBalance: AccountTokenBalance) {
if (!account) return false;
return accountTokenBalance.accountId === account.id && matchNetworkId(accountTokenBalance);
return (
(accountTokenBalance.accountId === account.id && matchNetworkId(accountTokenBalance)) ||
accountTokenBalance.networkId === 'DEFAULT' ||
accountTokenBalance.networkId === currentNetwork.networkId
);
}

async function toggleDisplayOption(account: Account, token: TokenModel, activated: boolean) {
Expand All @@ -94,6 +100,7 @@ export const useTokenBalance = (): {
return accountTokenBalance;
});
setAccountTokenBalances(changedAccountTokenBalances);
console.log(changedAccountTokenBalances);
await tokenService.updateAccountTokenMetainfos(changedAccountTokenBalances);
}

Expand Down
3 changes: 3 additions & 0 deletions packages/adena-extension/src/hooks/use-token-metainfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useRecoilState } from "recoil";
import { useAdenaContext } from "./use-context";
import { useCurrentAccount } from "./use-current-account";
import { GRC20TokenModel, TokenModel, isGRC20TokenModel, isNativeTokenModel } from "@models/token-model";
import { useNetwork } from "./use-network";

interface GRC20Token {
tokenId: string;
Expand All @@ -17,6 +18,7 @@ export const useTokenMetainfo = () => {
const { balanceService, tokenService } = useAdenaContext();
const [tokenMetainfos, setTokenMetainfo] = useRecoilState(TokenState.tokenMetainfos);
const { currentAccount } = useCurrentAccount();
const { currentNetwork } = useNetwork();

const initTokenMetainfos = async () => {
if (currentAccount) {
Expand Down Expand Up @@ -96,6 +98,7 @@ export const useTokenMetainfo = () => {
const tokenMetainfo: GRC20TokenModel = {
main: false,
tokenId,
networkId: currentNetwork.networkId,
pkgPath: path,
symbol,
type: 'grc20',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export class InjectCore {
public async initGnoProvider() {
try {
const network = await this.chainService.getCurrentNetwork();
this.tokenService.setNetworkMetainfo(network);
this.gnoProvider = new GnoProvider(network.rpcUrl, network.networkId);
this.accountService.setGnoProvider(this.gnoProvider);
this.transactionService.setGnoProvider(this.gnoProvider);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { decryptAES } from 'adena-module';
import { StorageMigration004 } from './storage-migration-v004';

const mockStorageData = {
NETWORKS: [],
CURRENT_CHAIN_ID: '',
CURRENT_NETWORK_ID: '',
SERIALIZED: 'U2FsdGVkX19eI8kOCI/T9o1Ru0b2wdj5rHxmG4QbLQ0yZH4kDa8/gg6Ac2JslvEm',
ENCRYPTED_STORED_PASSWORD: '',
CURRENT_ACCOUNT_ID: '',
ACCOUNT_NAMES: {},
ESTABLISH_SITES: {},
ADDRESS_BOOK: [],
ACCOUNT_TOKEN_METAINFOS: {},
};

describe('serialized wallet migration V003', () => {
it('version', () => {
const migration = new StorageMigration004();
expect(migration.version).toBe(4);
});

it('up success', async () => {
const mockData = {
version: 2,
data: mockStorageData,
};
const migration = new StorageMigration004();
const result = await migration.up(mockData);

expect(result.version).toBe(4);
expect(result.data).not.toBeNull();
expect(result.data.NETWORKS).toEqual([]);
expect(result.data.CURRENT_CHAIN_ID).toBe('');
expect(result.data.CURRENT_NETWORK_ID).toBe('');
expect(result.data.SERIALIZED).toBe(
'U2FsdGVkX19eI8kOCI/T9o1Ru0b2wdj5rHxmG4QbLQ0yZH4kDa8/gg6Ac2JslvEm',
);
expect(result.data.ENCRYPTED_STORED_PASSWORD).toBe('');
expect(result.data.CURRENT_ACCOUNT_ID).toBe('');
expect(result.data.ACCOUNT_NAMES).toEqual({});
expect(result.data.ESTABLISH_SITES).toEqual({});
expect(result.data.ADDRESS_BOOK).toEqual([]);
});

it('up password success', async () => {
const mockData = {
version: 1,
data: mockStorageData,
};
const password = '123';
const migration = new StorageMigration004();
const result = await migration.up(mockData, password);

expect(result.version).toBe(4);
expect(result.data).not.toBeNull();
expect(result.data.NETWORKS).toEqual([]);
expect(result.data.CURRENT_CHAIN_ID).toBe('');
expect(result.data.CURRENT_NETWORK_ID).toBe('');
expect(result.data.SERIALIZED).not.toBe('');
expect(result.data.ENCRYPTED_STORED_PASSWORD).toBe('');
expect(result.data.CURRENT_ACCOUNT_ID).toBe('');
expect(result.data.ACCOUNT_NAMES).toEqual({});
expect(result.data.ESTABLISH_SITES).toEqual({});
expect(result.data.ADDRESS_BOOK).toEqual([]);

const serialized = result.data.SERIALIZED;
const decrypted = await decryptAES(serialized, password);
const wallet = JSON.parse(decrypted);

expect(wallet.accounts).toHaveLength(0);
expect(wallet.keyrings).toHaveLength(0);
});

it('up failed throw error', async () => {
const mockData: any = {
version: 1,
data: { ...mockStorageData, SERIALIZED: null },
};
const migration = new StorageMigration004();

await expect(migration.up(mockData)).rejects.toThrow(
'Stroage Data doesn not match version V003',
);
});
});
Loading

0 comments on commit c3362fa

Please sign in to comment.