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

Test/address check #180

Closed
wants to merge 37 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
dbe5e3b
feat: Get min dst gas before sending
Halibao-Lala Nov 29, 2024
28811d1
fix: Check destination min gas limit on CAKE
Halibao-Lala Dec 3, 2024
792cae1
feat: Release version
Halibao-Lala Dec 3, 2024
c891340
Merge pull request #151 from bnb-chain/cake/gasLimit1129
Halibao-Lala Dec 3, 2024
fb33ef4
chore: Update versions
github-actions[bot] Dec 3, 2024
c9dad77
Merge pull request #152 from bnb-chain/changeset-release/main
Halibao-Lala Dec 3, 2024
e2e59c6
fix: Hide input error message if not connect to wallet
Halibao-Lala Dec 3, 2024
f98ef0d
Merge pull request #154 from bnb-chain/cake/gasLimit1129
Halibao-Lala Dec 3, 2024
601fb21
chore: Release version
Halibao-Lala Dec 3, 2024
4cb6c4c
Merge pull request #155 from bnb-chain/cake/gasLimit1129
Halibao-Lala Dec 3, 2024
005fa8d
chore: Update versions
github-actions[bot] Dec 3, 2024
545a906
Merge pull request #156 from bnb-chain/changeset-release/main
wenty22 Dec 3, 2024
034cc4f
feat: Fix balance issue
wenty22 Dec 5, 2024
c92f2f2
Merge pull request #169 from bnb-chain/fix/balance1205
wenty22 Dec 5, 2024
0054f65
chore: Update versions
github-actions[bot] Dec 5, 2024
22fbff8
Merge pull request #170 from bnb-chain/changeset-release/main
wenty22 Dec 5, 2024
085db2f
feat: Bridge and token address validation
Halibao-Lala Dec 2, 2024
6994e70
chore: Update error message
Halibao-Lala Dec 2, 2024
35fe7b5
chore: Check meson native token
Halibao-Lala Dec 3, 2024
a4e73ad
fix: Check lower case address
Halibao-Lala Dec 3, 2024
96beb63
test: Test fake token address
Halibao-Lala Dec 4, 2024
9063b55
test: Remove token address validation
Halibao-Lala Dec 4, 2024
702b769
test: Test wrong from chain id
Halibao-Lala Dec 4, 2024
5c1fe9c
test: Test deBridge wrong bridge address
Halibao-Lala Dec 4, 2024
f4495c2
test: Test meson token
Halibao-Lala Dec 4, 2024
9de3358
test: Test meson okse
Halibao-Lala Dec 4, 2024
5edc480
test: Test debridge wrong token address
Halibao-Lala Dec 5, 2024
470a462
test: Send debridge tx data to cbridge address
Halibao-Lala Dec 5, 2024
3bca023
test: Revert debridge token address
Halibao-Lala Dec 5, 2024
caffe1c
test: Revert debridge contract address
Halibao-Lala Dec 5, 2024
182dab5
chore: Enable debridge validation check
Halibao-Lala Dec 5, 2024
0a16bed
chore: Add jest
Halibao-Lala Dec 6, 2024
f57420e
chore: Transfer validation WIP
Halibao-Lala Dec 6, 2024
6dd1a91
chore: Update cbridge token validation error message
Halibao-Lala Dec 9, 2024
717b3c7
chore: Migrate deBridge token info checking logic into SDK
Halibao-Lala Dec 9, 2024
ea88ba2
chore: Migrate Stargate and LayerZero token validation logic into SDK
Halibao-Lala Dec 9, 2024
773b5d7
chore: Migrate Meson token validation to SDK
Halibao-Lala Dec 9, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ export function useTransferConfig() {
137: ['cUSDCv3'],
42161: ['cUSDCv3'],
43114: ['BNB'],
7565164: ['So11111111111111111111111111111111111111112'],
},
},
bridgedTokenGroups: [
Expand Down
1,227 changes: 1,184 additions & 43 deletions common/config/rush/pnpm-lock.yaml

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions packages/canonical-bridge-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @bnb-chain/canonical-bridge-sdk

## 0.4.2

### Patch Changes

- 792cae1: Check Layerzero destination gas limit

## 0.4.1

### Patch Changes
Expand Down
8 changes: 5 additions & 3 deletions packages/canonical-bridge-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bnb-chain/canonical-bridge-sdk",
"version": "0.4.1",
"version": "0.4.2",
"description": "canonical bridge sdk",
"author": "bnb-chain",
"private": false,
Expand All @@ -26,7 +26,8 @@
"scripts": {
"dev": "vite __dev__ --config vite.config.ts --port 3333 --host 0.0.0.0 --open",
"watch": "vite build --watch --emptyOutDir=false",
"build": "vite build"
"build": "vite build",
"test": "jest"
},
"peerDependencies": {
"axios": "^0",
Expand All @@ -43,6 +44,7 @@
"typescript": "^5",
"viem": "~2.21.14",
"vite": "^4.5.0",
"vite-plugin-dts": "^3.6.3"
"vite-plugin-dts": "^3.6.3",
"jest": "~29.7.0"
}
}
150 changes: 150 additions & 0 deletions packages/canonical-bridge-sdk/src/cbridge/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
CBridgeSendRangeInput,
CBridgeTransferConfigs,
CBridgeTransferEstimatedTime,
ICBridgeTokenValidateParams,
ICBridgeTransferConfig,
IGetCBridgeABI,
IGetCBridgeTransferAddressInput,
IGetCBridgeTransferFunction,
Expand All @@ -26,6 +28,7 @@ import { ORIGINAL_TOKEN_VAULT_V2 } from '@/cbridge/abi/originalTokenVaultV2';
import { PEGGED_TOKEN_BRIDGE } from '@/cbridge/abi/peggedTokenBridge';
import { PEGGED_TOKEN_BRIDGE_V2 } from '@/cbridge/abi/peggedTokenBridgeV2';
import { createAdapter } from '@/cbridge/utils/createAdapter';
import { isEvmAddress } from '@/core/utils/address';

export * from './types';

Expand Down Expand Up @@ -364,4 +367,151 @@ export class CBridge {
args: args,
};
}

/**
* validate token and contract information
*/
validateCBridgeToken = async ({
isPegged,
fromChainId,
fromTokenAddress,
fromTokenSymbol,
bridgeAddress,
toChainId,
toTokenAddress,
toTokenSymbol,
amount,
decimals,
cBridgeEndpoint,
}: ICBridgeTokenValidateParams) => {
try {
if (
!fromChainId ||
!toChainId ||
!fromTokenAddress ||
!bridgeAddress ||
!fromTokenSymbol ||
!toTokenAddress ||
!toTokenSymbol ||
!amount ||
!decimals ||
!cBridgeEndpoint
) {
console.log('Failed to get cBridge token address validation params');
console.log('isPegged', isPegged);
console.log('fromChainId', fromChainId);
console.log('fromTokenAddress', fromTokenAddress);
console.log('fromTokenSymbol', fromTokenSymbol);
console.log('bridgeAddress', bridgeAddress);
console.log('toChainId', toChainId);
console.log('toTokenAddress', toTokenAddress);
console.log('toTokenSymbol', toTokenSymbol);
console.log('amount', amount);
console.log('decimals', decimals);
console.log('cBridgeEndpoint', cBridgeEndpoint);
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) => {
const orgToken = pair.org_token.token;
const peggedToken = pair.pegged_token.token;
return (
(pair.pegged_deposit_contract_addr === bridgeAddress &&
pair.org_chain_id === fromChainId &&
orgToken.address === fromTokenAddress &&
orgToken.symbol === fromTokenSymbol &&
orgToken.decimal === decimals &&
peggedToken.address === toTokenAddress &&
peggedToken.symbol === toTokenSymbol &&
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 &&
orgToken.address === toTokenAddress &&
orgToken.symbol === toTokenSymbol &&
pair.org_chain_id === toChainId)
);
});
if (!!peggedToken && peggedToken.length > 0) {
console.log('cBridge pegged token info matched', peggedToken);
return true;
}
console.log('Can not find cBridge pegged info');
console.log('-- isPegged', isPegged);
console.log('-- fromChainId', fromChainId);
console.log('-- fromTokenAddress', fromTokenAddress);
console.log('-- fromTokenSymbol', fromTokenSymbol);
console.log('-- toChainId', toChainId);
console.log('-- toTokenAddress', toTokenAddress);
console.log('-- toTokenSymbol', toTokenSymbol);
console.log('-- bridgeAddress', bridgeAddress);
return false;
} else {
// bridge address
const addressInfo = cBridgeConfig.chains.filter((chain) => {
return (
chain.id === fromChainId &&
chain.contract_addr.toLowerCase() === bridgeAddress.toLowerCase()
);
});
// token info
const fromTokenInfo = cBridgeConfig.chain_token[
fromChainId
].token.filter((t) => {
return (
t.token.address.toLowerCase() === fromTokenAddress.toLowerCase() &&
t.token.symbol === fromTokenSymbol
);
});
const toTokenInfo = cBridgeConfig.chain_token[toChainId].token.filter(
(t) => {
return (
t.token.address.toLowerCase() === toTokenAddress.toLowerCase() &&
t.token.symbol === toTokenSymbol
);
}
);
if (
addressInfo?.length > 0 &&
fromTokenInfo?.length > 0 &&
toTokenInfo?.length > 0
) {
console.log(
'cBridge pool info matched',
addressInfo,
fromTokenInfo,
toTokenInfo
);
return true;
} else {
console.log('Can not find cBridge pool info');
console.log('-- isPegged', isPegged);
console.log('-- fromChainId', fromChainId);
console.log('-- fromTokenAddress', fromTokenAddress);
console.log('-- fromTokenSymbol', fromTokenSymbol);
console.log('-- toChainId', toChainId);
console.log('-- toTokenAddress', toTokenAddress);
console.log('-- toTokenSymbol', toTokenSymbol);
console.log('-- bridgeAddress', bridgeAddress);
return false;
}
}
} catch (error) {
// eslint-disable-next-line no-console
console.log('cBridge token address validation error', error);
return false;
}
};
}
88 changes: 88 additions & 0 deletions packages/canonical-bridge-sdk/src/cbridge/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,91 @@ export interface IGetCBridgeTransferFunction {
isPegged: boolean;
transferType?: 'deposit' | 'withdraw';
}

export interface ICBridgeToken {
token: {
symbol: string;
address: string;
decimal: number;
xfer_disabled: boolean;
display_symbol?: string; /// FOR ETH <=====> WETH
};
name: string;
icon: string;
inbound_lmt: string;
inbound_epoch_cap: string;
transfer_disabled: boolean;
liq_add_disabled: boolean;
liq_rm_disabled: boolean;
liq_agg_rm_src_disabled: boolean;
delay_threshold: string;
delay_period: number;
method?: string;
bridgeAddress?: string; //bridge address for transfer
}

export interface ICBridgeChain {
id: number;
name: string;
icon: string;
block_delay: number;
gas_token_symbol: string;
explore_url: string;
contract_addr: string;
drop_gas_amt: string;
drop_gas_cost_amt: string;
drop_gas_balance_alert: string;
suggested_gas_cost: string;
flat_usd_fee: number;
farming_reward_contract_addr: string;
transfer_agent_contract_addr: string;
disabled: boolean;
}

export interface ICBridgePeggedPairConfig {
org_chain_id: number;
org_token: ICBridgeToken;
pegged_chain_id: number;
pegged_token: ICBridgeToken;
pegged_deposit_contract_addr: string;
pegged_burn_contract_addr: string;
canonical_token_contract_addr: string;
vault_version: number;
bridge_version: number;
migration_peg_burn_contract_addr: string;
}

export interface ICBridgeTransferConfig {
chains: ICBridgeChain[];
chain_token: {
[k: number]: {
token: ICBridgeToken[];
};
};
farming_reward_contract_addr: string;
pegged_pair_configs: ICBridgePeggedPairConfig[];
blocked_bridge_direct: {
symbol: string;
src_chain_id: string;
dst_chain_id: string;
}[];
redirect_to_aggregators_config: {
symbol: string;
src_chain_id: string;
dst_chain_id: string;
}[];
}

export interface ICBridgeTokenValidateParams {
isPegged: boolean;
fromChainId?: number;
fromTokenAddress: `0x${string}`;
fromTokenSymbol: string;
bridgeAddress: `0x${string}`;
toChainId?: number;
toTokenAddress?: `0x${string}`;
toTokenSymbol?: string;
amount: number;
decimals: number;
cBridgeEndpoint: string;
}
43 changes: 43 additions & 0 deletions packages/canonical-bridge-sdk/src/core/utils/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,46 @@ export function isSameAddress(A?: string, B?: string) {
export function isEvmAddress(address?: string) {
return !!address && /^0x[a-f0-9]{40}$/i.test(address);
}

export function isSolanaAddress(address?: string) {
return !!address && /^[1-9A-HJ-NP-Za-km-z]{32,44}$/.test(address);
}

export function isTronAddress(address?: string) {
return !!address && /^T[a-zA-Z0-9]{33}$/.test(address);
}

export const isValidTokenAddress = ({
contractAddress,
chainType,
isSourceChain,
}: {
contractAddress: string;
chainType: string;
isSourceChain: boolean;
}) => {
const fromOrTo = isSourceChain ? 'from' : 'to';
if (chainType === 'evm') {
if (!isEvmAddress(contractAddress)) {
console.log(
`Invalid evm ${fromOrTo} contract address`,
chainType,
contractAddress
);
return false;
}
} else if (chainType === 'solana') {
if (!isSolanaAddress(contractAddress)) {
console.log(
`Invalid solana ${fromOrTo} contract address`,
chainType,
contractAddress
);
return false;
}
} else {
console.log(`Invalid ${fromOrTo} chain type`, chainType, contractAddress);
return false;
}
return true;
};
Loading
Loading