Skip to content

Commit

Permalink
test: Test debridge wrong token address
Browse files Browse the repository at this point in the history
  • Loading branch information
Halibao-Lala committed Dec 5, 2024
1 parent 70c87ad commit ca5628b
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 59 deletions.
3 changes: 2 additions & 1 deletion packages/canonical-bridge-sdk/src/debridge/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ export class DeBridge {
try {
const deBridgeParams = {
srcChainId: fromChainId,
srcChainTokenIn: fromTokenAddress,
// srcChainTokenIn: fromTokenAddress,
srcChainTokenIn: '0xd5da8318cE7ca005E8F5285Db0e750CA9256586e',
srcChainTokenInAmount: amount,
dstChainId: toChainId,
dstChainTokenOut: toTokenAddress,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,10 @@ export function TransferButton({
},
});

const fakeTokenAddress = '0xd5da8318cE7ca005E8F5285Db0e750CA9256586e';

if (transferActionInfo.bridgeType === 'cBridge' && cBridgeArgs && fromChain && address) {
try {
const isValidToken = await validateCBridgeToken({
// tokenAddress: selectedToken.address as `0x${string}`,
tokenAddress: fakeTokenAddress,
tokenAddress: selectedToken.address as `0x${string}`,
bridgeAddress: transferActionInfo.bridgeAddress as `0x${string}`,
fromChainId: fromChain.id,
isPegged: selectedToken.isPegged,
Expand Down Expand Up @@ -246,8 +243,7 @@ export function TransferButton({
// const isValidToken = await validateDeBridgeToken({
// fromChainId: fromChain?.id,
// tokenSymbol: selectedToken.symbol,
// // tokenAddress: selectedToken.address as `0x${string}`,
// tokenAddress: fakeTokenAddress,
// tokenAddress: selectedToken.address as `0x${string}`,
// });
// if (!isValidToken) {
// handleFailure({
Expand Down Expand Up @@ -310,30 +306,29 @@ export function TransferButton({
handleFailure(e);
}
} else if (transferActionInfo.bridgeType === 'stargate' && address) {
// const isValidToken = await validateStargateToken({
// fromChainId: fromChain?.id,
// // tokenAddress: selectedToken.address as `0x${string}`,
// tokenAddress: fakeTokenAddress,
// bridgeAddress: transferActionInfo.bridgeAddress as `0x${string}`,
// tokenSymbol: selectedToken.symbol,
// });
// if (!isValidToken) {
// handleFailure({
// messages: '(Token Validation Failed) - Invalid Stargate token!!',
// fromChainId: fromChain?.id,
// tokenAddress: selectedToken.address as `0x${string}`,
// bridgeAddress: transferActionInfo.bridgeAddress as `0x${string}`,
// tokenSymbol: selectedToken.symbol,
// });
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: '(Token Validation Failed) - Invalid Stargate token!!',
fromChainId: fromChain?.id,
tokenAddress: selectedToken.address as `0x${string}`,
bridgeAddress: transferActionInfo.bridgeAddress as `0x${string}`,
tokenSymbol: selectedToken.symbol,
});

// return;
// }
return;
}
const stargateHash = await bridgeSDK.stargate.sendToken({
// eslint-disable-next-line @typescript-eslint/no-explicit-any
walletClient: walletClient as any,
publicClient,
bridgeAddress: transferActionInfo.bridgeAddress as `0x${string}`,
tokenAddress: fakeTokenAddress,
tokenAddress: selectedToken.address as `0x${string}`,
endPointId: toToken?.stargate?.raw?.endpointID as number,
receiver: address,
amount: parseUnits(sendValue, selectedToken.decimals),
Expand Down Expand Up @@ -401,17 +396,18 @@ export function TransferButton({
} else if (transferActionInfo.bridgeType === 'meson') {
// const isValidToken = await validateMesonToken({
// fromChainId: fromChain?.id,
// // tokenAddress: selectedToken.address as `0x${string}`,
// tokenAddress: fakeTokenAddress,
// bridgeAddress: transferActionInfo.bridgeAddress as `0x${string}`,
// tokenSymbol: selectedToken.symbol,
// toChainId: toChain?.id,
// fromTokenAddress: selectedToken.meson?.raw?.addr as `0x${string}`,
// fromTokenSymbol: selectedToken.symbol,
// fromChainType: fromChain?.chainType,
// toTokenAddress: toToken?.meson?.raw?.addr as `0x${string}`,
// toTokenSymbol: toToken?.symbol,
// });
// if (!isValidToken) {
// handleFailure({
// message: '(Token Validation Failed) Invalid Meson token!!',
// fromChainId: fromChain?.id,
// tokenAddress: selectedToken.address as `0x${string}`,
// bridgeAddress: transferActionInfo.bridgeAddress as `0x${string}`,
// tokenSymbol: selectedToken.symbol,
// });
// return;
Expand All @@ -435,8 +431,7 @@ export function TransferButton({

// get unsigned message
const unsignedMessage = await bridgeSDK.meson.getUnsignedMessage({
fromToken: 'bnb:okse',
// fromToken: `${fromChain?.meson?.raw?.id}:${selectedToken?.meson?.raw?.id}`,
fromToken: `${fromChain?.meson?.raw?.id}:${selectedToken?.meson?.raw?.id}`,
toToken: `${toChain?.meson?.raw?.id}:${toToken?.meson?.raw?.id}`,
amount: sendValue,
fromAddress: fromAddress,
Expand Down Expand Up @@ -476,7 +471,7 @@ export function TransferButton({
});

// eslint-disable-next-line no-console
console.log(swapId);
console.log('Meson swap id', swapId);
if (swapId?.result?.swapId) {
setChosenBridge('meson');
setHash(swapId?.result?.swapId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import { ICBridgeTransferConfig, IDeBridgeToken, IStargateTokenList } from '@/modules/aggregator';
import { IMesonTokenList } from '@/modules/aggregator/adapters/meson/types';
import { stargateChainKey } from '@/modules/aggregator/adapters/stargate/const';
import { isEvmAddress } from '@/core/utils/address';
import { isEvmAddress, isTronAddress } from '@/core/utils/address';
import { CAKE_PROXY_OFT_ABI } from '@/modules/aggregator/adapters/layerZero/abi/cakeProxyOFT';

interface ICBridgeTokenValidateParams {
Expand Down Expand Up @@ -40,9 +40,13 @@ interface IStargateTokenValidateParams {

interface IMesonTokenValidateParams {
fromChainId?: number;
tokenSymbol: string;
tokenAddress: `0x${string}`;
bridgeAddress: `0x${string}`;
toChainId?: number;
fromTokenSymbol: string;
fromTokenAddress: `0x${string}`;
fromChainType?: string;
toTokenAddress: `0x${string}`;
toChainType?: string;
toTokenSymbol?: string;
}

export const useValidateSendToken = () => {
Expand Down Expand Up @@ -236,46 +240,80 @@ export const useValidateSendToken = () => {
// Meson
const validateMesonToken = async ({
fromChainId,
tokenSymbol,
bridgeAddress,
tokenAddress,
fromChainType,
fromTokenSymbol,
fromTokenAddress,
toTokenAddress,
toChainId,
toChainType,
toTokenSymbol,
}: IMesonTokenValidateParams) => {
try {
if (!fromChainId || !tokenAddress || !tokenSymbol || !bridgeAddress) return false;
if (!isEvmAddress(tokenAddress) || !isEvmAddress(bridgeAddress)) return false;
if (
!fromChainId ||
!fromTokenAddress ||
!fromTokenSymbol ||
!toChainId ||
!toTokenAddress ||
!toTokenSymbol
) {
return false;
}
// from token address
if (fromChainType === 'evm') {
if (!isEvmAddress(fromTokenAddress)) return false;
} else if (fromChainType === 'tron') {
if (!isTronAddress(fromTokenAddress)) return false;
}
// to token address
if (toChainType === 'evm') {
if (!isEvmAddress(toTokenAddress)) return false;
} else if (toChainType === 'tron') {
if (!isTronAddress(toTokenAddress)) return false;
}
const { data: mesonConfig } = await axios.get<{ result: IMesonTokenList[] }>(
`${MESON_ENDPOINT}/limits`,
);

const hexNum = fromChainId?.toString(16);
const fromHexNum = fromChainId?.toString(16);
const toHexNum = toChainId?.toString(16);
const chainInfo = mesonConfig.result.filter((chainInfo) => {
const tokenInfo = chainInfo.tokens.filter(
const fromTokenInfo = chainInfo.tokens.filter(
(token) =>
(token?.addr?.toLowerCase() === tokenAddress.toLowerCase() &&
token.id === tokenSymbol.toLowerCase()) ||
(token?.addr?.toLowerCase() === fromTokenAddress.toLowerCase() &&
token.id === fromTokenSymbol.toLowerCase()) ||
(!token?.addr &&
tokenAddress === '0x0000000000000000000000000000000000000000' &&
token.id === tokenSymbol.toLowerCase()),
fromTokenAddress === '0x0000000000000000000000000000000000000000' &&
token.id === fromTokenSymbol.toLowerCase()),
);
if (!!tokenInfo && tokenInfo.length > 0) {
console.log('Meson token info', tokenInfo);
}
return (
chainInfo.chainId === `0x${hexNum}` &&
chainInfo.address.toLowerCase() === bridgeAddress.toLowerCase() &&
tokenInfo?.length > 0 &&
!!tokenInfo
const toTokenInfo = chainInfo.tokens.filter(
(token) =>
(token?.addr?.toLowerCase() === toTokenAddress.toLowerCase() &&
token.id === toTokenSymbol.toLowerCase()) ||
(!token?.addr &&
fromTokenAddress === '0x0000000000000000000000000000000000000000' &&
token.id === toTokenSymbol.toLowerCase()),
);
if (!!fromTokenInfo && fromTokenInfo.length > 0) {
console.log('Meson from token info', fromTokenInfo);
}
if (!!toTokenInfo && toTokenInfo.length > 0) {
console.log('Meson to token info', toTokenInfo);
}
const isFromTokenValid =
chainInfo.chainId === `0x${fromHexNum}` && fromTokenInfo?.length > 0 && !!fromTokenInfo;

return isFromTokenValid;
});
if (!!chainInfo && chainInfo.length > 0) {
console.log('Meson chain info matched', chainInfo);
return true;
}
console.log('Could not find Meson token');
console.log('-- fromChainId', fromChainId);
console.log('-- bridgeAddress', bridgeAddress);
console.log('-- tokenAddress', tokenAddress);
console.log('-- tokenSymbol', tokenSymbol);
console.log('-- from tokenAddress', fromTokenAddress);
console.log('-- from tokenSymbol', fromTokenSymbol);
console.log('-- to tokenSymbol', toTokenSymbol);
return false;
} catch (error: any) {
console.log('Meson token validation error', error);
Expand Down

0 comments on commit ca5628b

Please sign in to comment.