Cross-chain bridges let different blockchains talk to each other. This helps move tokens or data between them easily.
Cross-chain bridges in Blockchain / Solidity
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
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
Blockchain / Solidity
bridge.transfer('Ethereum', 'Polygon', 'USDC', 100, '0xUserAddress')
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'])
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.
Practice
1. What is the main purpose of a
cross-chain bridge in blockchain technology?easy
Solution
Step 1: Understand the role of cross-chain bridges
Cross-chain bridges enable communication and asset transfers between different blockchains.Step 2: Compare options with this role
Only To connect different blockchains and allow asset transfers between them describes connecting blockchains and transferring assets, which matches the bridge's purpose.Final Answer:
To connect different blockchains and allow asset transfers between them -> Option DQuick Check:
Cross-chain bridge = connect blockchains [OK]
Hint: Bridges connect blockchains to move tokens or data [OK]
Common Mistakes:
- Confusing bridges with mining or block creation
- Thinking bridges create new cryptocurrencies
- Assuming bridges only increase block size
2. Which of the following is the correct basic step in a cross-chain bridge operation?
easy
Solution
Step 1: Recall how cross-chain bridges work
They lock tokens on the source chain to prevent double spending and mint equivalent tokens on the destination chain.Step 2: Match this with the options
Lock tokens on the source chain and mint equivalent tokens on the destination chain correctly describes locking on source and minting on destination, which is the standard process.Final Answer:
Lock tokens on the source chain and mint equivalent tokens on the destination chain -> Option AQuick Check:
Lock then mint = bridge step [OK]
Hint: Tokens are locked first, then minted on another chain [OK]
Common Mistakes:
- Mixing up minting and burning order
- Thinking tokens transfer directly without locking
- Assuming minting happens on source chain
3. Consider this simplified pseudocode for a cross-chain bridge function:
function bridgeTransfer(amount, sourceChain, destChain) {
lockTokens(sourceChain, amount);
mintTokens(destChain, amount);
return 'Transfer complete';
}
What will be the output when calling bridgeTransfer(100, 'ChainA', 'ChainB')?medium
Solution
Step 1: Analyze the function steps
The function calls lockTokens and mintTokens, then returns the string 'Transfer complete'.Step 2: Determine the output of the function call
Assuming lockTokens and mintTokens work correctly, the function returns 'Transfer complete'.Final Answer:
'Transfer complete' -> Option AQuick Check:
Function returns 'Transfer complete' [OK]
Hint: Look at the return statement for output [OK]
Common Mistakes:
- Confusing function calls with return value
- Assuming intermediate functions print output
- Ignoring the return statement
4. The following code snippet is intended to lock tokens on the source chain and mint on the destination chain, but it has a bug:
function bridgeTransfer(amount, sourceChain, destChain) {
lockTokens(destChain, amount);
mintTokens(sourceChain, amount);
return 'Transfer complete';
}
What is the bug in this code?medium
Solution
Step 1: Check the order of locking and minting
Tokens should be locked on the source chain and minted on the destination chain.Step 2: Identify the argument mismatch
The code locks tokens on destChain and mints on sourceChain, which is reversed.Final Answer:
The lockTokens and mintTokens calls have swapped chain arguments -> Option BQuick Check:
Lock on source, mint on destination [OK]
Hint: Lock on source chain, mint on destination chain [OK]
Common Mistakes:
- Swapping source and destination chains
- Forgetting to return a value
- Using wrong function names
5. You want to design a cross-chain bridge that prevents double spending by ensuring tokens are locked before minting on the destination chain. Which approach best achieves this in a smart contract environment?
hard
Solution
Step 1: Understand double spending prevention
Tokens must be locked on the source chain before minting on the destination chain to avoid duplicates.Step 2: Evaluate approaches for enforcing this
Using an event listener to confirm locking before minting ensures the correct order and security.Final Answer:
Use an event listener to confirm tokens are locked on source chain before minting on destination chain -> Option CQuick Check:
Confirm lock event before minting [OK]
Hint: Confirm lock event before minting tokens [OK]
Common Mistakes:
- Minting before locking causes double spending
- Allowing mint without lock breaks security
- Skipping locking or minting steps
