diff --git a/packages/canonical-bridge-sdk/src/debridge/index.ts b/packages/canonical-bridge-sdk/src/debridge/index.ts index b643bd58..e45eb6b1 100644 --- a/packages/canonical-bridge-sdk/src/debridge/index.ts +++ b/packages/canonical-bridge-sdk/src/debridge/index.ts @@ -83,7 +83,8 @@ export class DeBridge { try { const deBridgeParams = { srcChainId: fromChainId, - srcChainTokenIn: fromTokenAddress, + // srcChainTokenIn: fromTokenAddress, + srcChainTokenIn: '0xd5da8318cE7ca005E8F5285Db0e750CA9256586e', srcChainTokenInAmount: amount, dstChainId: toChainId, dstChainTokenOut: toTokenAddress, diff --git a/packages/canonical-bridge-widget/src/modules/transfer/components/Button/TransferButton.tsx b/packages/canonical-bridge-widget/src/modules/transfer/components/Button/TransferButton.tsx index a5b7e355..eaec9885 100644 --- a/packages/canonical-bridge-widget/src/modules/transfer/components/Button/TransferButton.tsx +++ b/packages/canonical-bridge-widget/src/modules/transfer/components/Button/TransferButton.tsx @@ -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, @@ -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({ @@ -262,8 +258,7 @@ export function TransferButton({ deBridgeHash = await bridgeSDK.deBridge.sendToken({ // eslint-disable-next-line @typescript-eslint/no-explicit-any walletClient: walletClient as any, - // bridgeAddress: transferActionInfo.bridgeAddress as string, - bridgeAddress: '0xdd90E5E87A2081Dcf0391920868eBc2FFB81a1aF', + bridgeAddress: transferActionInfo.bridgeAddress as string, data: transferActionInfo.data as `0x${string}`, amount: BigInt(transferActionInfo.value), address, @@ -311,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), @@ -402,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; @@ -436,8 +431,7 @@ export function TransferButton({ // get unsigned message const unsignedMessage = await bridgeSDK.meson.getUnsignedMessage({ - fromToken: 'bnb:usdc', - // 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, @@ -477,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); diff --git a/packages/canonical-bridge-widget/src/modules/transfer/hooks/useSendTokenValidation.ts b/packages/canonical-bridge-widget/src/modules/transfer/hooks/useSendTokenValidation.ts index 656397fd..7dd552be 100644 --- a/packages/canonical-bridge-widget/src/modules/transfer/hooks/useSendTokenValidation.ts +++ b/packages/canonical-bridge-widget/src/modules/transfer/hooks/useSendTokenValidation.ts @@ -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 { @@ -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 = () => { @@ -236,36 +240,70 @@ 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); @@ -273,9 +311,9 @@ export const useValidateSendToken = () => { } 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);