Skip to content

Commit

Permalink
feat: Bridge and token address validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Halibao-Lala committed Dec 2, 2024
1 parent 2723b11 commit 9026cb5
Show file tree
Hide file tree
Showing 6 changed files with 397 additions and 1 deletion.
5 changes: 5 additions & 0 deletions packages/canonical-bridge-widget/src/core/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ export const DEFAULT_ADDRESS = '0x6836CbaCbBd1E798cC56802AC7d8BDf6Da0d0980';
export const DEFAULT_TRON_ADDRESS = 'TTb3A6ASFejJuGcM1UVcRCJA23WGiJKSiY';
export const DEFAULT_SOLANA_ADDRESS = 'J7JYXS8PMMBgfFKP1bqUu7mGgWyWUDL9xqfYujznc61r';

export const CBRIDGE_ENDPOINT = 'https://cbridge-prod2.celer.app/v2';
export const DEBRIDGE_ENDPOINT = 'https://deswap.debridge.finance/v1.0';
export const STARGATE_ENDPOINT = 'https://mainnet.stargate-api.com/v1/metadata?version=v2';
export const MESON_ENDPOINT = 'https://relayer.meson.fi/api/v1';

export const nativeTokenMap = {
1: 'ETH',
10: 'ETH',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,19 @@ export interface IMesonChain {
address: string; // bridge address
tokens: IMesonToken[];
}

interface IMesonTokenLimits {
id: string;
decimals: number;
addr?: `0x${string}`;
min: string;
max: string;
}

export interface IMesonTokenList {
id: string;
name: string;
chainId: `0x${string}`;
address: `0x${string}`;
tokens: IMesonTokenLimits[];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export const stargateChainKey: { [key: number]: string } = {
1: 'ethereum',
10: 'optimism',
14: 'flare',
56: 'bsc',
137: 'polygon',
250: 'fantom',
1088: 'metis',
1116: 'coredao',
1329: 'sei',
1625: 'gravity',
2222: 'kava',
5000: 'mantle',
8217: 'klaytn',
8453: 'base',
8822: 'iota',
42161: 'arbitrum',
43114: 'avalanche',
59144: 'linea',
167000: 'taiko',
534352: 'scroll',
1313161554: 'aurora',
1380012617: 'rarible',
};
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,46 @@ export interface IStargateBusDriveSettings {
maxWaitTime: number;
passengersToDrive: number;
}

export interface IStargateBridgeTokenInfo {
stargateType: string;
address: `0x${string}`;
token: {
address: `0x${string}`;
decimals: number;
symbol: string;
};
lpToken: {
address: `0x${string}`;
decimals: number;
symbol: string;
};
farm: {
stargateStaking: {
address: `0x${string}`;
rewardTokens: [
{
address: `0x${string}`;
decimals: number;
symbol: string;
},
{
address: `0x${string}`;
decimals: number;
symbol: string;
},
];
};
};
id: string;
assetId: string;
chainKey: string;
chainName: string;
tokenMessaging: `0x${string}`;
sharedDecimals: number;
}

export interface IStargateTokenList {
v1: IStargateBridgeTokenInfo[];
v2: IStargateBridgeTokenInfo[];
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { useTronContract } from '@/modules/aggregator/adapters/meson/hooks/useTr
import { useSolanaTransferInfo } from '@/modules/transfer/hooks/solana/useSolanaTransferInfo';
import { useTronAccount } from '@/modules/wallet/hooks/useTronAccount';
import { useWaitForTxReceipt } from '@/core/hooks/useWaitForTxReceipt';
import { useValidateSendToken } from '@/modules/transfer/hooks/useSendTokenValidation';

export function TransferButton({
onOpenSubmittedModal,
Expand Down Expand Up @@ -83,6 +84,8 @@ export function TransferButton({
const { isConnected: isEvmConnected } = useAccount();
const { isConnected: isTronConnected } = useTronAccount();
const { waitForTxReceipt } = useWaitForTxReceipt();
const { validateCBridgeToken, validateDeBridgeToken, validateMesonToken, validateStargateToken } =
useValidateSendToken();

const isApproveNeeded =
(fromChain?.chainType === 'evm' &&
Expand Down Expand Up @@ -171,6 +174,26 @@ export function TransferButton({

if (transferActionInfo.bridgeType === 'cBridge' && cBridgeArgs && fromChain && address) {
try {
const isValid = await validateCBridgeToken({
tokenAddress: selectedToken.address as `0x${string}`,
bridgeAddress: transferActionInfo.bridgeAddress as `0x${string}`,
fromChainId: fromChain.id,
isPegged: selectedToken.isPegged,
tokenSymbol: selectedToken.symbol,
toChainId: toChain?.id,
});
if (!isValid) {
handleFailure({
tokenAddress: selectedToken.address as `0x${string}`,
bridgeAddress: transferActionInfo.bridgeAddress as `0x${string}`,
fromChainId: fromChain.id,
isPegged: selectedToken.isPegged,
tokenSymbol: selectedToken.symbol,
toChainId: toChain?.id,
message: `Invalid cBridge token!!`,
});
return;
}
const cBridgeHash = await bridgeSDK.cBridge.sendToken({
// eslint-disable-next-line @typescript-eslint/no-explicit-any
walletClient: walletClient as any,
Expand Down Expand Up @@ -212,8 +235,21 @@ export function TransferButton({
} else if (transferActionInfo.bridgeType === 'deBridge') {
try {
let deBridgeHash: string | undefined;

if (fromChain?.chainType === 'evm' && transferActionInfo.value && address) {
const isValidToken = await validateDeBridgeToken({
fromChainId: fromChain?.id,
tokenSymbol: selectedToken.symbol,
tokenAddress: selectedToken.address as `0x${string}`,
});
if (!isValidToken) {
handleFailure({
message: 'Invalid deBridge token!!',
fromChainId: fromChain?.id,
tokenSymbol: selectedToken.symbol,
tokenAddress: selectedToken.address as `0x${string}`,
});
return;
}
deBridgeHash = await bridgeSDK.deBridge.sendToken({
// eslint-disable-next-line @typescript-eslint/no-explicit-any
walletClient: walletClient as any,
Expand Down Expand Up @@ -265,6 +301,23 @@ export function TransferButton({
handleFailure(e);
}
} else if (transferActionInfo.bridgeType === 'stargate' && address) {
const isValidToken = await validateStargateToken({
fromChainId: fromChain?.id,
tokenAddress: selectedToken.address as `0x${string}`,
bridgeAddress: transferActionInfo.bridgeAddress as `0x${string}`,
tokenSymbol: selectedToken.symbol,
});
if (!isValidToken) {
handleFailure({
messages: 'Invalid Stargate token!!',
fromChainId: fromChain?.id,
tokenAddress: selectedToken.address as `0x${string}`,
bridgeAddress: transferActionInfo.bridgeAddress as `0x${string}`,
tokenSymbol: selectedToken.symbol,
});

return;
}
const stargateHash = await bridgeSDK.stargate.sendToken({
// eslint-disable-next-line @typescript-eslint/no-explicit-any
walletClient: walletClient as any,
Expand Down Expand Up @@ -318,6 +371,22 @@ export function TransferButton({
onOpenSubmittedModal();
}
} else if (transferActionInfo.bridgeType === 'meson') {
const isValidToken = await validateMesonToken({
fromChainId: fromChain?.id,
tokenAddress: selectedToken.address as `0x${string}`,
bridgeAddress: transferActionInfo.bridgeAddress as `0x${string}`,
tokenSymbol: selectedToken.symbol,
});
if (!isValidToken) {
handleFailure({
message: 'Invalid Meson token!!',
fromChainId: fromChain?.id,
tokenAddress: selectedToken.address as `0x${string}`,
bridgeAddress: transferActionInfo.bridgeAddress as `0x${string}`,
tokenSymbol: selectedToken.symbol,
});
return;
}
let fromAddress = '';
let toAddress = '';
let msg = '';
Expand Down
Loading

0 comments on commit 9026cb5

Please sign in to comment.