Skip to content

Commit

Permalink
Running eslint in package.json and fixing exhaustive-deps
Browse files Browse the repository at this point in the history
Signed-off-by: Emre Bogazliyanlioglu <emre@wormholelabs.xyz>
  • Loading branch information
emreboga committed Oct 31, 2024
1 parent c09e303 commit b9a82f3
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 93 deletions.
9 changes: 5 additions & 4 deletions wormhole-connect/.eslintrc.strict.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
"prettier"
],
"rules": {
"comma-dangle": ["error", "always-multiline"],
"semi": ["error", "always"],
"@typescript-eslint/no-explicit-any": ["error", { "ignoreRestArgs": true }],
"@typescript-eslint/explicit-module-boundary-types": ["error"],
"@typescript-eslint/no-explicit-any": ["error", { "ignoreRestArgs": true }],
"@typescript-eslint/no-non-null-assertion": ["error"],
"@typescript-eslint/no-unused-vars": ["warn", { "args": "none" }],
"comma-dangle": ["error", "always-multiline"],
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": ["warn", { "args": "none" }]
"react-hooks/exhaustive-deps": ["error"],
"semi": ["error", "always"]
}
}
1 change: 1 addition & 0 deletions wormhole-connect/src/hooks/useComputeFees.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ const useComputeFees = (props: Props): returnProps => {
sourceChain,
destChain,
tokenPrices,
getEstimatedTime,
]);

return {
Expand Down
3 changes: 2 additions & 1 deletion wormhole-connect/src/hooks/useComputeSourceTokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ const useComputeSourceTokens = (props: Props): ReturnProps => {
active = false;
};
// IMPORTANT: do not include token in dependency array
}, [route, sourceChain, destToken, dispatch]);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [destChain, destToken, dispatch, route, sourceChain]);

return { isFetching };
};
Expand Down
2 changes: 1 addition & 1 deletion wormhole-connect/src/hooks/useFetchRedeemTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const useFetchRedeemTx = (): void => {
return () => {
isActive = false;
};
}, [receipt]);
}, [dispatch, receipt]);
};

export default useFetchRedeemTx;
2 changes: 1 addition & 1 deletion wormhole-connect/src/hooks/useGetTokenBalances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ const useGetTokenBalances = (
return () => {
isActive = false;
};
}, [cachedBalances, walletAddress, chain, tokens]);
}, [cachedBalances, chain, dispatch, tokens, walletAddress]);

return { isFetching, balances };
};
Expand Down
11 changes: 10 additions & 1 deletion wormhole-connect/src/hooks/useRoutesQuotesBulk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ const useRoutesQuotesBulk = (routes: string[], params: Params): HookReturn => {
}
};
}, [
// eslint-disable-next-line react-hooks/exhaustive-deps
routes.join(),
params.sourceChain,
params.sourceToken,
Expand All @@ -110,6 +111,9 @@ const useRoutesQuotesBulk = (routes: string[], params: Params): HookReturn => {
params.nativeGas,
nonce,
isTransactionInProgress,
params,
routes,
refreshTimeout,
]);

const quotesMap = useMemo(
Expand All @@ -118,7 +122,12 @@ const useRoutesQuotesBulk = (routes: string[], params: Params): HookReturn => {
acc[route] = quotes[index];
return acc;
}, {} as Record<string, QuoteResult | undefined>),
[routes.join(), quotes],
[
// eslint-disable-next-line react-hooks/exhaustive-deps
routes.join(),
routes,
quotes,
],
);

// Filter out quotes that would result in a large instant loss
Expand Down
12 changes: 9 additions & 3 deletions wormhole-connect/src/hooks/useSortedRoutesWithQuotes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export const useSortedRoutesWithQuotes = (): HookReturn => {
destToken,
nativeGas: toNativeToken,
}),
[parseFloat(amount), fromChain, token, toChain, destToken, toNativeToken],
[amount, fromChain, token, toChain, destToken, toNativeToken],
);

const { quotesMap, isFetching } = useRoutesQuotesBulk(
Expand Down Expand Up @@ -122,7 +122,7 @@ export const useSortedRoutesWithQuotes = (): HookReturn => {
// Returning BigInt results in TypeError
return Number(destAmountB - destAmountA);
});
}, [routesWithQuotes]);
}, [preferredRouteName, routesWithQuotes]);

const sortedRoutes = useMemo(
() => sortedRoutesWithQuotes.map((r) => r.route),
Expand All @@ -137,6 +137,12 @@ export const useSortedRoutesWithQuotes = (): HookReturn => {
quotesMap,
isFetchingQuotes: isFetching,
}),
[supportedRoutes, sortedRoutesWithQuotes, quotesMap, isFetching],
[
supportedRoutes,
sortedRoutes,
sortedRoutesWithQuotes,
quotesMap,
isFetching,
],
);
};
4 changes: 2 additions & 2 deletions wormhole-connect/src/hooks/useTrackTransfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const useTrackTransfer = (): void => {

let sleepTime = 5000;

if (txData && txData.eta && txData.eta < 30_000) {
if (txData?.eta && txData.eta < 30_000) {
// Poll aggressively for very fast transfers
sleepTime = 1000;
}
Expand Down Expand Up @@ -83,7 +83,7 @@ const useTrackTransfer = (): void => {
return () => {
isActive = false;
};
}, [routeContext, txData?.eta, timestamp]);
}, [routeContext, txData?.eta, timestamp, dispatch]);
};

export default useTrackTransfer;
4 changes: 3 additions & 1 deletion wormhole-connect/src/hooks/useTransactionHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ const useTransactionHistory = (
if (hasMoreWHScan && whScanPage !== page) {
setWHScanPage(page);
}
}, [hasMoreMayan, hasMoreWHScan, page]);
}, [hasMoreMayan, hasMoreWHScan, mayanPage, page, whScanPage]);

// Side-effect to merge transactions in time-order whenever there is new data
useEffect(() => {
Expand Down Expand Up @@ -180,6 +180,8 @@ const useTransactionHistory = (
updateMayanIndex(mayanLocalIdx);
// Append the merged transactions and exit
setTransactions((txs) => appendTxs(txs, mergedTxs));
// We only need to re-run this side-effect when either of the transaction data changes
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [whScanTxs, mayanTxs]);

return {
Expand Down
2 changes: 1 addition & 1 deletion wormhole-connect/src/hooks/useTransactionHistoryMayan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ const useTransactionHistoryMayan = (
return () => {
cancelled = true;
};
}, [page, pageSize]);
}, [address, page, pageSize, parseTransactions]);

return {
transactions,
Expand Down
177 changes: 99 additions & 78 deletions wormhole-connect/src/hooks/useTransactionHistoryWHScan.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback, useEffect, useState } from 'react';
import { useCallback, useEffect, useMemo, useState } from 'react';
import {
amount as sdkAmount,
chainIdToChain,
Expand Down Expand Up @@ -118,7 +118,10 @@ const useTransactionHistoryWHScan = (

const { address, page = 0, pageSize = 30 } = props;

const parseSingleTx = (tx: WormholeScanTransaction) => {
// Common parsing logic for a single transaction from WHScan API.
// IMPORTANT: Anything specific to a route, please use that route's parser:
// parseTokenBridgeTx | parseNTTTx | parseCCTPTx | parsePorticoTx
const parseSingleTx = useCallback((tx: WormholeScanTransaction) => {
const { content, data, sourceChain, targetChain } = tx;
const { tokenAmount, usdAmount } = data || {};
const { standarizedProperties } = content || {};
Expand Down Expand Up @@ -219,97 +222,115 @@ const useTransactionHistoryWHScan = (
};

return txData;
};
}, []);

// Parser for Portal Token Bridge transactions (appId === PORTAL_TOKEN_BRIDGE)
// IMPORTANT: This is where we can add any customizations specific to Token Bridge data
// that we have retrieved from WHScan API
const parseTokenBridgeTx = (tx: WormholeScanTransaction) => {
return parseSingleTx(tx);
};
const parseTokenBridgeTx = useCallback(
(tx: WormholeScanTransaction) => {
return parseSingleTx(tx);
},
[parseSingleTx],
);

// Parser for NTT transactions (appId === NATIVE_TOKEN_TRANSFER)
// IMPORTANT: This is where we can add any customizations specific to NTT data
// that we have retrieved from WHScan API
const parseNTTTx = (tx: WormholeScanTransaction) => {
return parseSingleTx(tx);
};
const parseNTTTx = useCallback(
(tx: WormholeScanTransaction) => {
return parseSingleTx(tx);
},
[parseSingleTx],
);

// Parser for CCTP transactions (appId === CCTP_WORMHOLE_INTEGRATION)
// IMPORTANT: This is where we can add any customizations specific to CCTP data
// that we have retrieved from WHScan API
const parseCCTPTx = (tx: WormholeScanTransaction) => {
return parseSingleTx(tx);
};
const parseCCTPTx = useCallback(
(tx: WormholeScanTransaction) => {
return parseSingleTx(tx);
},
[parseSingleTx],
);

// Parser for Portico transactions (appId === ETH_BRIDGE or USDT_BRIDGE)
// IMPORTANT: This is where we can add any customizations specific to Portico data
// that we have retrieved from WHScan API
const parsePorticoTx = (tx: WormholeScanTransaction) => {
const txData = parseSingleTx(tx);
if (!txData) return;

const payload = tx.content.payload
.parsedPayload as unknown as WormholeScanPorticoParsedPayload;

const {
finalTokenAddress,
flagSet,
minAmountFinish,
recipientAddress,
relayerFee,
} = payload;

const nativeTokenKey = getNativeVersionOfToken(
tx.data.symbol,
txData.fromChain,
);
const nativeToken = config.tokens[nativeTokenKey];
if (!nativeToken) return;

const startToken = flagSet.flags.shouldWrapNative
? getGasToken(txData.fromChain)
: nativeToken;

const finalTokenConfig = config.sdkConverter.findTokenConfigV1(
Wormhole.tokenId(
txData.toChain,
toNative(txData.toChain, finalTokenAddress).toString(),
),
config.tokensArr,
);
if (!finalTokenConfig) return;

const receiveAmount = BigInt(minAmountFinish) - BigInt(relayerFee);

// Override with Portico specific data
txData.tokenKey = startToken.key;
txData.tokenAddress = startToken.tokenId?.address || 'native';
txData.receivedTokenKey = flagSet.flags.shouldUnwrapNative
? getGasToken(txData.toChain).key
: finalTokenConfig.key;
txData.receiveAmount =
receiveAmount > 0
? toFixedDecimals(
sdkAmount.display(
sdkAmount.fromBaseUnits(receiveAmount, finalTokenConfig.decimals),
0,
),
DECIMALS,
)
: '';
txData.recipient = toNative(txData.toChain, recipientAddress).toString();

return txData;
};
const parsePorticoTx = useCallback(
(tx: WormholeScanTransaction) => {
const txData = parseSingleTx(tx);
if (!txData) return;

const payload = tx.content.payload
.parsedPayload as unknown as WormholeScanPorticoParsedPayload;

const {
finalTokenAddress,
flagSet,
minAmountFinish,
recipientAddress,
relayerFee,
} = payload;

const nativeTokenKey = getNativeVersionOfToken(
tx.data.symbol,
txData.fromChain,
);
const nativeToken = config.tokens[nativeTokenKey];
if (!nativeToken) return;

const startToken = flagSet.flags.shouldWrapNative
? getGasToken(txData.fromChain)
: nativeToken;

const finalTokenConfig = config.sdkConverter.findTokenConfigV1(
Wormhole.tokenId(
txData.toChain,
toNative(txData.toChain, finalTokenAddress).toString(),
),
config.tokensArr,
);
if (!finalTokenConfig) return;

const receiveAmount = BigInt(minAmountFinish) - BigInt(relayerFee);

// Override with Portico specific data
txData.tokenKey = startToken.key;
txData.tokenAddress = startToken.tokenId?.address || 'native';
txData.receivedTokenKey = flagSet.flags.shouldUnwrapNative
? getGasToken(txData.toChain).key
: finalTokenConfig.key;
txData.receiveAmount =
receiveAmount > 0
? toFixedDecimals(
sdkAmount.display(
sdkAmount.fromBaseUnits(
receiveAmount,
finalTokenConfig.decimals,
),
0,
),
DECIMALS,
)
: '';
txData.recipient = toNative(txData.toChain, recipientAddress).toString();

return txData;
},
[parseSingleTx],
);

const PARSERS = {
PORTAL_TOKEN_BRIDGE: parseTokenBridgeTx,
NATIVE_TOKEN_TRANSFER: parseNTTTx,
CCTP_WORMHOLE_INTEGRATION: parseCCTPTx,
ETH_BRIDGE: parsePorticoTx,
USDT_BRIDGE: parsePorticoTx,
};
const PARSERS = useMemo(
() => ({
PORTAL_TOKEN_BRIDGE: parseTokenBridgeTx,
NATIVE_TOKEN_TRANSFER: parseNTTTx,
CCTP_WORMHOLE_INTEGRATION: parseCCTPTx,
ETH_BRIDGE: parsePorticoTx,
USDT_BRIDGE: parsePorticoTx,
}),
[parseCCTPTx, parseNTTTx, parsePorticoTx, parseTokenBridgeTx],
);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const parseTransactions = useCallback(
Expand Down Expand Up @@ -339,7 +360,7 @@ const useTransactionHistoryWHScan = (
})
.filter((tx) => !!tx); // Filter out unsupported transactions
},
[],
[PARSERS, parsePorticoTx],
);

useEffect(() => {
Expand Down Expand Up @@ -413,7 +434,7 @@ const useTransactionHistoryWHScan = (
return () => {
cancelled = true;
};
}, [page, pageSize]);
}, [address, page, pageSize, parseTransactions]);

return {
transactions,
Expand Down

0 comments on commit b9a82f3

Please sign in to comment.