3

I understand what a bridge does but I 'm interested in knowing how it works under the hood. How does it work when transferring assets from one network to another. Lets take for example the ethereum to matic polygon bridge and say I want to transfer an ERC-20 token from Ethereum to Matic:

How do the smart contracts interact because I'm assuming the L1 smart contract can't send a signal to the L2 smart contract on Matic for the token transfer. Is it that there exists some code not written in a smart contract programming language e.g. Solidity but something off the chain that poll's the L1 smart contract to see if there's been a token transfer on the smart contract bridge and then creates a new transaction on the L2 bridge to complete the transaction ? Because I'm assuming a smart contract will not be able to automate the full process of transferring assets there has to be some middleman (software) that looks at the L1 contract to determine if there has been a transfer and then that software triggers the L2 contract to send the token transfer to the user on the matic network ?

3 Answers 3

4

Since Blockchains are basically closed silos of publicly stored information. They cannot communicate with the outside world using HTTP or other data transfer protocols directly. Someone has to handle the listening and reaction of the events for deposits/withdrawals between chains either handled by centralized operators called relayers that handle the liquidity transfer between bridged tokens usually for a fee.

There are 2 ways that we usually see bridges used now:

  1. Create 2 contracts on 2 chains. 1.1. When a user wants to move tokens to the secondary chain, you lock their tokens in mainchain contract 1.2. A compiled software or script usually listens off-chain (you choose Node.js/GoLang/Python/etc) for deposit events that are triggered when locking tokens on mainchain.
    1.3. Once the deposit is caught by the off-chain trusted (whitelister/relayer/messenger) that now mints the equivalent amount of tokens on the second chain. 1.4. If you want to withdraw your tokens from the secondary chain, you burn your tokens on the secondary chain, then the off-chain service unlocks the equivalent burned of tokens and transfers them to the users mainchain address.

  2. The contract structure is the same the only difference here is that you burn tokens on mainchain and mint on secondary chain.

  3. BONUS: Blockchains could hardcode in their monolitical code their own Trusted Relayer or they are moving to more decentralized relaying solutions like the Chainlink API and Chainlink Keepers that automatically listen and run functions when conditions are met either on-chain or off-chain. Hope that clears the basic methodology but it's still a big mess on every chain because you get a different contract to address for every new token pair. Go on Coinmarketcap and under every token you will see a bridge with different addresses. In theory, I imagine that we could get a deterministic address if we use the create2() function to get the same address for a token between all EVM-compatible chains. Most today deploy a proxy contract or use a 3rd party provider to do it for them but it's still hard to create deterministic addresses for the token that is bridged. But since everyone is biased when they write code, achieving a singular address for the token between chains might turn out to be a big burden to handle bridges without a proxy.

7
  • "mints the equivalent amount of tokens" how does this work? I assumed for example in Eth, it is only miners that can mint tokens. So how would a minting of token occur using a bridge instead of a miner? Jun 17, 2022 at 12:21
  • 1
    Bridges are centralized atm, someone has to listen for minting/burning events between chains and act accordingly, they are mostly labeled as relayers but they are infact whitelisted inside the Bridge Smart Contract that listen and reacts on user bridge transfer events. Chainlink is working on a more decentralized Bridge that will offer safer handling for token bridges.
    – Liberalite
    Jun 17, 2022 at 17:44
  • I get. But I am still missing how a new token can just be manufactured from "thin air" on the other chain to represent the chain that was burned on the other chain. Jun 19, 2022 at 10:48
  • 1
    You basically have to create and maintain a mapping between the 2 tokens, one on mainnet and the other one on the layer 2 sidechain. If the token has adoption and people start to use it inside their protocols then it will be added to let's say Binance/Coinmarketcap whitelisted token list and they will add a pricefeed to it.
    – Liberalite
    Jun 24, 2022 at 11:13
  • What if you're not dealing with 1:1 Ether and its layer-2 equivalent like Polygon, but instead NFTs such that the bridge is agnostic to the myriad of NFT collections (contracts) that users want to have migrated through the bridge? Can the complexity of the protocol remain at 1:1 somehow?
    – user610620
    Dec 9, 2022 at 16:36
1

Each L2 it differently. For example, Optimism has smart contracts on the L1 called messengers that relay data (bridge) between L1 to L2: https://community.optimism.io/docs/developers/bridge/standard-bridge.html

0

give me 1 eth on my wallet i will give you matic on you erc20 wallet it's all.

After all bridge work with smart contract for the security tranfert.

2
  • Yes I understand this but how does it work at the smart contract level. I'm assuming there are smart contract I'm interacting with when I transfer my assets. I.e. it's not some python code on the other end. It's a solidity smart contract taking in my funds then outputting them on the other chain
    – rjs
    Jan 16, 2022 at 16:15
  • 1
    yes you right for me after each bridge will work differently exemple try to see the doc on docs.allbridge.io you will see all contract from each blockchain : docs.allbridge.io/allbridge-overview/bridge-contracts
    – karl block
    Jan 16, 2022 at 16:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.