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: testnet mode #1057

Merged
merged 38 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
19719aa
useUserAssets testnetMode flag
estebanmino Oct 13, 2023
3866d08
using getSupportedTestnetChainIds to get assets
estebanmino Oct 13, 2023
144babd
testnet state with testnet toggle and testnet shortcut toggle
estebanmino Oct 13, 2023
336ba3e
add shortcut + command k
estebanmino Oct 13, 2023
adc841e
correct testnets chain name
estebanmino Oct 13, 2023
f274c0d
token details on testnet
estebanmino Oct 13, 2023
26ff614
testnet txs
estebanmino Oct 13, 2023
a522e7b
handle testnet mode from more menu
estebanmino Oct 13, 2023
25a40e9
using windowstroke for border
estebanmino Oct 16, 2023
0c61725
testnetbar
estebanmino Oct 16, 2023
6b9f130
animate testnetbar
estebanmino Oct 17, 2023
d2f03d8
empty tokens state when no assets in testmode
estebanmino Oct 17, 2023
f455b5f
background and border color
estebanmino Oct 17, 2023
a13d4f6
testnetbar background svg
estebanmino Oct 17, 2023
38300dc
improve animation
estebanmino Oct 17, 2023
5b462c7
fix height when testnet is enabled
estebanmino Oct 17, 2023
d4428b3
dont show testnetbar while unlocking
estebanmino Oct 17, 2023
1d30f6d
send validations
estebanmino Oct 17, 2023
3e65fc1
handle testing alert for swaps and input heights on send
estebanmino Oct 17, 2023
367e036
make WarningInfo responsive to testnet bar
estebanmino Oct 17, 2023
b80d62a
ui tweaks
estebanmino Oct 17, 2023
5031251
secrets tweaks
estebanmino Oct 17, 2023
3da818e
rmlog
estebanmino Oct 17, 2023
e5311f4
fix activity out of boundaries
estebanmino Oct 17, 2023
d7dbd3d
navigate to tx details
estebanmino Oct 17, 2023
7e4dc8c
fix activity details for testnets
estebanmino Oct 17, 2023
dfefe40
fix some alignments on dapp requests
estebanmino Oct 17, 2023
360cb2c
network switcher testnet mode
estebanmino Oct 18, 2023
e74597d
setting networks label
estebanmino Oct 18, 2023
0959254
ordering testnets
estebanmino Oct 18, 2023
59c3b6e
simulate clock on tabbar when switching to testnet mode
estebanmino Oct 18, 2023
a9b40d2
ApproveAppRequestWrapper
estebanmino Oct 18, 2023
a1c906f
send user to settings from magic menu
estebanmino Oct 19, 2023
a08df3e
left hardhat op out of testnetsid
estebanmino Oct 19, 2023
9c52af2
fix init issue
estebanmino Oct 19, 2023
5114090
one rebase fix
estebanmino Oct 24, 2023
833b515
code review
estebanmino Oct 24, 2023
b6e6cb3
app css
estebanmino Oct 24, 2023
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: 6 additions & 6 deletions src/core/references/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,18 +101,18 @@ export const NATIVE_ASSETS_PER_CHAIN: Record<ChainId, AddressOrEth> = {
[ChainId.goerli]: ETH_ADDRESS,
[ChainId.sepolia]: ETH_ADDRESS,
[ChainId.arbitrum]: ETH_ARBITRUM_ADDRESS as Address,
[ChainId.arbitrumGoerli]: ETH_ARBITRUM_ADDRESS as Address,
[ChainId['arbitrum-goerli']]: ETH_ARBITRUM_ADDRESS as Address,
[ChainId.bsc]: BSC_BNB_ADDRESS as Address,
[ChainId.bscTestnet]: BSC_BNB_ADDRESS as Address,
[ChainId['bsc-testnet']]: BSC_BNB_ADDRESS as Address,
[ChainId.optimism]: ETH_OPTIMISM_ADDRESS as Address,
[ChainId.hardhatOptimism]: ETH_OPTIMISM_ADDRESS as Address,
[ChainId.optimismGoerli]: ETH_OPTIMISM_ADDRESS as Address,
[ChainId['optimism-goerli']]: ETH_OPTIMISM_ADDRESS as Address,
[ChainId.base]: ETH_BASE_ADDRESS as Address,
[ChainId.baseGoerli]: ETH_BASE_ADDRESS as Address,
[ChainId['base-goerli']]: ETH_BASE_ADDRESS as Address,
[ChainId.zora]: ETH_ZORA_ADDRESS as Address,
[ChainId.zoraTestnet]: ETH_ZORA_ADDRESS as Address,
[ChainId['zora-testnet']]: ETH_ZORA_ADDRESS as Address,
[ChainId.polygon]: MATIC_POLYGON_ADDRESS as Address,
[ChainId.polygonMumbai]: MATIC_POLYGON_ADDRESS as Address,
[ChainId['polygon-mumbai']]: MATIC_POLYGON_ADDRESS as Address,
};
export const OVM_GAS_PRICE_ORACLE =
'0x420000000000000000000000000000000000000F';
Expand Down
4 changes: 4 additions & 0 deletions src/core/references/shortcuts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ export const shortcuts = {
display: 'L',
key: 'l',
},
TESTNET_MODE: {
display: 'T',
key: 't',
},
OPEN_MORE_MENU: {
display: '.',
key: '.',
Expand Down
61 changes: 45 additions & 16 deletions src/core/resources/assets/userAssets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
queryClient,
} from '~/core/react-query';
import { SupportedCurrencyKey } from '~/core/references';
import { useConnectedToHardhatStore } from '~/core/state/currentSettings/connectedToHardhat';
import { connectedToHardhatStore } from '~/core/state/currentSettings/connectedToHardhat';
import { useTestnetModeStore } from '~/core/state/currentSettings/testnetMode';
import {
ParsedAssetsDictByChain,
ParsedUserAsset,
Expand All @@ -24,7 +25,10 @@ import {
filterAsset,
parseUserAsset,
} from '~/core/utils/assets';
import { getSupportedChainIds } from '~/core/utils/chains';
import {
getSupportedChainIds,
getSupportedTestnetChainIds,
} from '~/core/utils/chains';
import { greaterThan } from '~/core/utils/numbers';
import { RainbowError, logger } from '~/logger';
import {
Expand All @@ -46,30 +50,42 @@ export const USER_ASSETS_STALE_INTERVAL = 30000;
export type UserAssetsArgs = {
address?: Address;
currency: SupportedCurrencyKey;
testnetMode?: boolean;
};

type SetUserAssetsArgs = {
address?: Address;
currency: SupportedCurrencyKey;
userAssets?: UserAssetsResult;
testnetMode?: boolean;
};

type SetUserDefaultsArgs = {
address?: Address;
currency: SupportedCurrencyKey;
staleTime: number;
testnetMode?: boolean;
};

type FetchUserAssetsArgs = {
address?: Address;
currency: SupportedCurrencyKey;
testnetMode?: boolean;
};

// ///////////////////////////////////////////////
// Query Key

export const userAssetsQueryKey = ({ address, currency }: UserAssetsArgs) =>
createQueryKey('userAssets', { address, currency }, { persisterVersion: 2 });
export const userAssetsQueryKey = ({
address,
currency,
testnetMode,
}: UserAssetsArgs) =>
createQueryKey(
'userAssets',
{ address, currency, testnetMode },
{ persisterVersion: 3 },
);

type UserAssetsQueryKey = ReturnType<typeof userAssetsQueryKey>;

Expand All @@ -79,9 +95,10 @@ type UserAssetsQueryKey = ReturnType<typeof userAssetsQueryKey>;
export const userAssetsFetchQuery = ({
address,
currency,
testnetMode,
}: FetchUserAssetsArgs) => {
queryClient.fetchQuery(
userAssetsQueryKey({ address, currency }),
userAssetsQueryKey({ address, currency, testnetMode }),
userAssetsQueryFunction,
);
};
Expand All @@ -90,32 +107,40 @@ export const userAssetsSetQueryDefaults = ({
address,
currency,
staleTime,
testnetMode,
}: SetUserDefaultsArgs) => {
queryClient.setQueryDefaults(userAssetsQueryKey({ address, currency }), {
staleTime,
});
queryClient.setQueryDefaults(
userAssetsQueryKey({ address, currency, testnetMode }),
{
staleTime,
},
);
};

export const userAssetsSetQueryData = ({
address,
currency,
userAssets,
testnetMode,
}: SetUserAssetsArgs) => {
queryClient.setQueryData(
userAssetsQueryKey({ address, currency }),
userAssetsQueryKey({ address, currency, testnetMode }),
userAssets,
);
};

async function userAssetsQueryFunction({
queryKey: [{ address, currency }],
queryKey: [{ address, currency, testnetMode }],
}: QueryFunctionArgs<typeof userAssetsQueryKey>) {
const cache = queryClient.getQueryCache();
const cachedUserAssets = (cache.find(
userAssetsQueryKey({ address, currency }),
userAssetsQueryKey({ address, currency, testnetMode }),
)?.state?.data || {}) as ParsedAssetsDictByChain;
try {
const url = `/${getSupportedChainIds().join(',')}/${address}/assets`;
const supportedChainIds = testnetMode
? getSupportedTestnetChainIds()
: getSupportedChainIds();
const url = `/${supportedChainIds.join(',')}/${address}/assets`;
const res = await addysHttp.get<AddressAssetsReceivedMessage>(url, {
params: {
currency: currency.toLowerCase(),
Expand All @@ -131,6 +156,7 @@ async function userAssetsQueryFunction({
address,
chainIds: chainIdsWithErrorsInResponse,
currency,
testnetMode,
});
if (assets.length && chainIdsInResponse.length) {
const parsedAssetsDict = await parseUserAssets({
Expand Down Expand Up @@ -164,15 +190,17 @@ async function userAssetsQueryFunctionRetryByChain({
address,
chainIds,
currency,
testnetMode,
}: {
address: Address;
chainIds: ChainId[];
currency: SupportedCurrencyKey;
testnetMode?: boolean;
}) {
try {
const cache = queryClient.getQueryCache();
const cachedUserAssets =
(cache.find(userAssetsQueryKey({ address, currency }))?.state
(cache.find(userAssetsQueryKey({ address, currency, testnetMode }))?.state
?.data as ParsedAssetsDictByChain) || {};
const retries = [];
for (const chainIdWithError of chainIds) {
Expand All @@ -195,7 +223,7 @@ async function userAssetsQueryFunctionRetryByChain({
}
}
queryClient.setQueryData(
userAssetsQueryKey({ address, currency }),
userAssetsQueryKey({ address, currency, testnetMode }),
cachedUserAssets,
);
} catch (e) {
Expand Down Expand Up @@ -237,7 +265,7 @@ export async function parseUserAssets({
}
}
const { connectedToHardhat, connectedToHardhatOp } =
useConnectedToHardhatStore.getState();
connectedToHardhatStore.getState();
if (connectedToHardhat || connectedToHardhatOp) {
// separating out these ternaries for readability
const selectedHardhatChainId = connectedToHardhat
Expand Down Expand Up @@ -303,8 +331,9 @@ export function useUserAssets<TSelectResult = UserAssetsResult>(
UserAssetsQueryKey
> = {},
) {
const { testnetMode } = useTestnetModeStore();
return useQuery(
userAssetsQueryKey({ address, currency }),
userAssetsQueryKey({ address, currency, testnetMode }),
userAssetsQueryFunction,
{
...config,
Expand Down
18 changes: 14 additions & 4 deletions src/core/resources/transactions/consolidatedTransactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import {
queryClient,
} from '~/core/react-query';
import { SupportedCurrencyKey } from '~/core/references';
import { useTestnetModeStore } from '~/core/state/currentSettings/testnetMode';
import { ChainName } from '~/core/types/chains';
import { TransactionsReceivedMessage } from '~/core/types/refraction';
import { RainbowTransaction } from '~/core/types/transactions';
import {
chainIdFromChainName,
getSupportedChainIds,
getSupportedTestnetChainIds,
} from '~/core/utils/chains';
import { parseTransaction } from '~/core/utils/transactions';
import { RainbowError, logger } from '~/logger';
Expand All @@ -29,6 +31,7 @@ const CONSOLIDATED_TRANSACTIONS_TIMEOUT = 20000;
export type ConsolidatedTransactionsArgs = {
address?: string;
currency: SupportedCurrencyKey;
testnetMode?: boolean;
};

// ///////////////////////////////////////////////
Expand All @@ -37,10 +40,11 @@ export type ConsolidatedTransactionsArgs = {
export const consolidatedTransactionsQueryKey = ({
address,
currency,
testnetMode,
}: ConsolidatedTransactionsArgs) =>
createQueryKey(
'consolidatedTransactions',
{ address, currency },
{ address, currency, testnetMode },
{ persisterVersion: 1 },
);

Expand All @@ -54,7 +58,7 @@ type ConsolidatedTransactionsQueryKey = ReturnType<
export async function fetchConsolidatedTransactions<
TSelectData = ConsolidatedTransactionsResult,
>(
{ address, currency }: ConsolidatedTransactionsArgs,
{ address, currency, testnetMode }: ConsolidatedTransactionsArgs,
config: QueryConfig<
ConsolidatedTransactionsResult,
Error,
Expand All @@ -66,6 +70,7 @@ export async function fetchConsolidatedTransactions<
consolidatedTransactionsQueryKey({
address,
currency,
testnetMode,
}),
consolidatedTransactionsQueryFunction,
config,
Expand All @@ -82,14 +87,17 @@ type _QueryResult = {
};

export async function consolidatedTransactionsQueryFunction({
queryKey: [{ address, currency }],
queryKey: [{ address, currency, testnetMode }],
pageParam,
}: QueryFunctionArgs<
typeof consolidatedTransactionsQueryKey
>): Promise<_QueryResult> {
try {
const supportedChainIds = testnetMode
? getSupportedTestnetChainIds()
: getSupportedChainIds();
const response = await addysHttp.get<TransactionsReceivedMessage>(
`/${getSupportedChainIds().join(',')}/${address}/transactions`,
`/${supportedChainIds.join(',')}/${address}/transactions`,
{
params: {
currency: currency.toLowerCase(),
Expand Down Expand Up @@ -154,10 +162,12 @@ export function useConsolidatedTransactions<
TSelectData
> = {},
) {
const { testnetMode } = useTestnetModeStore();
return useInfiniteQuery(
consolidatedTransactionsQueryKey({
address,
currency,
testnetMode,
}),
consolidatedTransactionsQueryFunction,
{
Expand Down
28 changes: 28 additions & 0 deletions src/core/state/currentSettings/testnetMode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import create from 'zustand';

import { createStore } from '~/core/state/internal/createStore';

export interface TestnetModeState {
testnetMode: boolean;
testnetModeShortcutEnabled: boolean;
setTestnetMode: (testnetMode: boolean) => void;
setTestnetModeShortcutEnabled: (testnetModeShortcutEnabled: boolean) => void;
}

export const testnetModeStore = createStore<TestnetModeState>(
(set) => ({
testnetMode: false,
testnetModeShortcutEnabled: false,
setTestnetMode: (testnetMode) => set({ testnetMode }),
setTestnetModeShortcutEnabled: (testnetModeShortcutEnabled) =>
set({ testnetModeShortcutEnabled }),
}),
{
persist: {
name: 'testnetMode',
version: 0,
},
},
);

export const useTestnetModeStore = create(testnetModeStore);
25 changes: 24 additions & 1 deletion src/core/state/userChains/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,21 @@ export interface UserChainsState {
chainId: ChainId;
enabled: boolean;
}) => void;
updateUserChains: ({
chainIds,
enabled,
}: {
chainIds: ChainId[];
enabled: boolean;
}) => void;
updateUserChainsOrder: ({
userChainsOrder,
}: {
userChainsOrder: ChainId[];
}) => void;
}

const chains = SUPPORTED_CHAINS.filter((chain) => !chain.testnet).reduce(
const chains = SUPPORTED_CHAINS.reduce(
(acc, chain) => ({
...acc,
[chain.id]: true,
Expand All @@ -36,6 +43,22 @@ export const userChainsStore = createStore<UserChainsState>(
(set, get) => ({
userChains: chains,
userChainsOrder,
updateUserChains: ({ chainIds, enabled }) => {
const { userChains } = get();
const chainsUpdated = chainIds.reduce(
(acc, chainId) => {
acc[chainId] = enabled;
return acc;
},
{} as Record<ChainId, boolean>,
) satisfies Record<ChainId, boolean>;
set({
userChains: {
...userChains,
...chainsUpdated,
},
});
},
updateUserChain: ({ chainId, enabled }) => {
const { userChains } = get();
set({
Expand Down
Loading
Loading