0
0
Blockchain / Solidityprogramming~5 mins

Cross-chain bridges in Blockchain / Solidity

Choose your learning style9 modes available
Introduction

Cross-chain bridges let different blockchains talk to each other. This helps move tokens or data between them easily.

You want to send tokens from Ethereum to Binance Smart Chain.
You need to use a dApp on another blockchain without selling your assets.
You want to combine features from two blockchains in one app.
You want to trade tokens that exist on different blockchains.
You want to increase your options for decentralized finance (DeFi) across chains.
Syntax
Blockchain / Solidity
bridge.transfer(fromChain, toChain, asset, amount, userAddress)

fromChain: The blockchain you send assets from.

toChain: The blockchain you send assets to.

Examples
Sends 100 USDC tokens from Ethereum to Polygon for the user.
Blockchain / Solidity
bridge.transfer('Ethereum', 'Polygon', 'USDC', 100, '0xUserAddress')
Sends 50 BNB tokens from Binance Smart Chain to Avalanche.
Blockchain / Solidity
bridge.transfer('BinanceSmartChain', 'Avalanche', 'BNB', 50, '0xUserAddress')
Sample Program

This simple program shows moving USDC tokens from Ethereum to Polygon. It checks if enough tokens exist, then updates balances and prints the result.

Blockchain / Solidity
class SimpleBridge:
    def __init__(self):
        self.balances = {
            'Ethereum': {'USDC': 1000},
            'Polygon': {'USDC': 200}
        }

    def transfer(self, fromChain, toChain, asset, amount, user):
        if self.balances.get(fromChain, {}).get(asset, 0) < amount:
            print(f"Not enough {asset} on {fromChain} to transfer.")
            return
        self.balances[fromChain][asset] -= amount
        self.balances.setdefault(toChain, {}).setdefault(asset, 0)
        self.balances[toChain][asset] += amount
        print(f"Transferred {amount} {asset} from {fromChain} to {toChain} for {user}.")

bridge = SimpleBridge()
bridge.transfer('Ethereum', 'Polygon', 'USDC', 150, '0xUserAddress')
print('Ethereum balance:', bridge.balances['Ethereum']['USDC'])
print('Polygon balance:', bridge.balances['Polygon']['USDC'])
OutputSuccess
Important Notes

Cross-chain bridges often lock tokens on one chain and mint wrapped tokens on another.

Security is important because bridges can be targets for hacks.

Some bridges use validators or smart contracts to confirm transfers.

Summary

Cross-chain bridges connect different blockchains to move assets or data.

They help users use tokens and apps across multiple blockchains.

Bridges work by locking tokens on one chain and releasing or minting on another.