async function bridgeWithValidation(bridgeProtocol, targetChain, recipient, token, amount) {
try {
// Validate chain
const supportedChains = [
'ethereum', 'arbitrum', 'optimism', 'polygon', 'berachain', 'ink',
'plasma', 'conflux', 'corn', 'avalanche', 'celo', 'flare', 'hyperevm', 'mantle', 'megaeth',
'monad', 'morph', 'rootstock', 'sei', 'stable', 'unichain', 'xlayer',
'solana', 'ton', 'tron'
]
if (!supportedChains.includes(targetChain)) {
throw new Error('Chain not supported')
}
// Validate recipient format by target chain family
if (['solana'].includes(targetChain)) {
if (!/^[1-9A-HJ-NP-Za-km-z]{32,44}$/.test(recipient)) {
throw new Error('Invalid Solana recipient address')
}
} else if (['ton', 'tron'].includes(targetChain)) {
if (!recipient || recipient.length < 10) {
throw new Error('Invalid non-EVM recipient address')
}
} else if (!recipient.startsWith('0x') || recipient.length !== 42) {
throw new Error('Invalid EVM recipient address')
}
if (!token.startsWith('0x') || token.length !== 42) {
throw new Error('Invalid token address')
}
// Get quote first
const quote = await bridgeProtocol.quoteBridge({
targetChain,
recipient,
token,
amount
})
console.log('Bridge quote:')
console.log(' Fee:', quote.fee, 'wei')
console.log(' Bridge fee:', quote.bridgeFee, 'wei')
// Check if fees are acceptable
if (quote.fee + quote.bridgeFee > 1000000000000000n) {
throw new Error('Fees too high')
}
// Execute bridge
const result = await bridgeProtocol.bridge({
targetChain,
recipient,
token,
amount
})
console.log('Bridge successful:', result.hash)
return result
} catch (error) {
console.error('Bridge validation failed:', error.message)
throw error
}
}