Cross-Chain & Interoperability
Bridges, cross-chain messaging, interoperability protocols, and multi-chain.
Overview
Cross-chain enables communication between blockchains.
Bridge Types
| Type | Description |
|---|---|
| Lock & Mint | Lock assets, mint wrapped |
| Burn & Mint | Burn on one, mint on other |
| Atomic Swap | Exchange across chains |
| Relay | Cross-chain messages |
Simple Bridge Contract
// Source chain
contract BridgeSource {
mapping(bytes32 => bool) public processed;
event Locked(address user, uint256 amount, uint256 targetChain);
function lock(uint256 amount, uint256 targetChain) public {
require(token.transferFrom(msg.sender, address(this), amount));
bytes32 txHash = keccak256(abi.encodePacked(msg.sender, amount, targetChain, block.timestamp));
processed[txHash] = true;
emit Locked(msg.sender, amount, targetChain);
}
}
// Destination chain
contract BridgeDestination {
mapping(bytes32 => bool) public processed;
function mint(address to, uint256 amount, bytes32 proof) public {
require(!processed[proof]);
require(verifyProof(proof));
processed[proof] = true;
token.mint(to, amount);
}
}
Cross-Chain Messaging
// LayerZero style
contract CrossChainApp {
function sendMessage(uint16 _dstChainId, bytes calldata _message) public {
// Encode message
bytes memory payload = abi.encode(msg.sender, _message);
// Send cross-chain
lzSend(_dstChainId, payload, payable(msg.sender), address(0), "", 0);
}
function lzReceive(uint16 _srcChainId, bytes calldata _message) public {
(address sender, bytes memory message) = abi.decode(_message, (address, bytes));
// Process received message
}
}
Interoperability Protocols
| Protocol | Type |
|---|---|
| LayerZero | Messaging |
| Axelar | Cross-chain |
| Wormhole | Bridge |
| IBC | Cosmos standard |
Best Practices
- Security audits — Bridge vulnerabilities
- Finality checks — Confirm transactions
- Rate limiting — Prevent exploits
- Monitoring — Track bridge activity
Practice
Build a simple token bridge between two chains.