Skip to content

Commit

Permalink
fix: remove eth as swappable token (#5)
Browse files Browse the repository at this point in the history
Signed-off-by: ryanml <[email protected]>
  • Loading branch information
ryanml authored Jul 29, 2024
1 parent 37a6f8b commit 85dbef4
Show file tree
Hide file tree
Showing 15 changed files with 79 additions and 31 deletions.
7 changes: 5 additions & 2 deletions src/background/services/tokens/handlers/getTokenList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { TokenManagerService } from '../TokenManagerService';
type HandlerType = ExtensionRequestHandler<
ExtensionRequest.GET_NETWORK_TOKENS,
{ tokens: { [address: string]: NetworkContractToken } },
[number]
[number, string[]]
>;

@injectable()
Expand All @@ -18,7 +18,7 @@ export class GetTokensListHandler implements HandlerType {
constructor(private tokenManagerService: TokenManagerService) {}

handle: HandlerType['handle'] = async ({ request }) => {
const [chainId] = request.params;
const [chainId, disallowedAssets] = request.params;

const [tokens, err] = await resolve<NetworkContractToken[]>(
this.tokenManagerService.getTokensByChainId(chainId)
Expand All @@ -35,6 +35,9 @@ export class GetTokensListHandler implements HandlerType {
...request,
result: {
tokens: tokens.reduce((allTokens, token) => {
if (disallowedAssets.includes(token.symbol)) {
return allTokens;
}
allTokens[token.address] = token;
return allTokens;
}, {}),
Expand Down
6 changes: 5 additions & 1 deletion src/contexts/SwapProvider/SwapProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
SwapContextAPI,
SwapParams,
hasParaswapError,
DISALLOWED_SWAP_ASSETS,
} from './models';

export const SwapContext = createContext<SwapContextAPI>({} as any);
Expand All @@ -51,7 +52,10 @@ export function SwapContextProvider({ children }: { children: any }) {
const { networkFee } = useNetworkFeeContext();
const { captureEncrypted } = useAnalyticsContext();
const { t } = useTranslation();
const tokens = useTokensWithBalances(true);
const tokens = useTokensWithBalances({
forceShowTokensWithoutBalances: true,
disallowedAssets: DISALLOWED_SWAP_ASSETS,
});

const paraswap = useMemo(
() => new ParaSwap(ChainId.AVALANCHE_MAINNET_ID, undefined, new Web3()),
Expand Down
5 changes: 5 additions & 0 deletions src/contexts/SwapProvider/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,8 @@ export type SwapContextAPI = {
approveTxHash: string;
}>;
};

export const DISALLOWED_SWAP_ASSETS: string[] = [
// ETH is disabled in Swaps per issue CP-8409
'ETH',
];
4 changes: 3 additions & 1 deletion src/hooks/useTokenFromParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ export function useTokenFromParams(
withDefault = true
): TokenWithBalance | undefined {
const { search } = useLocation();
const allTokens = useTokensWithBalances(true);
const allTokens = useTokensWithBalances({
forceShowTokensWithoutBalances: true,
});
const [selectedToken, setSelectedToken] = useState<
TokenWithBalance | undefined
>(undefined);
Expand Down
30 changes: 22 additions & 8 deletions src/hooks/useTokensWithBalances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,29 @@ import { ExtensionRequest } from '@src/background/connections/extensionConnectio
import { merge } from 'lodash';
import { getAddressForChain } from '@src/utils/getAddressForChain';

type UseTokensWithBalanceOptions = {
// Requests the tokens WITH and WITHOUT balances
forceShowTokensWithoutBalances?: boolean;
// string array of asset symbols that are to be excluded from the result
disallowedAssets?: string[];
chainId?: number;
};

const bnZero = new BN(0);

const nativeTokensFirst = (tokens: TokenWithBalance[]): TokenWithBalance[] =>
[...tokens].sort((t) => (t.type === TokenType.NATIVE ? -1 : 1));

const DISALLOWED_ASSETS = [];

/**
*
* @param forceShowTokensWithoutBalances - show the tokens WITH and WITHOUT balances
* @param chainId
* @param {UseTokensWithBalanceOptions} options
* @returns Tokens list with OR without balances based on `forceShowTokensWithoutBalances`
*/
export function useTokensWithBalances(
forceShowTokensWithoutBalances?: boolean,
chainId?: number
) {
export const useTokensWithBalances = (
options: UseTokensWithBalanceOptions = {}
) => {
const [selectedChainId, setSelectedChainId] = useState<number | undefined>(
undefined
);
Expand All @@ -46,6 +54,11 @@ export function useTokensWithBalances(
accounts: { active: activeAccount },
} = useAccountsContext();
const { network } = useNetworkContext();
const {
forceShowTokensWithoutBalances = false,
disallowedAssets = DISALLOWED_ASSETS,
chainId = undefined,
} = options;

const customTokensWithZeroBalance: {
[address: string]: TokenWithBalance;
Expand Down Expand Up @@ -85,7 +98,7 @@ export function useTokensWithBalances(
try {
const networkTokens = await request<GetTokensListHandler>({
method: ExtensionRequest.GET_NETWORK_TOKENS,
params: [selectedChainId],
params: [selectedChainId, disallowedAssets],
});

const tokensWithPlaceholderBalances = Object.entries(
Expand Down Expand Up @@ -124,6 +137,7 @@ export function useTokensWithBalances(
forceShowTokensWithoutBalances,
showTokensWithoutBalances,
customTokensWithZeroBalance,
disallowedAssets,
]);

return useMemo<TokenWithBalance[]>(() => {
Expand Down Expand Up @@ -176,4 +190,4 @@ export function useTokensWithBalances(
showTokensWithoutBalances,
allTokensWithPlaceholderBalances,
]);
}
};
4 changes: 3 additions & 1 deletion src/pages/Bridge/hooks/useAssetBalancesEVM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ export function useAssetBalancesEVM(

const { getTokenSymbolOnNetwork } = useGetTokenSymbolOnNetwork();

const tokens = useTokensWithBalances(true);
const tokens = useTokensWithBalances({
forceShowTokensWithoutBalances: true,
});

// For balances on the Avalanche side, for all bridge assets on avalanche
const balances = useMemo(() => {
Expand Down
18 changes: 9 additions & 9 deletions src/pages/Bridge/hooks/useBtcBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,16 @@ export function useBtcBridge(amountInBtc: Big): BridgeAdapter {
const { getNetworkFee } = useNetworkFeeContext();
const { config } = useBridgeConfig();
const { createBridgeTransaction } = useBridgeContext();
const avalancheTokens = useTokensWithBalances(
true,
isDeveloperMode
const avalancheTokens = useTokensWithBalances({
forceShowTokensWithoutBalances: true,
chainId: isDeveloperMode
? ChainId.AVALANCHE_TESTNET_ID
: ChainId.AVALANCHE_MAINNET_ID
);
const btcTokens = useTokensWithBalances(
true,
isDeveloperMode ? ChainId.BITCOIN_TESTNET : ChainId.BITCOIN
);
: ChainId.AVALANCHE_MAINNET_ID,
});
const btcTokens = useTokensWithBalances({
forceShowTokensWithoutBalances: true,
chainId: isDeveloperMode ? ChainId.BITCOIN_TESTNET : ChainId.BITCOIN,
});
const {
accounts: { active: activeAccount },
} = useAccountsContext();
Expand Down
5 changes: 4 additions & 1 deletion src/pages/Bridge/hooks/useLogoUriForBridgeTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ export function useLogoUriForBridgeTransaction(
: ChainId.AVALANCHE_TESTNET_ID;
}

const tokens = useTokensWithBalances(true, chainId);
const tokens = useTokensWithBalances({
forceShowTokensWithoutBalances: true,
chainId,
});

if (!bridgeTransaction || !targetBlockchain) {
return;
Expand Down
2 changes: 1 addition & 1 deletion src/pages/Collectibles/CollectibleSend.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function CollectibleSend() {
accounts: { active },
} = useAccountsContext();
const { captureEncrypted } = useAnalyticsContext();
const tokens = useTokensWithBalances(false);
const tokens = useTokensWithBalances();
const { nft } = useCollectibleFromParams();

const { isFunctionAvailable, isFunctionSupported } = useIsFunctionAvailable(
Expand Down
4 changes: 3 additions & 1 deletion src/pages/ManageTokens/AddToken.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ export function AddToken() {
const { t } = useTranslation();
const { request } = useConnectionContext();
const { network } = useNetworkContext();
const tokens = useTokensWithBalances(true);
const tokens = useTokensWithBalances({
forceShowTokensWithoutBalances: true,
});
const history = useHistory();

const [addressInput, setAddressInput] = useState<string>('');
Expand Down
4 changes: 3 additions & 1 deletion src/pages/ManageTokens/ManageTokensList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ type ManageTokensListProps = {
};

export const ManageTokensList = ({ searchQuery }: ManageTokensListProps) => {
const tokensWithBalances = useTokensWithBalances(true);
const tokensWithBalances = useTokensWithBalances({
forceShowTokensWithoutBalances: true,
});

return (
<Stack
Expand Down
2 changes: 1 addition & 1 deletion src/pages/Send/Send.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export function SendPage() {
accounts: { active },
} = useAccountsContext();
const { captureEncrypted } = useAnalyticsContext();
const tokens = useTokensWithBalances(false);
const tokens = useTokensWithBalances();

const { isFunctionAvailable, isFunctionSupported } = useIsFunctionAvailable(
FunctionNames.SEND
Expand Down
4 changes: 3 additions & 1 deletion src/pages/SignTransaction/SignTransaction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ export function SignTransactionPage() {
const [transactionProgressState, setTransactionProgressState] = useState(
TransactionProgressState.NOT_APPROVED
);
const tokens = useTokensWithBalances(false, network?.chainId);
const tokens = useTokensWithBalances({
chainId: network?.chainId,
});
const header = useSignTransactionHeader(transaction?.displayData);
const isUsingLedgerWallet = useIsUsingLedgerWallet();
const isUsingKeystoneWallet = useIsUsingKeystoneWallet();
Expand Down
10 changes: 8 additions & 2 deletions src/pages/Swap/Swap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { TokenSelect } from '@src/components/common/TokenSelect';
import { useAccountsContext } from '@src/contexts/AccountsProvider';
import { isBitcoinNetwork } from '@src/background/services/network/utils/isBitcoinNetwork';
import { isUserRejectionError } from '@src/utils/errors';
import { DISALLOWED_SWAP_ASSETS } from '@src/contexts/SwapProvider/models';

const ReviewOrderButtonContainer = styled('div')<{
isTransactionDetailsOpen: boolean;
Expand All @@ -60,8 +61,13 @@ export function Swap() {
} = useIsFunctionAvailable(FunctionNames.SWAP);
const history = useHistory();
const theme = useTheme();
const tokensWBalances = useTokensWithBalances();
const allTokensOnNetwork = useTokensWithBalances(true);
const tokensWBalances = useTokensWithBalances({
disallowedAssets: DISALLOWED_SWAP_ASSETS,
});
const allTokensOnNetwork = useTokensWithBalances({
forceShowTokensWithoutBalances: true,
disallowedAssets: DISALLOWED_SWAP_ASSETS,
});
const {
accounts: { active: activeAccount },
} = useAccountsContext();
Expand Down
5 changes: 4 additions & 1 deletion src/pages/Swap/hooks/useSwapStateFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ import { useSendAnalyticsData } from '@src/hooks/useSendAnalyticsData';
import BN from 'bn.js';
import { bnToLocaleString } from '@avalabs/utils-sdk';
import { useSwap } from './useSwap';
import { DISALLOWED_SWAP_ASSETS } from '@src/contexts/SwapProvider/models';

export function useSwapStateFunctions() {
const tokensWBalances = useTokensWithBalances();
const tokensWBalances = useTokensWithBalances({
disallowedAssets: DISALLOWED_SWAP_ASSETS,
});
const { setNavigationHistoryData } = usePageHistory();
const { sendTokenSelectedAnalytics, sendAmountEnteredAnalytics } =
useSendAnalyticsData();
Expand Down

0 comments on commit 85dbef4

Please sign in to comment.