Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/address check #165

Merged
merged 2 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 All @@ -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,
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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);
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
Loading