Skip to content

Commit

Permalink
fix: remove check for bridge/relay source tokens and add validation
Browse files Browse the repository at this point in the history
  • Loading branch information
anondev2323 committed Sep 11, 2023
1 parent 1b5097b commit 0a22028
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 11 deletions.
18 changes: 13 additions & 5 deletions wormhole-connect/src/components/TokensModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ function DisplayTokens(props: {
<div className={classes.tokenRowRight}>
<div className={classes.tokenRowBalanceText}>Balance</div>
<div className={classes.tokenRowBalance}>
{balances[token.key] && walletAddress ? (
{balances && balances[token.key] && walletAddress ? (
<div>{balances[token.key]}</div>
) : chain && walletAddress ? (
<CircularProgress size={14} />
Expand Down Expand Up @@ -266,8 +266,13 @@ function TokensModal(props: Props) {
const [tokens, setTokens] = useState<TokenConfig[]>([]);
const [search, setSearch] = useState('');

const { balances, supportedSourceTokens, supportedDestTokens, route } =
useSelector((state: RootState) => state.transferInput);
const {
balances,
supportedSourceTokens,
supportedDestTokens,
allSupportedDestTokens,
route,
} = useSelector((state: RootState) => state.transferInput);

const supportedTokens = useMemo(() => {
const supported =
Expand Down Expand Up @@ -333,9 +338,12 @@ function TokensModal(props: Props) {
setBalancesLoaded(true);
return;
}

const queryTokens =
type === 'dest' ? allSupportedDestTokens : supportedTokens;
// fetch all N tokens and trigger a single update action
const balancesArr = await Promise.all(
supportedTokens.map(async (t) => {
queryTokens.map(async (t) => {
let balance: BigNumber | null = null;
try {
if (t.tokenId) {
Expand Down Expand Up @@ -447,7 +455,7 @@ function TokensModal(props: Props) {
label: 'All Tokens',
panel: (
<DisplayTokens
tokens={supportedTokens}
tokens={type === 'dest' ? allSupportedDestTokens : supportedTokens}
balances={chainBalancesCache?.balances}
walletAddress={walletAddress}
chain={chain}
Expand Down
9 changes: 9 additions & 0 deletions wormhole-connect/src/store/transferInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export interface TransferInputState {
isTransactionInProgress: boolean;
receiverNativeBalance: string | undefined;
supportedSourceTokens: TokenConfig[];
allSupportedDestTokens: TokenConfig[];
supportedDestTokens: TokenConfig[];
}

Expand Down Expand Up @@ -111,6 +112,7 @@ const initialState: TransferInputState = {
isTransactionInProgress: false,
receiverNativeBalance: '',
supportedSourceTokens: [],
allSupportedDestTokens: [],
supportedDestTokens: [],
};

Expand Down Expand Up @@ -270,6 +272,12 @@ export const transferInputSlice = createSlice({
) => {
state.supportedDestTokens = payload;
},
setAllSupportedDestTokens: (
state: TransferInputState,
{ payload }: PayloadAction<TokenConfig[]>,
) => {
state.allSupportedDestTokens = payload;
},
swapChains: (state: TransferInputState) => {
const tmp = state.fromChain;
state.fromChain = state.toChain;
Expand Down Expand Up @@ -335,6 +343,7 @@ export const {
setIsTransactionInProgress,
setReceiverNativeBalance,
setSupportedDestTokens,
setAllSupportedDestTokens,
setSupportedSourceTokens,
swapChains,
} = transferInputSlice.actions;
Expand Down
8 changes: 4 additions & 4 deletions wormhole-connect/src/utils/routes/baseRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ export abstract class BaseRoute extends RouteAbstract {
sourceChain?: ChainName | ChainId,
): Promise<boolean> {
if (!token) return false;
if (destToken) {
const wrapped = getWrappedToken(token);
return wrapped.key === destToken.key;
}
// if (destToken) {
// const wrapped = getWrappedToken(token);
// return wrapped.key === destToken.key;
// }

if (!sourceChain) return true;
const chainName = wh.toChainName(sourceChain);
Expand Down
9 changes: 7 additions & 2 deletions wormhole-connect/src/utils/transferValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { AnyAction } from '@reduxjs/toolkit';
import { ChainName } from '@wormhole-foundation/wormhole-connect-sdk';

import { BRIDGE_DEFAULTS, CHAINS, TOKENS } from 'config';
import { Route } from 'config/types';
import { Route, TokenConfig } from 'config/types';
import { SANCTIONED_WALLETS } from 'consts/wallet';
import { store } from 'store';
import {
Expand Down Expand Up @@ -74,6 +74,7 @@ export const validateToken = (
export const validateDestToken = (
token: string,
chain: ChainName | undefined,
supportedTokens: TokenConfig[],
): ValidationErr => {
if (!token) return 'Select an asset';
const tokenConfig = TOKENS[token];
Expand All @@ -84,6 +85,9 @@ export const validateDestToken = (
if (!tokenConfig.tokenId && tokenConfig.nativeChain !== chain)
return `${token} not available on ${chain}, select a different token`;
}
if (!supportedTokens.some((t) => t.key === token)) {
return 'No route available for this token, please select another';
}
return '';
};

Expand Down Expand Up @@ -196,6 +200,7 @@ export const validateAll = async (
foreignAsset,
associatedTokenAddress,
route,
supportedDestTokens,
availableRoutes,
} = transferData;
const { maxSwapAmt, toNativeToken } = relayData;
Expand All @@ -210,7 +215,7 @@ export const validateAll = async (
fromChain: validateFromChain(fromChain),
toChain: validateToChain(toChain, fromChain),
token: validateToken(token, fromChain),
destToken: validateDestToken(destToken, toChain),
destToken: validateDestToken(destToken, toChain, supportedDestTokens),
amount: validateAmount(amount, sendingTokenBalance, minAmt),
route: validateRoute(route, availableRoutes),
toNativeToken: '',
Expand Down
7 changes: 7 additions & 0 deletions wormhole-connect/src/views/Bridge/Bridge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
setToken,
setSupportedSourceTokens,
setSupportedDestTokens,
setAllSupportedDestTokens,
setTransferRoute,
TransferInputState,
} from 'store/transferInput';
Expand Down Expand Up @@ -135,6 +136,12 @@ function Bridge() {
toChain,
);
dispatch(setSupportedDestTokens(supported));
const allSupported = await RouteOperator.allSupportedDestTokens(
undefined,
fromChain,
toChain,
);
dispatch(setAllSupportedDestTokens(allSupported));
const selectedIsSupported = isSupportedToken(destToken, supported);
if (!selectedIsSupported) {
dispatch(setDestToken(''));
Expand Down

0 comments on commit 0a22028

Please sign in to comment.