Skip to content

Commit

Permalink
Merge pull request #184 from bnb-chain/feat/addressCheck
Browse files Browse the repository at this point in the history
Feat/address check
  • Loading branch information
Halibao-Lala authored Dec 10, 2024
2 parents 64153d8 + a9048f3 commit 5aa4289
Show file tree
Hide file tree
Showing 11 changed files with 166,201 additions and 39 deletions.
28,421 changes: 28,421 additions & 0 deletions apps/canonical-bridge-ui/token-config/mainnet/cBridge/config.json

Large diffs are not rendered by default.

137,706 changes: 137,706 additions & 0 deletions apps/canonical-bridge-ui/token-config/mainnet/deBridge/config.json

Large diffs are not rendered by default.

20 changes: 11 additions & 9 deletions apps/canonical-bridge-ui/token-config/mainnet/useTransferConfig.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
import { useEffect, useState } from 'react';
import {
ICBridgeTransferConfig,
// ICBridgeTransferConfig,
IDeBridgeTransferConfig,
ITransferConfig,
} from '@bnb-chain/canonical-bridge-widget';
import axios from 'axios';
// import axios from 'axios';

import { env } from '@/core/env';
// import { env } from '@/core/env';
import stargateConfig from '@/token-config/mainnet/stargate/config.json';
import layerZeroConfig from '@/token-config/mainnet/layerZero/config.json';
import mesonConfig from '@/token-config/mainnet/meson/config.json';
import deBridgeRes from '@/token-config/mainnet/deBridge/config.json';
import cBridgeRes from '@/token-config/mainnet/cBridge/config.json';

export function useTransferConfig() {
const [transferConfig, setTransferConfig] = useState<ITransferConfig>();

useEffect(() => {
const initConfig = async () => {
const [cBridgeRes, deBridgeRes] = await Promise.all([
axios.get<{ data: ICBridgeTransferConfig }>(`${env.SERVER_ENDPOINT}/api/bridge/cbridge`),
axios.get<{ data: IDeBridgeTransferConfig }>(`${env.SERVER_ENDPOINT}/api/bridge/debridge`),
]);
// const [cBridgeRes, deBridgeRes] = await Promise.all([
// axios.get<{ data: ICBridgeTransferConfig }>(`${env.SERVER_ENDPOINT}/api/bridge/cbridge`),
// axios.get<{ data: IDeBridgeTransferConfig }>(`${env.SERVER_ENDPOINT}/api/bridge/debridge`),
// ]);

const cBridgeConfig = cBridgeRes.data.data;
const deBridgeConfig = handleDeBridgeConfig(deBridgeRes.data.data);
const cBridgeConfig = cBridgeRes.data;
const deBridgeConfig = handleDeBridgeConfig(deBridgeRes as any);

const transferConfig: ITransferConfig = {
defaultSelectedInfo: {
Expand Down
34 changes: 20 additions & 14 deletions packages/canonical-bridge-sdk/src/cbridge/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,12 +376,13 @@ export class CBridge {
fromChainId,
fromTokenAddress,
fromTokenSymbol,
fromTokenDecimals,
bridgeAddress,
toChainId,
toTokenAddress,
toTokenSymbol,
toTokenDecimals,
amount,
decimals,
cBridgeEndpoint,
}: ICBridgeTokenValidateParams) => {
try {
Expand All @@ -391,10 +392,11 @@ export class CBridge {
!fromTokenAddress ||
!bridgeAddress ||
!fromTokenSymbol ||
!fromTokenDecimals ||
!toTokenAddress ||
!toTokenSymbol ||
!toTokenDecimals ||
!amount ||
!decimals ||
!cBridgeEndpoint
) {
console.log('Failed to get cBridge token address validation params');
Expand All @@ -407,19 +409,21 @@ export class CBridge {
console.log('toTokenAddress', toTokenAddress);
console.log('toTokenSymbol', toTokenSymbol);
console.log('amount', amount);
console.log('decimals', decimals);
console.log('fromTokenDecimals', fromTokenDecimals);
console.log('toTokenDecimals', toTokenDecimals);
console.log('cBridgeEndpoint', cBridgeEndpoint);
return false;
}
if (!isEvmAddress(fromTokenAddress) || !isEvmAddress(bridgeAddress)) {
return false;
}
const { data: cBridgeConfig } = await axios.get<ICBridgeTransferConfig>(
`${cBridgeEndpoint}`
);
if (!cBridgeConfig) {
console.log('failed to get cBridge API config');
return false;
}
if (!isEvmAddress(fromTokenAddress) || !isEvmAddress(bridgeAddress))
return false;
if (isPegged === true) {
// pegged token
const peggedToken = cBridgeConfig.pegged_pair_configs.filter((pair) => {
Expand All @@ -430,17 +434,19 @@ export class CBridge {
pair.org_chain_id === fromChainId &&
orgToken.address === fromTokenAddress &&
orgToken.symbol === fromTokenSymbol &&
orgToken.decimal === decimals &&
orgToken.decimal === fromTokenDecimals &&
peggedToken.address === toTokenAddress &&
peggedToken.symbol === toTokenSymbol &&
peggedToken.decimal === toTokenDecimals &&
pair.pegged_chain_id === toChainId) ||
(pair.pegged_burn_contract_addr === bridgeAddress &&
pair.pegged_chain_id === fromChainId &&
peggedToken.address === fromTokenAddress &&
peggedToken.symbol === fromTokenSymbol &&
peggedToken.decimal === decimals &&
peggedToken.decimal === fromTokenDecimals &&
orgToken.address === toTokenAddress &&
orgToken.symbol === toTokenSymbol &&
orgToken.decimal === toTokenDecimals &&
pair.org_chain_id === toChainId)
);
});
Expand All @@ -459,7 +465,7 @@ export class CBridge {
console.log('-- bridgeAddress', bridgeAddress);
return false;
} else {
// bridge address
// bridge contract address
const addressInfo = cBridgeConfig.chains.filter((chain) => {
return (
chain.id === fromChainId &&
Expand All @@ -472,13 +478,15 @@ export class CBridge {
].token.filter((t) => {
return (
t.token.address.toLowerCase() === fromTokenAddress.toLowerCase() &&
t.token.decimal === fromTokenDecimals &&
t.token.symbol === fromTokenSymbol
);
});
const toTokenInfo = cBridgeConfig.chain_token[toChainId].token.filter(
(t) => {
return (
t.token.address.toLowerCase() === toTokenAddress.toLowerCase() &&
t.token.decimal === toTokenDecimals &&
t.token.symbol === toTokenSymbol
);
}
Expand All @@ -488,12 +496,10 @@ export class CBridge {
fromTokenInfo?.length > 0 &&
toTokenInfo?.length > 0
) {
console.log(
'cBridge pool info matched',
addressInfo,
fromTokenInfo,
toTokenInfo
);
console.log('cBridge pool info matched');
console.log('bridge contract address info', addressInfo);
console.log('bridge from token info', fromTokenInfo);
console.log('bridge to token info', toTokenInfo);
return true;
} else {
console.log('Can not find cBridge pool info');
Expand Down
3 changes: 2 additions & 1 deletion packages/canonical-bridge-sdk/src/cbridge/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,11 +308,12 @@ export interface ICBridgeTokenValidateParams {
fromChainId?: number;
fromTokenAddress: `0x${string}`;
fromTokenSymbol: string;
fromTokenDecimals: number;
bridgeAddress: `0x${string}`;
toChainId?: number;
toTokenAddress?: `0x${string}`;
toTokenSymbol?: string;
toTokenDecimals: number;
amount: number;
decimals: number;
cBridgeEndpoint: string;
}
10 changes: 10 additions & 0 deletions packages/canonical-bridge-sdk/src/meson/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,12 @@ export class Meson {
fromChainType,
fromTokenSymbol,
fromTokenAddress,
fromTokenDecimals,
toTokenAddress,
toChainId,
toChainType,
toTokenSymbol,
toTokenDecimals,
amount,
mesonEndpoint,
}: IMesonTokenValidateParams) => {
Expand All @@ -121,9 +123,11 @@ export class Meson {
!fromChainId ||
!fromTokenAddress ||
!fromTokenSymbol ||
!fromTokenDecimals ||
!fromChainType ||
!toChainId ||
!toTokenAddress ||
!toTokenDecimals ||
!toChainType ||
!toTokenSymbol ||
!amount ||
Expand All @@ -133,9 +137,11 @@ export class Meson {
console.log('-- fromChainId', fromChainId);
console.log('-- fromTokenAddress', fromTokenAddress);
console.log('-- fromTokenSymbol', fromTokenSymbol);
console.log('-- fromTokenDecimals', fromTokenDecimals);
console.log('-- fromChainType', fromChainType);
console.log('-- toChainId', toChainId);
console.log('-- toTokenAddress', toTokenAddress);
console.log('-- toTokenDecimals', toTokenDecimals);
console.log('-- toChainType', toChainType);
console.log('-- toTokenSymbol', toTokenSymbol);
console.log('-- amount', amount);
Expand Down Expand Up @@ -187,10 +193,12 @@ export class Meson {
const fromTokenInfo = chainInfo.tokens.filter(
(token) =>
(token?.addr?.toLowerCase() === fromTokenAddress.toLowerCase() &&
token.decimals === fromTokenDecimals &&
token.id === fromTokenSymbol.toLowerCase()) ||
(!token?.addr &&
fromTokenAddress ===
'0x0000000000000000000000000000000000000000' &&
token.decimals === fromTokenDecimals &&
token.id === fromTokenSymbol.toLowerCase())
);
if (!!fromTokenInfo && fromTokenInfo.length > 0) {
Expand All @@ -207,9 +215,11 @@ export class Meson {
const toTokenInfo = chainInfo.tokens.filter(
(token) =>
(token?.addr?.toLowerCase() === toTokenAddress.toLowerCase() &&
token.decimals === toTokenDecimals &&
token.id === toTokenSymbol.toLowerCase()) ||
(!token?.addr &&
toTokenAddress === '0x0000000000000000000000000000000000000000' &&
token.decimals === toTokenDecimals &&
token.id === toTokenSymbol.toLowerCase())
);
if (!!toTokenInfo && toTokenInfo.length > 0) {
Expand Down
4 changes: 3 additions & 1 deletion packages/canonical-bridge-sdk/src/meson/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ export interface IMesonTokenValidateParams {
toChainId?: number;
fromTokenSymbol: string;
fromTokenAddress: `0x${string}`;
fromTokenDecimals: number;
fromChainType?: string;
toTokenAddress: `0x${string}`;
toTokenAddress?: `0x${string}`;
toTokenDecimals?: number;
toChainType?: string;
toTokenSymbol?: string;
amount: number;
Expand Down
29 changes: 18 additions & 11 deletions packages/canonical-bridge-sdk/src/stargate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,12 @@ export class Stargate {
async validateStargateToken({
fromBridgeAddress,
fromTokenAddress,
fromTokenDecimals,
fromTokenSymbol,
fromChainId,
toTokenAddress,
toTokenSymbol,
toTokenDecimals,
toChainId,
toBridgeAddress,
amount,
Expand All @@ -227,11 +229,13 @@ export class Stargate {
if (
!fromChainId ||
!fromTokenAddress ||
!fromTokenSymbol ||
!fromTokenDecimals ||
!fromBridgeAddress ||
!toBridgeAddress ||
!fromTokenSymbol ||
!toTokenAddress ||
!toTokenSymbol ||
!toTokenDecimals ||
!toChainId ||
!amount ||
!toPublicClient ||
Expand All @@ -242,11 +246,13 @@ export class Stargate {
console.log('-- from ChainId', fromChainId);
console.log('-- from TokenAddress', fromTokenAddress);
console.log('-- from TokenSymbol', fromTokenSymbol);
console.log('-- from TokenDecimals', fromTokenDecimals);
console.log('-- from bridgeAddress', fromBridgeAddress);
console.log('-- to ChainId', toChainId);
console.log('-- to TokenAddress', toTokenAddress);
console.log('-- to bridgeAddress', toBridgeAddress);
console.log('-- to TokenSymbol', toTokenSymbol);
console.log('-- to TokenDecimals', toTokenDecimals);
console.log('-- amount', amount);
console.log('-- to PublicClient', toPublicClient);
console.log('-- from PublicClient', fromPublicClient);
Expand All @@ -270,11 +276,6 @@ export class Stargate {
);
return false;
}
// Check token information from API
const { data: stargateConfig } = await axios.get<{
data: IStargateTokenList;
}>(`${stargateEndpoint}`);

const fromChainKey = stargateChainKey[fromChainId] ?? '';
const toChainKey = stargateChainKey[toChainId] ?? '';

Expand All @@ -284,10 +285,6 @@ export class Stargate {
console.log('To chain key', toChainKey, toChainId);
return false;
}
if (!stargateConfig) {
console.log('Failed to get Stargate API config');
return false;
}
// Check Stargate from chain information
const fromApiTokenAddr = await fromPublicClient.readContract({
address: fromBridgeAddress as `0x${string}`,
Expand Down Expand Up @@ -325,11 +322,20 @@ export class Stargate {
);
return false;
}
// Check token information from API
const { data: stargateConfig } = await axios.get<{
data: IStargateTokenList;
}>(`${stargateEndpoint}`);
if (!stargateConfig) {
console.log('Failed to get Stargate API config');
return false;
}
// From token info
const fromTokenInfo = stargateConfig.data?.v2?.filter((fromToken) => {
return (
fromToken.chainKey.toLowerCase() === fromChainKey.toLowerCase() &&
fromToken.address.toLowerCase() === fromBridgeAddress.toLowerCase() &&
fromToken.address.toLowerCase() === fromBridgeAddress.toLowerCase() && // bridge contract address
fromToken.token.decimals === fromTokenDecimals &&
fromToken.token.symbol === fromTokenSymbol &&
fromToken.token.address.toLowerCase() ===
fromTokenAddress.toLowerCase()
Expand All @@ -341,6 +347,7 @@ export class Stargate {
toToken.chainKey.toLowerCase() === toChainKey.toLowerCase() &&
toToken.address.toLowerCase() === toBridgeAddress.toLowerCase() &&
toToken.token.symbol === toTokenSymbol &&
toToken.token.decimals === toTokenDecimals &&
toToken.token.address.toLowerCase() === toTokenAddress.toLowerCase()
);
});
Expand Down
2 changes: 2 additions & 0 deletions packages/canonical-bridge-sdk/src/stargate/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,11 @@ export interface IStargateTokenValidateParams {
toBridgeAddress: `0x${string}`;
fromTokenAddress: `0x${string}`;
fromTokenSymbol: string;
fromTokenDecimals: number;
fromChainId?: number;
toTokenAddress: `0x${string}`;
toTokenSymbol: string;
toTokenDecimals: number;
toChainId?: number;
amount: number;
toPublicClient: PublicClient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,13 @@ export function TransferButton({
fromTokenAddress: fakeFromToken as `0x${string}`,
// fromTokenAddress: selectedToken?.cBridge?.raw?.token.address as `0x${string}`,
fromTokenSymbol: selectedToken?.cBridge?.raw?.token?.symbol as string,
fromTokenDecimals: selectedToken.cBridge?.raw?.token.decimal as number,
bridgeAddress: transferActionInfo.bridgeAddress as `0x${string}`,
toChainId: toChain?.id,
toTokenAddress: toToken?.cBridge?.raw?.token.address as `0x${string}`,
toTokenSymbol: toToken?.cBridge?.raw?.token.symbol,
toTokenDecimals: toToken?.cBridge?.raw?.token.decimal as number,
amount: Number(sendValue),
decimals: selectedToken.cBridge?.raw?.token.decimal as number,
cBridgeEndpoint: `${CBRIDGE_ENDPOINT}/getTransferConfigsForAll`,
});

Expand Down Expand Up @@ -336,9 +337,11 @@ export function TransferButton({
// fromTokenAddress: selectedToken?.stargate?.raw?.address as `0x${string}`,
fromTokenAddress: fakeFromToken as `0x${string}`,
fromTokenSymbol: selectedToken?.stargate?.raw?.symbol as string,
fromTokenDecimals: selectedToken?.stargate?.raw?.decimals as number,
fromChainId: fromChain?.id,
toTokenAddress: toToken?.stargate?.raw?.address as `0x${string}`,
toTokenSymbol: toToken?.stargate?.raw?.symbol as string,
toTokenDecimals: toToken?.stargate?.raw?.decimals as number,
toChainId: toChain?.id,
amount: Number(sendValue),
dstEndpointId: toToken?.stargate?.raw?.endpointID as number,
Expand Down Expand Up @@ -435,11 +438,13 @@ export function TransferButton({
toChainId: toChain?.id,
// fromTokenAddress: selectedToken.meson?.raw?.addr as `0x${string}`,
fromTokenAddress: fakeFromToken as `0x${string}`,
fromTokenSymbol: selectedToken.symbol,
fromTokenSymbol: selectedToken.meson?.raw?.id as string,
fromTokenDecimals: selectedToken.meson?.raw?.decimals as number,
fromChainType: fromChain?.chainType,
toChainType: toChain?.chainType,
toTokenAddress: toToken?.meson?.raw?.addr as `0x${string}`,
toTokenSymbol: toToken?.meson?.raw?.id,
toTokenDecimals: toToken?.meson?.raw?.decimals,
amount: Number(sendValue),
mesonEndpoint: MESON_ENDPOINT,
});
Expand Down
Loading

0 comments on commit 5aa4289

Please sign in to comment.