A cross-chain token bridge enables token transfers between separate blockchains (like Ethereum and Binance Smart Chain) by using a burn-and-mint mechanism: when a user sends tokens from one chain, the bridge smart contract burns the tokens on the source chain and emits an event, which a bridge API detects and triggers the destination chain's bridge contract to mint equivalent tokens for the recipient, ensuring the same value is destroyed and recreated without loss.
Build a Cross-Chain Token Bridge: Ethereum & BSC Tutorial
Added:there is a new trend in d5 more and more d5 projects are going cross-chain the two biggest changes for d5 are ethereum and binance smart chain so i thought it'd be interesting to build a bridge between these two chains so that you can transfer tokens back and forth this is not going to be easy but if we can pull it off we'll be able to build cross-chain nt5 project at the bleeding edge of d5 hey if you don't know me i'm julian and i'll eat the blocks i teach blockchain development why do we even need a bridge on the smart contract of erc20 tokens there is a function called transfer that allow you to transfer your tokens to any other address right and since spine and smart chain is based on ethereum its addresses also respect the format of ethereum so can't we just call the transfer function of a token with the address of the recipient on the binance machine and it would work no it wouldn't work if you do this it would transfer the token to the recipient address but on the ethereum blockchain even though ethereum and binance machine share the same technology there are two separate networks completely independent smart contract and ethereum can only interact with the ethereum blockchain and smart contract on binding smart chain can only interact with the binance matching so we will need another approach [Music] we cannot do a real transfer of token from one chain to another however we could create the same token on the other chain and have a system where when you want to do a transfer you destroy a token on one chain and recreate the same token on the other chin we actually use the terms burn and mint instead of creating and destroying economically this would be correct the same value that is destroyed is recreated nothing is lost nothing is added how would that work we are going to have two bridge smart contract one on ethereum and one on binance smart chain we also going to have an api that runs script 24 7. so let's say that someone with tokens on ethereum wants to transfer his tokens to the balance smart chain first the sender sends his token to the bridge smart contract on ethereum which burns the token an event is emitted with the detail of the transfer including the recipient address on binance march and nd amount transferred then our bridge api received the transfer event and send a transaction to the other bridge smart contract on binance matching to mint the same quantity of token that was destroyed on ethereum and send it to the recipient address this is for transfers from ethereum to binance smart chain and of course you can also do transfers in the other direction for that in the bridge api you will need to run a second process to listen to the transfer events from the binance smart chain okay so this is the high level architecture next we are going to start the code this is the code of our bridge in the github repo of the blocks so this is a truffle project and i will show you the smart contracts in the contracts directory so first of all we have two token deploy on binance matching and on ethereum and each of them inherit from token base here so token base is an es20 token and it has two important function mint and burn to create and destroy token and these two functions are protected only the admin of the token is able to call them okay so then we will see the bridge smart contracts will have one bridge deployed on binance marching and one bridge deployed on ethereum and both of them inherit from bridge bays so here we define a couple of variables so there will be an admin address so that will be an address controlled by the bridge api then we'll have a pointer to the es20 token of this chain so if this is the bridge of ethereum this will be the token on the ethereum side otherwise would be the token on abundance much inside then we have none is to avoid to process the same transfer twice i will explain after right work and it works with this mapping also that's a key value stall where we're basically going to store which nodes we already processed then an event for each transfer from which address it is sent what's the recipient address what's the amount the date the nonce and the steps that means if we are burning or minting so then in the constructor we pass the address of the token and we instantiate the admin and token variable and here we have a burn function so when you initiate a transfer that's the function you're going to call with the address of the recipient and the amount and here we are going to burn the token for the center of the transaction for this amount and then we emit a transfer event and we increment the nodes so each transfer will have a unique nonce and after we have another function mint so this is in the other direction where there is an incoming transfer from the other blockchain so this is the bridge api that is going to call this function to the recipient address the amount and the nonce that was used by the other chain so first of all we make sure that this is the admin that called this function so this is going to be the bridge api so this is an important difference between the min function and the burn function the burn function anybody can call it to initiate the transfer but the mint function this is only the bridge api then here we make sure that we haven't already processed this transfer so after we update our mapping so that we cannot process the transfer twice then we are going to mint new token for the recipient for this amount and we emit a transfer event this time we specify that for the step we are minting not burning okay so that's it for the smart contracts next i'm going to explain the code of the bridge api [Music] so for the bridge api the script is inside the scripts folder it's this file if bsc bridge so first we import the json file produced by truffle folder to bridge that's where we're going to find the api and the deploy address then we instantiate two instance of web3 one for ethereum and one for opponents smart chain so here you'll have to replace this with an url pointing to a node on the ring could be public testnet so for that you create an account on infra you create an ethereum project and you copy paste the ring could be url make sure to use the websocket url because we're going to need it to in order to listen to event and then this is the url for the abundance match and test net and of course if you want to do this on ethereum net and binance function magnet you'll have to replace the url here then here you'll have to create an ethereum address and put the private key here so here on this line we import the address on the reps3 instance for binance smart chain so that now we can sign transaction by using this private key then we instantiate a contract object for the bridge on ethereum and the bridge on binance smart chain and then we are going to listen to events on the bridge smart contract on ethereum we're going to listen to the transfer event and here we're going to filter the event with step zero so this is the minting event and every time a new event is received so this callback here is going to be called so we're going to extract the field of the event here then we're going to build a transaction to call the mint method on the branchbot contract on binance machine and we pass it the correct parameter then we're going to get an estimation of the gas price and the gas cost for this transaction then here we're going to build a parameter for this transaction so the sending address the recipient address that's the bridge contract on balance marching then the data so which function we want to call with which argument and gas cost gas price then we actually send a transaction here and we print the transaction hash and also print detail about the transfer okay so that's it for the code of the bridge api next we are going to actually do the transfer with the command line [Music] we're going to do transaction on a test net of ethereum and on the test net of abandoned smart chain so for ethereum we're going to choose the ring to be test net and you do need to get some ring kobe ether and for that you can use their faux set so you put your address there and they're going to send you for free some ring could be ether and you will need to do the same thing for binance matching test net b and b tokens so if you're not familiar with balance match and the native currency is bnb token that's how you pay for a transaction after you will need to add the configuration for the to blockchain in truffle config okay so first here you need to put the mnemonic that you use to create your admin address and here if we scroll down in the network section you're going to define two networks so if testnet and bsc test net and so here you pass you mnemonic that's high you're going to be able to send transaction on behalf of the admin and here you're going to put the url for your ring b node and this means that you're going to generate one address with the mnemonic and after for bsc test net we also have the mnemonic and the url to the network okay so then in the migration folder we also have this migration file so if we are deploying to if testnet first we're going to deploy our token then we gonna mint 1000 token for the first address which is the admin then we're going to deploy the bridge and then once the bridge is deployed we're going to make the bridge the admin of the token so that it has the right to mint and burn token and for binance smart chain this is going to be almost the same thing except that we're not going to mint any token in advance and so what we're going to do is transfer token from ether test net to balance matching testnet okay so here in my terminal i am at the root of the project at first you need to run npm install and after that you're gonna need to run the migration for ether testnet and bsc test net so truffle migrate reset [Music] network if testnet all right so the deployment is successful for the bridge spot contract on ethereum and next we're gonna do the same thing but for binance smart chance so here's this is gonna be bsc test net okay so our bridge is also deployed on binance smart chain so next we're gonna check the token balance on ethereum and binance machine for that i've prepared some script token balance and network if test net so it's going to give us the token balance of the sender should be 1 000 yes okay and for binance marching should be zero oops uh wrong script it's not that one is bsc token balance okay should be zero yes it is so we're going to transfer this 1000 token from ether to finance matching so first we need to create another terminal and we're going to run the bridge api for that we're not going to use the truffle exec command so this is a command that injects some object in your file like web3 connected to the correct network etc but in the case of the bridge this is connected to two different blockchains so it's a little bit more complex so it's just directly a node.js script so node script and here if vsc bridge okay so now this is listening to the transfer event coming from the bridge contract on ethereum and on our other terminal we are going to initiate the transfer so truffle exec script if bsc transfer and the network we send this transaction on ethereum okay let's send a transaction and in the other terminal we can see that the bridge api detected the transfer event and triggered the main function on the bridge smart contract on binance much ends here we can see the transaction hash and it did process the transfer so just to make it simple here i reused the same address on ethereum and on finance machine so that's why from and two are the same and here we can see the amount and the date and now we can check the balance in the other terminals so let's see the token balance on ethereum so it should be zero and now on binance smart chain should be 1000 and it is correct yes i'm super happy we managed to make it work i wasn't sure would be able to figure it out at the beginning one interesting lesson is that from a single code base we can do an integration between different blockchains this is because we did an integration between two blockchains that are evm based but of course if you do an integration between two blockchains that use different technologies it's gonna be a little bit more challenging there are a couple of potential issues with my implementation first it's not 100 decentralized since we rely on the private key of the bridge api users have to trust us and also trust that we will keep the breach safe from hackers another potential issue is how to finance the bridge with my solution the bridge api has to pay for every transaction but we could have a system where the sender of token also has to pay some ether to the bridge contract in order to cover the cost another potential issue is transfer order currently transfer can be processed out of order by the bridge api but it might be better to respect the order of the transfer requests there are many other bridges you can build especially between ethereum and layer 2 chain like matic let me know in the comment down below if you're interested in other tutorials for token bridge that's it for now i will see you in another video
Up Next

Signature Replay Attack in Solidity 0.6: Prevention Techniques
@smartcontractprogrammer
11.1K views•2020-09-20

Torrent File Format & Bencoding: A Technical Deep Dive
@AsliEngineering
12.5K views•2022-08-08

Solidity Timelock Contract Tutorial: Delaying Transactions Securely
@EatTheBlocks
6.7K views•2022-08-02

Understanding Ethereum: A Comprehensive Beginner's Overview
@99Bitcoins
3.1M views•2018-06-26
Related Study Plans & Knowledge Roadmaps
Structured learning paths in Blockchain & Crypto















![[솔리디티 문해력] 2회차 - Solidity 주요 문법 3시간 완성](https://i.ytimg.com/vi_webp/m2z_FrOeG08/sddefault.webp)





















![Altseason Just Went Full SEND! [Next Altcoin Plays]](https://i.ytimg.com/vi/KZFN2aKfYn8/maxresdefault.jpg)

